Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@ethereumjs/common

Package Overview
Dependencies
Maintainers
3
Versions
34
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ethereumjs/common - npm Package Compare versions

Comparing version 3.0.0 to 3.0.1

dist/utils.d.ts

4

dist/chains/goerli.json

@@ -75,6 +75,6 @@ {

{
"//_comment": "The forkHash will remain same as mergeForkIdTransition is post merge",
"//_comment": "The forkHash will remain same as mergeForkIdTransition is post merge, terminal block: https://goerli.etherscan.io/block/7382818",
"name": "merge",
"ttd": "10790000",
"block": null,
"block": 7382819,
"forkHash": "0xb8c6299d"

@@ -81,0 +81,0 @@ },

@@ -91,9 +91,10 @@ {

{
"name": "mergeForkIdTransition",
"block": null,
"forkHash": null
"//_comment": "The forkHash will remain same as mergeForkIdTransition is post merge, terminal block: https://etherscan.io/block/15537393",
"name": "merge",
"ttd": "58750000000000000000000",
"block": 15537394,
"forkHash": "0xf0afd0e3"
},
{
"name": "merge",
"ttd": "58750000000000000000000",
"name": "mergeForkIdTransition",
"block": null,

@@ -100,0 +101,0 @@ "forkHash": null

@@ -77,6 +77,6 @@ {

{
"//_comment": "The forkHash will remain same as mergeForkIdTransition is post merge",
"//_comment": "The forkHash will remain same as mergeForkIdTransition is post merge, terminal block: https://sepolia.etherscan.io/block/1450408",
"name": "merge",
"ttd": "17000000000000000",
"block": null,
"block": 1450409,
"forkHash": "0xfe3366e7"

@@ -83,0 +83,0 @@ },

@@ -6,3 +6,3 @@ /// <reference types="node" />

import type { ConsensusAlgorithm, ConsensusType } from './enums';
import type { BootstrapNodeConfig, CasperConfig, ChainConfig, ChainsConfig, CliqueConfig, CommonOpts, CustomCommonOpts, EthashConfig, GenesisBlockConfig, HardforkConfig } from './types';
import type { BootstrapNodeConfig, CasperConfig, ChainConfig, ChainsConfig, CliqueConfig, CommonOpts, CustomCommonOpts, EthashConfig, GenesisBlockConfig, GethConfigOpts, HardforkConfig } from './types';
import type { BigIntLike } from '@ethereumjs/util';

@@ -23,2 +23,3 @@ /**

private _customChains;
private HARDFORK_CHANGES;
/**

@@ -50,2 +51,9 @@ * Creates a {@link Common} object for a custom chain, based on a standard one.

/**
* Static method to load and set common from a geth genesis json
* @param genesisJson json of geth configuration
* @param { chain, genesisHash, hardfork } to futher configure the common instance
* @returns Common
*/
static fromGethGenesis(genesisJson: any, { chain, genesisHash, hardfork }: GethConfigOpts): Common;
/**
* Static method to determine if a {@link chainId} is supported as a standard chain

@@ -79,3 +87,3 @@ * @param chainId bigint id (`1`) of a standard chain

* @param blockNumber
* @param td
* @param td : total difficulty of the parent block (for block hf) OR of the the chain latest (for chain hf)
* @returns The name of the HF

@@ -241,2 +249,8 @@ */

/**
* Sets any missing forkHashes on the passed-in {@link Common} instance
* @param common The {@link Common} to set the forkHashes for
* @param genesisHash The genesis block hash
*/
setForkHashes(genesisHash: Buffer): void;
/**
* Returns the Genesis parameters of the current chain

@@ -243,0 +257,0 @@ * @returns Genesis dictionary

@@ -15,2 +15,3 @@ "use strict";

const hardforks_1 = require("./hardforks");
const utils_1 = require("./utils");
/**

@@ -31,2 +32,7 @@ * Common class to access chain and hardfork parameters and to provide

this.DEFAULT_HARDFORK = this._chainParams.defaultHardfork ?? enums_1.Hardfork.Merge;
// Assign hardfork changes in the sequence of the applied hardforks
this.HARDFORK_CHANGES = this.hardforks().map((hf) => [
hf.name,
hardforks_1.hardforks[hf.name],
]);
this._hardfork = this.DEFAULT_HARDFORK;

@@ -128,2 +134,20 @@ if (opts.hardfork !== undefined) {

/**
* Static method to load and set common from a geth genesis json
* @param genesisJson json of geth configuration
* @param { chain, genesisHash, hardfork } to futher configure the common instance
* @returns Common
*/
static fromGethGenesis(genesisJson, { chain, genesisHash, hardfork }) {
const genesisParams = (0, utils_1.parseGethGenesis)(genesisJson, chain);
const common = new Common({
chain: genesisParams.name ?? 'custom',
customChains: [genesisParams],
hardfork,
});
if (genesisHash !== undefined) {
common.setForkHashes(genesisHash);
}
return common;
}
/**
* Static method to determine if a {@link chainId} is supported as a standard chain

@@ -190,3 +214,3 @@ * @param chainId bigint id (`1`) of a standard chain

let existing = false;
for (const hfChanges of hardforks_1.hardforks) {
for (const hfChanges of this.HARDFORK_CHANGES) {
if (hfChanges[0] === hardfork) {

@@ -213,3 +237,3 @@ if (this._hardfork !== hardfork) {

* @param blockNumber
* @param td
* @param td : total difficulty of the parent block (for block hf) OR of the the chain latest (for chain hf)
* @returns The name of the HF

@@ -220,47 +244,52 @@ */

td = (0, util_1.toType)(td, util_1.TypeOutput.BigInt);
let hardfork = enums_1.Hardfork.Chainstart;
let minTdHF;
let maxTdHF;
let previousHF;
for (const hf of this.hardforks()) {
// Skip comparison for not applied HFs
if (hf.block === null) {
if (td !== undefined && td !== null && hf.ttd !== undefined && hf.ttd !== null) {
if (td >= BigInt(hf.ttd)) {
return hf.name;
}
}
continue;
// Filter out hardforks with no block number and no ttd (i.e. unapplied hardforks)
const hfs = this.hardforks().filter((hf) => hf.block !== null || (hf.ttd !== null && hf.ttd !== undefined));
const mergeIndex = hfs.findIndex((hf) => hf.ttd !== null && hf.ttd !== undefined);
const doubleTTDHF = hfs
.slice(mergeIndex + 1)
.findIndex((hf) => hf.ttd !== null && hf.ttd !== undefined);
if (doubleTTDHF >= 0) {
throw Error(`More than one merge hardforks found with ttd specified`);
}
// Find the first hardfork that has a block number greater than `blockNumber` (skips the merge hardfork since
// it cannot have a block number specified).
let hfIndex = hfs.findIndex((hf) => hf.block !== null && hf.block > blockNumber);
// Move hfIndex one back to arrive at candidate hardfork
if (hfIndex === -1) {
// all hardforks apply, set hfIndex to the last one as thats the candidate
hfIndex = hfs.length - 1;
}
else if (hfIndex === 0) {
// cannot have a case where a block number is before all applied hardforks
// since the chain has to start with a hardfork
throw Error('Must have at least one hardfork at block 0');
}
else {
// The previous hardfork is the candidate here
hfIndex = hfIndex - 1;
}
let hardfork;
if (hfs[hfIndex].block === null) {
// We're on the merge hardfork. Let's check the TTD
if (td === undefined || td === null || BigInt(hfs[hfIndex].ttd) > td) {
// Merge ttd greater than current td so we're on hardfork before merge
hardfork = hfs[hfIndex - 1];
}
if (blockNumber >= BigInt(hf.block)) {
hardfork = hf.name;
else {
// Merge ttd equal or less than current td so we're on merge hardfork
hardfork = hfs[hfIndex];
}
if (td && (typeof hf.ttd === 'string' || typeof hf.ttd === 'bigint')) {
if (td >= BigInt(hf.ttd)) {
minTdHF = hf.name;
}
else {
maxTdHF = previousHF;
}
}
previousHF = hf.name;
}
if (td) {
let msgAdd = `block number: ${blockNumber} (-> ${hardfork}), `;
if (minTdHF !== undefined) {
if (!this.hardforkGteHardfork(hardfork, minTdHF)) {
const msg = 'HF determined by block number is lower than the minimum total difficulty HF';
msgAdd += `total difficulty: ${td} (-> ${minTdHF})`;
throw new Error(`${msg}: ${msgAdd}`);
else {
if (mergeIndex >= 0 && td !== undefined && td !== null) {
if (hfIndex >= mergeIndex && BigInt(hfs[mergeIndex].ttd) > td) {
throw Error('Maximum HF determined by total difficulty is lower than the block number HF');
}
}
if (maxTdHF !== undefined) {
if (!this.hardforkGteHardfork(maxTdHF, hardfork)) {
const msg = 'Maximum HF determined by total difficulty is lower than the block number HF';
msgAdd += `total difficulty: ${td} (-> ${maxTdHF})`;
throw new Error(`${msg}: ${msgAdd}`);
else if (hfIndex < mergeIndex && BigInt(hfs[mergeIndex].ttd) <= td) {
throw Error('HF determined by block number is lower than the minimum total difficulty HF');
}
}
hardfork = hfs[hfIndex];
}
return hardfork;
return hardfork.name;
}

@@ -351,3 +380,3 @@ /**

let value = null;
for (const hfChanges of hardforks_1.hardforks) {
for (const hfChanges of this.HARDFORK_CHANGES) {
// EIP-referencing HF file (e.g. berlin.json)

@@ -422,3 +451,3 @@ if ('eips' in hfChanges[1]) {

}
for (const hfChanges of hardforks_1.hardforks) {
for (const hfChanges of this.HARDFORK_CHANGES) {
const hf = hfChanges[1];

@@ -504,3 +533,3 @@ if (this.gteHardfork(hf['name']) && 'eips' in hf) {

eipBlock(eip) {
for (const hfChanges of hardforks_1.hardforks) {
for (const hfChanges of this.HARDFORK_CHANGES) {
const hf = hfChanges[1];

@@ -548,3 +577,13 @@ if ('eips' in hf) {

hardfork = hardfork ?? this._hardfork;
const hfBlock = this.hardforkBlock(hardfork);
let hfBlock = this.hardforkBlock(hardfork);
// If this is a merge hardfork with block not set, then we fallback to previous hardfork
// to find the nextHardforkBlock
if (hfBlock === null && hardfork === enums_1.Hardfork.Merge) {
const hfs = this.hardforks();
const mergeIndex = hfs.findIndex((hf) => hf.ttd !== null && hf.ttd !== undefined);
if (mergeIndex < 0) {
throw Error(`Merge hardfork should have been found`);
}
hfBlock = this.hardforkBlock(hfs[mergeIndex - 1].name);
}
if (hfBlock === null) {

@@ -558,3 +597,5 @@ return null;

const nextHfBlock = this.hardforks().reduce((acc, hf) => {
const block = BigInt(typeof hf.block !== 'number' ? 0 : hf.block);
// We need to ignore the merge block in our next hardfork calc
const block = BigInt(hf.block === null || (hf.ttd !== undefined && hf.ttd !== null) ? 0 : hf.block);
// Typescript can't seem to follow that the hfBlock is not null at this point
return block > hfBlock && acc === null ? block : acc;

@@ -586,6 +627,10 @@ }, null);

for (const hf of this.hardforks()) {
const block = hf.block;
const { block, ttd } = hf;
// Skip for chainstart (0), not applied HFs (null) and
// when already applied on same block number HFs
if (typeof block === 'number' && block !== 0 && block !== prevBlock) {
// and on the merge since forkhash doesn't change on merge hf
if (typeof block === 'number' &&
block !== 0 &&
block !== prevBlock &&
(ttd === null || ttd === undefined)) {
const hfBlockBuffer = Buffer.from(block.toString(16).padStart(16, '0'), 'hex');

@@ -637,2 +682,16 @@ hfBuffer = Buffer.concat([hfBuffer, hfBlockBuffer]);

/**
* Sets any missing forkHashes on the passed-in {@link Common} instance
* @param common The {@link Common} to set the forkHashes for
* @param genesisHash The genesis block hash
*/
setForkHashes(genesisHash) {
for (const hf of this.hardforks()) {
if ((hf.forkHash === null || hf.forkHash === undefined) &&
typeof hf.block !== 'undefined' &&
(hf.block !== null || typeof hf.ttd !== 'undefined')) {
hf.forkHash = this.forkHash(hf.name, genesisHash);
}
}
}
/**
* Returns the Genesis parameters of the current chain

@@ -709,3 +768,3 @@ * @returns Genesis dictionary

let value;
for (const hfChanges of hardforks_1.hardforks) {
for (const hfChanges of this.HARDFORK_CHANGES) {
if ('consensus' in hfChanges[1]) {

@@ -731,3 +790,3 @@ value = hfChanges[1]['consensus']['type'];

let value;
for (const hfChanges of hardforks_1.hardforks) {
for (const hfChanges of this.HARDFORK_CHANGES) {
if ('consensus' in hfChanges[1]) {

@@ -758,3 +817,3 @@ value = hfChanges[1]['consensus']['algorithm'];

let value;
for (const hfChanges of hardforks_1.hardforks) {
for (const hfChanges of this.HARDFORK_CHANGES) {
if ('consensus' in hfChanges[1]) {

@@ -761,0 +820,0 @@ // The config parameter is named after the respective consensus algorithm

@@ -6,3 +6,3 @@ {

"url": "https://eips.ethereum.org/EIPS/eip-3675",
"status": "Review",
"status": "Final",
"minimumHardfork": "london",

@@ -9,0 +9,0 @@ "requiredEIPs": [],

@@ -1,2 +0,20 @@

export declare const hardforks: any[][];
export declare const hardforks: {
chainstart: any;
homestead: any;
dao: any;
tangerineWhistle: any;
spuriousDragon: any;
byzantium: any;
constantinople: any;
petersburg: any;
istanbul: any;
muirGlacier: any;
berlin: any;
london: any;
shanghai: any;
arrowGlacier: any;
grayGlacier: any;
mergeForkIdTransition: any;
merge: any;
};
//# sourceMappingURL=index.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.hardforks = void 0;
exports.hardforks = [
['chainstart', require('./chainstart.json')],
['homestead', require('./homestead.json')],
['dao', require('./dao.json')],
['tangerineWhistle', require('./tangerineWhistle.json')],
['spuriousDragon', require('./spuriousDragon.json')],
['byzantium', require('./byzantium.json')],
['constantinople', require('./constantinople.json')],
['petersburg', require('./petersburg.json')],
['istanbul', require('./istanbul.json')],
['muirGlacier', require('./muirGlacier.json')],
['berlin', require('./berlin.json')],
['london', require('./london.json')],
['shanghai', require('./shanghai.json')],
['arrowGlacier', require('./arrowGlacier.json')],
['grayGlacier', require('./grayGlacier.json')],
['mergeForkIdTransition', require('./mergeForkIdTransition.json')],
['merge', require('./merge.json')],
];
exports.hardforks = {
chainstart: require('./chainstart.json'),
homestead: require('./homestead.json'),
dao: require('./dao.json'),
tangerineWhistle: require('./tangerineWhistle.json'),
spuriousDragon: require('./spuriousDragon.json'),
byzantium: require('./byzantium.json'),
constantinople: require('./constantinople.json'),
petersburg: require('./petersburg.json'),
istanbul: require('./istanbul.json'),
muirGlacier: require('./muirGlacier.json'),
berlin: require('./berlin.json'),
london: require('./london.json'),
shanghai: require('./shanghai.json'),
arrowGlacier: require('./arrowGlacier.json'),
grayGlacier: require('./grayGlacier.json'),
mergeForkIdTransition: require('./mergeForkIdTransition.json'),
merge: require('./merge.json'),
};
//# sourceMappingURL=index.js.map
export * from './common';
export * from './enums';
export * from './types';
export * from './utils';
//# sourceMappingURL=index.d.ts.map

@@ -20,2 +20,3 @@ "use strict";

__exportStar(require("./types"), exports);
__exportStar(require("./utils"), exports);
//# sourceMappingURL=index.js.map

@@ -0,1 +1,2 @@

/// <reference types="node" />
import type { Chain, ConsensusAlgorithm, ConsensusType, Hardfork } from './enums';

@@ -106,3 +107,8 @@ export interface ChainName {

}
export interface GethConfigOpts {
chain?: string;
hardfork?: string | Hardfork;
genesisHash?: Buffer;
}
export {};
//# sourceMappingURL=types.d.ts.map
{
"name": "@ethereumjs/common",
"version": "3.0.0",
"version": "3.0.1",
"description": "Resources common to all Ethereum implementations",

@@ -47,3 +47,3 @@ "keywords": [

"test:browser": "karma start karma.conf.js",
"test:node": "npm run tape -- ./tests/*.spec.ts",
"test:node": "npm run tape -- ./test/*.spec.ts",
"tsc": "../../config/cli/ts-compile.sh"

@@ -50,0 +50,0 @@ },

@@ -198,2 +198,18 @@ # @ethereumjs/common

#### Initialize using Geth's genesis json
For lots of custom chains (for e.g. devnets and testnets), you might come across a genesis json config which
has both config specification for the chain as well as the genesis state specification. You can derive the
common from such configuration in the following manner:
```typescript
import { Common } from '@ethereumjs/common'
// Load geth genesis json file into lets say `genesisJson` and optional `chain` and `genesisHash`
const common = Common.fromGethGenesis(genesisJson, { chain: 'customChain', genesisHash })
// If you don't have `genesisHash` while initiating common, you can later configure common (for e.g.
// post calculating it via `blockchain`)
common.setForkHashes(genesisHash)
```
### Hardforks

@@ -200,0 +216,0 @@

@@ -75,6 +75,6 @@ {

{
"//_comment": "The forkHash will remain same as mergeForkIdTransition is post merge",
"//_comment": "The forkHash will remain same as mergeForkIdTransition is post merge, terminal block: https://goerli.etherscan.io/block/7382818",
"name": "merge",
"ttd": "10790000",
"block": null,
"block": 7382819,
"forkHash": "0xb8c6299d"

@@ -81,0 +81,0 @@ },

@@ -91,9 +91,10 @@ {

{
"name": "mergeForkIdTransition",
"block": null,
"forkHash": null
"//_comment": "The forkHash will remain same as mergeForkIdTransition is post merge, terminal block: https://etherscan.io/block/15537393",
"name": "merge",
"ttd": "58750000000000000000000",
"block": 15537394,
"forkHash": "0xf0afd0e3"
},
{
"name": "merge",
"ttd": "58750000000000000000000",
"name": "mergeForkIdTransition",
"block": null,

@@ -100,0 +101,0 @@ "forkHash": null

@@ -77,6 +77,6 @@ {

{
"//_comment": "The forkHash will remain same as mergeForkIdTransition is post merge",
"//_comment": "The forkHash will remain same as mergeForkIdTransition is post merge, terminal block: https://sepolia.etherscan.io/block/1450408",
"name": "merge",
"ttd": "17000000000000000",
"block": null,
"block": 1450409,
"forkHash": "0xfe3366e7"

@@ -83,0 +83,0 @@ },

@@ -12,3 +12,4 @@ import { TypeOutput, intToBuffer, toType } from '@ethereumjs/util'

import { Chain, CustomChain, Hardfork } from './enums'
import { hardforks as HARDFORK_CHANGES } from './hardforks'
import { hardforks as HARDFORK_SPECS } from './hardforks'
import { parseGethGenesis } from './utils'

@@ -27,2 +28,3 @@ import type { ConsensusAlgorithm, ConsensusType } from './enums'

GenesisBlockConfig,
GethConfigOpts,
HardforkConfig,

@@ -32,2 +34,4 @@ } from './types'

type HardforkSpecKeys = keyof typeof HARDFORK_SPECS
type HardforkSpecValues = typeof HARDFORK_SPECS[HardforkSpecKeys]
/**

@@ -49,2 +53,4 @@ * Common class to access chain and hardfork parameters and to provide

private HARDFORK_CHANGES: [HardforkSpecKeys, HardforkSpecValues][]
/**

@@ -160,2 +166,24 @@ * Creates a {@link Common} object for a custom chain, based on a standard one.

/**
* Static method to load and set common from a geth genesis json
* @param genesisJson json of geth configuration
* @param { chain, genesisHash, hardfork } to futher configure the common instance
* @returns Common
*/
static fromGethGenesis(
genesisJson: any,
{ chain, genesisHash, hardfork }: GethConfigOpts
): Common {
const genesisParams = parseGethGenesis(genesisJson, chain)
const common = new Common({
chain: genesisParams.name ?? 'custom',
customChains: [genesisParams],
hardfork,
})
if (genesisHash !== undefined) {
common.setForkHashes(genesisHash)
}
return common
}
/**
* Static method to determine if a {@link chainId} is supported as a standard chain

@@ -198,2 +226,7 @@ * @param chainId bigint id (`1`) of a standard chain

this.DEFAULT_HARDFORK = this._chainParams.defaultHardfork ?? Hardfork.Merge
// Assign hardfork changes in the sequence of the applied hardforks
this.HARDFORK_CHANGES = this.hardforks().map((hf) => [
hf.name as HardforkSpecKeys,
HARDFORK_SPECS[hf.name as HardforkSpecKeys],
])
this._hardfork = this.DEFAULT_HARDFORK

@@ -247,3 +280,3 @@ if (opts.hardfork !== undefined) {

let existing = false
for (const hfChanges of HARDFORK_CHANGES) {
for (const hfChanges of this.HARDFORK_CHANGES) {
if (hfChanges[0] === hardfork) {

@@ -271,3 +304,3 @@ if (this._hardfork !== hardfork) {

* @param blockNumber
* @param td
* @param td : total difficulty of the parent block (for block hf) OR of the the chain latest (for chain hf)
* @returns The name of the HF

@@ -279,46 +312,52 @@ */

let hardfork = Hardfork.Chainstart
let minTdHF
let maxTdHF
let previousHF
for (const hf of this.hardforks()) {
// Skip comparison for not applied HFs
if (hf.block === null) {
if (td !== undefined && td !== null && hf.ttd !== undefined && hf.ttd !== null) {
if (td >= BigInt(hf.ttd)) {
return hf.name
}
}
continue
}
if (blockNumber >= BigInt(hf.block)) {
hardfork = hf.name as Hardfork
}
if (td && (typeof hf.ttd === 'string' || typeof hf.ttd === 'bigint')) {
if (td >= BigInt(hf.ttd)) {
minTdHF = hf.name
} else {
maxTdHF = previousHF
}
}
previousHF = hf.name
// Filter out hardforks with no block number and no ttd (i.e. unapplied hardforks)
const hfs = this.hardforks().filter(
(hf) => hf.block !== null || (hf.ttd !== null && hf.ttd !== undefined)
)
const mergeIndex = hfs.findIndex((hf) => hf.ttd !== null && hf.ttd !== undefined)
const doubleTTDHF = hfs
.slice(mergeIndex + 1)
.findIndex((hf) => hf.ttd !== null && hf.ttd !== undefined)
if (doubleTTDHF >= 0) {
throw Error(`More than one merge hardforks found with ttd specified`)
}
if (td) {
let msgAdd = `block number: ${blockNumber} (-> ${hardfork}), `
if (minTdHF !== undefined) {
if (!this.hardforkGteHardfork(hardfork, minTdHF)) {
const msg = 'HF determined by block number is lower than the minimum total difficulty HF'
msgAdd += `total difficulty: ${td} (-> ${minTdHF})`
throw new Error(`${msg}: ${msgAdd}`)
}
// Find the first hardfork that has a block number greater than `blockNumber` (skips the merge hardfork since
// it cannot have a block number specified).
let hfIndex = hfs.findIndex((hf) => hf.block !== null && hf.block > blockNumber)
// Move hfIndex one back to arrive at candidate hardfork
if (hfIndex === -1) {
// all hardforks apply, set hfIndex to the last one as thats the candidate
hfIndex = hfs.length - 1
} else if (hfIndex === 0) {
// cannot have a case where a block number is before all applied hardforks
// since the chain has to start with a hardfork
throw Error('Must have at least one hardfork at block 0')
} else {
// The previous hardfork is the candidate here
hfIndex = hfIndex - 1
}
let hardfork
if (hfs[hfIndex].block === null) {
// We're on the merge hardfork. Let's check the TTD
if (td === undefined || td === null || BigInt(hfs[hfIndex].ttd!) > td) {
// Merge ttd greater than current td so we're on hardfork before merge
hardfork = hfs[hfIndex - 1]
} else {
// Merge ttd equal or less than current td so we're on merge hardfork
hardfork = hfs[hfIndex]
}
if (maxTdHF !== undefined) {
if (!this.hardforkGteHardfork(maxTdHF, hardfork)) {
const msg = 'Maximum HF determined by total difficulty is lower than the block number HF'
msgAdd += `total difficulty: ${td} (-> ${maxTdHF})`
throw new Error(`${msg}: ${msgAdd}`)
} else {
if (mergeIndex >= 0 && td !== undefined && td !== null) {
if (hfIndex >= mergeIndex && BigInt(hfs[mergeIndex].ttd!) > td) {
throw Error('Maximum HF determined by total difficulty is lower than the block number HF')
} else if (hfIndex < mergeIndex && BigInt(hfs[mergeIndex].ttd!) <= td) {
throw Error('HF determined by block number is lower than the minimum total difficulty HF')
}
}
hardfork = hfs[hfIndex]
}
return hardfork
return hardfork.name
}

@@ -414,3 +453,3 @@

let value = null
for (const hfChanges of HARDFORK_CHANGES) {
for (const hfChanges of this.HARDFORK_CHANGES) {
// EIP-referencing HF file (e.g. berlin.json)

@@ -487,3 +526,3 @@ if ('eips' in hfChanges[1]) {

}
for (const hfChanges of HARDFORK_CHANGES) {
for (const hfChanges of this.HARDFORK_CHANGES) {
const hf = hfChanges[1]

@@ -575,3 +614,3 @@ if (this.gteHardfork(hf['name']) && 'eips' in hf) {

eipBlock(eip: number): bigint | null {
for (const hfChanges of HARDFORK_CHANGES) {
for (const hfChanges of this.HARDFORK_CHANGES) {
const hf = hfChanges[1]

@@ -622,3 +661,13 @@ if ('eips' in hf) {

hardfork = hardfork ?? this._hardfork
const hfBlock = this.hardforkBlock(hardfork)
let hfBlock = this.hardforkBlock(hardfork)
// If this is a merge hardfork with block not set, then we fallback to previous hardfork
// to find the nextHardforkBlock
if (hfBlock === null && hardfork === Hardfork.Merge) {
const hfs = this.hardforks()
const mergeIndex = hfs.findIndex((hf) => hf.ttd !== null && hf.ttd !== undefined)
if (mergeIndex < 0) {
throw Error(`Merge hardfork should have been found`)
}
hfBlock = this.hardforkBlock(hfs[mergeIndex - 1].name)
}
if (hfBlock === null) {

@@ -632,4 +681,8 @@ return null

const nextHfBlock = this.hardforks().reduce((acc: bigint | null, hf: HardforkConfig) => {
const block = BigInt(typeof hf.block !== 'number' ? 0 : hf.block)
return block > hfBlock && acc === null ? block : acc
// We need to ignore the merge block in our next hardfork calc
const block = BigInt(
hf.block === null || (hf.ttd !== undefined && hf.ttd !== null) ? 0 : hf.block
)
// Typescript can't seem to follow that the hfBlock is not null at this point
return block > hfBlock! && acc === null ? block : acc
}, null)

@@ -663,7 +716,13 @@ return nextHfBlock

for (const hf of this.hardforks()) {
const block = hf.block
const { block, ttd } = hf
// Skip for chainstart (0), not applied HFs (null) and
// when already applied on same block number HFs
if (typeof block === 'number' && block !== 0 && block !== prevBlock) {
// and on the merge since forkhash doesn't change on merge hf
if (
typeof block === 'number' &&
block !== 0 &&
block !== prevBlock &&
(ttd === null || ttd === undefined)
) {
const hfBlockBuffer = Buffer.from(block.toString(16).padStart(16, '0'), 'hex')

@@ -718,2 +777,19 @@ hfBuffer = Buffer.concat([hfBuffer, hfBlockBuffer])

/**
* Sets any missing forkHashes on the passed-in {@link Common} instance
* @param common The {@link Common} to set the forkHashes for
* @param genesisHash The genesis block hash
*/
setForkHashes(genesisHash: Buffer) {
for (const hf of this.hardforks()) {
if (
(hf.forkHash === null || hf.forkHash === undefined) &&
typeof hf.block !== 'undefined' &&
(hf.block !== null || typeof hf.ttd !== 'undefined')
) {
hf.forkHash = this.forkHash(hf.name, genesisHash)
}
}
}
/**
* Returns the Genesis parameters of the current chain

@@ -800,3 +876,3 @@ * @returns Genesis dictionary

let value
for (const hfChanges of HARDFORK_CHANGES) {
for (const hfChanges of this.HARDFORK_CHANGES) {
if ('consensus' in hfChanges[1]) {

@@ -823,3 +899,3 @@ value = hfChanges[1]['consensus']['type']

let value
for (const hfChanges of HARDFORK_CHANGES) {
for (const hfChanges of this.HARDFORK_CHANGES) {
if ('consensus' in hfChanges[1]) {

@@ -851,3 +927,3 @@ value = hfChanges[1]['consensus']['algorithm']

let value
for (const hfChanges of HARDFORK_CHANGES) {
for (const hfChanges of this.HARDFORK_CHANGES) {
if ('consensus' in hfChanges[1]) {

@@ -854,0 +930,0 @@ // The config parameter is named after the respective consensus algorithm

@@ -6,3 +6,3 @@ {

"url": "https://eips.ethereum.org/EIPS/eip-3675",
"status": "Review",
"status": "Final",
"minimumHardfork": "london",

@@ -9,0 +9,0 @@ "requiredEIPs": [],

@@ -1,19 +0,19 @@

export const hardforks = [
['chainstart', require('./chainstart.json')],
['homestead', require('./homestead.json')],
['dao', require('./dao.json')],
['tangerineWhistle', require('./tangerineWhistle.json')],
['spuriousDragon', require('./spuriousDragon.json')],
['byzantium', require('./byzantium.json')],
['constantinople', require('./constantinople.json')],
['petersburg', require('./petersburg.json')],
['istanbul', require('./istanbul.json')],
['muirGlacier', require('./muirGlacier.json')],
['berlin', require('./berlin.json')],
['london', require('./london.json')],
['shanghai', require('./shanghai.json')],
['arrowGlacier', require('./arrowGlacier.json')],
['grayGlacier', require('./grayGlacier.json')],
['mergeForkIdTransition', require('./mergeForkIdTransition.json')],
['merge', require('./merge.json')],
]
export const hardforks = {
chainstart: require('./chainstart.json'),
homestead: require('./homestead.json'),
dao: require('./dao.json'),
tangerineWhistle: require('./tangerineWhistle.json'),
spuriousDragon: require('./spuriousDragon.json'),
byzantium: require('./byzantium.json'),
constantinople: require('./constantinople.json'),
petersburg: require('./petersburg.json'),
istanbul: require('./istanbul.json'),
muirGlacier: require('./muirGlacier.json'),
berlin: require('./berlin.json'),
london: require('./london.json'),
shanghai: require('./shanghai.json'),
arrowGlacier: require('./arrowGlacier.json'),
grayGlacier: require('./grayGlacier.json'),
mergeForkIdTransition: require('./mergeForkIdTransition.json'),
merge: require('./merge.json'),
}
export * from './common'
export * from './enums'
export * from './types'
export * from './utils'

@@ -116,1 +116,7 @@ import type { Chain, ConsensusAlgorithm, ConsensusType, Hardfork } from './enums'

}
export interface GethConfigOpts {
chain?: string
hardfork?: string | Hardfork
genesisHash?: Buffer
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc