Skip to content

Commit

Permalink
allforks: Allow genesis to be post merge
Browse files Browse the repository at this point in the history
  • Loading branch information
g11tech committed Feb 14, 2023
1 parent 5cfa4e7 commit 28083fb
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 26 deletions.
22 changes: 12 additions & 10 deletions packages/block/src/header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ export class BlockHeader {
* @throws if any check fails
*/
_consensusFormatValidation() {
const { nonce, uncleHash, difficulty, extraData } = this
const { nonce, uncleHash, difficulty, extraData, number } = this
const hardfork = this._common.hardfork()

// Consensus type dependent checks
Expand Down Expand Up @@ -404,15 +404,17 @@ export class BlockHeader {
)} (expected: ${KECCAK256_RLP_ARRAY.toString('hex')})`
error = true
}
if (difficulty !== BigInt(0)) {
errorMsg += `, difficulty: ${difficulty} (expected: 0)`
error = true
}
if (extraData.length > 32) {
errorMsg += `, extraData: ${extraData.toString(
'hex'
)} (cannot exceed 32 bytes length, received ${extraData.length} bytes)`
error = true
if (number !== BigInt(0)) {
if (difficulty !== BigInt(0)) {
errorMsg += `, difficulty: ${difficulty} (expected: 0)`
error = true
}
if (extraData.length > 32) {
errorMsg += `, extraData: ${extraData.toString(
'hex'
)} (cannot exceed 32 bytes length, received ${extraData.length} bytes)`
error = true
}
}
if (!nonce.equals(zeros(8))) {
errorMsg += `, nonce: ${nonce.toString('hex')} (expected: ${zeros(8).toString('hex')})`
Expand Down
6 changes: 5 additions & 1 deletion packages/blockchain/src/blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1256,7 +1256,11 @@ export class Blockchain implements BlockchainInterface {
*/
createGenesisBlock(stateRoot: Buffer): Block {
const common = this._common.copy()
common.setHardforkByBlockNumber(0, undefined, common.genesis().timestamp)
common.setHardforkByBlockNumber(
0,
BigInt(common.genesis().difficulty),
common.genesis().timestamp
)

const header: BlockData['header'] = {
...common.genesis(),
Expand Down
4 changes: 2 additions & 2 deletions packages/blockchain/src/db/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export class DBManager {
const blockData = [header.raw(), ...body] as BlockBuffer
const opts: BlockOptions = { common: this._common }
if (number === BigInt(0)) {
opts.hardforkByBlockNumber = true
opts.hardforkByTTD = await this.getTotalDifficulty(hash, BigInt(0))
} else {
opts.hardforkByTTD = await this.getTotalDifficulty(header.parentHash, number - BigInt(1))
}
Expand All @@ -143,7 +143,7 @@ export class DBManager {
const encodedHeader = await this.get(DBTarget.Header, { blockHash, blockNumber })
const opts: BlockOptions = { common: this._common }
if (blockNumber === BigInt(0)) {
opts.hardforkByBlockNumber = true
opts.hardforkByTTD = await this.getTotalDifficulty(blockHash, BigInt(0))
} else {
const parentHash = await this.numberToHash(blockNumber - BigInt(1))
opts.hardforkByTTD = await this.getTotalDifficulty(parentHash, blockNumber - BigInt(1))
Expand Down
15 changes: 2 additions & 13 deletions packages/common/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,20 +169,9 @@ function parseGethParams(json: any, mergeForkIdPostMerge: boolean = true) {
block: null,
}

// If any of the genesis block require merge, then we need merge just right after genesis
const isMergeJustPostGenesis: boolean = params.hardforks
.filter((hf: ConfigHardfork) => hf.block === 0)
.reduce(
(acc: boolean, hf: ConfigHardfork) => acc || forkMap[hf.name]?.postMerge === true,
false
)

// Merge hardfork has to be placed before first non-zero block hardfork that is dependent
// on merge or first non zero block hardfork if any of genesis hardforks require merge
// Merge hardfork has to be placed before first hardfork that is dependent on merge
const postMergeIndex = params.hardforks.findIndex(
(hf: any) =>
(isMergeJustPostGenesis || forkMap[hf.name]?.postMerge === true) &&
(hf.block > 0 || (hf.timestamp ?? 0) > 0)
(hf: any) => forkMap[hf.name]?.postMerge === true
)
if (postMergeIndex !== -1) {
params.hardforks.splice(postMergeIndex, 0, mergeConfig as unknown as ConfigHardfork)
Expand Down

0 comments on commit 28083fb

Please sign in to comment.