@@ -142,6 +142,71 @@ contract('PartialMerkleTree', async ([_, primary, nonPrimary]) => {
142142 assert . equal ( web3 . toUtf8 ( await tree . get ( 'foo' ) ) , 'bar' )
143143 } )
144144 } )
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+ } )
145210 } )
146211
147212 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]) => {
166231 siblingsForKey1 = proof [ 1 ]
167232 } )
168233
169- it ( 'should start with same root hash by initialization' , async ( ) => {
234+ it ( 'should start with same root hash by initialization' , async ( ) => {
170235 //initilaze with the first root hash
171236 await treeB . initialize ( firstPhaseOfTreeA )
172237 assert . equal ( await treeB . getRootHash ( ) , firstPhaseOfTreeA )
173238 } )
174239
175- it ( 'should not change root after committing branch data' , async ( ) => {
240+ it ( 'should not change root after committing branch data' , async ( ) => {
176241 // commit branch data
177242 await treeB . commitBranch ( 'key1' , referredValueForKey1 , branchMaskForKey1 , siblingsForKey1 )
178243 assert . equal ( await treeB . getRootHash ( ) , firstPhaseOfTreeA )
179244 } )
180245
181- it ( 'should be able to return proof data' , async ( ) => {
246+ it ( 'should be able to return proof data' , async ( ) => {
182247 // commit branch data
183248 await treeB . getProof ( 'key1' )
184249 } )
185250
251+ let secondPhaseOfTreeA
252+ let secondPhaseOfTreeB
186253 it ( 'should have same root hash when we update key1' , async ( ) => {
187254 await treeA . insert ( 'key1' , 'val4' )
188255 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 ( )
191258 assert . equal ( secondPhaseOfTreeA , secondPhaseOfTreeB )
192259 } )
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+ } )
193283 } )
194284} )
0 commit comments