Skip to content

Commit

Permalink
fix Children() and add test (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
gballet authored Dec 17, 2021
1 parent c6ec2b6 commit b7619f9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
12 changes: 8 additions & 4 deletions stateless.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,15 @@ func NewStateless() *StatelessNode {
}

func (n *StatelessNode) Children() []VerkleNode {
var children []VerkleNode
for _, child := range n.children {
children = append(children, child)
var children [256]VerkleNode
for i := range children {
if n.children[byte(i)] != nil {
children[i] = n.children[byte(i)]
} else {
children[i] = Empty(struct{}{})
}
}
return children
return children[:]
}

func (n *StatelessNode) SetChild(i int, v VerkleNode) error {
Expand Down
32 changes: 32 additions & 0 deletions stateless_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,38 @@ import (
"testing"
)

func TestStatelessChildren(t *testing.T) {
root := NewStateless()
root.Insert(zeroKeyTest, fourtyKeyTest, nil)
root.Insert(oneKeyTest, fourtyKeyTest, nil)

list := root.Children()
if len(list) != NodeWidth {
t.Fatal("invalid list length")
}

var emptycount = 0
for _, v := range list {
if _, ok := v.(Empty); ok {
emptycount++
}
}
if emptycount != NodeWidth-1 {
t.Fatal("invalid number of children")
}

if err := root.SetChild(72, Empty{}); err == nil {
t.Fatal("didn't catch a stateful node being inserted in a stateless node")
}
if err := root.SetChild(512, Empty{}); err == nil {
t.Fatal("didn't catch a node being inserted at an invalid index in a stateless node")
}

if err := root.SetChild(3, &StatelessNode{}); err != nil {
t.Fatal("error inserting stateless node")
}
}

func TestStatelessDelete(t *testing.T) {
root := NewStateless()
root.Insert(zeroKeyTest, fourtyKeyTest, nil)
Expand Down

0 comments on commit b7619f9

Please sign in to comment.