import { getAddressFromPublicKey } from '@liskhq/lisk-cryptography' ; import { dataStructures } from '@liskhq/lisk-utils' ; import { Chain , Validator , CONSENSUS_STATE_VALIDATORS_KEY , validatorsSchema , StateStore , testing , } from '@liskhq/lisk-chain' ; import { codec } from '@liskhq/lisk-codec' ; import * as invalidBlockHeaderSpec from '../bft_specs/bft_invalid_block_headers.json' ; import { FinalityManager } from '../../src/finality_manager' ; import { convertHeader } from '../fixtures/blocks' ; describe ( 'FinalityManager' , ( ) => { const { StateStoreMock } = testing ; describe ( 'addBlockHeader' , ( ) => { let stateStore ; let chainStub ; beforeEach ( ( ) => { chainStub = ( { slots : { getSlotNumber : jest . fn ( ) , isWithinTimeslot : jest . fn ( ) , timeSinceGenesis : jest . fn ( ) , } , dataAccess : { getConsensusState : jest . fn ( ) , } , numberOfValidators : 103 , } as unknown ) as Chain ; stateStore = ( new StateStoreMock ( ) as unknown ) as StateStore ; } ) ; invalidBlockHeaderSpec . testCases . forEach ( testCase => { it ( 'should fail adding invalid block header' , async ( ) => { const validatorsMap = new dataStructures . BufferMap < Validator > ( ) ; for ( const blockHeader of testCase . config . blockHeaders ) { const addr = getAddressFromPublicKey ( Buffer . from ( blockHeader . generatorPublicKey , 'hex' ) ) ; validatorsMap . set ( addr , { address : addr , isConsensusParticipant : true , minActiveHeight : blockHeader . delegateMinHeightActive , } ) ; } await stateStore . consensus . set ( CONSENSUS_STATE_VALIDATORS_KEY , codec . encode ( validatorsSchema , { validators : validatorsMap . values ( ) } ) ) ; ( stateStore . chain as any ) . lastBlockHeaders = testCase . config . blockHeaders . map ( bh => convertHeader ( bh ) ) ; const finalityManager = new FinalityManager ( { chain : chainStub , genesisHeight : 0 , finalizedHeight : invalidBlockHeaderSpec . config . finalizedHeight , threshold : invalidBlockHeaderSpec . config . activeDelegates , } ) ; await expect ( finalityManager . addBlockHeader ( testCase . input as any , stateStore ) ) . rejects . toThrow ( ) ; } ) ; } ) ; } ) ; } ) ;