import * as Level from 'level' ; import { Contract , Wallet } from 'ethers' ; import { JsonRpcProvider } from 'ethers/providers' ; import { BaseDB , DB , EthereumEventProcessor } from '@pigi/core-db' ; import { getLogger } from '@pigi/core-utils' ; import { config } from 'dotenv' ; import { resolve } from 'path' ; config ( { path : resolve ( __dirname , `../../../../config/.env` ) } ) ; import * as RollupChain from '../../../build/contracts/RollupChain.json' ; import * as fs from 'fs' ; import { Address , RollupStateValidator , State } from '../../types' ; import { DefaultRollupStateMachine , getGenesisState } from '../../common' ; import { DefaultRollupStateValidator } from '../rollup-state-validator' ; import { RollupFraudGuard } from '../rollup-fraud-guard' ; const log = getLogger ( 'monitor-and-validate' ) ; const aggregatorAddress = process . env . AGGREGATOR_ADDRESS ; const rollupContractAddress = process . env . ROLLUP_CONTRACT_ADDRESS ; const mnemonic = process . env . VALIDATOR_MNEMONIC ; const jsonRpcUrl = process . env . JSON_RPC_URL ; const genesisStateFilePath = process . env . GENESIS_STATE_RELATIVE_FILE_PATH ; let genesisState ; if ( ! ! genesisStateFilePath ) { genesisState = JSON . parse ( fs . readFileSync ( resolve ( __dirname , genesisStateFilePath ) ) . toString ( 'utf-8' ) ) ; log . info ( `Loaded genesis state from ${ genesisStateFilePath }. ${ genesisState . length } balances loaded.` ) ; } else { log . info ( 'No genesis state provided!' ) ; } const waitForever = ( ) : Promise < void > => { return new Promise ( ( ) => { } ) ; } ; async function runValidator ( ) { const levelOptions = { keyEncoding : 'binary' , valueEncoding : 'binary' , } ; const validatorDB = new BaseDB ( ( await Level ( 'build/level/validator' , levelOptions ) ) as any ) ; log . debug ( `Connecting to contract [${ rollupContractAddress }] at [${ jsonRpcUrl }]` ) ; const contract = new Contract ( rollupContractAddress , RollupChain . interface as any , Wallet . fromMnemonic ( mnemonic ) . connect ( new JsonRpcProvider ( jsonRpcUrl ) ) ) ; log . debug ( `Connected` ) ; const rollupStateMachine = ( await DefaultRollupStateMachine . create ( getGenesisState ( aggregatorAddress , genesisState ) , validatorDB , aggregatorAddress ) ) as DefaultRollupStateMachine ; const validator = new DefaultRollupStateValidator ( rollupStateMachine ) ; const fraudGuard = await RollupFraudGuard . create ( validatorDB , validator , contract ) ; const blockProcessorDB = new BaseDB ( ( await Level ( 'build/level/validator-blockProcessor' , levelOptions ) ) as any , 256 ) ; const processor = new EthereumEventProcessor ( blockProcessorDB ) ; await processor . subscribe ( contract , 'NewRollupBlock' , fraudGuard , true ) ; log . info ( `Started. Waiting forever.` ) ; await waitForever ( ) ; } runValidator ( ) ;