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

Supporting forge script for Foundry tools #247

Closed
nvdtf opened this issue May 15, 2024 · 7 comments
Closed

Supporting forge script for Foundry tools #247

nvdtf opened this issue May 15, 2024 · 7 comments
Assignees

Comments

@nvdtf
Copy link
Member

nvdtf commented May 15, 2024

Instructions

Foundry is a popular EVM toolchain. We've received reports from partners having trouble using it with Flow PreviewNet, specifically the script feature to manage deployments and updates.

A similar problem has been observed with HardHat Ignition (NomicFoundation/hardhat-ignition#738).

Problem

Foundry can deploy contracts normally using forge create but deployments with forge script error. The error we see is:

failed to get account for 0x0514250500425DdE3bb35D49cC75F5ea861BCbFc: server returned an error response: error code -32000: internal error address=0x0514250500425DdE3bb35D49cC75F5ea861BCbFc

This error does not happen continuously. There are periods of time where the script runs and succeeds but mostly it fails with the above error. We couldn't find a specific pattern to succeed/fail.

Steps to Reproduce

Here's a quick way to reproduce this error:

  1. Init a new project:

    forge init
  2. Create new key pair and fund with faucet:

    cast wallet new
  3. Normal deployment succeeds with starter template contract:

    forge create --rpc-url https://previewnet.evm.nodes.onflow.org \
     --private-key $YOUR_PRIVATE_KEY \
     --legacy \
     src/Counter.sol:Counter
  4. Update script/Counter.s.sol:

    // SPDX-License-Identifier: UNLICENSED
    pragma solidity ^0.8.13;
    
    import {Script, console} from "forge-std/Script.sol";
    import "../src/Counter.sol";
    
    contract CounterScript is Script {
        function setUp() public {}
    
        function run() public {
            // Setup
            uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
            vm.startBroadcast(deployerPrivateKey);
    
            // Deploy
            new Counter();
    
            // End
            vm.stopBroadcast();
        }
    }
  5. Run script (will fail):

    export PRIVATE_KEY=0xPRIVATE_KEY
    
    forge script script/Counter.s.sol:CounterScript \
     --broadcast \
     -vvv \
     --legacy \
     --rpc-url https://previewnet.evm.nodes.onflow.org/

Acceptance Criteria

forge script should function properly as forge create.

Context

This is blocking to USDC EVM development as they are relying on deployment scripts. You can check out sample Circle scripts here but the simple case outlined above will also reproduce the error.

@m-Peter
Copy link
Collaborator

m-Peter commented May 17, 2024

I had a shot at this locally and on PreviewNet, but I can't seem to reproduce any failures. Here's what I got:

$ cast wallet new

Successfully created new keypair.
Address:     0xd6AACF6C06908dd6CB525bd7d999A7C9F36c6AaE
Private key: 0x32589b2b129a420489a109929f5aa82a3e6ff116303972c4e0fb3e9163d9edd5
$ forge create --rpc-url https://previewnet.evm.nodes.onflow.org \  
 --private-key 0x32589b2b129a420489a109929f5aa82a3e6ff116303972c4e0fb3e9163d9edd5 \
 --legacy \
 src/Counter.sol:Counter

[⠊] Compiling...
No files changed, compilation skipped
Deployer: 0xd6AACF6C06908dd6CB525bd7d999A7C9F36c6AaE
Deployed to: 0x50aC67b41B4610657F895f2f80a3Fc2816ca172C
Transaction hash: 0x13d699907209491f85f5858337cfb6f29918560bcf4c362239e68b34c1b528e6
$ export PRIVATE_KEY=0x32589b2b129a420489a109929f5aa82a3e6ff116303972c4e0fb3e9163d9edd5

$ forge script script/Counter.s.sol:CounterScript \
 --broadcast \
 -vvv \
 --legacy \
 --rpc-url https://previewnet.evm.nodes.onflow.org/

[⠒] Compiling...
No files changed, compilation skipped
Script ran successfully.

## Setting up 1 EVM.

==========================

Chain 646

Estimated gas price: 0 gwei

Estimated total gas used for script: 138729

Estimated amount required: 0. ETH

==========================
##
Sending transactions [0 - 0].
⠁ [00:00:00] [###########################################################################################] 1/1 txes (0.0s)##
Waiting for receipts.
⠉ [00:00:07] [#######################################################################################] 1/1 receipts (0.0s)
##### 646
✅  [Success]Hash: 0xb92434f0e232993b091c9a6a819a593e0e908f72fc79012ef8ce800ef268944c
Contract Address: 0x9d533aeD3486648B2A94e73899CA57D82E3856b3
Block: 5988
Gas Used: 106733



==========================

ONCHAIN EXECUTION COMPLETE & SUCCESSFUL.
Total Paid: 0. ETH (106733 gas * avg 0 gwei)

Transactions saved to: /home/mpeter/Dev/forge-testing/broadcast/Counter.s.sol/646/run-latest.json

Sensitive values saved to: /home/mpeter/Dev/forge-testing/cache/Counter.s.sol/646/run-latest.json

@nvdtf Can you make another attempt on this, and let me know?

@nvdtf
Copy link
Member Author

nvdtf commented May 17, 2024

I just recreated and tried again and got success like you.

Can't produce a failure right now, but this was the behaviour when I reported this issue. I need to find what triggers failures and if it's happening still or not.

Will report back to Circle to test. Will keep this issue open in case we find out more.

@nvdtf
Copy link
Member Author

nvdtf commented May 17, 2024

Getting errors now, can't produce a success case:

→ forge script script/Counter.s.sol:CounterScript \
    --broadcast \
    --private-key 0x32589b2b129a420489a109929f5aa82a3e6ff116303972c4e0fb3e9163d9edd5 \
    -vvv \
    --legacy \
    --rpc-url https://previewnet.evm.nodes.onflow.org/
[⠊] Compiling...
No files changed, compilation skipped
2024-05-17T18:19:49.318743Z ERROR sharedbackend: Failed to send/recv `basic` err=failed to get account for 0xd6AACF6C06908dd6CB525bd7d999A7C9F36c6AaE: server returned an error response: error code -32000: internal error address=0xd6AACF6C06908dd6CB525bd7d999A7C9F36c6AaE
Error:
failed to get account for 0xd6AACF6C06908dd6CB525bd7d999A7C9F36c6AaE: server returned an error response: error code -32000: internal error

UPDATE 2:
After a few hours, I'm now getting success. I tried multiple ways to see if I can produce a failure but couldn't which makes me think this is an intermittent issue from the server side. I'll try the Circle deploy scripts directly to see if I can reproduce.

Could Foundry be querying accounts for nonces and can't fetch them?

@franklywatson
Copy link
Contributor

@sideninja for visibility

@nvdtf
Copy link
Member Author

nvdtf commented May 17, 2024

I found a way to make the failure go away:

  • Trying the above script after waiting for a while produces a failure.
  • Generate an unrelated wallet (cast wallet new) and fund it with the PreviewNet faucet.
  • Retry step 1. It now succeeds. It will continue to succeed for some time.
    • Note that the account we're using is NOT the account we just generated. It's the same script as step 1 with same key.

This makes me think this might be related to block production on PreviewNet and what tools expect. Using the faucet produces a new block. Maybe if the latest block is stale, the tool can't query something it needs like a nonce?

@m-Peter
Copy link
Collaborator

m-Peter commented May 18, 2024

@nvdtf Thanks for the detailed breakdown.

This makes me think this might be related to block production on PreviewNet and what tools expect. Using the faucet produces a new block. Maybe if the latest block is stale, the tool can't query something it needs like a nonce?

Regarding the above, this looks similar to the issue I discovered here: NomicFoundation/hardhat-ignition#738 (comment). Where the command hangs until the necessary blocks for tx confirmation are produced.

@m-Peter
Copy link
Collaborator

m-Peter commented May 30, 2024

Context

This is blocking to USDC EVM development as they are relying on deployment scripts. You can check out sample Circle scripts here but the simple case outlined above will also reproduce the error.

@nvdtf Regarding the block of USDC EVM deployment, I think this comment, solves the error you faced.

As for the current issue, I am not able to re-produce a failure, neither locally nor in PreviewNet.

This makes me think this might be related to block production on PreviewNet and what tools expect. Using the faucet produces a new block. Maybe if the latest block is stale, the tool can't query something it needs like a nonce?

I found out that hardhat-ignition plugin makes use of confirmations, which is basically related to block production. But there's a setting to override this.
However, tools like forge, what they do is that they repeatedly call eth_getTransactionReceipt with a tx hash, until the result is no longer null. This means the transaction is has been executed, and included in a block. After that, they will check the receipt's status, to see if the transaction was successful.

I am closing the issue for now. Feel free to re-open, if you find something strange 🙏

@m-Peter m-Peter closed this as completed May 30, 2024
@github-project-automation github-project-automation bot moved this from 🏗 In Progress to ✅ Done in 🌊 Flow 4D May 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

No branches or pull requests

4 participants