Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate runBlockchain to typescript #517

Merged
merged 2 commits into from
May 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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