mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 02:12:23 +00:00
trie: roll back some changes
This commit is contained in:
parent
5c4588667d
commit
97de2299fd
6 changed files with 62 additions and 53 deletions
|
|
@ -945,7 +945,7 @@ func (s *StateDB) fastDeleteStorage(snaps *snapshot.Tree, addrHash common.Hash,
|
||||||
slots = make(map[common.Hash][]byte)
|
slots = make(map[common.Hash][]byte)
|
||||||
)
|
)
|
||||||
stack := trie.NewStackTrie(func(path []byte, hash common.Hash, blob []byte) {
|
stack := trie.NewStackTrie(func(path []byte, hash common.Hash, blob []byte) {
|
||||||
nodes.AddNode(string(path), trienode.NewDeleted())
|
nodes.AddNode(path, trienode.NewDeleted())
|
||||||
})
|
})
|
||||||
for iter.Next() {
|
for iter.Next() {
|
||||||
slot := common.CopyBytes(iter.Slot())
|
slot := common.CopyBytes(iter.Slot())
|
||||||
|
|
@ -991,7 +991,7 @@ func (s *StateDB) slowDeleteStorage(addr common.Address, addrHash common.Hash, r
|
||||||
if it.Hash() == (common.Hash{}) {
|
if it.Hash() == (common.Hash{}) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
nodes.AddNode(string(it.Path()), trienode.NewDeleted())
|
nodes.AddNode(it.Path(), trienode.NewDeleted())
|
||||||
}
|
}
|
||||||
if err := it.Error(); err != nil {
|
if err := it.Error(); err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
|
|
|
||||||
|
|
@ -35,9 +35,9 @@ type committer struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// newCommitter creates a new committer or picks one from the pool.
|
// newCommitter creates a new committer or picks one from the pool.
|
||||||
func newCommitter(nodes *trienode.NodeSet, tracer *tracer, collectLeaf bool, parallel bool) *committer {
|
func newCommitter(nodeset *trienode.NodeSet, tracer *tracer, collectLeaf bool, parallel bool) *committer {
|
||||||
return &committer{
|
return &committer{
|
||||||
nodes: nodes,
|
nodes: nodeset,
|
||||||
tracer: tracer,
|
tracer: tracer,
|
||||||
collectLeaf: collectLeaf,
|
collectLeaf: collectLeaf,
|
||||||
parallel: parallel,
|
parallel: parallel,
|
||||||
|
|
@ -118,20 +118,22 @@ func (c *committer) commitChildren(path []byte, n *fullNode, parallel bool) [17]
|
||||||
children[i] = c.commit(append(path, byte(i)), child, false)
|
children[i] = c.commit(append(path, byte(i)), child, false)
|
||||||
} else {
|
} else {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
|
var nodesMu sync.Mutex
|
||||||
go func(index int) {
|
go func(index int) {
|
||||||
p := append(path, byte(i))
|
p := append(path, byte(i))
|
||||||
set := trienode.NewNodeSet(c.nodes.Owner)
|
set := trienode.NewNodeSet(c.nodes.Owner)
|
||||||
childComitter := newCommitter(set, c.tracer, c.collectLeaf, false)
|
childComitter := newCommitter(set, c.tracer, c.collectLeaf, false)
|
||||||
h := childComitter.commit(p, child, false)
|
h := childComitter.commit(p, child, false)
|
||||||
children[index] = h
|
children[index] = h
|
||||||
|
nodesMu.Lock()
|
||||||
c.nodes.MergeSet(set)
|
c.nodes.MergeSet(set)
|
||||||
|
nodesMu.Unlock()
|
||||||
wg.Done()
|
wg.Done()
|
||||||
}(i)
|
}(i)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if parallel {
|
if parallel {
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
||||||
}
|
}
|
||||||
// For the 17th child, it's possible the type is valuenode.
|
// For the 17th child, it's possible the type is valuenode.
|
||||||
if n.Children[16] != nil {
|
if n.Children[16] != nil {
|
||||||
|
|
@ -156,12 +158,13 @@ func (c *committer) store(path []byte, n node) node {
|
||||||
// deleted only if the node was existent in database before.
|
// deleted only if the node was existent in database before.
|
||||||
_, ok := c.tracer.accessList[string(path)]
|
_, ok := c.tracer.accessList[string(path)]
|
||||||
if ok {
|
if ok {
|
||||||
c.nodes.AddNode(path, trienode.NewDeleted()) // TODO
|
c.nodes.AddNode(path, trienode.NewDeleted())
|
||||||
}
|
}
|
||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
|
// Collect the dirty node to nodeset for return.
|
||||||
nhash := common.BytesToHash(hash)
|
nhash := common.BytesToHash(hash)
|
||||||
c.nodes.AddNode(path, trienode.New(nhash, nodeToBytes(n))) // TODO
|
c.nodes.AddNode(path, trienode.New(nhash, nodeToBytes(n)))
|
||||||
|
|
||||||
// Collect the corresponding leaf node if it's required. We don't check
|
// Collect the corresponding leaf node if it's required. We don't check
|
||||||
// full node since it's impossible to store value in fullNode. The key
|
// full node since it's impossible to store value in fullNode. The key
|
||||||
|
|
@ -169,7 +172,7 @@ func (c *committer) store(path []byte, n node) node {
|
||||||
if c.collectLeaf {
|
if c.collectLeaf {
|
||||||
if sn, ok := n.(*shortNode); ok {
|
if sn, ok := n.(*shortNode); ok {
|
||||||
if val, ok := sn.Val.(valueNode); ok {
|
if val, ok := sn.Val.(valueNode); ok {
|
||||||
c.nodes.AddLeaf(nhash, val) // TODO
|
c.nodes.AddLeaf(nhash, val)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
60
trie/trie.go
60
trie/trie.go
|
|
@ -37,13 +37,25 @@ import (
|
||||||
//
|
//
|
||||||
// Trie is not safe for concurrent use.
|
// Trie is not safe for concurrent use.
|
||||||
type Trie struct {
|
type Trie struct {
|
||||||
root node
|
root node
|
||||||
owner common.Hash
|
owner common.Hash
|
||||||
committed bool // The Flag whether the commit operation is already performed
|
|
||||||
reader *trieReader // The handler trie can retrieve nodes from
|
// Flag whether the commit operation is already performed. If so the
|
||||||
tracer *tracer // The tool to track the trie changes
|
// trie is not usable(latest states is invisible).
|
||||||
mutate int // The number of trie mutations that have been performed
|
committed bool
|
||||||
hashed int // The number of mutations that have been hashed
|
|
||||||
|
// Keep track of the number leaves which have been inserted since the last
|
||||||
|
// hashing operation. This number will not directly map to the number of
|
||||||
|
// actually unhashed nodes.
|
||||||
|
unhashed int
|
||||||
|
// uncommitted is the number of updates since last commit.
|
||||||
|
uncommitted int
|
||||||
|
|
||||||
|
// reader is the handler trie can retrieve nodes from.
|
||||||
|
reader *trieReader
|
||||||
|
|
||||||
|
// tracer is the tool to track the trie changes.
|
||||||
|
tracer *tracer
|
||||||
}
|
}
|
||||||
|
|
||||||
// newFlag returns the cache flag value for a newly created node.
|
// newFlag returns the cache flag value for a newly created node.
|
||||||
|
|
@ -54,13 +66,13 @@ func (t *Trie) newFlag() nodeFlag {
|
||||||
// Copy returns a copy of Trie.
|
// Copy returns a copy of Trie.
|
||||||
func (t *Trie) Copy() *Trie {
|
func (t *Trie) Copy() *Trie {
|
||||||
return &Trie{
|
return &Trie{
|
||||||
root: t.root,
|
root: t.root,
|
||||||
owner: t.owner,
|
owner: t.owner,
|
||||||
committed: t.committed,
|
committed: t.committed,
|
||||||
reader: t.reader,
|
reader: t.reader,
|
||||||
tracer: t.tracer.copy(),
|
tracer: t.tracer.copy(),
|
||||||
mutate: t.mutate,
|
uncommitted: t.uncommitted,
|
||||||
hashed: t.hashed,
|
unhashed: t.unhashed,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -295,11 +307,12 @@ func (t *Trie) Update(key, value []byte) error {
|
||||||
if t.committed {
|
if t.committed {
|
||||||
return ErrCommitted
|
return ErrCommitted
|
||||||
}
|
}
|
||||||
t.mutate++
|
|
||||||
return t.update(key, value)
|
return t.update(key, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Trie) update(key, value []byte) error {
|
func (t *Trie) update(key, value []byte) error {
|
||||||
|
t.unhashed++
|
||||||
|
t.uncommitted++
|
||||||
k := keybytesToHex(key)
|
k := keybytesToHex(key)
|
||||||
if len(value) != 0 {
|
if len(value) != 0 {
|
||||||
_, n, err := t.insert(t.root, nil, k, valueNode(value))
|
_, n, err := t.insert(t.root, nil, k, valueNode(value))
|
||||||
|
|
@ -413,7 +426,8 @@ func (t *Trie) Delete(key []byte) error {
|
||||||
if t.committed {
|
if t.committed {
|
||||||
return ErrCommitted
|
return ErrCommitted
|
||||||
}
|
}
|
||||||
t.mutate++
|
t.uncommitted++
|
||||||
|
t.unhashed++
|
||||||
k := keybytesToHex(key)
|
k := keybytesToHex(key)
|
||||||
_, n, err := t.delete(t.root, nil, k)
|
_, n, err := t.delete(t.root, nil, k)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -633,9 +647,9 @@ func (t *Trie) Commit(collectLeaf bool) (common.Hash, *trienode.NodeSet) {
|
||||||
for _, path := range t.tracer.deletedNodes() {
|
for _, path := range t.tracer.deletedNodes() {
|
||||||
nodes.AddNode([]byte(path), trienode.NewDeleted())
|
nodes.AddNode([]byte(path), trienode.NewDeleted())
|
||||||
}
|
}
|
||||||
// If the number of changes is below 100, we let one thread handle it
|
// If the number of changes is below 400, we let one thread handle it
|
||||||
t.root = newCommitter(nodes, t.tracer, collectLeaf, t.mutate > 100).Commit(t.root)
|
t.root = newCommitter(nodes, t.tracer, collectLeaf, t.uncommitted > 400).Commit(t.root)
|
||||||
t.mutate = 0
|
t.uncommitted = 0
|
||||||
return rootHash, nodes
|
return rootHash, nodes
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -645,10 +659,10 @@ func (t *Trie) hashRoot() (node, node) {
|
||||||
return hashNode(types.EmptyRootHash.Bytes()), nil
|
return hashNode(types.EmptyRootHash.Bytes()), nil
|
||||||
}
|
}
|
||||||
// If the number of changes is below 100, we let one thread handle it
|
// If the number of changes is below 100, we let one thread handle it
|
||||||
h := newHasher(t.mutate-t.hashed >= 100)
|
h := newHasher(t.unhashed >= 100)
|
||||||
defer func() {
|
defer func() {
|
||||||
returnHasherToPool(h)
|
returnHasherToPool(h)
|
||||||
t.hashed = t.mutate
|
t.unhashed = 0
|
||||||
}()
|
}()
|
||||||
hashed, cached := h.hash(t.root, true)
|
hashed, cached := h.hash(t.root, true)
|
||||||
return hashed, cached
|
return hashed, cached
|
||||||
|
|
@ -670,8 +684,8 @@ func (t *Trie) Witness() map[string]struct{} {
|
||||||
func (t *Trie) Reset() {
|
func (t *Trie) Reset() {
|
||||||
t.root = nil
|
t.root = nil
|
||||||
t.owner = common.Hash{}
|
t.owner = common.Hash{}
|
||||||
|
t.unhashed = 0
|
||||||
|
t.uncommitted = 0
|
||||||
t.tracer.reset()
|
t.tracer.reset()
|
||||||
t.committed = false
|
t.committed = false
|
||||||
t.hashed = 0
|
|
||||||
t.mutate = 0
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -26,6 +26,7 @@ import (
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"reflect"
|
"reflect"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"testing/quick"
|
"testing/quick"
|
||||||
|
|
||||||
|
|
@ -40,7 +41,6 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/trie/trienode"
|
"github.com/ethereum/go-ethereum/trie/trienode"
|
||||||
"github.com/holiman/uint256"
|
"github.com/holiman/uint256"
|
||||||
"golang.org/x/crypto/sha3"
|
"golang.org/x/crypto/sha3"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|
@ -1240,8 +1240,8 @@ func BenchmarkCommit(b *testing.B) {
|
||||||
//benchmarkCommit(b, 100)
|
//benchmarkCommit(b, 100)
|
||||||
//benchmarkCommit(b, 200)
|
//benchmarkCommit(b, 200)
|
||||||
//benchmarkCommit(b, 500)
|
//benchmarkCommit(b, 500)
|
||||||
//benchmarkCommit(b, 1000)
|
benchmarkCommit(b, 1000)
|
||||||
//benchmarkCommit(b, 2000)
|
benchmarkCommit(b, 2000)
|
||||||
benchmarkCommit(b, 5000)
|
benchmarkCommit(b, 5000)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1265,7 +1265,7 @@ func testCommit(b *testing.B, n int, parallel bool) {
|
||||||
}
|
}
|
||||||
tries[i].Hash()
|
tries[i].Hash()
|
||||||
if !parallel {
|
if !parallel {
|
||||||
tries[i].mutate = 0
|
tries[i].uncommitted = 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
|
|
@ -1286,9 +1286,8 @@ func TestCommitCorrect(t *testing.T) {
|
||||||
refTrie.Update(common.CopyBytes(key), common.CopyBytes(val))
|
refTrie.Update(common.CopyBytes(key), common.CopyBytes(val))
|
||||||
}
|
}
|
||||||
paraTrie.Hash()
|
paraTrie.Hash()
|
||||||
//paraTrie.mutate = 0
|
|
||||||
refTrie.Hash()
|
refTrie.Hash()
|
||||||
refTrie.mutate = 0
|
refTrie.uncommitted = 0
|
||||||
|
|
||||||
haveRoot, haveNodes := paraTrie.Commit(true)
|
haveRoot, haveNodes := paraTrie.Commit(true)
|
||||||
wantRoot, wantNodes := refTrie.Commit(true)
|
wantRoot, wantNodes := refTrie.Commit(true)
|
||||||
|
|
@ -1315,7 +1314,7 @@ func printSet(set *trienode.NodeSet) string {
|
||||||
var out = new(strings.Builder)
|
var out = new(strings.Builder)
|
||||||
fmt.Fprintf(out, "nodeset owner: %v\n", set.Owner)
|
fmt.Fprintf(out, "nodeset owner: %v\n", set.Owner)
|
||||||
var paths []string
|
var paths []string
|
||||||
for k, _ := range set.Nodes {
|
for k := range set.Nodes {
|
||||||
paths = append(paths, k)
|
paths = append(paths, k)
|
||||||
}
|
}
|
||||||
sort.Strings(paths)
|
sort.Strings(paths)
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"sync"
|
"golang.org/x/exp/maps"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Node is a wrapper which contains the encoded blob of the trie node and its
|
// Node is a wrapper which contains the encoded blob of the trie node and its
|
||||||
|
|
@ -60,8 +60,6 @@ type leaf struct {
|
||||||
// NodeSet contains a set of nodes collected during the commit operation.
|
// NodeSet contains a set of nodes collected during the commit operation.
|
||||||
// Each node is keyed by path. It's not thread-safe to use.
|
// Each node is keyed by path. It's not thread-safe to use.
|
||||||
type NodeSet struct {
|
type NodeSet struct {
|
||||||
mu sync.Mutex
|
|
||||||
|
|
||||||
Owner common.Hash
|
Owner common.Hash
|
||||||
Leaves []*leaf
|
Leaves []*leaf
|
||||||
Nodes map[string]*Node
|
Nodes map[string]*Node
|
||||||
|
|
@ -102,21 +100,18 @@ func (set *NodeSet) AddNode(path []byte, n *Node) {
|
||||||
set.Nodes[string(path)] = n
|
set.Nodes[string(path)] = n
|
||||||
}
|
}
|
||||||
|
|
||||||
// MergeSet mergest set with other. It assumes that the sets are disjunct, so
|
// MergeSet merges this 'set' with 'other'. It assumes that the sets are disjoint, and
|
||||||
// that it does not need to deduplicate data (count deletes, dedup leaves etc).
|
// thus does not deduplicate data (count deletes, dedup leaves etc).
|
||||||
func (set *NodeSet) MergeSet(other *NodeSet) error {
|
func (set *NodeSet) MergeSet(other *NodeSet) error {
|
||||||
if set.Owner != other.Owner {
|
if set.Owner != other.Owner {
|
||||||
return fmt.Errorf("nodesets belong to different owner are not mergeable %x-%x", set.Owner, other.Owner)
|
return fmt.Errorf("nodesets belong to different owner are not mergeable %x-%x", set.Owner, other.Owner)
|
||||||
}
|
}
|
||||||
set.mu.Lock()
|
maps.Copy(set.Nodes, other.Nodes)
|
||||||
defer set.mu.Unlock()
|
|
||||||
for path, node := range other.Nodes {
|
|
||||||
set.Nodes[path] = node
|
|
||||||
}
|
|
||||||
set.deletes += other.deletes
|
set.deletes += other.deletes
|
||||||
set.updates += other.updates
|
set.updates += other.updates
|
||||||
// Since we assume the sets are disjunct, we can safely append leaves
|
// Since we assume the sets are disjoint, we can safely append leaves
|
||||||
// like this without dedup.
|
// like this without deduplication.
|
||||||
set.Leaves = append(set.Leaves, other.Leaves...)
|
set.Leaves = append(set.Leaves, other.Leaves...)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
@ -126,8 +121,6 @@ func (set *NodeSet) Merge(owner common.Hash, nodes map[string]*Node) error {
|
||||||
if set.Owner != owner {
|
if set.Owner != owner {
|
||||||
return fmt.Errorf("nodesets belong to different owner are not mergeable %x-%x", set.Owner, owner)
|
return fmt.Errorf("nodesets belong to different owner are not mergeable %x-%x", set.Owner, owner)
|
||||||
}
|
}
|
||||||
set.mu.Lock()
|
|
||||||
defer set.mu.Unlock()
|
|
||||||
for path, node := range nodes {
|
for path, node := range nodes {
|
||||||
prev, ok := set.Nodes[path]
|
prev, ok := set.Nodes[path]
|
||||||
if ok {
|
if ok {
|
||||||
|
|
|
||||||
|
|
@ -42,7 +42,7 @@ func benchmarkMerge(b *testing.B, count int) {
|
||||||
blob := make([]byte, 32)
|
blob := make([]byte, 32)
|
||||||
rand.Read(blob)
|
rand.Read(blob)
|
||||||
hash := crypto.Keccak256Hash(blob)
|
hash := crypto.Keccak256Hash(blob)
|
||||||
s.AddNode(string(path), New(hash, blob))
|
s.AddNode(path, New(hash, blob))
|
||||||
}
|
}
|
||||||
for i := 0; i < count; i++ {
|
for i := 0; i < count; i++ {
|
||||||
// Random path of 4 nibbles
|
// Random path of 4 nibbles
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue