Skip to content

Commit 60e681e

Browse files
committed
fix Children() and add test (#159)
1 parent b2b8076 commit 60e681e

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

stateless.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,15 @@ func NewStateless() *StatelessNode {
7070
}
7171

7272
func (n *StatelessNode) Children() []VerkleNode {
73-
var children []VerkleNode
74-
for _, child := range n.children {
75-
children = append(children, child)
73+
var children [256]VerkleNode
74+
for i := range children {
75+
if n.children[byte(i)] != nil {
76+
children[i] = n.children[byte(i)]
77+
} else {
78+
children[i] = Empty(struct{}{})
79+
}
7680
}
77-
return children
81+
return children[:]
7882
}
7983

8084
func (n *StatelessNode) SetChild(i int, v VerkleNode) error {

stateless_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,38 @@ import (
3333
"testing"
3434
)
3535

36+
func TestStatelessChildren(t *testing.T) {
37+
root := NewStateless()
38+
root.Insert(zeroKeyTest, fourtyKeyTest, nil)
39+
root.Insert(oneKeyTest, fourtyKeyTest, nil)
40+
41+
list := root.Children()
42+
if len(list) != NodeWidth {
43+
t.Fatal("invalid list length")
44+
}
45+
46+
var emptycount = 0
47+
for _, v := range list {
48+
if _, ok := v.(Empty); ok {
49+
emptycount++
50+
}
51+
}
52+
if emptycount != NodeWidth-1 {
53+
t.Fatal("invalid number of children")
54+
}
55+
56+
if err := root.SetChild(72, Empty{}); err == nil {
57+
t.Fatal("didn't catch a stateful node being inserted in a stateless node")
58+
}
59+
if err := root.SetChild(512, Empty{}); err == nil {
60+
t.Fatal("didn't catch a node being inserted at an invalid index in a stateless node")
61+
}
62+
63+
if err := root.SetChild(3, &StatelessNode{}); err != nil {
64+
t.Fatal("error inserting stateless node")
65+
}
66+
}
67+
3668
func TestStatelessDelete(t *testing.T) {
3769
root := NewStateless()
3870
root.Insert(zeroKeyTest, fourtyKeyTest, nil)

0 commit comments

Comments
 (0)