Skip to content

Commit

Permalink
Change Root method result.
Browse files Browse the repository at this point in the history
  • Loading branch information
onrik committed Apr 14, 2017
1 parent 289fdf1 commit 653b020
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
4 changes: 2 additions & 2 deletions gomerkle.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ func (tree *Tree) Leaves() []Node {
}

// Root returns the root node of the tree, if available, else nil
func (tree *Tree) Root() *Node {
func (tree *Tree) Root() []byte {
if tree.Nodes == nil {
return nil
}

return &tree.Levels[0][0]
return tree.Levels[0][0].Hash
}

// GetProof generates proof
Expand Down
16 changes: 6 additions & 10 deletions gomerkle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,17 +318,13 @@ func verifyGeneratedTree(t *testing.T, tree *Tree) {
require.NotNil(t, rootRow)

// The root row should be of length 1
require.Equal(t, len(rootRow), 1,
"The root row should contain only 1 node")
require.Equal(t, 1, len(rootRow))

// the Root() should be the only item in the top row
require.Equal(t, tree.Root(), &rootRow[0],
"tree.Root() is not the expected node")
require.Equal(t, rootRow[0].Hash, tree.Root())

// The Leaves() should the deepest row
require.Equal(t, len(tree.Leaves()),
len(tree.GetNodesAtHeight(tree.Height())),
"tree.Leaves() is not the expected row")
require.Equal(t, len(tree.GetNodesAtHeight(tree.Height())), len(tree.Leaves()))
}

func verifyInitialState(t *testing.T, tree *Tree) {
Expand Down Expand Up @@ -427,7 +423,7 @@ func TestGenerateWithOneNode(t *testing.T) {

proof := tree.GetProof(0)
require.Equal(t, 0, len(proof))
require.Equal(t, tree.hash(value), tree.Root().Hash)
require.Equal(t, tree.hash(value), tree.Root())
}

func TestGetNodesAtHeight(t *testing.T) {
Expand Down Expand Up @@ -508,7 +504,7 @@ func TestRootHashValue(t *testing.T) {
// Calculate the root hash with the simpler method
merk := simpleMerkle(data)

require.True(t, bytes.Equal(tree.Root().Hash, merk))
require.True(t, bytes.Equal(tree.Root(), merk))
}

func TestGetProof(t *testing.T) {
Expand Down Expand Up @@ -555,7 +551,7 @@ func TestVerifyProof(t *testing.T) {
err := tree.Generate()
require.Nil(t, err)

root := tree.Root().Hash
root := tree.Root()
for i := 0; i < len(data); i++ {
proof := tree.GetProof(i)
require.True(t, tree.VerifyProof(proof, root, data[i]))
Expand Down

0 comments on commit 653b020

Please sign in to comment.