import { BIG_ENDIAN } from '@pigi/core-utils' ; import { GenericMerkleIntervalTree , GenericMerkleIntervalTreeNode , MerkleIntervalInclusionProof , MerkleIntervalTreeNode , STATE_ID_LENGTH , } from '@pigi/core-db' ; import { AbiStateUpdate } from '../serialization' ; export class MerkleStateIntervalTree extends GenericMerkleIntervalTree { public generateLeafNode ( dataBlock ) { return MerkleStateIntervalTree . calculateStateUpdateLeaf ( dataBlock ) ; } public static calculateStateUpdateLeaf ( stateUpdate ) { const hash = GenericMerkleIntervalTree . hash ( Buffer . from ( stateUpdate . encoded ) ) ; const index = stateUpdate . range . start . toBuffer ( BIG_ENDIAN , STATE_ID_LENGTH ) ; return new GenericMerkleIntervalTreeNode ( hash , index ) ; } public static verifyExectedRoot ( stateUpdate , inclusionProof ) { const leafNode = MerkleStateIntervalTree . calculateStateUpdateLeaf ( stateUpdate ) ; const rootAndBound = GenericMerkleIntervalTree . getRootAndBounds ( leafNode , inclusionProof ) ; if ( stateUpdate . range . end . gt ( rootAndBound . upperBound ) ) { throw new Error ( 'State Update range.end exceeds the max for its inclusion proof.' ) ; } return rootAndBound . root ; } }