Skip to content

Enable hardhat forking RSK mainnet#433

Merged
tjcloa merged 15 commits intodevelopmentfrom
rsk-hardhat-forking
Jun 23, 2022
Merged

Enable hardhat forking RSK mainnet#433
tjcloa merged 15 commits intodevelopmentfrom
rsk-hardhat-forking

Conversation

@koirikivi
Copy link
Copy Markdown
Contributor

@koirikivi koirikivi commented Apr 29, 2022

Enable npx hardhat node --fork (and related test patterns) with RSK. Requires a bit of patching because of RSK.

The hardhat patch is automatically applied after running npm install

There's a command (npm run fork:rsk-mainnet) to start a local node forking from mainnet (npm run fork:rsk-testnet for the testnet).
Forking can be also used in tests. Please refer to Hardhat forking documentation https://hardhat.org/hardhat-network/guides/mainnet-forking#resetting-the-fork

IMPORTANT!! Consider before merging

  • This PR includes test hardhat task check-fork-patch to verify if the forking patch works. It will fail if there's no network connection or if the mainnet sovryn rpc node is down, which causes tests to fail.
  • The patch will probably not work after hardhat is updated (we should get a patch upstream to hardhat, but it requires more work)
  • This PR doesn't have the package-lock.json or yarn.lock modifications, because I'm not sure if we're using npm or yarn and which version of node and npm we're using package-lock.json is added too.
  • There are some issues with eth_getStorageAt returning 0x0 -- a fix in progress. Should be fixed.
  • The patch has not been tested extensively, though everything seems to work!

Technical details

Calling a forked node (hardhat node --fork MY_RSK_NODE_RPC_URL) failed with:

eth_call
Invalid JSON-RPC response's result.  Errors: Invalid value null supplied
to: RpcBlockWithTransactions | null/transactions: RpcTransaction
Array/0: RpcTransaction/v: QUANTITY, Invalid value null supplied to:
RpcBlockWithTransactions | null/transactions: RpcTransaction Array/0:
RpcTransaction/r: QUANTITY, Invalid value null supplied to:
RpcBlockWithTransactions | null/transactions: RpcTransaction Array/0:
RpcTransaction/s: QUANTITY

We use patch-package to patch the local hardhat installation, since there's no upstream patch

This patch is based on:
https://gist.github.com/0x0scion/0422f9135bc37642ba36d55b59e8b424

More reading:
NomicFoundation/hardhat#2395
https://github.com/NomicFoundation/hardhat/pull/2313/files
NomicFoundation/hardhat#2106

Requires a bit of patching because of RSK.

Calling a forked node (hardhat node --fork MY_RSK_NODE_RPC_URL) failed with:

    eth_call
    Invalid JSON-RPC response's result.  Errors: Invalid value null supplied
    to: RpcBlockWithTransactions | null/transactions: RpcTransaction
    Array/0: RpcTransaction/v: QUANTITY, Invalid value null supplied to:
    RpcBlockWithTransactions | null/transactions: RpcTransaction Array/0:
    RpcTransaction/r: QUANTITY, Invalid value null supplied to:
    RpcBlockWithTransactions | null/transactions: RpcTransaction Array/0:
    RpcTransaction/s: QUANTITY

This patch is based on:
https://gist.github.com/0x0scion/0422f9135bc37642ba36d55b59e8b424

More reading:
NomicFoundation/hardhat#2395
https://github.com/NomicFoundation/hardhat/pull/2313/files
NomicFoundation/hardhat#2106
@tjcloa tjcloa self-requested a review May 2, 2022 23:45
Copy link
Copy Markdown
Contributor

@tjcloa tjcloa left a comment

Choose a reason for hiding this comment

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

lgtm
to work around possible issues with future hh updates, i suggest:

  1. create a hh task from the forking.test.js and remove the test as it does not test the domain logic; this way we can avoid false-negatives due to the node issues etc. when running the tests
  2. add the PR comment as disclaimer and containing the instruction on removing the patch as .md doc;
    2.1. do a hook - overload hh task with initial warning with the doc link which can be removed once the patch is included in hh

@koirikivi
Copy link
Copy Markdown
Contributor Author

@tjcloa changes lgtm

@tjcloa tjcloa merged commit b1093b9 into development Jun 23, 2022
@dzarezenko
Copy link
Copy Markdown

Could you provide some instructions on how to apply this? I was trying to fork Harmony mainnet and received this error:

 An unexpected error occurred:

InvalidResponseError: Invalid JSON-RPC response's result.

Errors: Invalid value undefined supplied to : RpcBlockWithTransactions | null/totalDifficulty: QUANTITY

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants