From 1280d75987fb59d9eee385c357871d5150155c3c Mon Sep 17 00:00:00 2001 From: SangIlMo Date: Sat, 20 Jul 2024 17:08:27 +0900 Subject: [PATCH 1/4] Add testcase for createAccessList when gasPrice is less than baseFee (default 875M) --- ethclient/gethclient/gethclient_test.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/ethclient/gethclient/gethclient_test.go b/ethclient/gethclient/gethclient_test.go index 36ea290a857..893c79ee54b 100644 --- a/ethclient/gethclient/gethclient_test.go +++ b/ethclient/gethclient/gethclient_test.go @@ -20,6 +20,7 @@ import ( "bytes" "context" "encoding/json" + "errors" "math/big" "testing" @@ -214,6 +215,28 @@ func testAccessList(t *testing.T, client *rpc.Client) { if (*al)[0].StorageKeys[0] != common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000081") { t.Fatalf("unexpected storage key: %v", (*al)[0].StorageKeys[0]) } + + // error when gasPrice is less than baseFee + msg = ethereum.CallMsg{ + From: testAddr, + To: &common.Address{}, + Gas: 21000, + GasPrice: big.NewInt(1), // less than baseFee + Value: big.NewInt(1), + } + al, gas, vmErr, err = ec.CreateAccessList(context.Background(), msg) + if errors.Is(err, core.ErrBlobFeeCapTooLow) { + t.Fatalf("unexpected error: %v", err) + } + if vmErr != "" { + t.Fatalf("unexpected vm error: %v", vmErr) + } + if gas != 0 { + t.Fatalf("unexpected gas used: %v", gas) + } + if al != nil { + t.Fatalf("unexpected accesslist: %v", len(*al)) + } } func testGetProof(t *testing.T, client *rpc.Client, addr common.Address) { From 3e190e95b200665ea6102e3eaedd6c3b54df7d2e Mon Sep 17 00:00:00 2001 From: SangIlMo Date: Tue, 23 Jul 2024 15:08:19 +0900 Subject: [PATCH 2/4] Add testcode for gasPrice is not specified --- ethclient/gethclient/gethclient_test.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/ethclient/gethclient/gethclient_test.go b/ethclient/gethclient/gethclient_test.go index 893c79ee54b..4d4ca114e49 100644 --- a/ethclient/gethclient/gethclient_test.go +++ b/ethclient/gethclient/gethclient_test.go @@ -237,6 +237,27 @@ func testAccessList(t *testing.T, client *rpc.Client) { if al != nil { t.Fatalf("unexpected accesslist: %v", len(*al)) } + + // when gasPrice is not specified + msg = ethereum.CallMsg{ + From: testAddr, + To: &common.Address{}, + Gas: 21000, + Value: big.NewInt(1), + } + al, gas, vmErr, err = ec.CreateAccessList(context.Background(), msg) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if vmErr != "" { + t.Fatalf("unexpected vm error: %v", vmErr) + } + if gas != 21000 { + t.Fatalf("unexpected gas used: %v", gas) + } + if len(*al) != 0 { + t.Fatalf("unexpected length of accesslist: %v", len(*al)) + } } func testGetProof(t *testing.T, client *rpc.Client, addr common.Address) { From 675bd3dc2938a644d19d16f4b41dd80c50d6a12c Mon Sep 17 00:00:00 2001 From: SangIlMo Date: Wed, 24 Jul 2024 17:45:43 +0900 Subject: [PATCH 3/4] Fix testcode (not related error) --- ethclient/gethclient/gethclient_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ethclient/gethclient/gethclient_test.go b/ethclient/gethclient/gethclient_test.go index 4d4ca114e49..684c326eeef 100644 --- a/ethclient/gethclient/gethclient_test.go +++ b/ethclient/gethclient/gethclient_test.go @@ -20,8 +20,8 @@ import ( "bytes" "context" "encoding/json" - "errors" "math/big" + "strings" "testing" "github.com/ethereum/go-ethereum" @@ -225,7 +225,7 @@ func testAccessList(t *testing.T, client *rpc.Client) { Value: big.NewInt(1), } al, gas, vmErr, err = ec.CreateAccessList(context.Background(), msg) - if errors.Is(err, core.ErrBlobFeeCapTooLow) { + if err != nil && !strings.Contains(err.Error(), core.ErrFeeCapTooLow.Error()) { t.Fatalf("unexpected error: %v", err) } if vmErr != "" { From 32620333ba044f1ddc20014196c1b6519634dd46 Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Fri, 8 Nov 2024 09:24:02 +0100 Subject: [PATCH 4/4] ethclient/gethclient: make test tabledriven --- ethclient/gethclient/gethclient_test.go | 169 +++++++++++------------- 1 file changed, 78 insertions(+), 91 deletions(-) diff --git a/ethclient/gethclient/gethclient_test.go b/ethclient/gethclient/gethclient_test.go index 684c326eeef..65d006d1e6a 100644 --- a/ethclient/gethclient/gethclient_test.go +++ b/ethclient/gethclient/gethclient_test.go @@ -165,98 +165,85 @@ func TestGethClient(t *testing.T) { func testAccessList(t *testing.T, client *rpc.Client) { ec := New(client) - // Test transfer - msg := ethereum.CallMsg{ - From: testAddr, - To: &common.Address{}, - Gas: 21000, - GasPrice: big.NewInt(875000000), - Value: big.NewInt(1), - } - al, gas, vmErr, err := ec.CreateAccessList(context.Background(), msg) - if err != nil { - t.Fatalf("unexpected error: %v", err) - } - if vmErr != "" { - t.Fatalf("unexpected vm error: %v", vmErr) - } - if gas != 21000 { - t.Fatalf("unexpected gas used: %v", gas) - } - if len(*al) != 0 { - t.Fatalf("unexpected length of accesslist: %v", len(*al)) - } - // Test reverting transaction - msg = ethereum.CallMsg{ - From: testAddr, - To: nil, - Gas: 100000, - GasPrice: big.NewInt(1000000000), - Value: big.NewInt(1), - Data: common.FromHex("0x608060806080608155fd"), - } - al, gas, vmErr, err = ec.CreateAccessList(context.Background(), msg) - if err != nil { - t.Fatalf("unexpected error: %v", err) - } - if vmErr == "" { - t.Fatalf("wanted vmErr, got none") - } - if gas == 21000 { - t.Fatalf("unexpected gas used: %v", gas) - } - if len(*al) != 1 || al.StorageKeys() != 1 { - t.Fatalf("unexpected length of accesslist: %v", len(*al)) - } - // address changes between calls, so we can't test for it. - if (*al)[0].Address == common.HexToAddress("0x0") { - t.Fatalf("unexpected address: %v", (*al)[0].Address) - } - if (*al)[0].StorageKeys[0] != common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000081") { - t.Fatalf("unexpected storage key: %v", (*al)[0].StorageKeys[0]) - } - // error when gasPrice is less than baseFee - msg = ethereum.CallMsg{ - From: testAddr, - To: &common.Address{}, - Gas: 21000, - GasPrice: big.NewInt(1), // less than baseFee - Value: big.NewInt(1), - } - al, gas, vmErr, err = ec.CreateAccessList(context.Background(), msg) - if err != nil && !strings.Contains(err.Error(), core.ErrFeeCapTooLow.Error()) { - t.Fatalf("unexpected error: %v", err) - } - if vmErr != "" { - t.Fatalf("unexpected vm error: %v", vmErr) - } - if gas != 0 { - t.Fatalf("unexpected gas used: %v", gas) - } - if al != nil { - t.Fatalf("unexpected accesslist: %v", len(*al)) - } - - // when gasPrice is not specified - msg = ethereum.CallMsg{ - From: testAddr, - To: &common.Address{}, - Gas: 21000, - Value: big.NewInt(1), - } - al, gas, vmErr, err = ec.CreateAccessList(context.Background(), msg) - if err != nil { - t.Fatalf("unexpected error: %v", err) - } - if vmErr != "" { - t.Fatalf("unexpected vm error: %v", vmErr) - } - if gas != 21000 { - t.Fatalf("unexpected gas used: %v", gas) - } - if len(*al) != 0 { - t.Fatalf("unexpected length of accesslist: %v", len(*al)) + for i, tc := range []struct { + msg ethereum.CallMsg + wantGas uint64 + wantErr string + wantVMErr string + wantAL string + }{ + { // Test transfer + msg: ethereum.CallMsg{ + From: testAddr, + To: &common.Address{}, + Gas: 21000, + GasPrice: big.NewInt(875000000), + Value: big.NewInt(1), + }, + wantGas: 21000, + wantAL: `[]`, + }, + { // Test reverting transaction + msg: ethereum.CallMsg{ + From: testAddr, + To: nil, + Gas: 100000, + GasPrice: big.NewInt(1000000000), + Value: big.NewInt(1), + Data: common.FromHex("0x608060806080608155fd"), + }, + wantGas: 77496, + wantVMErr: "execution reverted", + wantAL: `[ + { + "address": "0x3a220f351252089d385b29beca14e27f204c296a", + "storageKeys": [ + "0x0000000000000000000000000000000000000000000000000000000000000081" + ] + } +]`, + }, + { // error when gasPrice is less than baseFee + msg: ethereum.CallMsg{ + From: testAddr, + To: &common.Address{}, + Gas: 21000, + GasPrice: big.NewInt(1), // less than baseFee + Value: big.NewInt(1), + }, + wantErr: "max fee per gas less than block base fee", + }, + { // when gasPrice is not specified + msg: ethereum.CallMsg{ + From: testAddr, + To: &common.Address{}, + Gas: 21000, + Value: big.NewInt(1), + }, + wantGas: 21000, + wantAL: `[]`, + }, + } { + al, gas, vmErr, err := ec.CreateAccessList(context.Background(), tc.msg) + if tc.wantErr != "" { + if !strings.Contains(err.Error(), tc.wantErr) { + t.Fatalf("test %d: wrong error: %v", i, err) + } + continue + } else if err != nil { + t.Fatalf("test %d: wrong error: %v", i, err) + } + if have, want := vmErr, tc.wantVMErr; have != want { + t.Fatalf("test %d: vmErr wrong, have %v want %v", i, have, want) + } + if have, want := gas, tc.wantGas; have != want { + t.Fatalf("test %d: gas wrong, have %v want %v", i, have, want) + } + haveList, _ := json.MarshalIndent(al, "", " ") + if have, want := string(haveList), tc.wantAL; have != want { + t.Fatalf("test %d: access list wrong, have:\n%v\nwant:\n%v", i, have, want) + } } }