@@ -22,113 +22,59 @@ import (
2222 "strings"
2323
2424 "github.com/ethereum/go-ethereum/common"
25+ "github.com/ethereum/go-ethereum/trie/trienode"
2526)
2627
27- // memoryNode is all the information we know about a single cached trie node
28- // in the memory.
29- type memoryNode struct {
30- hash common.Hash // Node hash by hashing node blob, empty for deleted nodes
31- node []byte // Encoded node blob, nil for deleted nodes
32- }
33-
34- // memorySize returns the total memory size used by this node.
35- // nolint:unused
36- func (n * memoryNode ) memorySize (pathlen int ) int {
37- return len (n .node ) + common .HashLength + pathlen
38- }
39-
40- // rlp returns the raw rlp encoded blob of the cached trie node, either directly
41- // from the cache, or by regenerating it from the collapsed node.
42- // nolint:unused
43- func (n * memoryNode ) rlp () []byte {
44- return n .node
45- }
46-
47- // obj returns the decoded and expanded trie node, either directly from the cache,
48- // or by regenerating it from the rlp encoded blob.
49- // nolint:unused
50- func (n * memoryNode ) obj () node {
51- return mustDecodeNode (n .hash [:], n .node )
52- }
53-
54- // isDeleted returns the indicator if the node is marked as deleted.
55- func (n * memoryNode ) isDeleted () bool {
56- return n .hash == (common.Hash {})
57- }
58-
59- // nodeWithPrev wraps the memoryNode with the previous node value.
60- // nolint: unused
61- type nodeWithPrev struct {
62- * memoryNode
63- prev []byte // RLP-encoded previous value, nil means it's non-existent
64- }
65-
66- // unwrap returns the internal memoryNode object.
67- // nolint:unused
68- func (n * nodeWithPrev ) unwrap () * memoryNode {
69- return n .memoryNode
70- }
71-
72- // memorySize returns the total memory size used by this node. It overloads
73- // the function in memoryNode by counting the size of previous value as well.
74- // nolint: unused
75- func (n * nodeWithPrev ) memorySize (pathlen int ) int {
76- return n .memoryNode .memorySize (pathlen ) + len (n .prev )
77- }
78-
7928// NodeSet contains all dirty nodes collected during the commit operation.
8029// Each node is keyed by path. It's not thread-safe to use.
8130type NodeSet struct {
82- owner common.Hash // the identifier of the trie
83- nodes map [ string ] * memoryNode // the set of dirty nodes(inserted, updated, deleted)
84- leaves [] * leaf // the list of dirty leaves
85- updates int // the count of updated and inserted nodes
86- deletes int // the count of deleted nodes
87-
88- // The list of accessed nodes, which records the original node value.
89- // The origin value is expected to be nil for newly inserted node
90- // and is expected to be non-nil for other types(updated, deleted) .
91- accessList map [string ][] byte
31+ owner common.Hash // the identifier of the trie
32+ leaves [] * leaf // the list of dirty leaves
33+ updates int // the count of updated and inserted nodes
34+ deletes int // the count of deleted nodes
35+
36+ // The set of all dirty nodes. Dirty nodes include newly inserted nodes,
37+ // deleted nodes and updated nodes. The original value of the newly
38+ // inserted node must be nil, and the original value of the other two
39+ // types must be non-nil.
40+ nodes map [string ]* trienode. WithPrev
9241}
9342
9443// NewNodeSet initializes an empty node set to be used for tracking dirty nodes
9544// from a specific account or storage trie. The owner is zero for the account
9645// trie and the owning account address hash for storage tries.
97- func NewNodeSet (owner common.Hash , accessList map [ string ][] byte ) * NodeSet {
46+ func NewNodeSet (owner common.Hash ) * NodeSet {
9847 return & NodeSet {
99- owner : owner ,
100- nodes : make (map [string ]* memoryNode ),
101- accessList : accessList ,
48+ owner : owner ,
49+ nodes : make (map [string ]* trienode.WithPrev ),
10250 }
10351}
10452
10553// forEachWithOrder iterates the dirty nodes with the order from bottom to top,
10654// right to left, nodes with the longest path will be iterated first.
107- func (set * NodeSet ) forEachWithOrder (callback func (path string , n * memoryNode )) {
55+ func (set * NodeSet ) forEachWithOrder (callback func (path string , n * trienode. Node )) {
10856 var paths sort.StringSlice
10957 for path := range set .nodes {
11058 paths = append (paths , path )
11159 }
11260 // Bottom-up, longest path first
11361 sort .Sort (sort .Reverse (paths ))
11462 for _ , path := range paths {
115- callback (path , set .nodes [path ])
63+ callback (path , set .nodes [path ]. Unwrap () )
11664 }
11765}
11866
119- // markUpdated marks the node as dirty(newly-inserted or updated).
120- func (set * NodeSet ) markUpdated (path []byte , node * memoryNode ) {
121- set .nodes [string (path )] = node
122- set .updates += 1
123- }
124-
125- // markDeleted marks the node as deleted.
126- func (set * NodeSet ) markDeleted (path []byte ) {
127- set .nodes [string (path )] = & memoryNode {}
128- set .deletes += 1
67+ // addNode adds the provided dirty node into set.
68+ func (set * NodeSet ) addNode (path []byte , n * trienode.WithPrev ) {
69+ if n .IsDeleted () {
70+ set .deletes += 1
71+ } else {
72+ set .updates += 1
73+ }
74+ set .nodes [string (path )] = n
12975}
13076
131- // addLeaf collects the provided leaf node into set.
77+ // addLeaf adds the provided leaf node into set.
13278func (set * NodeSet ) addLeaf (node * leaf ) {
13379 set .leaves = append (set .leaves , node )
13480}
@@ -143,7 +89,7 @@ func (set *NodeSet) Size() (int, int) {
14389func (set * NodeSet ) Hashes () []common.Hash {
14490 var ret []common.Hash
14591 for _ , node := range set .nodes {
146- ret = append (ret , node .hash )
92+ ret = append (ret , node .Hash )
14793 }
14894 return ret
14995}
@@ -155,18 +101,17 @@ func (set *NodeSet) Summary() string {
155101 if set .nodes != nil {
156102 for path , n := range set .nodes {
157103 // Deletion
158- if n .isDeleted () {
159- fmt .Fprintf (out , " [-]: %x prev: %x\n " , path , set . accessList [ path ] )
104+ if n .IsDeleted () {
105+ fmt .Fprintf (out , " [-]: %x prev: %x\n " , path , n . Prev )
160106 continue
161107 }
162108 // Insertion
163- origin , ok := set .accessList [path ]
164- if ! ok {
165- fmt .Fprintf (out , " [+]: %x -> %v\n " , path , n .hash )
109+ if len (n .Prev ) == 0 {
110+ fmt .Fprintf (out , " [+]: %x -> %v\n " , path , n .Hash )
166111 continue
167112 }
168113 // Update
169- fmt .Fprintf (out , " [*]: %x -> %v prev: %x\n " , path , n .hash , origin )
114+ fmt .Fprintf (out , " [*]: %x -> %v prev: %x\n " , path , n .Hash , n . Prev )
170115 }
171116 }
172117 for _ , n := range set .leaves {
0 commit comments