github.com/iotexproject/iotex-core@v1.14.1-rc1/api/web3server_marshal_test.go (about) 1 package api 2 3 import ( 4 "encoding/hex" 5 "encoding/json" 6 "math/big" 7 "testing" 8 "time" 9 10 "github.com/ethereum/go-ethereum/core/types" 11 "github.com/iotexproject/go-pkgs/crypto" 12 "github.com/iotexproject/go-pkgs/hash" 13 "github.com/iotexproject/iotex-address/address" 14 "github.com/iotexproject/iotex-proto/golang/iotextypes" 15 "github.com/stretchr/testify/require" 16 17 "github.com/iotexproject/iotex-core/action" 18 "github.com/iotexproject/iotex-core/blockchain/block" 19 "github.com/iotexproject/iotex-core/pkg/unit" 20 "github.com/iotexproject/iotex-core/test/identityset" 21 ) 22 23 var ( 24 _testContractIoAddr = "io1ryygckqjw06720cg9j6tkwtprxu4jgcag4w6vn" 25 _testSenderIoAddr, _ = address.FromString("io154mvzs09vkgn0hw6gg3ayzw5w39jzp47f8py9v") 26 _testBlkHash, _ = hash.HexStringToHash256("c4aace64c1f4d7c0b6ebe74ba01e00e27c7ff4b2552c36ef617f38f0f2b1ebb3") 27 _testTxHash, _ = hash.HexStringToHash256("25bef7a7e20402a625973613b19bbc1793ed3a38cad270abf623222120a10fd0") 28 29 _testTopic1, _ = hash.HexStringToHash256("ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef") 30 _testTopic2, _ = hash.HexStringToHash256("0000000000000000000000008a68e01add9adc8b887025dc54c36cfa91432f58") 31 _testTopic3, _ = hash.HexStringToHash256("000000000000000000000000567ff65f8b4bec33b9925cad6f7ec3c45ac79b26") 32 33 _testPubKey, _ = crypto.HexStringToPublicKey("04e9f906040bf6f1df25d6fff3f36f6aa135060ff54acf96564a2a298e469ac7162c78564903d4cf39d976493b44906a2bb553997e12b747439d173adcd02d6552") 34 ) 35 36 func TestWeb3ResponseMarshal(t *testing.T) { 37 require := require.New(t) 38 39 t.Run("Result", func(t *testing.T) { 40 res, err := json.Marshal(&web3Response{ 41 id: 1, 42 result: false, 43 err: nil, 44 }) 45 require.NoError(err) 46 require.JSONEq(` 47 { 48 "jsonrpc":"2.0", 49 "id":1, 50 "result":false 51 } 52 `, string(res)) 53 }) 54 55 t.Run("Error", func(t *testing.T) { 56 res, err := json.Marshal(&web3Response{ 57 id: 1, 58 result: nil, 59 err: errInvalidBlock, 60 }) 61 require.NoError(err) 62 require.JSONEq(` 63 { 64 "jsonrpc":"2.0", 65 "id":1, 66 "error":{ 67 "code":-32603, 68 "message":"invalid block" 69 } 70 } 71 `, string(res)) 72 }) 73 } 74 75 func TestBlockObjectMarshal(t *testing.T) { 76 require := require.New(t) 77 78 var ( 79 receiptRoot, _ = hex.DecodeString("0c26064b778ca775ed2f4220882ce20ced34f806ceb5edf67a7fb4cdb7b1a5dc") 80 deltaStateDigest, _ = hex.DecodeString("900d80ab3bb6d12a98ae177268610b28a14c9ef84fb891a9c809d6d863d79cd3") 81 previousBlockHash, _ = hex.DecodeString("1f20ad92a25748c2459aa6820a4fe5a25a1c57702045f4ab910dc88df5a04fce") 82 ) 83 tsf, err := action.NewExecution(action.EmptyAddress, 84 uint64(2), 85 unit.ConvertIotxToRau(1000), 86 21000, 87 unit.ConvertIotxToRau(1), 88 []byte{}, 89 ) 90 require.NoError(err) 91 evlp := (&action.EnvelopeBuilder{}). 92 SetAction(tsf). 93 SetGasLimit(tsf.GasLimit()). 94 SetGasPrice(tsf.GasPrice()). 95 SetNonce(2). 96 SetVersion(1). 97 Build() 98 sevlp, err := action.Sign(evlp, identityset.PrivateKey(24)) 99 require.NoError(err) 100 ra := (&block.RunnableActionsBuilder{}).AddActions([]*action.SealedEnvelope{sevlp}...).Build() 101 blk, err := block.NewBuilder(ra). 102 SetHeight(uint64(1)). 103 SetTimestamp(time.Date(2011, 1, 26, 0, 0, 0, 0, time.UTC)). 104 SetVersion(1). 105 SetReceiptRoot(hash.BytesToHash256(receiptRoot)). 106 SetDeltaStateDigest(hash.BytesToHash256(deltaStateDigest)). 107 SetPrevBlockHash(hash.BytesToHash256(previousBlockHash)). 108 SignAndBuild(identityset.PrivateKey(28)) 109 require.NoError(err) 110 txHash, err := sevlp.Hash() 111 require.NoError(err) 112 blk.Receipts = []*action.Receipt{{ 113 Status: 1, 114 BlockHeight: 1, 115 ActionHash: txHash, 116 GasConsumed: 21000, 117 ContractAddress: _testContractIoAddr, 118 TxIndex: 1, 119 }} 120 121 t.Run("BlockWithoutDetail", func(t *testing.T) { 122 res, err := json.Marshal(&getBlockResult{ 123 blk: &blk, 124 transactions: []interface{}{string("0x2133ee7ff4562535166e3f16fd7407c19e5ed1acd036f78d3528a5a40e40ad42")}, 125 }) 126 require.NoError(err) 127 require.JSONEq(` 128 { 129 "author":"0x1e14d5373E1AF9Cc77F0032aD2cd0FBA8be5Ea2e", 130 "number":"0x1", 131 "hash":"0x2584c5383cf2f9ac36cda227e092935dc3d410e349d5e815300a3c7d148e0b79", 132 "parentHash":"0x1f20ad92a25748c2459aa6820a4fe5a25a1c57702045f4ab910dc88df5a04fce", 133 "sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347", 134 "logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", 135 "transactionsRoot":"0x8e281592f4d4585e195c1d6c499b891f856c55e77aa2880e61d27755fe09186f", 136 "stateRoot":"0x900d80ab3bb6d12a98ae177268610b28a14c9ef84fb891a9c809d6d863d79cd3", 137 "receiptsRoot":"0x0c26064b778ca775ed2f4220882ce20ced34f806ceb5edf67a7fb4cdb7b1a5dc", 138 "miner":"0x1e14d5373E1AF9Cc77F0032aD2cd0FBA8be5Ea2e", 139 "difficulty":"0xfffffffffffffffffffffffffffffffe", 140 "totalDifficulty":"0xff14700000000000000000000000486001d72", 141 "extraData":"0x", 142 "size":"0x1", 143 "gasLimit":"0x5208", 144 "gasUsed":"0x5208", 145 "timestamp":"0x4d3f6400", 146 "transactions":[ 147 "0x2133ee7ff4562535166e3f16fd7407c19e5ed1acd036f78d3528a5a40e40ad42" 148 ], 149 "step":"373422302", 150 "uncles":[ 151 152 ] 153 } 154 `, string(res)) 155 }) 156 157 t.Run("BlockWithDetail", func(t *testing.T) { 158 raw := types.NewTx(&types.LegacyTx{ 159 Nonce: 2, 160 Value: unit.ConvertIotxToRau(1000), 161 Gas: 21000, 162 GasPrice: unit.ConvertIotxToRau(1), 163 Data: []byte{}, 164 }) 165 signer, err := action.NewEthSigner(iotextypes.Encoding_ETHEREUM_EIP155, 0) 166 require.NoError(err) 167 ethTx, err := action.RawTxToSignedTx(raw, signer, sevlp.Signature()) 168 require.NoError(err) 169 tx := &getTransactionResult{ 170 blockHash: &_testBlkHash, 171 to: nil, 172 ethTx: ethTx, 173 receipt: blk.Receipts[0], 174 pubkey: sevlp.SrcPubkey(), 175 } 176 res, err := json.Marshal(&getBlockResult{ 177 blk: &blk, 178 transactions: []interface{}{tx}, 179 }) 180 require.NoError(err) 181 require.JSONEq(` 182 { 183 "author":"0x1e14d5373E1AF9Cc77F0032aD2cd0FBA8be5Ea2e", 184 "number":"0x1", 185 "hash":"0x2584c5383cf2f9ac36cda227e092935dc3d410e349d5e815300a3c7d148e0b79", 186 "parentHash":"0x1f20ad92a25748c2459aa6820a4fe5a25a1c57702045f4ab910dc88df5a04fce", 187 "sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347", 188 "logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", 189 "transactionsRoot":"0x8e281592f4d4585e195c1d6c499b891f856c55e77aa2880e61d27755fe09186f", 190 "stateRoot":"0x900d80ab3bb6d12a98ae177268610b28a14c9ef84fb891a9c809d6d863d79cd3", 191 "receiptsRoot":"0x0c26064b778ca775ed2f4220882ce20ced34f806ceb5edf67a7fb4cdb7b1a5dc", 192 "miner":"0x1e14d5373E1AF9Cc77F0032aD2cd0FBA8be5Ea2e", 193 "difficulty":"0xfffffffffffffffffffffffffffffffe", 194 "totalDifficulty":"0xff14700000000000000000000000486001d72", 195 "extraData":"0x", 196 "size":"0x1", 197 "gasLimit":"0x5208", 198 "gasUsed":"0x5208", 199 "timestamp":"0x4d3f6400", 200 "transactions":[ 201 { 202 "hash":"0x8e281592f4d4585e195c1d6c499b891f856c55e77aa2880e61d27755fe09186f", 203 "nonce":"0x2", 204 "blockHash":"0xc4aace64c1f4d7c0b6ebe74ba01e00e27c7ff4b2552c36ef617f38f0f2b1ebb3", 205 "blockNumber":"0x1", 206 "transactionIndex":"0x1", 207 "from":"0x2b5e18f6f541dce2b7d6c19203f886cf93319c61", 208 "to":null, 209 "value":"0x3635c9adc5dea00000", 210 "gasPrice":"0xde0b6b3a7640000", 211 "gas":"0x5208", 212 "input":"0x", 213 "r":"0xd7b08f7b37c7fac89a2d0b819225f459cfab6d6d14307893ce711269c26c1c67", 214 "s":"0x5f9b182b55d50734447f0859b7261701b0195357048d902a723dcb40616ebdda", 215 "v":"0x1c" 216 } 217 ], 218 "step":"373422302", 219 "uncles":[ 220 221 ] 222 } 223 `, string(res)) 224 }) 225 } 226 227 func TestTransactionObjectMarshal(t *testing.T) { 228 require := require.New(t) 229 230 receipt := &action.Receipt{ 231 Status: 1, 232 BlockHeight: 16, 233 ActionHash: _testTxHash, 234 GasConsumed: 21000, 235 ContractAddress: _testContractIoAddr, 236 TxIndex: 1, 237 } 238 239 t.Run("ContractCreation", func(t *testing.T) { 240 raw := types.NewTx(&types.LegacyTx{ 241 Nonce: 1, 242 Value: big.NewInt(10), 243 Gas: 21000, 244 GasPrice: big.NewInt(0), 245 Data: []byte{}, 246 }) 247 sig, _ := hex.DecodeString("363964383961306166323764636161363766316236326133383335393464393735393961616464326237623136346362343131326161386464666434326638391b") 248 signer, err := action.NewEthSigner(iotextypes.Encoding_ETHEREUM_EIP155, 4690) 249 require.NoError(err) 250 tx, err := action.RawTxToSignedTx(raw, signer, sig) 251 require.NoError(err) 252 res, err := json.Marshal(&getTransactionResult{ 253 blockHash: &_testBlkHash, 254 to: nil, 255 ethTx: tx, 256 receipt: receipt, 257 pubkey: _testPubKey, 258 }) 259 require.NoError(err) 260 require.JSONEq(` 261 { 262 "hash":"0x25bef7a7e20402a625973613b19bbc1793ed3a38cad270abf623222120a10fd0", 263 "nonce":"0x1", 264 "blockHash":"0xc4aace64c1f4d7c0b6ebe74ba01e00e27c7ff4b2552c36ef617f38f0f2b1ebb3", 265 "blockNumber":"0x10", 266 "transactionIndex":"0x1", 267 "from":"0x0666dba65b0ef88d11cdcbe857ffb6618310dcfa", 268 "to":null, 269 "value":"0xa", 270 "gasPrice":"0x0", 271 "gas":"0x5208", 272 "input":"0x", 273 "r":"0x3639643839613061663237646361613637663162363261333833353934643937", 274 "s":"0x3539396161646432623762313634636234313132616138646466643432663839", 275 "v":"0x24c7" 276 } 277 `, string(res)) 278 }) 279 t.Run("ISSUE3932", func(t *testing.T) { 280 sig, _ := hex.DecodeString("00d50c3169003e7c9c9392069a1c9e5251c8f558120cceb9aa312f0fa46247701f4cad47a5540cdda5459f7a46c62df752ca588e0b73722ec767ff08994134a41b") 281 blkHash, _ := hash.HexStringToHash256("bdcf722cbc0dc9b9eb5b46ad4af9e15944a886310b1cb2227129070c0e4b7de5") 282 contract := "0xc3527348de07d591c9d567ce1998efa2031b8675" 283 data, _ := hex.DecodeString("1fad948c0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000ec4daee51c4bf81bd00af165f8ca66823ee3b12a000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000003e24491a4f2a946e30baf624fda6b4484d106c12000000000000000000000000000000000000000000000000000000000000005b000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001800000000000000000000000000000000000000000000000000000000000009fd90000000000000000000000000000000000000000000000000000000000011da4000000000000000000000000000000000000000000000000000000000000db26000000000000000000000000000000000000000000000000000000e8d4a51000000000000000000000000000000000000000000000000000000000e8d4a510000000000000000000000000000000000000000000000000000000000000000240000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000084b61d27f6000000000000000000000000065e1164818487818e6ba714e8d80b91718ad75800000000000000000000000000000000000000000000000000038d7ea4c6800000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000095ccc7012efb2e65aa31752f3ac01e23817c08a47500000000000000000000000000000000000000000000000000000000650af9f8000000000000000000000000000000000000000000000000000000006509a878b5acba7277159ae6fa661ed1988cc10ac2c96c58dc332bde2a6dc0d8531ea3924d9d04cda681c271411250ae7d9e9aea47661dba67a66f08d19804a255e45c561b0000000000000000000000000000000000000000000000000000000000000000000000000000000000004160daa88165299ca7e585d5d286cee98b54397b57ac704b74331a48d67651195322ef3884c7d60023333f2542a07936f34edc9efa3cbd19e8cd0f8972c54171a21b00000000000000000000000000000000000000000000000000000000000000") 284 pubkey, _ := crypto.HexStringToPublicKey("04806b217cb0b6a675974689fd99549e525d967287eee9a62dc4e598eea981b8158acfe026da7bf58397108abd0607672832c28ef3bc7b5855077f6e67ab5fc096") 285 actHash, _ := hash.HexStringToHash256("cbc2560d986d79a46bfd96a08d18c6045b29f97352c1360289e371d9cffd6b6a") 286 raw := types.NewTx(&types.LegacyTx{ 287 Nonce: 305, 288 Value: big.NewInt(0), 289 Gas: 297131, 290 GasPrice: big.NewInt(1000000000000), 291 Data: data, 292 }) 293 signer, err := action.NewEthSigner(iotextypes.Encoding_ETHEREUM_EIP155, 0) 294 require.NoError(err) 295 tx, err := action.RawTxToSignedTx(raw, signer, sig) 296 require.NoError(err) 297 res, err := json.Marshal(&getTransactionResult{ 298 blockHash: &blkHash, 299 to: &contract, 300 ethTx: tx, 301 receipt: &action.Receipt{ 302 Status: 1, 303 BlockHeight: 22354907, 304 ActionHash: actHash, 305 GasConsumed: 196223, 306 ContractAddress: "", 307 TxIndex: 0, 308 }, 309 pubkey: pubkey, 310 }) 311 require.NoError(err) 312 require.JSONEq(` 313 { 314 "hash": "0xcbc2560d986d79a46bfd96a08d18c6045b29f97352c1360289e371d9cffd6b6a", 315 "nonce": "0x131", 316 "blockHash": "0xbdcf722cbc0dc9b9eb5b46ad4af9e15944a886310b1cb2227129070c0e4b7de5", 317 "blockNumber": "0x1551bdb", 318 "transactionIndex": "0x0", 319 "from": "0xec4daee51c4bf81bd00af165f8ca66823ee3b12a", 320 "to": "0xc3527348de07d591c9d567ce1998efa2031b8675", 321 "value": "0x0", 322 "gasPrice": "0xe8d4a51000", 323 "gas": "0x488ab", 324 "input": "0x1fad948c0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000ec4daee51c4bf81bd00af165f8ca66823ee3b12a000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000003e24491a4f2a946e30baf624fda6b4484d106c12000000000000000000000000000000000000000000000000000000000000005b000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001800000000000000000000000000000000000000000000000000000000000009fd90000000000000000000000000000000000000000000000000000000000011da4000000000000000000000000000000000000000000000000000000000000db26000000000000000000000000000000000000000000000000000000e8d4a51000000000000000000000000000000000000000000000000000000000e8d4a510000000000000000000000000000000000000000000000000000000000000000240000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000084b61d27f6000000000000000000000000065e1164818487818e6ba714e8d80b91718ad75800000000000000000000000000000000000000000000000000038d7ea4c6800000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000095ccc7012efb2e65aa31752f3ac01e23817c08a47500000000000000000000000000000000000000000000000000000000650af9f8000000000000000000000000000000000000000000000000000000006509a878b5acba7277159ae6fa661ed1988cc10ac2c96c58dc332bde2a6dc0d8531ea3924d9d04cda681c271411250ae7d9e9aea47661dba67a66f08d19804a255e45c561b0000000000000000000000000000000000000000000000000000000000000000000000000000000000004160daa88165299ca7e585d5d286cee98b54397b57ac704b74331a48d67651195322ef3884c7d60023333f2542a07936f34edc9efa3cbd19e8cd0f8972c54171a21b00000000000000000000000000000000000000000000000000000000000000", 325 "r": "0xd50c3169003e7c9c9392069a1c9e5251c8f558120cceb9aa312f0fa4624770", 326 "s": "0x1f4cad47a5540cdda5459f7a46c62df752ca588e0b73722ec767ff08994134a4", 327 "v": "0x1b" 328 } 329 `, string(res)) 330 }) 331 t.Run("PendingTransaction", func(t *testing.T) { 332 sig, _ := hex.DecodeString("00d50c3169003e7c9c9392069a1c9e5251c8f558120cceb9aa312f0fa46247701f4cad47a5540cdda5459f7a46c62df752ca588e0b73722ec767ff08994134a41b") 333 contract := "0xc3527348de07d591c9d567ce1998efa2031b8675" 334 data, _ := hex.DecodeString("1fad948c0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000ec4daee51c4bf81bd00af165f8ca66823ee3b12a000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000003e24491a4f2a946e30baf624fda6b4484d106c12000000000000000000000000000000000000000000000000000000000000005b000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001800000000000000000000000000000000000000000000000000000000000009fd90000000000000000000000000000000000000000000000000000000000011da4000000000000000000000000000000000000000000000000000000000000db26000000000000000000000000000000000000000000000000000000e8d4a51000000000000000000000000000000000000000000000000000000000e8d4a510000000000000000000000000000000000000000000000000000000000000000240000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000084b61d27f6000000000000000000000000065e1164818487818e6ba714e8d80b91718ad75800000000000000000000000000000000000000000000000000038d7ea4c6800000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000095ccc7012efb2e65aa31752f3ac01e23817c08a47500000000000000000000000000000000000000000000000000000000650af9f8000000000000000000000000000000000000000000000000000000006509a878b5acba7277159ae6fa661ed1988cc10ac2c96c58dc332bde2a6dc0d8531ea3924d9d04cda681c271411250ae7d9e9aea47661dba67a66f08d19804a255e45c561b0000000000000000000000000000000000000000000000000000000000000000000000000000000000004160daa88165299ca7e585d5d286cee98b54397b57ac704b74331a48d67651195322ef3884c7d60023333f2542a07936f34edc9efa3cbd19e8cd0f8972c54171a21b00000000000000000000000000000000000000000000000000000000000000") 335 pubkey, _ := crypto.HexStringToPublicKey("04806b217cb0b6a675974689fd99549e525d967287eee9a62dc4e598eea981b8158acfe026da7bf58397108abd0607672832c28ef3bc7b5855077f6e67ab5fc096") 336 raw := types.NewTx(&types.LegacyTx{ 337 Nonce: 305, 338 Value: big.NewInt(0), 339 Gas: 297131, 340 GasPrice: big.NewInt(1000000000000), 341 Data: data, 342 }) 343 signer, err := action.NewEthSigner(iotextypes.Encoding_ETHEREUM_EIP155, 0) 344 require.NoError(err) 345 tx, err := action.RawTxToSignedTx(raw, signer, sig) 346 require.NoError(err) 347 res, err := json.Marshal(&getTransactionResult{ 348 blockHash: nil, 349 to: &contract, 350 ethTx: tx, 351 receipt: nil, 352 pubkey: pubkey, 353 }) 354 require.NoError(err) 355 require.JSONEq(` 356 { 357 "hash": "0xd5de34d026608b4453218f5125f8e22afd6f56710ad9da75d1ac5df127118add", 358 "nonce": "0x131", 359 "blockHash": null, 360 "blockNumber": null, 361 "transactionIndex": null, 362 "from": "0xec4daee51c4bf81bd00af165f8ca66823ee3b12a", 363 "to": "0xc3527348de07d591c9d567ce1998efa2031b8675", 364 "value": "0x0", 365 "gasPrice": "0xe8d4a51000", 366 "gas": "0x488ab", 367 "input": "0x1fad948c0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000ec4daee51c4bf81bd00af165f8ca66823ee3b12a000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000003e24491a4f2a946e30baf624fda6b4484d106c12000000000000000000000000000000000000000000000000000000000000005b000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001800000000000000000000000000000000000000000000000000000000000009fd90000000000000000000000000000000000000000000000000000000000011da4000000000000000000000000000000000000000000000000000000000000db26000000000000000000000000000000000000000000000000000000e8d4a51000000000000000000000000000000000000000000000000000000000e8d4a510000000000000000000000000000000000000000000000000000000000000000240000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000084b61d27f6000000000000000000000000065e1164818487818e6ba714e8d80b91718ad75800000000000000000000000000000000000000000000000000038d7ea4c6800000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000095ccc7012efb2e65aa31752f3ac01e23817c08a47500000000000000000000000000000000000000000000000000000000650af9f8000000000000000000000000000000000000000000000000000000006509a878b5acba7277159ae6fa661ed1988cc10ac2c96c58dc332bde2a6dc0d8531ea3924d9d04cda681c271411250ae7d9e9aea47661dba67a66f08d19804a255e45c561b0000000000000000000000000000000000000000000000000000000000000000000000000000000000004160daa88165299ca7e585d5d286cee98b54397b57ac704b74331a48d67651195322ef3884c7d60023333f2542a07936f34edc9efa3cbd19e8cd0f8972c54171a21b00000000000000000000000000000000000000000000000000000000000000", 368 "r": "0xd50c3169003e7c9c9392069a1c9e5251c8f558120cceb9aa312f0fa4624770", 369 "s": "0x1f4cad47a5540cdda5459f7a46c62df752ca588e0b73722ec767ff08994134a4", 370 "v": "0x1b" 371 } 372 `, string(res)) 373 }) 374 } 375 376 func TestReceiptObjectMarshal(t *testing.T) { 377 require := require.New(t) 378 379 receipt := &action.Receipt{ 380 Status: 1, 381 BlockHeight: 16, 382 ActionHash: _testTxHash, 383 GasConsumed: 21000, 384 ContractAddress: _testContractIoAddr, 385 TxIndex: 1, 386 } 387 388 t.Run("ContractCreation", func(t *testing.T) { 389 contractEthaddr, _ := ioAddrToEthAddr(_testContractIoAddr) 390 res, err := json.Marshal(&getReceiptResult{ 391 blockHash: _testBlkHash, 392 from: _testSenderIoAddr, 393 to: nil, 394 contractAddress: &contractEthaddr, 395 logsBloom: "00008000000100000400000000000040000000000000000000000000000000000000000001000200000400000000000000000000001000000000000000001000000000001000000000200000004000000000000000000101000000000000000008000008000208000000000000400000000000000000000000000000000000000000080010000000000200010000000000000500000000000000000000000000004080000000000000001000000800020000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000400000000000000000000000000080000400010200000", 396 receipt: receipt, 397 }) 398 require.NoError(err) 399 require.JSONEq(` 400 { 401 "transactionIndex":"0x1", 402 "transactionHash":"0x25bef7a7e20402a625973613b19bbc1793ed3a38cad270abf623222120a10fd0", 403 "blockHash":"0xc4aace64c1f4d7c0b6ebe74ba01e00e27c7ff4b2552c36ef617f38f0f2b1ebb3", 404 "blockNumber":"0x10", 405 "from":"0xa576c141e5659137ddda4223d209d4744b2106be", 406 "to":null, 407 "cumulativeGasUsed":"0x5208", 408 "gasUsed":"0x5208", 409 "contractAddress":"0x19088c581273F5E53f082CB4BB396119b959231D", 410 "logsBloom":"0x00008000000100000400000000000040000000000000000000000000000000000000000001000200000400000000000000000000001000000000000000001000000000001000000000200000004000000000000000000101000000000000000008000008000208000000000000400000000000000000000000000000000000000000080010000000000200010000000000000500000000000000000000000000004080000000000000001000000800020000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000400000000000000000000000000080000400010200000", 411 "logs":[ 412 413 ], 414 "status":"0x1" 415 } 416 `, string(res)) 417 }) 418 419 t.Run("ContractExecution", func(t *testing.T) { 420 receipt.AddLogs(&action.Log{ 421 Address: _testContractIoAddr, 422 Topics: action.Topics{_testTopic1, _testTopic2, _testTopic3}, 423 Data: []byte("test"), 424 BlockHeight: 16, 425 ActionHash: _testTxHash, 426 Index: 3, 427 TxIndex: 1, 428 NotFixTopicCopyBug: false, 429 }) 430 contractEthaddr, _ := ioAddrToEthAddr(_testContractIoAddr) 431 res, err := json.Marshal(&getReceiptResult{ 432 blockHash: _testBlkHash, 433 from: _testSenderIoAddr, 434 to: &contractEthaddr, 435 contractAddress: nil, 436 logsBloom: "00008000000100000400000000000040000000000000000000000000000000000000000001000200000400000000000000000000001000000000000000001000000000001000000000200000004000000000000000000101000000000000000008000008000208000000000000400000000000000000000000000000000000000000080010000000000200010000000000000500000000000000000000000000004080000000000000001000000800020000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000400000000000000000000000000080000400010200000", 437 receipt: receipt, 438 }) 439 require.NoError(err) 440 require.JSONEq(` 441 { 442 "transactionIndex":"0x1", 443 "transactionHash":"0x25bef7a7e20402a625973613b19bbc1793ed3a38cad270abf623222120a10fd0", 444 "blockHash":"0xc4aace64c1f4d7c0b6ebe74ba01e00e27c7ff4b2552c36ef617f38f0f2b1ebb3", 445 "blockNumber":"0x10", 446 "from":"0xa576c141e5659137ddda4223d209d4744b2106be", 447 "to":"0x19088c581273F5E53f082CB4BB396119b959231D", 448 "cumulativeGasUsed":"0x5208", 449 "gasUsed":"0x5208", 450 "contractAddress":null, 451 "logsBloom":"0x00008000000100000400000000000040000000000000000000000000000000000000000001000200000400000000000000000000001000000000000000001000000000001000000000200000004000000000000000000101000000000000000008000008000208000000000000400000000000000000000000000000000000000000080010000000000200010000000000000500000000000000000000000000004080000000000000001000000800020000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000400000000000000000000000000080000400010200000", 452 "logs":[ 453 { 454 "removed":false, 455 "logIndex":"0x3", 456 "transactionIndex":"0x1", 457 "transactionHash":"0x25bef7a7e20402a625973613b19bbc1793ed3a38cad270abf623222120a10fd0", 458 "blockHash":"0xc4aace64c1f4d7c0b6ebe74ba01e00e27c7ff4b2552c36ef617f38f0f2b1ebb3", 459 "blockNumber":"0x10", 460 "address":"0x19088c581273F5E53f082CB4BB396119b959231D", 461 "data":"0x74657374", 462 "topics":[ 463 "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", 464 "0x0000000000000000000000008a68e01add9adc8b887025dc54c36cfa91432f58", 465 "0x000000000000000000000000567ff65f8b4bec33b9925cad6f7ec3c45ac79b26" 466 ] 467 } 468 ], 469 "status":"0x1" 470 } 471 `, string(res)) 472 }) 473 } 474 475 func TestLogsObjectMarshal(t *testing.T) { 476 require := require.New(t) 477 478 res, err := json.Marshal(&getLogsResult{ 479 blockHash: _testBlkHash, 480 log: &action.Log{ 481 Address: _testContractIoAddr, 482 Topics: action.Topics{_testTopic1, _testTopic2, _testTopic3}, 483 Data: []byte("test"), 484 BlockHeight: 2, 485 ActionHash: _testTxHash, 486 Index: 3, 487 TxIndex: 1, 488 NotFixTopicCopyBug: false, 489 }, 490 }) 491 require.NoError(err) 492 require.JSONEq(` 493 { 494 "removed":false, 495 "transactionIndex":"0x1", 496 "logIndex":"0x3", 497 "transactionHash":"0x25bef7a7e20402a625973613b19bbc1793ed3a38cad270abf623222120a10fd0", 498 "blockHash":"0xc4aace64c1f4d7c0b6ebe74ba01e00e27c7ff4b2552c36ef617f38f0f2b1ebb3", 499 "blockNumber":"0x2", 500 "address":"0x19088c581273F5E53f082CB4BB396119b959231D", 501 "data":"0x74657374", 502 "topics":[ 503 "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", 504 "0x0000000000000000000000008a68e01add9adc8b887025dc54c36cfa91432f58", 505 "0x000000000000000000000000567ff65f8b4bec33b9925cad6f7ec3c45ac79b26" 506 ] 507 } 508 `, string(res)) 509 } 510 511 func TestStreamResponseMarshal(t *testing.T) { 512 require := require.New(t) 513 514 res, err := json.Marshal(&streamResponse{ 515 id: "0xcd0c3e8af590364c09d0fa6a1210faf5", 516 result: &getLogsResult{ 517 blockHash: _testBlkHash, 518 log: &action.Log{ 519 Address: _testContractIoAddr, 520 Topics: action.Topics{_testTopic1, _testTopic2, _testTopic3}, 521 Data: []byte("test"), 522 BlockHeight: 2, 523 ActionHash: _testTxHash, 524 Index: 3, 525 TxIndex: 1, 526 NotFixTopicCopyBug: false, 527 }, 528 }}) 529 require.NoError(err) 530 require.JSONEq(` 531 { 532 "jsonrpc":"2.0", 533 "method":"eth_subscription", 534 "params":{ 535 "subscription":"0xcd0c3e8af590364c09d0fa6a1210faf5", 536 "result":{ 537 "removed":false, 538 "logIndex":"0x3", 539 "transactionIndex":"0x1", 540 "transactionHash":"0x25bef7a7e20402a625973613b19bbc1793ed3a38cad270abf623222120a10fd0", 541 "blockHash":"0xc4aace64c1f4d7c0b6ebe74ba01e00e27c7ff4b2552c36ef617f38f0f2b1ebb3", 542 "blockNumber":"0x2", 543 "address":"0x19088c581273F5E53f082CB4BB396119b959231D", 544 "data":"0x74657374", 545 "topics":[ 546 "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", 547 "0x0000000000000000000000008a68e01add9adc8b887025dc54c36cfa91432f58", 548 "0x000000000000000000000000567ff65f8b4bec33b9925cad6f7ec3c45ac79b26" 549 ] 550 } 551 } 552 } 553 `, string(res)) 554 }