Skip to content

Commit 22b5e1f

Browse files
authored
fix: lazy init global edr context (#4505)
1 parent 08f7b42 commit 22b5e1f

File tree

6 files changed

+27
-32
lines changed

6 files changed

+27
-32
lines changed

Diff for: packages/hardhat-core/src/internal/hardhat-network/provider/context/edr.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,17 @@ import { RandomBufferGenerator } from "../utils/random";
2323

2424
export const UNLIMITED_CONTRACT_SIZE_VALUE = 2n ** 64n - 1n;
2525

26-
// Only one is allowed to exist
27-
export const globalEdrContext = new EdrContext();
26+
let _globalEdrContext: EdrContext | undefined;
27+
28+
// Lazy initialize the global EDR context.
29+
export function getGlobalEdrContext(): EdrContext {
30+
if (_globalEdrContext === undefined) {
31+
// Only one is allowed to exist
32+
_globalEdrContext = new EdrContext();
33+
}
34+
35+
return _globalEdrContext;
36+
}
2837

2938
export class EdrEthContext implements EthContextAdapter {
3039
constructor(
@@ -65,7 +74,7 @@ export class EdrEthContext implements EthContextAdapter {
6574

6675
blockchain = new EdrBlockchain(
6776
await Blockchain.fork(
68-
globalEdrContext,
77+
getGlobalEdrContext(),
6978
specId,
7079
config.forkConfig.jsonRpcUrl,
7180
config.forkConfig.blockNumber !== undefined
@@ -91,7 +100,7 @@ export class EdrEthContext implements EthContextAdapter {
91100
config.forkConfig.blockNumber = Number(latestBlockNumber);
92101
} else {
93102
state = EdrStateManager.withGenesisAccounts(
94-
globalEdrContext,
103+
getGlobalEdrContext(),
95104
config.genesisAccounts
96105
);
97106

Diff for: packages/hardhat-core/src/internal/hardhat-network/provider/miner/dual.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { MinimalInterpreterStep } from "../vm/proxy-vm";
33

44
import { Address } from "@nomicfoundation/ethereumjs-util";
55
import { keccak256 } from "../../../util/keccak";
6-
import { globalEdrContext } from "../context/edr";
6+
import { getGlobalEdrContext } from "../context/edr";
77
import { randomHashSeed } from "../fork/ForkStateManager";
88
import { BlockMinerAdapter, PartialMineBlockResult } from "../miner";
99
import {
@@ -46,7 +46,7 @@ export class DualBlockMiner implements BlockMinerAdapter {
4646
nextStateRootSeed = keccak256(stateRootSeed);
4747
}
4848

49-
globalEdrContext.setStateRootGeneratorSeed(stateRootSeed);
49+
getGlobalEdrContext().setStateRootGeneratorSeed(stateRootSeed);
5050

5151
const edrResult = await this._edrMiner.mineBlock(
5252
blockTimestamp,
@@ -67,7 +67,7 @@ export class DualBlockMiner implements BlockMinerAdapter {
6767
return edrResult;
6868
} catch (error) {
6969
// Ensure that the state root generator seed is re-aligned upon an error
70-
globalEdrContext.setStateRootGeneratorSeed(randomHashSeed());
70+
getGlobalEdrContext().setStateRootGeneratorSeed(randomHashSeed());
7171

7272
// eslint-disable-next-line @nomicfoundation/hardhat-internal-rules/only-hardhat-error
7373
throw error;

Diff for: packages/hardhat-core/src/internal/hardhat-network/provider/vm/block-builder/dual.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
assertEqualRunTxResults,
88
} from "../../utils/assertions";
99
import { randomHashSeed } from "../../fork/ForkStateManager";
10-
import { globalEdrContext } from "../../context/edr";
10+
import { getGlobalEdrContext } from "../../context/edr";
1111

1212
export class DualModeBlockBuilder implements BlockBuilderAdapter {
1313
constructor(
@@ -20,7 +20,7 @@ export class DualModeBlockBuilder implements BlockBuilderAdapter {
2020
const edrResult = await this._edrBuilder.addTransaction(tx);
2121

2222
// Matches EthereumJS' runCall checkpoint call
23-
globalEdrContext.setStateRootGeneratorSeed(randomHashSeed());
23+
getGlobalEdrContext().setStateRootGeneratorSeed(randomHashSeed());
2424

2525
assertEqualRunTxResults(ethereumJSResult, edrResult);
2626

Diff for: packages/hardhat-core/src/internal/hardhat-network/provider/vm/dual.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
import { opcodeName } from "../../stack-traces/opcodes";
2424
import { VMTracer } from "../../stack-traces/vm-tracer";
2525
import { RpcDebugTraceOutput } from "../output";
26-
import { globalEdrContext } from "../context/edr";
26+
import { getGlobalEdrContext } from "../context/edr";
2727
import { randomHashSeed } from "../fork/ForkStateManager";
2828
import { assertEqualRunTxResults } from "../utils/assertions";
2929

@@ -88,13 +88,13 @@ export class DualModeAdapter implements VMAdapter {
8888
]);
8989

9090
// Matches EthereumJS' runCall checkpoint call
91-
globalEdrContext.setStateRootGeneratorSeed(randomHashSeed());
91+
getGlobalEdrContext().setStateRootGeneratorSeed(randomHashSeed());
9292

9393
assertEqualRunTxResults(ethereumJSResult, edrResult);
9494
return edrResult;
9595
} catch (error) {
9696
// Ensure that the state root generator seed is re-aligned upon an error
97-
globalEdrContext.setStateRootGeneratorSeed(randomHashSeed());
97+
getGlobalEdrContext().setStateRootGeneratorSeed(randomHashSeed());
9898

9999
throw error;
100100
}
@@ -218,7 +218,7 @@ export class DualModeAdapter implements VMAdapter {
218218
config
219219
);
220220

221-
globalEdrContext.setStateRootGeneratorSeed(randomHashSeed());
221+
getGlobalEdrContext().setStateRootGeneratorSeed(randomHashSeed());
222222

223223
return edrResult;
224224
}
@@ -256,7 +256,7 @@ export class DualModeAdapter implements VMAdapter {
256256
]);
257257

258258
// Matches EthereumJS' runCall checkpoint call
259-
globalEdrContext.setStateRootGeneratorSeed(randomHashSeed());
259+
getGlobalEdrContext().setStateRootGeneratorSeed(randomHashSeed());
260260

261261
assertEqualRunTxResults(ethereumJSResult, edrResult);
262262

@@ -266,7 +266,7 @@ export class DualModeAdapter implements VMAdapter {
266266
return edrResult;
267267
} catch (error) {
268268
// Ensure that the state root generator seed is re-aligned upon an error
269-
globalEdrContext.setStateRootGeneratorSeed(randomHashSeed());
269+
getGlobalEdrContext().setStateRootGeneratorSeed(randomHashSeed());
270270

271271
throw error;
272272
}

Diff for: packages/hardhat-core/src/internal/hardhat-network/provider/vm/edr.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ import { MessageTrace } from "../../stack-traces/message-trace";
5454
import { VMTracer } from "../../stack-traces/vm-tracer";
5555

5656
import {
57-
globalEdrContext,
57+
getGlobalEdrContext,
5858
UNLIMITED_CONTRACT_SIZE_VALUE,
5959
} from "../context/edr";
6060
import { RunTxResult, VMAdapter } from "./vm-adapter";
@@ -97,13 +97,13 @@ export class EdrAdapter implements VMAdapter {
9797

9898
if (isForkedNodeConfig(config)) {
9999
state = await EdrStateManager.forkRemote(
100-
globalEdrContext,
100+
getGlobalEdrContext(),
101101
config.forkConfig,
102102
config.genesisAccounts
103103
);
104104
} else {
105105
state = EdrStateManager.withGenesisAccounts(
106-
globalEdrContext,
106+
getGlobalEdrContext(),
107107
config.genesisAccounts
108108
);
109109
}

Diff for: yarn.lock

-14
Original file line numberDiff line numberDiff line change
@@ -1346,11 +1346,6 @@
13461346
resolved "https://registry.yarnpkg.com/@nomicfoundation/ethereumjs-rlp/-/ethereumjs-rlp-4.0.3.tgz#8d9147fbd0d49e8f4c5ce729d226694a8fe03eb8"
13471347
integrity sha512-DZMzB/lqPK78T6MluyXqtlRmOMcsZbTTbbEyAjo0ncaff2mqu/k8a79PBcyvpgAhWD/R59Fjq/x3ro5Lof0AtA==
13481348

1349-
"@nomicfoundation/[email protected]":
1350-
version "5.0.2"
1351-
resolved "https://registry.yarnpkg.com/@nomicfoundation/ethereumjs-rlp/-/ethereumjs-rlp-5.0.2.tgz#4fee8dc58a53ac6ae87fb1fca7c15dc06c6b5dea"
1352-
integrity sha512-QwmemBc+MMsHJ1P1QvPl8R8p2aPvvVcKBbvHnQOKBpBztEo0omN0eaob6FeZS/e3y9NSe+mfu3nNFBHszqkjTA==
1353-
13541349
"@nomicfoundation/[email protected]":
13551350
version "5.0.3"
13561351
resolved "https://registry.yarnpkg.com/@nomicfoundation/ethereumjs-rlp/-/ethereumjs-rlp-5.0.3.tgz#9431390cee638f0dd2c8cdbce824cb5b575c1f74"
@@ -1391,15 +1386,6 @@
13911386
"@nomicfoundation/ethereumjs-util" "9.0.3"
13921387
ethereum-cryptography "0.1.3"
13931388

1394-
"@nomicfoundation/[email protected]":
1395-
version "9.0.2"
1396-
resolved "https://registry.yarnpkg.com/@nomicfoundation/ethereumjs-util/-/ethereumjs-util-9.0.2.tgz#16bdc1bb36f333b8a3559bbb4b17dac805ce904d"
1397-
integrity sha512-4Wu9D3LykbSBWZo8nJCnzVIYGvGCuyiYLIJa9XXNVt1q1jUzHdB+sJvx95VGCpPkCT+IbLecW6yfzy3E1bQrwQ==
1398-
dependencies:
1399-
"@chainsafe/ssz" "^0.10.0"
1400-
"@nomicfoundation/ethereumjs-rlp" "5.0.2"
1401-
ethereum-cryptography "0.1.3"
1402-
14031389
"@nomicfoundation/[email protected]":
14041390
version "9.0.3"
14051391
resolved "https://registry.yarnpkg.com/@nomicfoundation/ethereumjs-util/-/ethereumjs-util-9.0.3.tgz#2f87783b0224f47c131d57f1eecc518afeb01bb2"

0 commit comments

Comments
 (0)