diff --git a/core/blockchain.go b/core/blockchain.go index 1524ace48..a4402bf8c 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -78,6 +78,9 @@ var ( triedbCommitTimer = metrics.NewRegisteredResettingTimer("chain/triedb/commits", nil) + triedbSizeGauge = metrics.NewRegisteredGauge("chain/triedb/size", nil) + triedbGCProcGauge = metrics.NewRegisteredGauge("chain/triedb/gcproc", nil) + blockInsertTimer = metrics.NewRegisteredResettingTimer("chain/inserts", nil) blockValidationTimer = metrics.NewRegisteredResettingTimer("chain/validation", nil) blockExecutionTimer = metrics.NewRegisteredResettingTimer("chain/execution", nil) @@ -1654,6 +1657,11 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types. bc.triedb.Dereference(prevEntry.Root) } } + + _, dirtyNodesBufferedSize, _ := bc.triedb.Size() + triedbSizeGauge.Update(int64(dirtyNodesBufferedSize)) + triedbGCProcGauge.Update(int64(bc.gcproc)) + return nil } diff --git a/core/blockchain_arbitrum.go b/core/blockchain_arbitrum.go index 55966e98c..908f72691 100644 --- a/core/blockchain_arbitrum.go +++ b/core/blockchain_arbitrum.go @@ -19,14 +19,51 @@ package core import ( "fmt" + "sync" "time" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/rpc" ) +func (bc *BlockChain) FlushTrieDB(advanceBlockChainMutex *sync.Mutex, capLimit common.StorageSize) error { + if bc.triedb.Scheme() == rawdb.PathScheme { + return nil + } + + advanceBlockChainMutex.Lock() + defer advanceBlockChainMutex.Unlock() + + if !bc.triegc.Empty() { + _, triegcBlockNumber := bc.triegc.Peek() + blockNumber := uint64(-triegcBlockNumber) + + header := bc.GetHeaderByNumber(blockNumber) + if header == nil { + log.Warn("Reorg in progress, trie commit postponed") + } else { + err := bc.triedb.Commit(header.Root, true) + if err != nil { + return err + } + + bc.gcproc = 0 + bc.lastWrite = blockNumber + } + } + + err := bc.triedb.Cap(capLimit) + if err != nil { + return err + } + + return nil +} + // WriteBlockAndSetHeadWithTime also counts processTime, which will cause intermittent TrieDirty cache writes func (bc *BlockChain) WriteBlockAndSetHeadWithTime(block *types.Block, receipts []*types.Receipt, logs []*types.Log, state *state.StateDB, emitHeadEvent bool, processTime time.Duration) (status WriteStatus, err error) { if !bc.chainmu.TryLock() {