Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Negative block number queried when forked network block number is near zero #4131

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/healthy-poems-talk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"hardhat": patch
---
Added logic to use the latest block as the forking block if the difference between the latest block and the max reorganization block is negative.
This decision is based on the assumption that if the max reorganization block is greater than the latest block then there is a high probability that the fork is occurring on a devnet.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export async function makeForkClient(
const maxReorg = actualMaxReorg ?? FALLBACK_MAX_REORG;

const latestBlock = await getLatestBlockNumber(provider);
const lastSafeBlock = latestBlock - maxReorg;
const lastSafeBlock = getLastSafeBlock(latestBlock, maxReorg);

let forkBlockNumber;
if (forkConfig.blockNumber !== undefined) {
Expand Down Expand Up @@ -127,3 +127,12 @@ async function getLatestBlockNumber(provider: HttpProvider) {
const latestBlock = BigInt(latestBlockString);
return latestBlock;
}

export function getLastSafeBlock(
latestBlock: bigint,
maxReorg: bigint
): bigint {
// Design choice: if latestBlock - maxReorg results in a negative number then the latestBlock block will be used.
// This decision is based on the assumption that if maxReorg > latestBlock then there is a high probability that the fork is occurring on a devnet.
return latestBlock - maxReorg >= 0 ? latestBlock - maxReorg : latestBlock;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import { assert } from "chai";
import { JsonRpcClient } from "../../../../../src/internal/hardhat-network/jsonrpc/client";
import { ForkBlockchain } from "../../../../../src/internal/hardhat-network/provider/fork/ForkBlockchain";
import { randomHashBuffer } from "../../../../../src/internal/hardhat-network/provider/utils/random";
import { makeForkClient } from "../../../../../src/internal/hardhat-network/provider/utils/makeForkClient";
import {
makeForkClient,
getLastSafeBlock,
} from "../../../../../src/internal/hardhat-network/provider/utils/makeForkClient";
import { ALCHEMY_URL } from "../../../../setup";
import {
createTestLog,
Expand Down Expand Up @@ -66,6 +69,17 @@ describe("ForkBlockchain", () => {
assert.instanceOf(fb, ForkBlockchain);
});

describe("getLastSafeBlock", () => {
it("should return a safe block that is the difference between the latestBlock and maxReorg", () => {
assert.strictEqual(getLastSafeBlock(100n, 50n), 50n);
assert.strictEqual(getLastSafeBlock(100n, 100n), 0n); // 0 is a valid fork block number
});

it("should return latestBlock as fork block because the difference between the latestBlock and maxReorg is < 0", () => {
assert.strictEqual(getLastSafeBlock(20n, 200n), 20n);
});
});

describe("getBlock", () => {
it("can get remote block object by block number", async () => {
const block = await fb.getBlock(BLOCK_NUMBER_OF_10496585);
Expand Down