Skip to content

Commit 3c7181d

Browse files
committed
Fixed a copy issue in the trie which could cause a consensus failure
1 parent 6fecb15 commit 3c7181d

7 files changed

Lines changed: 53 additions & 19 deletions

File tree

‎trie/cache.go‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ func (self *Cache) Flush() {
3737
//self.Reset()
3838
}
3939

40+
func (self *Cache) Copy() *Cache {
41+
cache := NewCache(self.backend)
42+
for k, v := range self.store {
43+
cache.store[k] = v
44+
}
45+
return cache
46+
}
47+
4048
func (self *Cache) Reset() {
41-
self.store = make(map[string][]byte)
49+
//self.store = make(map[string][]byte)
4250
}

‎trie/fullnode.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ func (self *FullNode) Branches() []Node {
2020
return self.nodes[:16]
2121
}
2222

23-
func (self *FullNode) Copy() Node {
24-
nnode := NewFullNode(self.trie)
23+
func (self *FullNode) Copy(t *Trie) Node {
24+
nnode := NewFullNode(t)
2525
for i, node := range self.nodes {
2626
if node != nil {
27-
nnode.nodes[i] = node
27+
nnode.nodes[i] = node.Copy(t)
2828
}
2929
}
3030

‎trie/hashnode.go‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package trie
22

3+
import "github.com/ethereum/go-ethereum/ethutil"
4+
35
type HashNode struct {
4-
key []byte
6+
key []byte
7+
trie *Trie
58
}
69

7-
func NewHash(key []byte) *HashNode {
8-
return &HashNode{key}
10+
func NewHash(key []byte, trie *Trie) *HashNode {
11+
return &HashNode{key, trie}
912
}
1013

1114
func (self *HashNode) RlpData() interface{} {
@@ -17,6 +20,6 @@ func (self *HashNode) Hash() interface{} {
1720
}
1821

1922
// These methods will never be called but we have to satisfy Node interface
20-
func (self *HashNode) Value() Node { return nil }
21-
func (self *HashNode) Dirty() bool { return true }
22-
func (self *HashNode) Copy() Node { return self }
23+
func (self *HashNode) Value() Node { return nil }
24+
func (self *HashNode) Dirty() bool { return true }
25+
func (self *HashNode) Copy(t *Trie) Node { return NewHash(ethutil.CopyBytes(self.key), t) }

‎trie/node.go‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ var indices = []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b
66

77
type Node interface {
88
Value() Node
9-
Copy() Node // All nodes, for now, return them self
9+
Copy(*Trie) Node // All nodes, for now, return them self
1010
Dirty() bool
1111
fstring(string) string
1212
Hash() interface{}
@@ -18,7 +18,11 @@ func (self *ValueNode) String() string { return self.fstring("") }
1818
func (self *FullNode) String() string { return self.fstring("") }
1919
func (self *ShortNode) String() string { return self.fstring("") }
2020
func (self *ValueNode) fstring(ind string) string { return fmt.Sprintf("%x ", self.data) }
21-
func (self *HashNode) fstring(ind string) string { return fmt.Sprintf("< %x > ", self.key) }
21+
22+
//func (self *HashNode) fstring(ind string) string { return fmt.Sprintf("< %x > ", self.key) }
23+
func (self *HashNode) fstring(ind string) string {
24+
return fmt.Sprintf("%v", self.trie.trans(self))
25+
}
2226

2327
// Full node
2428
func (self *FullNode) fstring(ind string) string {

‎trie/shortnode.go‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package trie
22

3+
import "github.com/ethereum/go-ethereum/ethutil"
4+
35
type ShortNode struct {
46
trie *Trie
57
key []byte
@@ -15,7 +17,11 @@ func (self *ShortNode) Value() Node {
1517
return self.value
1618
}
1719
func (self *ShortNode) Dirty() bool { return true }
18-
func (self *ShortNode) Copy() Node { return NewShortNode(self.trie, self.key, self.value) }
20+
func (self *ShortNode) Copy(t *Trie) Node {
21+
node := &ShortNode{t, nil, self.value.Copy(t)}
22+
node.key = ethutil.CopyBytes(self.key)
23+
return node
24+
}
1925

2026
func (self *ShortNode) RlpData() interface{} {
2127
return []interface{}{self.key, self.value.Hash()}

‎trie/trie.go‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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

5153
func (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

332342
func (self *Trie) PrintRoot() {
333343
fmt.Println(self.root)
344+
fmt.Printf("root=%x\n", self.Root())
334345
}

‎trie/valuenode.go‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package trie
22

3+
import "github.com/ethereum/go-ethereum/ethutil"
4+
35
type ValueNode struct {
46
trie *Trie
57
data []byte
@@ -8,6 +10,6 @@ type ValueNode struct {
810
func (self *ValueNode) Value() Node { return self } // Best not to call :-)
911
func (self *ValueNode) Val() []byte { return self.data }
1012
func (self *ValueNode) Dirty() bool { return true }
11-
func (self *ValueNode) Copy() Node { return &ValueNode{self.trie, self.data} }
13+
func (self *ValueNode) Copy(t *Trie) Node { return &ValueNode{t, ethutil.CopyBytes(self.data)} }
1214
func (self *ValueNode) RlpData() interface{} { return self.data }
1315
func (self *ValueNode) Hash() interface{} { return self.data }

0 commit comments

Comments
 (0)