Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Nov 18, 2021
1 parent 6673e86 commit 8274c87
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 11 deletions.
11 changes: 10 additions & 1 deletion lib/trie/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,16 @@ func encodeChildsSequentially(children [16]node, buffer *bytes.Buffer) (err erro
}

func encodeChild(child node, buffer *bytes.Buffer) (err error) {
if child == nil {
var isNil bool
switch impl := child.(type) {
case *branch:
isNil = impl == nil
case *leaf:
isNil = impl == nil
default:
isNil = child == nil
}
if isNil {
return nil
}

Expand Down
90 changes: 80 additions & 10 deletions lib/trie/hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,11 @@ func TestHashLeaf(t *testing.T) {

buffer := bytes.NewBuffer(nil)
const parallel = false

err := encodeNode(n, buffer, parallel)

if err != nil {
t.Errorf("did not hash leaf node: %s", err)
} else if buffer.Len() == 0 {
t.Errorf("did not hash leaf node: nil")
}
require.NoError(t, err)
assert.NotZero(t, buffer.Len())
}

func TestHashBranch(t *testing.T) {
Expand All @@ -48,13 +46,11 @@ func TestHashBranch(t *testing.T) {

buffer := bytes.NewBuffer(nil)
const parallel = false

err := encodeNode(n, buffer, parallel)

if err != nil {
t.Errorf("did not hash branch node: %s", err)
} else if buffer.Len() == 0 {
t.Errorf("did not hash branch node: nil")
}
require.NoError(t, err)
assert.NotZero(t, buffer.Len())
}

func TestHashShort(t *testing.T) {
Expand All @@ -73,3 +69,77 @@ func TestHashShort(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, encodingBuffer.Bytes(), digestBuffer.Bytes())
}

func Test_encodeChildsSequentially(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
children [16]node
expected []byte
err error
}{
"nil children": {},
}

for name, testCase := range testCases {
testCase := testCase
t.Run(name, func(t *testing.T) {
t.Parallel()

buffer := bytes.NewBuffer(nil)
err := encodeChildsSequentially(testCase.children, buffer)

if testCase.err != nil {
assert.EqualError(t, err, testCase.err.Error())
} else {
require.NoError(t, err)
}

assert.Equal(t, testCase.expected, buffer.Bytes())
})
}
}

func Test_encodeChild(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
child node
expected []byte
err error
}{
"nil node": {},
"nil leaf": {
child: (*leaf)(nil),
},
"nil branch": {
child: (*branch)(nil),
},
"empty leaf child": {
child: &leaf{},
expected: []byte{0x8, 0x40, 0x0},
},
"empty branch child": {
child: &branch{},
expected: []byte{0xc, 0x80, 0x0, 0x0},
},
}

for name, testCase := range testCases {
testCase := testCase
t.Run(name, func(t *testing.T) {
t.Parallel()

buffer := bytes.NewBuffer(nil)
err := encodeChild(testCase.child, buffer)

if testCase.err != nil {
assert.EqualError(t, err, testCase.err.Error())
} else {
require.NoError(t, err)
}

assert.Equal(t, testCase.expected, buffer.Bytes())
})
}
}

0 comments on commit 8274c87

Please sign in to comment.