Skip to content

Commit

Permalink
Adjust/fix some client tests
Browse files Browse the repository at this point in the history
  • Loading branch information
holgerd77 committed Aug 17, 2024
1 parent 86dcb21 commit 01e6266
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 94 deletions.
63 changes: 13 additions & 50 deletions packages/client/src/execution/vmexecution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}

Expand All @@ -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}`,
Expand Down Expand Up @@ -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) {
Expand All @@ -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,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
}

Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
9 changes: 4 additions & 5 deletions packages/client/src/miner/miner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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
}

Expand Down
3 changes: 0 additions & 3 deletions packages/client/src/miner/pendingBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 7 additions & 3 deletions packages/client/test/miner/miner.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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', () => {
Expand Down
59 changes: 26 additions & 33 deletions packages/client/test/miner/pendingBlock.spec.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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 = {
Expand Down Expand Up @@ -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)
Expand All @@ -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))

Expand All @@ -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))
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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')

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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, {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 01e6266

Please sign in to comment.