import { Chain } from '@liskhq/lisk-chain' ; import { BlockHeaderWithReceivedAt as BlockHeader } from './types' ; type Slots = Chain [ 'slots' ] ; export const forgingSlot = ( slots , block ) : number => slots . getSlotNumber ( block . timestamp ) ; export const isBlockReceivedWithinForgingSlot = ( slots , { timestamp , receivedAt } ) : boolean => slots . isWithinTimeslot ( slots . getSlotNumber ( timestamp ) , receivedAt ) ; export const isLastAppliedBlockReceivedWithinForgingSlot = ( slots , lastAppliedBlock ) : boolean => { if ( ! lastAppliedBlock . receivedAt ) { return true ; } return isBlockReceivedWithinForgingSlot ( slots , lastAppliedBlock ) ; } ; export const isValidBlock = ( lastBlock , currentBlock ) : boolean => lastBlock . height + 1 === currentBlock . height && lastBlock . id . equals ( currentBlock . previousBlockID ) ; export const isIdenticalBlock = ( lastBlock , currentBlock ) : boolean => lastBlock . id . equals ( currentBlock . id ) ; export const isDuplicateBlock = ( lastBlock , currentBlock ) : boolean => lastBlock . height === currentBlock . height && lastBlock . asset . maxHeightPrevoted === currentBlock . asset . maxHeightPrevoted && lastBlock . previousBlockID . equals ( currentBlock . previousBlockID ) ; export const isDoubleForging = ( lastBlock , currentBlock ) : boolean => isDuplicateBlock ( lastBlock , currentBlock ) && lastBlock . generatorPublicKey . equals ( currentBlock . generatorPublicKey ) ; export const isTieBreak = ( { slots , lastAppliedBlock , receivedBlock , } ) : boolean => isDuplicateBlock ( lastAppliedBlock , receivedBlock ) && forgingSlot ( slots , lastAppliedBlock ) < forgingSlot ( slots , receivedBlock ) && ! isLastAppliedBlockReceivedWithinForgingSlot ( slots , lastAppliedBlock ) && isBlockReceivedWithinForgingSlot ( slots , receivedBlock ) ; export const isDifferentChain = ( lastBlock , currentBlock ) : boolean => { const maxHeightPrevoted = lastBlock . asset . maxHeightPrevoted || 0 ; return ( maxHeightPrevoted < currentBlock . asset . maxHeightPrevoted || ( lastBlock . height < currentBlock . height && maxHeightPrevoted === currentBlock . asset . maxHeightPrevoted ) ) ; } ;