@@ -142,6 +142,47 @@ contract('PartialMerkleTree', async ([_, primary, nonPrimary]) => {
142
142
assert . equal ( web3 . toUtf8 ( await tree . get ( 'foo' ) ) , 'bar' )
143
143
} )
144
144
} )
145
+
146
+ describe ( 'getNonInclusionProof()' , async ( ) => {
147
+ let items = { key1 : 'value1' , key2 : 'value2' , key3 : 'value3' }
148
+ it ( 'should return proof data when the key does not exist' , async ( ) => {
149
+ for ( const key of Object . keys ( items ) ) {
150
+ await tree . insert ( key , items [ key ] , { from : primary } )
151
+ }
152
+ await tree . getNonInclusionProof ( 'key4' )
153
+ } )
154
+ it ( 'should not return data when the key does exist' , async ( ) => {
155
+ for ( const key of Object . keys ( items ) ) {
156
+ await tree . insert ( key , items [ key ] , { from : primary } )
157
+ }
158
+ try {
159
+ await tree . getNonInclusionProof ( 'key1' )
160
+ assert . fail ( 'Did not reverted' )
161
+ } catch ( e ) {
162
+ assert . ok ( 'Reverted successfully' )
163
+ }
164
+ } )
165
+ } )
166
+
167
+ describe ( 'verifyNonInclusionProof()' , async ( ) => {
168
+ it ( 'should be passed when we use correct proof data' , async ( ) => {
169
+ let items = { key1 : 'value1' , key2 : 'value2' , key3 : 'value3' }
170
+ for ( const key of Object . keys ( items ) ) {
171
+ await tree . insert ( key , items [ key ] , { from : primary } )
172
+ }
173
+ let rootHash = await tree . getRootHash ( )
174
+ let [ potentialSiblingLabel , potentialSiblingValue , branchMask , siblings ] = await tree . getNonInclusionProof ( 'key4' )
175
+ await tree . verifyNonInclusionProof ( rootHash , 'key4' , potentialSiblingLabel , potentialSiblingValue , branchMask , siblings )
176
+ for ( const key of Object . keys ( items ) ) {
177
+ try {
178
+ await tree . verifyNonInclusionProof ( rootHash , key , potentialSiblingLabel , potentialSiblingValue , branchMask , siblings )
179
+ assert . fail ( 'Did not reverted' )
180
+ } catch ( e ) {
181
+ assert . ok ( 'Reverted successfully' )
182
+ }
183
+ }
184
+ } )
185
+ } )
145
186
} )
146
187
147
188
context ( 'We can reenact merkle tree transformation by submitting only referred siblings instead of submitting all nodes' , async ( ) => {
@@ -166,29 +207,54 @@ contract('PartialMerkleTree', async ([_, primary, nonPrimary]) => {
166
207
siblingsForKey1 = proof [ 1 ]
167
208
} )
168
209
169
- it ( 'should start with same root hash by initialization' , async ( ) => {
210
+ it ( 'should start with same root hash by initialization' , async ( ) => {
170
211
//initilaze with the first root hash
171
212
await treeB . initialize ( firstPhaseOfTreeA )
172
213
assert . equal ( await treeB . getRootHash ( ) , firstPhaseOfTreeA )
173
214
} )
174
215
175
- it ( 'should not change root after committing branch data' , async ( ) => {
216
+ it ( 'should not change root after committing branch data' , async ( ) => {
176
217
// commit branch data
177
218
await treeB . commitBranch ( 'key1' , referredValueForKey1 , branchMaskForKey1 , siblingsForKey1 )
178
219
assert . equal ( await treeB . getRootHash ( ) , firstPhaseOfTreeA )
179
220
} )
180
221
181
- it ( 'should be able to return proof data' , async ( ) => {
222
+ it ( 'should be able to return proof data' , async ( ) => {
182
223
// commit branch data
183
224
await treeB . getProof ( 'key1' )
184
225
} )
185
226
227
+ let secondPhaseOfTreeA
228
+ let secondPhaseOfTreeB
186
229
it ( 'should have same root hash when we update key1' , async ( ) => {
187
230
await treeA . insert ( 'key1' , 'val4' )
188
231
await treeB . insert ( 'key1' , 'val4' )
189
- let secondPhaseOfTreeA = await treeA . getRootHash ( )
190
- let secondPhaseOfTreeB = await treeB . getRootHash ( )
232
+ secondPhaseOfTreeA = await treeA . getRootHash ( )
233
+ secondPhaseOfTreeB = await treeB . getRootHash ( )
191
234
assert . equal ( secondPhaseOfTreeA , secondPhaseOfTreeB )
192
235
} )
236
+
237
+ it ( 'should revert before the branch data of non inclusion is committed' , async ( ) => {
238
+ try {
239
+ await treeB . insert ( 'key4' , 'val4' )
240
+ assert . fail ( 'Did not reverted' )
241
+ } catch ( e ) {
242
+ assert . ok ( 'Reverted successfully' )
243
+ }
244
+ } )
245
+
246
+ let thirdPhaseOfTreeA
247
+ let thirdPhaseOfTreeB
248
+ it ( 'should be able to insert a non inclusion key-value pair after committting related branch data' , async ( ) => {
249
+ let [ potentialSiblingLabel , potentialSiblingValue , branchMask , siblings ] = await treeA . getNonInclusionProof ( 'key4' )
250
+ await treeB . commitBranchOfNonInclusion ( 'key4' , potentialSiblingLabel , potentialSiblingValue , branchMask , siblings )
251
+ assert . equal ( await treeB . getRootHash ( ) , secondPhaseOfTreeB )
252
+
253
+ await treeA . insert ( 'key4' , 'val4' )
254
+ await treeB . insert ( 'key4' , 'val4' )
255
+ thirdPhaseOfTreeA = await treeA . getRootHash ( )
256
+ thirdPhaseOfTreeB = await treeB . getRootHash ( )
257
+ assert . equal ( thirdPhaseOfTreeA , thirdPhaseOfTreeB )
258
+ } )
193
259
} )
194
260
} )
0 commit comments