This repository has been archived by the owner on Feb 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
deepsubtree_test.go
159 lines (144 loc) · 5.26 KB
/
deepsubtree_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package smt
import (
"bytes"
"crypto/sha256"
"errors"
"testing"
)
func TestDeepSparseMerkleSubTreeBasic(t *testing.T) {
smt := NewSparseMerkleTree(NewSimpleMap(), NewSimpleMap(), sha256.New())
smt.Update([]byte("testKey1"), []byte("testValue1"))
smt.Update([]byte("testKey2"), []byte("testValue2"))
smt.Update([]byte("testKey3"), []byte("testValue3"))
smt.Update([]byte("testKey4"), []byte("testValue4"))
smt.Update([]byte("testKey6"), []byte("testValue6"))
originalRoot := make([]byte, len(smt.Root()))
copy(originalRoot, smt.Root())
proof1, _ := smt.ProveUpdatable([]byte("testKey1"))
proof2, _ := smt.ProveUpdatable([]byte("testKey2"))
proof5, _ := smt.ProveUpdatable([]byte("testKey5"))
dsmst := NewDeepSparseMerkleSubTree(NewSimpleMap(), NewSimpleMap(), sha256.New(), smt.Root())
err := dsmst.AddBranch(proof1, []byte("testKey1"), []byte("testValue1"))
if err != nil {
t.Errorf("returned error when adding branch to deep subtree: %v", err)
}
err = dsmst.AddBranch(proof2, []byte("testKey2"), []byte("testValue2"))
if err != nil {
t.Errorf("returned error when adding branch to deep subtree: %v", err)
}
err = dsmst.AddBranch(proof5, []byte("testKey5"), defaultValue)
if err != nil {
t.Errorf("returned error when adding branch to deep subtree: %v", err)
}
value, err := dsmst.Get([]byte("testKey1"))
if err != nil {
t.Errorf("returned error when getting value in deep subtree: %v", err)
}
if !bytes.Equal(value, []byte("testValue1")) {
t.Error("did not get correct value in deep subtree")
}
value, err = dsmst.GetDescend([]byte("testKey1"))
if err != nil {
t.Errorf("returned error when getting value in deep subtree: %v", err)
}
if !bytes.Equal(value, []byte("testValue1")) {
t.Error("did not get correct value in deep subtree")
}
value, err = dsmst.Get([]byte("testKey2"))
if err != nil {
t.Errorf("returned error when getting value in deep subtree: %v", err)
}
if !bytes.Equal(value, []byte("testValue2")) {
t.Error("did not get correct value in deep subtree")
}
value, err = dsmst.GetDescend([]byte("testKey2"))
if err != nil {
t.Errorf("returned error when getting value in deep subtree: %v", err)
}
if !bytes.Equal(value, []byte("testValue2")) {
t.Error("did not get correct value in deep subtree")
}
value, err = dsmst.Get([]byte("testKey5"))
if err != nil {
t.Errorf("returned error when getting value in deep subtree: %v", err)
}
if !bytes.Equal(value, defaultValue) {
t.Error("did not get correct value in deep subtree")
}
value, err = dsmst.GetDescend([]byte("testKey5"))
if err != nil {
t.Errorf("returned error when getting value in deep subtree: %v", err)
}
if !bytes.Equal(value, defaultValue) {
t.Error("did not get correct value in deep subtree")
}
_, err = dsmst.GetDescend([]byte("testKey6"))
if err == nil {
t.Error("did not error when getting non-added value in deep subtree")
}
_, err = dsmst.Update([]byte("testKey1"), []byte("testValue3"))
if err != nil {
t.Errorf("returned error when updating deep subtree: %v", err)
}
_, err = dsmst.Update([]byte("testKey2"), defaultValue)
if err != nil {
t.Errorf("returned error when updating deep subtree: %v", err)
}
_, err = dsmst.Update([]byte("testKey5"), []byte("testValue5"))
if err != nil {
t.Errorf("returned error when updating deep subtree: %v", err)
}
value, err = dsmst.Get([]byte("testKey1"))
if err != nil {
t.Error("returned error when getting value in deep subtree")
}
if !bytes.Equal(value, []byte("testValue3")) {
t.Error("did not get correct value in deep subtree")
}
value, err = dsmst.Get([]byte("testKey2"))
if err != nil {
t.Error("returned error when getting value in deep subtree")
}
if !bytes.Equal(value, defaultValue) {
t.Error("did not get correct value in deep subtree")
}
value, err = dsmst.Get([]byte("testKey5"))
if err != nil {
t.Error("returned error when getting value in deep subtree")
}
if !bytes.Equal(value, []byte("testValue5")) {
t.Error("did not get correct value in deep subtree")
}
_, err = smt.Update([]byte("testKey1"), []byte("testValue3"))
if err != nil {
t.Errorf("returned error when updating main tree: %v", err)
}
_, err = smt.Update([]byte("testKey2"), defaultValue)
if err != nil {
t.Errorf("returned error when updating main tree: %v", err)
}
_, err = smt.Update([]byte("testKey5"), []byte("testValue5"))
if err != nil {
t.Errorf("returned error when updating main tree: %v", err)
}
if !bytes.Equal(smt.Root(), dsmst.Root()) {
t.Error("roots of identical standard tree and subtree do not match")
}
if bytes.Equal(smt.Root(), originalRoot) {
t.Error("root stayed the same despite updates")
}
}
func TestDeepSparseMerkleSubTreeBadInput(t *testing.T) {
smt := NewSparseMerkleTree(NewSimpleMap(), NewSimpleMap(), sha256.New())
smt.Update([]byte("testKey1"), []byte("testValue1"))
smt.Update([]byte("testKey2"), []byte("testValue2"))
smt.Update([]byte("testKey3"), []byte("testValue3"))
smt.Update([]byte("testKey4"), []byte("testValue4"))
badProof, _ := smt.Prove([]byte("testKey1"))
badProof.SideNodes[0][0] = byte(0)
dsmst := NewDeepSparseMerkleSubTree(NewSimpleMap(), NewSimpleMap(), sha256.New(), smt.Root())
err := dsmst.AddBranch(badProof, []byte("testKey1"), []byte("testValue1"))
if !errors.Is(err, ErrBadProof) {
t.Error("did not return ErrBadProof for bad proof input")
}
}