trie: refactor stacktrie

This change refactors stacktrie to separate the stacktrie itself from the
internal representation of nodes: a stacktrie is not a recursive structure
of stacktries, rather, a framework for representing and operating upon a set of nodes.
This commit is contained in:
Martin Holst Swende 2023-10-01 17:14:54 +02:00
parent 7b6ff527d5
commit 171a932c44
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0
2 changed files with 233 additions and 261 deletions

View file

@ -17,11 +17,7 @@
package trie package trie
import ( import (
"bufio"
"bytes"
"encoding/gob"
"errors" "errors"
"io"
"sync" "sync"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
@ -29,186 +25,50 @@ import (
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
) )
var ErrCommitDisabled = errors.New("no database for committing") var (
ErrCommitDisabled = errors.New("no database for committing")
var stPool = sync.Pool{ stPool = sync.Pool{New: func() any { return new(stNode) }}
New: func() interface{} { _ = types.TrieHasher((*StackTrie)(nil))
return NewStackTrie(nil) )
},
}
// NodeWriteFunc is used to provide all information of a dirty node for committing // NodeWriteFunc is used to provide all information of a dirty node for committing
// so that callers can flush nodes into database with desired scheme. // so that callers can flush nodes into database with desired scheme.
type NodeWriteFunc = func(owner common.Hash, path []byte, hash common.Hash, blob []byte) type NodeWriteFunc = func(owner common.Hash, path []byte, hash common.Hash, blob []byte)
func stackTrieFromPool(writeFn NodeWriteFunc, owner common.Hash) *StackTrie {
st := stPool.Get().(*StackTrie)
st.owner = owner
st.writeFn = writeFn
return st
}
func returnToPool(st *StackTrie) {
st.Reset()
stPool.Put(st)
}
// StackTrie is a trie implementation that expects keys to be inserted // StackTrie is a trie implementation that expects keys to be inserted
// in order. Once it determines that a subtree will no longer 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. // into, it will hash it and free up the memory it uses.
type StackTrie struct { type StackTrie struct {
owner common.Hash // the owner of the trie owner common.Hash // the owner of the trie
nodeType uint8 // node type (as in branch, ext, leaf)
val []byte // value contained by this node if it's a leaf
key []byte // key chunk covered by this (leaf|ext) node
children [16]*StackTrie // list of children (for branch and exts)
writeFn NodeWriteFunc // function for committing nodes, can be nil writeFn NodeWriteFunc // function for committing nodes, can be nil
root *stNode
h *hasher
} }
// NewStackTrie allocates and initializes an empty trie. // NewStackTrie allocates and initializes an empty trie.
func NewStackTrie(writeFn NodeWriteFunc) *StackTrie { func NewStackTrie(writeFn NodeWriteFunc) *StackTrie {
return &StackTrie{ return &StackTrie{
nodeType: emptyNode,
writeFn: writeFn, writeFn: writeFn,
root: stPool.Get().(*stNode),
h: newHasher(false),
} }
} }
// NewStackTrieWithOwner allocates and initializes an empty trie, but with // NewStackTrieWithOwner allocates and initializes an empty trie, but with
// the additional owner field. // the additional owner field.
func NewStackTrieWithOwner(writeFn NodeWriteFunc, owner common.Hash) *StackTrie { func NewStackTrieWithOwner(writeFn NodeWriteFunc, owner common.Hash) *StackTrie {
return &StackTrie{ stack := NewStackTrie(writeFn)
owner: owner, stack.owner = owner
nodeType: emptyNode, return stack
writeFn: writeFn,
}
} }
// NewFromBinary initialises a serialized stacktrie with the given db.
func NewFromBinary(data []byte, writeFn NodeWriteFunc) (*StackTrie, error) {
var st StackTrie
if err := st.UnmarshalBinary(data); err != nil {
return nil, err
}
// If a database is used, we need to recursively add it to every child
if writeFn != nil {
st.setWriter(writeFn)
}
return &st, nil
}
// MarshalBinary implements encoding.BinaryMarshaler
func (st *StackTrie) MarshalBinary() (data []byte, err error) {
var (
b bytes.Buffer
w = bufio.NewWriter(&b)
)
if err := gob.NewEncoder(w).Encode(struct {
Owner common.Hash
NodeType uint8
Val []byte
Key []byte
}{
st.owner,
st.nodeType,
st.val,
st.key,
}); err != nil {
return nil, err
}
for _, child := range st.children {
if child == nil {
w.WriteByte(0)
continue
}
w.WriteByte(1)
if childData, err := child.MarshalBinary(); err != nil {
return nil, err
} else {
w.Write(childData)
}
}
w.Flush()
return b.Bytes(), nil
}
// UnmarshalBinary implements encoding.BinaryUnmarshaler
func (st *StackTrie) UnmarshalBinary(data []byte) error {
r := bytes.NewReader(data)
return st.unmarshalBinary(r)
}
func (st *StackTrie) unmarshalBinary(r io.Reader) error {
var dec struct {
Owner common.Hash
NodeType uint8
Val []byte
Key []byte
}
if err := gob.NewDecoder(r).Decode(&dec); err != nil {
return err
}
st.owner = dec.Owner
st.nodeType = dec.NodeType
st.val = dec.Val
st.key = dec.Key
var hasChild = make([]byte, 1)
for i := range st.children {
if _, err := r.Read(hasChild); err != nil {
return err
} else if hasChild[0] == 0 {
continue
}
var child StackTrie
if err := child.unmarshalBinary(r); err != nil {
return err
}
st.children[i] = &child
}
return nil
}
func (st *StackTrie) setWriter(writeFn NodeWriteFunc) {
st.writeFn = writeFn
for _, child := range st.children {
if child != nil {
child.setWriter(writeFn)
}
}
}
func newLeaf(owner common.Hash, key, val []byte, writeFn NodeWriteFunc) *StackTrie {
st := stackTrieFromPool(writeFn, owner)
st.nodeType = leafNode
st.key = append(st.key, key...)
st.val = val
return st
}
func newExt(owner common.Hash, key []byte, child *StackTrie, writeFn NodeWriteFunc) *StackTrie {
st := stackTrieFromPool(writeFn, owner)
st.nodeType = extNode
st.key = append(st.key, key...)
st.children[0] = child
return st
}
// List all values that StackTrie#nodeType can hold
const (
emptyNode = iota
branchNode
extNode
leafNode
hashedNode
)
// Update inserts a (key, value) pair into the stack trie. // Update inserts a (key, value) pair into the stack trie.
func (st *StackTrie) Update(key, value []byte) error { func (stack *StackTrie) Update(key, value []byte) error {
k := keybytesToHex(key) k := keybytesToHex(key)
if len(value) == 0 { if len(value) == 0 {
panic("deletion not supported") panic("deletion not supported")
} }
st.insert(k[:len(k)-1], value, nil) stack.insert(stack.root, k[:len(k)-1], value, nil)
return nil return nil
} }
@ -220,21 +80,59 @@ func (st *StackTrie) MustUpdate(key, value []byte) {
} }
} }
func (st *StackTrie) Reset() { func (stack *StackTrie) Reset() {
st.owner = common.Hash{} stack.owner = (common.Hash{})
st.writeFn = nil stack.writeFn = nil
stack.root = stPool.Get().(*stNode)
}
// stNode represents a node within a StackTrie
type stNode struct {
nodeType uint8 // node type (as in branch, ext, leaf)
val []byte // value contained by this node if it's a leaf
key []byte // key chunk covered by this (leaf|ext) node
children [16]*stNode // list of children (for branch and exts)
}
func newLeaf(key, val []byte) *stNode {
st := stPool.Get().(*stNode)
st.nodeType = leafNode
st.key = append(st.key, key...)
st.val = val
return st
}
func newExt(key []byte, child *stNode) *stNode {
st := stPool.Get().(*stNode)
st.nodeType = extNode
st.key = append(st.key, key...)
st.children[0] = child
return st
}
// List all values that stNode#nodeType can hold
const (
emptyNode = iota
branchNode
extNode
leafNode
hashedNode
)
func (st *stNode) Reset() *stNode {
st.key = st.key[:0] st.key = st.key[:0]
st.val = nil st.val = nil
for i := range st.children { for i := range st.children {
st.children[i] = nil st.children[i] = nil
} }
st.nodeType = emptyNode st.nodeType = emptyNode
return st
} }
// Helper function that, given a full key, determines the index // Helper function that, given a full key, determines the index
// at which the chunk pointed by st.keyOffset is different from // at which the chunk pointed by st.keyOffset is different from
// the same chunk in the full key. // the same chunk in the full key.
func (st *StackTrie) getDiffIndex(key []byte) int { func (st *stNode) getDiffIndex(key []byte) int {
for idx, nibble := range st.key { for idx, nibble := range st.key {
if nibble != key[idx] { if nibble != key[idx] {
return idx return idx
@ -245,7 +143,7 @@ func (st *StackTrie) 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 (st *StackTrie) insert(key, value []byte, prefix []byte) { func (stack *StackTrie) insert(st *stNode, key, value []byte, prefix []byte) {
switch st.nodeType { switch st.nodeType {
case branchNode: /* Branch */ case branchNode: /* Branch */
idx := int(key[0]) idx := int(key[0])
@ -254,7 +152,7 @@ func (st *StackTrie) insert(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].nodeType != hashedNode { if st.children[i].nodeType != hashedNode {
st.children[i].hash(append(prefix, byte(i))) stack.hash(st.children[i], append(prefix, byte(i)))
} }
break break
} }
@ -262,9 +160,9 @@ func (st *StackTrie) insert(key, value []byte, prefix []byte) {
// Add new child // Add new child
if st.children[idx] == nil { if st.children[idx] == nil {
st.children[idx] = newLeaf(st.owner, key[1:], value, st.writeFn) st.children[idx] = newLeaf(key[1:], value)
} else { } else {
st.children[idx].insert(key[1:], value, append(prefix, key[0])) stack.insert(st.children[idx], key[1:], value, append(prefix, key[0]))
} }
case extNode: /* Ext */ case extNode: /* Ext */
@ -279,29 +177,29 @@ func (st *StackTrie) insert(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.
st.children[0].insert(key[diffidx:], value, append(prefix, key[:diffidx]...)) stack.insert(st.children[0], key[diffidx:], value, append(prefix, key[:diffidx]...))
return return
} }
// Save the original part. Depending if the break is // Save the original part. Depending if the break is
// at the extension's last byte or not, create an // at the extension's last byte or not, create an
// intermediate extension or use the extension's child // intermediate extension or use the extension's child
// node directly. // node directly.
var n *StackTrie var n *stNode
if diffidx < len(st.key)-1 { if diffidx < len(st.key)-1 {
// Break on the non-last byte, insert an intermediate // Break on the non-last byte, insert an intermediate
// 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.owner, st.key[diffidx+1:], st.children[0], st.writeFn) n = newExt(st.key[diffidx+1:], st.children[0])
n.hash(append(prefix, st.key[:diffidx+1]...)) stack.hash(n, append(prefix, 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]
n.hash(append(prefix, st.key...)) stack.hash(n, append(prefix, st.key...))
} }
var p *StackTrie var p *stNode
if diffidx == 0 { if diffidx == 0 {
// the break is on the first byte, so // the break is on the first byte, so
// the current node is converted into // the current node is converted into
@ -313,12 +211,12 @@ func (st *StackTrie) insert(key, value []byte, prefix []byte) {
// the common prefix is at least one byte // the common prefix is at least one byte
// long, insert a new intermediate branch // long, insert a new intermediate branch
// node. // node.
st.children[0] = stackTrieFromPool(st.writeFn, st.owner) st.children[0] = stPool.Get().(*stNode)
st.children[0].nodeType = branchNode st.children[0].nodeType = branchNode
p = st.children[0] p = st.children[0]
} }
// Create a leaf for the inserted part // Create a leaf for the inserted part
o := newLeaf(st.owner, key[diffidx+1:], value, st.writeFn) o := newLeaf(key[diffidx+1:], value)
// Insert both child leaves where they belong: // Insert both child leaves where they belong:
origIdx := st.key[diffidx] origIdx := st.key[diffidx]
@ -344,7 +242,7 @@ func (st *StackTrie) insert(key, value []byte, prefix []byte) {
// Check if the split occurs at the first nibble of the // Check if the split occurs at the first nibble of the
// chunk. In that case, no prefix extnode is necessary. // chunk. In that case, no prefix extnode is necessary.
// Otherwise, create that // Otherwise, create that
var p *StackTrie var p *stNode
if diffidx == 0 { if diffidx == 0 {
// Convert current leaf into a branch // Convert current leaf into a branch
st.nodeType = branchNode st.nodeType = branchNode
@ -354,7 +252,7 @@ func (st *StackTrie) insert(key, value []byte, prefix []byte) {
// Convert current node into an ext, // Convert current node into an ext,
// and insert a child branch node. // and insert a child branch node.
st.nodeType = extNode st.nodeType = extNode
st.children[0] = NewStackTrieWithOwner(st.writeFn, st.owner) st.children[0] = stPool.Get().(*stNode)
st.children[0].nodeType = branchNode st.children[0].nodeType = branchNode
p = st.children[0] p = st.children[0]
} }
@ -363,11 +261,11 @@ func (st *StackTrie) insert(key, value []byte, prefix []byte) {
// value and another containing the new value. The child leaf // value and another containing the new value. The child leaf
// 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.owner, st.key[diffidx+1:], st.val, st.writeFn) p.children[origIdx] = newLeaf(st.key[diffidx+1:], st.val)
p.children[origIdx].hash(append(prefix, st.key[:diffidx+1]...)) stack.hash(p.children[origIdx], append(prefix, st.key[:diffidx+1]...))
newIdx := key[diffidx] newIdx := key[diffidx]
p.children[newIdx] = newLeaf(st.owner, key[diffidx+1:], value, st.writeFn) p.children[newIdx] = newLeaf(key[diffidx+1:], value)
// Finally, cut off the key part that has been passed // Finally, cut off the key part that has been passed
// over to the children. // over to the children.
@ -398,14 +296,7 @@ func (st *StackTrie) insert(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 (st *StackTrie) hash(path []byte) { func (stack *StackTrie) hash(st *stNode, path []byte) {
h := newHasher(false)
defer returnHasherToPool(h)
st.hashRec(h, path)
}
func (st *StackTrie) hashRec(hasher *hasher, path []byte) {
// The switch below sets this to the RLP-encoding of this node. // The switch below sets this to the RLP-encoding of this node.
var encodedNode []byte var encodedNode []byte
@ -426,7 +317,7 @@ func (st *StackTrie) hashRec(hasher *hasher, path []byte) {
nodes.Children[i] = nilValueNode nodes.Children[i] = nilValueNode
continue continue
} }
child.hashRec(hasher, append(path, byte(i))) stack.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)
} else { } else {
@ -435,14 +326,14 @@ func (st *StackTrie) hashRec(hasher *hasher, path []byte) {
// Release child back to pool. // Release child back to pool.
st.children[i] = nil st.children[i] = nil
returnToPool(child) stPool.Put(child.Reset())
} }
nodes.encode(hasher.encbuf) nodes.encode(stack.h.encbuf)
encodedNode = hasher.encodedBytes() encodedNode = stack.h.encodedBytes()
case extNode: case extNode:
st.children[0].hashRec(hasher, append(path, st.key...)) stack.hash(st.children[0], append(path, st.key...))
n := shortNode{Key: hexToCompactInPlace(st.key)} n := shortNode{Key: hexToCompactInPlace(st.key)}
if len(st.children[0].val) < 32 { if len(st.children[0].val) < 32 {
@ -451,19 +342,20 @@ func (st *StackTrie) hashRec(hasher *hasher, path []byte) {
n.Val = hashNode(st.children[0].val) n.Val = hashNode(st.children[0].val)
} }
n.encode(hasher.encbuf) n.encode(stack.h.encbuf)
encodedNode = hasher.encodedBytes() encodedNode = stack.h.encodedBytes()
// Release child back to pool. // Release child back to pool.
returnToPool(st.children[0]) stPool.Put(st.children[0].Reset())
st.children[0] = nil st.children[0] = nil
case leafNode: case leafNode:
st.key = append(st.key, byte(16)) st.key = append(st.key, byte(16))
n := shortNode{Key: hexToCompactInPlace(st.key), Val: valueNode(st.val)} n := shortNode{Key: hexToCompactInPlace(st.key), Val: valueNode(st.val)}
n.encode(hasher.encbuf) n.encode(stack.h.encbuf)
encodedNode = hasher.encodedBytes() encodedNode = stack.h.encodedBytes()
default: default:
panic("invalid node type") panic("invalid node type")
@ -478,18 +370,16 @@ func (st *StackTrie) hashRec(hasher *hasher, path []byte) {
// Write the hash to the 'val'. We allocate a new val here to not mutate // Write the hash to the 'val'. We allocate a new val here to not mutate
// input values // input values
st.val = hasher.hashData(encodedNode) st.val = stack.h.hashData(encodedNode)
if st.writeFn != nil { if stack.writeFn != nil {
st.writeFn(st.owner, path, common.BytesToHash(st.val), encodedNode) stack.writeFn(stack.owner, path, common.BytesToHash(st.val), encodedNode)
} }
} }
// Hash returns the hash of the current node. // Hash returns the hash of the current node.
func (st *StackTrie) Hash() (h common.Hash) { func (stack *StackTrie) Hash() (h common.Hash) {
hasher := newHasher(false) st := stack.root
defer returnHasherToPool(hasher) stack.hash(st, nil)
st.hashRec(hasher, nil)
if len(st.val) == 32 { if len(st.val) == 32 {
copy(h[:], st.val) copy(h[:], st.val)
return h return h
@ -497,9 +387,9 @@ func (st *StackTrie) Hash() (h common.Hash) {
// If the node's RLP isn't 32 bytes long, the node will not // If the node's RLP isn't 32 bytes long, the node will not
// be hashed, and instead contain the rlp-encoding of the // be hashed, and instead contain the rlp-encoding of the
// node. For the top level node, we need to force the hashing. // node. For the top level node, we need to force the hashing.
hasher.sha.Reset() stack.h.sha.Reset()
hasher.sha.Write(st.val) stack.h.sha.Write(st.val)
hasher.sha.Read(h[:]) stack.h.sha.Read(h[:])
return h return h
} }
@ -510,14 +400,12 @@ func (st *StackTrie) Hash() (h common.Hash) {
// //
// The associated database is expected, otherwise the whole commit // The associated database is expected, otherwise the whole commit
// functionality should be disabled. // functionality should be disabled.
func (st *StackTrie) Commit() (h common.Hash, err error) { func (stack *StackTrie) Commit() (h common.Hash, err error) {
if st.writeFn == nil { if stack.writeFn == nil {
return common.Hash{}, ErrCommitDisabled return common.Hash{}, ErrCommitDisabled
} }
hasher := newHasher(false) st := stack.root
defer returnHasherToPool(hasher) stack.hash(st, nil)
st.hashRec(hasher, nil)
if len(st.val) == 32 { if len(st.val) == 32 {
copy(h[:], st.val) copy(h[:], st.val)
return h, nil return h, nil
@ -525,10 +413,95 @@ func (st *StackTrie) Commit() (h common.Hash, err error) {
// If the node's RLP isn't 32 bytes long, the node will not // If the node's RLP isn't 32 bytes long, the node will not
// be hashed (and committed), and instead contain the rlp-encoding of the // be hashed (and committed), and instead contain the rlp-encoding of the
// node. For the top level node, we need to force the hashing+commit. // node. For the top level node, we need to force the hashing+commit.
hasher.sha.Reset() stack.h.sha.Reset()
hasher.sha.Write(st.val) stack.h.sha.Write(st.val)
hasher.sha.Read(h[:]) stack.h.sha.Read(h[:])
st.writeFn(st.owner, nil, h, st.val) stack.writeFn(stack.owner, nil, h, st.val)
return h, nil return h, nil
} }
//// NewFromBinary initialises a serialized stacktrie with the given db.
//func NewFromBinary(data []byte, writeFn NodeWriteFunc) (*StackTrie, error) {
// var st StackTrie
// if err := st.UnmarshalBinary(data); err != nil {
// return nil, err
// }
// // If a database is used, we need to recursively add it to every child
// if writeFn != nil {
// st.setWriter(writeFn)
// }
// return &st, nil
//}
//
//// MarshalBinary implements encoding.BinaryMarshaler
//func (st *StackTrie) MarshalBinary() (data []byte, err error) {
// var (
// b bytes.Buffer
// w = bufio.NewWriter(&b)
// )
// if err := gob.NewEncoder(w).Encode(struct {
// Owner common.Hash
// NodeType uint8
// Val []byte
// Key []byte
// }{
// st.owner,
// st.nodeType,
// st.val,
// st.key,
// }); err != nil {
// return nil, err
// }
// for _, child := range st.children {
// if child == nil {
// w.WriteByte(0)
// continue
// }
// w.WriteByte(1)
// if childData, err := child.MarshalBinary(); err != nil {
// return nil, err
// } else {
// w.Write(childData)
// }
// }
// w.Flush()
// return b.Bytes(), nil
//}
//
//// UnmarshalBinary implements encoding.BinaryUnmarshaler
//func (st *StackTrie) UnmarshalBinary(data []byte) error {
// r := bytes.NewReader(data)
// return st.unmarshalBinary(r)
//}
//
//func (st *StackTrie) unmarshalBinary(r io.Reader) error {
// var dec struct {
// Owner common.Hash
// NodeType uint8
// Val []byte
// Key []byte
// }
// if err := gob.NewDecoder(r).Decode(&dec); err != nil {
// return err
// }
// st.owner = dec.Owner
// st.nodeType = dec.NodeType
// st.val = dec.Val
// st.key = dec.Key
//
// var hasChild = make([]byte, 1)
// for i := range st.children {
// if _, err := r.Read(hasChild); err != nil {
// return err
// } else if hasChild[0] == 0 {
// continue
// }
// var child StackTrie
// if err := child.unmarshalBinary(r); err != nil {
// return err
// }
// st.children[i] = &child
// }
// return nil
//}

View file

@ -198,12 +198,11 @@ func TestStackTrieInsertAndHash(t *testing.T) {
{"000003", "XXXXXXXXXXXXXXXXXXXXXXXXXXXX", "962c0fffdeef7612a4f7bff1950d67e3e81c878e48b9ae45b3b374253b050bd8"}, {"000003", "XXXXXXXXXXXXXXXXXXXXXXXXXXXX", "962c0fffdeef7612a4f7bff1950d67e3e81c878e48b9ae45b3b374253b050bd8"},
}, },
} }
st := NewStackTrie(nil)
for i, test := range tests { for i, test := range tests {
// The StackTrie does not allow Insert(), Hash(), Insert(), ... // The StackTrie does not allow Insert(), Hash(), Insert(), ...
// so we will create new trie for every sequence length of inserts. // so we will create new trie for every sequence length of inserts.
for l := 1; l <= len(test); l++ { for l := 1; l <= len(test); l++ {
st.Reset() st := NewStackTrie(nil)
for j := 0; j < l; j++ { for j := 0; j < l; j++ {
kv := &test[j] kv := &test[j]
if err := st.Update(common.FromHex(kv.K), []byte(kv.V)); err != nil { if err := st.Update(common.FromHex(kv.K), []byte(kv.V)); err != nil {
@ -380,45 +379,45 @@ func TestStacktrieNotModifyValues(t *testing.T) {
// TestStacktrieSerialization tests that the stacktrie works well if we // TestStacktrieSerialization tests that the stacktrie works well if we
// serialize/unserialize it a lot // serialize/unserialize it a lot
func TestStacktrieSerialization(t *testing.T) { //func TestStacktrieSerialization(t *testing.T) {
var ( // var (
st = NewStackTrie(nil) // st = NewStackTrie(nil)
nt = NewEmpty(NewDatabase(rawdb.NewMemoryDatabase(), nil)) // nt = NewEmpty(NewDatabase(rawdb.NewMemoryDatabase(), nil))
keyB = big.NewInt(1) // keyB = big.NewInt(1)
keyDelta = big.NewInt(1) // keyDelta = big.NewInt(1)
vals [][]byte // vals [][]byte
keys [][]byte // keys [][]byte
) // )
getValue := func(i int) []byte { // getValue := func(i int) []byte {
if i%2 == 0 { // large // if i%2 == 0 { // large
return crypto.Keccak256(big.NewInt(int64(i)).Bytes()) // return crypto.Keccak256(big.NewInt(int64(i)).Bytes())
} else { //small // } else { //small
return big.NewInt(int64(i)).Bytes() // return big.NewInt(int64(i)).Bytes()
} // }
} // }
for i := 0; i < 10; i++ { // for i := 0; i < 10; i++ {
vals = append(vals, getValue(i)) // vals = append(vals, getValue(i))
keys = append(keys, common.BigToHash(keyB).Bytes()) // keys = append(keys, common.BigToHash(keyB).Bytes())
keyB = keyB.Add(keyB, keyDelta) // keyB = keyB.Add(keyB, keyDelta)
keyDelta.Add(keyDelta, common.Big1) // keyDelta.Add(keyDelta, common.Big1)
} // }
for i, k := range keys { // for i, k := range keys {
nt.Update(k, common.CopyBytes(vals[i])) // nt.Update(k, common.CopyBytes(vals[i]))
} // }
//
for i, k := range keys { // for i, k := range keys {
blob, err := st.MarshalBinary() // blob, err := st.MarshalBinary()
if err != nil { // if err != nil {
t.Fatal(err) // t.Fatal(err)
} // }
newSt, err := NewFromBinary(blob, nil) // newSt, err := NewFromBinary(blob, nil)
if err != nil { // if err != nil {
t.Fatal(err) // t.Fatal(err)
} // }
st = newSt // st = newSt
st.Update(k, common.CopyBytes(vals[i])) // st.Update(k, common.CopyBytes(vals[i]))
} // }
if have, want := st.Hash(), nt.Hash(); have != want { // if have, want := st.Hash(), nt.Hash(); have != want {
t.Fatalf("have %#x want %#x", have, want) // t.Fatalf("have %#x want %#x", have, want)
} // }
} //}