import { SparseMerkleTreeImpl , newInMemoryDB } from '@pigi/core-db' ; import { hexStrToBuf , bufToHexString , BigNumber } from '@pigi/core-utils' ; import { RollupBlock , RollupTransition } from '../types' ; import { abiEncodeTransition } from '../common/serialization' ; export type ContractFraudProof = any [ ] ; interface TransitionInclusionProof { blockNumber : number ; transitionIndex : number ; path : string ; siblings : string [ ] ; } interface IncludedTransition { transition : string ; inclusionProof : TransitionInclusionProof ; } export class DefaultRollupBlock implements RollupBlock { public transitions : RollupTransition [ ] ; public encodedTransitions : string [ ] ; public blockNumber : number ; public tree : SparseMerkleTreeImpl ; constructor ( transitions , blockNumber ) { this . transitions = transitions ; this . encodedTransitions = transitions . map ( ( transition ) => abiEncodeTransition ( transition ) ) ; this . blockNumber = blockNumber ; } public async generateTree ( ) { const treeHeight = Math . ceil ( Math . log2 ( this . transitions . length ) ) + 1 ; this . tree = await SparseMerkleTreeImpl . create ( newInMemoryDB ( ) , undefined , treeHeight ) ; for ( let i = 0 ; i < this . encodedTransitions . length ; i ++ ) { await this . tree . update ( new BigNumber ( i , 10 ) , hexStrToBuf ( this . encodedTransitions [ i ] ) ) ; } } public async getIncludedTransition ( transitionIndex ) { const inclusionProof = await this . getInclusionProof ( transitionIndex ) ; return { transition : this . encodedTransitions [ transitionIndex ] , inclusionProof , } ; } public async getInclusionProof ( transitionIndex ) { const blockInclusion = await this . tree . getMerkleProof ( new BigNumber ( transitionIndex ) , hexStrToBuf ( this . encodedTransitions [ transitionIndex ] ) ) ; const path = bufToHexString ( blockInclusion . key . toBuffer ( 'B' , 32 ) ) ; const siblings = blockInclusion . siblings . map ( ( sibBuf ) => bufToHexString ( sibBuf ) ) ; return { blockNumber : this . blockNumber , transitionIndex , path , siblings , } ; } }