import '../../setup' ; import { HashAlgorithm , HashFunction , keccak256 } from '@pigi/core-utils' ; import { newInMemoryDB } from '@pigi/core-db' ; import * as assert from 'assert' ; import { CannotDecideError , ForAllSuchThatDecider , ForAllSuchThatInput , HashPreimageExistenceDecider , IntegerRangeQuantifier , HashPreimageDBInterface , HashPreimageDB , Decision , PropertyFactory , } from '../../../src' ; describe ( 'PreimageExistenceOnRangeOfHashes' , ( ) => { const forAllDecider = new ForAllSuchThatDecider ( ) ; const rangeQuantifier = new IntegerRangeQuantifier ( ) ; const hashAlgorithm = HashAlgorithm . KECCAK256 ; const hashFunction = keccak256 ; let hashDecider ; let preimageDB ; beforeEach ( ( ) => { preimageDB = new HashPreimageDB ( newInMemoryDB ( ) ) ; hashDecider = new HashPreimageExistenceDecider ( preimageDB , hashAlgorithm ) ; } ) ; const savePreimages = async ( numbers ) => { for ( const num of numbers ) { await preimageDB . storePreimage ( num . toString ( ) , hashAlgorithm ) ; } } ; describe ( 'decide' , ( ) => { it ( 'should decide true when preimages produce hashes' , async ( ) => { await savePreimages ( [ 2 , 3 , 4 , 5 , 6 , 7 , 8 ] ) ; const propertyFactory = ( num ) => { return { decider : hashDecider , input : { hash : hashFunction ( num . toString ( ) ) , } , } ; } ; const forAllInput = { quantifier : rangeQuantifier , quantifierParameters : { start : 2 , end : 8 } , propertyFactory , } ; const decision = await forAllDecider . decide ( forAllInput ) ; decision . outcome . should . eq ( true ) ; } ) ; it ( 'should return cannot decide when a single preimage does not produce the correct hash' , async ( ) => { await savePreimages ( [ 2 , 3 , 4 , 6 , 7 , 8 ] ) ; const propertyFactory = ( num ) => { return { decider : hashDecider , input : { hash : hashFunction ( num . toString ( ) ) , } , } ; } ; const forAllInput = { quantifier : rangeQuantifier , quantifierParameters : { start : 2 , end : 8 } , propertyFactory , } ; try { await forAllDecider . decide ( forAllInput , undefined ) ; assert ( false , 'Should have thrown.' ) ; } catch ( e ) { if ( ! ( e instanceof CannotDecideError ) ) { throw e ; } } } ) ; } ) ; } ) ;