Skip to content

Commit 59c3133

Browse files
authored
Merge pull request ethereumjs#525 from ethereumjs/pass-hardfork-and-chain
Accept a Common object when constructing the VM and pass it when copying it
2 parents 176058b + f0a0ed0 commit 59c3133

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

lib/index.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export interface VMOpts {
4545
* 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
4646
*/
4747
allowUnlimitedContractSize?: boolean
48+
common?: Common
4849
}
4950

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

7273
this.opts = opts
7374

74-
const chain = opts.chain ? opts.chain : 'mainnet'
75-
const hardfork = opts.hardfork ? opts.hardfork : 'petersburg'
76-
const supportedHardforks = ['byzantium', 'constantinople', 'petersburg']
77-
this._common = new Common(chain, hardfork, supportedHardforks)
75+
if (opts.common) {
76+
if (opts.chain || opts.hardfork) {
77+
throw new Error(
78+
'You can only instantiate the VM class with one of: opts.common, or opts.chain and opts.hardfork',
79+
)
80+
}
81+
82+
this._common = opts.common
83+
} else {
84+
const chain = opts.chain ? opts.chain : 'mainnet'
85+
const hardfork = opts.hardfork ? opts.hardfork : 'petersburg'
86+
const supportedHardforks = ['byzantium', 'constantinople', 'petersburg']
87+
88+
this._common = new Common(chain, hardfork, supportedHardforks)
89+
}
7890

7991
if (opts.stateManager) {
8092
this.stateManager = opts.stateManager
@@ -141,6 +153,7 @@ export default class VM extends AsyncEventEmitter {
141153
return new VM({
142154
stateManager: this.stateManager.copy(),
143155
blockchain: this.blockchain,
156+
common: this._common,
144157
})
145158
}
146159

tests/api/index.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const promisify = require('util.promisify')
22
const tape = require('tape')
33
const util = require('ethereumjs-util')
44
const Block = require('ethereumjs-block')
5+
const Common = require('ethereumjs-common').default
56
const Trie = require('merkle-patricia-tree/secure')
67
const VM = require('../../dist/index').default
78
const { setupVM } = require('./utils')
@@ -31,6 +32,25 @@ tape('VM with default blockchain', (t) => {
3132
st.end()
3233
})
3334

35+
t.test('should only accept common or chain and fork', (st) => {
36+
const common = new Common('mainnet');
37+
38+
st.throws(() => new VM({ chain: 'a', common }))
39+
st.throws(() => new VM({ hardfork: 'a', common }))
40+
st.throws(() => new VM({ chain: 'a', hardfork: 'a', common }))
41+
42+
st.end()
43+
})
44+
45+
t.test('should accept a common object as option', (st) => {
46+
const common = new Common('mainnet')
47+
48+
const vm = new VM({ common })
49+
st.equal(vm._common, common)
50+
51+
st.end()
52+
})
53+
3454
t.test('should only accept valid chain and fork', (st) => {
3555
let vm = new VM({ chain: 'ropsten', hardfork: 'byzantium' })
3656
st.equal(vm.stateManager._common.param('gasPrices', 'ecAdd'), 500)
@@ -115,6 +135,18 @@ tape('VM with blockchain', (t) => {
115135

116136
st.end()
117137
})
138+
139+
t.test('should pass the correct Common object when copying the VM', st => {
140+
const vm = setupVM({ chain: 'goerli', hardfork: 'byzantium' })
141+
st.equal(vm._common.chainName(), 'goerli')
142+
st.equal(vm._common.hardfork(), 'byzantium')
143+
144+
const copiedVM = vm.copy()
145+
st.equal(copiedVM._common.chainName(), 'goerli')
146+
st.equal(copiedVM._common.hardfork(), 'byzantium')
147+
148+
st.end()
149+
})
118150
})
119151

120152
const runBlockchainP = (vm) => promisify(vm.runBlockchain.bind(vm))()

0 commit comments

Comments
 (0)