Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ethclient: added createAccessList #22965

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions ethclient/ethclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ethclient is designed for all eth-compatible clients. Is it true that eth_createAccessList RPC endpoint is supported by all the (major) clients?

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
Expand Down
57 changes: 57 additions & 0 deletions ethclient/ethclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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])
}
}