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

Common: Chain Refactor #3545

Merged
merged 47 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
a088bd5
Reduce Common chain option to ChainDict, remove all conversion functi…
holgerd77 Jul 26, 2024
c5e696f
Adjust tx, remove chain ID infering, fix a lot of tests
holgerd77 Jul 26, 2024
6cb429b
Merge branch 'master' into common-chain-refactor
holgerd77 Jul 26, 2024
aafc37c
Merge remote-tracking branch 'origin/master' into common-chain-refactor
acolytec3 Jul 29, 2024
a2950d1
replace chain.Mainnet with Mainnet [no ci]
acolytec3 Jul 29, 2024
8fab9c7
add preconfig chain util
acolytec3 Jul 29, 2024
3793b55
various fixes
acolytec3 Jul 29, 2024
dc486c4
Lots of client test fixes [no ci]
acolytec3 Jul 29, 2024
ff16012
Remove edits [no ci]
acolytec3 Jul 29, 2024
1408f16
Always add trace to tests
acolytec3 Jul 29, 2024
002e3a9
remove console log
acolytec3 Jul 29, 2024
ba190e2
more test fixes [no ci]
acolytec3 Jul 29, 2024
b4493bd
Fix blockchain tests
acolytec3 Jul 29, 2024
30580dc
fix block tests
acolytec3 Jul 29, 2024
b3e676f
fix devp2p tests
acolytec3 Jul 29, 2024
d082f31
fix ethash tests
acolytec3 Jul 29, 2024
3163201
fix evm tests
acolytec3 Jul 29, 2024
851f3d8
some sm fixes
acolytec3 Jul 29, 2024
6ca851a
lots o vm test fixes [no ci]
acolytec3 Jul 29, 2024
7f59280
fix verkle tests
acolytec3 Jul 29, 2024
350a034
many client test fixes
acolytec3 Jul 29, 2024
e0344be
more client test fixes [no ci]
acolytec3 Jul 29, 2024
177645e
half fixed
acolytec3 Jul 29, 2024
beaa077
yet more fixes
acolytec3 Jul 30, 2024
3c605b4
fix benchmarks
acolytec3 Jul 30, 2024
0be16fa
add defaults
acolytec3 Jul 30, 2024
c4beeea
remove unnecessary parsing
acolytec3 Jul 30, 2024
6ea208f
fix vmexecution test
acolytec3 Jul 30, 2024
fe8f45e
activate all sendRawTransaction tests
acolytec3 Jul 30, 2024
333a097
fix examples [no ci]
acolytec3 Jul 31, 2024
b287bdc
Merge remote-tracking branch 'origin/master' into common-chain-refactor
acolytec3 Jul 31, 2024
9e90c2d
lint [no ci]
acolytec3 Jul 31, 2024
b34511d
Merge branch 'master' into common-chain-refactor
jochem-brouwer Jul 31, 2024
e216820
block: fix header tests
jochem-brouwer Jul 31, 2024
475be17
client: fix some sendRawTx tests
jochem-brouwer Jul 31, 2024
6e24613
tx: partial 4844 fix
jochem-brouwer Jul 31, 2024
507b85b
client: eth: fix param reads from tx
jochem-brouwer Jul 31, 2024
d5eed95
client/tests: cli remove it.only
jochem-brouwer Jul 31, 2024
c4bdcb2
fix param parsing
acolytec3 Jul 31, 2024
f09809d
lint
acolytec3 Jul 31, 2024
3df0520
fix custom chain ops
acolytec3 Jul 31, 2024
4525142
fix test
acolytec3 Jul 31, 2024
341fad3
one more
acolytec3 Jul 31, 2024
e301f04
update createCustomCommon docs
acolytec3 Jul 31, 2024
1a38eb2
really fix test
acolytec3 Jul 31, 2024
729846c
test fix
acolytec3 Jul 31, 2024
ab09903
change kill to sigterm
acolytec3 Jul 31, 2024
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
4 changes: 2 additions & 2 deletions packages/block/examples/1559.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createBlock } from '@ethereumjs/block'
import { Chain, Common, Hardfork } from '@ethereumjs/common'
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.London })
import { Common, Hardfork, Mainnet } from '@ethereumjs/common'
const common = new Common({ chain: Mainnet, hardfork: Hardfork.London })

