Skip to content

Commit

Permalink
fix(core): assert valid bundle size
Browse files Browse the repository at this point in the history
  • Loading branch information
sam-goldman committed Apr 29, 2023
1 parent d7d6352 commit 1eeba58
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/sharp-rats-protect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@chugsplash/core': patch
---

Assert valid bundle size
47 changes: 47 additions & 0 deletions packages/core/src/config/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
getContractAddress,
isDataHexString,
isLiveNetwork,
isContractDeployed,
} from '../utils'
import {
UserChugSplashConfig,
Expand Down Expand Up @@ -2184,6 +2185,8 @@ export const parseAndValidateChugSplashConfig = async (
await assertContractsBelowSizeLimit(parsedConfig, cachedArtifacts, cre)
}

await assertValidBundleSize(provider, parsedConfig, cre)

// Complete misc pre-deploy validation
// I.e run storage slot checker + other safety checks, detect if the deployment is an upgrade, etc
const upgrade = await assertValidParsedChugSplashFile(
Expand Down Expand Up @@ -2214,6 +2217,50 @@ export const parseAndValidateChugSplashConfig = async (
return parsedConfig
}

/**
* Asserts that the ChugSplash config can be initiated in a single transaction.
*/
export const assertValidBundleSize = async (
provider: providers.Provider,
parsedConfig: ParsedChugSplashConfig,
cre: ChugSplashRuntimeEnvironment
) => {
const { gasLimit: blockGasLimit } = await provider.getBlock('latest')

const deployedProxyPromises = Object.values(parsedConfig.contracts).map(
async (contract) =>
contract.kind === 'internal-default' &&
!(await isContractDeployed(contract.address, provider))
? ethers.BigNumber.from(550_000)
: ethers.BigNumber.from(0)
)

const resolvedPromises = await Promise.all(deployedProxyPromises)

const defaultProxyGasCosts = resolvedPromises.reduce(
(a, b) => a.add(b),
ethers.BigNumber.from(0)
)

const numTargets = Object.values(parsedConfig.contracts).filter(
(contract) => contract.kind !== 'no-proxy'
).length
const initiationGasCost = ethers.BigNumber.from(100_000).mul(numTargets)

const totalInitiationGasCost = defaultProxyGasCosts.add(initiationGasCost)
const costWithBuffer = totalInitiationGasCost.mul(12).div(10)

if (costWithBuffer.gt(blockGasLimit)) {
logValidationError(
'error',
`Too many contracts in your ChugSplash config.`,
[],
cre.silent,
cre.stream
)
}
}

/**
* Asserts that the contracts in the parsed config are below the contract size limit (24576 bytes).
*/
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/fund.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ export const estimateExecutionGas = async (

const deployedProxyPromises = Object.values(parsedConfig.contracts).map(
async (contract) =>
// If the proxy has already been deployed, then estimate 0 gas. Otherwise, estimate 550k for the default proxy.
(await isContractDeployed(contract.address, provider))
? ethers.BigNumber.from(0)
: ethers.BigNumber.from(550_000)
contract.kind === 'internal-default' &&
!(await isContractDeployed(contract.address, provider))
? ethers.BigNumber.from(550_000)
: ethers.BigNumber.from(0)
)

const deployedContractPromises = actions
Expand Down

0 comments on commit 1eeba58

Please sign in to comment.