refactor test

This commit is contained in:
Sina Mahmoodi 2025-03-26 16:04:08 +01:00
parent 62a07266b3
commit 6f8df04a3e

View file

@ -3534,7 +3534,7 @@ func TestCreateAccessListWithStateOverrides(t *testing.T) {
// Initialize test backend // Initialize test backend
genesis := &core.Genesis{ genesis := &core.Genesis{
Config: params.TestChainConfig, Config: params.TestChainConfig,
Alloc: core.GenesisAlloc{ Alloc: types.GenesisAlloc{
common.HexToAddress("0x71562b71999873db5b286df957af199ec94617f7"): {Balance: big.NewInt(1000000000000000000)}, common.HexToAddress("0x71562b71999873db5b286df957af199ec94617f7"): {Balance: big.NewInt(1000000000000000000)},
}, },
} }
@ -3555,78 +3555,52 @@ func TestCreateAccessListWithStateOverrides(t *testing.T) {
// return value; // return value;
// } // }
// } // }
contractCode := common.Hex2Bytes("6080604052348015600f57600080fd5b506004361060285760003560e01c80632e64cec114602d575b600080fd5b60336047565b604051603e91906067565b60405180910390f35b60008054905090565b6000819050919050565b6061816050565b82525050565b6000602082019050607a6000830184605a565b9291505056") var (
codeBytes := hexutil.Bytes(contractCode) contractCode = hexutil.Bytes(common.Hex2Bytes("6080604052348015600f57600080fd5b506004361060285760003560e01c80632e64cec114602d575b600080fd5b60336047565b604051603e91906067565b60405180910390f35b60008054905090565b6000819050919050565b6061816050565b82525050565b6000602082019050607a6000830184605a565b9291505056"))
// Create state overrides with more complete state // Create state overrides with more complete state
contractAddr := common.HexToAddress("0x1234567890123456789012345678901234567890") contractAddr = common.HexToAddress("0x1234567890123456789012345678901234567890")
balance := (*hexutil.Big)(big.NewInt(1000000000000000000)) nonce = hexutil.Uint64(1)
nonce := hexutil.Uint64(1) overrides = &override.StateOverride{
// Add initial storage values
storage := map[common.Hash]common.Hash{
common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"): common.HexToHash("0x000000000000000000000000000000000000000000000000000000000000002a"),
}
overrides := &override.StateOverride{
contractAddr: override.OverrideAccount{ contractAddr: override.OverrideAccount{
Code: &codeBytes, Code: &contractCode,
Balance: balance, Balance: (*hexutil.Big)(big.NewInt(1000000000000000000)),
Nonce: &nonce, Nonce: &nonce,
State: storage, State: map[common.Hash]common.Hash{
common.Hash{}: common.HexToHash("0x000000000000000000000000000000000000000000000000000000000000002a"),
},
}, },
} }
)
// Create transaction arguments with gas and value // Create transaction arguments with gas and value
from := common.HexToAddress("0x71562b71999873db5b286df957af199ec94617f7") var (
to := contractAddr from = common.HexToAddress("0x71562b71999873db5b286df957af199ec94617f7")
data := hexutil.Bytes(common.Hex2Bytes("2e64cec1")) // retrieve() data = hexutil.Bytes(common.Hex2Bytes("2e64cec1")) // retrieve()
gas := hexutil.Uint64(100000) gas = hexutil.Uint64(100000)
value := (*hexutil.Big)(big.NewInt(0)) args = TransactionArgs{
args := TransactionArgs{
From: &from, From: &from,
To: &to, To: &contractAddr,
Data: &data, Data: &data,
Gas: &gas, Gas: &gas,
Value: value, Value: new(hexutil.Big),
} }
)
// Call CreateAccessList // Call CreateAccessList
result, err := api.CreateAccessList(context.Background(), args, nil, overrides) result, err := api.CreateAccessList(context.Background(), args, nil, overrides)
if err != nil { if err != nil {
t.Fatalf("Failed to create access list: %v", err) t.Fatalf("Failed to create access list: %v", err)
} }
if err != nil || result == nil {
// Verify the result t.Fatalf("Failed to create access list: %v", err)
if result == nil {
t.Fatal("Expected non-nil result")
}
if result.Accesslist == nil {
t.Fatal("Expected non-nil access list")
}
if result.GasUsed == 0 {
t.Fatal("Expected non-zero gas used")
} }
require.NotNil(t, result.Accesslist)
// Verify access list contains the contract address and storage slot // Verify access list contains the contract address and storage slot
foundAddr := false expected := &types.AccessList{{
foundSlot := false Address: contractAddr,
for _, tuple := range *result.Accesslist { StorageKeys: []common.Hash{
if tuple.Address == contractAddr { common.Hash{},
foundAddr = true },
for _, slot := range tuple.StorageKeys { }}
if slot == common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000") { require.Equal(t, expected, result.Accesslist)
foundSlot = true
break
}
}
break
}
}
if !foundAddr {
t.Fatal("Access list should contain the contract address")
}
if !foundSlot {
t.Fatal("Access list should contain the storage slot")
}
} }