From 01e6266766ae5db561bed14668b387e4cefc7ce1 Mon Sep 17 00:00:00 2001 From: Holger Drewes Date: Sat, 17 Aug 2024 17:27:43 +0200 Subject: [PATCH] Adjust/fix some client tests --- packages/client/src/execution/vmexecution.ts | 63 ++++--------------- packages/client/src/miner/miner.ts | 9 ++- packages/client/src/miner/pendingBlock.ts | 3 - packages/client/test/miner/miner.spec.ts | 10 ++- .../client/test/miner/pendingBlock.spec.ts | 59 ++++++++--------- 5 files changed, 50 insertions(+), 94 deletions(-) diff --git a/packages/client/src/execution/vmexecution.ts b/packages/client/src/execution/vmexecution.ts index 460b049a83..0bdf364195 100644 --- a/packages/client/src/execution/vmexecution.ts +++ b/packages/client/src/execution/vmexecution.ts @@ -576,7 +576,7 @@ export class VMExecution extends Execution { this.config.logger.warn( `Setting execution head to hash=${short(jumpToHash)} number=${jumpToNumber}`, ) - await this.vm.blockchain.setIteratorHead('vm', jumpToHash) + await this.chain.blockchain.setIteratorHead('vm', jumpToHash) }) } @@ -595,19 +595,9 @@ export class VMExecution extends Execution { this.running = true let numExecuted: number | null | undefined = undefined - const { blockchain } = this.vm - if (typeof blockchain.getIteratorHead !== 'function') { - throw new Error('cannot get iterator head: blockchain has no getIteratorHead function') - } - let startHeadBlock = await blockchain.getIteratorHead() + let startHeadBlock = await this.chain.blockchain.getIteratorHead() await this.checkAndReset(startHeadBlock) - - if (typeof blockchain.getCanonicalHeadBlock !== 'function') { - throw new Error( - 'cannot get iterator head: blockchain has no getCanonicalHeadBlock function', - ) - } - let canonicalHead = await blockchain.getCanonicalHeadBlock() + let canonicalHead = await this.chain.blockchain.getCanonicalHeadBlock() this.config.logger.debug( `Running execution startHeadBlock=${startHeadBlock?.header.number} canonicalHead=${canonicalHead?.header.number} loop=${loop}`, @@ -637,14 +627,14 @@ export class VMExecution extends Execution { headBlock = undefined parentState = undefined errorBlock = undefined - this.vmPromise = blockchain + this.vmPromise = this.chain.blockchain .iterator( 'vm', async (block: Block, reorg: boolean) => { // determine starting state for block run // if we are just starting or if a chain reorg has happened if (headBlock === undefined || reorg) { - headBlock = await blockchain.getBlock(block.header.parentHash) + headBlock = await this.chain.blockchain.getBlock(block.header.parentHash) parentState = headBlock.header.stateRoot if (reorg) { @@ -664,11 +654,6 @@ export class VMExecution extends Execution { // run block, update head if valid try { const { number, timestamp } = block.header - if (typeof blockchain.getTotalDifficulty !== 'function') { - throw new Error( - 'cannot get iterator head: blockchain has no getTotalDifficulty function', - ) - } const hardfork = this.config.execCommon.getHardforkBy({ blockNumber: number, @@ -800,7 +785,7 @@ export class VMExecution extends Execution { backStepToHash ?? 'na', )} hasParentStateRoot=${short(backStepToRoot ?? 'na')}:\n${error}`, ) - await this.vm.blockchain.setIteratorHead('vm', backStepToHash) + await this.chain.blockchain.setIteratorHead('vm', backStepToHash) } else { this.config.logger.error( `${errorMsg}, couldn't back step to vmHead number=${backStepTo} hash=${short( @@ -855,14 +840,7 @@ export class VMExecution extends Execution { numExecuted = await this.vmPromise if (numExecuted !== null) { - let endHeadBlock - if (typeof this.vm.blockchain.getIteratorHead === 'function') { - endHeadBlock = await this.vm.blockchain.getIteratorHead('vm') - } else { - throw new Error( - 'cannot get iterator head: blockchain has no getIteratorHead function', - ) - } + const endHeadBlock = await this.chain.blockchain.getIteratorHead('vm') if (typeof numExecuted === 'number' && numExecuted > 0) { const firstNumber = startHeadBlock.header.number @@ -891,12 +869,7 @@ export class VMExecution extends Execution { ) } startHeadBlock = endHeadBlock - if (typeof this.vm.blockchain.getCanonicalHeadBlock !== 'function') { - throw new Error( - 'cannot get iterator head: blockchain has no getCanonicalHeadBlock function', - ) - } - canonicalHead = await this.vm.blockchain.getCanonicalHeadBlock() + canonicalHead = await this.chain.blockchain.getCanonicalHeadBlock() } } @@ -917,19 +890,12 @@ export class VMExecution extends Execution { this.STATS_INTERVAL, ) - const { blockchain } = this.vm if (this.running || !this.started) { return false } - if (typeof blockchain.getIteratorHead !== 'function') { - throw new Error('cannot get iterator head: blockchain has no getIteratorHead function') - } - const vmHeadBlock = await blockchain.getIteratorHead() - if (typeof blockchain.getCanonicalHeadBlock !== 'function') { - throw new Error('cannot get iterator head: blockchain has no getCanonicalHeadBlock function') - } - const canonicalHead = await blockchain.getCanonicalHeadBlock() + const vmHeadBlock = await this.chain.blockchain.getIteratorHead() + const canonicalHead = await this.chain.blockchain.getCanonicalHeadBlock() const infoStr = `vmHead=${vmHeadBlock.header.number} canonicalHead=${ canonicalHead.header.number @@ -984,7 +950,7 @@ export class VMExecution extends Execution { async executeBlocks(first: number, last: number, txHashes: string[]) { this.config.logger.info('Preparing for block execution (debug mode, no services started)...') - const block = await this.vm.blockchain.getBlock(first) + const block = await this.chain.blockchain.getBlock(first) const startExecutionHardfork = this.config.execCommon.getHardforkBy({ blockNumber: block.header.number, timestamp: block.header.timestamp, @@ -1000,13 +966,10 @@ export class VMExecution extends Execution { const vm = await this.vm.shallowCopy(false) for (let blockNumber = first; blockNumber <= last; blockNumber++) { - const block = await vm.blockchain.getBlock(blockNumber) - const parentBlock = await vm.blockchain.getBlock(block.header.parentHash) + const block = await this.chain.blockchain.getBlock(blockNumber) + const parentBlock = await this.chain.blockchain.getBlock(block.header.parentHash) // Set the correct state root const root = parentBlock.header.stateRoot - if (typeof vm.blockchain.getTotalDifficulty !== 'function') { - throw new Error('cannot get iterator head: blockchain has no getTotalDifficulty function') - } vm.common.setHardforkBy({ blockNumber, timestamp: block.header.timestamp, diff --git a/packages/client/src/miner/miner.ts b/packages/client/src/miner/miner.ts index 271c92b92d..7e6e5124a2 100644 --- a/packages/client/src/miner/miner.ts +++ b/packages/client/src/miner/miner.ts @@ -12,7 +12,7 @@ import type { Config } from '../config.js' import type { VMExecution } from '../execution/index.js' import type { FullEthereumService } from '../service/index.js' import type { FullSynchronizer } from '../sync/index.js' -import type { CliqueConsensus } from '@ethereumjs/blockchain' +import type { Blockchain, CliqueConsensus } from '@ethereumjs/blockchain' import type { CliqueConfig } from '@ethereumjs/common' import type { Miner as EthashMiner, Solution } from '@ethereumjs/ethash' @@ -244,10 +244,9 @@ export class Miner { const [signerAddress, signerPrivKey] = this.config.accounts[0] cliqueSigner = signerPrivKey // Determine if signer is INTURN (2) or NOTURN (1) - inTurn = await (vmCopy.blockchain.consensus as CliqueConsensus).cliqueSignerInTurn( - signerAddress, - number, - ) + inTurn = await ( + (vmCopy.blockchain as Blockchain).consensus as CliqueConsensus + ).cliqueSignerInTurn(signerAddress, number) difficulty = inTurn ? 2 : 1 } diff --git a/packages/client/src/miner/pendingBlock.ts b/packages/client/src/miner/pendingBlock.ts index 4f901aea20..ce795435be 100644 --- a/packages/client/src/miner/pendingBlock.ts +++ b/packages/client/src/miner/pendingBlock.ts @@ -104,9 +104,6 @@ export class PendingBlock { const { timestamp, mixHash, parentBeaconBlockRoot, coinbase } = headerData let { gasLimit } = parentBlock.header - if (typeof vm.blockchain.getTotalDifficulty !== 'function') { - throw new Error('cannot get iterator head: blockchain has no getTotalDifficulty function') - } vm.common.setHardforkBy({ blockNumber: number, timestamp, diff --git a/packages/client/test/miner/miner.spec.ts b/packages/client/test/miner/miner.spec.ts index e00a129d3b..8579359021 100644 --- a/packages/client/test/miner/miner.spec.ts +++ b/packages/client/test/miner/miner.spec.ts @@ -22,7 +22,7 @@ import { wait } from '../integration/util.js' import type { FullSynchronizer } from '../../src/sync/index.js' import type { Block } from '@ethereumjs/block' -import type { CliqueConsensus } from '@ethereumjs/blockchain' +import type { Blockchain, CliqueConsensus } from '@ethereumjs/blockchain' import type { VM } from '@ethereumjs/vm' const A = { @@ -242,7 +242,9 @@ describe('assembleBlocks() -> with a single tx', async () => { await txPool.add(txA01) // disable consensus to skip PoA block signer validation - ;(vm.blockchain.consensus as CliqueConsensus).cliqueActiveSigners = () => [A.address] // stub + ;((vm.blockchain as Blockchain).consensus as CliqueConsensus).cliqueActiveSigners = () => [ + A.address, + ] // stub chain.putBlocks = (blocks: Block[]) => { it('should include tx in new block', () => { @@ -280,7 +282,9 @@ describe('assembleBlocks() -> with a hardfork mismatching tx', async () => { }) // disable consensus to skip PoA block signer validation - ;(vm.blockchain.consensus as CliqueConsensus).cliqueActiveSigners = () => [A.address] // stub + ;((vm.blockchain as Blockchain).consensus as CliqueConsensus).cliqueActiveSigners = () => [ + A.address, + ] // stub chain.putBlocks = (blocks: Block[]) => { it('should not include tx', () => { diff --git a/packages/client/test/miner/pendingBlock.spec.ts b/packages/client/test/miner/pendingBlock.spec.ts index 9008483750..74de0f0d9c 100644 --- a/packages/client/test/miner/pendingBlock.spec.ts +++ b/packages/client/test/miner/pendingBlock.spec.ts @@ -1,4 +1,5 @@ import { Block, BlockHeader, createBlockHeader } from '@ethereumjs/block' +import { createBlockchain } from '@ethereumjs/blockchain' import { Common, Goerli, Hardfork, Mainnet, createCommonFromGethGenesis } from '@ethereumjs/common' import { DefaultStateManager } from '@ethereumjs/statemanager' import { createBlob4844Tx, createFeeMarket1559Tx, createLegacyTx } from '@ethereumjs/tx' @@ -25,6 +26,7 @@ import { PendingBlock } from '../../src/miner/index.js' import { TxPool } from '../../src/service/txpool.js' import { mockBlockchain } from '../rpc/mockBlockchain.js' +import type { Blockchain } from '@ethereumjs/blockchain' import type { TypedTransaction } from '@ethereumjs/tx' const A = { @@ -124,14 +126,15 @@ describe('[PendingBlock]', async () => { it('should start and build', async () => { const { txPool } = setup() - const vm = await VM.create({ common }) + const blockchain = await createBlockchain({ common }) + const vm = await VM.create({ common, blockchain }) await setBalance(vm, A.address, BigInt(5000000000000000)) await setBalance(vm, B.address, BigInt(5000000000000000)) await txPool.add(txA01) await txPool.add(txA02) // skip hardfork validation for ease const pendingBlock = new PendingBlock({ config, txPool, skipHardForkValidation: true }) - const parentBlock = await vm.blockchain.getCanonicalHeadBlock!() + const parentBlock = await (vm.blockchain as Blockchain).getCanonicalHeadBlock!() const payloadId = await pendingBlock.start(vm, parentBlock) assert.equal(pendingBlock.pendingPayloads.size, 1, 'should set the pending payload') await txPool.add(txB01) @@ -151,7 +154,8 @@ describe('[PendingBlock]', async () => { it('should include txs with mismatching hardforks that can still be executed', async () => { const { txPool } = setup() - const vm = await VM.create({ common }) + const blockchain = await createBlockchain({ common }) + const vm = await VM.create({ common, blockchain }) await setBalance(vm, A.address, BigInt(5000000000000000)) await setBalance(vm, B.address, BigInt(5000000000000000)) @@ -160,7 +164,7 @@ describe('[PendingBlock]', async () => { assert.equal(txPool.txsInPool, 1, '1 txA011 should be added') // skip hardfork validation for ease const pendingBlock = new PendingBlock({ config, txPool }) - const parentBlock = await vm.blockchain.getCanonicalHeadBlock!() + const parentBlock = await (vm.blockchain as Blockchain).getCanonicalHeadBlock!() const payloadId = await pendingBlock.start(vm, parentBlock) assert.equal(pendingBlock.pendingPayloads.size, 1, 'should set the pending payload') const payload = pendingBlock.pendingPayloads.get(bytesToHex(payloadId)) @@ -199,9 +203,10 @@ describe('[PendingBlock]', async () => { const { txPool } = setup() await txPool.add(txA01) const pendingBlock = new PendingBlock({ config, txPool, skipHardForkValidation: true }) - const vm = await VM.create({ common }) + const blockchain = await createBlockchain({ common }) + const vm = await VM.create({ common, blockchain }) await setBalance(vm, A.address, BigInt(5000000000000000)) - const parentBlock = await vm.blockchain.getCanonicalHeadBlock!() + const parentBlock = await (vm.blockchain as Blockchain).getCanonicalHeadBlock!() const payloadId = await pendingBlock.start(vm, parentBlock) assert.equal(pendingBlock.pendingPayloads.size, 1, 'should set the pending payload') pendingBlock.stop(payloadId) @@ -219,7 +224,8 @@ describe('[PendingBlock]', async () => { const prevGasLimit = common['_chainParams'].genesis.gasLimit common['_chainParams'].genesis.gasLimit = 50000 - const vm = await VM.create({ common }) + const blockchain = await createBlockchain({ common }) + const vm = await VM.create({ common, blockchain }) await setBalance(vm, A.address, BigInt(5000000000000000)) // create alternate transactions with custom gas limits to @@ -242,7 +248,7 @@ describe('[PendingBlock]', async () => { await txPool.add(txA03) const pendingBlock = new PendingBlock({ config, txPool, skipHardForkValidation: true }) await setBalance(vm, A.address, BigInt(5000000000000000)) - const parentBlock = await vm.blockchain.getCanonicalHeadBlock!() + const parentBlock = await (vm.blockchain as Blockchain).getCanonicalHeadBlock!() const payloadId = await pendingBlock.start(vm, parentBlock) assert.equal(pendingBlock.pendingPayloads.size, 1, 'should set the pending payload') @@ -270,7 +276,8 @@ describe('[PendingBlock]', async () => { it('should skip adding txs when tx too big to fit', async () => { const { txPool } = setup() - const vm = await VM.create({ common }) + const blockchain = await createBlockchain({ common }) + const vm = await VM.create({ common, blockchain }) await setBalance(vm, A.address, BigInt(5000000000000000)) await txPool.add(txA01) await txPool.add(txA02) @@ -286,7 +293,7 @@ describe('[PendingBlock]', async () => { await txPool.add(txA03) const pendingBlock = new PendingBlock({ config, txPool, skipHardForkValidation: true }) await setBalance(vm, A.address, BigInt(5000000000000000)) - const parentBlock = await vm.blockchain.getCanonicalHeadBlock!() + const parentBlock = await (vm.blockchain as Blockchain).getCanonicalHeadBlock!() const payloadId = await pendingBlock.start(vm, parentBlock) assert.equal(pendingBlock.pendingPayloads.size, 1, 'should set the pending payload') const built = await pendingBlock.build(payloadId) @@ -311,8 +318,9 @@ describe('[PendingBlock]', async () => { const { txPool } = setup() await txPool.add(txA01) const pendingBlock = new PendingBlock({ config, txPool, skipHardForkValidation: true }) - const vm = await VM.create({ common }) - const parentBlock = await vm.blockchain.getCanonicalHeadBlock!() + const blockchain = await createBlockchain({ common }) + const vm = await VM.create({ common, blockchain }) + const parentBlock = await (vm.blockchain as Blockchain).getCanonicalHeadBlock!() const payloadId = await pendingBlock.start(vm, parentBlock) assert.equal(pendingBlock.pendingPayloads.size, 1, 'should set the pending payload') const built = await pendingBlock.build(payloadId) @@ -333,23 +341,6 @@ describe('[PendingBlock]', async () => { ) }) - it('should throw when blockchain does not have getTotalDifficulty function', async () => { - const { txPool } = setup() - const pendingBlock = new PendingBlock({ config, txPool, skipHardForkValidation: true }) - const vm = txPool['service'].execution.vm - // override total difficulty function to trigger error case - vm.blockchain.getTotalDifficulty = undefined - try { - await pendingBlock.start(vm, new Block()) - assert.fail('should have thrown') - } catch (err: any) { - assert.equal( - err.message, - 'cannot get iterator head: blockchain has no getTotalDifficulty function', - ) - } - }) - it('construct blob bundles', async () => { const kzg = await loadKZG() const common = createCommonFromGethGenesis(gethGenesis, { @@ -407,9 +398,10 @@ describe('[PendingBlock]', async () => { assert.equal(txPool.txsInPool, 4, '4 txs should still be in the pool') const pendingBlock = new PendingBlock({ config, txPool }) - const vm = await VM.create({ common }) + const blockchain = await createBlockchain({ common }) + const vm = await VM.create({ common, blockchain }) await setBalance(vm, A.address, BigInt(500000000000000000)) - const parentBlock = await vm.blockchain.getCanonicalHeadBlock!() + const parentBlock = await (vm.blockchain as Blockchain).getCanonicalHeadBlock!() // stub the vm's common set hf to do nothing but stay in cancun vm.common.setHardforkBy = () => { return vm.common.hardfork() @@ -466,9 +458,10 @@ describe('[PendingBlock]', async () => { assert.equal(txPool.txsInPool, 1, '1 txs should still be in the pool') const pendingBlock = new PendingBlock({ config, txPool }) - const vm = await VM.create({ common }) + const blockchain = await createBlockchain({ common }) + const vm = await VM.create({ common, blockchain }) await setBalance(vm, A.address, BigInt(500000000000000000)) - const parentBlock = await vm.blockchain.getCanonicalHeadBlock!() + const parentBlock = await (vm.blockchain as Blockchain).getCanonicalHeadBlock!() // stub the vm's common set hf to do nothing but stay in cancun vm.common.setHardforkBy = () => { return vm.common.hardfork()