Skip to content

Commit

Permalink
fix the interface and add pass timestamp param
Browse files Browse the repository at this point in the history
  • Loading branch information
g11tech committed Dec 11, 2022
1 parent 75f08c3 commit 74ebde3
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 23 deletions.
6 changes: 4 additions & 2 deletions packages/block/src/header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,9 @@ export class BlockHeader {
const number = toType(headerData.number, TypeOutput.BigInt) ?? defaults.number
const gasLimit = toType(headerData.gasLimit, TypeOutput.BigInt) ?? defaults.gasLimit
const gasUsed = toType(headerData.gasUsed, TypeOutput.BigInt) ?? defaults.gasUsed
const timestamp = toType(headerData.timestamp, TypeOutput.BigInt) ?? defaults.timestamp

const inputTimeStamp = toType(headerData.timestamp, TypeOutput.BigInt)
const timestamp = inputTimeStamp ?? defaults.timestamp
const extraData = toType(headerData.extraData, TypeOutput.Buffer) ?? defaults.extraData
const mixHash = toType(headerData.mixHash, TypeOutput.Buffer) ?? defaults.mixHash
const nonce = toType(headerData.nonce, TypeOutput.Buffer) ?? defaults.nonce
Expand All @@ -183,7 +185,7 @@ export class BlockHeader {

const hardforkByBlockNumber = options.hardforkByBlockNumber ?? false
if (hardforkByBlockNumber || options.hardforkByTTD !== undefined) {
this._common.setHardforkByBlockNumber(number, options.hardforkByTTD)
this._common.setHardforkByBlockNumber(number, options.hardforkByTTD, inputTimeStamp)
}

if (this._common.isActivatedEIP(1559) === true) {
Expand Down
10 changes: 7 additions & 3 deletions packages/blockchain/src/blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ export class Blockchain implements BlockchainInterface {
if (this._hardforkByHeadBlockNumber) {
const latestHeader = await this._getHeader(this._headHeaderHash)
const td = await this.getTotalDifficulty(this._headHeaderHash)
this.checkAndTransitionHardForkByNumber(latestHeader.number, td)
this.checkAndTransitionHardForkByNumber(latestHeader.number, td, latestHeader.timestamp)
}

this._isInitialized = true
Expand Down Expand Up @@ -1172,8 +1172,12 @@ export class Blockchain implements BlockchainInterface {
return this.dbManager.getHeader(hash, number)
}

protected checkAndTransitionHardForkByNumber(number: bigint, td?: BigIntLike): void {
this._common.setHardforkByBlockNumber(number, td)
protected checkAndTransitionHardForkByNumber(
number: bigint,
td?: BigIntLike,
timestamp?: BigIntLike
): void {
this._common.setHardforkByBlockNumber(number, td, timestamp)

// If custom consensus algorithm is used, skip merge hardfork consensus checks
if (!Object.values(ConsensusAlgorithm).includes(this.consensus.algorithm as ConsensusAlgorithm))
Expand Down
6 changes: 5 additions & 1 deletion packages/client/lib/blockchain/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,11 @@ export class Chain {
this._headers = headers
this._blocks = blocks

this.config.chainCommon.setHardforkByBlockNumber(headers.latest.number, headers.td)
this.config.chainCommon.setHardforkByBlockNumber(
headers.latest.number,
headers.td,
headers.latest.timestamp
)

if (emit) {
this.config.events.emit(Event.CHAIN_UPDATED)
Expand Down
10 changes: 5 additions & 5 deletions packages/client/lib/execution/vmexecution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ export class VMExecution extends Execution {
throw new Error('cannot get iterator head: blockchain has no getIteratorHead function')
}
const headBlock = await this.vm.blockchain.getIteratorHead()
const { number } = headBlock.header
const { number, timestamp } = headBlock.header
if (typeof this.vm.blockchain.getTotalDifficulty !== 'function') {
throw new Error('cannot get iterator head: blockchain has no getTotalDifficulty function')
}
const td = await this.vm.blockchain.getTotalDifficulty(headBlock.header.hash())
this.config.execCommon.setHardforkByBlockNumber(number, td)
this.config.execCommon.setHardforkByBlockNumber(number, td, timestamp)
this.hardfork = this.config.execCommon.hardfork()
this.config.logger.info(`Initializing VM execution hardfork=${this.hardfork}`)
if (number === BigInt(0)) {
Expand Down Expand Up @@ -232,7 +232,7 @@ export class VMExecution extends Execution {
}
// run block, update head if valid
try {
const { number } = block.header
const { number, timestamp } = block.header
if (typeof blockchain.getTotalDifficulty !== 'function') {
throw new Error(
'cannot get iterator head: blockchain has no getTotalDifficulty function'
Expand All @@ -246,7 +246,7 @@ export class VMExecution extends Execution {
this.config.logger.info(
`Execution hardfork switch on block number=${number} hash=${hash} old=${this.hardfork} new=${hardfork}`
)
this.hardfork = this.config.execCommon.setHardforkByBlockNumber(number, td)
this.hardfork = this.config.execCommon.setHardforkByBlockNumber(number, td, timestamp)
}
let skipBlockValidation = false
if (this.config.execCommon.consensusType() === ConsensusType.ProofOfAuthority) {
Expand Down Expand Up @@ -413,7 +413,7 @@ export class VMExecution extends Execution {
throw new Error('cannot get iterator head: blockchain has no getTotalDifficulty function')
}
const td = await vm.blockchain.getTotalDifficulty(block.header.parentHash)
vm._common.setHardforkByBlockNumber(blockNumber, td)
vm._common.setHardforkByBlockNumber(blockNumber, td, block.header.timestamp)

if (txHashes.length === 0) {
// we are skipping header validation because the block has been picked from the
Expand Down
4 changes: 3 additions & 1 deletion packages/client/lib/net/protocol/ethprotocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ export class EthProtocol extends Protocol {
this.chain.headers.latest?.number ?? // Use latest header number if available OR
this.config.syncTargetHeight ?? // Use sync target height if available OR
common.hardforkBlock(common.hardfork()) ?? // Use current hardfork block number OR
BigInt(0) // Use chainstart
BigInt(0), // Use chainstart,
undefined,
this.chain.headers.latest?.timestamp ?? Math.floor(Date.now() / 1000)
)
return txs.map((txData) => TransactionFactory.fromSerializedData(txData, { common }))
},
Expand Down
3 changes: 2 additions & 1 deletion packages/client/lib/rpc/modules/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,13 +229,14 @@ const assembleBlock = async (
feeRecipient: coinbase,
transactions,
withdrawals: withdrawalsData,
timestamp,
} = payload
const { config } = chain
const common = config.chainCommon.copy()

// This is a post merge block, so set its common accordingly
const ttd = common.hardforkTTD(Hardfork.Merge)
common.setHardforkByBlockNumber(number, ttd !== null ? ttd : undefined)
common.setHardforkByBlockNumber(number, ttd !== null ? ttd : undefined, timestamp)

const txs = []
for (const [index, serializedTx] of transactions.entries()) {
Expand Down
2 changes: 1 addition & 1 deletion packages/client/lib/rpc/modules/eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,7 @@ export class Eth {
// Set the tx common to an appropriate HF to create a tx
// with matching HF rules
if (typeof syncTargetHeight === 'bigint' && syncTargetHeight !== BigInt(0)) {
common.setHardforkByBlockNumber(syncTargetHeight)
common.setHardforkByBlockNumber(syncTargetHeight, undefined, Math.floor(Date.now() / 1000))
}

let tx
Expand Down
3 changes: 2 additions & 1 deletion packages/client/lib/sync/beaconsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ export class BeaconSynchronizer extends Synchronizer {
const { height: number, td } = this.chain.blocks
const hash = this.chain.blocks.latest!.hash()
this.startingBlock = number
this.config.chainCommon.setHardforkByBlockNumber(number, td)
const timestamp = this.chain.blocks.latest?.header.timestamp
this.config.chainCommon.setHardforkByBlockNumber(number, td, timestamp)

this.config.logger.info(
`Latest local block number=${Number(number)} td=${td} hash=${hash.toString(
Expand Down
3 changes: 2 additions & 1 deletion packages/client/lib/sync/fullsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ export class FullSynchronizer extends Synchronizer {
const { height: number, td } = this.chain.blocks
const hash = this.chain.blocks.latest!.hash()
this.startingBlock = number
this.config.chainCommon.setHardforkByBlockNumber(number, td)
const timestamp = this.chain.blocks.latest?.header.timestamp
this.config.chainCommon.setHardforkByBlockNumber(number, td, timestamp)

this.config.logger.info(
`Latest local block number=${Number(number)} td=${td} hash=${short(
Expand Down
13 changes: 8 additions & 5 deletions packages/common/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,8 @@ export class Common extends EventEmitter {
*/
getHardforkByBlockNumber(
blockNumber: BigIntLike,
{ td, timestamp }: { td?: BigIntLike; timestamp?: BigIntLike } = {}
td?: BigIntLike,
timestamp?: BigIntLike
): string {
blockNumber = toType(blockNumber, TypeOutput.BigInt)
td = toType(td, TypeOutput.BigInt)
Expand Down Expand Up @@ -403,9 +404,10 @@ export class Common extends EventEmitter {
*/
setHardforkByBlockNumber(
blockNumber: BigIntLike,
{ td, timestamp }: { td?: BigIntLike; timestamp?: BigIntLike } = {}
td?: BigIntLike,
timestamp?: BigIntLike
): string {
const hardfork = this.getHardforkByBlockNumber(blockNumber, { td, timestamp })
const hardfork = this.getHardforkByBlockNumber(blockNumber, td, timestamp)
this.setHardfork(hardfork)
return hardfork
}
Expand Down Expand Up @@ -538,9 +540,10 @@ export class Common extends EventEmitter {
topic: string,
name: string,
blockNumber: BigIntLike,
{ td, timestamp }: { td?: BigIntLike; timestamp?: BigIntLike }
td?: BigIntLike,
timestamp?: BigIntLike
): bigint {
const hardfork = this.getHardforkByBlockNumber(blockNumber, { td, timestamp })
const hardfork = this.getHardforkByBlockNumber(blockNumber, td, timestamp)
return this.paramByHardfork(topic, name, hardfork)
}

Expand Down
3 changes: 2 additions & 1 deletion packages/vm/src/runBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ export async function runBlock(this: VM, opts: RunBlockOpts): Promise<RunBlockRe
) {
this._common.setHardforkByBlockNumber(
block.header.number,
opts.hardforkByTTD ?? this._hardforkByTTD
opts.hardforkByTTD ?? this._hardforkByTTD,
block.header.timestamp
)
}

Expand Down
4 changes: 3 additions & 1 deletion packages/vm/test/tester/runners/BlockchainTestsRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,16 @@ export async function runBlockchainTest(options: any, testData: any, t: tape.Tes
const blockRlp = Buffer.from((raw.rlp as string).slice(2), 'hex')
// Update common HF
let TD: bigint | undefined = undefined
let timestamp: bigint | undefined = undefined
try {
const decoded: any = RLP.decode(blockRlp)
const parentHash = decoded[0][0]
TD = await blockchain.getTotalDifficulty(parentHash)
timestamp = bufferToBigInt(decoded[0][11])
// eslint-disable-next-line no-empty
} catch (e) {}

common.setHardforkByBlockNumber(currentBlock, TD)
common.setHardforkByBlockNumber(currentBlock, TD, timestamp)

// transactionSequence is provided when txs are expected to be rejected.
// To run this field we try to import them on the current state.
Expand Down

0 comments on commit 74ebde3

Please sign in to comment.