Skip to content

Commit

Permalink
Merge pull request #398 from OffchainLabs/triedb_commit_maintenance
Browse files Browse the repository at this point in the history
Flush triedb function
  • Loading branch information
tsahee authored Jan 23, 2025
2 parents 2bbf552 + 92e3134 commit 95d405a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
8 changes: 8 additions & 0 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

Expand Down
37 changes: 37 additions & 0 deletions core/blockchain_arbitrum.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 95d405a

Please sign in to comment.