From 487b392ed50bd8020d7ca97be0b1f55df7922506 Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Mon, 13 May 2019 10:19:54 +0200 Subject: [PATCH 1/3] Migrate index to typescript --- lib/{index.js => index.ts} | 36 ++++++++++++++++++++++---------- package.json | 3 ++- tests/BlockchainTestsRunner.js | 6 +++--- tests/GeneralStateTestsRunner.js | 4 ++-- tests/api/index.js | 2 +- tests/api/runBlockchain.js | 2 +- tests/api/runCode.js | 2 +- tests/api/runTx.js | 2 +- tests/api/utils.js | 4 ++-- 9 files changed, 38 insertions(+), 23 deletions(-) rename lib/{index.js => index.ts} (79%) diff --git a/lib/index.js b/lib/index.ts similarity index 79% rename from lib/index.js rename to lib/index.ts index dc890fba63..1f12d75365 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' +import Blockchain from 'ethereumjs-blockchain' 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 Trie = require('merkle-patricia-tree/secure.js') -const BN = ethUtil.BN + +export interface VMOpts { + chain?: string + hardfork?: string + stateManager?: StateManager + state?: any // TODO + blockchain?: Blockchain + 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: Blockchain + 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..440077b152 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,7 @@ "core-js-pure": "^3.0.1", "ethereumjs-account": "^3.0.0", "ethereumjs-block": "~2.2.0", - "ethereumjs-blockchain": "^3.4.0", + "ethereumjs-blockchain": "^4.0.0", "ethereumjs-common": "^1.1.0", "ethereumjs-util": "^6.1.0", "fake-merkle-patricia-tree": "^1.0.1", @@ -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..12e397bb6f 100644 --- a/tests/BlockchainTestsRunner.js +++ b/tests/BlockchainTestsRunner.js @@ -3,7 +3,7 @@ const testUtil = require('./util.js') const ethUtil = require('ethereumjs-util') const Trie = require('merkle-patricia-tree/secure') const Block = require('ethereumjs-block') -const Blockchain = require('ethereumjs-blockchain') +const Blockchain = require('ethereumjs-blockchain').default const BlockHeader = require('ethereumjs-block/header.js') const level = require('level') const levelMem = require('level-mem') @@ -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/runBlockchain.js b/tests/api/runBlockchain.js index 8e74cbeb8d..aae20a75cf 100644 --- a/tests/api/runBlockchain.js +++ b/tests/api/runBlockchain.js @@ -1,7 +1,7 @@ const tape = require('tape') const level = require('level-mem') const promisify = require('util.promisify') -const Blockchain = require('ethereumjs-blockchain') +const Blockchain = require('ethereumjs-blockchain').default const Block = require('ethereumjs-block') const Common = require('ethereumjs-common').default const util = require('ethereumjs-util') 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..64717f2a19 100644 --- a/tests/api/utils.js +++ b/tests/api/utils.js @@ -1,8 +1,8 @@ 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 Blockchain = require('ethereumjs-blockchain').default +const VM = require('../../dist/index').default function createGenesis (opts = {}) { opts.chain = opts.chain ? opts.chain : 'mainnet' From 13ecdf0ed7bdf08619455e476c99af4881549e12 Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Mon, 13 May 2019 10:20:30 +0200 Subject: [PATCH 2/3] Remove root index file --- index.js | 1 - 1 file changed, 1 deletion(-) delete mode 100644 index.js 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') From f97b118da5269a4227075575afdc46a3c702e6ed Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Mon, 13 May 2019 12:24:24 +0200 Subject: [PATCH 3/3] Downgrade blockchain to v3.4.0 --- lib/index.ts | 6 +++--- package.json | 2 +- tests/BlockchainTestsRunner.js | 2 +- tests/api/runBlockchain.js | 2 +- tests/api/utils.js | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/index.ts b/lib/index.ts index 1f12d75365..1088593c3b 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -2,9 +2,9 @@ import BN = require('bn.js') import { StateManager } from './state' import Common from 'ethereumjs-common' import Account from 'ethereumjs-account' -import Blockchain from 'ethereumjs-blockchain' const promisify = require('util.promisify') const AsyncEventEmitter = require('async-eventemitter') +const Blockchain = require('ethereumjs-blockchain') const Trie = require('merkle-patricia-tree/secure.js') export interface VMOpts { @@ -12,7 +12,7 @@ export interface VMOpts { hardfork?: string stateManager?: StateManager state?: any // TODO - blockchain?: Blockchain + blockchain?: any // TODO activatePrecompiles?: boolean allowUnlimitedContractSize?: boolean } @@ -33,7 +33,7 @@ export default class VM extends AsyncEventEmitter { opts: VMOpts _common: Common stateManager: StateManager - blockchain: Blockchain + blockchain: any allowUnlimitedContractSize: boolean constructor (opts: VMOpts = {}) { diff --git a/package.json b/package.json index 440077b152..80346e2869 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,7 @@ "core-js-pure": "^3.0.1", "ethereumjs-account": "^3.0.0", "ethereumjs-block": "~2.2.0", - "ethereumjs-blockchain": "^4.0.0", + "ethereumjs-blockchain": "^3.4.0", "ethereumjs-common": "^1.1.0", "ethereumjs-util": "^6.1.0", "fake-merkle-patricia-tree": "^1.0.1", diff --git a/tests/BlockchainTestsRunner.js b/tests/BlockchainTestsRunner.js index 12e397bb6f..80734be9c1 100644 --- a/tests/BlockchainTestsRunner.js +++ b/tests/BlockchainTestsRunner.js @@ -3,7 +3,7 @@ const testUtil = require('./util.js') const ethUtil = require('ethereumjs-util') const Trie = require('merkle-patricia-tree/secure') const Block = require('ethereumjs-block') -const Blockchain = require('ethereumjs-blockchain').default +const Blockchain = require('ethereumjs-blockchain') const BlockHeader = require('ethereumjs-block/header.js') const level = require('level') const levelMem = require('level-mem') diff --git a/tests/api/runBlockchain.js b/tests/api/runBlockchain.js index aae20a75cf..8e74cbeb8d 100644 --- a/tests/api/runBlockchain.js +++ b/tests/api/runBlockchain.js @@ -1,7 +1,7 @@ const tape = require('tape') const level = require('level-mem') const promisify = require('util.promisify') -const Blockchain = require('ethereumjs-blockchain').default +const Blockchain = require('ethereumjs-blockchain') const Block = require('ethereumjs-block') const Common = require('ethereumjs-common').default const util = require('ethereumjs-util') diff --git a/tests/api/utils.js b/tests/api/utils.js index 64717f2a19..0720989050 100644 --- a/tests/api/utils.js +++ b/tests/api/utils.js @@ -1,7 +1,7 @@ const Block = require('ethereumjs-block') const Account = require('ethereumjs-account').default const level = require('level-mem') -const Blockchain = require('ethereumjs-blockchain').default +const Blockchain = require('ethereumjs-blockchain') const VM = require('../../dist/index').default function createGenesis (opts = {}) {