trie, triedb: address comments

This commit is contained in:
Gary Rong 2024-10-16 14:42:46 +08:00
parent d6b2ebbe7b
commit ef42ebbcb1
4 changed files with 16 additions and 73 deletions

View file

@ -155,7 +155,7 @@ func (set *NodeSet) Size() (int, int) {
// HashSet returns a set of trie nodes keyed by node hash. // HashSet returns a set of trie nodes keyed by node hash.
func (set *NodeSet) HashSet() map[common.Hash][]byte { func (set *NodeSet) HashSet() map[common.Hash][]byte {
ret := make(map[common.Hash][]byte) ret := make(map[common.Hash][]byte, len(set.Nodes))
for _, n := range set.Nodes { for _, n := range set.Nodes {
ret[n.Hash] = n.Blob ret[n.Hash] = n.Blob
} }

View file

@ -1,53 +0,0 @@
// Copyright 2023 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>
package triestate
import "github.com/ethereum/go-ethereum/common"
// Set represents a collection of mutated states during a state transition.
// The value refers to the original content of state before the transition
// is made. Nil means that the state was not present previously.
type Set struct {
Accounts map[common.Address][]byte // Mutated account set, nil means the account was not present
Storages map[common.Address]map[common.Hash][]byte // Mutated storage set, nil means the slot was not present
size common.StorageSize // Approximate size of set
}
// New constructs the state set with provided data.
func New(accounts map[common.Address][]byte, storages map[common.Address]map[common.Hash][]byte) *Set {
return &Set{
Accounts: accounts,
Storages: storages,
}
}
// Size returns the approximate memory size occupied by the set.
func (s *Set) Size() common.StorageSize {
if s.size != 0 {
return s.size
}
for _, account := range s.Accounts {
s.size += common.StorageSize(common.AddressLength + len(account))
}
for _, slots := range s.Storages {
for _, val := range slots {
s.size += common.StorageSize(common.HashLength + len(val))
}
s.size += common.StorageSize(common.AddressLength)
}
return s.size
}

View file

@ -57,10 +57,6 @@ func (b *buffer) node(owner common.Hash, path []byte) (*trienode.Node, bool) {
} }
// commit merges the provided states and trie nodes into the buffer. // commit merges the provided states and trie nodes into the buffer.
//
// This operation does not take ownership of the passed maps, which belong to
// the bottom-most diff layer. Instead, it holds references to the given maps,
// which are safe to copy.
func (b *buffer) commit(nodes *nodeSet) *buffer { func (b *buffer) commit(nodes *nodeSet) *buffer {
b.layers++ b.layers++
b.nodes.merge(nodes) b.nodes.merge(nodes)
@ -133,7 +129,7 @@ func (b *buffer) flush(db ethdb.KeyValueStore, freezer ethdb.AncientWriter, node
return err return err
} }
} }
nodes := b.nodes.write(batch, b.nodes.nodes, nodesCache) nodes := b.nodes.write(batch, nodesCache)
rawdb.WritePersistentStateID(batch, id) rawdb.WritePersistentStateID(batch, id)
// Flush all mutations in a single batch // Flush all mutations in a single batch
@ -145,6 +141,6 @@ func (b *buffer) flush(db ethdb.KeyValueStore, freezer ethdb.AncientWriter, node
commitNodesMeter.Mark(int64(nodes)) commitNodesMeter.Mark(int64(nodes))
commitTimeTimer.UpdateSince(start) commitTimeTimer.UpdateSince(start)
b.reset() b.reset()
log.Info("Persisted buffer content", "nodes", nodes, "bytes", common.StorageSize(size), "elapsed", common.PrettyDuration(time.Since(start))) log.Debug("Persisted buffer content", "nodes", nodes, "bytes", common.StorageSize(size), "elapsed", common.PrettyDuration(time.Since(start)))
return nil return nil
} }

View file

@ -66,6 +66,17 @@ func (s *nodeSet) computeSize() {
s.size = size s.size = size
} }
// updateSize updates the total cache size by the given delta.
func (s *nodeSet) updateSize(delta int64) {
size := int64(s.size) + delta
if size >= 0 {
s.size = uint64(size)
return
}
log.Error("Nodeset size underflow", "prev", common.StorageSize(s.size), "delta", common.StorageSize(delta))
s.size = 0
}
// node retrieves the trie node with node path and its trie identifier. // node retrieves the trie node with node path and its trie identifier.
func (s *nodeSet) node(owner common.Hash, path []byte) (*trienode.Node, bool) { func (s *nodeSet) node(owner common.Hash, path []byte) (*trienode.Node, bool) {
subset, ok := s.nodes[owner] subset, ok := s.nodes[owner]
@ -211,8 +222,8 @@ func (s *nodeSet) decode(r *rlp.Stream) error {
} }
// write flushes nodes into the provided database batch as a whole. // write flushes nodes into the provided database batch as a whole.
func (s *nodeSet) write(batch ethdb.Batch, nodes map[common.Hash]map[string]*trienode.Node, clean *fastcache.Cache) int { func (s *nodeSet) write(batch ethdb.Batch, clean *fastcache.Cache) int {
return writeNodes(batch, nodes, clean) return writeNodes(batch, s.nodes, clean)
} }
// reset clears all cached trie node data. // reset clears all cached trie node data.
@ -221,17 +232,6 @@ func (s *nodeSet) reset() {
s.size = 0 s.size = 0
} }
// updateSize updates the total cache size by the given delta.
func (s *nodeSet) updateSize(delta int64) {
size := int64(s.size) + delta
if size >= 0 {
s.size = uint64(size)
return
}
log.Error("Nodeset size underflow", "prev", common.StorageSize(s.size), "delta", common.StorageSize(delta))
s.size = 0
}
// dbsize returns the approximate size of db write. // dbsize returns the approximate size of db write.
func (s *nodeSet) dbsize() int { func (s *nodeSet) dbsize() int {
var m int var m int