add a way to cleanup snaps on state

This commit is contained in:
jstr1121 2023-10-26 21:01:33 +08:00
parent 3ef9c7fbe1
commit 6735dde9ed
3 changed files with 16 additions and 3 deletions

View file

@ -180,6 +180,11 @@ func (s *StateDB) GetWitness() *trienode.Witness {
return s.trie.GetWitness() return s.trie.GetWitness()
} }
func (s *StateDB) CleanSnaps() {
s.snaps = nil
s.snap = nil
}
// StartPrefetcher initializes a new trie prefetcher to pull in nodes from the // StartPrefetcher initializes a new trie prefetcher to pull in nodes from the
// state trie concurrently while the state is mutated so that when we reach the // state trie concurrently while the state is mutated so that when we reach the
// commit phase, most of the needed data is already hot. // commit phase, most of the needed data is already hot.

View file

@ -972,6 +972,7 @@ func (s *BlockChainAPI) GetRequiredBlockState(ctx context.Context, blockNrOrHash
if err != nil { if err != nil {
return nil, nil return nil, nil
} }
state.CleanSnaps()
s.ProcessBlock(ctx, block, state, vm.Config{}) s.ProcessBlock(ctx, block, state, vm.Config{})
return state.GetWitness(), nil return state.GetWitness(), nil
// return s.traceBlock(ctx, block) // return s.traceBlock(ctx, block)

View file

@ -32,13 +32,20 @@ type Witness struct {
} }
func (u *Witness) MarshalJSON() ([]byte, error) { func (u *Witness) MarshalJSON() ([]byte, error) {
nodes := make(map[string]string) type Node struct {
Key string
Value string
}
nodes := []Node{}
for key, node := range u.Nodes { for key, node := range u.Nodes {
nodes[key] = hex.EncodeToString(node) nodes = append(nodes, Node{
Key: hex.EncodeToString([]byte(key)),
Value: hex.EncodeToString(node),
})
} }
return json.Marshal(&struct { return json.Marshal(&struct {
Owner common.Hash Owner common.Hash
Nodes map[string]string Nodes []Node
}{ }{
Owner: u.Owner, Owner: u.Owner,
Nodes: nodes, Nodes: nodes,