This commit is contained in:
georgehao 2025-03-22 23:55:47 +08:00
parent ee519501b1
commit 0da1e087c3
No known key found for this signature in database
3 changed files with 105 additions and 124 deletions

View file

@ -77,8 +77,6 @@ func (ec *Client) CreateBatchAccessList(ctx context.Context, msgs []ethereum.Cal
callArgs[i] = toCallArg(msg) callArgs[i] = toCallArg(msg)
} }
log.Info("callArgs", "callArgs", callArgs)
var result batchAccessListResult var result batchAccessListResult
if err := ec.c.CallContext(ctx, &result, "eth_createBatchAccessList", callArgs); err != nil { if err := ec.c.CallContext(ctx, &result, "eth_createBatchAccessList", callArgs); err != nil {
return nil, nil, nil, err return nil, nil, nil, err

View file

@ -20,9 +20,7 @@ import (
"bytes" "bytes"
"context" "context"
"encoding/json" "encoding/json"
"github.com/ethereum/go-ethereum/log"
"math/big" "math/big"
"os"
"strings" "strings"
"testing" "testing"
@ -65,7 +63,6 @@ func newTestBackend(t *testing.T) (*node.Node, []*types.Block) {
if err != nil { if err != nil {
t.Fatalf("can't create new ethereum service: %v", err) t.Fatalf("can't create new ethereum service: %v", err)
} }
log.SetDefault(log.NewLogger(log.NewTerminalHandlerWithLevel(os.Stderr, log.LvlTrace, true)))
filterSystem := filters.NewFilterSystem(ethservice.APIBackend, filters.Config{}) filterSystem := filters.NewFilterSystem(ethservice.APIBackend, filters.Config{})
n.RegisterAPIs([]rpc.API{ n.RegisterAPIs([]rpc.API{
{ {
@ -196,55 +193,57 @@ func testAccessList(t *testing.T, client *rpc.Client) {
}{ }{
{ // Test transfer { // Test transfer
msg: ethereum.CallMsg{ msg: ethereum.CallMsg{
From: testAddr, From: testAddr,
To: &common.Address{}, To: &common.Address{},
Gas: 21000, Gas: 21000,
GasPrice: big.NewInt(875000000), GasPrice: big.NewInt(875000000),
Value: big.NewInt(1), Value: big.NewInt(1),
GasFeeCap: nil,
GasTipCap: nil,
},
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, wantGas: 21000,
wantAL: `[]`, 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) al, gas, vmErr, err := ec.CreateAccessList(context.Background(), tc.msg)
if tc.wantErr != "" { if tc.wantErr != "" {
@ -275,7 +274,6 @@ func testBatchAccessList(t *testing.T, client *rpc.Client) {
name string name string
msgs []ethereum.CallMsg msgs []ethereum.CallMsg
wantGas []uint64 wantGas []uint64
wantErr string
wantVMErr []string wantVMErr []string
wantALs []string wantALs []string
}{ }{
@ -302,72 +300,70 @@ func testBatchAccessList(t *testing.T, client *rpc.Client) {
`[]`, `[]`,
`[]`, `[]`,
}, },
wantVMErr: []string{"", ""},
},
{
name: "Contract creation and interaction",
msgs: []ethereum.CallMsg{
{
From: testAddr,
To: nil,
Gas: 100000,
GasPrice: big.NewInt(1000000000),
Value: big.NewInt(0),
Data: common.FromHex("0x608060806080608155fd"),
},
{
From: testAddr,
To: &common.Address{},
Gas: 21000,
GasPrice: big.NewInt(1000000000),
Value: big.NewInt(1),
},
},
wantGas: []uint64{77496, 21000},
wantVMErr: []string{
"execution reverted",
"",
},
wantALs: []string{`[
{
"address": "0x3a220f351252089d385b29beca14e27f204c296a",
"storageKeys": [
"0x0000000000000000000000000000000000000000000000000000000000000081"
]
}
]`,
`[]`,
},
},
{
name: "Invalid gas price",
msgs: []ethereum.CallMsg{
{
From: testAddr,
To: &common.Address{},
Gas: 21000,
GasPrice: big.NewInt(1), // less than baseFee
Value: big.NewInt(1),
},
},
wantVMErr: []string{
"max fee per gas less than block base fee",
},
}, },
// {
// name: "Contract creation and interaction",
// msgs: []ethereum.CallMsg{
// {
// From: testAddr,
// To: nil,
// Gas: 100000,
// GasPrice: big.NewInt(1000000000),
// Value: big.NewInt(0),
// Data: common.FromHex("0x608060806080608155fd"),
// },
// {
// From: testAddr,
// To: &testContract,
// Gas: 100000,
// GasPrice: big.NewInt(1000000000),
// Value: big.NewInt(0),
// Data: common.FromHex("0x1234"),
// },
// },
// wantGas: []uint64{77496, 21896},
// wantVMErr: []string{
// "execution reverted",
// "",
// },
// wantALs: []string{
// `[
// {
// "address": "0x3a220f351252089d385b29beca14e27f204c296a",
// "storageKeys": [
// "0x0000000000000000000000000000000000000000000000000000000000000081"
// ]
// }
//]`,
// `[
// {
// "address": "0x000000000000000000000000000000000000beef",
// "storageKeys": []
// }
//]`,
// },
// },
// {
// name: "Invalid gas price",
// msgs: []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",
// },
} }
for i, tc := range testCases { for i, tc := range testCases {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
als, gas, vmErrs, err := ec.CreateBatchAccessList(context.Background(), tc.msgs) als, gas, vmErrs, err := ec.CreateBatchAccessList(context.Background(), tc.msgs)
if tc.wantErr != "" { if len(tc.wantVMErr) > 0 {
if err == nil || !strings.Contains(err.Error(), tc.wantErr) { for j, wantErr := range tc.wantVMErr {
t.Fatalf("test %d: expected error containing %q, got %v", i, tc.wantErr, err) if !strings.Contains(vmErrs[j], wantErr) {
t.Fatalf("test %d: expected error containing %q, got %v", i, wantErr, vmErrs[i])
}
return
} }
return
} else if err != nil { } else if err != nil {
t.Fatalf("test %d: unexpected error: %v", i, err) t.Fatalf("test %d: unexpected error: %v", i, err)
} }

View file

@ -1125,7 +1125,6 @@ type batchAccessListResult struct {
// This function executes transactions sequentially, with each transaction's state changes // This function executes transactions sequentially, with each transaction's state changes
// affecting the subsequent transactions in the batch. // affecting the subsequent transactions in the batch.
func (api *BlockChainAPI) CreateBatchAccessList(ctx context.Context, argsList []TransactionArgs, blockNrOrHash *rpc.BlockNumberOrHash) (*batchAccessListResult, error) { func (api *BlockChainAPI) CreateBatchAccessList(ctx context.Context, argsList []TransactionArgs, blockNrOrHash *rpc.BlockNumberOrHash) (*batchAccessListResult, error) {
log.Error("CreateBatchAccessList", "blockNrOrHash", blockNrOrHash, "argsList", argsList)
bNrOrHash := rpc.BlockNumberOrHashWithNumber(rpc.LatestBlockNumber) bNrOrHash := rpc.BlockNumberOrHashWithNumber(rpc.LatestBlockNumber)
if blockNrOrHash != nil { if blockNrOrHash != nil {
bNrOrHash = *blockNrOrHash bNrOrHash = *blockNrOrHash
@ -1148,27 +1147,16 @@ func (api *BlockChainAPI) CreateBatchAccessList(ctx context.Context, argsList []
// Use a copy of the state so we can apply changes without affecting the original state // Use a copy of the state so we can apply changes without affecting the original state
txStateDB := stateDB.Copy() txStateDB := stateDB.Copy()
// Set fee defaults and any missing fields
if err = args.setFeeDefaults(ctx, api.b, header); err != nil {
result.Errors[i] = "failed to set fee defaults: " + err.Error()
continue
}
// Set nonce if not provided // Set nonce if not provided
if args.Nonce == nil { if args.Nonce == nil {
nonce := hexutil.Uint64(txStateDB.GetNonce(args.from())) nonce := hexutil.Uint64(txStateDB.GetNonce(args.from()))
args.Nonce = &nonce args.Nonce = &nonce
} }
// Set defaults for call
if err = args.CallDefaults(api.b.RPCGasCap(), header.BaseFee, api.b.ChainConfig().ChainID); err != nil {
result.Errors[i] = "failed to set call defaults: " + err.Error()
continue
}
// Create access list for this transaction // Create access list for this transaction
acl, gasUsed, vmerr, err := AccessList(ctx, api.b, bNrOrHash, args) acl, gasUsed, vmerr, err := AccessList(ctx, api.b, bNrOrHash, args)
if err != nil { if err != nil {
log.Error("CreateBatchAccessList create access list failed", "err", err, "args", args, "vmerr", vmerr)
// If there's an error creating the access list, record it and stop processing // If there's an error creating the access list, record it and stop processing
result.Errors[i] = err.Error() result.Errors[i] = err.Error()
continue continue
@ -1200,7 +1188,6 @@ func (api *BlockChainAPI) CreateBatchAccessList(ctx context.Context, argsList []
stateDB = txStateDB stateDB = txStateDB
} }
} }
return result, nil return result, nil
} }