From b2df1473b8e3fbf9315236d1099bde516a3d002b Mon Sep 17 00:00:00 2001 From: Jochem Brouwer Date: Thu, 22 Jun 2023 18:21:04 +0200 Subject: [PATCH] Shanghai to Cancun transition network runner (#2770) * vm: 4844 transition network runner * blockchain: add excess data gas check * blockchain: remove calculateExcessDataGas [no ci] * block/header: add mandatory fields 4844 check * header: move checks down * header: use header.common instead of opts.common --- packages/block/src/header.ts | 17 +++++++++++++---- packages/blockchain/src/blockchain.ts | 7 +++++++ packages/vm/test/tester/config.ts | 11 ++++++++++- 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/packages/block/src/header.ts b/packages/block/src/header.ts index f46d5d858b..cc6195e3b3 100644 --- a/packages/block/src/header.ts +++ b/packages/block/src/header.ts @@ -110,16 +110,25 @@ export class BlockHeader { */ public static fromValuesArray(values: BlockHeaderBytes, opts: BlockOptions = {}) { const headerData = valuesArrayToHeaderData(values) - const { number, baseFeePerGas } = headerData + const { number, baseFeePerGas, excessDataGas, dataGasUsed } = headerData + const header = BlockHeader.fromHeaderData(headerData, opts) // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions - if (opts.common?.isActivatedEIP(1559) && baseFeePerGas === undefined) { - const eip1559ActivationBlock = bigIntToBytes(opts.common?.eipBlock(1559)!) + if (header._common.isActivatedEIP(1559) && baseFeePerGas === undefined) { + const eip1559ActivationBlock = bigIntToBytes(header._common.eipBlock(1559)!) // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions if (eip1559ActivationBlock && equalsBytes(eip1559ActivationBlock, number as Uint8Array)) { throw new Error('invalid header. baseFeePerGas should be provided') } } - return BlockHeader.fromHeaderData(headerData, opts) + // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions + if (header._common.isActivatedEIP(4844)) { + if (excessDataGas === undefined) { + throw new Error('invalid header. excessDataGas should be provided') + } else if (dataGasUsed === undefined) { + throw new Error('invalid header. dataGasUsed should be provided') + } + } + return header } /** * This constructor takes the values, validates them, assigns them and freezes the object. diff --git a/packages/blockchain/src/blockchain.ts b/packages/blockchain/src/blockchain.ts index c4698d552c..076ecd4f38 100644 --- a/packages/blockchain/src/blockchain.ts +++ b/packages/blockchain/src/blockchain.ts @@ -595,6 +595,13 @@ export class Blockchain implements BlockchainInterface { throw new Error(`Invalid block: base fee not correct ${header.errorStr()}`) } } + + if (header._common.isActivatedEIP(4844) === true) { + const expectedExcessDataGas = parentHeader.calcNextExcessDataGas() + if (header.excessDataGas !== expectedExcessDataGas) { + throw new Error(`expected data gas: ${expectedExcessDataGas}, got: ${header.excessDataGas}`) + } + } } /** diff --git a/packages/vm/test/tester/config.ts b/packages/vm/test/tester/config.ts index 78e6ae52b1..a58480ced4 100644 --- a/packages/vm/test/tester/config.ts +++ b/packages/vm/test/tester/config.ts @@ -280,7 +280,7 @@ export function getTestDirs(network: string, testType: string) { * @param ttd If set: total terminal difficulty to switch to merge * @returns */ -function setupCommonWithNetworks(network: string, ttd?: number) { +function setupCommonWithNetworks(network: string, ttd?: number, timestamp?: number) { let networkLowercase: string // This only consists of the target hardfork, so without the EIPs if (network.includes('+')) { const index = network.indexOf('+') @@ -323,6 +323,13 @@ function setupCommonWithNetworks(network: string, ttd?: number) { ttd: BigInt(ttd), }) } + if (timestamp !== undefined && hf.name !== Hardfork.Dao) { + testHardforks.push({ + name: hf.name, + block: null, + timestamp, + }) + } } } const common = Common.custom( @@ -369,6 +376,8 @@ export function getCommon(network: string): Common { const startNetwork = network.substring(0, start) // HF before the merge const TTD = Number('0x' + network.substring(end)) // Total difficulty to transition to PoS return setupCommonWithNetworks(startNetwork, TTD) + } else if (networkLowercase === 'shanghaitocancunattime15k') { + return setupCommonWithNetworks('Shanghai', undefined, 15000) } else { // Case 3: this is not a "default fork" network, but it is a "transition" network. Test the VM if it transitions the right way const transitionForks =