Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix browser test issues #468

Merged
merged 4 commits into from
Mar 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,8 @@ module.exports = function (config) {
],

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

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

// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity
concurrency: Infinity,

// Fail after timeout
browserDisconnectTimeout: 100000,
browserNoActivityTimeout: 100000
})
}
4 changes: 2 additions & 2 deletions tests/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ tape('VM with blockchain', (t) => {
})

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

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

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

Expand Down
9 changes: 1 addition & 8 deletions tests/api/runBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ function setup (vm = null) {
data: testData,
p: {
runBlock: promisify(runBlock.bind(vm)),
putAccount: promisify(vm.stateManager.putAccount.bind(vm.stateManager)),
generateCanonicalGenesis: promisify(vm.stateManager.generateCanonicalGenesis.bind(vm.stateManager))
putAccount: promisify(vm.stateManager.putAccount.bind(vm.stateManager))
}
}
}
Expand All @@ -59,8 +58,6 @@ tape('runBlock', async (t) => {
t.test('should fail when runTx fails', async (st) => {
const block = new Block(util.rlp.decode(suite.data.blocks[0].rlp))

await suite.p.generateCanonicalGenesis()

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

await suite.p.generateCanonicalGenesis()

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

await suite.p.generateCanonicalGenesis()

// The mocked VM uses a mocked runCall
// which always returns an error.
// runTx is a full implementation that works.
Expand Down
31 changes: 20 additions & 11 deletions tests/api/runBlockchain.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
const tape = require('tape')
const level = require('level-mem')
const { promisify } = require('util')
const promisify = require('util.promisify')
const Blockchain = require('ethereumjs-blockchain')
const Block = require('ethereumjs-block')
const Common = require('ethereumjs-common').default
const util = require('ethereumjs-util')
const runBlockchain = require('../../lib/runBlockchain')
const { StateManager } = require('../../lib/state')
const { createGenesis } = require('./utils')

tape('runBlockchain', (t) => {
const blockchainDB = level()
const blockchain = new Blockchain({ db: blockchainDB })
const vm = { stateManager: new StateManager(), blockchain }
const blockchain = new Blockchain({
db: blockchainDB,
chain: 'goerli',
validate: false
})
const vm = {
stateManager: new StateManager({ common: new Common('goerli') }),
blockchain: blockchain
}

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

t.test('should run with genesis block', async (st) => {
const genesis = createGenesis()
const genesis = createGenesis({ chain: 'goerli' })

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

const genesis = createGenesis()
const genesis = createGenesis({ chain: 'goerli' })
await putGenesisP(genesis)

const b1 = createBlock(genesis, 1)
const b2 = createBlock(b1, 2)
const b3 = createBlock(b2, 3)
const b1 = createBlock(genesis, 1, { chain: 'goerli' })
const b2 = createBlock(b1, 2, { chain: 'goerli' })
const b3 = createBlock(b2, 3, { chain: 'goerli' })

blockchain.validate = false

Expand All @@ -79,12 +87,13 @@ tape('runBlockchain', (t) => {
})
})

function createBlock (parent = null, n = 0) {
function createBlock (parent = null, n = 0, opts = {}) {
opts.chain = opts.chain ? opts.chain : 'mainnet'
if (parent === null) {
return createGenesis()
return createGenesis(opts)
}

const b = new Block()
const b = new Block(null, opts)
b.header.number = util.toBuffer(n)
b.header.parentHash = parent.hash()
b.header.difficulty = '0xfffffff'
Expand Down
7 changes: 3 additions & 4 deletions tests/api/state/stateManager.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { promisify } = require('util')
const promisify = require('util.promisify')
const tape = require('tape')
const util = require('ethereumjs-util')
const StateManager = require('../../../lib/state/stateManager')
Expand All @@ -9,13 +9,12 @@ tape('StateManager', (t) => {
const stateManager = new StateManager()

st.deepEqual(stateManager._trie.root, util.KECCAK256_RLP, 'it has default root')
st.equal(stateManager._common.hardfork(), 'byzantium', 'it has default hardfork')
stateManager.getStateRoot((err, res) => {
st.error(err, 'getStateRoot returns no error')
st.deepEqual(res, util.KECCAK256_RLP, 'it has default root')
st.end()
})

st.equal(stateManager._common.hardfork(), 'byzantium', 'it has default hardfork')
st.end()
})

t.test('should clear the cache when the state root is set', async (st) => {
Expand Down
2 changes: 1 addition & 1 deletion tests/api/state/storageReader.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { promisify } = require('util')
const promisify = require('util.promisify')
const tape = require('tape')
const { StorageReader } = require('../../../lib/state')

Expand Down
11 changes: 6 additions & 5 deletions tests/api/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ const level = require('level-mem')
const Blockchain = require('ethereumjs-blockchain')
const VM = require('../../lib/index')

function createGenesis () {
const genesis = new Block()
function createGenesis (opts = {}) {
opts.chain = opts.chain ? opts.chain : 'mainnet'
const genesis = new Block(null, opts)
genesis.setGenesisParams()

return genesis
Expand All @@ -20,10 +21,10 @@ function createAccount (nonce, balance) {
return acc
}

function setupVM () {
function setupVM (opts = {}) {
const db = level()
const blockchain = new Blockchain(db)
const vm = new VM({ blockchain })
opts.blockchain = opts.blockchain ? opts.blockchain : new Blockchain({ db, validate: false })
const vm = new VM(opts)

return vm
}
Expand Down