Skip to content

Commit 64ca976

Browse files
committed
core/rawdb: utilize AncientRange when initiating from freezer
1 parent 0e7efd6 commit 64ca976

File tree

1 file changed

+37
-14
lines changed

1 file changed

+37
-14
lines changed

core/rawdb/chain_iterator.go

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,29 @@ func InitDatabaseFromFreezer(db ethdb.Database) {
4444
logged = start.Add(-7 * time.Second) // Unindex during import is fast, don't double log
4545
hash common.Hash
4646
)
47-
for i := uint64(0); i < frozen; i++ {
48-
// Since the freezer has all data in sequential order on a file,
49-
// it would be 'neat' to read more data in one go, and let the
50-
// freezerdb return N items (e.g up to 1000 items per go)
51-
// That would require an API change in Ancients though
52-
if h, err := db.Ancient(freezerHashTable, i); err != nil {
47+
for i := uint64(0); i < frozen; {
48+
// We read 100K hashes at a time, for a total of 3.2M
49+
count := uint64(100_000)
50+
if i+count > frozen {
51+
count = frozen - i
52+
}
53+
data, err := db.AncientRange(freezerHashTable, i, count, 32*count)
54+
if err != nil {
5355
log.Crit("Failed to init database from freezer", "err", err)
54-
} else {
55-
hash = common.BytesToHash(h)
5656
}
57-
WriteHeaderNumber(batch, hash, i)
58-
// If enough data was accumulated in memory or we're at the last block, dump to disk
59-
if batch.ValueSize() > ethdb.IdealBatchSize {
60-
if err := batch.Write(); err != nil {
61-
log.Crit("Failed to write data to db", "err", err)
57+
for j, h := range data {
58+
number := i + uint64(j)
59+
hash = common.BytesToHash(h)
60+
WriteHeaderNumber(batch, hash, number)
61+
// If enough data was accumulated in memory or we're at the last block, dump to disk
62+
if batch.ValueSize() > ethdb.IdealBatchSize {
63+
if err := batch.Write(); err != nil {
64+
log.Crit("Failed to write data to db", "err", err)
65+
}
66+
batch.Reset()
6267
}
63-
batch.Reset()
6468
}
69+
i += uint64(len(data))
6570
// If we've spent too much time already, notify the user of what we're doing
6671
if time.Since(logged) > 8*time.Second {
6772
log.Info("Initializing database from freezer", "total", frozen, "number", i, "hash", hash, "elapsed", common.PrettyDuration(time.Since(start)))
@@ -76,6 +81,24 @@ func InitDatabaseFromFreezer(db ethdb.Database) {
7681
WriteHeadHeaderHash(db, hash)
7782
WriteHeadFastBlockHash(db, hash)
7883
log.Info("Initialized database from freezer", "blocks", frozen, "elapsed", common.PrettyDuration(time.Since(start)))
84+
/**
85+
Sanity check
86+
This code should be removed before merging this PR, but it
87+
is useful for validating that things work as expected.
88+
*/
89+
for i := uint64(0); i < frozen; i++ {
90+
h, err := db.Ancient(freezerHashTable, i)
91+
if err != nil {
92+
log.Crit("Something bad happened", "number", i, "err", err)
93+
}
94+
num := ReadHeaderNumber(db, common.BytesToHash(h))
95+
if num == nil {
96+
log.Crit("Missing hash->number mapping", "number", i, "err", err)
97+
}
98+
if *num != i {
99+
log.Crit("Erroneous hash->number mapping", "number", i)
100+
}
101+
}
79102
}
80103

81104
type blockTxHashes struct {

0 commit comments

Comments
 (0)