Skip to content

Commit 244b4ac

Browse files
authored
Migrate runBlockchain to typescript (ethereumjs#517)
* Change runBlockchain filetype to ts * Migrate runBlockchain to ts
1 parent d642354 commit 244b4ac

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

lib/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { default as runCode, RunCodeOpts, RunCodeCb } from './runCode'
66
import { default as runCall, RunCallOpts, RunCallCb } from './runCall'
77
import { default as runTx, RunTxOpts, RunTxCb } from './runTx'
88
import { default as runBlock, RunBlockOpts, RunBlockCb } from './runBlock'
9+
import runBlockchain from './runBlockchain'
910
const promisify = require('util.promisify')
1011
const AsyncEventEmitter = require('async-eventemitter')
1112
const Blockchain = require('ethereumjs-blockchain')
@@ -69,8 +70,10 @@ export default class VM extends AsyncEventEmitter {
6970
this.blockchain = opts.blockchain || new Blockchain({ common: this._common })
7071

7172
this.allowUnlimitedContractSize = opts.allowUnlimitedContractSize === undefined ? false : opts.allowUnlimitedContractSize
73+
}
7274

73-
this.runBlockchain = require('./runBlockchain.js').bind(this)
75+
runBlockchain (blockchain: any, cb: any): void {
76+
runBlockchain.bind(this)(blockchain, cb)
7477
}
7578

7679
runBlock (opts: RunBlockOpts, cb: RunBlockCb): void {

lib/runBlockchain.js renamed to lib/runBlockchain.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import VM from './index'
12
const async = require('async')
23

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

1315
// parse arguments
1416
if (typeof blockchain === 'function') {
1517
cb = blockchain
16-
blockchain = undefined
18+
blockchain = this.blockchain
1719
}
1820

19-
blockchain = blockchain || self.blockchain
21+
blockchain = blockchain || this.blockchain
2022

2123
// setup blockchain iterator
2224
blockchain.iterator('vm', processBlock, cb)
23-
function processBlock (block, reorg, cb) {
25+
function processBlock (block: any, reorg: boolean, cb: any) {
2426
async.series([
2527
getStartingState,
2628
runBlock
2729
], cb)
2830

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

4951
// run block, update head if valid
50-
function runBlock (cb) {
52+
function runBlock (cb: any) {
5153
self.runBlock({
5254
block: block,
5355
root: parentState

tests/api/runBlockchain.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const Blockchain = require('ethereumjs-blockchain')
55
const Block = require('ethereumjs-block')
66
const Common = require('ethereumjs-common').default
77
const util = require('ethereumjs-util')
8-
const runBlockchain = require('../../dist/runBlockchain')
8+
const runBlockchain = require('../../dist/runBlockchain').default
99
const { StateManager } = require('../../dist/state')
1010
const { createGenesis } = require('./utils')
1111

0 commit comments

Comments
 (0)