Skip to content

Commit

Permalink
Actually change the method signature to only take one argument
Browse files Browse the repository at this point in the history
  • Loading branch information
liamsi committed Mar 19, 2021
1 parent 901d0bd commit a98611c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 34 deletions.
2 changes: 1 addition & 1 deletion fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestFuzzProveVerifyNameSpace(t *testing.T) {
for _, ns := range sortedKeys {
leafDataList := nidDataMap[ns]
for _, d := range leafDataList {
err := tree.Push(d[:size], d[size:])
err := tree.Push(d[:size])
if err != nil {
t.Fatalf("error on Push(): %v", err)
}
Expand Down
11 changes: 6 additions & 5 deletions nmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,16 +246,17 @@ func (n NamespacedMerkleTree) NamespaceSize() namespace.IDSize {
// Returns an error if the namespace ID size of the input
// does not match the tree's NamespaceSize() or the leaves are not pushed in
// order (i.e. lexicographically sorted by namespace ID).
func (n *NamespacedMerkleTree) Push(id namespace.ID, data []byte) error {
err := n.validateNamespace(id)
func (n *NamespacedMerkleTree) Push(namespacedData namespace.PrefixedData) error {
nid := namespace.ID(namespacedData[:n.NamespaceSize()])
err := n.validateNamespace(nid)
if err != nil {
return err
}
leafData := append(id, data...)

// update relevant "caches":
n.leaves = append(n.leaves, leafData)
n.leaves = append(n.leaves, namespacedData)
n.updateNamespaceRanges()
n.updateMinMaxID(id)
n.updateMinMaxID(nid)
return nil
}

Expand Down
55 changes: 28 additions & 27 deletions nmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@ func ExampleNamespacedMerkleTree() {
// the tree will use this namespace size
nidSize := 1
// the leaves that will be pushed
data := []namespaceDataPair{
newNamespaceDataPair(namespace.ID{0}, []byte("leaf_0")),
newNamespaceDataPair(namespace.ID{0}, []byte("leaf_1")),
newNamespaceDataPair(namespace.ID{1}, []byte("leaf_2")),
newNamespaceDataPair(namespace.ID{1}, []byte("leaf_3"))}
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:
tree := New(sha256.New(), NamespaceIDSize(nidSize))
for _, d := range data {
if err := tree.Push(d.ID, d.Data); err != nil {
if err := tree.Push(d); err != nil {
panic("unexpected error")
}
}
Expand Down Expand Up @@ -89,27 +89,28 @@ func ExampleNamespacedMerkleTree() {
func TestNamespacedMerkleTree_Push(t *testing.T) {
tests := []struct {
name string
data namespaceDataPair
data namespace.PrefixedData
wantErr bool
}{
{"1st push: always OK", newNamespaceDataPair([]byte{0, 0, 0}, []byte("dummy data")), false},
{"push with same namespace: OK", newNamespaceDataPair([]byte{0, 0, 0}, []byte("dummy data")), false},
{"push with greater namespace: OK", newNamespaceDataPair([]byte{0, 0, 1}, []byte("dummy data")), false},
{"push with smaller namespace: Err", newNamespaceDataPair([]byte{0, 0, 0}, []byte("dummy data")), true},
{"push with same namespace: Ok", newNamespaceDataPair([]byte{0, 0, 1}, []byte("dummy data")), false},
{"push with greater namespace: Ok", newNamespaceDataPair([]byte{1, 0, 0}, []byte("dummy data")), false},
{"push with smaller namespace: Err", newNamespaceDataPair([]byte{0, 0, 1}, []byte("dummy data")), true},
{"push with smaller namespace: Err", newNamespaceDataPair([]byte{0, 0, 0}, []byte("dummy data")), true},
{"push with smaller namespace: Err", newNamespaceDataPair([]byte{0, 1, 0}, []byte("dummy data")), true},
{"push with same as last namespace: OK", newNamespaceDataPair([]byte{1, 0, 0}, []byte("dummy data")), false},
{"push with greater as last namespace: OK", newNamespaceDataPair([]byte{1, 1, 0}, []byte("dummy data")), false},
// note this tests for another kind of error: ErrMismatchedNamespaceSize
{"push with wrong namespace size: Err", newNamespaceDataPair([]byte{1, 1, 0, 0}, []byte("dummy data")), true},
{"1st push: always OK", append([]byte{0, 0, 0}, []byte("dummy data")...), false},
{"push with same namespace: OK", append([]byte{0, 0, 0}, []byte("dummy data")...), false},
{"push with greater namespace: OK", append([]byte{0, 0, 1}, []byte("dummy data")...), false},
{"push with smaller namespace: Err", append([]byte{0, 0, 0}, []byte("dummy data")...), true},
{"push with same namespace: Ok", append([]byte{0, 0, 1}, []byte("dummy data")...), false},
{"push with greater namespace: Ok", append([]byte{1, 0, 0}, []byte("dummy data")...), false},
{"push with smaller namespace: Err", append([]byte{0, 0, 1}, []byte("dummy data")...), true},
{"push with smaller namespace: Err", append([]byte{0, 0, 0}, []byte("dummy data")...), true},
{"push with smaller namespace: Err", append([]byte{0, 1, 0}, []byte("dummy data")...), true},
{"push with same as last namespace: OK", append([]byte{1, 0, 0}, []byte("dummy data")...), false},
{"push with greater as last namespace: OK", append([]byte{1, 1, 0}, []byte("dummy data")...), false},
// note this will not error as the NMT can not clearly know the border between the namespace and the raw data
// TODO(ismail): remove the corresponding error code ErrMismatchedNamespaceSize
{"push with wrong namespace size: Err", append([]byte{1, 1, 0, 0}, []byte("dummy data")...), false},
}
n := New(sha256.New(), NamespaceIDSize(3))
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := n.Push(tt.data.ID, tt.data.Data); (err != nil) != tt.wantErr {
if err := n.Push(tt.data); (err != nil) != tt.wantErr {
t.Errorf("Push() error = %v, wantErr %v", err, tt.wantErr)
}
})
Expand Down Expand Up @@ -148,7 +149,7 @@ func TestNamespacedMerkleTreeRoot(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
n := New(sha256.New(), NamespaceIDSize(tt.nidLen))
for _, d := range tt.pushedData {
if err := n.Push(d.ID, d.Data); err != nil {
if err := n.Push(namespace.PrefixedData(append(d.ID, d.Data...))); err != nil {
t.Errorf("Push() error = %v, expected no error", err)
}
}
Expand Down Expand Up @@ -233,7 +234,7 @@ func TestNamespacedMerkleTree_ProveNamespace_Ranges_And_Verify(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
n := New(sha256.New(), NamespaceIDSize(tt.nidLen))
for _, d := range tt.pushData {
err := n.Push(d.ID, d.Data)
err := n.Push(namespace.PrefixedData(append(d.ID, d.Data...)))
if err != nil {
t.Fatalf("invalid test case: %v, error on Push(): %v", tt.name, err)
}
Expand Down Expand Up @@ -435,7 +436,7 @@ func TestIgnoreMaxNamespace(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
tree := New(hash, NamespaceIDSize(nidSize), IgnoreMaxNamespace(tc.ignoreMaxNamespace))
for _, d := range tc.pushData {
if err := tree.Push(d.NamespaceID(), d.Data()); err != nil {
if err := tree.Push(namespace.PrefixedData(d)); err != nil {
panic("unexpected error")
}
}
Expand Down Expand Up @@ -485,7 +486,7 @@ func TestNodeVisitor(t *testing.T) {
data := generateRandNamespacedRawData(numLeaves, nidSize, leafSize)
n := New(sha256.New(), NamespaceIDSize(nidSize), NodeVisitor(collectNodeHashes))
for j := 0; j < numLeaves; j++ {
if err := n.Push(data[j][:nidSize], data[j][nidSize:]); err != nil {
if err := n.Push(data[j][:nidSize]); err != nil {
t.Errorf("err: %v", err)
}
}
Expand Down Expand Up @@ -522,7 +523,7 @@ func TestNamespacedMerkleTree_ProveErrors(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
n := New(sha256.New(), NamespaceIDSize(tt.nidLen), InitialCapacity(len(tt.pushData)))
for _, d := range tt.pushData {
err := n.Push(d.ID, d.Data)
err := n.Push(namespace.PrefixedData(append(d.ID, d.Data...)))
if err != nil {
t.Fatalf("invalid test case: %v, error on Push(): %v", tt.name, err)
}
Expand Down Expand Up @@ -603,7 +604,7 @@ func BenchmarkComputeRoot(b *testing.B) {
for i := 0; i < b.N; i++ {
n := New(sha256.New())
for j := 0; j < tt.numLeaves; j++ {
if err := n.Push(data[j][:tt.nidSize], data[j][tt.nidSize:]); err != nil {
if err := n.Push(data[j][:tt.nidSize]); err != nil {
b.Errorf("err: %v", err)
}
}
Expand Down
2 changes: 1 addition & 1 deletion proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestProof_VerifyNamespace_False(t *testing.T) {
generateLeafData(testNidLen, 0, 9, []byte("data"))...,
), newNamespaceDataPair([]byte{0, 0, 8}, []byte("last leaf")))
for _, d := range data {
err := n.Push(d.ID, d.Data)
err := n.Push(namespace.PrefixedData(append(d.ID, d.Data...)))
if err != nil {
t.Fatalf("invalid test setup: error on Push(): %v", err)
}
Expand Down

0 comments on commit a98611c

Please sign in to comment.