Skip to content

Commit e7bb009

Browse files
authored
Merge pull request ethereumjs#468 from ethereumjs/fix/browser-tests
[WIP] Fix browser test issues
2 parents 469c1aa + 847a8f3 commit e7bb009

File tree

7 files changed

+39
-38
lines changed

7 files changed

+39
-38
lines changed

karma.conf.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,8 @@ module.exports = function (config) {
1717
],
1818

1919
// list of files / patterns to exclude
20-
// currently failing tests, open separate PRs to fix
2120
exclude: [
22-
'./tests/api/state/stateManager.js', // 4, "# should clear the cache when the state root is set"
23-
'./tests/api/state/storageReader.js', // 1, "# should get value from stateManager"
24-
'./tests/api/index.js', // 11, "# should run blockchain with mocked runBlock" not working"
25-
'./tests/api/runBlock.js', // 3, "# should fail when runTx fails"
26-
'./tests/api/runBlockchain.js' // 2, "# should run with valid and invalid blocks"
21+
'./tests/api/state/stateManager.js' // 4, "# should clear the cache when the state root is set"
2722
],
2823

2924
// preprocess matching files before serving them to the browser
@@ -69,6 +64,10 @@ module.exports = function (config) {
6964

7065
// Concurrency level
7166
// how many browser should be started simultaneous
72-
concurrency: Infinity
67+
concurrency: Infinity,
68+
69+
// Fail after timeout
70+
browserDisconnectTimeout: 100000,
71+
browserNoActivityTimeout: 100000
7372
})
7473
}

tests/api/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ tape('VM with blockchain', (t) => {
6969
})
7070

