@@ -6,41 +6,40 @@ import (
66 "fmt"
77 "sync"
88
9- "github.com/ethereum/go-ethereum/common"
109 "github.com/ethereum/go-ethereum/crypto"
10+ "github.com/ethereum/go-ethereum/common"
1111)
1212
1313func ParanoiaCheck (t1 * Trie , backend Backend ) (bool , * Trie ) {
14- t2 := New (common. Hash {} , backend )
14+ t2 := New (nil , backend )
1515
1616 it := t1 .Iterator ()
1717 for it .Next () {
1818 t2 .Update (it .Key , it .Value )
1919 }
2020
21- a , b := t2 .Hash (), t1 .Hash ()
22- return bytes .Equal (a [:], b [:]), t2
21+ return bytes .Equal (t2 .Hash (), t1 .Hash ()), t2
2322}
2423
2524type Trie struct {
2625 mu sync.Mutex
2726 root Node
28- roothash common. Hash
27+ roothash [] byte
2928 cache * Cache
3029
3130 revisions * list.List
3231}
3332
34- func New (root common. Hash , backend Backend ) * Trie {
33+ func New (root [] byte , backend Backend ) * Trie {
3534 trie := & Trie {}
3635 trie .revisions = list .New ()
3736 trie .roothash = root
3837 if backend != nil {
3938 trie .cache = NewCache (backend )
4039 }
4140
42- if ( root != common. Hash {}) {
43- value := common .NewValueFromBytes (trie .cache .Get (root [:] ))
41+ if root != nil {
42+ value := common .NewValueFromBytes (trie .cache .Get (root ))
4443 trie .root = trie .mknode (value )
4544 }
4645
@@ -52,13 +51,9 @@ func (self *Trie) Iterator() *Iterator {
5251}
5352
5453func (self * Trie ) Copy () * Trie {
55- //cpy := make([]byte, 32)
56- //copy(cpy, self.roothash)
57-
58- // cheap copying method
59- var cpy common.Hash
60- cpy .Set (self .roothash )
61- trie := New (common.Hash {}, nil )
54+ cpy := make ([]byte , 32 )
55+ copy (cpy , self .roothash )
56+ trie := New (nil , nil )
6257 trie .cache = self .cache .Copy ()
6358 if self .root != nil {
6459 trie .root = self .root .Copy (trie )
@@ -68,21 +63,21 @@ func (self *Trie) Copy() *Trie {
6863}
6964
7065// Legacy support
71- func (self * Trie ) Root () common. Hash { return self .Hash () }
72- func (self * Trie ) Hash () common. Hash {
73- var hash common. Hash
66+ func (self * Trie ) Root () [] byte { return self .Hash () }
67+ func (self * Trie ) Hash () [] byte {
68+ var hash [] byte
7469 if self .root != nil {
7570 t := self .root .Hash ()
76- if h , ok := t .(common. Hash ); ok && ( h != common. Hash {}) {
77- hash = h
71+ if byts , ok := t .([] byte ); ok && len ( byts ) > 0 {
72+ hash = byts
7873 } else {
79- hash = common . BytesToHash ( crypto .Sha3 (common .Encode (self .root .RlpData () )))
74+ hash = crypto .Sha3 (common .Encode (self .root .RlpData ()))
8075 }
8176 } else {
82- hash = common . BytesToHash ( crypto .Sha3 (common .Encode ("" ) ))
77+ hash = crypto .Sha3 (common .Encode ("" ))
8378 }
8479
85- if hash != self .roothash {
80+ if ! bytes . Equal ( hash , self .roothash ) {
8681 self .revisions .PushBack (self .roothash )
8782 self .roothash = hash
8883 }
@@ -107,21 +102,19 @@ func (self *Trie) Reset() {
107102 self .cache .Reset ()
108103
109104 if self .revisions .Len () > 0 {
110- revision := self .revisions .Remove (self .revisions .Back ()).(common. Hash )
105+ revision := self .revisions .Remove (self .revisions .Back ()).([] byte )
111106 self .roothash = revision
112107 }
113- value := common .NewValueFromBytes (self .cache .Get (self .roothash [:] ))
108+ value := common .NewValueFromBytes (self .cache .Get (self .roothash ))
114109 self .root = self .mknode (value )
115110}
116111
117- func (self * Trie ) UpdateString (key , value string ) Node {
118- return self .Update (common .StringToHash (key ), []byte (value ))
119- }
120- func (self * Trie ) Update (key common.Hash , value []byte ) Node {
112+ func (self * Trie ) UpdateString (key , value string ) Node { return self .Update ([]byte (key ), []byte (value )) }
113+ func (self * Trie ) Update (key , value []byte ) Node {
121114 self .mu .Lock ()
122115 defer self .mu .Unlock ()
123116
124- k := CompactHexDecode (key . Str ( ))
117+ k := CompactHexDecode (string ( key ))
125118
126119 if len (value ) != 0 {
127120 self .root = self .insert (self .root , k , & ValueNode {self , value })
@@ -132,12 +125,12 @@ func (self *Trie) Update(key common.Hash, value []byte) Node {
132125 return self .root
133126}
134127
135- func (self * Trie ) GetString (key string ) []byte { return self .Get (common . StringToHash (key )) }
136- func (self * Trie ) Get (key common. Hash ) []byte {
128+ func (self * Trie ) GetString (key string ) []byte { return self .Get ([] byte (key )) }
129+ func (self * Trie ) Get (key [] byte ) []byte {
137130 self .mu .Lock ()
138131 defer self .mu .Unlock ()
139132
140- k := CompactHexDecode (key . Str ( ))
133+ k := CompactHexDecode (string ( key ))
141134
142135 n := self .get (self .root , k )
143136 if n != nil {
@@ -147,12 +140,12 @@ func (self *Trie) Get(key common.Hash) []byte {
147140 return nil
148141}
149142
150- func (self * Trie ) DeleteString (key string ) Node { return self .Delete (common . StringToHash (key )) }
151- func (self * Trie ) Delete (key common. Hash ) Node {
143+ func (self * Trie ) DeleteString (key string ) Node { return self .Delete ([] byte (key )) }
144+ func (self * Trie ) Delete (key [] byte ) Node {
152145 self .mu .Lock ()
153146 defer self .mu .Unlock ()
154147
155- k := CompactHexDecode (key . Str ( ))
148+ k := CompactHexDecode (string ( key ))
156149 self .root = self .delete (self .root , k )
157150
158151 return self .root
0 commit comments