Skip to content

Commit

Permalink
test: ExampleNamespacedMerkleTree (#203)
Browse files Browse the repository at this point in the history
Previously the function `ExampleNamespacedMerkleTree` existed in
nmt_test.go. AFAIK Go only runs unit tests for functions with the name
`Test*` so I wouldn't expect `ExampleNamespacedMerkleTree` to run by the
Go test runner. But it is run and fails
[here](https://github.com/celestiaorg/nmt/actions/runs/5092193098/jobs/9153228914?pr=202#step:7:551).

This PR converts the example into a test. Defer to @staheri14 if it
should be deleted instead.
  • Loading branch information
rootulp authored Jun 1, 2023
1 parent 9efc9bf commit 5466a61
Showing 1 changed file with 17 additions and 34 deletions.
51 changes: 17 additions & 34 deletions nmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,62 +52,45 @@ func newNamespaceDataPairRaw(nidSize int, data []byte) namespaceDataPair {
}
}

func ExampleNamespacedMerkleTree() {
// the tree will use this namespace size
nidSize := 1
// the leaves that will be pushed
func TestExampleNamespacedMerkleTree(t *testing.T) {
data := [][]byte{
append(namespace.ID{0}, []byte("leaf_0")...),
append(namespace.ID{0}, []byte("leaf_1")...),
append(namespace.ID{1}, []byte("leaf_2")...),
append(namespace.ID{1}, []byte("leaf_3")...),
}
// Init a tree with the namespace size as well as
// the underlying hash function:
nidSize := 1
tree := New(sha256.New(), NamespaceIDSize(nidSize))

for _, d := range data {
if err := tree.Push(d); err != nil {
panic(fmt.Sprintf("unexpected error: %v", err))
}
err := tree.Push(d)
assert.NoError(t, err)
}
// compute the root

root, err := tree.Root()
if err != nil {
panic("unexpected error")
}
assert.NoError(t, err)

// the root's min/max namespace is the min and max namespace of all leaves:
minNS := MinNamespace(root, tree.NamespaceSize())
maxNS := MaxNamespace(root, tree.NamespaceSize())
if bytes.Equal(minNS, namespace.ID{0}) {
fmt.Printf("Min namespace: %x\n", minNS)
}
if bytes.Equal(maxNS, namespace.ID{1}) {
fmt.Printf("Max namespace: %x\n", maxNS)
}
assert.Equal(t, minNS, []byte(namespace.ID{0}))
assert.Equal(t, maxNS, []byte(namespace.ID{1}))

// compute proof for namespace 0:
// compute proof for namespace 0
proof, err := tree.ProveNamespace(namespace.ID{0})
if err != nil {
panic("unexpected error")
}
assert.NoError(t, err)

// verify proof using the root and the leaves of namespace 0:
// verify proof using the root and the leaves of namespace 0
leafs := [][]byte{
append(namespace.ID{0}, []byte("leaf_0")...),
append(namespace.ID{0}, []byte("leaf_1")...),
}

if proof.VerifyNamespace(sha256.New(), namespace.ID{0}, leafs, root) {
fmt.Printf("Successfully verified namespace: %x\n", namespace.ID{0})
}
got := proof.VerifyNamespace(sha256.New(), namespace.ID{0}, leafs, root)
assert.True(t, got)

if proof.VerifyNamespace(sha256.New(), namespace.ID{2}, leafs, root) {
panic(fmt.Sprintf("Proof for namespace %x, passed for namespace: %x\n", namespace.ID{0}, namespace.ID{2}))
}
// Output:
// Min namespace: 00
// Max namespace: 01
// Successfully verified namespace: 00
got = proof.VerifyNamespace(sha256.New(), namespace.ID{2}, leafs, root)
assert.False(t, got) // namespace 2 is not in the tree, so the proof should fail to verify
}

func TestNamespacedMerkleTree_Push(t *testing.T) {
Expand Down

0 comments on commit 5466a61

Please sign in to comment.