core, light, tests, trie: add state metrics (#23433)

This commit is contained in:
gary rong
2021-08-25 03:00:42 +08:00
committed by GitHub
parent a789dcc978
commit a5a5237178
15 changed files with 131 additions and 75 deletions

View File

@ -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)
}