import _ from 'lodash' ; import { SignedState , StateWithHash , toNitroState , unreachable } from '@statechannels/wallet-core' ; import { requireValidProtocolTransition , Status } from '@statechannels/nitro-protocol' ; import { Logger } from 'pino' ; import { validateAppTransitionWithEVM } from '../evm-validator' ; import { Bytes } from '../type-aliases' ; import { Channel } from '../models/channel' ; export function shouldValidateTransition ( incomingState , channel ) { const { supported , isLedger , fundingStrategy } = channel ; if ( fundingStrategy === 'Fake' && incomingState . turnNum === 3 ) { return false ; } const alreadyHaveState = _ . some ( channel . sortedStates , [ 'stateHash' , incomingState . stateHash ] ) ; const isOldState = incomingState . turnNum < ( supported ?. turnNum || 0 ) ; const isInFundingStage = incomingState . turnNum < 2 * incomingState . participants . length ; return ! ! supported && ! isOldState && ! alreadyHaveState && ! isLedger && ! isInFundingStage ; } export function validateTransition ( fromState , toState , logger ? , bytecode ? , skipAppTransitionCheck = false ) { try { const status = requireValidProtocolTransition ( toNitroState ( fromState ) , toNitroState ( toState ) ) ; switch ( status ) { case Status . True : return true ; case Status . NeedToCheckApp : { if ( skipAppTransitionCheck ) return true ; if ( ! bytecode ) return false ; const { success , revertReason } = validateAppTransitionWithEVM ( toNitroState ( fromState ) , toNitroState ( toState ) , bytecode ) ; if ( ! success ) logger ?. error ( `EVM transition validation failed` , { result : revertReason } ) ; return success ; } default : return unreachable ( status ) ; } } catch ( err ) { logger ?. error ( err ) ; return false ; } }