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

bug(forge script): running script with --broadcast for a transaction sequence can error out due to nonce desync from rpc latency #9095

Closed
2 tasks done
pogobounce opened this issue Oct 11, 2024 · 2 comments · Fixed by #9096
Labels
Cmd-forge-script Command: forge script T-bug Type: bug T-to-investigate Type: to investigate

Comments

@pogobounce
Copy link
Contributor

Component

Forge

Have you ensured that all of these are up to date?

  • Foundry
  • Foundryup

What version of Foundry are you on?

forge 0.2.0 (a17869a 2024-10-09T00:24:42.688171700Z)

What command(s) is the bug in?

forge script

Operating System

Windows

Describe the bug

On some chains with fast block times and/or relatively high latency providers, running a deployment script with --broadcast that sends a sequence of transactions can lead to the script failing, as the RPC will report an older transaction count for the EOA.

Examples of affected chains: Gnosis Mainnet, Arbitrum Sepolia. I'm sure there's others out there, or will be.

Note; --slow doesn't fix/alleviate the error for me, either.

Basic example script

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import "forge-std/Script.sol";

contract Test is Script {

    function run() public {

        uint256 privateKey = vm.envUint("PRIVATE_KEY");

        vm.startBroadcast(privateKey);

        for (uint256 i; i < 20; ++i) {                                              // sequence of 20 txs
            (bool success,) = 0x980B62Da83eFf3D4576C647993b0c1D7faf17c73.call("");  // WETH on Arb Sepolia
            require(success);
        }
        
        vm.stopBroadcast();
    }

}

Command

forge script ./script/Test.s.sol --rpc-url https://arbitrum-sepolia.infura.io/v3/<INFURA_KEY> --broadcast

Output

[⠒] Compiling...
[⠑] Compiling 1 files with Solc 0.8.24
[⠘] Solc 0.8.24 finished in 1.54s
Compiler run successful!
Script ran successfully.

## Setting up 1 EVM.

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

Chain 421614

Estimated gas price: 0.200000001 gwei

Estimated total gas used for script: 1209620

Estimated amount required: 0.00024192400120962 ETH

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

##### arbitrum-sepolia
✅  [Success]Hash: <0x....>
Block: 87738608
Paid: 0.0000042713 ETH (42713 gas * 0.1 gwei)


Transactions saved to: <...>

Sensitive values saved to: <...>

Error:
Failed to send transaction

Context:
- EOA nonce changed unexpectedly while sending transactions. Expected 132 got 131 from provider.

Sometimes it sends just one transaction before failing, sometimes it sends more, but invariably it fails before finishing the sequence.

@pogobounce pogobounce added T-bug Type: bug T-needs-triage Type: this issue needs to be labelled labels Oct 11, 2024
@github-project-automation github-project-automation bot moved this to Todo in Foundry Oct 11, 2024
@zerosnacks zerosnacks added T-to-investigate Type: to investigate Cmd-forge-script Command: forge script labels Oct 11, 2024
@zerosnacks zerosnacks changed the title running script with --broadcast for a transaction sequence can error out due to nonce desync from rpc latency bug(forge script): running script with --broadcast for a transaction sequence can error out due to nonce desync from rpc latency Oct 11, 2024
@zerosnacks zerosnacks removed the T-needs-triage Type: this issue needs to be labelled label Oct 11, 2024
pogobounce added a commit to pogobounce/foundry that referenced this issue Oct 11, 2024
@yash-atreya
Copy link
Member

yash-atreya commented Oct 11, 2024

Can you try using the --slow flag? It should mitigate this issue and maintain correct nonce as using the --slow flag sequentially broadcasts the transaction.

[EDIT: --slow doesn't work]

@pogobounce
Copy link
Contributor Author

pogobounce commented Oct 11, 2024

Can you try using the --slow flag? It should mitigate this issue and maintain correct nonce as using the --slow flag sequentially broadcasts the transaction.

I mentioned in the issue, --slow doesn't work

forge script ./script/Test.s.sol --rpc-url https://arbitrum-sepolia.infura.io/v3/... --broadcast --slow
[⠊] Compiling...
No files changed, compilation skipped
Script ran successfully.

## Setting up 1 EVM.

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

Chain 421614

Estimated gas price: 0.200000001 gwei

Estimated total gas used for script: 1137520

Estimated amount required: 0.00022750400113752 ETH

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

##### arbitrum-sepolia
✅  [Success]Hash: ...
Block: 87776017
Paid: 0.0000040733 ETH (40733 gas * 0.1 gwei)

                                                                                                                                                                                                                                             
##### arbitrum-sepolia                                                                                                                                                                                                                       
✅  [Success]Hash: ...
Block: 87776029
Paid: 0.0000040733 ETH (40733 gas * 0.1 gwei)


##### arbitrum-sepolia
✅  [Success]Hash: ...
Block: 87776035
Paid: 0.0000040733 ETH (40733 gas * 0.1 gwei)


##### arbitrum-sepolia
✅  [Success]Hash: ...
Block: 87776049
Paid: 0.0000040733 ETH (40733 gas * 0.1 gwei)


##### arbitrum-sepolia
✅  [Success]Hash: ...
Block: 87776054
Paid: 0.0000040733 ETH (40733 gas * 0.1 gwei)


##### arbitrum-sepolia
✅  [Success]Hash: ...
Block: 87776059
Paid: 0.0000040733 ETH (40733 gas * 0.1 gwei)


##### arbitrum-sepolia
✅  [Success]Hash: ...
Block: 87776064
Paid: 0.0000040733 ETH (40733 gas * 0.1 gwei)


##### arbitrum-sepolia
✅  [Success]Hash: ...
Block: 87776068
Paid: 0.0000040733 ETH (40733 gas * 0.1 gwei)


Transactions saved to: ...

Sensitive values saved to: ...

Error: 
Failed to send transaction

Context:
- EOA nonce changed unexpectedly while sending transactions. Expected 168 got 167 from provider.

klkvr pushed a commit that referenced this issue Oct 14, 2024
… error out due to nonce desync from rpc latency (#9096)

* fix for issue #9095

* changed 'if' statement into 'match'

* fmt fix

* repeat ask for provider nonce on desync

* loop break and tokio::time use instead of std::thread
@github-project-automation github-project-automation bot moved this from Todo to Done in Foundry Oct 14, 2024
rplusq pushed a commit to rplusq/foundry that referenced this issue Nov 29, 2024
… error out due to nonce desync from rpc latency (foundry-rs#9096)

* fix for issue foundry-rs#9095

* changed 'if' statement into 'match'

* fmt fix

* repeat ask for provider nonce on desync

* loop break and tokio::time use instead of std::thread
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Cmd-forge-script Command: forge script T-bug Type: bug T-to-investigate Type: to investigate
Projects
Archived in project
3 participants