Skip to content

Commit

Permalink
Merge pull request #525 from ethereumjs/pass-hardfork-and-chain
Browse files Browse the repository at this point in the history
Accept a Common object when constructing the VM and pass it when copying it
  • Loading branch information
holgerd77 authored May 28, 2019
2 parents 176058b + f0a0ed0 commit 59c3133
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
21 changes: 17 additions & 4 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export interface VMOpts {
* 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
*/
allowUnlimitedContractSize?: boolean
common?: Common
}

/**
Expand All @@ -71,10 +72,21 @@ export default class VM extends AsyncEventEmitter {

this.opts = opts

const chain = opts.chain ? opts.chain : 'mainnet'
const hardfork = opts.hardfork ? opts.hardfork : 'petersburg'
const supportedHardforks = ['byzantium', 'constantinople', 'petersburg']
this._common = new Common(chain, hardfork, supportedHardforks)
if (opts.common) {
if (opts.chain || opts.hardfork) {
throw new Error(
'You can only instantiate the VM class with one of: opts.common, or opts.chain and opts.hardfork',
)
}

this._common = opts.common
} else {
const chain = opts.chain ? opts.chain : 'mainnet'
const hardfork = opts.hardfork ? opts.hardfork : 'petersburg'
const supportedHardforks = ['byzantium', 'constantinople', 'petersburg']

this._common = new Common(chain, hardfork, supportedHardforks)
}

if (opts.stateManager) {
this.stateManager = opts.stateManager
Expand Down Expand Up @@ -141,6 +153,7 @@ export default class VM extends AsyncEventEmitter {
return new VM({
stateManager: this.stateManager.copy(),
blockchain: this.blockchain,
common: this._common,
})
}

Expand Down
32 changes: 32 additions & 0 deletions tests/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const promisify = require('util.promisify')
const tape = require('tape')
const util = require('ethereumjs-util')
const Block = require('ethereumjs-block')
const Common = require('ethereumjs-common').default
const Trie = require('merkle-patricia-tree/secure')
const VM = require('../../dist/index').default
const { setupVM } = require('./utils')
Expand Down Expand Up @@ -31,6 +32,25 @@ tape('VM with default blockchain', (t) => {
st.end()
})

t.test('should only accept common or chain and fork', (st) => {
const common = new Common('mainnet');

st.throws(() => new VM({ chain: 'a', common }))
st.throws(() => new VM({ hardfork: 'a', common }))
st.throws(() => new VM({ chain: 'a', hardfork: 'a', common }))

st.end()
})

t.test('should accept a common object as option', (st) => {
const common = new Common('mainnet')

const vm = new VM({ common })
st.equal(vm._common, common)

st.end()
})

t.test('should only accept valid chain and fork', (st) => {
let vm = new VM({ chain: 'ropsten', hardfork: 'byzantium' })
st.equal(vm.stateManager._common.param('gasPrices', 'ecAdd'), 500)
Expand Down Expand Up @@ -115,6 +135,18 @@ tape('VM with blockchain', (t) => {

st.end()
})

t.test('should pass the correct Common object when copying the VM', st => {
const vm = setupVM({ chain: 'goerli', hardfork: 'byzantium' })
st.equal(vm._common.chainName(), 'goerli')
st.equal(vm._common.hardfork(), 'byzantium')

const copiedVM = vm.copy()
st.equal(copiedVM._common.chainName(), 'goerli')
st.equal(copiedVM._common.hardfork(), 'byzantium')

st.end()
})
})

const runBlockchainP = (vm) => promisify(vm.runBlockchain.bind(vm))()
Expand Down

0 comments on commit 59c3133

Please sign in to comment.