Skip to content

Commit

Permalink
Migrate index to typescript (#507)
Browse files Browse the repository at this point in the history
* Migrate index to typescript

* Remove root index file

* Downgrade blockchain to v3.4.0
  • Loading branch information
s1na authored May 13, 2019
1 parent a53d432 commit 8c4b127
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 20 deletions.
1 change: 0 additions & 1 deletion index.js

This file was deleted.

36 changes: 25 additions & 11 deletions lib/index.js → lib/index.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions tests/BlockchainTestsRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions tests/GeneralStateTestsRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion tests/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion tests/api/runCode.js
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
2 changes: 1 addition & 1 deletion tests/api/runTx.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion tests/api/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 8c4b127

Please sign in to comment.