Skip to content

Commit 61ff3e8

Browse files
authored
core/state/snapshot, ethdb: track deletions more accurately (#22582)
* core/state/snapshot, ethdb: track deletions more accurately * core/state/snapshot: don't reset the iterator, leveldb's screwy * ethdb: don't mess with the insert batches for now
1 parent 3faae5d commit 61ff3e8

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

core/state/snapshot/snapshot.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,8 +484,17 @@ func diffToDisk(bottom *diffLayer) *diskLayer {
484484
if key := it.Key(); len(key) == 65 { // TODO(karalabe): Yuck, we should move this into the iterator
485485
batch.Delete(key)
486486
base.cache.Del(key[1:])
487-
488487
snapshotFlushStorageItemMeter.Mark(1)
488+
489+
// Ensure we don't delete too much data blindly (contract can be
490+
// huge). It's ok to flush, the root will go missing in case of a
491+
// crash and we'll detect and regenerate the snapshot.
492+
if batch.ValueSize() > ethdb.IdealBatchSize {
493+
if err := batch.Write(); err != nil {
494+
log.Crit("Failed to write storage deletions", "err", err)
495+
}
496+
batch.Reset()
497+
}
489498
}
490499
}
491500
it.Release()
@@ -503,6 +512,16 @@ func diffToDisk(bottom *diffLayer) *diskLayer {
503512

504513
snapshotFlushAccountItemMeter.Mark(1)
505514
snapshotFlushAccountSizeMeter.Mark(int64(len(data)))
515+
516+
// Ensure we don't write too much data blindly. It's ok to flush, the
517+
// root will go missing in case of a crash and we'll detect and regen
518+
// the snapshot.
519+
if batch.ValueSize() > ethdb.IdealBatchSize {
520+
if err := batch.Write(); err != nil {
521+
log.Crit("Failed to write storage deletions", "err", err)
522+
}
523+
batch.Reset()
524+
}
506525
}
507526
// Push all the storage slots into the database
508527
for accountHash, storage := range bottom.storageData {

ethdb/leveldb/leveldb.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ func (b *batch) Put(key, value []byte) error {
461461
// Delete inserts the a key removal into the batch for later committing.
462462
func (b *batch) Delete(key []byte) error {
463463
b.b.Delete(key)
464-
b.size++
464+
b.size += len(key)
465465
return nil
466466
}
467467

ethdb/memorydb/memorydb.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ func (b *batch) Put(key, value []byte) error {
211211
// Delete inserts the a key removal into the batch for later committing.
212212
func (b *batch) Delete(key []byte) error {
213213
b.writes = append(b.writes, keyvalue{common.CopyBytes(key), nil, true})
214-
b.size += 1
214+
b.size += len(key)
215215
return nil
216216
}
217217

0 commit comments

Comments
 (0)