core/state/snapshot: fix storageList memory accounting (#33505)

This commit is contained in:
Bashmunta 2025-12-31 03:40:43 +02:00 committed by GitHub
parent 52ae75afcd
commit 25439aac04
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 34 additions and 1 deletions

View file

@ -465,6 +465,6 @@ func (dl *diffLayer) StorageList(accountHash common.Hash) []common.Hash {
storageList := slices.SortedFunc(maps.Keys(dl.storageData[accountHash]), common.Hash.Cmp)
dl.storageList[accountHash] = storageList
dl.memory += uint64(len(dl.storageList)*common.HashLength + common.HashLength)
dl.memory += uint64(len(storageList)*common.HashLength + common.HashLength)
return storageList
}

View file

@ -198,6 +198,39 @@ func TestInsertAndMerge(t *testing.T) {
}
}
// TestStorageListMemoryAccounting ensures that StorageList increases dl.memory
// proportionally to the number of storage slots in the requested account and
// does not change memory usage on repeated calls for the same account.
func TestStorageListMemoryAccounting(t *testing.T) {
parent := newDiffLayer(emptyLayer(), common.Hash{}, nil, nil)
account := common.HexToHash("0x01")
slots := make(map[common.Hash][]byte)
for i := 0; i < 3; i++ {
slots[randomHash()] = []byte{0x01}
}
storage := map[common.Hash]map[common.Hash][]byte{
account: slots,
}
dl := newDiffLayer(parent, common.Hash{}, nil, storage)
before := dl.memory
list := dl.StorageList(account)
if have, want := len(list), len(slots); have != want {
t.Fatalf("StorageList length mismatch: have %d, want %d", have, want)
}
expectedDelta := uint64(len(list)*common.HashLength + common.HashLength)
if have, want := dl.memory-before, expectedDelta; have != want {
t.Fatalf("StorageList memory delta mismatch: have %d, want %d", have, want)
}
before = dl.memory
_ = dl.StorageList(account)
if dl.memory != before {
t.Fatalf("StorageList changed memory on cached call: have %d, want %d", dl.memory, before)
}
}
func emptyLayer() *diskLayer {
return &diskLayer{
diskdb: memorydb.New(),