small updates, add travis
arnaucube arnaucube@gmail.com
Sun, 07 Jul 2019 17:02:14 +0200
7 files changed,
48 insertions(+),
9 deletions(-)
M
README.md
→
README.md
@@ -1,5 +1,7 @@
-# go-merkletree [](https://goreportcard.com/report/github.com/arnaucube/go-merkletree) [](https://godoc.org/github.com/arnaucube/go-merkletree) +# go-merkletree [](https://goreportcard.com/report/github.com/arnaucube/go-merkletree) [](https://travis-ci.org/arnaucube/go-merkletree) [](https://godoc.org/github.com/arnaucube/go-merkletree) Optimized MerkleTree implementation in Go. + +Compatible with Rust version: https://github.com/arnaucube/merkletree-rs The MerkleTree is optimized in the design and concepts, to have a faster and lighter MerkleTree, maintaining compatibility with a non optimized MerkleTree. In this way, the MerkleRoot of the optimized MerkleTree will be the same that the MerkleRoot of the non optimized MerkleTree.
M
merkletree.go
→
merkletree.go
@@ -25,7 +25,7 @@ // ErrNodeAlreadyExists is an error that indicates that a node already exists in the merkletree database
ErrNodeAlreadyExists = errors.New("node already exists") rootNodeValue = HashBytes([]byte("root")) // EmptyNodeValue is a [32]byte EmptyNodeValue array, all to zero - EmptyNodeValue = Hash{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} + EmptyNodeValue = Hash{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} ) // Hash used in this tree, is the [32]byte keccak()@@ -113,6 +113,7 @@ ChildR: finalNode1Hash,
} } siblings = append(siblings, getEmptiesBetweenIAndPosHash(mt, i, posDiff+1)...) + if mt.root, err = mt.replaceLeaf(siblings, path[posDiff+1:], parentNode.Ht(), normalNodeType, 0, parentNode.Bytes()); err != nil { return err }@@ -233,8 +234,8 @@ }
if nodeType == byte(finalNodeType) { // check if nodeBytes path is different of hi index := nodeBytes[:indexLength] - hi := HashBytes(index) - nodePath := getPath(mt.numLevels, hi) + nodeHi := HashBytes(index) + nodePath := getPath(mt.numLevels, nodeHi) posDiff := comparePaths(path, nodePath) // if is different, return an EmptyNodeValue, else return the nodeBytes if posDiff != -1 {
M
merkletreeSpecification_test.go
→
merkletreeSpecification_test.go
@@ -48,6 +48,16 @@
// empty tree assert.Equal(t, "0x0000000000000000000000000000000000000000000000000000000000000000", mt.Root().Hex()) + leafN := testBytesLeaf{ + data: []byte{1, 2, 3, 4, 5}, + indexLength: 3, + } + assert.Nil(t, mt.Add(leafN)) + assert.Equal(t, "0xa0e72cc948119fcb71b413cf5ada12b2b825d5133299b20a6d9325ffc3e2fbf1", mt.Root().Hex()) + + mt = newTestingMerkle(t, 140) + defer mt.storage.Close() + // add leaf leaf := testBytesLeaf{ data: []byte("this is a test leaf"),@@ -129,24 +139,28 @@ assert.True(t, verified)
// add leafs in different orders mt1 := newTestingMerkle(t, 140) - defer mt.storage.Close() + defer mt1.storage.Close() mt1.Add(newTestBytesLeaf("0 this is a test leaf", 15)) mt1.Add(newTestBytesLeaf("1 this is a test leaf", 15)) mt1.Add(newTestBytesLeaf("2 this is a test leaf", 15)) mt1.Add(newTestBytesLeaf("3 this is a test leaf", 15)) mt1.Add(newTestBytesLeaf("4 this is a test leaf", 15)) + mt1.Add(newTestBytesLeaf("5 this is a test leaf", 15)) + // mt1.PrintFullMT() mt2 := newTestingMerkle(t, 140) - defer mt.storage.Close() + defer mt2.storage.Close() mt2.Add(newTestBytesLeaf("2 this is a test leaf", 15)) mt2.Add(newTestBytesLeaf("1 this is a test leaf", 15)) mt2.Add(newTestBytesLeaf("0 this is a test leaf", 15)) + mt2.Add(newTestBytesLeaf("5 this is a test leaf", 15)) mt2.Add(newTestBytesLeaf("3 this is a test leaf", 15)) mt2.Add(newTestBytesLeaf("4 this is a test leaf", 15)) assert.Equal(t, mt1.Root().Hex(), mt2.Root().Hex()) + assert.Equal(t, mt1.Root().Hex(), "0x264397f84da141b3134dcde1d7540d27a2bf0d787bbe8365d9ad5c9c18d3c621") // adding 1000 leafs mt1000 := newTestingMerkle(t, 140)
A
node_test.go
@@ -0,0 +1,16 @@
+package merkletree + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestNode(t *testing.T) { + n := treeNode{ + ChildL: EmptyNodeValue, + ChildR: EmptyNodeValue, + } + assert.Equal(t, "0xad3228b676f7d3cd4284a5443f17f1962b36e491b30a40b2405849e597ba5fb5", n.Ht().Hex()) + +}
M
print.go
→
print.go
@@ -1,7 +1,6 @@
package merkletree import ( - "encoding/hex" "fmt" "github.com/fatih/color"@@ -35,7 +34,8 @@ }
for i := 0; i < iLevel; i++ { fmt.Print(" ") } - color.Cyan(" leaf value: 0x" + hex.EncodeToString(leafNodeBytes)) + // color.Cyan(" leaf value: 0x" + hex.EncodeToString(leafNodeBytes)) + color.Cyan(" leaf value: " + string(leafNodeBytes)) } else { //EMPTY_NODE fmt.Print("[EmptyBranch]:")