github.com/klaytn/klaytn@v1.10.2/api/api_ethereum_test.go (about) 1 package api 2 3 import ( 4 "bytes" 5 "context" 6 "encoding/json" 7 "errors" 8 "fmt" 9 "math/big" 10 "reflect" 11 "testing" 12 13 "github.com/stretchr/testify/require" 14 15 "github.com/klaytn/klaytn/crypto" 16 "github.com/klaytn/klaytn/governance" 17 18 "github.com/golang/mock/gomock" 19 "github.com/klaytn/klaytn/accounts" 20 mock_accounts "github.com/klaytn/klaytn/accounts/mocks" 21 mock_api "github.com/klaytn/klaytn/api/mocks" 22 "github.com/klaytn/klaytn/blockchain" 23 "github.com/klaytn/klaytn/blockchain/types" 24 "github.com/klaytn/klaytn/blockchain/types/accountkey" 25 "github.com/klaytn/klaytn/common" 26 "github.com/klaytn/klaytn/common/hexutil" 27 "github.com/klaytn/klaytn/consensus/mocks" 28 "github.com/klaytn/klaytn/networks/rpc" 29 "github.com/klaytn/klaytn/params" 30 "github.com/klaytn/klaytn/rlp" 31 "github.com/klaytn/klaytn/storage/database" 32 "github.com/stretchr/testify/assert" 33 ) 34 35 var dummyChainConfigForEthereumAPITest = ¶ms.ChainConfig{ 36 ChainID: new(big.Int).SetUint64(111111), 37 IstanbulCompatibleBlock: new(big.Int).SetUint64(0), 38 LondonCompatibleBlock: new(big.Int).SetUint64(0), 39 EthTxTypeCompatibleBlock: new(big.Int).SetUint64(0), 40 UnitPrice: 25000000000, // 25 ston 41 } 42 43 // TestEthereumAPI_Etherbase tests Etherbase. 44 func TestEthereumAPI_Etherbase(t *testing.T) { 45 testNodeAddress(t, "Etherbase") 46 } 47 48 // TestEthereumAPI_Coinbase tests Coinbase. 49 func TestEthereumAPI_Coinbase(t *testing.T) { 50 testNodeAddress(t, "Coinbase") 51 } 52 53 // testNodeAddress generates nodeAddress and tests Etherbase and Coinbase. 54 func testNodeAddress(t *testing.T, testAPIName string) { 55 gov := governance.NewMixedEngineNoInit( 56 dummyChainConfigForEthereumAPITest, 57 database.NewMemoryDBManager(), 58 ) 59 key, _ := crypto.GenerateKey() 60 nodeAddress := crypto.PubkeyToAddress(key.PublicKey) 61 gov.SetNodeAddress(nodeAddress) 62 63 api := EthereumAPI{publicGovernanceAPI: governance.NewGovernanceAPI(gov)} 64 results := reflect.ValueOf(&api).MethodByName(testAPIName).Call([]reflect.Value{}) 65 result, ok := results[0].Interface().(common.Address) 66 assert.True(t, ok) 67 assert.Equal(t, nodeAddress, result) 68 } 69 70 // TestEthereumAPI_Hashrate tests Hasharate. 71 func TestEthereumAPI_Hashrate(t *testing.T) { 72 api := &EthereumAPI{} 73 assert.Equal(t, hexutil.Uint64(ZeroHashrate), api.Hashrate()) 74 } 75 76 // TestEthereumAPI_Mining tests Mining. 77 func TestEthereumAPI_Mining(t *testing.T) { 78 api := &EthereumAPI{} 79 assert.Equal(t, false, api.Mining()) 80 } 81 82 // TestEthereumAPI_GetWork tests GetWork. 83 func TestEthereumAPI_GetWork(t *testing.T) { 84 api := &EthereumAPI{} 85 _, err := api.GetWork() 86 assert.Equal(t, errNoMiningWork, err) 87 } 88 89 // TestEthereumAPI_SubmitWork tests SubmitWork. 90 func TestEthereumAPI_SubmitWork(t *testing.T) { 91 api := &EthereumAPI{} 92 assert.Equal(t, false, api.SubmitWork(BlockNonce{}, common.Hash{}, common.Hash{})) 93 } 94 95 // TestEthereumAPI_SubmitHashrate tests SubmitHashrate. 96 func TestEthereumAPI_SubmitHashrate(t *testing.T) { 97 api := &EthereumAPI{} 98 assert.Equal(t, false, api.SubmitHashrate(hexutil.Uint64(0), common.Hash{})) 99 } 100 101 // TestEthereumAPI_GetHashrate tests GetHashrate. 102 func TestEthereumAPI_GetHashrate(t *testing.T) { 103 api := &EthereumAPI{} 104 assert.Equal(t, ZeroHashrate, api.GetHashrate()) 105 } 106 107 // TestEthereumAPI_GetUncleByBlockNumberAndIndex tests GetUncleByBlockNumberAndIndex. 108 func TestEthereumAPI_GetUncleByBlockNumberAndIndex(t *testing.T) { 109 api := &EthereumAPI{} 110 uncleBlock, err := api.GetUncleByBlockNumberAndIndex(context.Background(), rpc.BlockNumber(0), hexutil.Uint(0)) 111 assert.NoError(t, err) 112 assert.Nil(t, uncleBlock) 113 } 114 115 // TestEthereumAPI_GetUncleByBlockHashAndIndex tests GetUncleByBlockHashAndIndex. 116 func TestEthereumAPI_GetUncleByBlockHashAndIndex(t *testing.T) { 117 api := &EthereumAPI{} 118 uncleBlock, err := api.GetUncleByBlockHashAndIndex(context.Background(), common.Hash{}, hexutil.Uint(0)) 119 assert.NoError(t, err) 120 assert.Nil(t, uncleBlock) 121 } 122 123 // TestTestEthereumAPI_GetUncleCountByBlockNumber tests GetUncleCountByBlockNumber. 124 func TestTestEthereumAPI_GetUncleCountByBlockNumber(t *testing.T) { 125 mockCtrl, mockBackend, api := testInitForEthApi(t) 126 block, _, _, _, _ := createTestData(t, nil) 127 128 // For existing block number, it must return 0. 129 mockBackend.EXPECT().BlockByNumber(gomock.Any(), gomock.Any()).Return(block, nil) 130 existingBlockNumber := rpc.BlockNumber(block.Number().Int64()) 131 assert.Equal(t, hexutil.Uint(ZeroUncleCount), *api.GetUncleCountByBlockNumber(context.Background(), existingBlockNumber)) 132 133 // For non-existing block number, it must return nil. 134 mockBackend.EXPECT().BlockByNumber(gomock.Any(), gomock.Any()).Return(nil, nil) 135 nonExistingBlockNumber := rpc.BlockNumber(5) 136 uncleCount := api.GetUncleCountByBlockNumber(context.Background(), nonExistingBlockNumber) 137 uintNil := hexutil.Uint(uint(0)) 138 expectedResult := &uintNil 139 expectedResult = nil 140 assert.Equal(t, expectedResult, uncleCount) 141 142 mockCtrl.Finish() 143 } 144 145 // TestTestEthereumAPI_GetUncleCountByBlockHash tests GetUncleCountByBlockHash. 146 func TestTestEthereumAPI_GetUncleCountByBlockHash(t *testing.T) { 147 mockCtrl, mockBackend, api := testInitForEthApi(t) 148 block, _, _, _, _ := createTestData(t, nil) 149 150 // For existing block hash, it must return 0. 151 mockBackend.EXPECT().BlockByHash(gomock.Any(), gomock.Any()).Return(block, nil) 152 existingHash := block.Hash() 153 assert.Equal(t, hexutil.Uint(ZeroUncleCount), *api.GetUncleCountByBlockHash(context.Background(), existingHash)) 154 155 // For non-existing block hash, it must return nil. 156 mockBackend.EXPECT().BlockByHash(gomock.Any(), gomock.Any()).Return(nil, nil) 157 nonExistingHash := block.Hash() 158 uncleCount := api.GetUncleCountByBlockHash(context.Background(), nonExistingHash) 159 uintNil := hexutil.Uint(uint(0)) 160 expectedResult := &uintNil 161 expectedResult = nil 162 assert.Equal(t, expectedResult, uncleCount) 163 164 mockCtrl.Finish() 165 } 166 167 // TestEthereumAPI_GetHeaderByNumber tests GetHeaderByNumber. 168 func TestEthereumAPI_GetHeaderByNumber(t *testing.T) { 169 testGetHeader(t, "GetHeaderByNumber", true) 170 } 171 172 // TestEthereumAPI_GetHeaderByHash tests GetHeaderByNumber. 173 func TestEthereumAPI_GetHeaderByHash(t *testing.T) { 174 testGetHeader(t, "GetHeaderByHash", true) 175 } 176 177 // TestEthereumAPI_GetHeaderByNumber tests GetHeaderByNumber. 178 func TestEthereumAPI_GetHeaderByNumber_BeforeEnableFork(t *testing.T) { 179 testGetHeader(t, "GetHeaderByNumber", false) 180 } 181 182 // TestEthereumAPI_GetHeaderByHash tests GetHeaderByNumber. 183 func TestEthereumAPI_GetHeaderByHash_BeforeEnableFork(t *testing.T) { 184 testGetHeader(t, "GetHeaderByHash", false) 185 } 186 187 // testGetHeader generates data to test GetHeader related functions in EthereumAPI 188 // and actually tests the API function passed as a parameter. 189 func testGetHeader(t *testing.T, testAPIName string, forkEnabled bool) { 190 mockCtrl, mockBackend, api := testInitForEthApi(t) 191 192 // Creates a MockEngine. 193 mockEngine := mocks.NewMockEngine(mockCtrl) 194 // GetHeader APIs calls internally below methods. 195 mockBackend.EXPECT().Engine().Return(mockEngine) 196 if forkEnabled { 197 mockBackend.EXPECT().ChainConfig().Return(dummyChainConfigForEthereumAPITest) 198 } else { 199 chainConfigForNotCompatibleEthBlock := ¶ms.ChainConfig{ 200 ChainID: dummyChainConfigForEthereumAPITest.ChainID, 201 IstanbulCompatibleBlock: dummyChainConfigForEthereumAPITest.IstanbulCompatibleBlock, 202 LondonCompatibleBlock: dummyChainConfigForEthereumAPITest.LondonCompatibleBlock, 203 EthTxTypeCompatibleBlock: nil, 204 UnitPrice: dummyChainConfigForEthereumAPITest.UnitPrice, 205 } 206 mockBackend.EXPECT().ChainConfig().Return(chainConfigForNotCompatibleEthBlock) 207 } 208 209 // Author is called when calculates miner field of Header. 210 dummyMiner := common.HexToAddress("0x9712f943b296758aaae79944ec975884188d3a96") 211 mockEngine.EXPECT().Author(gomock.Any()).Return(dummyMiner, nil) 212 var dummyTotalDifficulty uint64 = 5 213 mockBackend.EXPECT().GetTd(gomock.Any()).Return(new(big.Int).SetUint64(dummyTotalDifficulty)) 214 215 // Create dummy header 216 header := types.CopyHeader(&types.Header{ 217 ParentHash: common.HexToHash("0xc8036293065bacdfce87debec0094a71dbbe40345b078d21dcc47adb4513f348"), 218 Rewardbase: common.Address{}, 219 TxHash: common.HexToHash("0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"), 220 Root: common.HexToHash("0xad31c32942fa033166e4ef588ab973dbe26657c594de4ba98192108becf0fec9"), 221 ReceiptHash: common.HexToHash("0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"), 222 Bloom: types.Bloom{}, 223 BlockScore: new(big.Int).SetUint64(1), 224 Number: new(big.Int).SetUint64(4), 225 GasUsed: uint64(10000), 226 Time: new(big.Int).SetUint64(1641363540), 227 TimeFoS: uint8(85), 228 Extra: common.Hex2Bytes("0xd983010701846b6c617988676f312e31362e338664617277696e000000000000f89ed5949712f943b296758aaae79944ec975884188d3a96b8415a0614be7fd5ea40f11ce558e02993bd55f11ae72a3cfbc861875a57483ec5ec3adda3e5845fd7ab271d670c755480f9ef5b8dd731f4e1f032fff5d165b763ac01f843b8418867d3733167a0c737fa5b62dcc59ec3b0af5748bcc894e7990a0b5a642da4546713c9127b3358cdfe7894df1ca1db5a97560599986d7f1399003cd63660b98200"), 229 Governance: []byte{}, 230 Vote: []byte{}, 231 }) 232 233 var blockParam interface{} 234 switch testAPIName { 235 case "GetHeaderByNumber": 236 blockParam = rpc.BlockNumber(header.Number.Uint64()) 237 mockBackend.EXPECT().HeaderByNumber(gomock.Any(), gomock.Any()).Return(header, nil) 238 case "GetHeaderByHash": 239 blockParam = header.Hash() 240 mockBackend.EXPECT().HeaderByHash(gomock.Any(), gomock.Any()).Return(header, nil) 241 } 242 243 results := reflect.ValueOf(&api).MethodByName(testAPIName).Call( 244 []reflect.Value{ 245 reflect.ValueOf(context.Background()), 246 reflect.ValueOf(blockParam), 247 }, 248 ) 249 ethHeader, ok := results[0].Interface().(map[string]interface{}) 250 assert.Equal(t, true, ok) 251 assert.NotEqual(t, ethHeader, nil) 252 253 // TODO-klaytn size set to 0x214 254 // We can get a real mashaled data by using real backend instance, not mock 255 // Mock just return a header instance, not rlp decoded json data 256 expected := make(map[string]interface{}) 257 assert.NoError(t, json.Unmarshal([]byte(` 258 { 259 "jsonrpc": "2.0", 260 "id": 1, 261 "result": { 262 "difficulty": "0x1", 263 "extraData": "0x", 264 "gasLimit": "0xe8d4a50fff", 265 "gasUsed": "0x2710", 266 "hash": "0xd6d5d8295f612bc824762f1945f4271c73aee9306bcf91e151d269369526ba60", 267 "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", 268 "miner": "0x9712f943b296758aaae79944ec975884188d3a96", 269 "mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000", 270 "nonce": "0x0000000000000000", 271 "number": "0x4", 272 "parentHash": "0xc8036293065bacdfce87debec0094a71dbbe40345b078d21dcc47adb4513f348", 273 "receiptsRoot": "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470", 274 "sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347", 275 "size": "0x214", 276 "stateRoot": "0xad31c32942fa033166e4ef588ab973dbe26657c594de4ba98192108becf0fec9", 277 "timestamp": "0x61d53854", 278 "totalDifficulty": "0x5", 279 "transactionsRoot": "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470" 280 } 281 } 282 `), &expected)) 283 if forkEnabled { 284 expected["baseFeePerGas"] = "0x0" 285 } 286 checkEthereumBlockOrHeaderFormat(t, expected, ethHeader) 287 } 288 289 // TestEthereumAPI_GetBlockByNumber tests GetBlockByNumber. 290 func TestEthereumAPI_GetBlockByNumber(t *testing.T) { 291 testGetBlock(t, "GetBlockByNumber", false) 292 testGetBlock(t, "GetBlockByNumber", true) 293 } 294 295 // TestEthereumAPI_GetBlockByHash tests GetBlockByHash. 296 func TestEthereumAPI_GetBlockByHash(t *testing.T) { 297 testGetBlock(t, "GetBlockByHash", false) 298 testGetBlock(t, "GetBlockByHash", true) 299 } 300 301 // testGetBlock generates data to test GetBlock related functions in EthereumAPI 302 // and actually tests the API function passed as a parameter. 303 func testGetBlock(t *testing.T, testAPIName string, fullTxs bool) { 304 mockCtrl, mockBackend, api := testInitForEthApi(t) 305 306 // Creates a MockEngine. 307 mockEngine := mocks.NewMockEngine(mockCtrl) 308 // GetHeader APIs calls internally below methods. 309 mockBackend.EXPECT().Engine().Return(mockEngine) 310 mockBackend.EXPECT().ChainConfig().Return(dummyChainConfigForEthereumAPITest) 311 // Author is called when calculates miner field of Header. 312 dummyMiner := common.HexToAddress("0x9712f943b296758aaae79944ec975884188d3a96") 313 mockEngine.EXPECT().Author(gomock.Any()).Return(dummyMiner, nil) 314 var dummyTotalDifficulty uint64 = 5 315 mockBackend.EXPECT().GetTd(gomock.Any()).Return(new(big.Int).SetUint64(dummyTotalDifficulty)) 316 317 // Create dummy header 318 header := types.CopyHeader(&types.Header{ 319 ParentHash: common.HexToHash("0xc8036293065bacdfce87debec0094a71dbbe40345b078d21dcc47adb4513f348"), Rewardbase: common.Address{}, TxHash: common.HexToHash("0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"), 320 Root: common.HexToHash("0xad31c32942fa033166e4ef588ab973dbe26657c594de4ba98192108becf0fec9"), 321 ReceiptHash: common.HexToHash("0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"), 322 Bloom: types.Bloom{}, 323 BlockScore: new(big.Int).SetUint64(1), 324 Number: new(big.Int).SetUint64(4), 325 GasUsed: uint64(10000), 326 Time: new(big.Int).SetUint64(1641363540), 327 TimeFoS: uint8(85), 328 Extra: common.Hex2Bytes("0xd983010701846b6c617988676f312e31362e338664617277696e000000000000f89ed5949712f943b296758aaae79944ec975884188d3a96b8415a0614be7fd5ea40f11ce558e02993bd55f11ae72a3cfbc861875a57483ec5ec3adda3e5845fd7ab271d670c755480f9ef5b8dd731f4e1f032fff5d165b763ac01f843b8418867d3733167a0c737fa5b62dcc59ec3b0af5748bcc894e7990a0b5a642da4546713c9127b3358cdfe7894df1ca1db5a97560599986d7f1399003cd63660b98200"), 329 Governance: []byte{}, 330 Vote: []byte{}, 331 }) 332 block, _, _, _, _ := createTestData(t, header) 333 var blockParam interface{} 334 switch testAPIName { 335 case "GetBlockByNumber": 336 blockParam = rpc.BlockNumber(block.NumberU64()) 337 mockBackend.EXPECT().BlockByNumber(gomock.Any(), gomock.Any()).Return(block, nil) 338 case "GetBlockByHash": 339 blockParam = block.Hash() 340 mockBackend.EXPECT().BlockByHash(gomock.Any(), gomock.Any()).Return(block, nil) 341 } 342 343 results := reflect.ValueOf(&api).MethodByName(testAPIName).Call( 344 []reflect.Value{ 345 reflect.ValueOf(context.Background()), 346 reflect.ValueOf(blockParam), 347 reflect.ValueOf(fullTxs), 348 }, 349 ) 350 ethBlock, ok := results[0].Interface().(map[string]interface{}) 351 assert.Equal(t, true, ok) 352 assert.NotEqual(t, ethBlock, nil) 353 354 expected := make(map[string]interface{}) 355 if fullTxs { 356 assert.NoError(t, json.Unmarshal([]byte(` 357 { 358 "jsonrpc": "2.0", 359 "id": 1, 360 "result": { 361 "baseFeePerGas": "0x0", 362 "difficulty": "0x1", 363 "extraData": "0x", 364 "gasLimit": "0xe8d4a50fff", 365 "gasUsed": "0x2710", 366 "hash": "0xc74d8c04d4d2f2e4ed9cd1731387248367cea7f149731b7a015371b220ffa0fb", 367 "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", 368 "miner": "0x9712f943b296758aaae79944ec975884188d3a96", 369 "mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000", 370 "nonce": "0x0000000000000000", 371 "number": "0x4", 372 "parentHash": "0xc8036293065bacdfce87debec0094a71dbbe40345b078d21dcc47adb4513f348", 373 "receiptsRoot": "0xf6278dd71ffc1637f78dc2ee54f6f9e64d4b1633c1179dfdbc8c3b482efbdbec", 374 "sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347", 375 "size": "0xe44", 376 "stateRoot": "0xad31c32942fa033166e4ef588ab973dbe26657c594de4ba98192108becf0fec9", 377 "timestamp": "0x61d53854", 378 "totalDifficulty": "0x5", 379 "transactions": [ 380 { 381 "blockHash": "0xc74d8c04d4d2f2e4ed9cd1731387248367cea7f149731b7a015371b220ffa0fb", 382 "blockNumber": "0x4", 383 "from": "0x0000000000000000000000000000000000000000", 384 "gas": "0x1c9c380", 385 "gasPrice": "0x5d21dba00", 386 "hash": "0x6231f24f79d28bb5b8425ce577b3b77cd9c1ab766fcfc5233358a2b1c2f4ff70", 387 "input": "0x3078653331393765386630303030303030303030303030303030303030303030303065306265663939623461323232383665323736333062343835643036633561313437636565393331303030303030303030303030303030303030303030303030313538626566663863386364656264363436353461646435663661316439393337653733353336633030303030303030303030303030303030303030303030303030303030303030303030303030303030303030323962623565376662366265616533326366383030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030306530303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303138303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030316236306662343631346132326530303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303031303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030343030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303031353862656666386338636465626436343635346164643566366131643939333765373335333663303030303030303030303030303030303030303030303030373462613033313938666564326231356135316166323432623963363366616633633866346433343030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303033303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030", 388 "nonce": "0x0", 389 "to": "0x3736346135356338333362313038373730343930", 390 "transactionIndex": "0x0", 391 "value": "0x0", 392 "type": "0x0", 393 "v": "0x1", 394 "r": "0x2", 395 "s": "0x3" 396 }, 397 { 398 "blockHash": "0xc74d8c04d4d2f2e4ed9cd1731387248367cea7f149731b7a015371b220ffa0fb", 399 "blockNumber": "0x4", 400 "from": "0x3036656164333031646165616636376537376538", 401 "gas": "0x989680", 402 "gasPrice": "0x5d21dba00", 403 "hash": "0xf146858415c060eae65a389cbeea8aeadc79461038fbee331ffd97b41279dd63", 404 "input": "0x", 405 "nonce": "0x1", 406 "to": "0x3364613566326466626334613262333837316462", 407 "transactionIndex": "0x1", 408 "value": "0x5", 409 "type": "0x0", 410 "v": "0x1", 411 "r": "0x2", 412 "s": "0x3" 413 }, 414 { 415 "blockHash": "0xc74d8c04d4d2f2e4ed9cd1731387248367cea7f149731b7a015371b220ffa0fb", 416 "blockNumber": "0x4", 417 "from": "0x3730323366383135666136613633663761613063", 418 "gas": "0x1312d00", 419 "gasPrice": "0x5d21dba00", 420 "hash": "0x0a01fc67bb4c15c32fa43563c0fcf05cd5bf2fdcd4ec78122b5d0295993bca24", 421 "input": "0x68656c6c6f", 422 "nonce": "0x2", 423 "to": "0x3336623562313539333066323466653862616538", 424 "transactionIndex": "0x2", 425 "value": "0x3", 426 "type": "0x0", 427 "v": "0x1", 428 "r": "0x2", 429 "s": "0x3" 430 }, 431 { 432 "blockHash": "0xc74d8c04d4d2f2e4ed9cd1731387248367cea7f149731b7a015371b220ffa0fb", 433 "blockNumber": "0x4", 434 "from": "0x3936663364636533666637396132333733653330", 435 "gas": "0x1312d00", 436 "gasPrice": "0x5d21dba00", 437 "hash": "0x486f7561375c38f1627264f8676f92ec0dd1c4a7c52002ba8714e61fcc6bb649", 438 "input": "0x", 439 "nonce": "0x3", 440 "to": "0x3936663364636533666637396132333733653330", 441 "transactionIndex": "0x3", 442 "value": "0x0", 443 "type": "0x0", 444 "v": "0x1", 445 "r": "0x2", 446 "s": "0x3" 447 }, 448 { 449 "blockHash": "0xc74d8c04d4d2f2e4ed9cd1731387248367cea7f149731b7a015371b220ffa0fb", 450 "blockNumber": "0x4", 451 "from": "0x3936663364636533666637396132333733653330", 452 "gas": "0x5f5e100", 453 "gasPrice": "0x5d21dba00", 454 "hash": "0xbd3e57cd31dd3d6679326f7a949f0de312e9ae53bec5ef3c23b43a5319c220a4", 455 "input": "0x", 456 "nonce": "0x4", 457 "to": null, 458 "transactionIndex": "0x4", 459 "value": "0x0", 460 "type": "0x0", 461 "v": "0x1", 462 "r": "0x2", 463 "s": "0x3" 464 }, 465 { 466 "blockHash": "0xc74d8c04d4d2f2e4ed9cd1731387248367cea7f149731b7a015371b220ffa0fb", 467 "blockNumber": "0x4", 468 "from": "0x3936663364636533666637396132333733653330", 469 "gas": "0x2faf080", 470 "gasPrice": "0x5d21dba00", 471 "hash": "0xff666129a0c7227b17681d668ecdef5d6681fc93dbd58856eea1374880c598b0", 472 "input": "0x", 473 "nonce": "0x5", 474 "to": "0x3632323232656162393565396564323963346266", 475 "transactionIndex": "0x5", 476 "value": "0x0", 477 "type": "0x0", 478 "v": "0x1", 479 "r": "0x2", 480 "s": "0x3" 481 }, 482 { 483 "blockHash": "0xc74d8c04d4d2f2e4ed9cd1731387248367cea7f149731b7a015371b220ffa0fb", 484 "blockNumber": "0x4", 485 "from": "0x3936663364636533666637396132333733653330", 486 "gas": "0x2faf080", 487 "gasPrice": "0x5d21dba00", 488 "hash": "0xa8ad4f295f2acff9ef56b476b1c52ecb74fb3fd95a789d768c2edb3376dbeacf", 489 "input": "0x", 490 "nonce": "0x6", 491 "to": "0x3936663364636533666637396132333733653330", 492 "transactionIndex": "0x6", 493 "value": "0x0", 494 "type": "0x0", 495 "v": "0x1", 496 "r": "0x2", 497 "s": "0x3" 498 }, 499 { 500 "blockHash": "0xc74d8c04d4d2f2e4ed9cd1731387248367cea7f149731b7a015371b220ffa0fb", 501 "blockNumber": "0x4", 502 "from": "0x3936663364636533666637396132333733653330", 503 "gas": "0x2faf080", 504 "gasPrice": "0x5d21dba00", 505 "hash": "0x47dbfd201fc1dd4188fd2003c6328a09bf49414be607867ca3a5d63573aede93", 506 "input": "0xf8ad80b8aaf8a8a0072409b14b96f9d7dbf4788dbc68c5d30bd5fac1431c299e0ab55c92e70a28a4a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a00000000000000000000000000000000000000000000000000000000000000000a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a00000000000000000000000000000000000000000000000000000000000000000808080", 507 "nonce": "0x7", 508 "to": "0x3936663364636533666637396132333733653330", 509 "transactionIndex": "0x7", 510 "value": "0x0", 511 "type": "0x0", 512 "v": "0x1", 513 "r": "0x2", 514 "s": "0x3" 515 }, 516 { 517 "blockHash": "0xc74d8c04d4d2f2e4ed9cd1731387248367cea7f149731b7a015371b220ffa0fb", 518 "blockNumber": "0x4", 519 "from": "0x3036656164333031646165616636376537376538", 520 "gas": "0x989680", 521 "gasPrice": "0x5d21dba00", 522 "hash": "0x2283294e89b41df2df4dd37c375a3f51c3ad11877aa0a4b59d0f68cf5cfd865a", 523 "input": "0x", 524 "nonce": "0x8", 525 "to": "0x3364613566326466626334613262333837316462", 526 "transactionIndex": "0x8", 527 "value": "0x5", 528 "type": "0x0", 529 "v": "0x1", 530 "r": "0x2", 531 "s": "0x3" 532 }, 533 { 534 "blockHash": "0xc74d8c04d4d2f2e4ed9cd1731387248367cea7f149731b7a015371b220ffa0fb", 535 "blockNumber": "0x4", 536 "from": "0x3730323366383135666136613633663761613063", 537 "gas": "0x1312d00", 538 "gasPrice": "0x5d21dba00", 539 "hash": "0x80e05750d02d22d73926179a0611b431ae7658846406f836e903d76191423716", 540 "input": "0x68656c6c6f", 541 "nonce": "0x9", 542 "to": "0x3336623562313539333066323466653862616538", 543 "transactionIndex": "0x9", 544 "value": "0x3", 545 "type": "0x0", 546 "v": "0x1", 547 "r": "0x2", 548 "s": "0x3" 549 }, 550 { 551 "blockHash": "0xc74d8c04d4d2f2e4ed9cd1731387248367cea7f149731b7a015371b220ffa0fb", 552 "blockNumber": "0x4", 553 "from": "0x3936663364636533666637396132333733653330", 554 "gas": "0x1312d00", 555 "gasPrice": "0x5d21dba00", 556 "hash": "0xe8abdee5e8fef72fe4d98f7dbef36000407e97874e8c880df4d85646958dd2c1", 557 "input": "0x", 558 "nonce": "0xa", 559 "to": "0x3936663364636533666637396132333733653330", 560 "transactionIndex": "0xa", 561 "value": "0x0", 562 "type": "0x0", 563 "v": "0x1", 564 "r": "0x2", 565 "s": "0x3" 566 }, 567 { 568 "blockHash": "0xc74d8c04d4d2f2e4ed9cd1731387248367cea7f149731b7a015371b220ffa0fb", 569 "blockNumber": "0x4", 570 "from": "0x3936663364636533666637396132333733653330", 571 "gas": "0x5f5e100", 572 "gasPrice": "0x5d21dba00", 573 "hash": "0x4c970be1815e58e6f69321202ce38b2e5c5e5ecb70205634848afdbc57224811", 574 "input": "0x", 575 "nonce": "0xb", 576 "to": null, 577 "transactionIndex": "0xb", 578 "value": "0x0", 579 "type": "0x0", 580 "v": "0x1", 581 "r": "0x2", 582 "s": "0x3" 583 }, 584 { 585 "blockHash": "0xc74d8c04d4d2f2e4ed9cd1731387248367cea7f149731b7a015371b220ffa0fb", 586 "blockNumber": "0x4", 587 "from": "0x3936663364636533666637396132333733653330", 588 "gas": "0x2faf080", 589 "gasPrice": "0x5d21dba00", 590 "hash": "0x7ff0a809387d0a4cab77624d467f4d65ffc1ac95f4cc46c2246daab0407a7d83", 591 "input": "0x", 592 "nonce": "0xc", 593 "to": "0x3632323232656162393565396564323963346266", 594 "transactionIndex": "0xc", 595 "value": "0x0", 596 "type": "0x0", 597 "v": "0x1", 598 "r": "0x2", 599 "s": "0x3" 600 }, 601 { 602 "blockHash": "0xc74d8c04d4d2f2e4ed9cd1731387248367cea7f149731b7a015371b220ffa0fb", 603 "blockNumber": "0x4", 604 "from": "0x3936663364636533666637396132333733653330", 605 "gas": "0x2faf080", 606 "gasPrice": "0x5d21dba00", 607 "hash": "0xb510b11415b39d18a972a00e3b43adae1e0f583ea0481a4296e169561ff4d916", 608 "input": "0x", 609 "nonce": "0xd", 610 "to": "0x3936663364636533666637396132333733653330", 611 "transactionIndex": "0xd", 612 "value": "0x0", 613 "type": "0x0", 614 "v": "0x1", 615 "r": "0x2", 616 "s": "0x3" 617 }, 618 { 619 "blockHash": "0xc74d8c04d4d2f2e4ed9cd1731387248367cea7f149731b7a015371b220ffa0fb", 620 "blockNumber": "0x4", 621 "from": "0x3936663364636533666637396132333733653330", 622 "gas": "0x2faf080", 623 "gasPrice": "0x5d21dba00", 624 "hash": "0xab466145fb71a2d24d6f6af3bddf3bcfa43c20a5937905dd01963eaf9fc5e382", 625 "input": "0xf8ad80b8aaf8a8a0072409b14b96f9d7dbf4788dbc68c5d30bd5fac1431c299e0ab55c92e70a28a4a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a00000000000000000000000000000000000000000000000000000000000000000a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a00000000000000000000000000000000000000000000000000000000000000000808080", 626 "nonce": "0xe", 627 "to": "0x3936663364636533666637396132333733653330", 628 "transactionIndex": "0xe", 629 "value": "0x0", 630 "type": "0x0", 631 "v": "0x1", 632 "r": "0x2", 633 "s": "0x3" 634 }, 635 { 636 "blockHash": "0xc74d8c04d4d2f2e4ed9cd1731387248367cea7f149731b7a015371b220ffa0fb", 637 "blockNumber": "0x4", 638 "from": "0x3036656164333031646165616636376537376538", 639 "gas": "0x989680", 640 "gasPrice": "0x5d21dba00", 641 "hash": "0xec714ab0875768f482daeabf7eb7be804e3c94bc1f1b687359da506c7f3a66b2", 642 "input": "0x", 643 "nonce": "0xf", 644 "to": "0x3364613566326466626334613262333837316462", 645 "transactionIndex": "0xf", 646 "value": "0x5", 647 "type": "0x0", 648 "v": "0x1", 649 "r": "0x2", 650 "s": "0x3" 651 }, 652 { 653 "blockHash": "0xc74d8c04d4d2f2e4ed9cd1731387248367cea7f149731b7a015371b220ffa0fb", 654 "blockNumber": "0x4", 655 "from": "0x3730323366383135666136613633663761613063", 656 "gas": "0x1312d00", 657 "gasPrice": "0x5d21dba00", 658 "hash": "0x069af125fe88784e46f90ace9960a09e5d23e6ace20350062be75964a7ece8e6", 659 "input": "0x68656c6c6f", 660 "nonce": "0x10", 661 "to": "0x3336623562313539333066323466653862616538", 662 "transactionIndex": "0x10", 663 "value": "0x3", 664 "type": "0x0", 665 "v": "0x1", 666 "r": "0x2", 667 "s": "0x3" 668 }, 669 { 670 "blockHash": "0xc74d8c04d4d2f2e4ed9cd1731387248367cea7f149731b7a015371b220ffa0fb", 671 "blockNumber": "0x4", 672 "from": "0x3936663364636533666637396132333733653330", 673 "gas": "0x1312d00", 674 "gasPrice": "0x5d21dba00", 675 "hash": "0x4a6bb7b2cd68265eb6a693aa270daffa3cc297765267f92be293b12e64948c82", 676 "input": "0x", 677 "nonce": "0x11", 678 "to": "0x3936663364636533666637396132333733653330", 679 "transactionIndex": "0x11", 680 "value": "0x0", 681 "type": "0x0", 682 "v": "0x1", 683 "r": "0x2", 684 "s": "0x3" 685 }, 686 { 687 "blockHash": "0xc74d8c04d4d2f2e4ed9cd1731387248367cea7f149731b7a015371b220ffa0fb", 688 "blockNumber": "0x4", 689 "from": "0x3936663364636533666637396132333733653330", 690 "gas": "0x5f5e100", 691 "gasPrice": "0x5d21dba00", 692 "hash": "0xa354fe3fdde6292e85545e6327c314827a20e0d7a1525398b38526fe28fd36e1", 693 "input": "0x", 694 "nonce": "0x12", 695 "to": null, 696 "transactionIndex": "0x12", 697 "value": "0x0", 698 "type": "0x0", 699 "v": "0x1", 700 "r": "0x2", 701 "s": "0x3" 702 }, 703 { 704 "blockHash": "0xc74d8c04d4d2f2e4ed9cd1731387248367cea7f149731b7a015371b220ffa0fb", 705 "blockNumber": "0x4", 706 "from": "0x3936663364636533666637396132333733653330", 707 "gas": "0x2faf080", 708 "gasPrice": "0x5d21dba00", 709 "hash": "0x5bb64e885f196f7b515e62e3b90496864d960e2f5e0d7ad88550fa1c875ca691", 710 "input": "0x", 711 "nonce": "0x13", 712 "to": "0x3632323232656162393565396564323963346266", 713 "transactionIndex": "0x13", 714 "value": "0x0", 715 "type": "0x0", 716 "v": "0x1", 717 "r": "0x2", 718 "s": "0x3" 719 }, 720 { 721 "blockHash": "0xc74d8c04d4d2f2e4ed9cd1731387248367cea7f149731b7a015371b220ffa0fb", 722 "blockNumber": "0x4", 723 "from": "0x3936663364636533666637396132333733653330", 724 "gas": "0x2faf080", 725 "gasPrice": "0x5d21dba00", 726 "hash": "0x6f4308b3c98db2db215d02c0df24472a215df7aa283261fcb06a6c9f796df9af", 727 "input": "0x", 728 "nonce": "0x14", 729 "to": "0x3936663364636533666637396132333733653330", 730 "transactionIndex": "0x14", 731 "value": "0x0", 732 "type": "0x0", 733 "v": "0x1", 734 "r": "0x2", 735 "s": "0x3" 736 }, 737 { 738 "blockHash": "0xc74d8c04d4d2f2e4ed9cd1731387248367cea7f149731b7a015371b220ffa0fb", 739 "blockNumber": "0x4", 740 "from": "0x3936663364636533666637396132333733653330", 741 "gas": "0x2faf080", 742 "gasPrice": "0x5d21dba00", 743 "hash": "0x1df88d113f0c5833c1f7264687cd6ac43888c232600ffba8d3a7d89bb5013e71", 744 "input": "0xf8ad80b8aaf8a8a0072409b14b96f9d7dbf4788dbc68c5d30bd5fac1431c299e0ab55c92e70a28a4a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a00000000000000000000000000000000000000000000000000000000000000000a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a00000000000000000000000000000000000000000000000000000000000000000808080", 745 "nonce": "0x15", 746 "to": "0x3936663364636533666637396132333733653330", 747 "transactionIndex": "0x15", 748 "value": "0x0", 749 "type": "0x0", 750 "v": "0x1", 751 "r": "0x2", 752 "s": "0x3" 753 } 754 ], 755 "transactionsRoot": "0x0a83e34ab7302f42f4a9203e8295f545517645989da6555d8cbdc1e9599df85b", 756 "uncles": [] 757 } 758 } 759 `, 760 ), &expected)) 761 } else { 762 assert.NoError(t, json.Unmarshal([]byte(` 763 { 764 "jsonrpc": "2.0", 765 "id": 1, 766 "result": { 767 "baseFeePerGas": "0x0", 768 "difficulty": "0x1", 769 "extraData": "0x", 770 "gasLimit": "0xe8d4a50fff", 771 "gasUsed": "0x2710", 772 "hash": "0xc74d8c04d4d2f2e4ed9cd1731387248367cea7f149731b7a015371b220ffa0fb", 773 "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", 774 "miner": "0x9712f943b296758aaae79944ec975884188d3a96", 775 "mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000", 776 "nonce": "0x0000000000000000", 777 "number": "0x4", 778 "parentHash": "0xc8036293065bacdfce87debec0094a71dbbe40345b078d21dcc47adb4513f348", 779 "receiptsRoot": "0xf6278dd71ffc1637f78dc2ee54f6f9e64d4b1633c1179dfdbc8c3b482efbdbec", 780 "sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347", 781 "size": "0xe44", 782 "stateRoot": "0xad31c32942fa033166e4ef588ab973dbe26657c594de4ba98192108becf0fec9", 783 "timestamp": "0x61d53854", 784 "totalDifficulty": "0x5", 785 "transactions": [ 786 "0x6231f24f79d28bb5b8425ce577b3b77cd9c1ab766fcfc5233358a2b1c2f4ff70", 787 "0xf146858415c060eae65a389cbeea8aeadc79461038fbee331ffd97b41279dd63", 788 "0x0a01fc67bb4c15c32fa43563c0fcf05cd5bf2fdcd4ec78122b5d0295993bca24", 789 "0x486f7561375c38f1627264f8676f92ec0dd1c4a7c52002ba8714e61fcc6bb649", 790 "0xbd3e57cd31dd3d6679326f7a949f0de312e9ae53bec5ef3c23b43a5319c220a4", 791 "0xff666129a0c7227b17681d668ecdef5d6681fc93dbd58856eea1374880c598b0", 792 "0xa8ad4f295f2acff9ef56b476b1c52ecb74fb3fd95a789d768c2edb3376dbeacf", 793 "0x47dbfd201fc1dd4188fd2003c6328a09bf49414be607867ca3a5d63573aede93", 794 "0x2283294e89b41df2df4dd37c375a3f51c3ad11877aa0a4b59d0f68cf5cfd865a", 795 "0x80e05750d02d22d73926179a0611b431ae7658846406f836e903d76191423716", 796 "0xe8abdee5e8fef72fe4d98f7dbef36000407e97874e8c880df4d85646958dd2c1", 797 "0x4c970be1815e58e6f69321202ce38b2e5c5e5ecb70205634848afdbc57224811", 798 "0x7ff0a809387d0a4cab77624d467f4d65ffc1ac95f4cc46c2246daab0407a7d83", 799 "0xb510b11415b39d18a972a00e3b43adae1e0f583ea0481a4296e169561ff4d916", 800 "0xab466145fb71a2d24d6f6af3bddf3bcfa43c20a5937905dd01963eaf9fc5e382", 801 "0xec714ab0875768f482daeabf7eb7be804e3c94bc1f1b687359da506c7f3a66b2", 802 "0x069af125fe88784e46f90ace9960a09e5d23e6ace20350062be75964a7ece8e6", 803 "0x4a6bb7b2cd68265eb6a693aa270daffa3cc297765267f92be293b12e64948c82", 804 "0xa354fe3fdde6292e85545e6327c314827a20e0d7a1525398b38526fe28fd36e1", 805 "0x5bb64e885f196f7b515e62e3b90496864d960e2f5e0d7ad88550fa1c875ca691", 806 "0x6f4308b3c98db2db215d02c0df24472a215df7aa283261fcb06a6c9f796df9af", 807 "0x1df88d113f0c5833c1f7264687cd6ac43888c232600ffba8d3a7d89bb5013e71" 808 ], 809 "transactionsRoot": "0x0a83e34ab7302f42f4a9203e8295f545517645989da6555d8cbdc1e9599df85b", 810 "uncles": [] 811 } 812 } 813 `, 814 ), &expected)) 815 } 816 checkEthereumBlockOrHeaderFormat(t, expected, ethBlock) 817 } 818 819 // checkEthereumHeaderFormat checks that ethBlockOrHeader returned from 820 // GetHeader or GetBlock APIs have all keys of the Ethereum data structure and 821 // also checks the immutable values are well-defined or not. 822 func checkEthereumBlockOrHeaderFormat( 823 t *testing.T, 824 expected map[string]interface{}, 825 actual map[string]interface{}, 826 ) { 827 marshaledActual, err := json.Marshal(actual) 828 assert.NoError(t, err) 829 actualResult := make(map[string]interface{}) 830 assert.NoError(t, json.Unmarshal(marshaledActual, &actualResult)) 831 832 expectedResult, ok := expected["result"].(map[string]interface{}) 833 assert.True(t, ok) 834 marshaledExpectedResult, err := json.Marshal(expectedResult) 835 assert.NoError(t, err) 836 t.Logf("expectedResult: %s\n", marshaledExpectedResult) 837 t.Logf("actualResult: %s\n", marshaledActual) 838 // Because the order of key in map is not guaranteed in Go, comparing the maps 839 // by iterating keys is reasonable approach. 840 for key := range expectedResult { 841 assert.Equal(t, expectedResult[key], actualResult[key]) 842 } 843 } 844 845 // TestEthereumAPI_GetTransactionByBlockNumberAndIndex tests GetTransactionByBlockNumberAndIndex. 846 func TestEthereumAPI_GetTransactionByBlockNumberAndIndex(t *testing.T) { 847 mockCtrl, mockBackend, api := testInitForEthApi(t) 848 block, txs, _, _, _ := createTestData(t, nil) 849 850 // Mock Backend functions. 851 mockBackend.EXPECT().BlockByNumber(gomock.Any(), gomock.Any()).Return(block, nil).Times(txs.Len()) 852 853 // Get transaction by block number and index for each transaction types. 854 for i := 0; i < txs.Len(); i++ { 855 ethTx := api.GetTransactionByBlockNumberAndIndex(context.Background(), rpc.BlockNumber(block.NumberU64()), hexutil.Uint(i)) 856 checkEthRPCTransactionFormat(t, block, ethTx, txs[i], hexutil.Uint64(i)) 857 } 858 859 mockCtrl.Finish() 860 } 861 862 // TestEthereumAPI_GetTransactionByBlockHashAndIndex tests GetTransactionByBlockHashAndIndex. 863 func TestEthereumAPI_GetTransactionByBlockHashAndIndex(t *testing.T) { 864 mockCtrl, mockBackend, api := testInitForEthApi(t) 865 block, txs, _, _, _ := createTestData(t, nil) 866 867 // Mock Backend functions. 868 mockBackend.EXPECT().BlockByHash(gomock.Any(), gomock.Any()).Return(block, nil).Times(txs.Len()) 869 870 // Get transaction by block hash and index for each transaction types. 871 for i := 0; i < txs.Len(); i++ { 872 ethTx := api.GetTransactionByBlockHashAndIndex(context.Background(), block.Hash(), hexutil.Uint(i)) 873 checkEthRPCTransactionFormat(t, block, ethTx, txs[i], hexutil.Uint64(i)) 874 } 875 876 mockCtrl.Finish() 877 } 878 879 // TestEthereumAPI_GetTransactionByHash tests GetTransactionByHash. 880 func TestEthereumAPI_GetTransactionByHash(t *testing.T) { 881 mockCtrl, mockBackend, api := testInitForEthApi(t) 882 block, txs, txHashMap, _, _ := createTestData(t, nil) 883 884 // Define queryFromPool for ReadTxAndLookupInfo function return tx from hash map. 885 // MockDatabaseManager will initiate data with txHashMap, block and queryFromPool. 886 // If queryFromPool is true, MockDatabaseManager will return nil to query transactions from transaction pool, 887 // otherwise return a transaction from txHashMap. 888 mockDBManager := &MockDatabaseManager{txHashMap: txHashMap, blockData: block, queryFromPool: false} 889 890 // Mock Backend functions. 891 mockBackend.EXPECT().ChainDB().Return(mockDBManager).Times(txs.Len()) 892 mockBackend.EXPECT().BlockByHash(gomock.Any(), block.Hash()).Return(block, nil).Times(txs.Len()) 893 894 // Get transaction by hash for each transaction types. 895 for i := 0; i < txs.Len(); i++ { 896 ethTx, err := api.GetTransactionByHash(context.Background(), txs[i].Hash()) 897 if err != nil { 898 t.Fatal(err) 899 } 900 checkEthRPCTransactionFormat(t, block, ethTx, txs[i], hexutil.Uint64(i)) 901 } 902 903 mockCtrl.Finish() 904 } 905 906 // TestEthereumAPI_GetTransactionByHash tests GetTransactionByHash from transaction pool. 907 func TestEthereumAPI_GetTransactionByHashFromPool(t *testing.T) { 908 mockCtrl, mockBackend, api := testInitForEthApi(t) 909 block, txs, txHashMap, _, _ := createTestData(t, nil) 910 911 // Define queryFromPool for ReadTxAndLookupInfo function return nil. 912 // MockDatabaseManager will initiate data with txHashMap, block and queryFromPool. 913 // If queryFromPool is true, MockDatabaseManager will return nil to query transactions from transaction pool, 914 // otherwise return a transaction from txHashMap. 915 mockDBManager := &MockDatabaseManager{txHashMap: txHashMap, blockData: block, queryFromPool: true} 916 917 // Mock Backend functions. 918 mockBackend.EXPECT().ChainDB().Return(mockDBManager).Times(txs.Len()) 919 mockBackend.EXPECT().GetPoolTransaction(gomock.Any()).DoAndReturn( 920 func(hash common.Hash) *types.Transaction { 921 return txHashMap[hash] 922 }, 923 ).Times(txs.Len()) 924 925 // Get transaction by hash from the transaction pool for each transaction types. 926 for i := 0; i < txs.Len(); i++ { 927 ethTx, err := api.GetTransactionByHash(context.Background(), txs[i].Hash()) 928 if err != nil { 929 t.Fatal(err) 930 } 931 checkEthRPCTransactionFormat(t, nil, ethTx, txs[i], 0) 932 } 933 934 mockCtrl.Finish() 935 } 936 937 // TestEthereumAPI_PendingTransactionstests PendingTransactions. 938 func TestEthereumAPI_PendingTransactions(t *testing.T) { 939 mockCtrl, mockBackend, api := testInitForEthApi(t) 940 _, txs, txHashMap, _, _ := createTestData(t, nil) 941 942 mockAccountManager := mock_accounts.NewMockAccountManager(mockCtrl) 943 mockBackend.EXPECT().AccountManager().Return(mockAccountManager) 944 945 mockBackend.EXPECT().GetPoolTransactions().Return(txs, nil) 946 947 wallets := make([]accounts.Wallet, 1) 948 wallets[0] = NewMockWallet(txs) 949 mockAccountManager.EXPECT().Wallets().Return(wallets) 950 951 pendingTxs, err := api.PendingTransactions() 952 if err != nil { 953 t.Fatal(err) 954 } 955 956 for _, pt := range pendingTxs { 957 checkEthRPCTransactionFormat(t, nil, pt, txHashMap[pt.Hash], 0) 958 } 959 960 mockCtrl.Finish() 961 } 962 963 // TestEthereumAPI_GetTransactionReceipt tests GetTransactionReceipt. 964 func TestEthereumAPI_GetTransactionReceipt(t *testing.T) { 965 mockCtrl, mockBackend, api := testInitForEthApi(t) 966 block, txs, txHashMap, receiptMap, receipts := createTestData(t, nil) 967 968 // Mock Backend functions. 969 mockBackend.EXPECT().GetTxLookupInfoAndReceipt(gomock.Any(), gomock.Any()).DoAndReturn( 970 func(ctx context.Context, hash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, *types.Receipt) { 971 txLookupInfo := txHashMap[hash] 972 idx := txLookupInfo.Nonce() // Assume idx of the transaction is nonce 973 return txLookupInfo, block.Hash(), block.NumberU64(), idx, receiptMap[hash] 974 }, 975 ).Times(txs.Len()) 976 mockBackend.EXPECT().GetBlockReceipts(gomock.Any(), gomock.Any()).Return(receipts).Times(txs.Len()) 977 mockBackend.EXPECT().HeaderByHash(gomock.Any(), block.Hash()).Return(block.Header(), nil).Times(txs.Len()) 978 979 // Get receipt for each transaction types. 980 for i := 0; i < txs.Len(); i++ { 981 receipt, err := api.GetTransactionReceipt(context.Background(), txs[i].Hash()) 982 if err != nil { 983 t.Fatal(err) 984 } 985 txIdx := uint64(i) 986 checkEthTransactionReceiptFormat(t, block, receipts, receipt, RpcOutputReceipt(block.Header(), txs[i], block.Hash(), block.NumberU64(), txIdx, receiptMap[txs[i].Hash()]), txIdx) 987 } 988 989 mockCtrl.Finish() 990 } 991 992 func testInitForEthApi(t *testing.T) (*gomock.Controller, *mock_api.MockBackend, EthereumAPI) { 993 mockCtrl := gomock.NewController(t) 994 mockBackend := mock_api.NewMockBackend(mockCtrl) 995 996 blockchain.InitDeriveSha(dummyChainConfigForEthereumAPITest) 997 998 api := EthereumAPI{ 999 publicTransactionPoolAPI: NewPublicTransactionPoolAPI(mockBackend, new(AddrLocker)), 1000 publicKlayAPI: NewPublicKlayAPI(mockBackend), 1001 publicBlockChainAPI: NewPublicBlockChainAPI(mockBackend), 1002 } 1003 return mockCtrl, mockBackend, api 1004 } 1005 1006 func checkEthRPCTransactionFormat(t *testing.T, block *types.Block, ethTx *EthRPCTransaction, tx *types.Transaction, expectedIndex hexutil.Uint64) { 1007 // All Klaytn transaction types must be returned as TxTypeLegacyTransaction types. 1008 assert.Equal(t, types.TxType(ethTx.Type), types.TxTypeLegacyTransaction) 1009 1010 // Check the data of common fields of the transaction. 1011 from := getFrom(tx) 1012 assert.Equal(t, from, ethTx.From) 1013 assert.Equal(t, hexutil.Uint64(tx.Gas()), ethTx.Gas) 1014 assert.Equal(t, tx.GasPrice(), ethTx.GasPrice.ToInt()) 1015 assert.Equal(t, tx.Hash(), ethTx.Hash) 1016 assert.Equal(t, tx.GetTxInternalData().RawSignatureValues()[0].V, ethTx.V.ToInt()) 1017 assert.Equal(t, tx.GetTxInternalData().RawSignatureValues()[0].R, ethTx.R.ToInt()) 1018 assert.Equal(t, tx.GetTxInternalData().RawSignatureValues()[0].S, ethTx.S.ToInt()) 1019 assert.Equal(t, hexutil.Uint64(tx.Nonce()), ethTx.Nonce) 1020 1021 // Check the optional field of Klaytn transactions. 1022 assert.Equal(t, 0, bytes.Compare(ethTx.Input, tx.Data())) 1023 1024 to := tx.To() 1025 switch tx.Type() { 1026 case types.TxTypeAccountUpdate, types.TxTypeFeeDelegatedAccountUpdate, types.TxTypeFeeDelegatedAccountUpdateWithRatio, 1027 types.TxTypeCancel, types.TxTypeFeeDelegatedCancel, types.TxTypeFeeDelegatedCancelWithRatio, 1028 types.TxTypeChainDataAnchoring, types.TxTypeFeeDelegatedChainDataAnchoring, types.TxTypeFeeDelegatedChainDataAnchoringWithRatio: 1029 assert.Equal(t, &from, ethTx.To) 1030 default: 1031 assert.Equal(t, to, ethTx.To) 1032 } 1033 value := tx.Value() 1034 assert.Equal(t, value, ethTx.Value.ToInt()) 1035 1036 // If it is not a pending transaction and has already been processed and added into a block, 1037 // the following fields should be returned. 1038 if block != nil { 1039 assert.Equal(t, block.Hash().String(), ethTx.BlockHash.String()) 1040 assert.Equal(t, block.NumberU64(), ethTx.BlockNumber.ToInt().Uint64()) 1041 assert.Equal(t, expectedIndex, *ethTx.TransactionIndex) 1042 } 1043 1044 // Fields additionally used for Ethereum transaction types are not used 1045 // when returning Klaytn transactions. 1046 assert.Equal(t, true, reflect.ValueOf(ethTx.Accesses).IsNil()) 1047 assert.Equal(t, true, reflect.ValueOf(ethTx.ChainID).IsNil()) 1048 assert.Equal(t, true, reflect.ValueOf(ethTx.GasFeeCap).IsNil()) 1049 assert.Equal(t, true, reflect.ValueOf(ethTx.GasTipCap).IsNil()) 1050 } 1051 1052 func checkEthTransactionReceiptFormat(t *testing.T, block *types.Block, receipts []*types.Receipt, ethReceipt map[string]interface{}, kReceipt map[string]interface{}, idx uint64) { 1053 tx := block.Transactions()[idx] 1054 1055 // Check the common receipt fields. 1056 blockHash, ok := ethReceipt["blockHash"] 1057 if !ok { 1058 t.Fatal("blockHash is not defined in Ethereum transaction receipt format.") 1059 } 1060 assert.Equal(t, blockHash, kReceipt["blockHash"]) 1061 1062 blockNumber, ok := ethReceipt["blockNumber"] 1063 if !ok { 1064 t.Fatal("blockNumber is not defined in Ethereum transaction receipt format.") 1065 } 1066 assert.Equal(t, blockNumber.(hexutil.Uint64), hexutil.Uint64(kReceipt["blockNumber"].(*hexutil.Big).ToInt().Uint64())) 1067 1068 transactionHash, ok := ethReceipt["transactionHash"] 1069 if !ok { 1070 t.Fatal("transactionHash is not defined in Ethereum transaction receipt format.") 1071 } 1072 assert.Equal(t, transactionHash, kReceipt["transactionHash"]) 1073 1074 transactionIndex, ok := ethReceipt["transactionIndex"] 1075 if !ok { 1076 t.Fatal("transactionIndex is not defined in Ethereum transaction receipt format.") 1077 } 1078 assert.Equal(t, transactionIndex, hexutil.Uint64(kReceipt["transactionIndex"].(hexutil.Uint))) 1079 1080 from, ok := ethReceipt["from"] 1081 if !ok { 1082 t.Fatal("from is not defined in Ethereum transaction receipt format.") 1083 } 1084 assert.Equal(t, from, kReceipt["from"]) 1085 1086 // Klaytn transactions that do not use the 'To' field 1087 // fill in 'To' with from during converting format. 1088 toInTx := tx.To() 1089 fromAddress := getFrom(tx) 1090 to, ok := ethReceipt["to"] 1091 if !ok { 1092 t.Fatal("to is not defined in Ethereum transaction receipt format.") 1093 } 1094 switch tx.Type() { 1095 case types.TxTypeAccountUpdate, types.TxTypeFeeDelegatedAccountUpdate, types.TxTypeFeeDelegatedAccountUpdateWithRatio, 1096 types.TxTypeCancel, types.TxTypeFeeDelegatedCancel, types.TxTypeFeeDelegatedCancelWithRatio, 1097 types.TxTypeChainDataAnchoring, types.TxTypeFeeDelegatedChainDataAnchoring, types.TxTypeFeeDelegatedChainDataAnchoringWithRatio: 1098 assert.Equal(t, &fromAddress, to) 1099 default: 1100 assert.Equal(t, toInTx, to) 1101 } 1102 1103 gasUsed, ok := ethReceipt["gasUsed"] 1104 if !ok { 1105 t.Fatal("gasUsed is not defined in Ethereum transaction receipt format.") 1106 } 1107 assert.Equal(t, gasUsed, kReceipt["gasUsed"]) 1108 1109 // Compare with the calculated cumulative gas used value 1110 // to check whether the cumulativeGasUsed value is calculated properly. 1111 cumulativeGasUsed, ok := ethReceipt["cumulativeGasUsed"] 1112 if !ok { 1113 t.Fatal("cumulativeGasUsed is not defined in Ethereum transaction receipt format.") 1114 } 1115 calculatedCumulativeGas := uint64(0) 1116 for i := 0; i <= int(idx); i++ { 1117 calculatedCumulativeGas += receipts[i].GasUsed 1118 } 1119 assert.Equal(t, cumulativeGasUsed, hexutil.Uint64(calculatedCumulativeGas)) 1120 1121 contractAddress, ok := ethReceipt["contractAddress"] 1122 if !ok { 1123 t.Fatal("contractAddress is not defined in Ethereum transaction receipt format.") 1124 } 1125 assert.Equal(t, contractAddress, kReceipt["contractAddress"]) 1126 1127 logs, ok := ethReceipt["logs"] 1128 if !ok { 1129 t.Fatal("logs is not defined in Ethereum transaction receipt format.") 1130 } 1131 assert.Equal(t, logs, kReceipt["logs"]) 1132 1133 logsBloom, ok := ethReceipt["logsBloom"] 1134 if !ok { 1135 t.Fatal("logsBloom is not defined in Ethereum transaction receipt format.") 1136 } 1137 assert.Equal(t, logsBloom, kReceipt["logsBloom"]) 1138 1139 typeInt, ok := ethReceipt["type"] 1140 if !ok { 1141 t.Fatal("type is not defined in Ethereum transaction receipt format.") 1142 } 1143 assert.Equal(t, types.TxType(typeInt.(hexutil.Uint)), types.TxTypeLegacyTransaction) 1144 1145 effectiveGasPrice, ok := ethReceipt["effectiveGasPrice"] 1146 if !ok { 1147 t.Fatal("effectiveGasPrice is not defined in Ethereum transaction receipt format.") 1148 } 1149 assert.Equal(t, effectiveGasPrice, hexutil.Uint64(kReceipt["gasPrice"].(*hexutil.Big).ToInt().Uint64())) 1150 1151 status, ok := ethReceipt["status"] 1152 if !ok { 1153 t.Fatal("status is not defined in Ethereum transaction receipt format.") 1154 } 1155 assert.Equal(t, status, kReceipt["status"]) 1156 1157 // Check the receipt fields that should be removed. 1158 var shouldNotExisted []string 1159 shouldNotExisted = append(shouldNotExisted, "gas", "gasPrice", "senderTxHash", "signatures", "txError", "typeInt", "feePayer", "feePayerSignatures", "feeRatio", "input", "value", "codeFormat", "humanReadable", "key", "inputJSON") 1160 for i := 0; i < len(shouldNotExisted); i++ { 1161 k := shouldNotExisted[i] 1162 _, ok = ethReceipt[k] 1163 if ok { 1164 t.Fatal(k, " should not be defined in the Ethereum transaction receipt format.") 1165 } 1166 } 1167 } 1168 1169 func createTestData(t *testing.T, header *types.Header) (*types.Block, types.Transactions, map[common.Hash]*types.Transaction, map[common.Hash]*types.Receipt, []*types.Receipt) { 1170 var txs types.Transactions 1171 1172 gasPrice := big.NewInt(25 * params.Ston) 1173 deployData := "0x60806040526000805534801561001457600080fd5b506101ea806100246000396000f30060806040526004361061006d576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806306661abd1461007257806342cbb15c1461009d578063767800de146100c8578063b22636271461011f578063d14e62b814610150575b600080fd5b34801561007e57600080fd5b5061008761017d565b6040518082815260200191505060405180910390f35b3480156100a957600080fd5b506100b2610183565b6040518082815260200191505060405180910390f35b3480156100d457600080fd5b506100dd61018b565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b34801561012b57600080fd5b5061014e60048036038101908080356000191690602001909291905050506101b1565b005b34801561015c57600080fd5b5061017b600480360381019080803590602001909291905050506101b4565b005b60005481565b600043905090565b600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b50565b80600081905550505600a165627a7a7230582053c65686a3571c517e2cf4f741d842e5ee6aa665c96ce70f46f9a594794f11eb0029" 1174 executeData := "0xa9059cbb0000000000000000000000008a4c9c443bb0645df646a2d5bb55def0ed1e885a0000000000000000000000000000000000000000000000000000000000003039" 1175 var anchorData []byte 1176 1177 txHashMap := make(map[common.Hash]*types.Transaction) 1178 receiptMap := make(map[common.Hash]*types.Receipt) 1179 var receipts []*types.Receipt 1180 1181 // Create test data for chainDataAnchoring tx 1182 { 1183 dummyBlock := types.NewBlock(&types.Header{}, nil, nil) 1184 scData, err := types.NewAnchoringDataType0(dummyBlock, 0, uint64(dummyBlock.Transactions().Len())) 1185 if err != nil { 1186 t.Fatal(err) 1187 } 1188 anchorData, _ = rlp.EncodeToBytes(scData) 1189 } 1190 1191 // Make test transactions data 1192 { 1193 // TxTypeLegacyTransaction 1194 values := map[types.TxValueKeyType]interface{}{ 1195 // Simply set the nonce to txs.Len() to have a different nonce for each transaction type. 1196 types.TxValueKeyNonce: uint64(txs.Len()), 1197 types.TxValueKeyTo: common.StringToAddress("0xe0680cfce04f80a386f1764a55c833b108770490"), 1198 types.TxValueKeyAmount: big.NewInt(0), 1199 types.TxValueKeyGasLimit: uint64(30000000), 1200 types.TxValueKeyGasPrice: gasPrice, 1201 types.TxValueKeyData: []byte("0xe3197e8f000000000000000000000000e0bef99b4a22286e27630b485d06c5a147cee931000000000000000000000000158beff8c8cdebd64654add5f6a1d9937e73536c0000000000000000000000000000000000000000000029bb5e7fb6beae32cf8000000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000180000000000000000000000000000000000000000000000000001b60fb4614a22e000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000158beff8c8cdebd64654add5f6a1d9937e73536c00000000000000000000000074ba03198fed2b15a51af242b9c63faf3c8f4d3400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"), 1202 } 1203 tx, err := types.NewTransactionWithMap(types.TxTypeLegacyTransaction, values) 1204 assert.Equal(t, nil, err) 1205 1206 signatures := types.TxSignatures{ 1207 &types.TxSignature{V: big.NewInt(1), R: big.NewInt(2), S: big.NewInt(3)}, 1208 } 1209 tx.SetSignature(signatures) 1210 1211 txs = append(txs, tx) 1212 txHashMap[tx.Hash()] = tx 1213 // For testing, set GasUsed with tx.Gas() 1214 receiptMap[tx.Hash()] = createReceipt(t, tx, tx.Gas()) 1215 receipts = append(receipts, receiptMap[tx.Hash()]) 1216 1217 } 1218 { 1219 // TxTypeValueTransfer 1220 values := map[types.TxValueKeyType]interface{}{ 1221 types.TxValueKeyNonce: uint64(txs.Len()), 1222 types.TxValueKeyFrom: common.StringToAddress("0x520af902892196a3449b06ead301daeaf67e77e8"), 1223 types.TxValueKeyTo: common.StringToAddress("0xa06fa690d92788cac4953da5f2dfbc4a2b3871db"), 1224 types.TxValueKeyAmount: big.NewInt(5), 1225 types.TxValueKeyGasLimit: uint64(10000000), 1226 types.TxValueKeyGasPrice: gasPrice, 1227 } 1228 tx, err := types.NewTransactionWithMap(types.TxTypeValueTransfer, values) 1229 assert.Equal(t, nil, err) 1230 1231 signatures := types.TxSignatures{ 1232 &types.TxSignature{V: big.NewInt(1), R: big.NewInt(2), S: big.NewInt(3)}, 1233 &types.TxSignature{V: big.NewInt(2), R: big.NewInt(3), S: big.NewInt(4)}, 1234 } 1235 tx.SetSignature(signatures) 1236 1237 txs = append(txs, tx) 1238 txHashMap[tx.Hash()] = tx 1239 // For testing, set GasUsed with tx.Gas() 1240 receiptMap[tx.Hash()] = createReceipt(t, tx, tx.Gas()) 1241 receipts = append(receipts, receiptMap[tx.Hash()]) 1242 } 1243 { 1244 // TxTypeValueTransferMemo 1245 values := map[types.TxValueKeyType]interface{}{ 1246 types.TxValueKeyNonce: uint64(txs.Len()), 1247 types.TxValueKeyFrom: common.StringToAddress("0xc05e11f9075d453b4fc87023f815fa6a63f7aa0c"), 1248 types.TxValueKeyTo: common.StringToAddress("0xb5a2d79e9228f3d278cb36b5b15930f24fe8bae8"), 1249 types.TxValueKeyAmount: big.NewInt(3), 1250 types.TxValueKeyGasLimit: uint64(20000000), 1251 types.TxValueKeyGasPrice: gasPrice, 1252 types.TxValueKeyData: []byte(string("hello")), 1253 } 1254 tx, err := types.NewTransactionWithMap(types.TxTypeValueTransferMemo, values) 1255 assert.Equal(t, nil, err) 1256 1257 signatures := types.TxSignatures{ 1258 &types.TxSignature{V: big.NewInt(1), R: big.NewInt(2), S: big.NewInt(3)}, 1259 &types.TxSignature{V: big.NewInt(2), R: big.NewInt(3), S: big.NewInt(4)}, 1260 } 1261 tx.SetSignature(signatures) 1262 1263 txs = append(txs, tx) 1264 txHashMap[tx.Hash()] = tx 1265 // For testing, set GasUsed with tx.Gas() 1266 receiptMap[tx.Hash()] = createReceipt(t, tx, tx.Gas()) 1267 receipts = append(receipts, receiptMap[tx.Hash()]) 1268 } 1269 { 1270 // TxTypeAccountUpdate 1271 values := map[types.TxValueKeyType]interface{}{ 1272 types.TxValueKeyNonce: uint64(txs.Len()), 1273 types.TxValueKeyFrom: common.StringToAddress("0x23a519a88e79fbc0bab796f3dce3ff79a2373e30"), 1274 types.TxValueKeyGasLimit: uint64(20000000), 1275 types.TxValueKeyGasPrice: gasPrice, 1276 types.TxValueKeyAccountKey: accountkey.NewAccountKeyLegacy(), 1277 } 1278 tx, err := types.NewTransactionWithMap(types.TxTypeAccountUpdate, values) 1279 assert.Equal(t, nil, err) 1280 1281 signatures := types.TxSignatures{ 1282 &types.TxSignature{V: big.NewInt(1), R: big.NewInt(2), S: big.NewInt(3)}, 1283 &types.TxSignature{V: big.NewInt(2), R: big.NewInt(3), S: big.NewInt(4)}, 1284 } 1285 tx.SetSignature(signatures) 1286 1287 txs = append(txs, tx) 1288 txHashMap[tx.Hash()] = tx 1289 // For testing, set GasUsed with tx.Gas() 1290 receiptMap[tx.Hash()] = createReceipt(t, tx, tx.Gas()) 1291 receipts = append(receipts, receiptMap[tx.Hash()]) 1292 } 1293 { 1294 // TxTypeSmartContractDeploy 1295 values := map[types.TxValueKeyType]interface{}{ 1296 types.TxValueKeyNonce: uint64(txs.Len()), 1297 types.TxValueKeyFrom: common.StringToAddress("0x23a519a88e79fbc0bab796f3dce3ff79a2373e30"), 1298 types.TxValueKeyTo: (*common.Address)(nil), 1299 types.TxValueKeyAmount: big.NewInt(0), 1300 types.TxValueKeyGasLimit: uint64(100000000), 1301 types.TxValueKeyGasPrice: gasPrice, 1302 types.TxValueKeyData: common.Hex2Bytes(deployData), 1303 types.TxValueKeyHumanReadable: false, 1304 types.TxValueKeyCodeFormat: params.CodeFormatEVM, 1305 } 1306 tx, err := types.NewTransactionWithMap(types.TxTypeSmartContractDeploy, values) 1307 assert.Equal(t, nil, err) 1308 1309 signatures := types.TxSignatures{ 1310 &types.TxSignature{V: big.NewInt(1), R: big.NewInt(2), S: big.NewInt(3)}, 1311 &types.TxSignature{V: big.NewInt(2), R: big.NewInt(3), S: big.NewInt(4)}, 1312 } 1313 tx.SetSignature(signatures) 1314 1315 txs = append(txs, tx) 1316 txHashMap[tx.Hash()] = tx 1317 // For testing, set GasUsed with tx.Gas() 1318 r := createReceipt(t, tx, tx.Gas()) 1319 fromAddress, err := tx.From() 1320 if err != nil { 1321 t.Fatal(err) 1322 } 1323 tx.FillContractAddress(fromAddress, r) 1324 receiptMap[tx.Hash()] = r 1325 receipts = append(receipts, receiptMap[tx.Hash()]) 1326 } 1327 { 1328 // TxTypeSmartContractExecution 1329 values := map[types.TxValueKeyType]interface{}{ 1330 types.TxValueKeyNonce: uint64(txs.Len()), 1331 types.TxValueKeyFrom: common.StringToAddress("0x23a519a88e79fbc0bab796f3dce3ff79a2373e30"), 1332 types.TxValueKeyTo: common.StringToAddress("0x00ca1eee49a4d2b04e6562222eab95e9ed29c4bf"), 1333 types.TxValueKeyAmount: big.NewInt(0), 1334 types.TxValueKeyGasLimit: uint64(50000000), 1335 types.TxValueKeyGasPrice: gasPrice, 1336 types.TxValueKeyData: common.Hex2Bytes(executeData), 1337 } 1338 tx, err := types.NewTransactionWithMap(types.TxTypeSmartContractExecution, values) 1339 assert.Equal(t, nil, err) 1340 1341 signatures := types.TxSignatures{ 1342 &types.TxSignature{V: big.NewInt(1), R: big.NewInt(2), S: big.NewInt(3)}, 1343 &types.TxSignature{V: big.NewInt(2), R: big.NewInt(3), S: big.NewInt(4)}, 1344 } 1345 tx.SetSignature(signatures) 1346 1347 txs = append(txs, tx) 1348 txHashMap[tx.Hash()] = tx 1349 // For testing, set GasUsed with tx.Gas() 1350 receiptMap[tx.Hash()] = createReceipt(t, tx, tx.Gas()) 1351 receipts = append(receipts, receiptMap[tx.Hash()]) 1352 } 1353 { 1354 // TxTypeCancel 1355 values := map[types.TxValueKeyType]interface{}{ 1356 types.TxValueKeyNonce: uint64(txs.Len()), 1357 types.TxValueKeyFrom: common.StringToAddress("0x23a519a88e79fbc0bab796f3dce3ff79a2373e30"), 1358 types.TxValueKeyGasLimit: uint64(50000000), 1359 types.TxValueKeyGasPrice: gasPrice, 1360 } 1361 tx, err := types.NewTransactionWithMap(types.TxTypeCancel, values) 1362 assert.Equal(t, nil, err) 1363 1364 signatures := types.TxSignatures{ 1365 &types.TxSignature{V: big.NewInt(1), R: big.NewInt(2), S: big.NewInt(3)}, 1366 &types.TxSignature{V: big.NewInt(2), R: big.NewInt(3), S: big.NewInt(4)}, 1367 } 1368 tx.SetSignature(signatures) 1369 1370 txs = append(txs, tx) 1371 txHashMap[tx.Hash()] = tx 1372 // For testing, set GasUsed with tx.Gas() 1373 receiptMap[tx.Hash()] = createReceipt(t, tx, tx.Gas()) 1374 receipts = append(receipts, receiptMap[tx.Hash()]) 1375 } 1376 { 1377 // TxTypeChainDataAnchoring 1378 values := map[types.TxValueKeyType]interface{}{ 1379 types.TxValueKeyNonce: uint64(txs.Len()), 1380 types.TxValueKeyFrom: common.StringToAddress("0x23a519a88e79fbc0bab796f3dce3ff79a2373e30"), 1381 types.TxValueKeyGasLimit: uint64(50000000), 1382 types.TxValueKeyGasPrice: gasPrice, 1383 types.TxValueKeyAnchoredData: anchorData, 1384 } 1385 tx, err := types.NewTransactionWithMap(types.TxTypeChainDataAnchoring, values) 1386 assert.Equal(t, nil, err) 1387 1388 signatures := types.TxSignatures{ 1389 &types.TxSignature{V: big.NewInt(1), R: big.NewInt(2), S: big.NewInt(3)}, 1390 &types.TxSignature{V: big.NewInt(2), R: big.NewInt(3), S: big.NewInt(4)}, 1391 } 1392 tx.SetSignature(signatures) 1393 1394 txs = append(txs, tx) 1395 txHashMap[tx.Hash()] = tx 1396 // For testing, set GasUsed with tx.Gas() 1397 receiptMap[tx.Hash()] = createReceipt(t, tx, tx.Gas()) 1398 receipts = append(receipts, receiptMap[tx.Hash()]) 1399 } 1400 { 1401 // TxTypeFeeDelegatedValueTransfer 1402 values := map[types.TxValueKeyType]interface{}{ 1403 types.TxValueKeyNonce: uint64(txs.Len()), 1404 types.TxValueKeyFrom: common.StringToAddress("0x520af902892196a3449b06ead301daeaf67e77e8"), 1405 types.TxValueKeyTo: common.StringToAddress("0xa06fa690d92788cac4953da5f2dfbc4a2b3871db"), 1406 types.TxValueKeyAmount: big.NewInt(5), 1407 types.TxValueKeyGasLimit: uint64(10000000), 1408 types.TxValueKeyGasPrice: gasPrice, 1409 types.TxValueKeyFeePayer: common.StringToAddress("0xa142f7b24a618778165c9b06e15a61f100c51400"), 1410 } 1411 tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedValueTransfer, values) 1412 assert.Equal(t, nil, err) 1413 1414 signatures := types.TxSignatures{ 1415 &types.TxSignature{V: big.NewInt(1), R: big.NewInt(2), S: big.NewInt(3)}, 1416 &types.TxSignature{V: big.NewInt(2), R: big.NewInt(3), S: big.NewInt(4)}, 1417 } 1418 tx.SetSignature(signatures) 1419 1420 feePayerSignatures := types.TxSignatures{ 1421 &types.TxSignature{V: big.NewInt(3), R: big.NewInt(4), S: big.NewInt(5)}, 1422 &types.TxSignature{V: big.NewInt(4), R: big.NewInt(5), S: big.NewInt(6)}, 1423 } 1424 tx.SetFeePayerSignatures(feePayerSignatures) 1425 1426 txs = append(txs, tx) 1427 txHashMap[tx.Hash()] = tx 1428 // For testing, set GasUsed with tx.Gas() 1429 receiptMap[tx.Hash()] = createReceipt(t, tx, tx.Gas()) 1430 receipts = append(receipts, receiptMap[tx.Hash()]) 1431 } 1432 { 1433 // TxTypeFeeDelegatedValueTransferMemo 1434 values := map[types.TxValueKeyType]interface{}{ 1435 types.TxValueKeyNonce: uint64(txs.Len()), 1436 types.TxValueKeyFrom: common.StringToAddress("0xc05e11f9075d453b4fc87023f815fa6a63f7aa0c"), 1437 types.TxValueKeyTo: common.StringToAddress("0xb5a2d79e9228f3d278cb36b5b15930f24fe8bae8"), 1438 types.TxValueKeyAmount: big.NewInt(3), 1439 types.TxValueKeyGasLimit: uint64(20000000), 1440 types.TxValueKeyGasPrice: gasPrice, 1441 types.TxValueKeyData: []byte(string("hello")), 1442 types.TxValueKeyFeePayer: common.StringToAddress("0xa142f7b24a618778165c9b06e15a61f100c51400"), 1443 } 1444 tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedValueTransferMemo, values) 1445 assert.Equal(t, nil, err) 1446 1447 signatures := types.TxSignatures{ 1448 &types.TxSignature{V: big.NewInt(1), R: big.NewInt(2), S: big.NewInt(3)}, 1449 &types.TxSignature{V: big.NewInt(2), R: big.NewInt(3), S: big.NewInt(4)}, 1450 } 1451 tx.SetSignature(signatures) 1452 1453 feePayerSignatures := types.TxSignatures{ 1454 &types.TxSignature{V: big.NewInt(3), R: big.NewInt(4), S: big.NewInt(5)}, 1455 &types.TxSignature{V: big.NewInt(4), R: big.NewInt(5), S: big.NewInt(6)}, 1456 } 1457 tx.SetFeePayerSignatures(feePayerSignatures) 1458 1459 txs = append(txs, tx) 1460 txHashMap[tx.Hash()] = tx 1461 1462 // For testing, set GasUsed with tx.Gas() 1463 receiptMap[tx.Hash()] = createReceipt(t, tx, tx.Gas()) 1464 receipts = append(receipts, receiptMap[tx.Hash()]) 1465 } 1466 { 1467 // TxTypeFeeDelegatedAccountUpdate 1468 values := map[types.TxValueKeyType]interface{}{ 1469 types.TxValueKeyNonce: uint64(txs.Len()), 1470 types.TxValueKeyFrom: common.StringToAddress("0x23a519a88e79fbc0bab796f3dce3ff79a2373e30"), 1471 types.TxValueKeyGasLimit: uint64(20000000), 1472 types.TxValueKeyGasPrice: gasPrice, 1473 types.TxValueKeyAccountKey: accountkey.NewAccountKeyLegacy(), 1474 types.TxValueKeyFeePayer: common.StringToAddress("0xa142f7b24a618778165c9b06e15a61f100c51400"), 1475 } 1476 tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedAccountUpdate, values) 1477 assert.Equal(t, nil, err) 1478 1479 signatures := types.TxSignatures{ 1480 &types.TxSignature{V: big.NewInt(1), R: big.NewInt(2), S: big.NewInt(3)}, 1481 &types.TxSignature{V: big.NewInt(2), R: big.NewInt(3), S: big.NewInt(4)}, 1482 } 1483 tx.SetSignature(signatures) 1484 1485 feePayerSignatures := types.TxSignatures{ 1486 &types.TxSignature{V: big.NewInt(3), R: big.NewInt(4), S: big.NewInt(5)}, 1487 &types.TxSignature{V: big.NewInt(4), R: big.NewInt(5), S: big.NewInt(6)}, 1488 } 1489 tx.SetFeePayerSignatures(feePayerSignatures) 1490 1491 txs = append(txs, tx) 1492 txHashMap[tx.Hash()] = tx 1493 // For testing, set GasUsed with tx.Gas() 1494 receiptMap[tx.Hash()] = createReceipt(t, tx, tx.Gas()) 1495 receipts = append(receipts, receiptMap[tx.Hash()]) 1496 } 1497 { 1498 // TxTypeFeeDelegatedSmartContractDeploy 1499 values := map[types.TxValueKeyType]interface{}{ 1500 types.TxValueKeyNonce: uint64(txs.Len()), 1501 types.TxValueKeyFrom: common.StringToAddress("0x23a519a88e79fbc0bab796f3dce3ff79a2373e30"), 1502 types.TxValueKeyTo: (*common.Address)(nil), 1503 types.TxValueKeyAmount: big.NewInt(0), 1504 types.TxValueKeyGasLimit: uint64(100000000), 1505 types.TxValueKeyGasPrice: gasPrice, 1506 types.TxValueKeyData: common.Hex2Bytes(deployData), 1507 types.TxValueKeyHumanReadable: false, 1508 types.TxValueKeyCodeFormat: params.CodeFormatEVM, 1509 types.TxValueKeyFeePayer: common.StringToAddress("0xa142f7b24a618778165c9b06e15a61f100c51400"), 1510 } 1511 tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedSmartContractDeploy, values) 1512 assert.Equal(t, nil, err) 1513 1514 signatures := types.TxSignatures{ 1515 &types.TxSignature{V: big.NewInt(1), R: big.NewInt(2), S: big.NewInt(3)}, 1516 &types.TxSignature{V: big.NewInt(2), R: big.NewInt(3), S: big.NewInt(4)}, 1517 } 1518 tx.SetSignature(signatures) 1519 1520 feePayerSignatures := types.TxSignatures{ 1521 &types.TxSignature{V: big.NewInt(3), R: big.NewInt(4), S: big.NewInt(5)}, 1522 &types.TxSignature{V: big.NewInt(4), R: big.NewInt(5), S: big.NewInt(6)}, 1523 } 1524 tx.SetFeePayerSignatures(feePayerSignatures) 1525 1526 txs = append(txs, tx) 1527 txHashMap[tx.Hash()] = tx 1528 // For testing, set GasUsed with tx.Gas() 1529 r := createReceipt(t, tx, tx.Gas()) 1530 fromAddress, err := tx.From() 1531 if err != nil { 1532 t.Fatal(err) 1533 } 1534 tx.FillContractAddress(fromAddress, r) 1535 receiptMap[tx.Hash()] = r 1536 receipts = append(receipts, receiptMap[tx.Hash()]) 1537 } 1538 { 1539 // TxTypeFeeDelegatedSmartContractExecution 1540 values := map[types.TxValueKeyType]interface{}{ 1541 types.TxValueKeyNonce: uint64(txs.Len()), 1542 types.TxValueKeyFrom: common.StringToAddress("0x23a519a88e79fbc0bab796f3dce3ff79a2373e30"), 1543 types.TxValueKeyTo: common.StringToAddress("0x00ca1eee49a4d2b04e6562222eab95e9ed29c4bf"), 1544 types.TxValueKeyAmount: big.NewInt(0), 1545 types.TxValueKeyGasLimit: uint64(50000000), 1546 types.TxValueKeyGasPrice: gasPrice, 1547 types.TxValueKeyData: common.Hex2Bytes(executeData), 1548 types.TxValueKeyFeePayer: common.StringToAddress("0xa142f7b24a618778165c9b06e15a61f100c51400"), 1549 } 1550 tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedSmartContractExecution, values) 1551 assert.Equal(t, nil, err) 1552 1553 signatures := types.TxSignatures{ 1554 &types.TxSignature{V: big.NewInt(1), R: big.NewInt(2), S: big.NewInt(3)}, 1555 &types.TxSignature{V: big.NewInt(2), R: big.NewInt(3), S: big.NewInt(4)}, 1556 } 1557 tx.SetSignature(signatures) 1558 1559 feePayerSignatures := types.TxSignatures{ 1560 &types.TxSignature{V: big.NewInt(3), R: big.NewInt(4), S: big.NewInt(5)}, 1561 &types.TxSignature{V: big.NewInt(4), R: big.NewInt(5), S: big.NewInt(6)}, 1562 } 1563 tx.SetFeePayerSignatures(feePayerSignatures) 1564 1565 txs = append(txs, tx) 1566 txHashMap[tx.Hash()] = tx 1567 // For testing, set GasUsed with tx.Gas() 1568 receiptMap[tx.Hash()] = createReceipt(t, tx, tx.Gas()) 1569 receipts = append(receipts, receiptMap[tx.Hash()]) 1570 } 1571 { 1572 // TxTypeFeeDelegatedCancel 1573 values := map[types.TxValueKeyType]interface{}{ 1574 types.TxValueKeyNonce: uint64(txs.Len()), 1575 types.TxValueKeyFrom: common.StringToAddress("0x23a519a88e79fbc0bab796f3dce3ff79a2373e30"), 1576 types.TxValueKeyGasLimit: uint64(50000000), 1577 types.TxValueKeyGasPrice: gasPrice, 1578 types.TxValueKeyFeePayer: common.StringToAddress("0xa142f7b24a618778165c9b06e15a61f100c51400"), 1579 } 1580 tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedCancel, values) 1581 assert.Equal(t, nil, err) 1582 1583 signatures := types.TxSignatures{ 1584 &types.TxSignature{V: big.NewInt(1), R: big.NewInt(2), S: big.NewInt(3)}, 1585 &types.TxSignature{V: big.NewInt(2), R: big.NewInt(3), S: big.NewInt(4)}, 1586 } 1587 tx.SetSignature(signatures) 1588 1589 feePayerSignatures := types.TxSignatures{ 1590 &types.TxSignature{V: big.NewInt(3), R: big.NewInt(4), S: big.NewInt(5)}, 1591 &types.TxSignature{V: big.NewInt(4), R: big.NewInt(5), S: big.NewInt(6)}, 1592 } 1593 tx.SetFeePayerSignatures(feePayerSignatures) 1594 1595 txs = append(txs, tx) 1596 txHashMap[tx.Hash()] = tx 1597 // For testing, set GasUsed with tx.Gas() 1598 receiptMap[tx.Hash()] = createReceipt(t, tx, tx.Gas()) 1599 receipts = append(receipts, receiptMap[tx.Hash()]) 1600 } 1601 { 1602 // TxTypeFeeDelegatedChainDataAnchoring 1603 values := map[types.TxValueKeyType]interface{}{ 1604 types.TxValueKeyNonce: uint64(txs.Len()), 1605 types.TxValueKeyFrom: common.StringToAddress("0x23a519a88e79fbc0bab796f3dce3ff79a2373e30"), 1606 types.TxValueKeyGasLimit: uint64(50000000), 1607 types.TxValueKeyGasPrice: gasPrice, 1608 types.TxValueKeyAnchoredData: anchorData, 1609 types.TxValueKeyFeePayer: common.StringToAddress("0xa142f7b24a618778165c9b06e15a61f100c51400"), 1610 } 1611 tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedChainDataAnchoring, values) 1612 assert.Equal(t, nil, err) 1613 1614 signatures := types.TxSignatures{ 1615 &types.TxSignature{V: big.NewInt(1), R: big.NewInt(2), S: big.NewInt(3)}, 1616 &types.TxSignature{V: big.NewInt(2), R: big.NewInt(3), S: big.NewInt(4)}, 1617 } 1618 tx.SetSignature(signatures) 1619 1620 feePayerSignatures := types.TxSignatures{ 1621 &types.TxSignature{V: big.NewInt(3), R: big.NewInt(4), S: big.NewInt(5)}, 1622 &types.TxSignature{V: big.NewInt(4), R: big.NewInt(5), S: big.NewInt(6)}, 1623 } 1624 tx.SetFeePayerSignatures(feePayerSignatures) 1625 1626 txs = append(txs, tx) 1627 txHashMap[tx.Hash()] = tx 1628 1629 // For testing, set GasUsed with tx.Gas() 1630 receiptMap[tx.Hash()] = createReceipt(t, tx, tx.Gas()) 1631 receipts = append(receipts, receiptMap[tx.Hash()]) 1632 } 1633 { 1634 // TxTypeFeeDelegatedValueTransferWithRatio 1635 values := map[types.TxValueKeyType]interface{}{ 1636 types.TxValueKeyNonce: uint64(txs.Len()), 1637 types.TxValueKeyFrom: common.StringToAddress("0x520af902892196a3449b06ead301daeaf67e77e8"), 1638 types.TxValueKeyTo: common.StringToAddress("0xa06fa690d92788cac4953da5f2dfbc4a2b3871db"), 1639 types.TxValueKeyAmount: big.NewInt(5), 1640 types.TxValueKeyGasLimit: uint64(10000000), 1641 types.TxValueKeyGasPrice: gasPrice, 1642 types.TxValueKeyFeePayer: common.StringToAddress("0xa142f7b24a618778165c9b06e15a61f100c51400"), 1643 types.TxValueKeyFeeRatioOfFeePayer: types.FeeRatio(20), 1644 } 1645 tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedValueTransferWithRatio, values) 1646 assert.Equal(t, nil, err) 1647 1648 signatures := types.TxSignatures{ 1649 &types.TxSignature{V: big.NewInt(1), R: big.NewInt(2), S: big.NewInt(3)}, 1650 &types.TxSignature{V: big.NewInt(2), R: big.NewInt(3), S: big.NewInt(4)}, 1651 } 1652 tx.SetSignature(signatures) 1653 1654 feePayerSignatures := types.TxSignatures{ 1655 &types.TxSignature{V: big.NewInt(3), R: big.NewInt(4), S: big.NewInt(5)}, 1656 &types.TxSignature{V: big.NewInt(4), R: big.NewInt(5), S: big.NewInt(6)}, 1657 } 1658 tx.SetFeePayerSignatures(feePayerSignatures) 1659 1660 txs = append(txs, tx) 1661 txHashMap[tx.Hash()] = tx 1662 // For testing, set GasUsed with tx.Gas() 1663 receiptMap[tx.Hash()] = createReceipt(t, tx, tx.Gas()) 1664 receipts = append(receipts, receiptMap[tx.Hash()]) 1665 } 1666 { 1667 // TxTypeFeeDelegatedValueTransferMemoWithRatio 1668 values := map[types.TxValueKeyType]interface{}{ 1669 types.TxValueKeyNonce: uint64(txs.Len()), 1670 types.TxValueKeyFrom: common.StringToAddress("0xc05e11f9075d453b4fc87023f815fa6a63f7aa0c"), 1671 types.TxValueKeyTo: common.StringToAddress("0xb5a2d79e9228f3d278cb36b5b15930f24fe8bae8"), 1672 types.TxValueKeyAmount: big.NewInt(3), 1673 types.TxValueKeyGasLimit: uint64(20000000), 1674 types.TxValueKeyGasPrice: gasPrice, 1675 types.TxValueKeyData: []byte(string("hello")), 1676 types.TxValueKeyFeePayer: common.StringToAddress("0xa142f7b24a618778165c9b06e15a61f100c51400"), 1677 types.TxValueKeyFeeRatioOfFeePayer: types.FeeRatio(20), 1678 } 1679 tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedValueTransferMemoWithRatio, values) 1680 assert.Equal(t, nil, err) 1681 1682 signatures := types.TxSignatures{ 1683 &types.TxSignature{V: big.NewInt(1), R: big.NewInt(2), S: big.NewInt(3)}, 1684 &types.TxSignature{V: big.NewInt(2), R: big.NewInt(3), S: big.NewInt(4)}, 1685 } 1686 tx.SetSignature(signatures) 1687 1688 feePayerSignatures := types.TxSignatures{ 1689 &types.TxSignature{V: big.NewInt(3), R: big.NewInt(4), S: big.NewInt(5)}, 1690 &types.TxSignature{V: big.NewInt(4), R: big.NewInt(5), S: big.NewInt(6)}, 1691 } 1692 tx.SetFeePayerSignatures(feePayerSignatures) 1693 1694 txs = append(txs, tx) 1695 txHashMap[tx.Hash()] = tx 1696 // For testing, set GasUsed with tx.Gas() 1697 receiptMap[tx.Hash()] = createReceipt(t, tx, tx.Gas()) 1698 receipts = append(receipts, receiptMap[tx.Hash()]) 1699 } 1700 { 1701 // TxTypeFeeDelegatedAccountUpdateWithRatio 1702 values := map[types.TxValueKeyType]interface{}{ 1703 types.TxValueKeyNonce: uint64(txs.Len()), 1704 types.TxValueKeyFrom: common.StringToAddress("0x23a519a88e79fbc0bab796f3dce3ff79a2373e30"), 1705 types.TxValueKeyGasLimit: uint64(20000000), 1706 types.TxValueKeyGasPrice: gasPrice, 1707 types.TxValueKeyAccountKey: accountkey.NewAccountKeyLegacy(), 1708 types.TxValueKeyFeePayer: common.StringToAddress("0xa142f7b24a618778165c9b06e15a61f100c51400"), 1709 types.TxValueKeyFeeRatioOfFeePayer: types.FeeRatio(20), 1710 } 1711 tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedAccountUpdateWithRatio, values) 1712 assert.Equal(t, nil, err) 1713 1714 signatures := types.TxSignatures{ 1715 &types.TxSignature{V: big.NewInt(1), R: big.NewInt(2), S: big.NewInt(3)}, 1716 &types.TxSignature{V: big.NewInt(2), R: big.NewInt(3), S: big.NewInt(4)}, 1717 } 1718 tx.SetSignature(signatures) 1719 1720 feePayerSignatures := types.TxSignatures{ 1721 &types.TxSignature{V: big.NewInt(3), R: big.NewInt(4), S: big.NewInt(5)}, 1722 &types.TxSignature{V: big.NewInt(4), R: big.NewInt(5), S: big.NewInt(6)}, 1723 } 1724 tx.SetFeePayerSignatures(feePayerSignatures) 1725 1726 txs = append(txs, tx) 1727 txHashMap[tx.Hash()] = tx 1728 // For testing, set GasUsed with tx.Gas() 1729 receiptMap[tx.Hash()] = createReceipt(t, tx, tx.Gas()) 1730 receipts = append(receipts, receiptMap[tx.Hash()]) 1731 } 1732 { 1733 // TxTypeFeeDelegatedSmartContractDeployWithRatio 1734 values := map[types.TxValueKeyType]interface{}{ 1735 types.TxValueKeyNonce: uint64(txs.Len()), 1736 types.TxValueKeyFrom: common.StringToAddress("0x23a519a88e79fbc0bab796f3dce3ff79a2373e30"), 1737 types.TxValueKeyTo: (*common.Address)(nil), 1738 types.TxValueKeyAmount: big.NewInt(0), 1739 types.TxValueKeyGasLimit: uint64(100000000), 1740 types.TxValueKeyGasPrice: gasPrice, 1741 types.TxValueKeyData: common.Hex2Bytes(deployData), 1742 types.TxValueKeyHumanReadable: false, 1743 types.TxValueKeyCodeFormat: params.CodeFormatEVM, 1744 types.TxValueKeyFeePayer: common.StringToAddress("0xa142f7b24a618778165c9b06e15a61f100c51400"), 1745 types.TxValueKeyFeeRatioOfFeePayer: types.FeeRatio(20), 1746 } 1747 tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedSmartContractDeployWithRatio, values) 1748 assert.Equal(t, nil, err) 1749 1750 signatures := types.TxSignatures{ 1751 &types.TxSignature{V: big.NewInt(1), R: big.NewInt(2), S: big.NewInt(3)}, 1752 &types.TxSignature{V: big.NewInt(2), R: big.NewInt(3), S: big.NewInt(4)}, 1753 } 1754 tx.SetSignature(signatures) 1755 1756 feePayerSignatures := types.TxSignatures{ 1757 &types.TxSignature{V: big.NewInt(3), R: big.NewInt(4), S: big.NewInt(5)}, 1758 &types.TxSignature{V: big.NewInt(4), R: big.NewInt(5), S: big.NewInt(6)}, 1759 } 1760 tx.SetFeePayerSignatures(feePayerSignatures) 1761 1762 txs = append(txs, tx) 1763 txHashMap[tx.Hash()] = tx 1764 // For testing, set GasUsed with tx.Gas() 1765 r := createReceipt(t, tx, tx.Gas()) 1766 fromAddress, err := tx.From() 1767 if err != nil { 1768 t.Fatal(err) 1769 } 1770 tx.FillContractAddress(fromAddress, r) 1771 receiptMap[tx.Hash()] = r 1772 receipts = append(receipts, receiptMap[tx.Hash()]) 1773 } 1774 { 1775 // TxTypeFeeDelegatedSmartContractExecutionWithRatio 1776 values := map[types.TxValueKeyType]interface{}{ 1777 types.TxValueKeyNonce: uint64(txs.Len()), 1778 types.TxValueKeyFrom: common.StringToAddress("0x23a519a88e79fbc0bab796f3dce3ff79a2373e30"), 1779 types.TxValueKeyTo: common.StringToAddress("0x00ca1eee49a4d2b04e6562222eab95e9ed29c4bf"), 1780 types.TxValueKeyAmount: big.NewInt(0), 1781 types.TxValueKeyGasLimit: uint64(50000000), 1782 types.TxValueKeyGasPrice: gasPrice, 1783 types.TxValueKeyData: common.Hex2Bytes(executeData), 1784 types.TxValueKeyFeePayer: common.StringToAddress("0xa142f7b24a618778165c9b06e15a61f100c51400"), 1785 types.TxValueKeyFeeRatioOfFeePayer: types.FeeRatio(20), 1786 } 1787 tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedSmartContractExecutionWithRatio, values) 1788 assert.Equal(t, nil, err) 1789 1790 signatures := types.TxSignatures{ 1791 &types.TxSignature{V: big.NewInt(1), R: big.NewInt(2), S: big.NewInt(3)}, 1792 &types.TxSignature{V: big.NewInt(2), R: big.NewInt(3), S: big.NewInt(4)}, 1793 } 1794 tx.SetSignature(signatures) 1795 1796 feePayerSignatures := types.TxSignatures{ 1797 &types.TxSignature{V: big.NewInt(3), R: big.NewInt(4), S: big.NewInt(5)}, 1798 &types.TxSignature{V: big.NewInt(4), R: big.NewInt(5), S: big.NewInt(6)}, 1799 } 1800 tx.SetFeePayerSignatures(feePayerSignatures) 1801 1802 txs = append(txs, tx) 1803 txHashMap[tx.Hash()] = tx 1804 // For testing, set GasUsed with tx.Gas() 1805 receiptMap[tx.Hash()] = createReceipt(t, tx, tx.Gas()) 1806 receipts = append(receipts, receiptMap[tx.Hash()]) 1807 } 1808 { 1809 // TxTypeFeeDelegatedCancelWithRatio 1810 values := map[types.TxValueKeyType]interface{}{ 1811 types.TxValueKeyNonce: uint64(txs.Len()), 1812 types.TxValueKeyFrom: common.StringToAddress("0x23a519a88e79fbc0bab796f3dce3ff79a2373e30"), 1813 types.TxValueKeyGasLimit: uint64(50000000), 1814 types.TxValueKeyGasPrice: gasPrice, 1815 types.TxValueKeyFeePayer: common.StringToAddress("0xa142f7b24a618778165c9b06e15a61f100c51400"), 1816 types.TxValueKeyFeeRatioOfFeePayer: types.FeeRatio(20), 1817 } 1818 tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedCancelWithRatio, values) 1819 assert.Equal(t, nil, err) 1820 1821 signatures := types.TxSignatures{ 1822 &types.TxSignature{V: big.NewInt(1), R: big.NewInt(2), S: big.NewInt(3)}, 1823 &types.TxSignature{V: big.NewInt(2), R: big.NewInt(3), S: big.NewInt(4)}, 1824 } 1825 tx.SetSignature(signatures) 1826 1827 feePayerSignatures := types.TxSignatures{ 1828 &types.TxSignature{V: big.NewInt(3), R: big.NewInt(4), S: big.NewInt(5)}, 1829 &types.TxSignature{V: big.NewInt(4), R: big.NewInt(5), S: big.NewInt(6)}, 1830 } 1831 tx.SetFeePayerSignatures(feePayerSignatures) 1832 1833 txs = append(txs, tx) 1834 txHashMap[tx.Hash()] = tx 1835 // For testing, set GasUsed with tx.Gas() 1836 receiptMap[tx.Hash()] = createReceipt(t, tx, tx.Gas()) 1837 receipts = append(receipts, receiptMap[tx.Hash()]) 1838 } 1839 { 1840 // TxTypeFeeDelegatedChainDataAnchoringWithRatio 1841 values := map[types.TxValueKeyType]interface{}{ 1842 types.TxValueKeyNonce: uint64(txs.Len()), 1843 types.TxValueKeyFrom: common.StringToAddress("0x23a519a88e79fbc0bab796f3dce3ff79a2373e30"), 1844 types.TxValueKeyGasLimit: uint64(50000000), 1845 types.TxValueKeyGasPrice: gasPrice, 1846 types.TxValueKeyAnchoredData: anchorData, 1847 types.TxValueKeyFeePayer: common.StringToAddress("0xa142f7b24a618778165c9b06e15a61f100c51400"), 1848 types.TxValueKeyFeeRatioOfFeePayer: types.FeeRatio(20), 1849 } 1850 tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedChainDataAnchoringWithRatio, values) 1851 assert.Equal(t, nil, err) 1852 1853 signatures := types.TxSignatures{ 1854 &types.TxSignature{V: big.NewInt(1), R: big.NewInt(2), S: big.NewInt(3)}, 1855 &types.TxSignature{V: big.NewInt(2), R: big.NewInt(3), S: big.NewInt(4)}, 1856 } 1857 tx.SetSignature(signatures) 1858 1859 feePayerSignatures := types.TxSignatures{ 1860 &types.TxSignature{V: big.NewInt(3), R: big.NewInt(4), S: big.NewInt(5)}, 1861 &types.TxSignature{V: big.NewInt(4), R: big.NewInt(5), S: big.NewInt(6)}, 1862 } 1863 tx.SetFeePayerSignatures(feePayerSignatures) 1864 1865 txs = append(txs, tx) 1866 txHashMap[tx.Hash()] = tx 1867 // For testing, set GasUsed with tx.Gas() 1868 receiptMap[tx.Hash()] = createReceipt(t, tx, tx.Gas()) 1869 receipts = append(receipts, receiptMap[tx.Hash()]) 1870 } 1871 1872 // Create a block which includes all transaction data. 1873 var block *types.Block 1874 if header != nil { 1875 block = types.NewBlock(header, txs, receipts) 1876 } else { 1877 block = types.NewBlock(&types.Header{Number: big.NewInt(1)}, txs, nil) 1878 } 1879 1880 return block, txs, txHashMap, receiptMap, receipts 1881 } 1882 1883 func createEthereumTypedTestData(t *testing.T, header *types.Header) (*types.Block, types.Transactions, map[common.Hash]*types.Transaction, map[common.Hash]*types.Receipt, []*types.Receipt) { 1884 var txs types.Transactions 1885 1886 gasPrice := big.NewInt(25 * params.Ston) 1887 deployData := "0x60806040526000805534801561001457600080fd5b506101ea806100246000396000f30060806040526004361061006d576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806306661abd1461007257806342cbb15c1461009d578063767800de146100c8578063b22636271461011f578063d14e62b814610150575b600080fd5b34801561007e57600080fd5b5061008761017d565b6040518082815260200191505060405180910390f35b3480156100a957600080fd5b506100b2610183565b6040518082815260200191505060405180910390f35b3480156100d457600080fd5b506100dd61018b565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b34801561012b57600080fd5b5061014e60048036038101908080356000191690602001909291905050506101b1565b005b34801561015c57600080fd5b5061017b600480360381019080803590602001909291905050506101b4565b005b60005481565b600043905090565b600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b50565b80600081905550505600a165627a7a7230582053c65686a3571c517e2cf4f741d842e5ee6aa665c96ce70f46f9a594794f11eb0029" 1888 accessList := types.AccessList{ 1889 types.AccessTuple{ 1890 Address: common.StringToAddress("0x23a519a88e79fbc0bab796f3dce3ff79a2373e30"), 1891 StorageKeys: []common.Hash{ 1892 common.HexToHash("0xa145cd642157a5df01f5bc3837a1bb59b3dcefbbfad5ec435919780aebeaba2b"), 1893 common.HexToHash("0x12e2c26dca2fb2b8879f54a5ea1604924edf0e37965c2be8aa6133b75818da40"), 1894 }, 1895 }, 1896 } 1897 chainId := new(big.Int).SetUint64(2019) 1898 1899 txHashMap := make(map[common.Hash]*types.Transaction) 1900 receiptMap := make(map[common.Hash]*types.Receipt) 1901 var receipts []*types.Receipt 1902 1903 // Make test transactions data 1904 { 1905 // TxTypeEthereumAccessList 1906 to := common.StringToAddress("0xb5a2d79e9228f3d278cb36b5b15930f24fe8bae8") 1907 values := map[types.TxValueKeyType]interface{}{ 1908 types.TxValueKeyNonce: uint64(txs.Len()), 1909 types.TxValueKeyTo: &to, 1910 types.TxValueKeyAmount: big.NewInt(10), 1911 types.TxValueKeyGasLimit: uint64(50000000), 1912 types.TxValueKeyData: common.Hex2Bytes(deployData), 1913 types.TxValueKeyGasPrice: gasPrice, 1914 types.TxValueKeyAccessList: accessList, 1915 types.TxValueKeyChainID: chainId, 1916 } 1917 tx, err := types.NewTransactionWithMap(types.TxTypeEthereumAccessList, values) 1918 assert.Equal(t, nil, err) 1919 1920 signatures := types.TxSignatures{ 1921 &types.TxSignature{V: big.NewInt(1), R: big.NewInt(2), S: big.NewInt(3)}, 1922 } 1923 tx.SetSignature(signatures) 1924 1925 txs = append(txs, tx) 1926 txHashMap[tx.Hash()] = tx 1927 // For testing, set GasUsed with tx.Gas() 1928 receiptMap[tx.Hash()] = createReceipt(t, tx, tx.Gas()) 1929 receipts = append(receipts, receiptMap[tx.Hash()]) 1930 } 1931 { 1932 // TxTypeEthereumDynamicFee 1933 to := common.StringToAddress("0xb5a2d79e9228f3d278cb36b5b15930f24fe8bae8") 1934 values := map[types.TxValueKeyType]interface{}{ 1935 types.TxValueKeyNonce: uint64(txs.Len()), 1936 types.TxValueKeyTo: &to, 1937 types.TxValueKeyAmount: big.NewInt(3), 1938 types.TxValueKeyGasLimit: uint64(50000000), 1939 types.TxValueKeyData: common.Hex2Bytes(deployData), 1940 types.TxValueKeyGasTipCap: gasPrice, 1941 types.TxValueKeyGasFeeCap: gasPrice, 1942 types.TxValueKeyAccessList: accessList, 1943 types.TxValueKeyChainID: chainId, 1944 } 1945 tx, err := types.NewTransactionWithMap(types.TxTypeEthereumDynamicFee, values) 1946 assert.Equal(t, nil, err) 1947 1948 signatures := types.TxSignatures{ 1949 &types.TxSignature{V: big.NewInt(2), R: big.NewInt(3), S: big.NewInt(4)}, 1950 } 1951 tx.SetSignature(signatures) 1952 1953 txs = append(txs, tx) 1954 txHashMap[tx.Hash()] = tx 1955 // For testing, set GasUsed with tx.Gas() 1956 receiptMap[tx.Hash()] = createReceipt(t, tx, tx.Gas()) 1957 receipts = append(receipts, receiptMap[tx.Hash()]) 1958 } 1959 1960 // Create a block which includes all transaction data. 1961 var block *types.Block 1962 if header != nil { 1963 block = types.NewBlock(header, txs, receipts) 1964 } else { 1965 block = types.NewBlock(&types.Header{Number: big.NewInt(1)}, txs, nil) 1966 } 1967 1968 return block, txs, txHashMap, receiptMap, receipts 1969 } 1970 1971 func createReceipt(t *testing.T, tx *types.Transaction, gasUsed uint64) *types.Receipt { 1972 rct := types.NewReceipt(uint(0), tx.Hash(), gasUsed) 1973 rct.Logs = []*types.Log{} 1974 rct.Bloom = types.Bloom{} 1975 return rct 1976 } 1977 1978 // MockDatabaseManager is a mock of database.DBManager interface for overriding the ReadTxAndLookupInfo function. 1979 type MockDatabaseManager struct { 1980 database.DBManager 1981 1982 txHashMap map[common.Hash]*types.Transaction 1983 blockData *types.Block 1984 queryFromPool bool 1985 } 1986 1987 // GetTxLookupInfoAndReceipt retrieves a tx and lookup info and receipt for a given transaction hash. 1988 func (dbm *MockDatabaseManager) ReadTxAndLookupInfo(hash common.Hash) (*types.Transaction, common.Hash, uint64, uint64) { 1989 // If queryFromPool, return nil to query from pool after this function 1990 if dbm.queryFromPool { 1991 return nil, common.Hash{}, 0, 0 1992 } 1993 1994 txFromHashMap := dbm.txHashMap[hash] 1995 if txFromHashMap == nil { 1996 return nil, common.Hash{}, 0, 0 1997 } 1998 return txFromHashMap, dbm.blockData.Hash(), dbm.blockData.NumberU64(), txFromHashMap.Nonce() 1999 } 2000 2001 // MockWallet is a mock of accounts.Wallet interface for overriding the Accounts function. 2002 type MockWallet struct { 2003 accounts.Wallet 2004 2005 accounts []accounts.Account 2006 } 2007 2008 // NewMockWallet prepares accounts based on tx from. 2009 func NewMockWallet(txs types.Transactions) *MockWallet { 2010 mw := &MockWallet{} 2011 2012 for _, t := range txs { 2013 mw.accounts = append(mw.accounts, accounts.Account{Address: getFrom(t)}) 2014 } 2015 return mw 2016 } 2017 2018 // Accounts implements accounts.Wallet, returning an account list. 2019 func (mw *MockWallet) Accounts() []accounts.Account { 2020 return mw.accounts 2021 } 2022 2023 // TestEthTransactionArgs_setDefaults tests setDefaults method of EthTransactionArgs. 2024 func TestEthTransactionArgs_setDefaults(t *testing.T) { 2025 _, mockBackend, _ := testInitForEthApi(t) 2026 // To clarify the exact scope of this test, it is assumed that the user must fill in the gas. 2027 // Because when user does not specify gas, it calls estimateGas internally and it requires 2028 // many backend calls which are not directly related with this test. 2029 gas := hexutil.Uint64(1000000) 2030 from := common.HexToAddress("0x2eaad2bf70a070aaa2e007beee99c6148f47718e") 2031 poolNonce := uint64(1) 2032 accountNonce := uint64(5) 2033 to := common.HexToAddress("0x9712f943b296758aaae79944ec975884188d3a96") 2034 byteCode := common.Hex2Bytes("6080604052600436106049576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680632e64cec114604e5780636057361d146076575b600080fd5b348015605957600080fd5b50606060a0565b6040518082815260200191505060405180910390f35b348015608157600080fd5b50609e6004803603810190808035906020019092919050505060a9565b005b60008054905090565b80600081905550505600a165627a7a723058207783dba41884f73679e167576362b7277f88458815141651f48ca38c25b498f80029") 2035 unitPrice := new(big.Int).SetUint64(dummyChainConfigForEthereumAPITest.UnitPrice) 2036 value := new(big.Int).SetUint64(500) 2037 testSet := []struct { 2038 txArgs EthTransactionArgs 2039 expectedResult EthTransactionArgs 2040 dynamicFeeParamsSet bool 2041 nonceSet bool 2042 chainIdSet bool 2043 expectedError error 2044 }{ 2045 { 2046 txArgs: EthTransactionArgs{ 2047 From: nil, 2048 To: nil, 2049 Gas: &gas, 2050 GasPrice: nil, 2051 MaxFeePerGas: nil, 2052 MaxPriorityFeePerGas: nil, 2053 Value: nil, 2054 Nonce: nil, 2055 Data: (*hexutil.Bytes)(&byteCode), 2056 Input: nil, 2057 AccessList: nil, 2058 ChainID: nil, 2059 }, 2060 expectedResult: EthTransactionArgs{ 2061 From: nil, 2062 To: nil, 2063 Gas: &gas, 2064 GasPrice: nil, 2065 MaxFeePerGas: (*hexutil.Big)(unitPrice), 2066 MaxPriorityFeePerGas: (*hexutil.Big)(unitPrice), 2067 Value: (*hexutil.Big)(new(big.Int)), 2068 Nonce: (*hexutil.Uint64)(&poolNonce), 2069 Data: (*hexutil.Bytes)(&byteCode), 2070 Input: nil, 2071 AccessList: nil, 2072 ChainID: (*hexutil.Big)(dummyChainConfigForEthereumAPITest.ChainID), 2073 }, 2074 dynamicFeeParamsSet: false, 2075 nonceSet: false, 2076 chainIdSet: false, 2077 expectedError: nil, 2078 }, 2079 { 2080 txArgs: EthTransactionArgs{ 2081 From: &from, 2082 To: &to, 2083 Gas: &gas, 2084 GasPrice: (*hexutil.Big)(unitPrice), 2085 MaxFeePerGas: nil, 2086 MaxPriorityFeePerGas: nil, 2087 Value: (*hexutil.Big)(value), 2088 Nonce: nil, 2089 Data: (*hexutil.Bytes)(&byteCode), 2090 Input: nil, 2091 AccessList: nil, 2092 ChainID: nil, 2093 }, 2094 expectedResult: EthTransactionArgs{ 2095 From: &from, 2096 To: &to, 2097 Gas: &gas, 2098 GasPrice: (*hexutil.Big)(unitPrice), 2099 MaxFeePerGas: nil, 2100 MaxPriorityFeePerGas: nil, 2101 Value: (*hexutil.Big)(value), 2102 Nonce: (*hexutil.Uint64)(&poolNonce), 2103 Data: (*hexutil.Bytes)(&byteCode), 2104 Input: nil, 2105 AccessList: nil, 2106 ChainID: (*hexutil.Big)(dummyChainConfigForEthereumAPITest.ChainID), 2107 }, 2108 dynamicFeeParamsSet: false, 2109 nonceSet: false, 2110 chainIdSet: false, 2111 expectedError: nil, 2112 }, 2113 { 2114 txArgs: EthTransactionArgs{ 2115 From: &from, 2116 To: &to, 2117 Gas: &gas, 2118 GasPrice: nil, 2119 MaxFeePerGas: (*hexutil.Big)(new(big.Int).SetUint64(1)), 2120 MaxPriorityFeePerGas: nil, 2121 Value: (*hexutil.Big)(value), 2122 Nonce: nil, 2123 Data: nil, 2124 Input: nil, 2125 AccessList: nil, 2126 ChainID: nil, 2127 }, 2128 expectedResult: EthTransactionArgs{}, 2129 dynamicFeeParamsSet: false, 2130 nonceSet: false, 2131 chainIdSet: false, 2132 expectedError: fmt.Errorf("only %s is allowed to be used as maxFeePerGas and maxPriorityPerGas", unitPrice.Text(16)), 2133 }, 2134 { 2135 txArgs: EthTransactionArgs{ 2136 From: &from, 2137 To: &to, 2138 Gas: &gas, 2139 GasPrice: nil, 2140 MaxFeePerGas: nil, 2141 MaxPriorityFeePerGas: (*hexutil.Big)(unitPrice), 2142 Value: (*hexutil.Big)(value), 2143 Nonce: nil, 2144 Data: nil, 2145 Input: nil, 2146 AccessList: nil, 2147 ChainID: nil, 2148 }, 2149 expectedResult: EthTransactionArgs{ 2150 From: &from, 2151 To: &to, 2152 Gas: &gas, 2153 GasPrice: nil, 2154 MaxFeePerGas: (*hexutil.Big)(unitPrice), 2155 MaxPriorityFeePerGas: (*hexutil.Big)(unitPrice), 2156 Value: (*hexutil.Big)(value), 2157 Nonce: (*hexutil.Uint64)(&poolNonce), 2158 Data: nil, 2159 Input: nil, 2160 AccessList: nil, 2161 ChainID: (*hexutil.Big)(dummyChainConfigForEthereumAPITest.ChainID), 2162 }, 2163 dynamicFeeParamsSet: false, 2164 nonceSet: false, 2165 chainIdSet: false, 2166 expectedError: nil, 2167 }, 2168 { 2169 txArgs: EthTransactionArgs{ 2170 From: &from, 2171 To: &to, 2172 Gas: &gas, 2173 GasPrice: nil, 2174 MaxFeePerGas: nil, 2175 MaxPriorityFeePerGas: (*hexutil.Big)(new(big.Int).SetUint64(1)), 2176 Value: (*hexutil.Big)(value), 2177 Nonce: nil, 2178 Data: nil, 2179 Input: nil, 2180 AccessList: nil, 2181 ChainID: nil, 2182 }, 2183 expectedResult: EthTransactionArgs{}, 2184 dynamicFeeParamsSet: false, 2185 nonceSet: false, 2186 chainIdSet: false, 2187 expectedError: fmt.Errorf("only %s is allowed to be used as maxFeePerGas and maxPriorityPerGas", unitPrice.Text(16)), 2188 }, 2189 { 2190 txArgs: EthTransactionArgs{ 2191 From: &from, 2192 To: &to, 2193 Gas: &gas, 2194 GasPrice: (*hexutil.Big)(unitPrice), 2195 MaxFeePerGas: (*hexutil.Big)(unitPrice), 2196 MaxPriorityFeePerGas: (*hexutil.Big)(unitPrice), 2197 Value: (*hexutil.Big)(value), 2198 Nonce: nil, 2199 Data: nil, 2200 Input: nil, 2201 AccessList: nil, 2202 ChainID: nil, 2203 }, 2204 expectedResult: EthTransactionArgs{}, 2205 dynamicFeeParamsSet: false, 2206 nonceSet: false, 2207 chainIdSet: false, 2208 expectedError: errors.New("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified"), 2209 }, 2210 { 2211 txArgs: EthTransactionArgs{ 2212 From: &from, 2213 To: &to, 2214 Gas: &gas, 2215 GasPrice: nil, 2216 MaxFeePerGas: (*hexutil.Big)(unitPrice), 2217 MaxPriorityFeePerGas: (*hexutil.Big)(unitPrice), 2218 Value: (*hexutil.Big)(value), 2219 Nonce: nil, 2220 Data: nil, 2221 Input: nil, 2222 AccessList: nil, 2223 ChainID: nil, 2224 }, 2225 expectedResult: EthTransactionArgs{ 2226 From: &from, 2227 To: &to, 2228 Gas: &gas, 2229 GasPrice: nil, 2230 MaxFeePerGas: (*hexutil.Big)(unitPrice), 2231 MaxPriorityFeePerGas: (*hexutil.Big)(unitPrice), 2232 Value: (*hexutil.Big)(value), 2233 Nonce: (*hexutil.Uint64)(&poolNonce), 2234 Data: nil, 2235 Input: nil, 2236 AccessList: nil, 2237 ChainID: (*hexutil.Big)(dummyChainConfigForEthereumAPITest.ChainID), 2238 }, 2239 dynamicFeeParamsSet: true, 2240 nonceSet: false, 2241 chainIdSet: false, 2242 expectedError: nil, 2243 }, 2244 { 2245 txArgs: EthTransactionArgs{ 2246 From: &from, 2247 To: &to, 2248 Gas: &gas, 2249 GasPrice: nil, 2250 MaxFeePerGas: nil, 2251 MaxPriorityFeePerGas: nil, 2252 Value: (*hexutil.Big)(value), 2253 Nonce: (*hexutil.Uint64)(&accountNonce), 2254 Data: (*hexutil.Bytes)(&byteCode), 2255 Input: nil, 2256 AccessList: nil, 2257 ChainID: nil, 2258 }, 2259 expectedResult: EthTransactionArgs{ 2260 From: &from, 2261 To: &to, 2262 Gas: &gas, 2263 GasPrice: nil, 2264 MaxFeePerGas: (*hexutil.Big)(unitPrice), 2265 MaxPriorityFeePerGas: (*hexutil.Big)(unitPrice), 2266 Value: (*hexutil.Big)(value), 2267 Nonce: (*hexutil.Uint64)(&accountNonce), 2268 Data: (*hexutil.Bytes)(&byteCode), 2269 Input: nil, 2270 AccessList: nil, 2271 ChainID: (*hexutil.Big)(dummyChainConfigForEthereumAPITest.ChainID), 2272 }, 2273 dynamicFeeParamsSet: false, 2274 nonceSet: true, 2275 chainIdSet: false, 2276 expectedError: nil, 2277 }, 2278 { 2279 txArgs: EthTransactionArgs{ 2280 From: &from, 2281 To: &to, 2282 Gas: &gas, 2283 GasPrice: nil, 2284 MaxFeePerGas: (*hexutil.Big)(unitPrice), 2285 MaxPriorityFeePerGas: (*hexutil.Big)(unitPrice), 2286 Value: (*hexutil.Big)(value), 2287 Nonce: (*hexutil.Uint64)(&accountNonce), 2288 Data: (*hexutil.Bytes)(&byteCode), 2289 Input: nil, 2290 AccessList: nil, 2291 ChainID: nil, 2292 }, 2293 expectedResult: EthTransactionArgs{ 2294 From: &from, 2295 To: &to, 2296 Gas: &gas, 2297 GasPrice: nil, 2298 MaxFeePerGas: (*hexutil.Big)(unitPrice), 2299 MaxPriorityFeePerGas: (*hexutil.Big)(unitPrice), 2300 Value: (*hexutil.Big)(value), 2301 Nonce: (*hexutil.Uint64)(&accountNonce), 2302 Data: (*hexutil.Bytes)(&byteCode), 2303 Input: nil, 2304 AccessList: nil, 2305 ChainID: (*hexutil.Big)(dummyChainConfigForEthereumAPITest.ChainID), 2306 }, 2307 dynamicFeeParamsSet: true, 2308 nonceSet: true, 2309 chainIdSet: false, 2310 expectedError: nil, 2311 }, 2312 { 2313 txArgs: EthTransactionArgs{ 2314 From: &from, 2315 To: &to, 2316 Gas: &gas, 2317 GasPrice: nil, 2318 MaxFeePerGas: (*hexutil.Big)(unitPrice), 2319 MaxPriorityFeePerGas: (*hexutil.Big)(unitPrice), 2320 Value: (*hexutil.Big)(value), 2321 Nonce: (*hexutil.Uint64)(&accountNonce), 2322 Data: (*hexutil.Bytes)(&byteCode), 2323 Input: nil, 2324 AccessList: nil, 2325 ChainID: (*hexutil.Big)(new(big.Int).SetUint64(1234)), 2326 }, 2327 expectedResult: EthTransactionArgs{ 2328 From: &from, 2329 To: &to, 2330 Gas: &gas, 2331 GasPrice: nil, 2332 MaxFeePerGas: (*hexutil.Big)(unitPrice), 2333 MaxPriorityFeePerGas: (*hexutil.Big)(unitPrice), 2334 Value: (*hexutil.Big)(value), 2335 Nonce: (*hexutil.Uint64)(&accountNonce), 2336 Data: (*hexutil.Bytes)(&byteCode), 2337 Input: nil, 2338 AccessList: nil, 2339 ChainID: (*hexutil.Big)(new(big.Int).SetUint64(1234)), 2340 }, 2341 dynamicFeeParamsSet: true, 2342 nonceSet: true, 2343 chainIdSet: true, 2344 expectedError: nil, 2345 }, 2346 { 2347 txArgs: EthTransactionArgs{ 2348 From: &from, 2349 To: &to, 2350 Gas: &gas, 2351 GasPrice: nil, 2352 MaxFeePerGas: (*hexutil.Big)(unitPrice), 2353 MaxPriorityFeePerGas: (*hexutil.Big)(unitPrice), 2354 Value: (*hexutil.Big)(value), 2355 Nonce: (*hexutil.Uint64)(&accountNonce), 2356 Data: (*hexutil.Bytes)(&byteCode), 2357 Input: (*hexutil.Bytes)(&[]byte{0x1}), 2358 AccessList: nil, 2359 ChainID: (*hexutil.Big)(new(big.Int).SetUint64(1234)), 2360 }, 2361 expectedResult: EthTransactionArgs{}, 2362 dynamicFeeParamsSet: true, 2363 nonceSet: true, 2364 chainIdSet: true, 2365 expectedError: errors.New(`both "data" and "input" are set and not equal. Please use "input" to pass transaction call data`), 2366 }, 2367 } 2368 for _, test := range testSet { 2369 mockBackend.EXPECT().CurrentBlock().Return( 2370 types.NewBlockWithHeader(&types.Header{Number: new(big.Int).SetUint64(0)}), 2371 ) 2372 mockBackend.EXPECT().SuggestPrice(gomock.Any()).Return(unitPrice, nil) 2373 if !test.dynamicFeeParamsSet { 2374 mockBackend.EXPECT().ChainConfig().Return(dummyChainConfigForEthereumAPITest) 2375 } 2376 if !test.nonceSet { 2377 mockBackend.EXPECT().GetPoolNonce(context.Background(), gomock.Any()).Return(poolNonce) 2378 } 2379 if !test.chainIdSet { 2380 mockBackend.EXPECT().ChainConfig().Return(dummyChainConfigForEthereumAPITest) 2381 } 2382 mockBackend.EXPECT().RPCGasCap().Return(nil) 2383 txArgs := test.txArgs 2384 err := txArgs.setDefaults(context.Background(), mockBackend) 2385 require.Equal(t, test.expectedError, err) 2386 if err == nil { 2387 require.Equal(t, test.expectedResult, txArgs) 2388 } 2389 } 2390 } 2391 2392 func TestEthereumAPI_GetRawTransactionByHash(t *testing.T) { 2393 mockCtrl, mockBackend, api := testInitForEthApi(t) 2394 block, txs, txHashMap, _, _ := createEthereumTypedTestData(t, nil) 2395 2396 // Define queryFromPool for ReadTxAndLookupInfo function return tx from hash map. 2397 // MockDatabaseManager will initiate data with txHashMap, block and queryFromPool. 2398 // If queryFromPool is true, MockDatabaseManager will return nil to query transactions from transaction pool, 2399 // otherwise return a transaction from txHashMap. 2400 mockDBManager := &MockDatabaseManager{txHashMap: txHashMap, blockData: block, queryFromPool: false} 2401 2402 // Mock Backend functions. 2403 mockBackend.EXPECT().ChainDB().Return(mockDBManager).Times(txs.Len()) 2404 2405 for i := 0; i < txs.Len(); i++ { 2406 rawTx, err := api.GetRawTransactionByHash(context.Background(), txs[i].Hash()) 2407 if err != nil { 2408 t.Fatal(err) 2409 } 2410 prefix := types.TxType(rawTx[0]) 2411 // When get raw transaction by eth namespace API, EthereumTxTypeEnvelope must not be included. 2412 require.NotEqual(t, types.EthereumTxTypeEnvelope, prefix) 2413 } 2414 2415 mockCtrl.Finish() 2416 } 2417 2418 func TestEthereumAPI_GetRawTransactionByBlockNumberAndIndex(t *testing.T) { 2419 mockCtrl, mockBackend, api := testInitForEthApi(t) 2420 block, txs, _, _, _ := createEthereumTypedTestData(t, nil) 2421 2422 // Mock Backend functions. 2423 mockBackend.EXPECT().BlockByNumber(gomock.Any(), gomock.Any()).Return(block, nil).Times(txs.Len()) 2424 2425 for i := 0; i < txs.Len(); i++ { 2426 rawTx := api.GetRawTransactionByBlockNumberAndIndex(context.Background(), rpc.BlockNumber(block.NumberU64()), hexutil.Uint(i)) 2427 prefix := types.TxType(rawTx[0]) 2428 // When get raw transaction by eth namespace API, EthereumTxTypeEnvelope must not be included. 2429 require.NotEqual(t, types.EthereumTxTypeEnvelope, prefix) 2430 } 2431 2432 mockCtrl.Finish() 2433 }