Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/state/snapshot/difflayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
33 changes: 33 additions & 0 deletions core/state/snapshot/difflayer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down