Skip to content

Commit 58d477f

Browse files
committed
Fixed a bug where keys where serialised twice
1 parent 804af96 commit 58d477f

4 files changed

Lines changed: 23 additions & 12 deletions

File tree

‎ptrie/fullnode.go‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ func (self *FullNode) Branches() []Node {
2323
func (self *FullNode) Copy() Node {
2424
nnode := NewFullNode(self.trie)
2525
for i, node := range self.nodes {
26-
nnode.nodes[i] = node
26+
if node != nil {
27+
nnode.nodes[i] = node
28+
}
2729
}
2830

2931
return nnode
@@ -60,7 +62,6 @@ func (self *FullNode) RlpData() interface{} {
6062
func (self *FullNode) set(k byte, value Node) {
6163
if _, ok := value.(*ValueNode); ok && k != 16 {
6264
fmt.Println(value, k)
63-
panic(":(")
6465
}
6566

6667
self.nodes[int(k)] = value

‎ptrie/iterator.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type Iterator struct {
1414
}
1515

1616
func NewIterator(trie *Trie) *Iterator {
17-
return &Iterator{trie: trie, Key: []byte{0}}
17+
return &Iterator{trie: trie, Key: make([]byte, 32)}
1818
}
1919

2020
func (self *Iterator) Next() bool {

‎ptrie/node.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type Node interface {
1717
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("") }
20-
func (self *ValueNode) fstring(ind string) string { return fmt.Sprintf("%s ", self.data) }
20+
func (self *ValueNode) fstring(ind string) string { return fmt.Sprintf("%x ", self.data) }
2121
func (self *HashNode) fstring(ind string) string { return fmt.Sprintf("%x ", self.key) }
2222

2323
// Full node
@@ -36,5 +36,5 @@ func (self *FullNode) fstring(ind string) string {
3636

3737
// Short node
3838
func (self *ShortNode) fstring(ind string) string {
39-
return fmt.Sprintf("[ %s: %v ] ", self.key, self.value.fstring(ind+" "))
39+
return fmt.Sprintf("[ %x: %v ] ", self.key, self.value.fstring(ind+" "))
4040
}

‎ptrie/trie.go‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ func (self *Trie) get(node Node, key []byte) Node {
215215
}
216216

217217
func (self *Trie) delete(node Node, key []byte) Node {
218-
if len(key) == 0 {
218+
if len(key) == 0 && node == nil {
219219
return nil
220220
}
221221

@@ -234,7 +234,9 @@ func (self *Trie) delete(node Node, key []byte) Node {
234234
nkey := append(k, child.Key()...)
235235
n = NewShortNode(self, nkey, child.Value())
236236
case *FullNode:
237-
n = NewShortNode(self, node.key, child)
237+
sn := NewShortNode(self, node.Key(), child)
238+
sn.key = node.key
239+
n = sn
238240
}
239241

240242
return n
@@ -275,9 +277,10 @@ func (self *Trie) delete(node Node, key []byte) Node {
275277
}
276278

277279
return nnode
278-
280+
case nil:
281+
return nil
279282
default:
280-
panic("Invalid node")
283+
panic(fmt.Sprintf("%T: invalid node: %v (%v)", node, node, key))
281284
}
282285
}
283286

@@ -288,7 +291,10 @@ func (self *Trie) mknode(value *ethutil.Value) Node {
288291
case 0:
289292
return nil
290293
case 2:
291-
return NewShortNode(self, trie.CompactDecode(string(value.Get(0).Bytes())), self.mknode(value.Get(1)))
294+
// A value node may consists of 2 bytes.
295+
if value.Get(0).Len() != 0 {
296+
return NewShortNode(self, trie.CompactDecode(string(value.Get(0).Bytes())), self.mknode(value.Get(1)))
297+
}
292298
case 17:
293299
fnode := NewFullNode(self)
294300
for i := 0; i < l; i++ {
@@ -297,9 +303,9 @@ func (self *Trie) mknode(value *ethutil.Value) Node {
297303
return fnode
298304
case 32:
299305
return &HashNode{value.Bytes()}
300-
default:
301-
return &ValueNode{self, value.Bytes()}
302306
}
307+
308+
return &ValueNode{self, value.Bytes()}
303309
}
304310

305311
func (self *Trie) trans(node Node) Node {
@@ -323,3 +329,7 @@ func (self *Trie) store(node Node) interface{} {
323329

324330
return node.RlpData()
325331
}
332+
333+
func (self *Trie) PrintRoot() {
334+
fmt.Println(self.root)
335+
}

0 commit comments

Comments
 (0)