@@ -34,7 +34,9 @@ func New(root []byte, backend Backend) *Trie {
3434 trie := & Trie {}
3535 trie .revisions = list .New ()
3636 trie .roothash = root
37- trie .cache = NewCache (backend )
37+ if backend != nil {
38+ trie .cache = NewCache (backend )
39+ }
3840
3941 if root != nil {
4042 value := ethutil .NewValueFromBytes (trie .cache .Get (root ))
@@ -49,7 +51,15 @@ func (self *Trie) Iterator() *Iterator {
4951}
5052
5153func (self * Trie ) Copy () * Trie {
52- return New (self .roothash , self .cache .backend )
54+ cpy := make ([]byte , 32 )
55+ copy (cpy , self .roothash )
56+ trie := New (nil , nil )
57+ trie .cache = self .cache .Copy ()
58+ if self .root != nil {
59+ trie .root = self .root .Copy (trie )
60+ }
61+
62+ return trie
5363}
5464
5565// Legacy support
@@ -177,7 +187,7 @@ func (self *Trie) insert(node Node, key []byte, value Node) Node {
177187 return NewShortNode (self , key [:matchlength ], n )
178188
179189 case * FullNode :
180- cpy := node .Copy ().(* FullNode )
190+ cpy := node .Copy (self ).(* FullNode )
181191 cpy .set (key [0 ], self .insert (node .branch (key [0 ]), key [1 :], value ))
182192
183193 return cpy
@@ -244,7 +254,7 @@ func (self *Trie) delete(node Node, key []byte) Node {
244254 }
245255
246256 case * FullNode :
247- n := node .Copy ().(* FullNode )
257+ n := node .Copy (self ).(* FullNode )
248258 n .set (key [0 ], self .delete (n .branch (key [0 ]), key [1 :]))
249259
250260 pos := - 1
@@ -301,7 +311,7 @@ func (self *Trie) mknode(value *ethutil.Value) Node {
301311 }
302312 return fnode
303313 case 32 :
304- return & HashNode {value .Bytes ()}
314+ return & HashNode {value .Bytes (), self }
305315 }
306316
307317 return & ValueNode {self , value .Bytes ()}
@@ -331,4 +341,5 @@ func (self *Trie) store(node Node) interface{} {
331341
332342func (self * Trie ) PrintRoot () {
333343 fmt .Println (self .root )
344+ fmt .Printf ("root=%x\n " , self .Root ())
334345}
0 commit comments