import '../../setup' ; import { BigNumber , objectsEqual , ONE } from '@pigi/core-utils' ; import { MerkleTreeInclusionProof , newInMemoryDB , SparseMerkleTreeImpl , } from '@pigi/core-db' ; import * as assert from 'assert' ; import { Decider , Decision , ImplicationProofItem , MerkleInclusionProofDecider , } from '../../../src' ; describe ( 'MerkleTreeInclusionProofDecider' , ( ) => { describe ( 'decide' , ( ) => { const decider = new MerkleInclusionProofDecider ( ) ; const leafValue = Buffer . from ( 'Leaf value' ) ; it ( 'should return true when inclusion proof is valid' , async ( ) => { const merkleTree = await SparseMerkleTreeImpl . create ( newInMemoryDB ( 256 ) ) ; assert ( await merkleTree . update ( ONE , leafValue ) , 'Merkle tree update should have succeeded' ) ; const merkleProof = await merkleTree . getMerkleProof ( ONE , leafValue ) ; const decision = await decider . decide ( { merkleProof } ) ; decision . outcome . should . equal ( true ) ; decision . justification . length . should . equal ( 1 ) ; const justification = decision . justification [ 0 ] ; justification . implication . decider . should . equal ( decider ) ; assert ( objectsEqual ( justification . implication . input [ 'merkleProof' ] , merkleProof ) ) ; } ) ; it ( 'should return true when inclusion proof is valid for 32-height tree' , async ( ) => { const merkleTree = await SparseMerkleTreeImpl . create ( newInMemoryDB ( 256 ) , undefined , 32 ) ; assert ( await merkleTree . update ( ONE , leafValue ) , 'Merkle tree update should have succeeded' ) ; const merkleProof = await merkleTree . getMerkleProof ( ONE , leafValue ) ; const decision = await decider . decide ( { merkleProof } ) ; decision . outcome . should . equal ( true ) ; decision . justification . length . should . equal ( 1 ) ; const justification = decision . justification [ 0 ] ; justification . implication . decider . should . equal ( decider ) ; assert ( objectsEqual ( justification . implication . input [ 'merkleProof' ] , merkleProof ) ) ; } ) ; it ( 'should return true when inclusion proof is valid for different key' , async ( ) => { const key = new BigNumber ( 10 ) ; const merkleTree = await SparseMerkleTreeImpl . create ( newInMemoryDB ( 256 ) ) ; assert ( await merkleTree . update ( key , leafValue ) , 'Merkle tree update should have succeeded' ) ; const merkleProof = await merkleTree . getMerkleProof ( key , leafValue ) ; const decision = await decider . decide ( { merkleProof } ) ; decision . outcome . should . equal ( true ) ; decision . justification . length . should . equal ( 1 ) ; const justification = decision . justification [ 0 ] ; justification . implication . decider . should . equal ( decider ) ; assert ( objectsEqual ( justification . implication . input [ 'merkleProof' ] , merkleProof ) ) ; } ) ; it ( 'should return false when inclusion proof is invalid' , async ( ) => { const key = new BigNumber ( 10 ) ; const merkleTree = await SparseMerkleTreeImpl . create ( newInMemoryDB ( 256 ) ) ; assert ( await merkleTree . update ( key , leafValue ) , 'Merkle tree update should have succeeded' ) ; const merkleProof = await merkleTree . getMerkleProof ( key , leafValue ) ; merkleProof . siblings [ 0 ] = Buffer . from ( 'not the right hash' ) ; const decision = await decider . decide ( { merkleProof } ) ; decision . outcome . should . equal ( false ) ; decision . justification . length . should . equal ( 1 ) ; const justification = decision . justification [ 0 ] ; justification . implication . decider . should . equal ( decider ) ; assert ( objectsEqual ( justification . implication . input [ 'merkleProof' ] , merkleProof ) ) ; } ) ; } ) ; } ) ;