import { ByteArray , Hash } from 'as-scale-codec' ; import { Crypto , InherentData , Log , ResponseCodes , SignatureTypes , TransactionSource , Utils } from 'subsembly-core' ; import { Dispatcher } from '../generated/dispatcher' ; import { Aura , Timestamp } from '../pallets' ; import { BlockNumber , BlockType , HeaderType , Inherent , RuntimeConfig , SignatureType , UncheckedExtrinsic } from '../runtime/runtime' ; import { System , SystemEvents , SystemStorageEntries } from './system' ; description export namespace Executive { description param header export function initializeBlock ( header ) { System . _initialize ( header ) ; } description param block export function initialChecks ( block ) { let header = < HeaderType > block . getHeader ( ) ; let n = < BlockNumber > header . getNumber ( ) ; const previousBlock = instantiate < BlockNumber > ( n . unwrap ( ) - 1 ) ; const parentHash = SystemStorageEntries . BlockHash ( ) . get ( previousBlock ) ; if ( n == instantiate < BlockNumber > ( 0 ) && parentHash != header . getParentHash ( ) ) { Log . error ( "Initial checks: Parent hash should be valid." ) ; throw new Error ( "Executive: Initial checks for block execution failed" ) ; } } description param header export function finalChecks ( header ) { System . _computeExtrinsicsRoot ( ) ; let newHeader = System . _finalize ( ) ; let storageRoot = newHeader . getStateRoot ( ) ; if ( header . getStateRoot ( ) != storageRoot ) { Log . error ( "Storage root must match that calculated" ) ; throw new Error ( "Executive: Final checks for block execution failed" ) ; } } description param block export function executeBlock ( block ) { Executive . initializeBlock ( block . getHeader ( ) ) ; Executive . initialChecks ( block ) ; Executive . executeExtrinsicsWithBookKeeping ( block . getExtrinsics ( ) ) ; Executive . finalChecks ( block . getHeader ( ) ) ; } description export function finalizeBlock ( ) { System . _noteFinishedExtrinsics ( ) ; System . _computeExtrinsicsRoot ( ) ; return System . _finalize ( ) ; } description param data export function createExtrinsics ( data ) { const timestamp = Timestamp . _createInherent ( data ) ; const aura = Aura . _createInherent ( data ) ; return System . ALL_MODULES . concat ( timestamp . toU8a ( ) ) . concat ( aura ) ; } description param ext export function applyExtrinsic ( ext ) { const encodedLen = ext . encodedLength ( ) ; const result = Executive . applyExtrinsicWithLen ( ext , encodedLen ) ; if ( Utils . areArraysEqual ( result , ResponseCodes . SUCCESS ) ) { System . _noteAppliedExtrinsic ( ext ) ; } else { System . _depositEvent ( SystemEvents . ExtrinsicFailure , [ ] ) ; } return result ; } description param ext param encodedLen param encoded export function applyExtrinsicWithLen ( ext , encodedLen ) { return Dispatcher . dispatch ( ext ) ; } description param extrinsics export function executeExtrinsicsWithBookKeeping ( extrinsics ) { for ( let i = 0 ; i < extrinsics . length ; i ++ ) { Executive . applyExtrinsic ( extrinsics [ i ] ) ; } System . _noteFinishedExtrinsics ( ) ; } description param source param utx export function validateTransaction ( utx ) { if ( utx . isSigned ( ) ) { const extSignature = utx . signature ; const from = extSignature . signer ; const signedExt = extSignature . signedExtension ; const nonce = SystemStorageEntries . Account ( ) . get ( from ) . nonce ; if ( nonce && nonce . unwrap ( ) > signedExt . nonce . unwrap ( ) ) { Log . error ( "Validation error: Nonce value is less than or equal to the latest nonce" ) ; return ResponseCodes . NONCE_TOO_LOW ; } const blockNumber = SystemStorageEntries . Number ( ) . get ( ) ; const blockHash = SystemStorageEntries . BlockHash ( ) . get ( instantiate < BlockNumber > ( blockNumber . unwrap ( ) - 2 ) ) ; const genesisHash = SystemStorageEntries . BlockHash ( ) . get ( instantiate < BlockNumber > ( 0 ) ) ; const specVersion = RuntimeConfig . runtimeVersion ( ) . specVersion ; const transactionVersion = RuntimeConfig . runtimeVersion ( ) . transactionVersion ; const payload = utx . createPayload ( blockHash , genesisHash , specVersion , transactionVersion ) ; if ( ! Crypto . verifySignature ( < SignatureType > extSignature . signature , payload , from , SignatureTypes . sr25519 ) ) { Log . error ( "Validation error: Invalid signature" ) ; return ResponseCodes . INVALID_SIGNATURE ; } return utx . validate ( TransactionSource . External ) . toU8a ( ) ; } return utx . validateUnsigned ( ) . toU8a ( ) ; } description export function onFinalize ( ) { Log . info ( "onInitialize() called" ) ; } }