From 9b15ab48ad086abb0ce0424d5e369b14fad0a9ac Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Wed, 7 Apr 2021 11:14:18 +0300 Subject: [PATCH] core: restore MPT collapsing logic Fix #1867, revert part of bf20db09e05fae4d7e3ef142b823bab8df32ce7e. --- pkg/core/blockchain.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index 41c2758e2..f6cd03577 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -754,6 +754,13 @@ func (bc *Blockchain) storeBlock(block *block.Block, txpool *mempool.Pool) error writeBuf.Reset() } } + // Every persist cycle we also compact our in-memory MPT. It's flushed + // already in AddMPTBatch, so collapsing it is safe. + persistedHeight := atomic.LoadUint32(&bc.persistedHeight) + if persistedHeight == block.Index-1 { + // 10 is good and roughly estimated to fit remaining trie into 1M of memory. + mpt.Collapse(10) + } bc.lock.Lock() _, err = cache.Persist()