import '../../setup' ; import { BigNumber , TWO , ZERO } from '@pigi/core-utils' ; import { MerkleTree , MerkleTreeInclusionProof , MerkleUpdate , } from '@pigi/core-db' ; import * as assert from 'assert' ; import { MerkleInclusionQuantifier , QuantifierResult } from '../../../src' ; class MockedMerkleTree implements MerkleTree { private rootHash : Buffer ; private readonly leaves : { } = { } ; public setRootHash ( rootHash ) { this . rootHash = rootHash ; } public setLeaf ( key , leaf ) { this . leaves [ key . toString ( ) ] = leaf ; } public async getLeaf ( leafKey , rootHash ? ) { if ( ! ! rootHash && ( ! this . rootHash || ! this . rootHash . equals ( rootHash ) ) ) { return undefined ; } return leafKey . toString ( ) in this . leaves ? this . leaves [ leafKey . toString ( ) ] : undefined ; } public async getRootHash ( ) { return this . rootHash ; } public async update ( key , value ) { return undefined ; } public async getMerkleProof ( leafKey , leafValue ) { return undefined ; } public async batchUpdate ( updates ) { return undefined ; } public getHeight ( ) { return undefined ; } } describe ( 'MerkleTreeQuantifier' , ( ) => { describe ( 'getAllQuantified' , ( ) => { const key = ZERO ; let merkleTree ; beforeEach ( ( ) => { merkleTree = new MockedMerkleTree ( ) ; } ) ; it ( 'returns undefined on empty tree' , async ( ) => { const quantifier = new MerkleInclusionQuantifier ( merkleTree ) ; const result = await quantifier . getAllQuantified ( { key , root : Buffer . from ( 'some root' ) , } ) ; assert ( result . results . length === 1 , 'There should only be one result that is undefined.' ) ; assert ( result . results [ 0 ] === undefined , 'There should only be one result that is undefined.' ) ; assert ( ! result . allResultsQuantified , 'All results should not be quantified if we are unable to find a leaf node in the tree.' ) ; } ) ; it ( 'returns undefined on populated tree with a root that does not match' , async ( ) => { const quantifier = new MerkleInclusionQuantifier ( merkleTree ) ; merkleTree . setLeaf ( key , Buffer . from ( 'VALUE' ) ) ; const result = await quantifier . getAllQuantified ( { key , root : Buffer . from ( 'some root' ) , } ) ; assert ( result . results . length === 1 , 'There should only be one result that is undefined.' ) ; assert ( result . results [ 0 ] === undefined , 'There should only be one result that is undefined.' ) ; assert ( ! result . allResultsQuantified , 'All results should not be quantified if we are unable to find a leaf node in the tree.' ) ; } ) ; it ( 'returns undefined on empty tree with a root that matches' , async ( ) => { const quantifier = new MerkleInclusionQuantifier ( merkleTree ) ; const root = Buffer . from ( 'some root' ) ; merkleTree . setRootHash ( root ) ; const result = await quantifier . getAllQuantified ( { key , root , } ) ; assert ( result . results . length === 1 , 'There should only be one result that is undefined.' ) ; assert ( result . results [ 0 ] === undefined , 'There should only be one result that is undefined.' ) ; assert ( ! result . allResultsQuantified , 'All results should not be quantified if we are unable to find a leaf node in the tree.' ) ; } ) ; it ( 'returns value on populated tree tree with a root that matches' , async ( ) => { const quantifier = new MerkleInclusionQuantifier ( merkleTree ) ; const root = Buffer . from ( 'some root' ) ; const value = Buffer . from ( 'VALUE' ) ; merkleTree . setRootHash ( root ) ; merkleTree . setLeaf ( key , value ) ; const result = await quantifier . getAllQuantified ( { key , root , } ) ; assert ( result . results . length === 1 , 'There should only be one result that is the value.' ) ; assert ( result . results [ 0 ] . equals ( value ) , `Result value [${ result . results [ 0 ] . toString ( ) }] found when expecting [${ value . toString ( ) }]` ) ; assert ( result . allResultsQuantified , 'All results should be quantified if we are able to find the leaf node in the tree.' ) ; } ) ; it ( 'returns undefined on populated tree tree with a root that matches but mismatched key' , async ( ) => { const quantifier = new MerkleInclusionQuantifier ( merkleTree ) ; const root = Buffer . from ( 'some root' ) ; const value = Buffer . from ( 'VALUE' ) ; merkleTree . setRootHash ( root ) ; merkleTree . setLeaf ( key , value ) ; const result = await quantifier . getAllQuantified ( { key : TWO , root , } ) ; assert ( result . results . length === 1 , 'There should only be one result that is undefined.' ) ; assert ( result . results [ 0 ] === undefined , 'There should only be one result that is undefined.' ) ; assert ( ! result . allResultsQuantified , 'All results should not be quantified if we are unable to find a leaf node in the tree.' ) ; } ) ; } ) ; } ) ;