const block = createBlock(
{
Expand Down
4 changes: 2 additions & 2 deletions packages/block/examples/4844.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createBlock } from '@ethereumjs/block'
import { Chain, Common, Hardfork } from '@ethereumjs/common'
import { Common, Hardfork, Mainnet } from '@ethereumjs/common'
import { create4844BlobTx } from '@ethereumjs/tx'
import { createAddressFromPrivateKey } from '@ethereumjs/util'
import { randomBytes } from 'crypto'
Expand All @@ -9,7 +9,7 @@ const main = async () => {
const kzg = await loadKZG()

const common = new Common({
chain: Chain.Mainnet,
chain: Mainnet,
hardfork: Hardfork.Cancun,
customCrypto: {
kzg,
Expand Down
4 changes: 2 additions & 2 deletions packages/block/examples/clique.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createBlock } from '@ethereumjs/block'
import { Chain, Common, Hardfork } from '@ethereumjs/common'
import { Common, Goerli, Hardfork } from '@ethereumjs/common'

const common = new Common({ chain: Chain.Goerli, hardfork: Hardfork.Chainstart })
const common = new Common({ chain: Goerli, hardfork: Hardfork.Chainstart })

console.log(common.consensusType()) // 'poa'
console.log(common.consensusAlgorithm()) // 'clique'
Expand Down
4 changes: 2 additions & 2 deletions packages/block/examples/pos.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createBlock } from '@ethereumjs/block'
import { Chain, Common } from '@ethereumjs/common'
import { Common, Mainnet } from '@ethereumjs/common'

const common = new Common({ chain: Chain.Mainnet })
const common = new Common({ chain: Mainnet })

const block = createBlock(
{
Expand Down
4 changes: 2 additions & 2 deletions packages/block/examples/pow.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createBlock } from '@ethereumjs/block'
import { Chain, Common, Hardfork } from '@ethereumjs/common'
import { Common, Hardfork, Mainnet } from '@ethereumjs/common'

const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Chainstart })
const common = new Common({ chain: Mainnet, hardfork: Hardfork.Chainstart })

console.log(common.consensusType()) // 'pow'
console.log(common.consensusAlgorithm()) // 'ethash'
Expand Down
4 changes: 2 additions & 2 deletions packages/block/examples/withdrawals.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { createBlock } from '@ethereumjs/block'
import { Chain, Common } from '@ethereumjs/common'
import { Common, Mainnet } from '@ethereumjs/common'
import { Address, hexToBytes } from '@ethereumjs/util'

import type { WithdrawalData } from '@ethereumjs/util'

const common = new Common({ chain: Chain.Mainnet })
const common = new Common({ chain: Mainnet })

const withdrawal = <WithdrawalData>{
index: BigInt(0),
Expand Down
4 changes: 2 additions & 2 deletions packages/block/src/header.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Chain, Common, ConsensusAlgorithm, ConsensusType, Hardfork } from '@ethereumjs/common'
import { Common, ConsensusAlgorithm, ConsensusType, Hardfork, Mainnet } from '@ethereumjs/common'
import { RLP } from '@ethereumjs/rlp'
import {
Address,
Expand Down Expand Up @@ -101,7 +101,7 @@ export class BlockHeader {
this.common = opts.common.copy()
} else {
this.common = new Common({
chain: Chain.Mainnet, // default
chain: Mainnet, // default
})
}
this.common.updateParams(opts.params ?? paramsBlock)
Expand Down
75 changes: 36 additions & 39 deletions packages/block/test/block.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Chain, Common, Hardfork } from '@ethereumjs/common'
import { Common, Goerli, Hardfork, Mainnet, createCustomCommon } from '@ethereumjs/common'
import { RLP } from '@ethereumjs/rlp'
import { createLegacyTx } from '@ethereumjs/tx'
import {
Expand Down Expand Up @@ -26,12 +26,11 @@ import * as testDataPreLondon2 from './testdata/testdata_pre-london-2.json'
import * as testDataPreLondon from './testdata/testdata_pre-london.json'
import * as testnetMerge from './testdata/testnetMerge.json'

import type { ChainConfig } from '@ethereumjs/common'
import type { NestedUint8Array, PrefixedHexString } from '@ethereumjs/util'

describe('[Block]: block functions', () => {
it('should test block initialization', () => {
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Chainstart })
const common = new Common({ chain: Mainnet, hardfork: Hardfork.Chainstart })
const genesis = createBlock({}, { common })
assert.ok(bytesToHex(genesis.hash()), 'block should initialize')

Expand Down Expand Up @@ -93,11 +92,9 @@ describe('[Block]: block functions', () => {
})

it('initialization -> setHardfork option', () => {
const customChains = [testnetMerge]
const common = new Common({
chain: 'testnetMerge',
hardfork: Hardfork.Istanbul,
customChains: customChains as ChainConfig[],
// @ts-ignore type is too strict in this case
const common = createCustomCommon(testnetMerge.default, Mainnet, {
name: 'testnetMerge',
})

let block = createBlock(
Expand Down Expand Up @@ -148,24 +145,24 @@ describe('[Block]: block functions', () => {
})

it('should initialize with null parameters without throwing', () => {
const common = new Common({ chain: Chain.Goerli })
const common = new Common({ chain: Goerli })
const opts = { common }
assert.doesNotThrow(function () {
createBlock({}, opts)
})
})

it('should throw when trying to initialize with uncle headers on a PoA network', () => {
const common = new Common({ chain: Chain.Mainnet })
const common = new Common({ chain: Mainnet })
const uncleBlock = createBlock({ header: { extraData: new Uint8Array(117) } }, { common })
assert.throws(function () {
createBlock({ uncleHeaders: [uncleBlock.header] }, { common })
})
})

it('should test block validation on pow chain', async () => {
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Istanbul })
const blockRlp = hexToBytes(testDataPreLondon.blocks[0].rlp as PrefixedHexString)
const common = new Common({ chain: Mainnet, hardfork: Hardfork.Istanbul })
const blockRlp = hexToBytes(testDataPreLondon.default.blocks[0].rlp as PrefixedHexString)
try {
createBlockFromRLPSerializedBlock(blockRlp, { common })
assert.ok(true, 'should pass')
Expand All @@ -175,10 +172,10 @@ describe('[Block]: block functions', () => {
})

it('should test block validation on poa chain', async () => {
const common = new Common({ chain: Chain.Goerli, hardfork: Hardfork.Chainstart })
const common = new Common({ chain: Goerli, hardfork: Hardfork.Chainstart })

try {
createBlockFromRpc(testDataFromRpcGoerli as JsonRpcBlock, [], { common })
createBlockFromRpc(testDataFromRpcGoerli.default as JsonRpcBlock, [], { common })
assert.ok(true, 'does not throw')
} catch (error: any) {
assert.fail('error thrown')
Expand All @@ -191,8 +188,8 @@ describe('[Block]: block functions', () => {
}

it('should test transaction validation - invalid tx trie', async () => {
const blockRlp = hexToBytes(testDataPreLondon.blocks[0].rlp as PrefixedHexString)
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.London })
const blockRlp = hexToBytes(testDataPreLondon.default.blocks[0].rlp as PrefixedHexString)
const common = new Common({ chain: Mainnet, hardfork: Hardfork.London })
const block = createBlockFromRLPSerializedBlock(blockRlp, { common, freeze: false })
await testTransactionValidation(block)
;(block.header as any).transactionsTrie = new Uint8Array(32)
Expand Down Expand Up @@ -231,8 +228,8 @@ describe('[Block]: block functions', () => {
})

it('should test transaction validation with legacy tx in london', async () => {
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.London })
const blockRlp = hexToBytes(testDataPreLondon.blocks[0].rlp as PrefixedHexString)
const common = new Common({ chain: Mainnet, hardfork: Hardfork.London })
const blockRlp = hexToBytes(testDataPreLondon.default.blocks[0].rlp as PrefixedHexString)
const block = createBlockFromRLPSerializedBlock(blockRlp, { common, freeze: false })
await testTransactionValidation(block)
;(block.transactions[0] as any).gasPrice = BigInt(0)
Expand All @@ -244,8 +241,8 @@ describe('[Block]: block functions', () => {
})

it('should test uncles hash validation', async () => {
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Istanbul })
const blockRlp = hexToBytes(testDataPreLondon2.blocks[2].rlp as PrefixedHexString)
const common = new Common({ chain: Mainnet, hardfork: Hardfork.Istanbul })
const blockRlp = hexToBytes(testDataPreLondon2.default.blocks[2].rlp as PrefixedHexString)
const block = createBlockFromRLPSerializedBlock(blockRlp, { common, freeze: false })
assert.equal(block.uncleHashIsValid(), true)
;(block.header as any).uncleHash = new Uint8Array(32)
Expand Down Expand Up @@ -299,7 +296,7 @@ describe('[Block]: block functions', () => {
uncleHash: KECCAK256_RLP_ARRAY,
},
},
{ common: new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Shanghai }) },
{ common: new Common({ chain: Mainnet, hardfork: Hardfork.Shanghai }) },
)
await checkThrowsAsync(block.validateData(false, false), 'invalid withdrawals trie')

Expand All @@ -310,12 +307,12 @@ describe('[Block]: block functions', () => {
uncleHash: zeroRoot,
},
},
{ common: new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Chainstart }) },
{ common: new Common({ chain: Mainnet, hardfork: Hardfork.Chainstart }) },
)
await checkThrowsAsync(block.validateData(false, false), 'invalid uncle hash')

// Verkle withness
const common = new Common({ chain: Chain.Mainnet, eips: [6800], hardfork: Hardfork.Cancun })
const common = new Common({ chain: Mainnet, eips: [6800], hardfork: Hardfork.Cancun })
// Note: `executionWitness: undefined` will still initialize an execution witness in the block
// So, only testing for `null` here
block = createBlock({ executionWitness: null }, { common })
Expand All @@ -333,22 +330,22 @@ describe('[Block]: block functions', () => {
})

it('should test genesis hashes (mainnet default)', () => {
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Chainstart })
const rlp = hexToBytes(`0x${testDataGenesis.test.genesis_rlp_hex}`)
const hash = hexToBytes(`0x${testDataGenesis.test.genesis_hash}`)
const common = new Common({ chain: Mainnet, hardfork: Hardfork.Chainstart })
const rlp = hexToBytes(`0x${testDataGenesis.default.test.genesis_rlp_hex}`)
const hash = hexToBytes(`0x${testDataGenesis.default.test.genesis_hash}`)
const block = createBlockFromRLPSerializedBlock(rlp, { common })
assert.ok(equalsBytes(block.hash(), hash), 'genesis hash match')
})

it('should test hash() method (mainnet default)', () => {
let common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Chainstart })
const rlp = hexToBytes(`0x${testDataGenesis.test.genesis_rlp_hex}`)
const hash = hexToBytes(`0x${testDataGenesis.test.genesis_hash}`)
let common = new Common({ chain: Mainnet, hardfork: Hardfork.Chainstart })
const rlp = hexToBytes(`0x${testDataGenesis.default.test.genesis_rlp_hex}`)
const hash = hexToBytes(`0x${testDataGenesis.default.test.genesis_hash}`)
let block = createBlockFromRLPSerializedBlock(rlp, { common })
assert.ok(equalsBytes(block.hash(), hash), 'genesis hash match')

common = new Common({
chain: Chain.Mainnet,
chain: Mainnet,
hardfork: Hardfork.Chainstart,
customCrypto: {
keccak256: () => {
Expand Down Expand Up @@ -380,9 +377,9 @@ describe('[Block]: block functions', () => {
})

it('should return the same block data from raw()', () => {
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Istanbul })
const common = new Common({ chain: Mainnet, hardfork: Hardfork.Istanbul })
const block = createBlockFromRLPSerializedBlock(
toBytes(testDataPreLondon2.blocks[2].rlp as PrefixedHexString),
toBytes(testDataPreLondon2.default.blocks[2].rlp as PrefixedHexString),
{
common,
},
Expand All @@ -392,9 +389,9 @@ describe('[Block]: block functions', () => {
})

it('should test toJSON', () => {
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Istanbul })
const common = new Common({ chain: Mainnet, hardfork: Hardfork.Istanbul })
const block = createBlockFromRLPSerializedBlock(
toBytes(testDataPreLondon2.blocks[2].rlp as PrefixedHexString),
toBytes(testDataPreLondon2.default.blocks[2].rlp as PrefixedHexString),
{
common,
},
Expand All @@ -404,12 +401,12 @@ describe('[Block]: block functions', () => {

it('DAO hardfork', () => {
const blockData = RLP.decode(
testDataPreLondon2.blocks[0].rlp as PrefixedHexString,
testDataPreLondon2.default.blocks[0].rlp as PrefixedHexString,
) as NestedUint8Array
// Set block number from test block to mainnet DAO fork block 1920000
blockData[0][8] = hexToBytes('0x1D4C00')

const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Dao })
const common = new Common({ chain: Mainnet, hardfork: Hardfork.Dao })
assert.throws(
function () {
createBlockFromValuesArray(blockData as BlockBytes, { common })
Expand All @@ -428,15 +425,15 @@ describe('[Block]: block functions', () => {
})

it('should set canonical difficulty if I provide a calcDifficultyFromHeader header', () => {
let common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Chainstart })
let common = new Common({ chain: Mainnet, hardfork: Hardfork.Chainstart })
const genesis = createBlock({}, { common })

const nextBlockHeaderData = {
number: genesis.header.number + BigInt(1),
timestamp: genesis.header.timestamp + BigInt(10),
}

common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.London })
common = new Common({ chain: Mainnet, hardfork: Hardfork.London })
const blockWithoutDifficultyCalculation = createBlock(
{
header: nextBlockHeaderData,
Expand Down Expand Up @@ -495,7 +492,7 @@ describe('[Block]: block functions', () => {
})

it('should be able to initialize shanghai blocks with correct hardfork defaults', () => {
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Shanghai })
const common = new Common({ chain: Mainnet, hardfork: Hardfork.Shanghai })
const block = createBlock({}, { common })
assert.equal(block.common.hardfork(), Hardfork.Shanghai, 'hardfork should be set to shanghai')
assert.deepEqual(block.withdrawals, [], 'withdrawals should be set to default empty array')
Expand Down
4 changes: 2 additions & 2 deletions packages/block/test/clique.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Chain, Common, Hardfork } from '@ethereumjs/common'
import { Common, Goerli, Hardfork } from '@ethereumjs/common'
import { Address, createZeroAddress, hexToBytes } from '@ethereumjs/util'
import { assert, describe, it } from 'vitest'

import { createHeader } from '../src/constructors.js'

describe('[Header]: Clique PoA Functionality', () => {
const common = new Common({ chain: Chain.Goerli, hardfork: Hardfork.Chainstart })
const common = new Common({ chain: Goerli, hardfork: Hardfork.Chainstart })

it('Header Data', () => {
let header = createHeader({ number: 1 })
Expand Down
24 changes: 12 additions & 12 deletions packages/block/test/difficulty.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Chain, Common } from '@ethereumjs/common'
import { Common, Hardfork, Mainnet } from '@ethereumjs/common'
import { assert, describe, it } from 'vitest'

import * as difficultyMainNetwork from '../../ethereum-tests/BasicTests/difficultyMainNetwork.json'
Expand All @@ -23,17 +23,17 @@ function runDifficultyTests(test: any, parentBlock: Block, block: Block, msg: st
type TestData = { [key: string]: any }

const hardforkTestData: TestData = {
chainstart: difficultyFrontier.difficultyFrontier.Frontier,
homestead: difficultyHomestead.difficultyHomestead.Homestead,
byzantium: difficultyByzantium.difficultyByzantium.Byzantium,
constantinople: difficultyConstantinople.difficultyConstantinople.Constantinople,
chainstart: difficultyFrontier.default.difficultyFrontier.Frontier,
homestead: difficultyHomestead.default.difficultyHomestead.Homestead,
byzantium: difficultyByzantium.default.difficultyByzantium.Byzantium,
constantinople: difficultyConstantinople.default.difficultyConstantinople.Constantinople,
muirGlacier: Object.assign(
difficultyEIP2384.difficultyEIP2384.Berlin,
difficultyEIP2384_random.difficultyEIP2384_random.Berlin,
difficultyEIP2384_random_to20M.difficultyEIP2384_random_to20M.Berlin,
difficultyEIP2384.default.difficultyEIP2384.Berlin,
difficultyEIP2384_random.default.difficultyEIP2384_random.Berlin,
difficultyEIP2384_random_to20M.default.difficultyEIP2384_random_to20M.Berlin,
),
arrowGlacier: difficultyArrowGlacier.difficultyArrowGlacier.ArrowGlacier,
grayGlacier: difficultyGrayGlacier.difficultyGrayGlacier.GrayGlacier,
arrowGlacier: difficultyArrowGlacier.default.difficultyArrowGlacier.ArrowGlacier,
grayGlacier: difficultyGrayGlacier.default.difficultyGrayGlacier.GrayGlacier,
}

const chainTestData: TestData = {
Expand All @@ -47,7 +47,7 @@ describe('[Header]: difficulty tests', () => {
const testData = hardforkTestData[hardfork]
for (const testName in testData) {
const test = testData[testName]
const common = new Common({ chain: Chain.Mainnet, hardfork })
const common = new Common({ chain: Mainnet, hardfork })
// Unschedule any timestamp since tests are not configured for timestamps
common
.hardforks()
Expand Down Expand Up @@ -89,7 +89,7 @@ describe('[Header]: difficulty tests', () => {
const testData = chainTestData[chain]
for (const testName in testData.default) {
const test = testData[testName]
const common = new Common({ chain })
const common = new Common({ chain: Mainnet, hardfork: Hardfork.Chainstart })
const blockOpts = { common, setHardfork: true }
const uncleHash = test.parentUncles === '0x00' ? undefined : test.parentUncles
const parentBlock = createBlock(
Expand Down
Loading
Loading