2020-09-29 18:38:13 +03:00
|
|
|
// Copyright 2020 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 trie
|
|
|
|
|
|
|
|
import (
|
2023-10-23 18:31:56 +03:00
|
|
|
"bytes"
|
2023-10-25 15:53:50 +03:00
|
|
|
"errors"
|
2020-09-29 18:38:13 +03:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2023-02-21 14:12:27 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2020-09-29 18:38:13 +03:00
|
|
|
)
|
|
|
|
|
2023-10-10 09:28:56 +03:00
|
|
|
var (
|
2023-10-17 15:09:25 +03:00
|
|
|
stPool = sync.Pool{New: func() any { return new(stNode) }}
|
|
|
|
_ = types.TrieHasher((*StackTrie)(nil))
|
2023-10-10 09:28:56 +03:00
|
|
|
)
|
2020-09-29 18:38:13 +03:00
|
|
|
|
2024-04-16 10:05:36 +03:00
|
|
|
// OnTrieNode is a callback method invoked when a trie node is committed
|
|
|
|
// by the stack trie. The node is only committed if it's considered complete.
|
|
|
|
//
|
|
|
|
// The caller should not modify the contents of the returned path and blob
|
|
|
|
// slice, and their contents may be changed after the call. It is up to the
|
|
|
|
// `onTrieNode` receiver function to deep-copy the data if it wants to retain
|
|
|
|
// it after the call ends.
|
|
|
|
type OnTrieNode func(path []byte, hash common.Hash, blob []byte)
|
2023-10-23 18:31:56 +03:00
|
|
|
|
2020-09-29 18:38:13 +03:00
|
|
|
// StackTrie is a trie implementation that expects keys to be inserted
|
|
|
|
// in order. Once it determines that a subtree will no longer be inserted
|
|
|
|
// into, it will hash it and free up the memory it uses.
|
|
|
|
type StackTrie struct {
|
2024-04-16 10:05:36 +03:00
|
|
|
root *stNode
|
|
|
|
h *hasher
|
|
|
|
last []byte
|
|
|
|
onTrieNode OnTrieNode
|
2020-09-29 18:38:13 +03:00
|
|
|
}
|
|
|
|
|
2024-04-16 10:05:36 +03:00
|
|
|
// NewStackTrie allocates and initializes an empty trie. The committed nodes
|
|
|
|
// will be discarded immediately if no callback is configured.
|
|
|
|
func NewStackTrie(onTrieNode OnTrieNode) *StackTrie {
|
2020-09-29 18:38:13 +03:00
|
|
|
return &StackTrie{
|
2024-04-16 10:05:36 +03:00
|
|
|
root: stPool.Get().(*stNode),
|
|
|
|
h: newHasher(false),
|
|
|
|
onTrieNode: onTrieNode,
|
2020-09-29 18:38:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-10 09:28:56 +03:00
|
|
|
// Update inserts a (key, value) pair into the stack trie.
|
|
|
|
func (t *StackTrie) Update(key, value []byte) error {
|
|
|
|
if len(value) == 0 {
|
2023-10-25 15:53:50 +03:00
|
|
|
return errors.New("trying to insert empty (deletion)")
|
2021-04-20 11:42:02 +03:00
|
|
|
}
|
2023-10-25 15:53:50 +03:00
|
|
|
k := keybytesToHex(key)
|
2023-10-17 15:09:25 +03:00
|
|
|
k = k[:len(k)-1] // chop the termination flag
|
2023-10-25 15:53:50 +03:00
|
|
|
if bytes.Compare(t.last, k) >= 0 {
|
|
|
|
return errors.New("non-ascending key order")
|
|
|
|
}
|
2023-10-23 18:31:56 +03:00
|
|
|
if t.last == nil {
|
|
|
|
t.last = append([]byte{}, k...) // allocate key slice
|
|
|
|
} else {
|
|
|
|
t.last = append(t.last[:0], k...) // reuse key slice
|
|
|
|
}
|
2023-10-17 15:09:25 +03:00
|
|
|
t.insert(t.root, k, value, nil)
|
2023-10-10 09:28:56 +03:00
|
|
|
return nil
|
2021-04-20 11:42:02 +03:00
|
|
|
}
|
|
|
|
|
2023-10-17 15:09:25 +03:00
|
|
|
// Reset resets the stack trie object to empty state.
|
2023-10-10 09:28:56 +03:00
|
|
|
func (t *StackTrie) Reset() {
|
|
|
|
t.root = stPool.Get().(*stNode)
|
2023-10-23 18:31:56 +03:00
|
|
|
t.last = nil
|
2021-04-20 11:42:02 +03:00
|
|
|
}
|
|
|
|
|
2023-10-10 09:28:56 +03:00
|
|
|
// stNode represents a node within a StackTrie
|
|
|
|
type stNode struct {
|
|
|
|
typ uint8 // node type (as in branch, ext, leaf)
|
|
|
|
key []byte // key chunk covered by this (leaf|ext) node
|
|
|
|
val []byte // value contained by this node if it's a leaf
|
|
|
|
children [16]*stNode // list of children (for branch and exts)
|
2021-04-20 11:42:02 +03:00
|
|
|
}
|
|
|
|
|
2023-10-10 09:28:56 +03:00
|
|
|
// newLeaf constructs a leaf node with provided node key and value. The key
|
|
|
|
// will be deep-copied in the function and safe to modify afterwards, but
|
|
|
|
// value is not.
|
|
|
|
func newLeaf(key, val []byte) *stNode {
|
|
|
|
st := stPool.Get().(*stNode)
|
|
|
|
st.typ = leafNode
|
2021-11-29 13:02:40 +03:00
|
|
|
st.key = append(st.key, key...)
|
2020-09-29 18:38:13 +03:00
|
|
|
st.val = val
|
|
|
|
return st
|
|
|
|
}
|
|
|
|
|
2023-10-10 09:28:56 +03:00
|
|
|
// newExt constructs an extension node with provided node key and child. The
|
|
|
|
// key will be deep-copied in the function and safe to modify afterwards.
|
|
|
|
func newExt(key []byte, child *stNode) *stNode {
|
|
|
|
st := stPool.Get().(*stNode)
|
|
|
|
st.typ = extNode
|
2021-11-29 13:02:40 +03:00
|
|
|
st.key = append(st.key, key...)
|
2020-09-29 18:38:13 +03:00
|
|
|
st.children[0] = child
|
|
|
|
return st
|
|
|
|
}
|
|
|
|
|
2023-10-10 09:28:56 +03:00
|
|
|
// List all values that stNode#nodeType can hold
|
2020-09-29 18:38:13 +03:00
|
|
|
const (
|
|
|
|
emptyNode = iota
|
|
|
|
branchNode
|
|
|
|
extNode
|
|
|
|
leafNode
|
|
|
|
hashedNode
|
|
|
|
)
|
|
|
|
|
2023-10-10 09:28:56 +03:00
|
|
|
func (n *stNode) reset() *stNode {
|
|
|
|
n.key = n.key[:0]
|
|
|
|
n.val = nil
|
|
|
|
for i := range n.children {
|
|
|
|
n.children[i] = nil
|
2020-09-29 18:38:13 +03:00
|
|
|
}
|
2023-10-10 09:28:56 +03:00
|
|
|
n.typ = emptyNode
|
|
|
|
return n
|
2020-09-29 18:38:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Helper function that, given a full key, determines the index
|
|
|
|
// at which the chunk pointed by st.keyOffset is different from
|
|
|
|
// the same chunk in the full key.
|
2023-10-10 09:28:56 +03:00
|
|
|
func (n *stNode) getDiffIndex(key []byte) int {
|
|
|
|
for idx, nibble := range n.key {
|
2021-11-29 13:02:40 +03:00
|
|
|
if nibble != key[idx] {
|
|
|
|
return idx
|
|
|
|
}
|
2020-09-29 18:38:13 +03:00
|
|
|
}
|
2023-10-10 09:28:56 +03:00
|
|
|
return len(n.key)
|
2020-09-29 18:38:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Helper function to that inserts a (key, value) pair into
|
|
|
|
// the trie.
|
2023-10-17 15:09:25 +03:00
|
|
|
func (t *StackTrie) insert(st *stNode, key, value []byte, path []byte) {
|
2023-10-10 09:28:56 +03:00
|
|
|
switch st.typ {
|
2020-09-29 18:38:13 +03:00
|
|
|
case branchNode: /* Branch */
|
2021-11-29 13:02:40 +03:00
|
|
|
idx := int(key[0])
|
2022-03-09 16:45:17 +03:00
|
|
|
|
2020-09-29 18:38:13 +03:00
|
|
|
// Unresolve elder siblings
|
|
|
|
for i := idx - 1; i >= 0; i-- {
|
|
|
|
if st.children[i] != nil {
|
2023-10-10 09:28:56 +03:00
|
|
|
if st.children[i].typ != hashedNode {
|
2023-10-17 15:09:25 +03:00
|
|
|
t.hash(st.children[i], append(path, byte(i)))
|
2020-09-29 18:38:13 +03:00
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2022-03-09 16:45:17 +03:00
|
|
|
|
2020-09-29 18:38:13 +03:00
|
|
|
// Add new child
|
|
|
|
if st.children[idx] == nil {
|
2023-10-10 09:28:56 +03:00
|
|
|
st.children[idx] = newLeaf(key[1:], value)
|
2021-11-29 13:02:40 +03:00
|
|
|
} else {
|
2023-10-17 15:09:25 +03:00
|
|
|
t.insert(st.children[idx], key[1:], value, append(path, key[0]))
|
2020-09-29 18:38:13 +03:00
|
|
|
}
|
2022-03-09 16:45:17 +03:00
|
|
|
|
2020-09-29 18:38:13 +03:00
|
|
|
case extNode: /* Ext */
|
|
|
|
// Compare both key chunks and see where they differ
|
|
|
|
diffidx := st.getDiffIndex(key)
|
|
|
|
|
|
|
|
// Check if chunks are identical. If so, recurse into
|
|
|
|
// the child node. Otherwise, the key has to be split
|
|
|
|
// into 1) an optional common prefix, 2) the fullnode
|
|
|
|
// representing the two differing path, and 3) a leaf
|
|
|
|
// for each of the differentiated subtrees.
|
|
|
|
if diffidx == len(st.key) {
|
|
|
|
// Ext key and key segment are identical, recurse into
|
|
|
|
// the child node.
|
2023-10-17 15:09:25 +03:00
|
|
|
t.insert(st.children[0], key[diffidx:], value, append(path, key[:diffidx]...))
|
2020-09-29 18:38:13 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// Save the original part. Depending if the break is
|
|
|
|
// at the extension's last byte or not, create an
|
|
|
|
// intermediate extension or use the extension's child
|
|
|
|
// node directly.
|
2023-10-10 09:28:56 +03:00
|
|
|
var n *stNode
|
2020-09-29 18:38:13 +03:00
|
|
|
if diffidx < len(st.key)-1 {
|
2022-11-28 16:31:28 +03:00
|
|
|
// Break on the non-last byte, insert an intermediate
|
|
|
|
// extension. The path prefix of the newly-inserted
|
|
|
|
// extension should also contain the different byte.
|
2023-10-10 09:28:56 +03:00
|
|
|
n = newExt(st.key[diffidx+1:], st.children[0])
|
2023-10-17 15:09:25 +03:00
|
|
|
t.hash(n, append(path, st.key[:diffidx+1]...))
|
2020-09-29 18:38:13 +03:00
|
|
|
} else {
|
|
|
|
// Break on the last byte, no need to insert
|
2022-11-28 16:31:28 +03:00
|
|
|
// an extension node: reuse the current node.
|
|
|
|
// The path prefix of the original part should
|
|
|
|
// still be same.
|
2020-09-29 18:38:13 +03:00
|
|
|
n = st.children[0]
|
2023-10-17 15:09:25 +03:00
|
|
|
t.hash(n, append(path, st.key...))
|
2020-09-29 18:38:13 +03:00
|
|
|
}
|
2023-10-10 09:28:56 +03:00
|
|
|
var p *stNode
|
2020-09-29 18:38:13 +03:00
|
|
|
if diffidx == 0 {
|
|
|
|
// the break is on the first byte, so
|
|
|
|
// the current node is converted into
|
|
|
|
// a branch node.
|
|
|
|
st.children[0] = nil
|
|
|
|
p = st
|
2023-10-10 09:28:56 +03:00
|
|
|
st.typ = branchNode
|
2020-09-29 18:38:13 +03:00
|
|
|
} else {
|
|
|
|
// the common prefix is at least one byte
|
|
|
|
// long, insert a new intermediate branch
|
|
|
|
// node.
|
2023-10-10 09:28:56 +03:00
|
|
|
st.children[0] = stPool.Get().(*stNode)
|
|
|
|
st.children[0].typ = branchNode
|
2020-09-29 18:38:13 +03:00
|
|
|
p = st.children[0]
|
|
|
|
}
|
|
|
|
// Create a leaf for the inserted part
|
2023-10-10 09:28:56 +03:00
|
|
|
o := newLeaf(key[diffidx+1:], value)
|
2020-09-29 18:38:13 +03:00
|
|
|
|
|
|
|
// Insert both child leaves where they belong:
|
|
|
|
origIdx := st.key[diffidx]
|
2021-11-29 13:02:40 +03:00
|
|
|
newIdx := key[diffidx]
|
2020-09-29 18:38:13 +03:00
|
|
|
p.children[origIdx] = n
|
|
|
|
p.children[newIdx] = o
|
|
|
|
st.key = st.key[:diffidx]
|
|
|
|
|
|
|
|
case leafNode: /* Leaf */
|
|
|
|
// Compare both key chunks and see where they differ
|
|
|
|
diffidx := st.getDiffIndex(key)
|
|
|
|
|
|
|
|
// Overwriting a key isn't supported, which means that
|
|
|
|
// the current leaf is expected to be split into 1) an
|
|
|
|
// optional extension for the common prefix of these 2
|
|
|
|
// keys, 2) a fullnode selecting the path on which the
|
|
|
|
// keys differ, and 3) one leaf for the differentiated
|
|
|
|
// component of each key.
|
|
|
|
if diffidx >= len(st.key) {
|
|
|
|
panic("Trying to insert into existing key")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the split occurs at the first nibble of the
|
|
|
|
// chunk. In that case, no prefix extnode is necessary.
|
|
|
|
// Otherwise, create that
|
2023-10-10 09:28:56 +03:00
|
|
|
var p *stNode
|
2020-09-29 18:38:13 +03:00
|
|
|
if diffidx == 0 {
|
|
|
|
// Convert current leaf into a branch
|
2023-10-10 09:28:56 +03:00
|
|
|
st.typ = branchNode
|
2020-09-29 18:38:13 +03:00
|
|
|
p = st
|
|
|
|
st.children[0] = nil
|
|
|
|
} else {
|
|
|
|
// Convert current node into an ext,
|
|
|
|
// and insert a child branch node.
|
2023-10-10 09:28:56 +03:00
|
|
|
st.typ = extNode
|
|
|
|
st.children[0] = stPool.Get().(*stNode)
|
|
|
|
st.children[0].typ = branchNode
|
2020-09-29 18:38:13 +03:00
|
|
|
p = st.children[0]
|
|
|
|
}
|
|
|
|
|
2022-03-09 16:45:17 +03:00
|
|
|
// Create the two child leaves: one containing the original
|
|
|
|
// value and another containing the new value. The child leaf
|
|
|
|
// is hashed directly in order to free up some memory.
|
2020-09-29 18:38:13 +03:00
|
|
|
origIdx := st.key[diffidx]
|
2023-10-10 09:28:56 +03:00
|
|
|
p.children[origIdx] = newLeaf(st.key[diffidx+1:], st.val)
|
2023-10-17 15:09:25 +03:00
|
|
|
t.hash(p.children[origIdx], append(path, st.key[:diffidx+1]...))
|
2020-09-29 18:38:13 +03:00
|
|
|
|
2021-11-29 13:02:40 +03:00
|
|
|
newIdx := key[diffidx]
|
2023-10-10 09:28:56 +03:00
|
|
|
p.children[newIdx] = newLeaf(key[diffidx+1:], value)
|
2020-09-29 18:38:13 +03:00
|
|
|
|
|
|
|
// Finally, cut off the key part that has been passed
|
|
|
|
// over to the children.
|
|
|
|
st.key = st.key[:diffidx]
|
|
|
|
st.val = nil
|
2022-03-09 16:45:17 +03:00
|
|
|
|
2020-09-29 18:38:13 +03:00
|
|
|
case emptyNode: /* Empty */
|
2023-10-10 09:28:56 +03:00
|
|
|
st.typ = leafNode
|
2021-11-29 13:02:40 +03:00
|
|
|
st.key = key
|
2020-09-29 18:38:13 +03:00
|
|
|
st.val = value
|
2022-03-09 16:45:17 +03:00
|
|
|
|
2020-09-29 18:38:13 +03:00
|
|
|
case hashedNode:
|
|
|
|
panic("trying to insert into hash")
|
2022-03-09 16:45:17 +03:00
|
|
|
|
2020-09-29 18:38:13 +03:00
|
|
|
default:
|
|
|
|
panic("invalid type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-09 16:45:17 +03:00
|
|
|
// hash converts st into a 'hashedNode', if possible. Possible outcomes:
|
|
|
|
//
|
2020-09-29 18:38:13 +03:00
|
|
|
// 1. The rlp-encoded value was >= 32 bytes:
|
2022-09-10 14:25:40 +03:00
|
|
|
// - Then the 32-byte `hash` will be accessible in `st.val`.
|
|
|
|
// - And the 'st.type' will be 'hashedNode'
|
|
|
|
//
|
2020-09-29 18:38:13 +03:00
|
|
|
// 2. The rlp-encoded value was < 32 bytes
|
2022-09-10 14:25:40 +03:00
|
|
|
// - Then the <32 byte rlp-encoded value will be accessible in 'st.val'.
|
|
|
|
// - And the 'st.type' will be 'hashedNode' AGAIN
|
2020-09-29 18:38:13 +03:00
|
|
|
//
|
2022-03-09 16:45:17 +03:00
|
|
|
// This method also sets 'st.type' to hashedNode, and clears 'st.key'.
|
2023-10-10 09:28:56 +03:00
|
|
|
func (t *StackTrie) hash(st *stNode, path []byte) {
|
2024-04-16 10:05:36 +03:00
|
|
|
var blob []byte // RLP-encoded node blob
|
2023-10-10 09:28:56 +03:00
|
|
|
switch st.typ {
|
2022-03-09 16:45:17 +03:00
|
|
|
case hashedNode:
|
|
|
|
return
|
|
|
|
|
|
|
|
case emptyNode:
|
2023-02-21 14:12:27 +03:00
|
|
|
st.val = types.EmptyRootHash.Bytes()
|
2022-03-09 16:45:17 +03:00
|
|
|
st.key = st.key[:0]
|
2023-10-10 09:28:56 +03:00
|
|
|
st.typ = hashedNode
|
2022-03-09 16:45:17 +03:00
|
|
|
return
|
|
|
|
|
2020-09-29 18:38:13 +03:00
|
|
|
case branchNode:
|
2023-04-24 10:38:52 +03:00
|
|
|
var nodes fullNode
|
2020-09-29 18:38:13 +03:00
|
|
|
for i, child := range st.children {
|
|
|
|
if child == nil {
|
2023-04-24 10:38:52 +03:00
|
|
|
nodes.Children[i] = nilValueNode
|
2020-09-29 18:38:13 +03:00
|
|
|
continue
|
|
|
|
}
|
2023-10-10 09:28:56 +03:00
|
|
|
t.hash(child, append(path, byte(i)))
|
|
|
|
|
2020-09-29 18:38:13 +03:00
|
|
|
if len(child.val) < 32 {
|
2023-04-24 10:38:52 +03:00
|
|
|
nodes.Children[i] = rawNode(child.val)
|
2020-09-29 18:38:13 +03:00
|
|
|
} else {
|
2023-04-24 10:38:52 +03:00
|
|
|
nodes.Children[i] = hashNode(child.val)
|
2020-09-29 18:38:13 +03:00
|
|
|
}
|
2022-03-09 16:45:17 +03:00
|
|
|
st.children[i] = nil
|
2023-10-10 09:28:56 +03:00
|
|
|
stPool.Put(child.reset()) // Release child back to pool.
|
2020-09-29 18:38:13 +03:00
|
|
|
}
|
2023-10-10 09:28:56 +03:00
|
|
|
nodes.encode(t.h.encbuf)
|
2023-10-17 15:09:25 +03:00
|
|
|
blob = t.h.encodedBytes()
|
2022-03-09 16:45:17 +03:00
|
|
|
|
2020-09-29 18:38:13 +03:00
|
|
|
case extNode:
|
2023-10-17 15:09:25 +03:00
|
|
|
// recursively hash and commit child as the first step
|
2023-10-10 09:28:56 +03:00
|
|
|
t.hash(st.children[0], append(path, st.key...))
|
2022-03-09 16:45:17 +03:00
|
|
|
|
2023-10-17 15:09:25 +03:00
|
|
|
// encode the extension node
|
2023-08-18 23:41:19 +03:00
|
|
|
n := shortNode{Key: hexToCompactInPlace(st.key)}
|
2020-11-09 17:08:12 +03:00
|
|
|
if len(st.children[0].val) < 32 {
|
2022-03-09 16:45:17 +03:00
|
|
|
n.Val = rawNode(st.children[0].val)
|
2020-11-09 17:08:12 +03:00
|
|
|
} else {
|
2022-03-09 16:45:17 +03:00
|
|
|
n.Val = hashNode(st.children[0].val)
|
2020-09-29 18:38:13 +03:00
|
|
|
}
|
2023-10-10 09:28:56 +03:00
|
|
|
n.encode(t.h.encbuf)
|
2023-10-17 15:09:25 +03:00
|
|
|
blob = t.h.encodedBytes()
|
2022-03-09 16:45:17 +03:00
|
|
|
|
2023-10-10 09:28:56 +03:00
|
|
|
stPool.Put(st.children[0].reset()) // Release child back to pool.
|
2022-03-09 16:45:17 +03:00
|
|
|
st.children[0] = nil
|
|
|
|
|
2020-09-29 18:38:13 +03:00
|
|
|
case leafNode:
|
|
|
|
st.key = append(st.key, byte(16))
|
2023-08-18 23:41:19 +03:00
|
|
|
n := shortNode{Key: hexToCompactInPlace(st.key), Val: valueNode(st.val)}
|
2022-03-09 16:45:17 +03:00
|
|
|
|
2023-10-10 09:28:56 +03:00
|
|
|
n.encode(t.h.encbuf)
|
2023-10-17 15:09:25 +03:00
|
|
|
blob = t.h.encodedBytes()
|
2022-03-09 16:45:17 +03:00
|
|
|
|
2020-09-29 18:38:13 +03:00
|
|
|
default:
|
2022-03-09 16:45:17 +03:00
|
|
|
panic("invalid node type")
|
2020-09-29 18:38:13 +03:00
|
|
|
}
|
2024-04-16 10:05:36 +03:00
|
|
|
// Convert the node type to hashNode and reset the key slice.
|
2023-10-10 09:28:56 +03:00
|
|
|
st.typ = hashedNode
|
2022-03-09 16:45:17 +03:00
|
|
|
st.key = st.key[:0]
|
2023-10-17 15:09:25 +03:00
|
|
|
|
2024-04-16 10:05:36 +03:00
|
|
|
// Skip committing the non-root node if the size is smaller than 32 bytes
|
|
|
|
// as tiny nodes are always embedded in their parent except root node.
|
2023-10-17 15:09:25 +03:00
|
|
|
if len(blob) < 32 && len(path) > 0 {
|
|
|
|
st.val = common.CopyBytes(blob)
|
2020-09-29 18:38:13 +03:00
|
|
|
return
|
|
|
|
}
|
2021-04-16 15:21:01 +03:00
|
|
|
// Write the hash to the 'val'. We allocate a new val here to not mutate
|
2023-10-17 15:09:25 +03:00
|
|
|
// input values.
|
|
|
|
st.val = t.h.hashData(blob)
|
|
|
|
|
2024-04-16 10:05:36 +03:00
|
|
|
// Invoke the callback it's provided. Notably, the path and blob slices are
|
|
|
|
// volatile, please deep-copy the slices in callback if the contents need
|
|
|
|
// to be retained.
|
|
|
|
if t.onTrieNode != nil {
|
|
|
|
t.onTrieNode(path, common.BytesToHash(st.val), blob)
|
2023-10-23 18:31:56 +03:00
|
|
|
}
|
2020-09-29 18:38:13 +03:00
|
|
|
}
|
|
|
|
|
2023-10-17 15:09:25 +03:00
|
|
|
// Hash will firstly hash the entire trie if it's still not hashed and then commit
|
2024-04-16 10:05:36 +03:00
|
|
|
// all leftover nodes to the associated database. Actually most of the trie nodes
|
|
|
|
// have been committed already. The main purpose here is to commit the nodes on
|
|
|
|
// right boundary.
|
2023-10-17 15:09:25 +03:00
|
|
|
func (t *StackTrie) Hash() common.Hash {
|
|
|
|
n := t.root
|
|
|
|
t.hash(n, nil)
|
|
|
|
return common.BytesToHash(n.val)
|
2020-09-29 18:38:13 +03:00
|
|
|
}
|