Skip to content

Commit

Permalink
Remove now unneeded encodeRoot and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Jul 12, 2022
1 parent 5e89f49 commit 67c79b5
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 222 deletions.
77 changes: 0 additions & 77 deletions lib/trie/buffer_mock_test.go

This file was deleted.

9 changes: 0 additions & 9 deletions lib/trie/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package trie

import (
"errors"
"math/rand"
"testing"
"time"
Expand All @@ -13,14 +12,6 @@ import (
"github.com/stretchr/testify/require"
)

type writeCall struct {
written []byte
n int
err error
}

var errTest = errors.New("test error")

type keyValues struct {
key []byte
value []byte
Expand Down
14 changes: 1 addition & 13 deletions lib/trie/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,18 +153,6 @@ func (t *Trie) RootNode() *Node {
return t.root.Copy(copySettings)
}

// encodeRoot writes the encoding of the root node to the buffer.
func encodeRoot(root *Node, buffer node.Buffer) (err error) {
if root == nil {
_, err = buffer.Write([]byte{0})
if err != nil {
return fmt.Errorf("cannot write nil root node to buffer: %w", err)
}
return nil
}
return root.Encode(buffer)
}

// MustHash returns the hashed root of the trie.
// It panics if it fails to hash the root node.
func (t *Trie) MustHash() common.Hash {
Expand All @@ -182,7 +170,7 @@ func (t *Trie) Hash() (rootHash common.Hash, err error) {
buffer.Reset()
defer pools.EncodingBuffers.Put(buffer)

err = encodeRoot(t.root, buffer)
err = t.root.Encode(buffer)
if err != nil {
return [32]byte{}, err
}
Expand Down
123 changes: 0 additions & 123 deletions lib/trie/trie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/ChainSafe/gossamer/internal/trie/node"
"github.com/ChainSafe/gossamer/lib/common"
gomock "github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -291,128 +290,6 @@ func Test_Trie_RootNode(t *testing.T) {
assert.Equal(t, expectedRoot, root)
}

//go:generate mockgen -destination=buffer_mock_test.go -package $GOPACKAGE github.com/ChainSafe/gossamer/internal/trie/node Buffer

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

type bufferCalls struct {
writeCalls []writeCall
lenCall bool
lenReturn int
bytesCall bool
bytesReturn []byte
}

testCases := map[string]struct {
root *Node
bufferCalls bufferCalls
errWrapped error
errMessage string
expectedRoot *Node
}{
"nil root and no error": {
bufferCalls: bufferCalls{
writeCalls: []writeCall{
{written: []byte{0}},
},
},
},
"nil root and write error": {
bufferCalls: bufferCalls{
writeCalls: []writeCall{
{
written: []byte{0},
err: errTest,
},
},
},
errWrapped: errTest,
errMessage: "cannot write nil root node to buffer: test error",
},
"root encoding error": {
root: &Node{
Key: []byte{1, 2},
Value: []byte{1},
},
bufferCalls: bufferCalls{
writeCalls: []writeCall{
{
written: []byte{66},
err: errTest,
},
},
},
errWrapped: errTest,
errMessage: "cannot encode header: test error",
expectedRoot: &Node{
Key: []byte{1, 2},
Value: []byte{1},
},
},
"root encoding success": {
root: &Node{
Key: []byte{1, 2},
Value: []byte{1},
},
bufferCalls: bufferCalls{
writeCalls: []writeCall{
{written: []byte{66}},
{written: []byte{18}},
{written: []byte{4, 1}},
},
lenCall: true,
lenReturn: 3,
bytesCall: true,
bytesReturn: []byte{66, 18, 4, 1},
},
expectedRoot: &Node{
Key: []byte{1, 2},
Value: []byte{1},
Encoding: []byte{66, 18, 4},
},
},
}

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

buffer := NewMockBuffer(ctrl)

var previousCall *gomock.Call
for _, write := range testCase.bufferCalls.writeCalls {
call := buffer.EXPECT().
Write(write.written).
Return(write.n, write.err)

if previousCall != nil {
call.After(previousCall)
}
previousCall = call
}
if testCase.bufferCalls.lenCall {
buffer.EXPECT().Len().
Return(testCase.bufferCalls.lenReturn)
}
if testCase.bufferCalls.bytesCall {
buffer.EXPECT().Bytes().
Return(testCase.bufferCalls.bytesReturn)
}

err := encodeRoot(testCase.root, buffer)

assert.ErrorIs(t, err, testCase.errWrapped)
if testCase.errWrapped != nil {
assert.EqualError(t, err, testCase.errMessage)
}
assert.Equal(t, testCase.expectedRoot, testCase.root)
})
}
}

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

Expand Down

0 comments on commit 67c79b5

Please sign in to comment.