From e3e4e1e41c1338555b854b66e2d54c69dc46cb76 Mon Sep 17 00:00:00 2001 From: Roy Crihfield Date: Thu, 20 Apr 2023 17:29:53 +0800 Subject: [PATCH] Fix storage paths GetState takes a slot, not a leaf path --- state_object.go | 3 ++- statedb_test.go | 15 ++++++++------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/state_object.go b/state_object.go index 31fbc81..b18c5bc 100644 --- a/state_object.go +++ b/state_object.go @@ -159,7 +159,8 @@ func (s *stateObject) GetCommittedState(db StateDatabase, key common.Hash) commo } // If no live objects are available, load from database start := time.Now() - enc, err := db.StorageValue(s.addrHash, key, s.blockHash) + keyHash := crypto.Keccak256Hash(key[:]) + enc, err := db.StorageValue(s.addrHash, keyHash, s.blockHash) if metrics.EnabledExpensive { s.db.StorageReads += time.Since(start) } diff --git a/statedb_test.go b/statedb_test.go index bb097b9..e26dd4f 100644 --- a/statedb_test.go +++ b/statedb_test.go @@ -64,7 +64,8 @@ var ( Root: common.Hash{}, } - StorageLeafKey = crypto.Keccak256Hash(common.HexToHash("0").Bytes()) + StorageSlot = common.HexToHash("0") + StorageLeafKey = crypto.Keccak256Hash(StorageSlot[:]) StoredValue = crypto.Keccak256Hash([]byte{1, 2, 3, 4, 5}) StoragePartialPath = []byte{0, 1, 0, 2, 0, 4} @@ -301,7 +302,7 @@ func testSuite(t *testing.T, db statedb.StateDatabase) { checkAccountUnchanged := func() { require.Equal(t, Account.Balance, sdb.GetBalance(AccountAddress)) require.Equal(t, Account.Nonce, sdb.GetNonce(AccountAddress)) - require.Equal(t, StoredValue, sdb.GetState(AccountAddress, StorageLeafKey)) + require.Equal(t, StoredValue, sdb.GetState(AccountAddress, StorageSlot)) require.Equal(t, AccountCodeHash, sdb.GetCodeHash(AccountAddress)) require.Equal(t, AccountCode, sdb.GetCode(AccountAddress)) require.Equal(t, len(AccountCode), sdb.GetCodeSize(AccountAddress)) @@ -319,17 +320,17 @@ func testSuite(t *testing.T, db statedb.StateDatabase) { sdb.AddBalance(AccountAddress, big.NewInt(200)) sdb.SubBalance(AccountAddress, big.NewInt(100)) sdb.SetNonce(AccountAddress, 42) - sdb.SetState(AccountAddress, StorageLeafKey, newStorage) + sdb.SetState(AccountAddress, StorageSlot, newStorage) sdb.SetCode(AccountAddress, newCode) require.Equal(t, big.NewInt(400), sdb.GetBalance(AccountAddress)) require.Equal(t, uint64(42), sdb.GetNonce(AccountAddress)) - require.Equal(t, newStorage, sdb.GetState(AccountAddress, StorageLeafKey)) + require.Equal(t, newStorage, sdb.GetState(AccountAddress, StorageSlot)) require.Equal(t, newCode, sdb.GetCode(AccountAddress)) - sdb.AddSlotToAccessList(AccountAddress, StorageLeafKey) + sdb.AddSlotToAccessList(AccountAddress, StorageSlot) require.True(t, sdb.AddressInAccessList(AccountAddress)) - hasAddr, hasSlot := sdb.SlotInAccessList(AccountAddress, StorageLeafKey) + hasAddr, hasSlot := sdb.SlotInAccessList(AccountAddress, StorageSlot) require.True(t, hasAddr) require.True(t, hasSlot) @@ -337,7 +338,7 @@ func testSuite(t *testing.T, db statedb.StateDatabase) { checkAccountUnchanged() require.False(t, sdb.AddressInAccessList(AccountAddress)) - hasAddr, hasSlot = sdb.SlotInAccessList(AccountAddress, StorageLeafKey) + hasAddr, hasSlot = sdb.SlotInAccessList(AccountAddress, StorageSlot) require.False(t, hasAddr) require.False(t, hasSlot) })