Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core,eth: api for flushing block state #24736

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2324,3 +2324,16 @@ func (bc *BlockChain) InsertHeaderChain(chain []*types.Header, checkFreq int) (i
_, err := bc.hc.InsertHeaderChain(chain, start, bc.forker)
return 0, err
}

// FlushState persists the post-state of a block to disk.
func (bc *BlockChain) FlushState(number uint64) error {
if !bc.chainmu.TryLock() {
return errChainStopped
}
defer bc.chainmu.Unlock()

triedb := bc.stateCache.TrieDB()
block := bc.GetBlockByNumber(number)
log.Info("Writing cached state to disk", "block", block.Number(), "hash", block.Hash(), "root", block.Root())
return triedb.Commit(block.Root(), true, nil)
}
9 changes: 9 additions & 0 deletions eth/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -607,3 +607,12 @@ func (api *PrivateDebugAPI) GetAccessibleState(from, to rpc.BlockNumber) (uint64
}
return 0, fmt.Errorf("No state found")
}

// FlushState persists the state of a given block to disk.
func (api *PrivateDebugAPI) FlushState(number rpc.BlockNumber) error {
// Genesis, latest and pending not supported
if number.Int64() <= 0 {
return fmt.Errorf("Invalid block number")
}
return api.eth.BlockChain().FlushState(uint64(number.Int64()))
}
6 changes: 6 additions & 0 deletions internal/web3ext/web3ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,12 @@ web3._extend({
params: 2,
inputFormatter:[web3._extend.formatters.inputBlockNumberFormatter, web3._extend.formatters.inputBlockNumberFormatter],
}),
new web3._extend.Method({
name: 'flushState',
call: 'debug_flushState',
params: 1,
inputFormatter:[web3._extend.formatters.inputBlockNumberFormatter],
}),
],
properties: []
});
Expand Down