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 3 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
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,11 @@ async function getLatestBlockNumber(provider: HttpProvider) {
const latestBlock = BigInt(latestBlockString);
return latestBlock;
}

export function getLastSafeBlock(
latestBlock: bigint,
maxReorg: bigint
): bigint {
// Design choice: if latestBlock - maxReorg gives a negative number than the newest block will be used
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's explain that we do this because the forked network is almost surely a devnet

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

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,18 @@ describe("ForkBlockchain", () => {
assert.instanceOf(fb, ForkBlockchain);
});

// TODO: remove regex code
ChristopherDedominici marked this conversation as resolved.
Show resolved Hide resolved
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