-
Notifications
You must be signed in to change notification settings - Fork 33
/
proofs_ics23_test.go
178 lines (162 loc) · 4.61 KB
/
proofs_ics23_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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package store
import (
"crypto/sha256"
"testing"
ics23 "github.com/cosmos/ics23/go"
"github.com/pokt-network/pocket/persistence/kvstore"
"github.com/pokt-network/smt"
"github.com/stretchr/testify/require"
)
// Proof generation cannot fail but verification can
func TestICS23Proofs_GenerateCommitmentProofs(t *testing.T) {
nodeStore := kvstore.NewMemKVStore()
tree := smt.NewSparseMerkleTree(nodeStore, sha256.New(), smt.WithValueHasher(nil))
require.NotNil(t, tree)
// Set a value in the store
err := tree.Update([]byte("foo"), []byte("bar"))
require.NoError(t, err)
err = tree.Update([]byte("bar"), []byte("foo"))
require.NoError(t, err)
err = tree.Update([]byte("testKey"), []byte("testValue"))
require.NoError(t, err)
err = tree.Update([]byte("testKey2"), []byte("testValue2"))
require.NoError(t, err)
testCases := []struct {
key []byte
value []byte
nonmembership bool
fails bool
expected error
}{
{
// Successfully generates a membership proof for a key stored
key: []byte("foo"),
value: []byte("bar"),
nonmembership: false,
fails: false,
expected: nil,
},
{
// Successfully generates a non-membership proof for a key not stored
key: []byte("baz"),
value: []byte("testValue2"), // unrelated leaf data
nonmembership: true,
fails: false,
expected: nil,
},
{
// Successfully generates a non-membership proof for an unset nil key
key: nil,
value: []byte("foo"), // unrelated leaf data
nonmembership: true,
fails: false,
expected: nil,
},
}
for _, tc := range testCases {
var proof *ics23.CommitmentProof
if tc.nonmembership {
proof, err = createNonMembershipProof(tree, tc.key)
} else {
proof, err = createMembershipProof(tree, tc.key, tc.value)
}
if tc.fails {
require.EqualError(t, err, tc.expected.Error())
require.Nil(t, proof)
return
}
require.NoError(t, err)
require.NotNil(t, proof)
if tc.nonmembership {
require.Equal(t, tc.value, proof.GetExclusion().GetActualValueHash())
require.NotNil(t, proof.GetExclusion().GetLeaf())
require.NotNil(t, proof.GetExclusion().GetPath())
} else {
require.Equal(t, tc.value, proof.GetExist().GetValue())
require.NotNil(t, proof.GetExist().GetLeaf())
require.NotNil(t, proof.GetExist().GetPath())
}
}
err = nodeStore.Stop()
require.NoError(t, err)
}
func TestICS23Proofs_VerifyCommitmentProofs(t *testing.T) {
nodeStore := kvstore.NewMemKVStore()
tree := smt.NewSparseMerkleTree(nodeStore, sha256.New(), smt.WithValueHasher(nil))
require.NotNil(t, tree)
// Set a value in the store
err := tree.Update([]byte("foo"), []byte("bar"))
require.NoError(t, err)
err = tree.Update([]byte("bar"), []byte("foo"))
require.NoError(t, err)
err = tree.Update([]byte("testKey"), []byte("testValue"))
require.NoError(t, err)
err = tree.Update([]byte("testKey2"), []byte("testValue2"))
require.NoError(t, err)
root := tree.Root()
require.NotNil(t, root)
testCases := []struct {
key []byte
value []byte
nonmembership bool
valid bool
}{
{
// Successfully verifies a membership proof for a key-value stored pair
key: []byte("foo"),
value: []byte("bar"),
nonmembership: false,
valid: true,
},
{
// Successfully verifies a non-membership proof for a key-value pair not stored
key: []byte("not stored"),
value: nil,
nonmembership: true,
valid: true,
},
{
// Fails to verify a membership proof for a key-value pair not stored
key: []byte("baz"),
value: []byte("bar"),
nonmembership: false,
valid: false,
},
{
// Fails to verify a non-membership proof for a key stored in the tree
key: []byte("foo"),
value: nil,
nonmembership: true,
valid: false,
},
}
proof := new(ics23.CommitmentProof)
for _, tc := range testCases {
var err error
if tc.nonmembership {
proof, err = createNonMembershipProof(tree, tc.key)
} else {
proof, err = createMembershipProof(tree, tc.key, tc.value)
}
require.NoError(t, err)
require.NotNil(t, proof)
if tc.nonmembership {
require.NotNil(t, proof.GetExclusion())
} else {
require.NotNil(t, proof.GetExist())
}
var valid bool
if tc.nonmembership {
valid = VerifyNonMembership(root, proof, tc.key)
} else {
valid = VerifyMembership(root, proof, tc.key, tc.value)
}
if tc.valid {
require.True(t, valid)
} else {
require.False(t, valid)
}
}
err = nodeStore.Stop()
require.NoError(t, err)
}