Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into statemanager/refact…
Browse files Browse the repository at this point in the history
…or-into-capabilities
  • Loading branch information
acolytec3 committed Aug 5, 2024
2 parents cdfdf39 + 3810f1d commit 8fb507e
Show file tree
Hide file tree
Showing 70 changed files with 197 additions and 3,459 deletions.
7 changes: 0 additions & 7 deletions packages/block/src/header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import { paramsBlock } from './params.js'

import type { BlockHeaderBytes, BlockOptions, HeaderData, JsonHeader } from './types.js'
import type { CliqueConfig } from '@ethereumjs/common'
import type { BigIntLike } from '@ethereumjs/util'

interface HeaderCache {
hash: Uint8Array | undefined
Expand Down Expand Up @@ -154,12 +153,6 @@ export class BlockHeader {
blockNumber: number,
timestamp,
})
} else if (typeof setHardfork !== 'boolean') {
this.common.setHardforkBy({
blockNumber: number,
td: setHardfork as BigIntLike,
timestamp,
})
}

// Hardfork defaults which couldn't be paired with earlier defaults
Expand Down
5 changes: 1 addition & 4 deletions packages/block/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,9 @@ export interface BlockOptions {
* Set the hardfork either by timestamp (for HFs from Shanghai onwards) or by block number
* for older Hfs.
*
* Additionally it is possible to pass in a specific TD value to support live-Merge-HF
* transitions. Note that this should only be needed in very rare and specific scenarios.
*
* Default: `false` (HF is set to whatever default HF is set by the {@link Common} instance)
*/
setHardfork?: boolean | BigIntLike
setHardfork?: boolean
/**
* Block parameters sorted by EIP can be found in the exported `paramsBlock` dictionary,
* which is internally passed to the associated `@ethereumjs/common` instance which
Expand Down
19 changes: 2 additions & 17 deletions packages/block/test/block.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,27 +114,12 @@ describe('[Block]: block functions', () => {
number: 20, // Future block
},
},
{ common, setHardfork: 5001 },
{ common, setHardfork: true },
)
assert.equal(
block.common.hardfork(),
Hardfork.Paris,
'should use setHardfork option (td > threshold)',
)

block = createBlock(
{
header: {
number: 12, // Berlin block,
extraData: new Uint8Array(97),
},
},
{ common, setHardfork: 3000 },
)
assert.equal(
block.common.hardfork(),
Hardfork.Berlin,
'should work with setHardfork option (td < threshold)',
'should use setHardfork option post merge',
)
})

Expand Down
4 changes: 2 additions & 2 deletions packages/block/test/from-rpc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ describe('[fromRPC] - Alchemy/Infura API block responses', () => {
)
block = createBlockFromRpc(infura15571241woTxs as JsonRpcBlock, [], {
common,
setHardfork: 58750000000000000000000n,
setHardfork: true,
})
assert.equal(
bytesToHex(block.hash()),
Expand All @@ -157,7 +157,7 @@ describe('[fromRPC] - Alchemy/Infura API block responses', () => {

block = createBlockFromRpc(infura15571241wTxs as JsonRpcBlock, [], {
common,
setHardfork: 58750000000000000000000n,
setHardfork: true,
})
assert.equal(
bytesToHex(block.hash()),
Expand Down
12 changes: 0 additions & 12 deletions packages/block/test/header.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,6 @@ describe('[Block]: Header functions', () => {
'block should not be frozen when freeze deactivated in options',
)

assert.throws(
() =>
createHeaderFromRLP(rlpHeader, {
common,
freeze: false,
setHardfork: 1n, // Added to bypass defaulting setHardfork to true in static constructor
}),
'A base fee',
undefined,
'throws when RLP serialized block with no base fee on default hardfork (london) and setHardfork left undefined',
)

header = createHeaderFromRLP(
hexToBytes(
'0xf90214a00000000000000000000000000000000000000000000000000000000000000000a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347940000000000000000000000000000000000000000a0d7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000850400000000808213888080a011bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82faa00000000000000000000000000000000000000000000000000000000000000000880000000000000042',
Expand Down
2 changes: 1 addition & 1 deletion packages/block/test/testdata/4844-hardfork.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"period": 5,
"epoch": 30000
},
"terminalTotalDifficulty": 2,
"terminalTotalDifficulty": 0,
"terminalTotalDifficultyPassed": true
},
"nonce": "0x42",
Expand Down
3 changes: 1 addition & 2 deletions packages/block/test/testdata/testnetMerge.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@
},
{
"name": "paris",
"block": null,
"ttd": "5000"
"block": 15
},
{
"name": "shanghai",
Expand Down
8 changes: 2 additions & 6 deletions packages/blockchain/src/blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,14 +318,13 @@ export class Blockchain implements BlockchainInterface {
throw new Error(`no block for ${canonicalHead} found in DB`)
}
const header = await this._getHeader(hash, canonicalHead)
const td = await this.getParentTD(header)

const dbOps: DBOp[] = []
await this._deleteCanonicalChainReferences(canonicalHead + BIGINT_1, hash, dbOps)
const ops = dbOps.concat(this._saveHeadOps())

await this.dbManager.batch(ops)
await this.checkAndTransitionHardForkByNumber(canonicalHead, td, header.timestamp)
await this.checkAndTransitionHardForkByNumber(canonicalHead, header.timestamp)
})
if (this._deletedBlocks.length > 0) {
this.events.emit('deletedCanonicalBlocks', this._deletedBlocks)
Expand Down Expand Up @@ -429,7 +428,7 @@ export class Blockchain implements BlockchainInterface {
this._headBlockHash = blockHash
}
if (this._hardforkByHeadBlockNumber) {
await this.checkAndTransitionHardForkByNumber(blockNumber, parentTd, header.timestamp)
await this.checkAndTransitionHardForkByNumber(blockNumber, header.timestamp)
}

// delete higher number assignments and overwrite stale canonical chain
Expand Down Expand Up @@ -1211,12 +1210,10 @@ export class Blockchain implements BlockchainInterface {

async checkAndTransitionHardForkByNumber(
number: BigIntLike,
td?: BigIntLike,
timestamp?: BigIntLike,
): Promise<void> {
this.common.setHardforkBy({
blockNumber: number,
td,
timestamp,
})

Expand Down Expand Up @@ -1263,7 +1260,6 @@ export class Blockchain implements BlockchainInterface {
const common = this.common.copy()
common.setHardforkBy({
blockNumber: 0,
td: BigInt(common.genesis().difficulty),
timestamp: common.genesis().timestamp,
})

Expand Down
7 changes: 1 addition & 6 deletions packages/blockchain/src/constructors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,7 @@ export async function createBlockchain(opts: BlockchainOptions = {}) {

if (blockchain['_hardforkByHeadBlockNumber']) {
const latestHeader = await blockchain['_getHeader'](blockchain['_headHeaderHash'])
const td = await blockchain.getParentTD(latestHeader)
await blockchain.checkAndTransitionHardForkByNumber(
latestHeader.number,
td,
latestHeader.timestamp,
)
await blockchain.checkAndTransitionHardForkByNumber(latestHeader.number, latestHeader.timestamp)
}

return blockchain
Expand Down
26 changes: 3 additions & 23 deletions packages/blockchain/src/db/manager.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import {
createBlockFromValuesArray,
createHeaderFromValuesArray,
valuesArrayToHeaderData,
} from '@ethereumjs/block'
import { createBlockFromValuesArray, createHeaderFromValuesArray } from '@ethereumjs/block'
import { RLP } from '@ethereumjs/rlp'
import {
BIGINT_0,
BIGINT_1,
KECCAK256_RLP,
KECCAK256_RLP_ARRAY,
bytesToBigInt,
Expand Down Expand Up @@ -146,12 +140,7 @@ export class DBManager {
}

const blockData = [header.raw(), ...body] as BlockBytes
const opts: BlockOptions = { common: this.common }
if (number === BIGINT_0) {
opts.setHardfork = await this.getTotalDifficulty(hash, BIGINT_0)
} else {
opts.setHardfork = await this.getTotalDifficulty(header.parentHash, number - BIGINT_1)
}
const opts: BlockOptions = { common: this.common, setHardfork: true }
return createBlockFromValuesArray(blockData, opts)
}

Expand All @@ -170,16 +159,7 @@ export class DBManager {
const encodedHeader = await this.get(DBTarget.Header, { blockHash, blockNumber })
const headerValues = RLP.decode(encodedHeader)

const opts: BlockOptions = { common: this.common }
if (blockNumber === BIGINT_0) {
opts.setHardfork = await this.getTotalDifficulty(blockHash, BIGINT_0)
} else {
// Lets fetch the parent hash but not by number since this block might not
// be in canonical chain
const headerData = valuesArrayToHeaderData(headerValues as Uint8Array[])
const parentHash = headerData.parentHash as Uint8Array
opts.setHardfork = await this.getTotalDifficulty(parentHash, blockNumber - BIGINT_1)
}
const opts: BlockOptions = { common: this.common, setHardfork: true }
return createHeaderFromValuesArray(headerValues as Uint8Array[], opts)
}

Expand Down
1 change: 0 additions & 1 deletion packages/blockchain/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export async function genGenesisStateRoot(
const genCommon = common.copy()
genCommon.setHardforkBy({
blockNumber: 0,
td: BigInt(genCommon.genesis().difficulty),
timestamp: genCommon.genesis().timestamp,
})
if (genCommon.isActivatedEIP(6800)) {
Expand Down
117 changes: 0 additions & 117 deletions packages/blockchain/test/pos.spec.ts

This file was deleted.

Loading

0 comments on commit 8fb507e

Please sign in to comment.