import { AndDecider , Decider , Decision , ImplicationProofItem , MerkleInclusionProofDecider , SignedByDBInterface , } from '@pigi/ovm' ; import { BigNumber , getLogger , hexStrToBuf } from '@pigi/core-utils' ; import { abiEncodeState , abiEncodeStateReceipt , Address , RollupStateSolver , SignedStateReceipt , StateReceipt , } from '../index' ; const log = getLogger ( 'rollup-ovm' ) ; /**
 * The State Solver. Stores state, evaluates state validity, and sends fraud proofs to L1 as necessary.
 */ export class DefaultRollupStateSolver implements RollupStateSolver { constructor ( private readonly signedByDB , private readonly signedByDecider , private readonly merkleInclusionDecider = new MerkleInclusionProofDecider ( ) ) { } param signedReceipt public async storeSignedStateReceipt ( signedReceipt ) { await this . signedByDB . storeSignedMessage ( abiEncodeStateReceipt ( signedReceipt . stateReceipt ) , signedReceipt . signature ) ; } param stateReceipt param signer public async isStateReceiptProvablyValid ( stateReceipt , signer ) { return ( await this . decideIfStateReceiptIsValid ( stateReceipt , signer ) ) . outcome ; } param stateReceipt param signer returns public async getFraudProof ( stateReceipt , signer ) { const decision = await this . decideIfStateReceiptIsValid ( stateReceipt , signer ) ; return decision . outcome ? decision . justification : undefined ; } private async decideIfStateReceiptIsValid ( stateReceipt , aggregatorAddress ) { const input = { properties : [ { decider : this . signedByDecider , input : { publicKey : aggregatorAddress , serializedMessage : abiEncodeStateReceipt ( stateReceipt ) , } , } , { decider : this . merkleInclusionDecider , input : { merkleProof : { rootHash : hexStrToBuf ( stateReceipt . stateRoot ) , key : new BigNumber ( stateReceipt . slotIndex ) , value : Buffer . from ( abiEncodeState ( stateReceipt . state ) ) , siblings : stateReceipt . inclusionProof . map ( ( x ) => Buffer . from ( x , 'hex' ) ) , } , } , } , ] , } ; log . debug ( `Deciding if state is valid ${ JSON . stringify ( stateReceipt ) }, input: ${ JSON . stringify ( input ) }` ) ; const decision = await AndDecider . instance ( ) . decide ( input ) ; log . debug ( `State is ${ decision . outcome ? 'valid' : 'invalid' }: ${ JSON . stringify ( stateReceipt ) }` ) ; return decision ; } }