From d3f40d3d3dc015f45bcfd64dcc024fbffec0aea4 Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Wed, 20 Apr 2022 15:15:35 +0200 Subject: [PATCH 1/2] core,eth: add debug flush state --- core/blockchain.go | 7 +++++++ eth/api.go | 9 +++++++++ internal/web3ext/web3ext.go | 6 ++++++ 3 files changed, 22 insertions(+) diff --git a/core/blockchain.go b/core/blockchain.go index c6c845350349..74addd49fd2f 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -2324,3 +2324,10 @@ func (bc *BlockChain) InsertHeaderChain(chain []*types.Header, checkFreq int) (i _, err := bc.hc.InsertHeaderChain(chain, start, bc.forker) return 0, err } + +func (bc *BlockChain) FlushState(number uint64) error { + 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) +} diff --git a/eth/api.go b/eth/api.go index f81dfa922b7a..ad4c68a90fe7 100644 --- a/eth/api.go +++ b/eth/api.go @@ -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())) +} diff --git a/internal/web3ext/web3ext.go b/internal/web3ext/web3ext.go index 87bf464157ba..a653b118a371 100644 --- a/internal/web3ext/web3ext.go +++ b/internal/web3ext/web3ext.go @@ -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: [] }); From aa677c6dba4ddec5c7f683b5a9d576b22bebddd2 Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Fri, 22 Apr 2022 10:39:08 +0200 Subject: [PATCH 2/2] take chainmu --- core/blockchain.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/core/blockchain.go b/core/blockchain.go index 74addd49fd2f..f79a108012df 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -2325,7 +2325,13 @@ func (bc *BlockChain) InsertHeaderChain(chain []*types.Header, checkFreq int) (i 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())