diff --git a/ethclient/ethclient.go b/ethclient/ethclient.go index a17696356c7c..0694c5beedf9 100644 --- a/ethclient/ethclient.go +++ b/ethclient/ethclient.go @@ -515,6 +515,21 @@ func (ec *Client) EstimateGas(ctx context.Context, msg ethereum.CallMsg) (uint64 return uint64(hex), nil } +// CreateAccessList tries to create an access list for a specific transaction based on the +// current pending state of the blockchain. +func (ec *Client) CreateAccessList(ctx context.Context, msg ethereum.CallMsg) (*types.AccessList, uint64, string, error) { + type accessListResult struct { + Accesslist *types.AccessList `json:"accessList"` + Error string `json:"error,omitempty"` + GasUsed hexutil.Uint64 `json:"gasUsed"` + } + var result accessListResult + if err := ec.c.CallContext(ctx, &result, "eth_createAccessList", toCallArg(msg)); err != nil { + return nil, 0, "", err + } + return result.Accesslist, uint64(result.GasUsed), result.Error, nil +} + // SendTransaction injects a signed transaction into the pending pool for execution. // // If the transaction was a contract creation use the TransactionReceipt method to get the diff --git a/ethclient/ethclient_test.go b/ethclient/ethclient_test.go index 9fa5bf87a493..759ddff9ea0b 100644 --- a/ethclient/ethclient_test.go +++ b/ethclient/ethclient_test.go @@ -265,6 +265,9 @@ func TestEthClient(t *testing.T) { "TestAtFunctions": { func(t *testing.T) { testAtFunctions(t, client) }, }, + "TestAccessList": { + func(t *testing.T) { testAccessList(t, client) }, + }, } t.Parallel() @@ -573,3 +576,57 @@ func sendTransaction(ec *Client) error { // Send transaction return ec.SendTransaction(context.Background(), signedTx) } + +func testAccessList(t *testing.T, client *rpc.Client) { + ec := NewClient(client) + // Test transfer + msg := ethereum.CallMsg{ + From: testAddr, + To: &common.Address{}, + Gas: 21000, + GasPrice: big.NewInt(1), + 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(1), + 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]) + } +}