Skip to content

Commit afecee4

Browse files
committed
core, triedb/pathdb: address comments from marius
1 parent e14f9ee commit afecee4

File tree

7 files changed

+41
-35
lines changed

7 files changed

+41
-35
lines changed

core/blockchain_repair_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1999,14 +1999,16 @@ func testIssue23496(t *testing.T, scheme string) {
19991999
}
20002000
expHead := uint64(1)
20012001
if scheme == rawdb.PathScheme {
2002+
// The pathdb database makes sure that snapshot and trie are consistent,
2003+
// so only the last block is reverted in case of a crash.
20022004
expHead = uint64(3)
20032005
}
20042006
if head := chain.CurrentBlock(); head.Number.Uint64() != expHead {
20052007
t.Errorf("Head block mismatch: have %d, want %d", head.Number, expHead)
20062008
}
20072009
if scheme == rawdb.PathScheme {
2008-
// Reinsert B3-B4
2009-
if _, err := chain.InsertChain(blocks[2:]); err != nil {
2010+
// Reinsert B4
2011+
if _, err := chain.InsertChain(blocks[3:]); err != nil {
20102012
t.Fatalf("Failed to import canonical chain tail: %v", err)
20112013
}
20122014
} else {

core/blockchain_snapshot_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -569,11 +569,13 @@ func TestHighCommitCrashWithNewSnapshot(t *testing.T) {
569569
//
570570
// Expected head header : C8
571571
// Expected head fast block: C8
572-
// Expected head block : G
573-
// Expected snapshot disk : C4
572+
// Expected head block : G (Hash mode), C6 (Hash mode)
573+
// Expected snapshot disk : C4 (Hash mode)
574574
for _, scheme := range []string{rawdb.HashScheme, rawdb.PathScheme} {
575575
expHead := uint64(0)
576576
if scheme == rawdb.PathScheme {
577+
// The pathdb database makes sure that snapshot and trie are consistent,
578+
// so only the last two blocks are reverted in case of a crash.
577579
expHead = uint64(6)
578580
}
579581
test := &crashSnapshotTest{

core/state/database.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -175,27 +175,27 @@ func NewDatabaseForTesting() *CachingDB {
175175
func (db *CachingDB) Reader(stateRoot common.Hash) (Reader, error) {
176176
var readers []StateReader
177177

178-
// Set up the state snapshot reader if available. This feature
179-
// is optional and may be partially useful if it's not fully
180-
// generated.
181-
if db.snap != nil {
182-
// If standalone state snapshot is available (hash scheme),
183-
// then construct the legacy snap reader.
178+
// Configure the state reader using the standalone snapshot in hash mode.
179+
// This reader offers improved performance but is optional and only
180+
// partially useful if the snapshot is not fully generated.
181+
if db.TrieDB().Scheme() == rawdb.HashScheme && db.snap != nil {
184182
snap := db.snap.Snapshot(stateRoot)
185183
if snap != nil {
186184
readers = append(readers, newFlatReader(snap))
187185
}
188-
} else {
189-
// If standalone state snapshot is not available (path scheme
190-
// or the state snapshot is explicitly disabled in hash mode),
191-
// try to construct the state reader with database.
186+
}
187+
// Configure the state reader using the path database in path mode.
188+
// This reader offers improved performance but is optional and only
189+
// partially useful if the snapshot data in path database is not
190+
// fully generated.
191+
if db.TrieDB().Scheme() == rawdb.PathScheme {
192192
reader, err := db.triedb.StateReader(stateRoot)
193193
if err == nil {
194-
readers = append(readers, newFlatReader(reader)) // state reader is optional
194+
readers = append(readers, newFlatReader(reader))
195195
}
196196
}
197-
// Set up the trie reader, which is expected to always be available
198-
// as the gatekeeper unless the state is corrupted.
197+
// Configure the trie reader, which is expected to be available as the
198+
// gatekeeper unless the state is corrupted.
199199
tr, err := newTrieReader(stateRoot, db.triedb, db.pointCache)
200200
if err != nil {
201201
return nil, err

triedb/pathdb/context.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,9 @@ func (ctx *generatorContext) removeStorageAt(account common.Hash) error {
224224
return nil
225225
}
226226

227-
// removeStorageLeft deletes all storage entries which are located after
227+
// removeRemainingStorage deletes all storage entries which are located after
228228
// the current iterator position.
229-
func (ctx *generatorContext) removeStorageLeft() uint64 {
229+
func (ctx *generatorContext) removeRemainingStorage() uint64 {
230230
var (
231231
count uint64
232232
start = time.Now()

triedb/pathdb/database.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -348,12 +348,11 @@ func (db *Database) setStateGenerator() {
348348
// Construct the generator and link it to the disk layer, ensuring that the
349349
// generation progress is resolved to prevent accessing uncovered states
350350
// regardless of whether background state snapshot generation is allowed.
351-
noBuild := db.readOnly || db.config.SnapshotNoBuild
351+
noBuild := db.readOnly || db.config.SnapshotNoBuild || db.isVerkle
352352
dl.setGenerator(newGenerator(db.diskdb, noBuild, generator.Marker, stats))
353353

354-
// Short circuit if the background generation is not permitted. Notably,
355-
// snapshot generation is not functional in the verkle design.
356-
if noBuild || db.isVerkle || db.waitSync {
354+
// Short circuit if the background generation is not permitted
355+
if noBuild || db.waitSync {
357356
return
358357
}
359358
stats.log("Starting snapshot generation", root, generator.Marker)
@@ -478,7 +477,7 @@ func (db *Database) Enable(root common.Hash) error {
478477

479478
// Re-construct a new disk layer backed by persistent state
480479
// and schedule the state snapshot generation if it's permitted.
481-
db.tree.reset(generateSnapshot(db, root))
480+
db.tree.reset(generateSnapshot(db, root, db.isVerkle || db.config.SnapshotNoBuild))
482481
log.Info("Rebuilt trie database", "root", root)
483482
return nil
484483
}

triedb/pathdb/generate.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ func (g *generator) progressMarker() []byte {
171171
// into two parts.
172172
func splitMarker(marker []byte) ([]byte, []byte) {
173173
var accMarker []byte
174-
if len(marker) > 0 { // []byte{} is the start, use nil for that
174+
if len(marker) > 0 {
175175
accMarker = marker[:common.HashLength]
176176
}
177177
return accMarker, marker
@@ -180,16 +180,19 @@ func splitMarker(marker []byte) ([]byte, []byte) {
180180
// generateSnapshot regenerates a brand-new snapshot based on an existing state
181181
// database and head block asynchronously. The snapshot is returned immediately
182182
// and generation is continued in the background until done.
183-
func generateSnapshot(triedb *Database, root common.Hash) *diskLayer {
183+
func generateSnapshot(triedb *Database, root common.Hash, noBuild bool) *diskLayer {
184184
// Create a new disk layer with an initialized state marker at zero
185185
var (
186186
stats = &generatorStats{start: time.Now()}
187187
genMarker = []byte{} // Initialized but empty!
188188
)
189189
dl := newDiskLayer(root, 0, triedb, nil, nil, newBuffer(triedb.config.WriteBufferSize, nil, nil, 0))
190-
dl.setGenerator(newGenerator(triedb.diskdb, false, genMarker, stats))
191-
dl.generator.run(root)
192-
log.Info("Started snapshot generation", "root", root)
190+
dl.setGenerator(newGenerator(triedb.diskdb, noBuild, genMarker, stats))
191+
192+
if !noBuild {
193+
dl.generator.run(root)
194+
log.Info("Started snapshot generation", "root", root)
195+
}
193196
return dl
194197
}
195198

@@ -751,7 +754,7 @@ func (g *generator) generateAccounts(ctx *generatorContext, accMarker []byte) er
751754
// Last step, cleanup the storages after the last account.
752755
// All the left storages should be treated as dangling.
753756
if origin == nil || exhausted {
754-
g.stats.dangling += ctx.removeStorageLeft()
757+
g.stats.dangling += ctx.removeRemainingStorage()
755758
break
756759
}
757760
}

triedb/pathdb/generate_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func (t *genTester) Commit() common.Hash {
129129

130130
func (t *genTester) CommitAndGenerate() (common.Hash, *diskLayer) {
131131
root := t.Commit()
132-
dl := generateSnapshot(t.db, root)
132+
dl := generateSnapshot(t.db, root, false)
133133
return root, dl
134134
}
135135

@@ -338,7 +338,7 @@ func TestGenerateCorruptAccountTrie(t *testing.T) {
338338
rawdb.DeleteAccountTrieNode(helper.diskdb, path)
339339
helper.db.tree.bottom().resetCache()
340340

341-
dl := generateSnapshot(helper.db, root)
341+
dl := generateSnapshot(helper.db, root, false)
342342
select {
343343
case <-dl.generator.done:
344344
// Snapshot generation succeeded
@@ -370,7 +370,7 @@ func TestGenerateMissingStorageTrie(t *testing.T) {
370370
rawdb.DeleteStorageTrieNode(helper.diskdb, acc3, nil)
371371
helper.db.tree.bottom().resetCache()
372372

373-
dl := generateSnapshot(helper.db, root)
373+
dl := generateSnapshot(helper.db, root, false)
374374
select {
375375
case <-dl.generator.done:
376376
// Snapshot generation succeeded
@@ -408,7 +408,7 @@ func TestGenerateCorruptStorageTrie(t *testing.T) {
408408

409409
helper.db.tree.bottom().resetCache()
410410

411-
dl := generateSnapshot(helper.db, root)
411+
dl := generateSnapshot(helper.db, root, false)
412412
select {
413413
case <-dl.generator.done:
414414
// Snapshot generation succeeded
@@ -463,7 +463,7 @@ func TestGenerateWithExtraAccounts(t *testing.T) {
463463
if data := rawdb.ReadStorageSnapshot(helper.diskdb, hashData([]byte("acc-2")), hashData([]byte("b-key-1"))); data == nil {
464464
t.Fatalf("expected snap storage to exist")
465465
}
466-
dl := generateSnapshot(helper.db, root)
466+
dl := generateSnapshot(helper.db, root, false)
467467
select {
468468
case <-dl.generator.done:
469469
// Snapshot generation succeeded

0 commit comments

Comments
 (0)