import { common , ECPoint } from '@neo-one/client-common' ; import { func , Params , Source , TSQL } from '@neo-one/edge' ; import { Settings , VMProtocolSettingsIn } from '@neo-one/node-core' ; import path from 'path' ; import { InvalidByteError , InvalidIntError , InvalidUIntError } from './errors' ; import { DefaultMethods , DispatcherFunc } from './types' ; const directory = path . basename ( __dirname ) ; const APP_ROOT = directory === 'cjs' || directory === 'esm' ? path . resolve ( __dirname , '..' , '..' ) : path . resolve ( __dirname , '..' ) ; const CSHARP_APP_ROOT = process . env . EDGE_APP_ROOT ?? path . resolve ( APP_ROOT , 'lib' , 'bin' , 'Debug' , 'net5.0' ) ; export const constants = { APP_ROOT , CSHARP_APP_ROOT } ; type EdgeOptions = ( ( ) => void ) | string | Params | Source | TSQL ; export const createCSharpDispatchInvoke = < Methods extends DefaultMethods > ( options ) : DispatcherFunc < Methods > => { if ( process . env . EDGE_APP_ROOT === undefined ) { process . env . EDGE_APP_ROOT = CSHARP_APP_ROOT ; } if ( process . env . EDGE_USE_CORECLR === undefined ) { process . env . EDGE_USE_CORECLR = '1' ; } const invokeFunction = func ( options ) ; return ( input ) => invokeFunction ( input , true ) ; } ; export const blockchainSettingsToProtocolSettings = ( settingsIn ) : VMProtocolSettingsIn => ( { network : settingsIn . network , addressVersion : settingsIn . addressVersion , standbyCommittee : settingsIn . standbyCommittee . map ( ( ecpoint ) => common . ecPointToString ( ecpoint ) ) , committeeMembersCount : settingsIn . standbyCommittee . length , validatorsCount : settingsIn . validatorsCount , millisecondsPerBlock : settingsIn . millisecondsPerBlock , memoryPoolMaxTransactions : settingsIn . memoryPoolMaxTransactions , maxTraceableBlocks : settingsIn . maxTraceableBlocks , initialGasDistribution : settingsIn . initialGasDistribution . toString ( ) , maxTransactionsPerBlock : settingsIn . maxTransactionsPerBlock , nativeUpdateHistory : settingsIn . nativeUpdateHistory } ) ; const isInt = ( num ) => num === parseInt ( `${ num }` , 10 ) ; const numIsUint = ( num ) => num >= 0 && num < 2 ** 32 - 1 && isInt ( num ) ; const numIsInt = ( num ) => num >= - ( 2 ** 32 / 2 ) && num <= 2 ** 32 / 2 - 1 && isInt ( num ) ; const numIsByte = ( num ) => num >= 0 && num <= 2 ** 8 - 1 && isInt ( num ) ; export const validateProtocolSettings = ( settings ) => { const { network , addressVersion , committeeMembersCount , validatorsCount , millisecondsPerBlock , memoryPoolMaxTransactions , maxTraceableBlocks , maxTransactionsPerBlock , } = settings ; if ( network !== undefined && ! numIsUint ( network ) ) { throw new InvalidUIntError ( network ) ; } if ( addressVersion !== undefined && ! numIsByte ( addressVersion ) ) { throw new InvalidByteError ( addressVersion ) ; } if ( committeeMembersCount !== undefined && ! numIsInt ( committeeMembersCount ) ) { throw new InvalidIntError ( committeeMembersCount ) ; } if ( validatorsCount !== undefined && ! numIsInt ( validatorsCount ) ) { throw new InvalidIntError ( validatorsCount ) ; } if ( millisecondsPerBlock !== undefined && ! numIsUint ( millisecondsPerBlock ) ) { throw new InvalidUIntError ( millisecondsPerBlock ) ; } if ( memoryPoolMaxTransactions !== undefined && ! numIsInt ( memoryPoolMaxTransactions ) ) { throw new InvalidIntError ( memoryPoolMaxTransactions ) ; } if ( maxTraceableBlocks !== undefined && ! numIsUint ( maxTraceableBlocks ) ) { throw new InvalidUIntError ( maxTraceableBlocks ) ; } if ( maxTransactionsPerBlock !== undefined && ! numIsUint ( maxTransactionsPerBlock ) ) { throw new InvalidUIntError ( maxTransactionsPerBlock ) ; } return { ... settings , initialGasDistribution : settings . initialGasDistribution ?. toString ( ) } ; } ;