From 08f3b9b32bbcd4e2b2f299eed5add3c1a9c1e332 Mon Sep 17 00:00:00 2001 From: Asa Oines Date: Fri, 31 Mar 2023 11:17:50 -0400 Subject: [PATCH] Write agent config for core and IGP deploys (#2051) ### Description This PR fixes the broken E2E tests by writing agent config after core *or* IGP deploys. Rather than reading contract addresses from the deployed contracts, agent configs are built from the cached contract addresses in the SDK. Therefore, we must write (but not read) contract addresses when deploying to the test environment. --- typescript/infra/scripts/deploy.ts | 18 +++++++-- typescript/infra/src/core/deploy.ts | 43 --------------------- typescript/infra/src/deploy.ts | 60 +++++++++++++++++++++++++++-- 3 files changed, 71 insertions(+), 50 deletions(-) diff --git a/typescript/infra/scripts/deploy.ts b/typescript/infra/scripts/deploy.ts index d128bb662..2293d87de 100644 --- a/typescript/infra/scripts/deploy.ts +++ b/typescript/infra/scripts/deploy.ts @@ -115,11 +115,21 @@ async function main() { const verification = path.join(modulePath, 'verification.json'); - // do not cache for test environment - const cache = - environment === 'test' ? undefined : { addresses, verification }; + const cache = { + addresses, + verification, + read: environment !== 'test', + write: true, + }; + const agentConfig = ['core', 'igp'].includes(module) + ? { + addresses, + environment, + multiProvider, + } + : undefined; - await deployWithArtifacts(deployer, cache, fork); + await deployWithArtifacts(deployer, cache, fork, agentConfig); } main() diff --git a/typescript/infra/src/core/deploy.ts b/typescript/infra/src/core/deploy.ts index c256b4740..b50dfd06c 100644 --- a/typescript/infra/src/core/deploy.ts +++ b/typescript/infra/src/core/deploy.ts @@ -5,24 +5,15 @@ import { ChainMap, ChainName, CoreConfig, - CoreContracts, - HyperlaneAgentAddresses, HyperlaneCoreDeployer, MultiProvider, ProxiedContract, TransparentProxyAddresses, - buildAgentConfig, - objMap, - promiseObjAll, - serializeContracts, } from '@hyperlane-xyz/sdk'; import { DeployOptions } from '@hyperlane-xyz/sdk/dist/deploy/HyperlaneDeployer'; import { types } from '@hyperlane-xyz/utils'; -import { getAgentConfigDirectory } from '../../scripts/utils'; import { DeployEnvironment } from '../config'; -import { deployEnvToSdkEnv } from '../config/environment'; -import { writeJSON } from '../utils/utils'; export class HyperlaneCoreInfraDeployer extends HyperlaneCoreDeployer { environment: DeployEnvironment; @@ -36,40 +27,6 @@ export class HyperlaneCoreInfraDeployer extends HyperlaneCoreDeployer { this.environment = environment; } - protected async writeAgentConfig() { - // Write agent config indexing from the deployed or latest block numbers. - // For non-net-new deployments, these changes will need to be - // reverted manually. - const startBlocks = await promiseObjAll( - objMap(this.deployedContracts, async (chain, contracts) => { - const latest = await this.multiProvider - .getProvider(chain) - .getBlockNumber(); - const deployedBlocks = Object.values(contracts).map( - (c) => c.deployTransaction?.blockNumber ?? latest, - ); - return Math.min(...deployedBlocks); - }), - ); - const addresses = serializeContracts( - this.deployedContracts, - ) as ChainMap; - const agentConfig = buildAgentConfig( - this.multiProvider.getKnownChainNames(), - this.multiProvider, - addresses, - startBlocks, - ); - const sdkEnv = deployEnvToSdkEnv[this.environment]; - writeJSON(getAgentConfigDirectory(), `${sdkEnv}_config.json`, agentConfig); - } - - async deploy(): Promise> { - const result = await super.deploy(); - await this.writeAgentConfig(); - return result; - } - async deployMailbox( chain: ChainName, defaultIsmAddress: types.Address, diff --git a/typescript/infra/src/deploy.ts b/typescript/infra/src/deploy.ts index 8d26fc5fe..42dd1f8c1 100644 --- a/typescript/infra/src/deploy.ts +++ b/typescript/infra/src/deploy.ts @@ -1,25 +1,72 @@ import { + ChainMap, ChainName, + HyperlaneAddresses, + HyperlaneAgentAddresses, HyperlaneDeployer, + MultiProvider, + buildAgentConfig, buildContracts, + objMap, + promiseObjAll, serializeContracts, } from '@hyperlane-xyz/sdk'; +import { getAgentConfigDirectory } from '../scripts/utils'; + +import { DeployEnvironment, deployEnvToSdkEnv } from './config/environment'; import { readJSONAtPath, + writeJSON, writeJsonAtPath, writeMergedJSONAtPath, } from './utils/utils'; +export async function writeAgentConfig( + addressesPath: string, + multiProvider: MultiProvider, + environment: DeployEnvironment, +) { + let addresses: ChainMap = {}; + try { + addresses = readJSONAtPath(addressesPath); + } catch (e) { + console.error('Failed to load cached addresses'); + } + // Write agent config indexing from the deployed or latest block numbers. + // For non-net-new deployments, these changes will need to be + // reverted manually. + const startBlocks = await promiseObjAll( + objMap(addresses, (chain, _) => + multiProvider.getProvider(chain).getBlockNumber(), + ), + ); + const agentConfig = buildAgentConfig( + multiProvider.getKnownChainNames(), + multiProvider, + addresses as ChainMap, + startBlocks, + ); + const sdkEnv = deployEnvToSdkEnv[environment]; + writeJSON(getAgentConfigDirectory(), `${sdkEnv}_config.json`, agentConfig); +} + export async function deployWithArtifacts( deployer: HyperlaneDeployer, - cache?: { + cache: { addresses: string; verification: string; + read: boolean; + write: boolean; }, fork?: ChainName, + agentConfig?: { + multiProvider: MultiProvider; + addresses: string; + environment: DeployEnvironment; + }, ) { - if (cache) { + if (cache.read) { let addresses = {}; try { addresses = readJSONAtPath(cache.addresses); @@ -41,7 +88,7 @@ export async function deployWithArtifacts( console.error('Failed to deploy contracts', e); } - if (cache) { + if (cache.write) { // cache addresses of deployed contracts writeMergedJSONAtPath( cache.addresses, @@ -60,4 +107,11 @@ export async function deployWithArtifacts( deployer.mergeWithExistingVerificationInputs(savedVerification); writeJsonAtPath(cache.verification, inputs); } + if (agentConfig) { + await writeAgentConfig( + agentConfig.addresses, + agentConfig.multiProvider, + agentConfig.environment, + ); + } }