Skip to content

Commit

Permalink
Merge pull request #960 from lidofinance/fix/forking
Browse files Browse the repository at this point in the history
fix: forking URL interference with unit tests
  • Loading branch information
tamtamchik authored Feb 24, 2025
2 parents 337f9ac + fe9f677 commit e2b5dfa
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ MAINNET_RPC_URL=http://localhost:8545

# RPC URL for Hardhat Network forking, required for running tests on mainnet fork with tracing (Infura, Alchemy, etc.)
# https://hardhat.org/hardhat-network/docs/guides/forking-other-networks#forking-other-networks
HARDHAT_FORKING_URL=https://eth.drpc.org
FORK_RPC_URL=https://eth.drpc.org

# https://docs.lido.fi/deployed-contracts
MAINNET_LOCATOR_ADDRESS=0xC1d0b3DE6792Bf6b4b37EccdcC24e45978Cfd2Eb
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ This is the most common method for running integration tests. It uses an instanc
mainnet environment, allowing you to run integration tests with trace logging.
> [!NOTE]
> Ensure that `HARDHAT_FORKING_URL` is set to Ethereum Mainnet RPC and `MAINNET_*` environment variables are set in the
> Ensure that `FORK_RPC_URL` is set to Ethereum Mainnet RPC and `MAINNET_*` environment variables are set in the
> `.env` file (refer to `.env.example` for guidance). Otherwise, the tests will run against the Scratch deployment.
```bash
Expand Down
12 changes: 6 additions & 6 deletions globals.d.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
declare namespace NodeJS {
export interface ProcessEnv {
/* iternal logging verbosity (used in scratch deploy / integration tests) */
/* internal logging verbosity (used in scratch deploy / integration tests) */
LOG_LEVEL?: "all" | "debug" | "info" | "warn" | "error" | "none"; // default: "info"

/**
* Flags for changing the behavior of the Hardhat Network
*/

/* RPC URL for Hardhat Network forking, required for running tests on mainnet fork with tracing */
HARDHAT_FORKING_URL?: string;
/* Test execution mode: 'scratch' for fresh network, 'fork' for forked network */
MODE?: "scratch" | "forking"; // default: "scratch"

/* URL of the network to fork from */
FORK_RPC_URL?: string; // default: "https://eth.drpc.org"

/**
* Flags for changing the behavior of the integration tests
*/

/* if "on" the integration tests will deploy the contracts to the empty Hardhat Network node using scratch deploy */
INTEGRATION_ON_SCRATCH?: "on" | "off"; // default: "off"

/* if "on" the integration tests will assume CSM module is present in the StakingRouter, and adjust accordingly */
INTEGRATION_WITH_CSM?: "on" | "off"; // default: "off"

Expand Down
22 changes: 14 additions & 8 deletions hardhat.helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@ import { existsSync, readFileSync } from "node:fs";

/* Determines the forking configuration for Hardhat */
export function getHardhatForkingConfig() {
const forkingUrl = process.env.HARDHAT_FORKING_URL || "";
const mode = process.env.MODE || "scratch";

if (!forkingUrl) {
// Scratch deploy, need to disable CSM
process.env.INTEGRATION_ON_SCRATCH = "on";
process.env.INTEGRATION_WITH_CSM = "off";
return undefined;
}
switch (mode) {
case "scratch":
process.env.INTEGRATION_WITH_CSM = "off";
return undefined;

case "forking":
if (!process.env.FORK_RPC_URL) {
throw new Error("FORK_RPC_URL must be set when MODE=forking");
}
return { url: process.env.FORK_RPC_URL };

return { url: forkingUrl };
default:
throw new Error("MODE must be either 'scratch' or 'forking'");
}
}

// TODO: this plaintext accounts.json private keys management is a subject
Expand Down
11 changes: 6 additions & 5 deletions lib/protocol/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import hre from "hardhat";
import { deployScratchProtocol, deployUpgrade, ether, findEventsWithInterfaces, impersonate, log } from "lib";

import { discover } from "./discover";
import { isNonForkingHardhatNetwork } from "./networks";
import { provision } from "./provision";
import { ProtocolContext, ProtocolContextFlags, ProtocolSigners, Signer } from "./types";

Expand All @@ -14,8 +13,11 @@ const getSigner = async (signer: Signer, balance = ether("100"), signers: Protoc
};

export const getProtocolContext = async (): Promise<ProtocolContext> => {
if (isNonForkingHardhatNetwork()) {
await deployScratchProtocol(hre.network.name);
if (hre.network.name === "hardhat") {
const networkConfig = hre.config.networks[hre.network.name];
if (!networkConfig.forking?.enabled) {
await deployScratchProtocol(hre.network.name);
}
} else {
await deployUpgrade(hre.network.name);
}
Expand All @@ -25,12 +27,11 @@ export const getProtocolContext = async (): Promise<ProtocolContext> => {

// By default, all flags are "on"
const flags = {
onScratch: process.env.INTEGRATION_ON_SCRATCH === "on",
withCSM: process.env.INTEGRATION_WITH_CSM !== "off",
} as ProtocolContextFlags;

log.debug("Protocol context flags", {
"On scratch": flags.onScratch,
"With CSM": flags.withCSM,
});

const context = {
Expand Down
1 change: 0 additions & 1 deletion lib/protocol/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ export type ProtocolSigners = {
export type Signer = keyof ProtocolSigners;

export type ProtocolContextFlags = {
onScratch: boolean;
withCSM: boolean;
};

Expand Down
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
"test:trace": "hardhat test test/**/*.test.ts --trace --disabletracer",
"test:fulltrace": "hardhat test test/**/*.test.ts --fulltrace --disabletracer",
"test:watch": "hardhat watch",
"test:integration": "hardhat test test/integration/**/*.ts",
"test:integration:trace": "hardhat test test/integration/**/*.ts --trace --disabletracer",
"test:integration:fulltrace": "hardhat test test/integration/**/*.ts --fulltrace --disabletracer",
"test:integration:scratch": "HARDHAT_FORKING_URL= INTEGRATION_WITH_CSM=off INTEGRATION_WITH_SCRATCH_DEPLOY=on hardhat test test/integration/**/*.ts",
"test:integration:scratch:trace": "HARDHAT_FORKING_URL= INTEGRATION_WITH_CSM=off INTEGRATION_WITH_SCRATCH_DEPLOY=on hardhat test test/integration/**/*.ts --trace --disabletracer",
"test:integration:scratch:fulltrace": "HARDHAT_FORKING_URL= INTEGRATION_WITH_CSM=off INTEGRATION_WITH_SCRATCH_DEPLOY=on hardhat test test/integration/**/*.ts --fulltrace --disabletracer",
"test:integration:fork:local": "hardhat test test/integration/**/*.ts --network local",
"test:integration:fork:mainnet": "hardhat test test/integration/**/*.ts --network mainnet-fork",
"test:integration:fork:mainnet:custom": "hardhat test --network mainnet-fork",
"test:integration": "MODE=forking hardhat test test/integration/**/*.ts",
"test:integration:trace": "MODE=forking hardhat test test/integration/**/*.ts --trace --disabletracer",
"test:integration:fulltrace": "MODE=forking hardhat test test/integration/**/*.ts --fulltrace --disabletracer",
"test:integration:scratch": "MODE=scratch hardhat test test/integration/**/*.ts",
"test:integration:scratch:trace": "MODE=scratch hardhat test test/integration/**/*.ts --trace --disabletracer",
"test:integration:scratch:fulltrace": "MODE=scratch hardhat test test/integration/**/*.ts --fulltrace --disabletracer",
"test:integration:fork:local": "MODE=scratch hardhat test test/integration/**/*.ts --network local",
"test:integration:fork:mainnet": "MODE=forking hardhat test test/integration/**/*.ts --network mainnet-fork",
"test:integration:fork:mainnet:custom": "MODE=forking hardhat test --network mainnet-fork",
"typecheck": "tsc --noEmit",
"prepare": "husky",
"abis:extract": "hardhat abis:extract",
Expand Down
2 changes: 1 addition & 1 deletion tasks/verify-contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ task("verify:deployed", "Verifies deployed contracts based on state file").setAc
async function verifyContract(contract: DeployedContract, hre: HardhatRuntimeEnvironment) {
if (!contract.contract) {
// TODO: In the case of state processing on the local devnet there are skips, we need to find the cause
return
return;
}
const contractName = contract.contract.split("/").pop()?.split(".")[0];
const verificationParams = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ describe("ValidatorsExitBusOracle.sol:submitReportData", () => {
});

it("SUBMIT_DATA_ROLE is allowed", async () => {
oracle.grantRole(await oracle.SUBMIT_DATA_ROLE(), stranger, { from: admin });
await oracle.grantRole(await oracle.SUBMIT_DATA_ROLE(), stranger, { from: admin });
await consensus.advanceTimeToNextFrameStart();
const { reportData } = await prepareReportAndSubmitHash();
await oracle.connect(stranger).submitReportData(reportData, oracleVersion);
Expand Down

0 comments on commit e2b5dfa

Please sign in to comment.