Skip to content

Commit

Permalink
fix(core): set simulation transaction limit less than block gas limit
Browse files Browse the repository at this point in the history
  • Loading branch information
sam-goldman committed Jan 6, 2024
1 parent 0b42d2e commit 5cbecd5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
6 changes: 6 additions & 0 deletions .changeset/fluffy-phones-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@sphinx-labs/plugins': patch
'@sphinx-labs/core': patch
---

Set transaction gas limit lower than block gas limit in simulation
21 changes: 14 additions & 7 deletions packages/core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,20 @@ export const getGasPriceOverrides = async (
}

if (!isLiveNetwork_) {
// Hard-code the gas limit to be the block gas limit. This is an optimization that significantly
// speeds up deployments on local networks because it removes the need for EthersJS to call
// `eth_estimateGas`, which is a very slow operation for large transactions. We don't override
// this on live networks because the signer is the user's wallet, which may have a limited
// amount of ETH. It's fine to set a very high gas limit on local networks because we use an
// auto-generated wallet to execute the transactions.
overridden.gasLimit = block.gasLimit
// Hard-code the gas limit to be the 3/4 of the block gas limit. This is an optimization that
// significantly speeds up deployments on local networks because it removes the need for
// EthersJS to call `eth_estimateGas`, which is a very slow operation for large transactions. We
// don't override this on live networks because the signer is the user's wallet, which may have
// a limited amount of ETH. It's fine to set a very high gas limit on local networks because we
// use an auto-generated wallet to execute the transactions. We set it to 3/4 of the block gas
// limit for two reasons:
// 1. This value is considerably higher than the max batch size for `EXECUTE` Merkle leaves,
// which ensures that we don't accidentally underfund the transaction.
// 2. This value is considerably lower than the block gas limit. If, instead, we set this value
// equal to the current block gas limit, a situation could occur where the block gas limit
// decreases slightly after we set this value, which would cause an error due to the fact
// that the transaction's gas limit exceeds the block gas limit.
overridden.gasLimit = (BigInt(3) * block.gasLimit) / BigInt(4)
return overridden
}

Expand Down
4 changes: 3 additions & 1 deletion packages/plugins/src/hardhat/simulate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ export const simulate = async (

if (code !== 0) {
const networkName = getNetworkNameForChainId(BigInt(chainId))
throw new Error(`Simulation failed for ${networkName}: ${stderr}`)
throw new Error(
`Simulation failed for ${networkName} at block number ${block.number}. Reason:\n${stderr}`
)
}

const receipts = JSON.parse(stdout).receipts
Expand Down

0 comments on commit 5cbecd5

Please sign in to comment.