Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit 7ee7d4b

Browse files
adjisbAndres AdjimannMicaiahReiddavidmurdoch
authored
fix: set the earliest block tag on startup when forking (#3755)
Co-authored-by: Andres Adjimann <adjisb@sandbox.game> Co-authored-by: Micaiah Reid <micaiahreid@gmail.com> Co-authored-by: David Murdoch <187813+davidmurdoch@users.noreply.github.com>
1 parent 702db83 commit 7ee7d4b

File tree

3 files changed

+52
-7
lines changed

3 files changed

+52
-7
lines changed

src/chains/ethereum/ethereum/src/blockchain.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,14 @@ export default class Blockchain extends Emittery<BlockchainTypedEvents> {
328328
options.miner.blockGasLimit,
329329
initialAccounts
330330
);
331-
blocks.earliest = blocks.latest =
332-
await this.#blockBeingSavedPromise.then(({ block }) => block);
331+
blocks.latest = await this.#blockBeingSavedPromise.then(
332+
({ block }) => block
333+
);
334+
// when we are forking, blocks.earliest is already set to what was
335+
// retrieved from the fork
336+
if (!blocks.earliest) {
337+
blocks.earliest = blocks.latest;
338+
}
333339
}
334340
}
335341

src/chains/ethereum/ethereum/src/data-managers/block-manager.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
TypedDatabaseTransaction
1616
} from "@ganache/ethereum-transaction";
1717
import { GanacheLevelUp } from "../database";
18+
import { Ethereum } from "../api-types";
1819

1920
const LATEST_INDEX_KEY = BUFFER_ZERO;
2021

@@ -274,17 +275,39 @@ export default class BlockManager extends Manager<Block> {
274275
await this.#blockIndexes.put(LATEST_INDEX_KEY, number);
275276
}
276277

278+
async getEarliest() {
279+
const fallback = this.#blockchain.fallback;
280+
if (fallback) {
281+
const json = await fallback.request<Ethereum.Block<true, "public">>(
282+
"eth_getBlockByNumber",
283+
[Tag.earliest, true],
284+
// TODO: re-enable cache once this is fixed
285+
// https://github.com/trufflesuite/ganache/issues/3773
286+
{ disableCache: true }
287+
);
288+
if (json) {
289+
const common = fallback.getCommonForBlockNumber(
290+
this.#common,
291+
BigInt(json.number)
292+
);
293+
return new Block(BlockManager.rawFromJSON(json, common), common);
294+
}
295+
} else {
296+
// if we're forking, there shouldn't be an earliest block saved to the db,
297+
// it's always retrieved from the fork
298+
for await (const data of this.base.createValueStream({ limit: 1 })) {
299+
return new Block(data as Buffer, this.#common);
300+
}
301+
}
302+
}
303+
277304
/**
278305
* Updates the this.latest and this.earliest properties with data
279306
* from the database.
280307
*/
281308
async updateTaggedBlocks() {
282309
const [earliest, latestBlockNumber] = await Promise.all([
283-
(async () => {
284-
for await (const data of this.base.createValueStream({ limit: 1 })) {
285-
return new Block(data as Buffer, this.#common);
286-
}
287-
})(),
310+
this.getEarliest(),
288311
this.#blockIndexes.get(LATEST_INDEX_KEY).catch(e => null)
289312
]);
290313

src/chains/ethereum/ethereum/tests/forking/block.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,22 @@ describe("forking", function () {
4040
assert.deepStrictEqual(block.parentHash, remoteBlock.hash);
4141
});
4242

43+
it("after initialization our earliest block should be the fork earliest block, parentHash should match", async () => {
44+
const res = await request.post(URL).send({
45+
jsonrpc: "2.0",
46+
id: "1",
47+
method: "eth_getBlockByNumber",
48+
params: ["earliest", true]
49+
});
50+
const remoteBlock = JSON.parse(res.text).result;
51+
const block = await provider.send("eth_getBlockByNumber", [
52+
"earliest",
53+
true
54+
]);
55+
assert.deepStrictEqual(parseInt(block.number), parseInt(remoteBlock.number));
56+
assert.deepStrictEqual(block.hash, remoteBlock.hash);
57+
});
58+
4359
//todo: reinstate this test after https://github.com/trufflesuite/ganache/issues/3616 is fixed
4460
it.skip("should get a block from the original chain", async () => {
4561
const res = await request.post(URL).send({

0 commit comments

Comments
 (0)