Skip to content

Commit

Permalink
Merge pull request #4131 from NomicFoundation/bug/1568-negative-block…
Browse files Browse the repository at this point in the history
…-number-queried-when-forked-network-block-number-is-near-zero

Negative block number queried when forked network block number is near zero
  • Loading branch information
ChristopherDedominici authored Jul 13, 2023
2 parents d6ef40a + 092b771 commit 93363c0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
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

0 comments on commit 93363c0

Please sign in to comment.