Skip to content

Commit

Permalink
Migrate runBlockchain to typescript (#517)
Browse files Browse the repository at this point in the history
* Change runBlockchain filetype to ts

* Migrate runBlockchain to ts
  • Loading branch information
s1na authored May 24, 2019
1 parent d642354 commit 244b4ac
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
5 changes: 4 additions & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { default as runCode, RunCodeOpts, RunCodeCb } from './runCode'
import { default as runCall, RunCallOpts, RunCallCb } from './runCall'
import { default as runTx, RunTxOpts, RunTxCb } from './runTx'
import { default as runBlock, RunBlockOpts, RunBlockCb } from './runBlock'
import runBlockchain from './runBlockchain'
const promisify = require('util.promisify')
const AsyncEventEmitter = require('async-eventemitter')
const Blockchain = require('ethereumjs-blockchain')
Expand Down Expand Up @@ -69,8 +70,10 @@ export default class VM extends AsyncEventEmitter {
this.blockchain = opts.blockchain || new Blockchain({ common: this._common })

this.allowUnlimitedContractSize = opts.allowUnlimitedContractSize === undefined ? false : opts.allowUnlimitedContractSize
}

this.runBlockchain = require('./runBlockchain.js').bind(this)
runBlockchain (blockchain: any, cb: any): void {
runBlockchain.bind(this)(blockchain, cb)
}

runBlock (opts: RunBlockOpts, cb: RunBlockCb): void {
Expand Down
20 changes: 11 additions & 9 deletions lib/runBlockchain.js → lib/runBlockchain.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import VM from './index'
const async = require('async')

/**
Expand All @@ -6,31 +7,32 @@ const async = require('async')
* @param {Blockchain} blockchain A [blockchain](https://github.com/ethereum/ethereumjs-blockchain) that to process
* @param {Function} cb the callback function
*/
module.exports = function (blockchain, cb) {
var self = this
var headBlock, parentState
export default function runBlockchain (this: VM, blockchain: any, cb: any) {
const self = this
let headBlock: any
let parentState: Buffer

// parse arguments
if (typeof blockchain === 'function') {
cb = blockchain
blockchain = undefined
blockchain = this.blockchain
}

blockchain = blockchain || self.blockchain
blockchain = blockchain || this.blockchain

// setup blockchain iterator
blockchain.iterator('vm', processBlock, cb)
function processBlock (block, reorg, cb) {
function processBlock (block: any, reorg: boolean, cb: any) {
async.series([
getStartingState,
runBlock
], cb)

// determine starting state for block run
function getStartingState (cb) {
function getStartingState (cb: any) {
// if we are just starting or if a chain re-org has happened
if (!headBlock || reorg) {
blockchain.getBlock(block.header.parentHash, function (err, parentBlock) {
blockchain.getBlock(block.header.parentHash, function (err: any, parentBlock: any) {
parentState = parentBlock.header.stateRoot
// generate genesis state if we are at the genesis block
// we don't have the genesis state
Expand All @@ -47,7 +49,7 @@ module.exports = function (blockchain, cb) {
}

// run block, update head if valid
function runBlock (cb) {
function runBlock (cb: any) {
self.runBlock({
block: block,
root: parentState
Expand Down
2 changes: 1 addition & 1 deletion tests/api/runBlockchain.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const Blockchain = require('ethereumjs-blockchain')
const Block = require('ethereumjs-block')
const Common = require('ethereumjs-common').default
const util = require('ethereumjs-util')
const runBlockchain = require('../../dist/runBlockchain')
const runBlockchain = require('../../dist/runBlockchain').default
const { StateManager } = require('../../dist/state')
const { createGenesis } = require('./utils')

Expand Down

0 comments on commit 244b4ac

Please sign in to comment.