From e8ee4f866554cdab4093af06efbc97f5a30dfc12 Mon Sep 17 00:00:00 2001 From: Andres Adjimann Date: Mon, 3 Oct 2022 18:36:45 -0300 Subject: [PATCH 1/9] fix: set the right value to earliest when forking --- .../ethereum/ethereum/src/blockchain.ts | 6 +++-- .../src/data-managers/block-manager.ts | 27 +++++++++++++++---- .../ethereum/tests/forking/block.test.ts | 20 ++++++++++++-- 3 files changed, 44 insertions(+), 9 deletions(-) diff --git a/src/chains/ethereum/ethereum/src/blockchain.ts b/src/chains/ethereum/ethereum/src/blockchain.ts index f31e7c3288..76ca5c2bca 100644 --- a/src/chains/ethereum/ethereum/src/blockchain.ts +++ b/src/chains/ethereum/ethereum/src/blockchain.ts @@ -328,8 +328,10 @@ export default class Blockchain extends Emittery { options.miner.blockGasLimit, initialAccounts ); - blocks.earliest = blocks.latest = - await this.#blockBeingSavedPromise.then(({ block }) => block); + blocks.latest = await this.#blockBeingSavedPromise.then(({ block }) => block); + if (!blocks.earliest) { + blocks.earliest = blocks.latest; + } } } diff --git a/src/chains/ethereum/ethereum/src/data-managers/block-manager.ts b/src/chains/ethereum/ethereum/src/data-managers/block-manager.ts index 551d38740d..9adffd10c1 100644 --- a/src/chains/ethereum/ethereum/src/data-managers/block-manager.ts +++ b/src/chains/ethereum/ethereum/src/data-managers/block-manager.ts @@ -274,17 +274,34 @@ export default class BlockManager extends Manager { await this.#blockIndexes.put(LATEST_INDEX_KEY, number); } + async getEarliest() { + const fallback = this.#blockchain.fallback; + if (fallback) { + const json = await fallback.request( + "eth_getBlockByNumber", + [Tag.earliest, true], + { disableCache: true } + ); + if (json) { + const common = fallback.getCommonForBlockNumber( + this.#common, + BigInt(json.number) + ); + return new Block(BlockManager.rawFromJSON(json, common), common); + } + } + for await (const data of this.base.createValueStream({ limit: 1 })) { + return new Block(data as Buffer, this.#common); + } + } + /** * Updates the this.latest and this.earliest properties with data * from the database. */ async updateTaggedBlocks() { const [earliest, latestBlockNumber] = await Promise.all([ - (async () => { - for await (const data of this.base.createValueStream({ limit: 1 })) { - return new Block(data as Buffer, this.#common); - } - })(), + this.getEarliest(), this.#blockIndexes.get(LATEST_INDEX_KEY).catch(e => null) ]); diff --git a/src/chains/ethereum/ethereum/tests/forking/block.test.ts b/src/chains/ethereum/ethereum/tests/forking/block.test.ts index 500e8047bc..c2da67a0ef 100644 --- a/src/chains/ethereum/ethereum/tests/forking/block.test.ts +++ b/src/chains/ethereum/ethereum/tests/forking/block.test.ts @@ -3,7 +3,7 @@ import getProvider from "../helpers/getProvider"; import { EthereumProvider } from "../../src/provider"; import request from "superagent"; -describe("forking", function () { +describe("forking", function() { this.timeout(10000); describe("blocks", () => { @@ -11,7 +11,7 @@ describe("forking", function () { const blockNumHex = `0x${blockNumber.toString(16)}`; const URL = "https://mainnet.infura.io/v3/" + process.env.INFURA_KEY; let provider: EthereumProvider; - before(async function () { + before(async function() { if (!process.env.INFURA_KEY) { this.skip(); } @@ -40,6 +40,22 @@ describe("forking", function () { assert.deepStrictEqual(block.parentHash, remoteBlock.hash); }); + it("after initialization our earliest block should be the fork earliest block, parentHash should match", async () => { + const res = await request.post(URL).send({ + jsonrpc: "2.0", + id: "1", + method: "eth_getBlockByNumber", + params: ["earliest", true] + }); + const remoteBlock = JSON.parse(res.text).result; + const block = await provider.send("eth_getBlockByNumber", [ + "earliest", + true + ]); + assert.deepStrictEqual(parseInt(block.number), parseInt(remoteBlock.number)); + assert.deepStrictEqual(block.hash, remoteBlock.hash); + }); + //todo: reinstate this test after https://github.com/trufflesuite/ganache/issues/3616 is fixed it.skip("should get a block from the original chain", async () => { const res = await request.post(URL).send({ From 7e5fe029764a2dc520f953135a1ce206446b865c Mon Sep 17 00:00:00 2001 From: adjisb <85941014+adjisb@users.noreply.github.com> Date: Wed, 5 Oct 2022 21:08:29 -0300 Subject: [PATCH 2/9] Update src/chains/ethereum/ethereum/tests/forking/block.test.ts Co-authored-by: Micaiah Reid --- src/chains/ethereum/ethereum/tests/forking/block.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/chains/ethereum/ethereum/tests/forking/block.test.ts b/src/chains/ethereum/ethereum/tests/forking/block.test.ts index c2da67a0ef..b0ec69362f 100644 --- a/src/chains/ethereum/ethereum/tests/forking/block.test.ts +++ b/src/chains/ethereum/ethereum/tests/forking/block.test.ts @@ -11,7 +11,7 @@ describe("forking", function() { const blockNumHex = `0x${blockNumber.toString(16)}`; const URL = "https://mainnet.infura.io/v3/" + process.env.INFURA_KEY; let provider: EthereumProvider; - before(async function() { + before(async function () { if (!process.env.INFURA_KEY) { this.skip(); } From 1ac1a81f20dff257d08101ae4063fb20cf9b9904 Mon Sep 17 00:00:00 2001 From: adjisb <85941014+adjisb@users.noreply.github.com> Date: Wed, 5 Oct 2022 21:10:37 -0300 Subject: [PATCH 3/9] Update src/chains/ethereum/ethereum/src/blockchain.ts Co-authored-by: Micaiah Reid --- src/chains/ethereum/ethereum/src/blockchain.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/chains/ethereum/ethereum/src/blockchain.ts b/src/chains/ethereum/ethereum/src/blockchain.ts index 76ca5c2bca..94d14ed837 100644 --- a/src/chains/ethereum/ethereum/src/blockchain.ts +++ b/src/chains/ethereum/ethereum/src/blockchain.ts @@ -328,7 +328,9 @@ export default class Blockchain extends Emittery { options.miner.blockGasLimit, initialAccounts ); - blocks.latest = await this.#blockBeingSavedPromise.then(({ block }) => block); + blocks.latest = await this.#blockBeingSavedPromise.then( + ({ block }) => block + ); if (!blocks.earliest) { blocks.earliest = blocks.latest; } From 5b262635c4e2f4a07e3f9ca39292c7e0474da01c Mon Sep 17 00:00:00 2001 From: adjisb <85941014+adjisb@users.noreply.github.com> Date: Thu, 6 Oct 2022 19:26:40 -0300 Subject: [PATCH 4/9] Update src/chains/ethereum/ethereum/src/data-managers/block-manager.ts Co-authored-by: Micaiah Reid --- src/chains/ethereum/ethereum/src/data-managers/block-manager.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/chains/ethereum/ethereum/src/data-managers/block-manager.ts b/src/chains/ethereum/ethereum/src/data-managers/block-manager.ts index 9adffd10c1..7ae4e9e8db 100644 --- a/src/chains/ethereum/ethereum/src/data-managers/block-manager.ts +++ b/src/chains/ethereum/ethereum/src/data-managers/block-manager.ts @@ -280,6 +280,8 @@ export default class BlockManager extends Manager { const json = await fallback.request( "eth_getBlockByNumber", [Tag.earliest, true], + // TODO: re-enable cache once this is fixed + // https://github.com/trufflesuite/ganache/issues/3773 { disableCache: true } ); if (json) { From 20e290704e6864e9669ff242a59fa2d5cc07d368 Mon Sep 17 00:00:00 2001 From: adjisb <85941014+adjisb@users.noreply.github.com> Date: Fri, 7 Oct 2022 02:41:22 -0300 Subject: [PATCH 5/9] Update src/chains/ethereum/ethereum/tests/forking/block.test.ts Co-authored-by: David Murdoch <187813+davidmurdoch@users.noreply.github.com> --- src/chains/ethereum/ethereum/tests/forking/block.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/chains/ethereum/ethereum/tests/forking/block.test.ts b/src/chains/ethereum/ethereum/tests/forking/block.test.ts index b0ec69362f..296d535683 100644 --- a/src/chains/ethereum/ethereum/tests/forking/block.test.ts +++ b/src/chains/ethereum/ethereum/tests/forking/block.test.ts @@ -3,7 +3,7 @@ import getProvider from "../helpers/getProvider"; import { EthereumProvider } from "../../src/provider"; import request from "superagent"; -describe("forking", function() { +describe("forking", function () { this.timeout(10000); describe("blocks", () => { From a9b3f5b2b55c81a235a3839217bc7da0ec5f711a Mon Sep 17 00:00:00 2001 From: Andres Adjimann Date: Fri, 7 Oct 2022 03:14:31 -0300 Subject: [PATCH 6/9] fix: use Etereum.Block type --- .../ethereum/ethereum/src/data-managers/block-manager.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/chains/ethereum/ethereum/src/data-managers/block-manager.ts b/src/chains/ethereum/ethereum/src/data-managers/block-manager.ts index 7ae4e9e8db..250f048ae0 100644 --- a/src/chains/ethereum/ethereum/src/data-managers/block-manager.ts +++ b/src/chains/ethereum/ethereum/src/data-managers/block-manager.ts @@ -15,6 +15,7 @@ import { TypedDatabaseTransaction } from "@ganache/ethereum-transaction"; import { GanacheLevelUp } from "../database"; +import { Ethereum } from "../api-types"; const LATEST_INDEX_KEY = BUFFER_ZERO; @@ -277,7 +278,7 @@ export default class BlockManager extends Manager { async getEarliest() { const fallback = this.#blockchain.fallback; if (fallback) { - const json = await fallback.request( + const json = await fallback.request>( "eth_getBlockByNumber", [Tag.earliest, true], // TODO: re-enable cache once this is fixed From 22c6027f26e9e4b0dfdec499e6f6980ebd34870b Mon Sep 17 00:00:00 2001 From: MicaiahReid Date: Thu, 5 Jan 2023 17:14:48 -0500 Subject: [PATCH 7/9] add clarifying else and comment --- .../ethereum/ethereum/src/data-managers/block-manager.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/chains/ethereum/ethereum/src/data-managers/block-manager.ts b/src/chains/ethereum/ethereum/src/data-managers/block-manager.ts index 250f048ae0..e30eef0b5d 100644 --- a/src/chains/ethereum/ethereum/src/data-managers/block-manager.ts +++ b/src/chains/ethereum/ethereum/src/data-managers/block-manager.ts @@ -292,9 +292,12 @@ export default class BlockManager extends Manager { ); return new Block(BlockManager.rawFromJSON(json, common), common); } - } - for await (const data of this.base.createValueStream({ limit: 1 })) { - return new Block(data as Buffer, this.#common); + } else { + // if we're forking, there shouldn't be an earliest block saved to the db, + // it's always retrieved from the fork + for await (const data of this.base.createValueStream({ limit: 1 })) { + return new Block(data as Buffer, this.#common); + } } } From 906f1382e38b81a43838538aa5fa627bcf333f30 Mon Sep 17 00:00:00 2001 From: MicaiahReid Date: Thu, 5 Jan 2023 17:15:05 -0500 Subject: [PATCH 8/9] add explanatory comment --- src/chains/ethereum/ethereum/src/blockchain.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/chains/ethereum/ethereum/src/blockchain.ts b/src/chains/ethereum/ethereum/src/blockchain.ts index 94d14ed837..926ab62922 100644 --- a/src/chains/ethereum/ethereum/src/blockchain.ts +++ b/src/chains/ethereum/ethereum/src/blockchain.ts @@ -331,6 +331,8 @@ export default class Blockchain extends Emittery { blocks.latest = await this.#blockBeingSavedPromise.then( ({ block }) => block ); + // when we are forking, blocks.earliest is already set to what was + // retrieve from the fork if (!blocks.earliest) { blocks.earliest = blocks.latest; } From 232c34258bad27ab45f0ab23776c77e55d320c48 Mon Sep 17 00:00:00 2001 From: MicaiahReid Date: Thu, 5 Jan 2023 19:28:20 -0500 Subject: [PATCH 9/9] typo --- src/chains/ethereum/ethereum/src/blockchain.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/chains/ethereum/ethereum/src/blockchain.ts b/src/chains/ethereum/ethereum/src/blockchain.ts index 926ab62922..8614fc024c 100644 --- a/src/chains/ethereum/ethereum/src/blockchain.ts +++ b/src/chains/ethereum/ethereum/src/blockchain.ts @@ -332,7 +332,7 @@ export default class Blockchain extends Emittery { ({ block }) => block ); // when we are forking, blocks.earliest is already set to what was - // retrieve from the fork + // retrieved from the fork if (!blocks.earliest) { blocks.earliest = blocks.latest; }