@@ -49,12 +49,17 @@ func (sh *Encryptor) hash(n node.Node, db *Database, force bool) (node.Node, nod
49
49
return node .NewHashNode (nil ), n , err
50
50
}
51
51
52
- hashed , err := sh .store (collapsed , db , force )
52
+ encryptedNode , err := sh .encrypt (collapsed , db , force )
53
53
if err != nil {
54
54
return node .NewHashNode (nil ), n , err
55
55
}
56
56
57
- cachedHash , _ := hashed .(node.HashNode )
57
+ if hashData , ok := encryptedNode .(node.HashNode ); ok && db != nil {
58
+ // We are pooling the trie nodes into an intermediate memory cache
59
+ db .Insert (enc .BytesToHash (hashData ), sh .rlpBuf , n )
60
+ }
61
+
62
+ cachedHash , _ := encryptedNode .(node.HashNode )
58
63
switch cn := cached .(type ) {
59
64
case * node.ExtensionNode :
60
65
cn .Hash = cachedHash
@@ -68,7 +73,7 @@ func (sh *Encryptor) hash(n node.Node, db *Database, force bool) (node.Node, nod
68
73
}
69
74
}
70
75
71
- return hashed , cached , nil
76
+ return encryptedNode , cached , nil
72
77
}
73
78
74
79
// getChildHash hash children nodes
@@ -116,7 +121,8 @@ func (sh *Encryptor) getChildHash(original node.Node, db *Database) (node.Node,
116
121
}
117
122
}
118
123
119
- func (sh * Encryptor ) store (n node.Node , db * Database , force bool ) (node.Node , error ) {
124
+ // encrypt returns HashNode or incomin node
125
+ func (sh * Encryptor ) encrypt (n node.Node , db * Database , force bool ) (node.Node , error ) {
120
126
// Don't store hashes or empty nodes.
121
127
if _ , isHash := n .(node.HashNode ); n == nil || isHash {
122
128
return n , nil
@@ -134,19 +140,15 @@ func (sh *Encryptor) store(n node.Node, db *Database, force bool) (node.Node, er
134
140
}
135
141
136
142
// Larger nodes are replaced by their hash and stored in the database.
137
- hash , dirty := n .Cache ()
138
- if hash == nil || dirty {
139
- hash = sh .makeHashNode (sh .rlpBuf )
140
- }
141
-
142
- if db != nil {
143
- // We are pooling the trie nodes into an intermediate memory cache
144
- db .Insert (enc .BytesToHash (hash ), sh .rlpBuf , n )
143
+ newHash , dirty := n .Cache ()
144
+ if newHash == nil || dirty {
145
+ newHash = sh .makeHashNode (sh .rlpBuf )
145
146
}
146
147
147
- return node .NewHashNode (hash ), nil
148
+ return node .NewHashNode (newHash ), nil
148
149
}
149
150
151
+ // makeHashNode wrap data on sha3
150
152
func (sh * Encryptor ) makeHashNode (data []byte ) node.HashNode {
151
153
n := make (node.HashNode , sh .sha .Size ())
152
154
sh .sha .Reset ()
0 commit comments