Skip to content

Commit

Permalink
fix(pg): write canonical config to file system when using executing b…
Browse files Browse the repository at this point in the history
…undles locally
  • Loading branch information
sam-goldman committed Dec 21, 2022
1 parent 045ffed commit dba31f7
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 31 deletions.
7 changes: 7 additions & 0 deletions .changeset/sour-shrimps-work.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@chugsplash/core': patch
'@chugsplash/executor': patch
'@chugsplash/plugins': patch
---

Write canonical config to file system when using executing bundles locally
38 changes: 37 additions & 1 deletion packages/core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
ProxyABI,
} from '@chugsplash/contracts'

import { ParsedChugSplashConfig } from './config'
import { CanonicalChugSplashConfig, ParsedChugSplashConfig } from './config'
import { ChugSplashActionBundle, ChugSplashActionType } from './actions'

export const computeBundleId = (
Expand Down Expand Up @@ -363,3 +363,39 @@ export const formatEther = (
): string => {
return parseFloat(ethers.utils.formatEther(amount)).toFixed(decimals)
}

export const readCanonicalConfig = (
canonicalConfigFolderPath: string,
bundleId: string
): CanonicalChugSplashConfig => {
// Check that the file containing the canonical config exists.
const canonicalConfigPath = path.join(
canonicalConfigFolderPath,
`${bundleId}.json`
)
if (!fs.existsSync(canonicalConfigPath)) {
throw new Error(
`Could not find local bundle ID file. Please report this error.`
)
}

return JSON.parse(fs.readFileSync(canonicalConfigPath, 'utf8'))
}

export const writeCanonicalConfig = (
canonicalConfigFolderPath: string,
bundleId: string,
canonicalConfig: CanonicalChugSplashConfig
) => {
// Create the canonical config folder if it doesn't already exist.
if (!fs.existsSync(canonicalConfigFolderPath)) {
fs.mkdirSync(canonicalConfigFolderPath)
}

// Write the canonical config to the local file system. It will exist in a JSON file that has the
// bundle ID as its name.
fs.writeFileSync(
path.join(canonicalConfigFolderPath, `${bundleId}.json`),
JSON.stringify(canonicalConfig, null, 2)
)
}
26 changes: 19 additions & 7 deletions packages/executor/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import {
initializeChugSplash,
getProjectOwnerAddress,
ChugSplashBundleState,
ChugSplashActionBundle,
readCanonicalConfig,
} from '@chugsplash/core'
import * as Amplitude from '@amplitude/node'

Expand All @@ -28,6 +30,7 @@ import {
verifyChugSplash,
verifyChugSplashConfig,
isSupportedNetworkOnEtherscan,
bundleRemoteSubtask,
} from './utils'

export * from './utils'
Expand Down Expand Up @@ -164,7 +167,7 @@ export class ChugSplashExecutor extends BaseServiceV2<Options, Metrics, State> {
await this.setup(this.options)
}

async main(localCanonicalConfig?: CanonicalChugSplashConfig) {
async main(localBundleId?: string, canonicalConfigFolderPath?: string) {
const { provider, wallet, registry } = this.state

const latestBlockNumber = await provider.getBlockNumber()
Expand Down Expand Up @@ -226,12 +229,21 @@ export class ChugSplashExecutor extends BaseServiceV2<Options, Metrics, State> {

this.logger.info('[ChugSplash]: retrieving the bundle...')

// Compile the bundle using either the provided localCanonicalConfig (when running the
// executor from within the ChugSplash plugin), or using the Config URI
const { bundle, canonicalConfig } = await compileRemoteBundle(
proposalEvent.args.configUri,
localCanonicalConfig
)
// Compile the bundle using either the provided localBundleId (when running the in-process
// executor), or using the Config URI
let bundle: ChugSplashActionBundle
let canonicalConfig: CanonicalChugSplashConfig
if (localBundleId !== undefined) {
canonicalConfig = readCanonicalConfig(
canonicalConfigFolderPath,
localBundleId
)
bundle = await bundleRemoteSubtask({ canonicalConfig })
} else {
;({ bundle, canonicalConfig } = await compileRemoteBundle(
proposalEvent.args.configUri
))
}
const projectName = canonicalConfig.options.projectName

// ensure compiled bundle matches proposed bundle
Expand Down
8 changes: 2 additions & 6 deletions packages/executor/src/utils/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,12 @@ export const getArtifactsFromCanonicalConfig = async (
* @returns Compiled ChugSplashBundle.
*/
export const compileRemoteBundle = async (
configUri: string,
canonicalConfig?: CanonicalChugSplashConfig
configUri: string
): Promise<{
bundle: ChugSplashActionBundle
canonicalConfig: CanonicalChugSplashConfig
}> => {
// canonicalConfig is passed in when executing a local deployment
if (!canonicalConfig) {
canonicalConfig = await chugsplashFetchSubtask({ configUri })
}
const canonicalConfig = await chugsplashFetchSubtask({ configUri })

const bundle = await bundleRemoteSubtask({ canonicalConfig })
return { bundle, canonicalConfig }
Expand Down
25 changes: 12 additions & 13 deletions packages/plugins/src/hardhat/deployments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,16 @@ export const deployChugSplashConfig = async (
}

// Get the bundle ID without publishing anything to IPFS.
const { bundleId, bundle, configUri, canonicalConfig } =
await chugsplashCommitSubtask(
{
parsedConfig,
ipfsUrl,
commitToIpfs: false,
noCompile,
spinner,
},
hre
)
const { bundleId, bundle, configUri } = await chugsplashCommitSubtask(
{
parsedConfig,
ipfsUrl,
commitToIpfs: false,
noCompile,
spinner,
},
hre
)

spinner.start(`Checking the status of ${projectName}...`)

Expand Down Expand Up @@ -216,7 +215,7 @@ export const deployChugSplashConfig = async (
if (remoteExecution) {
await monitorExecution(hre, parsedConfig, bundle, bundleId, spinner)
} else {
// If executing locally, then startup executor with HRE provider and pass in canonical config
// Use the in-process executor if executing the bundle locally.
spinner.start('Executing project...')
const amountToDeposit = await getAmountToDeposit(
provider,
Expand All @@ -229,7 +228,7 @@ export const deployChugSplashConfig = async (
to: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
value: amountToDeposit,
})
await executor.main(canonicalConfig)
await executor.main(bundleId, hre.config.paths.canonicalConfigs)
spinner.succeed(`Executed ${projectName}.`)
}

Expand Down
17 changes: 13 additions & 4 deletions packages/plugins/src/hardhat/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
getAmountToDeposit,
EXECUTION_BUFFER_MULTIPLIER,
formatEther,
writeCanonicalConfig,
} from '@chugsplash/core'
import { ChugSplashManagerABI, ProxyABI } from '@chugsplash/contracts'
import ora from 'ora'
Expand Down Expand Up @@ -583,12 +584,11 @@ export const chugsplashCommitSubtask = async (
noCompile: boolean
spinner?: ora.Ora
},
hre
hre: HardhatRuntimeEnvironment
): Promise<{
bundle: ChugSplashActionBundle
configUri: string
bundleId: string
canonicalConfig: CanonicalChugSplashConfig
}> => {
const { parsedConfig, ipfsUrl, commitToIpfs, noCompile, spinner } = args

Expand Down Expand Up @@ -699,6 +699,15 @@ IPFS_API_KEY_SECRET: ...
configUri
)

// Write the canonical config to the local file system if we aren't committing it to IPFS.
if (!commitToIpfs) {
writeCanonicalConfig(
hre.config.paths.canonicalConfigs,
bundleId,
canonicalConfig
)
}

if (spinner) {
commitToIpfs
? spinner.succeed(
Expand All @@ -709,7 +718,7 @@ IPFS_API_KEY_SECRET: ...
)
}

return { bundle, configUri, bundleId, canonicalConfig }
return { bundle, configUri, bundleId }
}

subtask(TASK_CHUGSPLASH_COMMIT)
Expand Down Expand Up @@ -1700,7 +1709,7 @@ export const chugsplashInitTask = async (
args: {
silent: boolean
},
hre: any
hre: HardhatRuntimeEnvironment
) => {
const { silent } = args

Expand Down
5 changes: 5 additions & 0 deletions packages/plugins/src/hardhat/type-extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ declare module 'hardhat/types/config' {
export interface ProjectPathsConfig {
chugsplash: string
deployments: string
canonicalConfigs: string
}
}

Expand All @@ -35,6 +36,10 @@ declare module 'hardhat/types/runtime' {
extendConfig((config: HardhatConfig) => {
config.paths.chugsplash = path.join(config.paths.root, 'chugsplash')
config.paths.deployments = path.join(config.paths.root, 'deployments')
config.paths.canonicalConfigs = path.join(
config.paths.root,
'.canonical-configs'
)
})

extendEnvironment((hre: HardhatRuntimeEnvironment) => {
Expand Down

0 comments on commit dba31f7

Please sign in to comment.