7171
t.test('should run blockchain with mocked runBlock', async (st) => {
72-
const vm = setupVM()
72+
const vm = setupVM({ chain: 'goerli' })
7373
const genesis = new Block(Buffer.from(testData.genesisRLP.slice(2), 'hex'))
7474
const block = new Block(Buffer.from(testData.blocks[0].rlp.slice(2), 'hex'))
7575

@@ -96,7 +96,7 @@ tape('VM with blockchain', (t) => {
9696
})
9797

9898
t.test('should run blockchain with blocks', async (st) => {
99-
const vm = setupVM()
99+
const vm = setupVM({ chain: 'goerli' })
100100
const genesis = new Block(Buffer.from(testData.genesisRLP.slice(2), 'hex'))
101101
const block = new Block(Buffer.from(testData.blocks[0].rlp.slice(2), 'hex'))
102102

tests/api/runBlock.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ function setup (vm = null) {
3131
data: testData,
3232
p: {
3333
runBlock: promisify(runBlock.bind(vm)),
34-
putAccount: promisify(vm.stateManager.putAccount.bind(vm.stateManager)),
35-
generateCanonicalGenesis: promisify(vm.stateManager.generateCanonicalGenesis.bind(vm.stateManager))
34+
putAccount: promisify(vm.stateManager.putAccount.bind(vm.stateManager))
3635
}
3736
}
3837
}
@@ -59,8 +58,6 @@ tape('runBlock', async (t) => {
5958
t.test('should fail when runTx fails', async (st) => {
6059
const block = new Block(util.rlp.decode(suite.data.blocks[0].rlp))
6160

62-
await suite.p.generateCanonicalGenesis()
63-
6461
// The mocked VM uses a mocked runTx
6562
// which always returns an error.
6663
await suite.p.runBlock({ block, skipBlockValidation: true })
@@ -108,8 +105,6 @@ tape('should fail when tx gas limit higher than block gas limit', async (t) => {
108105
const block = new Block(util.rlp.decode(suite.data.blocks[0].rlp))
109106
block.transactions[0].gasLimit = Buffer.from('3fefba', 'hex')
110107

111-
await suite.p.generateCanonicalGenesis()
112-
113108
await suite.p.runBlock({ block, skipBlockValidation: true })
114109
.then(() => t.fail('should have returned error'))
115110
.catch((e) => t.ok(e.message.includes('higher gas limit')))
@@ -128,8 +123,6 @@ tape('should fail when runCall fails', async (t) => {
128123
await suite.p.putAccount(tx.from.toString('hex'), acc)
129124
}
130125

131-
await suite.p.generateCanonicalGenesis()
132-
133126
// The mocked VM uses a mocked runCall
134127
// which always returns an error.
135128
// runTx is a full implementation that works.

tests/api/runBlockchain.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
const tape = require('tape')
22
const level = require('level-mem')
3-
const { promisify } = require('util')
3+
const promisify = require('util.promisify')
44
const Blockchain = require('ethereumjs-blockchain')
55
const Block = require('ethereumjs-block')
6+
const Common = require('ethereumjs-common').default
67
const util = require('ethereumjs-util')
78
const runBlockchain = require('../../lib/runBlockchain')
89
const { StateManager } = require('../../lib/state')
910
const { createGenesis } = require('./utils')
1011

1112
tape('runBlockchain', (t) => {
1213
const blockchainDB = level()
13-
const blockchain = new Blockchain({ db: blockchainDB })
14-
const vm = { stateManager: new StateManager(), blockchain }
14+
const blockchain = new Blockchain({
15+
db: blockchainDB,
16+
chain: 'goerli',
17+
validate: false
18+
})
19+
const vm = {
20+
stateManager: new StateManager({ common: new Common('goerli') }),
21+
blockchain: blockchain
22+
}
1523

1624
const putGenesisP = promisify(blockchain.putGenesis.bind(blockchain))
1725
const putBlockP = promisify(blockchain.putBlock.bind(blockchain))
@@ -29,7 +37,7 @@ tape('runBlockchain', (t) => {
2937
})
3038

3139
t.test('should run with genesis block', async (st) => {
32-
const genesis = createGenesis()
40+
const genesis = createGenesis({ chain: 'goerli' })
3341

3442
await putGenesisP(genesis)
3543
st.ok(blockchain.meta.genesis, 'genesis should be set for blockchain')
@@ -49,12 +57,12 @@ tape('runBlockchain', (t) => {
4957
cb(null, {})
5058
}
5159

52-
const genesis = createGenesis()
60+
const genesis = createGenesis({ chain: 'goerli' })
5361
await putGenesisP(genesis)
5462

55-
const b1 = createBlock(genesis, 1)
56-
const b2 = createBlock(b1, 2)
57-
const b3 = createBlock(b2, 3)
63+
const b1 = createBlock(genesis, 1, { chain: 'goerli' })
64+
const b2 = createBlock(b1, 2, { chain: 'goerli' })
65+
const b3 = createBlock(b2, 3, { chain: 'goerli' })
5866

5967
blockchain.validate = false
6068

@@ -79,12 +87,13 @@ tape('runBlockchain', (t) => {
7987
})
8088
})
8189

82-
function createBlock (parent = null, n = 0) {
90+
function createBlock (parent = null, n = 0, opts = {}) {
91+
opts.chain = opts.chain ? opts.chain : 'mainnet'
8392
if (parent === null) {
84-
return createGenesis()
93+
return createGenesis(opts)
8594
}
8695

87-
const b = new Block()
96+
const b = new Block(null, opts)
8897
b.header.number = util.toBuffer(n)
8998
b.header.parentHash = parent.hash()
9099
b.header.difficulty = '0xfffffff'

tests/api/state/stateManager.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { promisify } = require('util')
1+
const promisify = require('util.promisify')
22
const tape = require('tape')
33
const util = require('ethereumjs-util')
44
const StateManager = require('../../../lib/state/stateManager')
@@ -9,13 +9,12 @@ tape('StateManager', (t) => {
99
const stateManager = new StateManager()
1010

1111
st.deepEqual(stateManager._trie.root, util.KECCAK256_RLP, 'it has default root')
12+
st.equal(stateManager._common.hardfork(), 'byzantium', 'it has default hardfork')
1213
stateManager.getStateRoot((err, res) => {
1314
st.error(err, 'getStateRoot returns no error')
1415
st.deepEqual(res, util.KECCAK256_RLP, 'it has default root')
16+
st.end()
1517
})
16-
17-
st.equal(stateManager._common.hardfork(), 'byzantium', 'it has default hardfork')
18-
st.end()
1918
})
2019

2120
t.test('should clear the cache when the state root is set', async (st) => {

tests/api/state/storageReader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { promisify } = require('util')
1+
const promisify = require('util.promisify')
22
const tape = require('tape')
33
const { StorageReader } = require('../../../lib/state')
44

tests/api/utils.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ const level = require('level-mem')
44
const Blockchain = require('ethereumjs-blockchain')
55
const VM = require('../../lib/index')
66

7-
function createGenesis () {
8-
const genesis = new Block()
7+
function createGenesis (opts = {}) {
8+
opts.chain = opts.chain ? opts.chain : 'mainnet'
9+
const genesis = new Block(null, opts)
910
genesis.setGenesisParams()
1011

1112
return genesis
@@ -20,10 +21,10 @@ function createAccount (nonce, balance) {
2021
return acc
2122
}
2223

23-
function setupVM () {
24+
function setupVM (opts = {}) {
2425
const db = level()
25-
const blockchain = new Blockchain(db)
26-
const vm = new VM({ blockchain })
26+
opts.blockchain = opts.blockchain ? opts.blockchain : new Blockchain({ db, validate: false })
27+
const vm = new VM(opts)
2728

2829
return vm
2930
}

0 commit comments

Comments
 (0)