ethclient/gethclient: testcase for createAccessList, make tabledriven (#30194)

Adds testcase for createAccessList when user requested gasPrice is less than baseFee, also makes the tests tabledriven
---------

Co-authored-by: Martin Holst Swende <martin@swende.se>
This commit is contained in:
SangIlMo 2024-11-08 21:28:16 +09:00 committed by GitHub
parent e92e22a7cf
commit 8e00f95056
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 80 additions and 49 deletions

View File

@ -21,6 +21,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"math/big" "math/big"
"strings"
"testing" "testing"
"github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum"
@ -164,55 +165,85 @@ func TestGethClient(t *testing.T) {
func testAccessList(t *testing.T, client *rpc.Client) { func testAccessList(t *testing.T, client *rpc.Client) {
ec := New(client) ec := New(client)
// Test transfer
msg := ethereum.CallMsg{ for i, tc := range []struct {
From: testAddr, msg ethereum.CallMsg
To: &common.Address{}, wantGas uint64
Gas: 21000, wantErr string
GasPrice: big.NewInt(875000000), wantVMErr string
Value: big.NewInt(1), wantAL string
} }{
al, gas, vmErr, err := ec.CreateAccessList(context.Background(), msg) { // Test transfer
if err != nil { msg: ethereum.CallMsg{
t.Fatalf("unexpected error: %v", err) From: testAddr,
} To: &common.Address{},
if vmErr != "" { Gas: 21000,
t.Fatalf("unexpected vm error: %v", vmErr) GasPrice: big.NewInt(875000000),
} Value: big.NewInt(1),
if gas != 21000 { },
t.Fatalf("unexpected gas used: %v", gas) wantGas: 21000,
} wantAL: `[]`,
if len(*al) != 0 { },
t.Fatalf("unexpected length of accesslist: %v", len(*al)) { // Test reverting transaction
} msg: ethereum.CallMsg{
// Test reverting transaction From: testAddr,
msg = ethereum.CallMsg{ To: nil,
From: testAddr, Gas: 100000,
To: nil, GasPrice: big.NewInt(1000000000),
Gas: 100000, Value: big.NewInt(1),
GasPrice: big.NewInt(1000000000), Data: common.FromHex("0x608060806080608155fd"),
Value: big.NewInt(1), },
Data: common.FromHex("0x608060806080608155fd"), wantGas: 77496,
} wantVMErr: "execution reverted",
al, gas, vmErr, err = ec.CreateAccessList(context.Background(), msg) wantAL: `[
if err != nil { {
t.Fatalf("unexpected error: %v", err) "address": "0x3a220f351252089d385b29beca14e27f204c296a",
} "storageKeys": [
if vmErr == "" { "0x0000000000000000000000000000000000000000000000000000000000000081"
t.Fatalf("wanted vmErr, got none") ]
} }
if gas == 21000 { ]`,
t.Fatalf("unexpected gas used: %v", gas) },
} { // error when gasPrice is less than baseFee
if len(*al) != 1 || al.StorageKeys() != 1 { msg: ethereum.CallMsg{
t.Fatalf("unexpected length of accesslist: %v", len(*al)) From: testAddr,
} To: &common.Address{},
// address changes between calls, so we can't test for it. Gas: 21000,
if (*al)[0].Address == common.HexToAddress("0x0") { GasPrice: big.NewInt(1), // less than baseFee
t.Fatalf("unexpected address: %v", (*al)[0].Address) Value: big.NewInt(1),
} },
if (*al)[0].StorageKeys[0] != common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000081") { wantErr: "max fee per gas less than block base fee",
t.Fatalf("unexpected storage key: %v", (*al)[0].StorageKeys[0]) },
{ // 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)
}
} }
} }