diff --git a/index.js b/index.js deleted file mode 100644 index 07efc17880..0000000000 --- a/index.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = require('./lib/index.js') diff --git a/lib/index.js b/lib/index.ts similarity index 81% rename from lib/index.js rename to lib/index.ts index dc890fba63..1088593c3b 100644 --- a/lib/index.js +++ b/lib/index.ts @@ -1,13 +1,21 @@ -const Buffer = require('safe-buffer').Buffer +import BN = require('bn.js') +import { StateManager } from './state' +import Common from 'ethereumjs-common' +import Account from 'ethereumjs-account' const promisify = require('util.promisify') -const ethUtil = require('ethereumjs-util') -const { StateManager } = require('./state') -const Common = require('ethereumjs-common').default -const Blockchain = require('ethereumjs-blockchain') -const Account = require('ethereumjs-account').default const AsyncEventEmitter = require('async-eventemitter') +const Blockchain = require('ethereumjs-blockchain') const Trie = require('merkle-patricia-tree/secure.js') -const BN = ethUtil.BN + +export interface VMOpts { + chain?: string + hardfork?: string + stateManager?: StateManager + state?: any // TODO + blockchain?: any // TODO + activatePrecompiles?: boolean + allowUnlimitedContractSize?: boolean +} /** * VM Class, `new VM(opts)` creates a new VM object @@ -21,8 +29,14 @@ const BN = ethUtil.BN * @param {Boolean} opts.activatePrecompiles create entries in the state tree for the precompiled contracts * @param {Boolean} opts.allowUnlimitedContractSize allows unlimited contract sizes while debugging. By setting this to `true`, the check for contract size limit of 24KB (see [EIP-170](https://git.io/vxZkK)) is bypassed. (default: `false`; ONLY set to `true` during debugging) */ -module.exports = class VM extends AsyncEventEmitter { - constructor (opts = {}) { +export default class VM extends AsyncEventEmitter { + opts: VMOpts + _common: Common + stateManager: StateManager + blockchain: any + allowUnlimitedContractSize: boolean + + constructor (opts: VMOpts = {}) { super() this.opts = opts @@ -61,11 +75,11 @@ module.exports = class VM extends AsyncEventEmitter { this.runBlockchain = require('./runBlockchain.js').bind(this) } - copy () { + copy (): VM { return new VM({ stateManager: this.stateManager.copy(), blockchain: this.blockchain }) } - async _emit (topic, data) { + async _emit (topic: string, data: any) { return promisify(this.emit.bind(this))(topic, data) } } diff --git a/package.json b/package.json index 3fdb0025b5..80346e2869 100644 --- a/package.json +++ b/package.json @@ -56,6 +56,7 @@ "@ethereumjs/config-prettier": "^1.1.1", "@types/bn.js": "^4.11.5", "@types/core-js": "^2.5.0", + "@types/lru-cache": "^5.1.0", "@types/node": "^11.13.4", "browserify": "^16.2.3", "coveralls": "^3.0.0", diff --git a/tests/BlockchainTestsRunner.js b/tests/BlockchainTestsRunner.js index 21bb0e1fd8..80734be9c1 100644 --- a/tests/BlockchainTestsRunner.js +++ b/tests/BlockchainTestsRunner.js @@ -28,9 +28,9 @@ module.exports = function runBlockchainTest (options, testData, t, cb) { } var VM if (options.dist) { - VM = require('../dist/index.js') + VM = require('../dist/index.js').default } else { - VM = require('../lib/index.js') + VM = require('../lib/index.js').default } var vm = new VM({ state: state, diff --git a/tests/GeneralStateTestsRunner.js b/tests/GeneralStateTestsRunner.js index 4ac4150adf..d6e1ff29c1 100644 --- a/tests/GeneralStateTestsRunner.js +++ b/tests/GeneralStateTestsRunner.js @@ -51,9 +51,9 @@ function runTestCase (options, testData, t, cb) { function (done) { var VM if (options.dist) { - VM = require('../dist/index.js') + VM = require('../dist/index.js').default } else { - VM = require('../lib/index.js') + VM = require('../lib/index').default } vm = new VM({ state: state, diff --git a/tests/api/index.js b/tests/api/index.js index 6da5142cea..9980f49355 100644 --- a/tests/api/index.js +++ b/tests/api/index.js @@ -3,7 +3,7 @@ const tape = require('tape') const util = require('ethereumjs-util') const Block = require('ethereumjs-block') const Trie = require('merkle-patricia-tree/secure') -const VM = require('../../dist/index') +const VM = require('../../dist/index').default const { setupVM } = require('./utils') const { setupPreConditions } = require('../util') const testData = require('./testdata.json') diff --git a/tests/api/runCode.js b/tests/api/runCode.js index 6efa895e6e..f8d9d2225e 100644 --- a/tests/api/runCode.js +++ b/tests/api/runCode.js @@ -1,7 +1,7 @@ const tape = require('tape') const async = require('async') const BN = require('bn.js') -const VM = require('../../dist/index') +const VM = require('../../dist/index').default const STOP = '00' const JUMP = '56' diff --git a/tests/api/runTx.js b/tests/api/runTx.js index 8ce01d3b95..7df9f91449 100644 --- a/tests/api/runTx.js +++ b/tests/api/runTx.js @@ -4,7 +4,7 @@ const Transaction = require('ethereumjs-tx') const ethUtil = require('ethereumjs-util') const runTx = require('../../dist/runTx') const { StateManager } = require('../../dist/state') -const VM = require('../../dist/index') +const VM = require('../../dist/index').default const { createAccount } = require('./utils') function setup (vm = null) { diff --git a/tests/api/utils.js b/tests/api/utils.js index 8010a510c0..0720989050 100644 --- a/tests/api/utils.js +++ b/tests/api/utils.js @@ -2,7 +2,7 @@ const Block = require('ethereumjs-block') const Account = require('ethereumjs-account').default const level = require('level-mem') const Blockchain = require('ethereumjs-blockchain') -const VM = require('../../dist/index') +const VM = require('../../dist/index').default function createGenesis (opts = {}) { opts.chain = opts.chain ? opts.chain : 'mainnet'