From 71f4221fb38b50ab20a26e84974f7639eba73947 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9ter=20Szil=C3=A1gyi?= Date: Tue, 21 May 2019 14:59:52 +0300 Subject: [PATCH] core: reinit chain from freezer in batches --- core/blockchain.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index 60707281c2..63b97232dd 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -222,24 +222,30 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par if bc.empty() { if frozen, err := bc.db.Ancients(); err == nil && frozen > 0 { var ( + batch = bc.db.NewBatch() start = time.Now() logged time.Time ) for i := uint64(0); i < frozen; i++ { - // Inject hash<->number mapping. + // Retrieve the canonical hash and block for tx index hash := rawdb.ReadCanonicalHash(bc.db, i) if hash == (common.Hash{}) { return nil, errors.New("broken ancient database") } - rawdb.WriteHeaderNumber(bc.db, hash, i) - - // Inject txlookup indexes. block := rawdb.ReadBlock(bc.db, hash, i) if block == nil { return nil, errors.New("broken ancient database") } - rawdb.WriteTxLookupEntries(bc.db, block) + // Inject hash<->number mapping and txlookup indexes + rawdb.WriteHeaderNumber(batch, hash, i) + rawdb.WriteTxLookupEntries(batch, block) + if batch.ValueSize() > ethdb.IdealBatchSize || i == frozen-1 { + if err := batch.Write(); err != nil { + return nil, err + } + batch.Reset() + } // If we've spent too much time already, notify the user of what we're doing if time.Since(logged) > 8*time.Second { log.Info("Initializing chain from ancient data", "number", i, "hash", hash, "total", frozen-1, "elapsed", common.PrettyDuration(time.Since(start)))