import '../../setup' ; import { HashAlgorithm , HashFunction , keccak256 , serializeObject , } from '@pigi/core-utils' ; import { DB , newInMemoryDB } from '@pigi/core-db' ; import * as assert from 'assert' ; import { CannotDecideError , HashPreimageExistenceDecider , Decision , HashPreimageDBInterface , ImplicationProofItem , HashPreimageDB , } from '../../../src' ; describe ( 'HashPreimageExistenceDecider' , ( ) => { const preimage = Buffer . from ( 'really great preimage' ) . toString ( 'hex' ) ; const hashFunction = keccak256 ; const hash = hashFunction ( preimage ) ; const hashAlgorithm = HashAlgorithm . KECCAK256 ; const notTheHashAlgorithm = HashAlgorithm . MD5 ; describe ( 'Constructor' , ( ) => { it ( 'should initialize' , async ( ) => { new HashPreimageExistenceDecider ( new HashPreimageDB ( newInMemoryDB ( ) ) , hashAlgorithm ) ; } ) ; } ) ; describe ( 'decide' , ( ) => { let decider ; let preimageDB ; let db ; beforeEach ( ( ) => { db = newInMemoryDB ( ) ; preimageDB = new HashPreimageDB ( db ) ; decider = new HashPreimageExistenceDecider ( preimageDB , hashAlgorithm ) ; } ) ; it ( 'should decide true for valid preimage' , async ( ) => { await preimageDB . storePreimage ( preimage , hashAlgorithm ) ; const decision = await decider . decide ( { hash } ) ; decision . outcome . should . equal ( true ) ; decision . justification . length . should . equal ( 1 ) ; const justification = decision . justification [ 0 ] ; justification . implication . decider . should . equal ( decider ) ; justification . implication . input [ 'hash' ] . should . equal ( hash ) ; assert ( justification . implicationWitness [ 'preimage' ] === preimage , `Justification preimage should equal expected preimage [${ preimage . toString ( ) }], but got [${ justification . implicationWitness [ 'preimage' ] . toString ( ) }]` ) ; } ) ; it ( 'should decide true for valid preimage from Message' , async ( ) => { await preimageDB . handleMessage ( serializeObject ( { channelID : 'chan' , data : { preimage } , } ) ) ; const decision = await decider . decide ( { hash } ) ; decision . outcome . should . equal ( true ) ; decision . justification . length . should . equal ( 1 ) ; const justification = decision . justification [ 0 ] ; justification . implication . decider . should . equal ( decider ) ; justification . implication . input [ 'hash' ] . should . equal ( hash ) ; assert ( justification . implicationWitness [ 'preimage' ] === preimage , `Justification preimage should equal expected preimage [${ preimage . toString ( ) }], but got [${ justification . implicationWitness [ 'preimage' ] . toString ( ) }]` ) ; } ) ; it ( 'should take hash algorithm into account' , async ( ) => { await preimageDB . storePreimage ( preimage , notTheHashAlgorithm ) ; try { await decider . decide ( { hash } ) ; assert ( false , 'This should have thrown' ) ; } catch ( e ) { if ( ! ( e instanceof CannotDecideError ) ) { throw e ; } } } ) ; it ( 'should throw when no preimages exist' , async ( ) => { try { await decider . decide ( { hash } ) ; assert ( false , 'This should have thrown' ) ; } catch ( e ) { if ( ! ( e instanceof CannotDecideError ) ) { throw e ; } } } ) ; it ( 'should throw invalid preimage' , async ( ) => { const wompwomp = Buffer . from ( 'womp womp' ) . toString ( 'hex' ) ; await preimageDB . storePreimage ( wompwomp , hashAlgorithm ) ; try { await decider . decide ( { hash } ) ; assert ( false , 'This should have thrown' ) ; } catch ( e ) { if ( ! ( e instanceof CannotDecideError ) ) { throw e ; } } } ) ; it ( 'should work with multiple Decisions that have been made' , async ( ) => { await preimageDB . storePreimage ( preimage , hashAlgorithm ) ; await decider . decide ( { hash } ) ; const secondPreimage = Buffer . from ( 'Another great preimage!' ) . toString ( 'hex' ) ; await preimageDB . storePreimage ( secondPreimage , hashAlgorithm ) ; const secondHash = hashFunction ( secondPreimage ) ; await decider . decide ( { hash : secondHash } ) ; const checkedDecision = await decider . decide ( { hash } ) ; checkedDecision . outcome . should . equal ( true ) ; checkedDecision . justification . length . should . equal ( 1 ) ; let justification = checkedDecision . justification [ 0 ] ; justification . implication . decider . should . equal ( decider ) ; assert ( justification . implication . input [ 'hash' ] === hash , 'decided hash is not what it should be' ) ; assert ( justification . implicationWitness [ 'preimage' ] === preimage , 'decided preimage is not what it should be' ) ; const secondCheckedDecision = await decider . decide ( { hash : secondHash , } ) ; secondCheckedDecision . outcome . should . equal ( true ) ; secondCheckedDecision . justification . length . should . equal ( 1 ) ; justification = secondCheckedDecision . justification [ 0 ] ; justification . implication . decider . should . equal ( decider ) ; assert ( justification . implication . input [ 'hash' ] === secondHash , 'second decided hash is not what it should be' ) ; assert ( justification . implicationWitness [ 'preimage' ] === secondPreimage , 'second decided preimage is not what it should be' ) ; } ) ; } ) ; } ) ;