@@ -142,6 +142,71 @@ contract('PartialMerkleTree', async ([_, primary, nonPrimary]) => {
142
142
assert . equal ( web3 . toUtf8 ( await tree . get ( 'foo' ) ) , 'bar' )
143
143
} )
144
144
} )
145
+
146
+ describe ( 'safeGet()' , async ( ) => {
147
+ it ( 'should return stored value for the given key' , async ( ) => {
148
+ await tree . insert ( 'foo' , 'bar' , { from : primary } )
149
+ assert . equal ( web3 . toUtf8 ( await tree . get ( 'foo' ) ) , 'bar' )
150
+ } )
151
+ it ( 'should throw if the given key is not included' , async ( ) => {
152
+ await tree . insert ( 'foo' , 'bar' , { from : primary } )
153
+ try {
154
+ await tree . get ( 'fuz' )
155
+ assert . fail ( 'Did not reverted' )
156
+ } catch ( e ) {
157
+ assert . ok ( 'Reverted successfully' )
158
+ }
159
+ } )
160
+ } )
161
+
162
+ describe ( 'doesInclude()' , async ( ) => {
163
+ it ( 'should return boolean whether the tree includes the given key or not' , async ( ) => {
164
+ await tree . insert ( 'foo' , 'bar' , { from : primary } )
165
+ assert . equal ( await tree . doesInclude ( 'foo' ) , true )
166
+ assert . equal ( await tree . doesInclude ( 'fuz' ) , false )
167
+ } )
168
+ } )
169
+
170
+ describe ( 'getNonInclusionProof()' , async ( ) => {
171
+ let items = { key1 : 'value1' , key2 : 'value2' , key3 : 'value3' }
172
+ it ( 'should return proof data when the key does not exist' , async ( ) => {
173
+ for ( const key of Object . keys ( items ) ) {
174
+ await tree . insert ( key , items [ key ] , { from : primary } )
175
+ }
176
+ await tree . getNonInclusionProof ( 'key4' )
177
+ } )
178
+ it ( 'should not return data when the key does exist' , async ( ) => {
179
+ for ( const key of Object . keys ( items ) ) {
180
+ await tree . insert ( key , items [ key ] , { from : primary } )
181
+ }
182
+ try {
183
+ await tree . getNonInclusionProof ( 'key1' )
184
+ assert . fail ( 'Did not reverted' )
185
+ } catch ( e ) {
186
+ assert . ok ( 'Reverted successfully' )
187
+ }
188
+ } )
189
+ } )
190
+
191
+ describe ( 'verifyNonInclusionProof()' , async ( ) => {
192
+ it ( 'should be passed when we use correct proof data' , async ( ) => {
193
+ let items = { key1 : 'value1' , key2 : 'value2' , key3 : 'value3' }
194
+ for ( const key of Object . keys ( items ) ) {
195
+ await tree . insert ( key , items [ key ] , { from : primary } )
196
+ }
197
+ let rootHash = await tree . getRootHash ( )
198
+ let [ potentialSiblingLabel , potentialSiblingValue , branchMask , siblings ] = await tree . getNonInclusionProof ( 'key4' )
199
+ await tree . verifyNonInclusionProof ( rootHash , 'key4' , potentialSiblingLabel , potentialSiblingValue , branchMask , siblings )
200
+ for ( const key of Object . keys ( items ) ) {
201
+ try {
202
+ await tree . verifyNonInclusionProof ( rootHash , key , potentialSiblingLabel , potentialSiblingValue , branchMask , siblings )
203
+ assert . fail ( 'Did not reverted' )
204
+ } catch ( e ) {
205
+ assert . ok ( 'Reverted successfully' )
206
+ }
207
+ }
208
+ } )
209
+ } )
145
210
} )
146
211
147
212
context ( 'We can reenact merkle tree transformation by submitting only referred siblings instead of submitting all nodes' , async ( ) => {
@@ -166,29 +231,54 @@ contract('PartialMerkleTree', async ([_, primary, nonPrimary]) => {
166
231
siblingsForKey1 = proof [ 1 ]
167
232
} )
168
233
169
- it ( 'should start with same root hash by initialization' , async ( ) => {
234
+ it ( 'should start with same root hash by initialization' , async ( ) => {
170
235
//initilaze with the first root hash
171
236
await treeB . initialize ( firstPhaseOfTreeA )
172
237
assert . equal ( await treeB . getRootHash ( ) , firstPhaseOfTreeA )
173
238
} )
174
239
175
- it ( 'should not change root after committing branch data' , async ( ) => {
240
+ it ( 'should not change root after committing branch data' , async ( ) => {
176
241
// commit branch data
177
242
await treeB . commitBranch ( 'key1' , referredValueForKey1 , branchMaskForKey1 , siblingsForKey1 )
178
243
assert . equal ( await treeB . getRootHash ( ) , firstPhaseOfTreeA )
179
244
} )
180
245
181
- it ( 'should be able to return proof data' , async ( ) => {
246
+ it ( 'should be able to return proof data' , async ( ) => {
182
247
// commit branch data
183
248
await treeB . getProof ( 'key1' )
184
249
} )
185
250
251
+ let secondPhaseOfTreeA
252
+ let secondPhaseOfTreeB
186
253
it ( 'should have same root hash when we update key1' , async ( ) => {
187
254
await treeA . insert ( 'key1' , 'val4' )
188
255
await treeB . insert ( 'key1' , 'val4' )
189
- let secondPhaseOfTreeA = await treeA . getRootHash ( )
190
- let secondPhaseOfTreeB = await treeB . getRootHash ( )
256
+ secondPhaseOfTreeA = await treeA . getRootHash ( )
257
+ secondPhaseOfTreeB = await treeB . getRootHash ( )
191
258
assert . equal ( secondPhaseOfTreeA , secondPhaseOfTreeB )
192
259
} )
260
+
261
+ it ( 'should revert before the branch data of non inclusion is committed' , async ( ) => {
262
+ try {
263
+ await treeB . insert ( 'key4' , 'val4' )
264
+ assert . fail ( 'Did not reverted' )
265
+ } catch ( e ) {
266
+ assert . ok ( 'Reverted successfully' )
267
+ }
268
+ } )
269
+
270
+ let thirdPhaseOfTreeA
271
+ let thirdPhaseOfTreeB
272
+ it ( 'should be able to insert a non inclusion key-value pair after committting related branch data' , async ( ) => {
273
+ let [ potentialSiblingLabel , potentialSiblingValue , branchMask , siblings ] = await treeA . getNonInclusionProof ( 'key4' )
274
+ await treeB . commitBranchOfNonInclusion ( 'key4' , potentialSiblingLabel , potentialSiblingValue , branchMask , siblings )
275
+ assert . equal ( await treeB . getRootHash ( ) , secondPhaseOfTreeB )
276
+
277
+ await treeA . insert ( 'key4' , 'val4' )
278
+ await treeB . insert ( 'key4' , 'val4' )
279
+ thirdPhaseOfTreeA = await treeA . getRootHash ( )
280
+ thirdPhaseOfTreeB = await treeB . getRootHash ( )
281
+ assert . equal ( thirdPhaseOfTreeA , thirdPhaseOfTreeB )
282
+ } )
193
283
} )
194
284
} )
0 commit comments