mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
trie: address comment
This commit is contained in:
parent
41c9a9d335
commit
5a5edd8706
1 changed files with 23 additions and 21 deletions
|
|
@ -152,7 +152,7 @@ func (n *stNode) getDiffIndex(key []byte) int {
|
||||||
|
|
||||||
// Helper function to that inserts a (key, value) pair into
|
// Helper function to that inserts a (key, value) pair into
|
||||||
// the trie.
|
// the trie.
|
||||||
func (t *StackTrie) insert(st *stNode, key, value []byte, prefix []byte) {
|
func (t *StackTrie) insert(st *stNode, key, value []byte, path []byte) {
|
||||||
switch st.typ {
|
switch st.typ {
|
||||||
case branchNode: /* Branch */
|
case branchNode: /* Branch */
|
||||||
idx := int(key[0])
|
idx := int(key[0])
|
||||||
|
|
@ -161,7 +161,7 @@ func (t *StackTrie) insert(st *stNode, key, value []byte, prefix []byte) {
|
||||||
for i := idx - 1; i >= 0; i-- {
|
for i := idx - 1; i >= 0; i-- {
|
||||||
if st.children[i] != nil {
|
if st.children[i] != nil {
|
||||||
if st.children[i].typ != hashedNode {
|
if st.children[i].typ != hashedNode {
|
||||||
t.hash(st.children[i], append(prefix, byte(i)))
|
t.hash(st.children[i], append(path, byte(i)))
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
@ -171,7 +171,7 @@ func (t *StackTrie) insert(st *stNode, key, value []byte, prefix []byte) {
|
||||||
if st.children[idx] == nil {
|
if st.children[idx] == nil {
|
||||||
st.children[idx] = newLeaf(key[1:], value)
|
st.children[idx] = newLeaf(key[1:], value)
|
||||||
} else {
|
} else {
|
||||||
t.insert(st.children[idx], key[1:], value, append(prefix, key[0]))
|
t.insert(st.children[idx], key[1:], value, append(path, key[0]))
|
||||||
}
|
}
|
||||||
|
|
||||||
case extNode: /* Ext */
|
case extNode: /* Ext */
|
||||||
|
|
@ -186,7 +186,7 @@ func (t *StackTrie) insert(st *stNode, key, value []byte, prefix []byte) {
|
||||||
if diffidx == len(st.key) {
|
if diffidx == len(st.key) {
|
||||||
// Ext key and key segment are identical, recurse into
|
// Ext key and key segment are identical, recurse into
|
||||||
// the child node.
|
// the child node.
|
||||||
t.insert(st.children[0], key[diffidx:], value, append(prefix, key[:diffidx]...))
|
t.insert(st.children[0], key[diffidx:], value, append(path, key[:diffidx]...))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// Save the original part. Depending if the break is
|
// Save the original part. Depending if the break is
|
||||||
|
|
@ -199,14 +199,14 @@ func (t *StackTrie) insert(st *stNode, key, value []byte, prefix []byte) {
|
||||||
// extension. The path prefix of the newly-inserted
|
// extension. The path prefix of the newly-inserted
|
||||||
// extension should also contain the different byte.
|
// extension should also contain the different byte.
|
||||||
n = newExt(st.key[diffidx+1:], st.children[0])
|
n = newExt(st.key[diffidx+1:], st.children[0])
|
||||||
t.hash(n, append(prefix, st.key[:diffidx+1]...))
|
t.hash(n, append(path, st.key[:diffidx+1]...))
|
||||||
} else {
|
} else {
|
||||||
// Break on the last byte, no need to insert
|
// Break on the last byte, no need to insert
|
||||||
// an extension node: reuse the current node.
|
// an extension node: reuse the current node.
|
||||||
// The path prefix of the original part should
|
// The path prefix of the original part should
|
||||||
// still be same.
|
// still be same.
|
||||||
n = st.children[0]
|
n = st.children[0]
|
||||||
t.hash(n, append(prefix, st.key...))
|
t.hash(n, append(path, st.key...))
|
||||||
}
|
}
|
||||||
var p *stNode
|
var p *stNode
|
||||||
if diffidx == 0 {
|
if diffidx == 0 {
|
||||||
|
|
@ -271,7 +271,7 @@ func (t *StackTrie) insert(st *stNode, key, value []byte, prefix []byte) {
|
||||||
// is hashed directly in order to free up some memory.
|
// is hashed directly in order to free up some memory.
|
||||||
origIdx := st.key[diffidx]
|
origIdx := st.key[diffidx]
|
||||||
p.children[origIdx] = newLeaf(st.key[diffidx+1:], st.val)
|
p.children[origIdx] = newLeaf(st.key[diffidx+1:], st.val)
|
||||||
t.hash(p.children[origIdx], append(prefix, st.key[:diffidx+1]...))
|
t.hash(p.children[origIdx], append(path, st.key[:diffidx+1]...))
|
||||||
|
|
||||||
newIdx := key[diffidx]
|
newIdx := key[diffidx]
|
||||||
p.children[newIdx] = newLeaf(key[diffidx+1:], value)
|
p.children[newIdx] = newLeaf(key[diffidx+1:], value)
|
||||||
|
|
@ -305,7 +305,7 @@ func (t *StackTrie) insert(st *stNode, key, value []byte, prefix []byte) {
|
||||||
// - And the 'st.type' will be 'hashedNode' AGAIN
|
// - And the 'st.type' will be 'hashedNode' AGAIN
|
||||||
//
|
//
|
||||||
// This method also sets 'st.type' to hashedNode, and clears 'st.key'.
|
// This method also sets 'st.type' to hashedNode, and clears 'st.key'.
|
||||||
func (t *StackTrie) hash(st *stNode, prefix []byte) {
|
func (t *StackTrie) hash(st *stNode, path []byte) {
|
||||||
var blob []byte // RLP-encoded node blob
|
var blob []byte // RLP-encoded node blob
|
||||||
|
|
||||||
switch st.typ {
|
switch st.typ {
|
||||||
|
|
@ -325,7 +325,7 @@ func (t *StackTrie) hash(st *stNode, prefix []byte) {
|
||||||
nodes.Children[i] = nilValueNode
|
nodes.Children[i] = nilValueNode
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
t.hash(child, append(prefix, byte(i)))
|
t.hash(child, append(path, byte(i)))
|
||||||
|
|
||||||
if len(child.val) < 32 {
|
if len(child.val) < 32 {
|
||||||
nodes.Children[i] = rawNode(child.val)
|
nodes.Children[i] = rawNode(child.val)
|
||||||
|
|
@ -340,7 +340,7 @@ func (t *StackTrie) hash(st *stNode, prefix []byte) {
|
||||||
|
|
||||||
case extNode:
|
case extNode:
|
||||||
// recursively hash and commit child as the first step
|
// recursively hash and commit child as the first step
|
||||||
t.hash(st.children[0], append(prefix, st.key...))
|
t.hash(st.children[0], append(path, st.key...))
|
||||||
|
|
||||||
// encode the extension node
|
// encode the extension node
|
||||||
n := shortNode{Key: hexToCompactInPlace(st.key)}
|
n := shortNode{Key: hexToCompactInPlace(st.key)}
|
||||||
|
|
@ -369,8 +369,8 @@ func (t *StackTrie) hash(st *stNode, prefix []byte) {
|
||||||
st.typ = hashedNode
|
st.typ = hashedNode
|
||||||
st.key = st.key[:0]
|
st.key = st.key[:0]
|
||||||
|
|
||||||
// Skip committing if the non-root node if the size is smaller than 32 bytes.
|
// Skip committing the non-root node if the size is smaller than 32 bytes.
|
||||||
if len(blob) < 32 && len(prefix) > 0 {
|
if len(blob) < 32 && len(path) > 0 {
|
||||||
st.val = common.CopyBytes(blob)
|
st.val = common.CopyBytes(blob)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -380,24 +380,26 @@ func (t *StackTrie) hash(st *stNode, prefix []byte) {
|
||||||
|
|
||||||
// Commit the trie node if the writer is configured.
|
// Commit the trie node if the writer is configured.
|
||||||
if t.options.Writer != nil {
|
if t.options.Writer != nil {
|
||||||
t.options.Writer(prefix, common.BytesToHash(st.val), blob)
|
t.options.Writer(path, common.BytesToHash(st.val), blob)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hash will firstly hash the entire trie if it's still not hashed
|
// Hash will firstly hash the entire trie if it's still not hashed and then commit
|
||||||
// and then commit all nodes to the associated database. Actually most
|
// all nodes to the associated database. Actually most of the trie nodes have been
|
||||||
// of the trie nodes MAY have been committed already. The main purpose
|
// committed already. The main purpose here is to commit the nodes on right boundary.
|
||||||
// here is to commit the root node.
|
//
|
||||||
|
// For stack trie, Hash and Commit are functionally identical.
|
||||||
func (t *StackTrie) Hash() common.Hash {
|
func (t *StackTrie) Hash() common.Hash {
|
||||||
n := t.root
|
n := t.root
|
||||||
t.hash(n, nil)
|
t.hash(n, nil)
|
||||||
return common.BytesToHash(n.val)
|
return common.BytesToHash(n.val)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Commit will firstly hash the entire trie if it's still not hashed
|
// Commit will firstly hash the entire trie if it's still not hashed and then commit
|
||||||
// and then commit all nodes to the associated database. Actually most
|
// all nodes to the associated database. Actually most of the trie nodes have been
|
||||||
// of the trie nodes MAY have been committed already. The main purpose
|
// committed already. The main purpose here is to commit the nodes on right boundary.
|
||||||
// here is to commit the root node.
|
//
|
||||||
|
// For stack trie, Hash and Commit are functionally identical.
|
||||||
func (t *StackTrie) Commit() common.Hash {
|
func (t *StackTrie) Commit() common.Hash {
|
||||||
return t.Hash()
|
return t.Hash()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue