Skip to content

Commit

Permalink
Merge pull request #1167 from ethereumjs/fix-common-custom-chain-error
Browse files Browse the repository at this point in the history
Blockchain: New Release v5.2.1 / Fixed Common custom chain error / VM API test rework and custom chain test additions
  • Loading branch information
holgerd77 authored Mar 26, 2021
2 parents 9186207 + 8cddbe2 commit 3d9854d
Show file tree
Hide file tree
Showing 22 changed files with 535 additions and 345 deletions.
3 changes: 2 additions & 1 deletion packages/block/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"browser": "dist.browser/index.js",
"scripts": {
"build": "ethereumjs-config-ts-build",
"prepublishOnly": "npm run build && npm run test",
"prepublishOnly": "npm run clean && npm run build && npm run test",
"clean": "rm -Rf ./dist && rm -Rf ./dist.browser",
"coverage": "ethereumjs-config-coverage",
"docs:build": "typedoc --options typedoc.js",
"format": "ethereumjs-config-format",
Expand Down
4 changes: 4 additions & 0 deletions packages/blockchain/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
(modification: no type change headlines) and this project adheres to
[Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## 5.2.1 - 2021-03-26

- Fixed a bug leading `Blockchain` to fail instantiating with a `common` **custom chain** setup when no `genesisBlock` was provided, PR [#1167](https://github.com/ethereumjs/ethereumjs-monorepo/pull/1167)

## 5.2.0 - 2021-03-18

### Berlin HF Support
Expand Down
5 changes: 3 additions & 2 deletions packages/blockchain/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ethereumjs/blockchain",
"version": "5.2.0",
"version": "5.2.1",
"description": "A module to store and interact with blocks",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand All @@ -10,7 +10,8 @@
],
"browser": "dist.browser/index.js",
"scripts": {
"prepublishOnly": "npm run build && npm run test",
"prepublishOnly": "npm run clean && npm run build && npm run test",
"clean": "rm -Rf ./dist && rm -Rf ./dist.browser",
"build": "ethereumjs-config-ts-build",
"coverage": "ethereumjs-config-coverage",
"docs:build": "typedoc --options typedoc.js",
Expand Down
6 changes: 2 additions & 4 deletions packages/blockchain/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,8 @@ export default class Blockchain implements BlockchainInterface {
}

if (!genesisBlock) {
const common = new Common({
chain: this._common.chainIdBN(),
hardfork: 'chainstart',
})
const common = this._common.copy()
common.setHardfork('chainstart')
genesisBlock = Block.genesis({}, { common })
}

Expand Down
5 changes: 3 additions & 2 deletions packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"build": "npm run build:node && npm run build:browser",
"build:node": "tsc -p ./tsconfig.prod.json",
"build:browser": "tsc -p ./tsconfig.browser.json && npm run bundle && rm -rf dist.browser",
"prepublishOnly": "npm run build && npm run test",
"prepublishOnly": "npm run clean && npm run build && npm run test",
"clean": "rm -Rf ./dist && rm -Rf ./dist.browser",
"bundle": "webpack",
"client:start": "ts-node bin/cli.ts",
"client:start:dev1": "npm run client:start -- --discDns=false --discV4=false --bootnodes",
Expand Down Expand Up @@ -54,7 +55,7 @@
"homepage": "https://github.com/ethereumjs/ethereumjs-client#readme",
"dependencies": {
"@ethereumjs/block": "^3.2.0",
"@ethereumjs/blockchain": "^5.2.0",
"@ethereumjs/blockchain": "^5.2.1",
"@ethereumjs/common": "^2.2.0",
"@ethereumjs/devp2p": "^3.0.3",
"@ethereumjs/tx": "^3.1.0",
Expand Down
3 changes: 2 additions & 1 deletion packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"browser": "dist.browser/index.js",
"scripts": {
"build": "ethereumjs-config-ts-build",
"prepublishOnly": "npm run build && npm run test",
"prepublishOnly": "npm run clean && npm run build && npm run test",
"clean": "rm -Rf ./dist && rm -Rf ./dist.browser",
"coverage": "ethereumjs-config-coverage",
"format": "ethereumjs-config-format",
"format:fix": "ethereumjs-config-format-fix",
Expand Down
6 changes: 5 additions & 1 deletion packages/common/tests/data/testnet2.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
"defaultHardfork": "istanbul",
"consensus": {
"type": "pos",
"algorithm": "clique"
"algorithm": "clique",
"clique": {
"period": 15,
"epoch": 30000
}
},
"comment": "Private test network",
"url": "[TESTNET_URL]",
Expand Down
6 changes: 5 additions & 1 deletion packages/common/tests/data/testnet3.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
"defaultHardfork": "istanbul",
"consensus": {
"type": "pos",
"algorithm": "clique"
"algorithm": "clique",
"clique": {
"period": 15,
"epoch": 30000
}
},
"comment": "Private test network",
"url": "[TESTNET_URL]",
Expand Down
3 changes: 2 additions & 1 deletion packages/devp2p/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
},
"scripts": {
"build": "ethereumjs-config-ts-build",
"prepublishOnly": "npm run build && npm run test",
"prepublishOnly": "npm run clean && npm run build && npm run test",
"clean": "rm -Rf ./dist && rm -Rf ./dist.browser",
"coverage": "ethereumjs-config-coverage",
"docs:build": "typedoc",
"format": "ethereumjs-config-format",
Expand Down
3 changes: 2 additions & 1 deletion packages/ethash/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"scripts": {
"build": "ethereumjs-config-ts-build",
"tsc": "ethereumjs-config-tsc",
"prepublishOnly": "npm run build && npm run test",
"prepublishOnly": "npm run clean && npm run build && npm run test",
"clean": "rm -Rf ./dist && rm -Rf ./dist.browser",
"lint": "ethereumjs-config-lint",
"lint:fix": "ethereumjs-config-lint-fix",
"coverage": "ethereumjs-config-coverage",
Expand Down
3 changes: 2 additions & 1 deletion packages/trie/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"scripts": {
"benchmarks": "node -r ts-node/register --max-old-space-size=8024 benchmarks",
"profiling": "tsc --target ES5 benchmarks/random.ts && 0x benchmarks/random.js",
"prepublishOnly": "npm run build && npm run test",
"prepublishOnly": "npm run clean && npm run build && npm run test",
"clean": "rm -Rf ./dist && rm -Rf ./dist.browser",
"build": "ethereumjs-config-ts-build",
"coverage": "ethereumjs-config-coverage",
"docs:build": "typedoc",
Expand Down
3 changes: 2 additions & 1 deletion packages/tx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"browser": "dist.browser/index.js",
"scripts": {
"build": "ethereumjs-config-ts-build",
"prepublishOnly": "npm run build && npm run test",
"prepublishOnly": "npm run clean && npm run build && npm run test",
"clean": "rm -Rf ./dist && rm -Rf ./dist.browser",
"coverage": "ethereumjs-config-coverage",
"docs:build": "typedoc --options typedoc.js",
"format": "ethereumjs-config-format",
Expand Down
3 changes: 2 additions & 1 deletion packages/util/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
],
"scripts": {
"build": "ethereumjs-config-ts-build",
"prepublishOnly": "npm run test && npm run build",
"prepublishOnly": "npm run clean && npm run build && npm run test",
"clean": "rm -Rf ./dist && rm -Rf ./dist.browser",
"coverage": "ethereumjs-config-coverage",
"docs:build": "npx typedoc --options typedoc.js",
"lint": "ethereumjs-config-lint",
Expand Down
5 changes: 3 additions & 2 deletions packages/vm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"build:benchmarks": "npm run build && tsc -p tsconfig.benchmarks.json",
"benchmarks": "node --max-old-space-size=4096 ./benchmarks/run.js benchmarks mainnetBlocks:10",
"profiling": "0x ./benchmarks/run.js profiling",
"prepublishOnly": "npm run build && npm run test:buildIntegrity",
"prepublishOnly": "npm run clean && npm run build && npm run test:buildIntegrity",
"clean": "rm -Rf ./dist && rm -Rf ./dist.browser",
"coverage": "nyc npm run coverage:test && nyc report --reporter=lcov",
"coverage:test": "tape -r ts-node/register './tests/api/**/*.ts' ./tests/tester.ts --state",
"docs:build": "typedoc --options typedoc.js",
Expand Down Expand Up @@ -45,7 +46,7 @@
],
"dependencies": {
"@ethereumjs/block": "^3.2.0",
"@ethereumjs/blockchain": "^5.2.0",
"@ethereumjs/blockchain": "^5.2.1",
"@ethereumjs/common": "^2.2.0",
"merkle-patricia-tree": "^4.1.0",
"@ethereumjs/tx": "^3.1.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Common from '@ethereumjs/common'
import VM from '../../../lib'

// See https://github.com/holiman/go-ethereum/blob/2c99023b68c573ba24a5b01db13e000bd9b82417/core/vm/testdata/precompiles/modexp_eip2565.json
const testData = require('./eip-2565-testdata.json')
const testData = require('../testdata/eip-2565.json')

tape('EIP-2565 ModExp gas cost tests', (t) => {
t.test('Test return data, gas cost and execution status against testdata', async (st) => {
Expand Down
141 changes: 61 additions & 80 deletions packages/vm/tests/api/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,38 @@
import tape from 'tape'
import { KECCAK256_RLP, toBuffer } from 'ethereumjs-util'
import { KECCAK256_RLP } from 'ethereumjs-util'
import { SecureTrie as Trie } from 'merkle-patricia-tree'
import { Block } from '@ethereumjs/block'
import Common from '@ethereumjs/common'
import { DefaultStateManager } from '../../lib/state'
import VM from '../../lib'
import { setupPreConditions, isRunningInKarma } from '../util'
import { isRunningInKarma } from '../util'
import { setupVM } from './utils'
import * as testData from './testdata.json'
import testnet from './testdata/testnet.json'
import testnet2 from './testdata/testnet2.json'

// explicitly import util and buffer,
// needed for karma-typescript bundling
// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars
import * as util from 'util'
// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars
import { Buffer } from 'buffer'

tape('VM with default blockchain', (t) => {
/**
* Tests for the main constructor API and
* exposed functionality by lib/index.js
*
* The following re-exported VM methods are tested within
* their own files:
*
* runBlockchain.spec.ts
* runBlock.spec.ts
* runTx.spec.ts
* runCall.spec.ts
* runCode.spec.ts
*
* opcodes.spec.ts (getActiveOpcodes())
*/

tape('VM -> basic instantiation / boolean switches', (t) => {
t.test('should instantiate without params', (st) => {
const vm = new VM()
st.ok(vm.stateManager)
Expand Down Expand Up @@ -48,24 +65,13 @@ tape('VM with default blockchain', (t) => {
)
st.end()
})
})

t.test('should work with trie (state) provided', async (st) => {
const trie = new Trie()
const vm = new VM({ state: trie, activatePrecompiles: true })
await vm.init()
st.notDeepEqual(
(vm.stateManager as DefaultStateManager)._trie.root,
KECCAK256_RLP,
'it has different root'
)
st.end()
})

tape('VM -> common (chain, HFs, EIPs)', (t) => {
t.test('should accept a common object as option', async (st) => {
const common = new Common({ chain: 'mainnet', hardfork: 'istanbul' })

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

st.end()
Expand Down Expand Up @@ -99,78 +105,53 @@ tape('VM with default blockchain', (t) => {
})
st.end()
})

t.test(
'should accept a custom chain config (Common.forCustomChain() static constructor)',
async (st) => {
const customChainParams = { name: 'custom', chainId: 123, networkId: 678 }
const common = Common.forCustomChain('mainnet', customChainParams, 'byzantium')

const vm = await VM.create({ common })
st.equal(vm._common, common)
st.end()
}
)

t.test(
'should accept a custom chain config (Common customChains constructor option)',
async (st) => {
const customChains = [testnet, testnet2]
const common = new Common({ chain: 'testnet2', hardfork: 'berlin', customChains })

const vm = await VM.create({ common })
st.equal(vm._common, common)
st.end()
}
)
})

tape('VM with blockchain', (t) => {
t.test('should instantiate', async (st) => {
const vm = setupVM()
tape('VM -> state (deprecated), blockchain', (t) => {
t.test('should work with trie (state) provided', async (st) => {
const trie = new Trie()
const vm = new VM({ state: trie, activatePrecompiles: true })
await vm.init()
st.deepEqual(
st.notDeepEqual(
(vm.stateManager as DefaultStateManager)._trie.root,
KECCAK256_RLP,
'it has default trie'
'it has different root'
)
st.end()
})

t.test('should run blockchain without blocks', async (st) => {
t.test('should instantiate', async (st) => {
const vm = setupVM()
await vm.runBlockchain()
st.end()
})

t.test('should run blockchain with mocked runBlock', async (st) => {
const common = new Common({ chain: 'ropsten' })
const genesisRlp = Buffer.from(testData.genesisRLP.slice(2), 'hex')
const genesisBlock = Block.fromRLPSerializedBlock(genesisRlp, { common })

const blockRlp = Buffer.from(testData.blocks[0].rlp.slice(2), 'hex')
const block = Block.fromRLPSerializedBlock(blockRlp, { common })

const vm = setupVM({ common, genesisBlock })
await vm.init()

st.equal(vm.blockchain.meta.genesis?.toString('hex'), testData.genesisBlockHeader.hash.slice(2))

await vm.blockchain.putBlock(block)
const head = await vm.blockchain.getHead()
st.equal(head.hash().toString('hex'), testData.blocks[0].blockHeader.hash.slice(2))

await setupPreConditions((vm.stateManager as DefaultStateManager)._trie, testData)

vm.runBlock = async () => new Promise((resolve, reject) => reject(new Error('test')))

try {
await vm.runBlockchain()
st.fail("it hasn't returned any errors")
} catch (e) {
st.equal(e.message, 'test', "it has correctly propagated runBlock's error")
st.end()
}
})

t.test('should run blockchain with blocks', async (st) => {
const common = new Common({ chain: 'ropsten' })

const genesisRlp = toBuffer(testData.genesisRLP)
const genesisBlock = Block.fromRLPSerializedBlock(genesisRlp, { common })

const blockRlp = toBuffer(testData.blocks[0].rlp)
const block = Block.fromRLPSerializedBlock(blockRlp, { common })

const vm = setupVM({ common, genesisBlock })
await vm.init()

st.equal(vm.blockchain.meta.genesis?.toString('hex'), testData.genesisBlockHeader.hash.slice(2))

await vm.blockchain.putBlock(block)
const head = await vm.blockchain.getHead()
st.equal(head.hash().toString('hex'), testData.blocks[0].blockHeader.hash.slice(2))

await setupPreConditions((vm.stateManager as DefaultStateManager)._trie, testData)

await vm.runBlockchain()

st.deepEqual(
(vm.stateManager as DefaultStateManager)._trie.root,
KECCAK256_RLP,
'it has default trie'
)
st.end()
})

Expand Down
Loading

0 comments on commit 3d9854d

Please sign in to comment.