add CreateAccessList with stateOverrides test

This commit is contained in:
georgehao 2025-03-26 15:33:57 +08:00
parent aad7c05417
commit 1b0c703d1e
No known key found for this signature in database

View file

@ -2583,7 +2583,6 @@ func TestFillBlobTransaction(t *testing.T) {
Value: (*hexutil.Big)(big.NewInt(1)), Value: (*hexutil.Big)(big.NewInt(1)),
Blobs: []kzg4844.Blob{{}}, Blobs: []kzg4844.Blob{{}},
Commitments: []kzg4844.Commitment{{}, {}}, Commitments: []kzg4844.Commitment{{}, {}},
Proofs: []kzg4844.Proof{{}, {}},
}, },
err: `number of blobs and commitments mismatch (have=2, want=1)`, err: `number of blobs and commitments mismatch (have=2, want=1)`,
}, },
@ -3543,25 +3542,52 @@ func TestCreateAccessListWithStateOverrides(t *testing.T) {
// Create a new BlockChainAPI instance // Create a new BlockChainAPI instance
api := NewBlockChainAPI(backend) api := NewBlockChainAPI(backend)
// Create test contract code // Create test contract code - a simple storage contract
contractCode := common.Hex2Bytes("608060405234801561001057600080fd5b506004361061002b5760003560e01c806360fe47b114610030575b600080fd5b61004a6004803603810190610045919061009d565b61004c565b005b8060008190555050565b600080fd5b6000819050919050565b61007a81610067565b811461008557600080fd5b50565b60008135905061009781610071565b92915050565b6000602082840312156100b3576100b2610062565b5b60006100c184828501610088565b9150509291505056") //
// SPDX-License-Identifier: MIT
// pragma solidity ^0.8.0;
//
// contract SimpleStorage {
// uint256 private value;
//
// function retrieve() public view returns (uint256) {
// return value;
// }
// }
contractCode := common.Hex2Bytes("6080604052348015600f57600080fd5b506004361060285760003560e01c80632e64cec114602d575b600080fd5b60336047565b604051603e91906067565b60405180910390f35b60008054905090565b6000819050919050565b6061816050565b82525050565b6000602082019050607a6000830184605a565b9291505056")
codeBytes := hexutil.Bytes(contractCode) codeBytes := hexutil.Bytes(contractCode)
// Create state overrides // Create state overrides with more complete state
contractAddr := common.HexToAddress("0x1234567890123456789012345678901234567890")
balance := (*hexutil.Big)(big.NewInt(1000000000000000000))
nonce := hexutil.Uint64(1)
// Add initial storage values
storage := map[common.Hash]common.Hash{
common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"): common.HexToHash("0x000000000000000000000000000000000000000000000000000000000000002a"),
}
overrides := &override.StateOverride{ overrides := &override.StateOverride{
common.HexToAddress("0x1234567890123456789012345678901234567890"): override.OverrideAccount{ contractAddr: override.OverrideAccount{
Code: &codeBytes, Code: &codeBytes,
Balance: balance,
Nonce: &nonce,
State: storage,
}, },
} }
// Create transaction arguments // Create transaction arguments with gas and value
from := common.HexToAddress("0x71562b71999873db5b286df957af199ec94617f7") from := common.HexToAddress("0x71562b71999873db5b286df957af199ec94617f7")
to := common.HexToAddress("0x1234567890123456789012345678901234567890") to := contractAddr
data := hexutil.Bytes(common.Hex2Bytes("60fe47b1000000000000000000000000000000000000000000000000000000000000002a")) // set(42) data := hexutil.Bytes(common.Hex2Bytes("2e64cec1")) // retrieve()
gas := hexutil.Uint64(100000)
value := (*hexutil.Big)(big.NewInt(0))
args := TransactionArgs{ args := TransactionArgs{
From: &from, From: &from,
To: &to, To: &to,
Data: &data, Data: &data,
Gas: &gas,
Value: value,
} }
// Call CreateAccessList // Call CreateAccessList
@ -3581,15 +3607,25 @@ func TestCreateAccessListWithStateOverrides(t *testing.T) {
t.Fatal("Expected non-zero gas used") t.Fatal("Expected non-zero gas used")
} }
// Verify access list contains the overridden contract address // Verify access list contains the contract address and storage slot
found := false foundAddr := false
foundSlot := false
for _, tuple := range *result.Accesslist { for _, tuple := range *result.Accesslist {
if tuple.Address == common.HexToAddress("0x1234567890123456789012345678901234567890") { if tuple.Address == contractAddr {
found = true foundAddr = true
for _, slot := range tuple.StorageKeys {
if slot == common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000") {
foundSlot = true
break
}
}
break break
} }
} }
if !found { if !foundAddr {
t.Fatal("Access list should contain the overridden contract address") t.Fatal("Access list should contain the contract address")
}
if !foundSlot {
t.Fatal("Access list should contain the storage slot")
} }
} }