-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Incorrect forge script
gas estimation on Sonic
#9723
Comments
Was able to reproduce w/ given setup It appears that this is resolved by forcing gas estimation via the RPC Would be great if you could check if this resolves it, branch |
Hey @zerosnacks! Thanks for looking into this! I can confirm that the issue went away after using your branch. However, it only went away for the example repo I prepared. I tried our prod deployment scripts repository and unfortunately nothing has changed. Note that we used this setup on multiple networks already and it has never been an issue. It's only affecting Sonic. Would you like to take a look at it? If so, these are the steps to reproduce. I prepared it in a way you can easily run the script with no git clone https://github.com/euler-xyz/euler-interfaces.git && git clone https://github.com/euler-xyz/evk-periphery.git
cd evk-periphery && git checkout issue-sonic && forge install && forge compile lib/euler-earn/src --force --optimize --optimizer-runs 800 --use 0.8.27 --out out-euler-earn && forge compile
anvil --fork-url https://rpc.soniclabs.com In a different terminal: env DEPLOYMENT_RPC_URL=http://127.0.0.1:8545 ADDRESSES_DIR_PATH=../euler-interfaces/addresses FORCE_MULTISIG_ADDRESSES=true forge script script/50_CoreAndPeriphery.s.sol --legacy --slow --rpc-url http://127.0.0.1:8545 --private-key 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 As you can see in |
It is still a bit of a mystery but thought I would share preliminary results. It appears that indeed after a couple of transactions it dramatically escalates the gas use. However when running the same test with the proposed fix applied it suddenly recovers. The first column is the gas usage before. I noticed that in the broadcast log it is including a The third column is after disabling this line which seems to yield the desired result: foundry/crates/script/src/transaction.rs Lines 78 to 79 in e16a75b
cc @klkvr I'm missing some context on this, would you know more? It was added in #4219 / #8833 It appears it could be related to the use of foundry/crates/script/src/broadcast.rs Lines 98 to 102 in b026f7a
|
Interesting! I didn't notice that it actually recovers after a few transactions, I checked only the first few 1B ones assuming it will be screwed until the very end as the last time. Re @zerosnacks @klkvr Do you have any clue what might be the reason? |
Interestingly, all 9 contract deployment transactions that hit 1B in case number 2 appear to be from one script ( The only thing that comes to my mind is that all those modules deployed in However, this whole script is a chain of contract deployments and interactions with them. If the state change made by the previous transactions is not taken into account in the following one, we would hit many more reverts, not only those 9. |
After further investigation it appears that passing My hunch is that because we have a default gas limit set at I'll have a look at whether we can make improvements on our end to infer this limit from the RPC during simulation / filling to prevent this going forward but this should unblock you for now. |
Thank you for your support @zerosnacks! It worked! I finally managed to successfully deploy on Sonic |
Component
Forge
Have you ensured that all of these are up to date?
What version of Foundry are you on?
forge 0.3.0 (5a8bd89 2024-12-20T08:45:53.204298000Z)
What version of Foundryup are you on?
foundryup: 0.3.0
What command(s) is the bug in?
forge script
Operating System
macOS (Apple Silicon)
Describe the bug
Description
I ran into an issue when trying to run our deployment script on Sonic. This is something that haven't been observed when running it against any other network so far.
The problem seems to be incorrect gas estimation when running
forge script
. We have very a complex deployment script and, as with any other script, when we run it, foundry first simulates the transactions, estimates gas and saves the results into the broadcast file. Then, it uses that data to broadcast the transactions.The issue is that after a couple transactions, foundry starts estimating gas as if we were hitting the block gas limit for all the remaining transactions. This can be seen in the broadcast file. The fact that gas gets overestimated makes the script fail because most likely the deployer does not have enough balance to deploy all the contracts according to the incorrectly estimated gas. If the deployer had enough balance to send the transactions, it would appear that only a small amount of gas was used, proving that the gas was heavily overestimated.
To reproduce the issue, I created a very simple example script. The only thing it does is trying to deploy the same contract multiple times. When run against an anvil fork (the same issue can be observed when running against the production network too), foundry estimates gas correctly for the first ~60 transactions and then overestimates the gas for the remaining transactions.
To see that, you can look at the broadcast file in the repo provided below. Gas for the first 60 transactions is equal to ~1.4M. However, for the remaining transactions, the gas estimated is equal to ~1B (roughly 700 times more, where 1B is the block gas limit on Sonic). Because the default private key on Anvil has enough balance to broadcast the transaction, the script passes, but there's no reason the deployment of exactly the same contract should consume vastly different amount of gas.
This is very problematic because on the production network the deployer does not have unlimited amount of gas currency. The trick could be to manually change the gas amount in the broadcast file and rerun the script with the
--resume
option. However, this seems more like a workaround and not a fix especially because it's not clear what's causing the issue.Steps to reproduce:
./broadcast/Test.s.sol/146/run-latest.json
) and the console logs. According to the console logs, gas estimate will be very high (look at the top) while the real gas consumed will be very low (look at the bottom). The only reason the script succeededs is that the default Anvil account has enough balance to cover the overestimated gas amount. When the same done against Ethereum mainnet or any other network I tested, the gas estimate is the same for all the transactions in the broadcast file.The text was updated successfully, but these errors were encountered: