core, light, tests, trie: add state metrics (#23433)
This commit is contained in:
@ -72,24 +72,24 @@ func returnCommitterToPool(h *committer) {
|
||||
committerPool.Put(h)
|
||||
}
|
||||
|
||||
// commit collapses a node down into a hash node and inserts it into the database
|
||||
func (c *committer) Commit(n node, db *Database) (hashNode, error) {
|
||||
// Commit collapses a node down into a hash node and inserts it into the database
|
||||
func (c *committer) Commit(n node, db *Database) (hashNode, int, error) {
|
||||
if db == nil {
|
||||
return nil, errors.New("no db provided")
|
||||
return nil, 0, errors.New("no db provided")
|
||||
}
|
||||
h, err := c.commit(n, db)
|
||||
h, committed, err := c.commit(n, db)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, 0, err
|
||||
}
|
||||
return h.(hashNode), nil
|
||||
return h.(hashNode), committed, nil
|
||||
}
|
||||
|
||||
// commit collapses a node down into a hash node and inserts it into the database
|
||||
func (c *committer) commit(n node, db *Database) (node, error) {
|
||||
func (c *committer) commit(n node, db *Database) (node, int, error) {
|
||||
// if this path is clean, use available cached data
|
||||
hash, dirty := n.cache()
|
||||
if hash != nil && !dirty {
|
||||
return hash, nil
|
||||
return hash, 0, nil
|
||||
}
|
||||
// Commit children, then parent, and remove remove the dirty flag.
|
||||
switch cn := n.(type) {
|
||||
@ -97,37 +97,38 @@ func (c *committer) commit(n node, db *Database) (node, error) {
|
||||
// Commit child
|
||||
collapsed := cn.copy()
|
||||
|
||||
// If the child is fullnode, recursively commit.
|
||||
// Otherwise it can only be hashNode or valueNode.
|
||||
// If the child is fullNode, recursively commit,
|
||||
// otherwise it can only be hashNode or valueNode.
|
||||
var childCommitted int
|
||||
if _, ok := cn.Val.(*fullNode); ok {
|
||||
childV, err := c.commit(cn.Val, db)
|
||||
childV, committed, err := c.commit(cn.Val, db)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, 0, err
|
||||
}
|
||||
collapsed.Val = childV
|
||||
collapsed.Val, childCommitted = childV, committed
|
||||
}
|
||||
// The key needs to be copied, since we're delivering it to database
|
||||
collapsed.Key = hexToCompact(cn.Key)
|
||||
hashedNode := c.store(collapsed, db)
|
||||
if hn, ok := hashedNode.(hashNode); ok {
|
||||
return hn, nil
|
||||
return hn, childCommitted + 1, nil
|
||||
}
|
||||
return collapsed, nil
|
||||
return collapsed, childCommitted, nil
|
||||
case *fullNode:
|
||||
hashedKids, err := c.commitChildren(cn, db)
|
||||
hashedKids, childCommitted, err := c.commitChildren(cn, db)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, 0, err
|
||||
}
|
||||
collapsed := cn.copy()
|
||||
collapsed.Children = hashedKids
|
||||
|
||||
hashedNode := c.store(collapsed, db)
|
||||
if hn, ok := hashedNode.(hashNode); ok {
|
||||
return hn, nil
|
||||
return hn, childCommitted + 1, nil
|
||||
}
|
||||
return collapsed, nil
|
||||
return collapsed, childCommitted, nil
|
||||
case hashNode:
|
||||
return cn, nil
|
||||
return cn, 0, nil
|
||||
default:
|
||||
// nil, valuenode shouldn't be committed
|
||||
panic(fmt.Sprintf("%T: invalid node: %v", n, n))
|
||||
@ -135,8 +136,11 @@ func (c *committer) commit(n node, db *Database) (node, error) {
|
||||
}
|
||||
|
||||
// commitChildren commits the children of the given fullnode
|
||||
func (c *committer) commitChildren(n *fullNode, db *Database) ([17]node, error) {
|
||||
var children [17]node
|
||||
func (c *committer) commitChildren(n *fullNode, db *Database) ([17]node, int, error) {
|
||||
var (
|
||||
committed int
|
||||
children [17]node
|
||||
)
|
||||
for i := 0; i < 16; i++ {
|
||||
child := n.Children[i]
|
||||
if child == nil {
|
||||
@ -144,25 +148,26 @@ func (c *committer) commitChildren(n *fullNode, db *Database) ([17]node, error)
|
||||
}
|
||||
// If it's the hashed child, save the hash value directly.
|
||||
// Note: it's impossible that the child in range [0, 15]
|
||||
// is a valuenode.
|
||||
// is a valueNode.
|
||||
if hn, ok := child.(hashNode); ok {
|
||||
children[i] = hn
|
||||
continue
|
||||
}
|
||||
// Commit the child recursively and store the "hashed" value.
|
||||
// Note the returned node can be some embedded nodes, so it's
|
||||
// possible the type is not hashnode.
|
||||
hashed, err := c.commit(child, db)
|
||||
// possible the type is not hashNode.
|
||||
hashed, childCommitted, err := c.commit(child, db)
|
||||
if err != nil {
|
||||
return children, err
|
||||
return children, 0, err
|
||||
}
|
||||
children[i] = hashed
|
||||
committed += childCommitted
|
||||
}
|
||||
// For the 17th child, it's possible the type is valuenode.
|
||||
if n.Children[16] != nil {
|
||||
children[16] = n.Children[16]
|
||||
}
|
||||
return children, nil
|
||||
return children, committed, nil
|
||||
}
|
||||
|
||||
// store hashes the node n and if we have a storage layer specified, it writes
|
||||
@ -176,7 +181,7 @@ func (c *committer) store(n node, db *Database) node {
|
||||
)
|
||||
if hash == nil {
|
||||
// This was not generated - must be a small node stored in the parent.
|
||||
// In theory we should apply the leafCall here if it's not nil(embedded
|
||||
// In theory, we should apply the leafCall here if it's not nil(embedded
|
||||
// node usually contains value). But small value(less than 32bytes) is
|
||||
// not our target.
|
||||
return n
|
||||
@ -224,7 +229,7 @@ func (c *committer) commitLoop(db *Database) {
|
||||
}
|
||||
case *fullNode:
|
||||
// For children in range [0, 15], it's impossible
|
||||
// to contain valuenode. Only check the 17th child.
|
||||
// to contain valueNode. Only check the 17th child.
|
||||
if n.Children[16] != nil {
|
||||
c.onleaf(nil, nil, n.Children[16].(valueNode), hash)
|
||||
}
|
||||
|
@ -393,7 +393,7 @@ func testIteratorContinueAfterSeekError(t *testing.T, memonly bool) {
|
||||
for _, val := range testdata1 {
|
||||
ctr.Update([]byte(val.k), []byte(val.v))
|
||||
}
|
||||
root, _ := ctr.Commit(nil)
|
||||
root, _, _ := ctr.Commit(nil)
|
||||
if !memonly {
|
||||
triedb.Commit(root, true, nil)
|
||||
}
|
||||
|
@ -144,7 +144,7 @@ func (t *SecureTrie) GetKey(shaKey []byte) []byte {
|
||||
//
|
||||
// Committing flushes nodes from memory. Subsequent Get calls will load nodes
|
||||
// from the database.
|
||||
func (t *SecureTrie) Commit(onleaf LeafCallback) (root common.Hash, err error) {
|
||||
func (t *SecureTrie) Commit(onleaf LeafCallback) (common.Hash, int, error) {
|
||||
// Write all the pre-images to the actual disk database
|
||||
if len(t.getSecKeyCache()) > 0 {
|
||||
if t.trie.db.preimages != nil { // Ugly direct check but avoids the below write lock
|
||||
|
13
trie/trie.go
13
trie/trie.go
@ -514,12 +514,12 @@ func (t *Trie) Hash() common.Hash {
|
||||
|
||||
// Commit writes all nodes to the trie's memory database, tracking the internal
|
||||
// and external (for account tries) references.
|
||||
func (t *Trie) Commit(onleaf LeafCallback) (root common.Hash, err error) {
|
||||
func (t *Trie) Commit(onleaf LeafCallback) (common.Hash, int, error) {
|
||||
if t.db == nil {
|
||||
panic("commit called on trie with nil database")
|
||||
}
|
||||
if t.root == nil {
|
||||
return emptyRoot, nil
|
||||
return emptyRoot, 0, nil
|
||||
}
|
||||
// Derive the hash for all dirty nodes first. We hold the assumption
|
||||
// in the following procedure that all nodes are hashed.
|
||||
@ -531,7 +531,7 @@ func (t *Trie) Commit(onleaf LeafCallback) (root common.Hash, err error) {
|
||||
// up goroutines. This can happen e.g. if we load a trie for reading storage
|
||||
// values, but don't write to it.
|
||||
if _, dirty := t.root.cache(); !dirty {
|
||||
return rootHash, nil
|
||||
return rootHash, 0, nil
|
||||
}
|
||||
var wg sync.WaitGroup
|
||||
if onleaf != nil {
|
||||
@ -543,8 +543,7 @@ func (t *Trie) Commit(onleaf LeafCallback) (root common.Hash, err error) {
|
||||
h.commitLoop(t.db)
|
||||
}()
|
||||
}
|
||||
var newRoot hashNode
|
||||
newRoot, err = h.Commit(t.root, t.db)
|
||||
newRoot, committed, err := h.Commit(t.root, t.db)
|
||||
if onleaf != nil {
|
||||
// The leafch is created in newCommitter if there was an onleaf callback
|
||||
// provided. The commitLoop only _reads_ from it, and the commit
|
||||
@ -554,10 +553,10 @@ func (t *Trie) Commit(onleaf LeafCallback) (root common.Hash, err error) {
|
||||
wg.Wait()
|
||||
}
|
||||
if err != nil {
|
||||
return common.Hash{}, err
|
||||
return common.Hash{}, 0, err
|
||||
}
|
||||
t.root = newRoot
|
||||
return rootHash, nil
|
||||
return rootHash, committed, nil
|
||||
}
|
||||
|
||||
// hashRoot calculates the root hash of the given trie
|
||||
|
@ -90,7 +90,7 @@ func testMissingNode(t *testing.T, memonly bool) {
|
||||
trie, _ := New(common.Hash{}, triedb)
|
||||
updateString(trie, "120000", "qwerqwerqwerqwerqwerqwerqwerqwer")
|
||||
updateString(trie, "123456", "asdfasdfasdfasdfasdfasdfasdfasdf")
|
||||
root, _ := trie.Commit(nil)
|
||||
root, _, _ := trie.Commit(nil)
|
||||
if !memonly {
|
||||
triedb.Commit(root, true, nil)
|
||||
}
|
||||
@ -172,7 +172,7 @@ func TestInsert(t *testing.T) {
|
||||
updateString(trie, "A", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
|
||||
|
||||
exp = common.HexToHash("d23786fb4a010da3ce639d66d5e904a11dbc02746d1ce25029e53290cabf28ab")
|
||||
root, err := trie.Commit(nil)
|
||||
root, _, err := trie.Commit(nil)
|
||||
if err != nil {
|
||||
t.Fatalf("commit error: %v", err)
|
||||
}
|
||||
@ -270,7 +270,7 @@ func TestReplication(t *testing.T) {
|
||||
for _, val := range vals {
|
||||
updateString(trie, val.k, val.v)
|
||||
}
|
||||
exp, err := trie.Commit(nil)
|
||||
exp, _, err := trie.Commit(nil)
|
||||
if err != nil {
|
||||
t.Fatalf("commit error: %v", err)
|
||||
}
|
||||
@ -285,7 +285,7 @@ func TestReplication(t *testing.T) {
|
||||
t.Errorf("trie2 doesn't have %q => %q", kv.k, kv.v)
|
||||
}
|
||||
}
|
||||
hash, err := trie2.Commit(nil)
|
||||
hash, _, err := trie2.Commit(nil)
|
||||
if err != nil {
|
||||
t.Fatalf("commit error: %v", err)
|
||||
}
|
||||
@ -429,11 +429,11 @@ func runRandTest(rt randTest) bool {
|
||||
rt[i].err = fmt.Errorf("mismatch for key 0x%x, got 0x%x want 0x%x", step.key, v, want)
|
||||
}
|
||||
case opCommit:
|
||||
_, rt[i].err = tr.Commit(nil)
|
||||
_, _, rt[i].err = tr.Commit(nil)
|
||||
case opHash:
|
||||
tr.Hash()
|
||||
case opReset:
|
||||
hash, err := tr.Commit(nil)
|
||||
hash, _, err := tr.Commit(nil)
|
||||
if err != nil {
|
||||
rt[i].err = err
|
||||
return false
|
||||
@ -633,7 +633,7 @@ func TestCommitAfterHash(t *testing.T) {
|
||||
if exp != root {
|
||||
t.Errorf("got %x, exp %x", root, exp)
|
||||
}
|
||||
root, _ = trie.Commit(nil)
|
||||
root, _, _ = trie.Commit(nil)
|
||||
if exp != root {
|
||||
t.Errorf("got %x, exp %x", root, exp)
|
||||
}
|
||||
@ -740,7 +740,7 @@ func TestCommitSequence(t *testing.T) {
|
||||
trie.Update(crypto.Keccak256(addresses[i][:]), accounts[i])
|
||||
}
|
||||
// Flush trie -> database
|
||||
root, _ := trie.Commit(nil)
|
||||
root, _, _ := trie.Commit(nil)
|
||||
// Flush memdb -> disk (sponge)
|
||||
db.Commit(root, false, func(c common.Hash) {
|
||||
// And spongify the callback-order
|
||||
@ -792,7 +792,7 @@ func TestCommitSequenceRandomBlobs(t *testing.T) {
|
||||
trie.Update(key, val)
|
||||
}
|
||||
// Flush trie -> database
|
||||
root, _ := trie.Commit(nil)
|
||||
root, _, _ := trie.Commit(nil)
|
||||
// Flush memdb -> disk (sponge)
|
||||
db.Commit(root, false, func(c common.Hash) {
|
||||
// And spongify the callback-order
|
||||
@ -834,7 +834,7 @@ func TestCommitSequenceStackTrie(t *testing.T) {
|
||||
stTrie.TryUpdate(key, val)
|
||||
}
|
||||
// Flush trie -> database
|
||||
root, _ := trie.Commit(nil)
|
||||
root, _, _ := trie.Commit(nil)
|
||||
// Flush memdb -> disk (sponge)
|
||||
db.Commit(root, false, nil)
|
||||
// And flush stacktrie -> disk
|
||||
@ -879,7 +879,7 @@ func TestCommitSequenceSmallRoot(t *testing.T) {
|
||||
trie.TryUpdate(key, []byte{0x1})
|
||||
stTrie.TryUpdate(key, []byte{0x1})
|
||||
// Flush trie -> database
|
||||
root, _ := trie.Commit(nil)
|
||||
root, _, _ := trie.Commit(nil)
|
||||
// Flush memdb -> disk (sponge)
|
||||
db.Commit(root, false, nil)
|
||||
// And flush stacktrie -> disk
|
||||
|
Reference in New Issue
Block a user