feat(envTests): Support using secret rpcs for CI env tests (#4408)

### Description

- Use Arbitrum and Optimism private RPCs that are set in Github Secrets
for env-test
- Increase env-test timeout to 4 mins for viction
- Adapt fork script so that it create anvil instance by calling a script

### Testing

Manual
pull/4122/head
Mohammed Hussan 3 months ago committed by GitHub
parent f30ba0efa4
commit 40974c7ec5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 6
      .github/workflows/test.yml
  2. 4
      typescript/infra/fork.sh
  3. 61
      typescript/infra/scripts/run-anvil.ts
  4. 38
      typescript/infra/src/config/chain.ts

@ -406,7 +406,11 @@ jobs:
env-test:
runs-on: ubuntu-latest
timeout-minutes: 3
env:
MAINNET3_ARBITRUM_RPC_URLS: ${{ secrets.MAINNET3_ARBITRUM_RPC_URLS }}
MAINNET3_OPTIMISM_RPC_URLS: ${{ secrets.MAINNET3_OPTIMISM_RPC_URLS }}
timeout-minutes: 4
needs: [yarn-build]
strategy:
fail-fast: false

@ -16,9 +16,7 @@ trap 'jobs -p | xargs -r kill' EXIT
# exit 1 on any subsequent failures
set -e
RPC_URL=`LOG_LEVEL=error yarn tsx ./scripts/print-chain-metadatas.ts -e $ENVIRONMENT | jq -r ".$CHAIN.rpcUrls[0].http"`
anvil --fork-url $RPC_URL --fork-retry-backoff 3 --compute-units-per-second 200 --gas-price 1 --silent &
LOG_LEVEL=error yarn tsx ./scripts/run-anvil.ts -e $ENVIRONMENT -c $CHAIN &
ANVIL_PID=$!
while ! cast bn &> /dev/null; do

@ -0,0 +1,61 @@
import { spawn } from 'child_process';
import { getArgs, withChainRequired } from './agent-utils.js';
import { getEnvironmentConfig } from './core-utils.js';
async function getRpcUrl() {
const { environment, chain } = await withChainRequired(getArgs()).argv;
const environmentConfig = getEnvironmentConfig(environment);
const registry = await environmentConfig.getRegistry(true);
const chainMetadata = await registry.getChainMetadata(chain);
if (!chainMetadata) {
throw Error(`Unsupported chain: ${chain}`);
}
return chainMetadata.rpcUrls[0].http;
}
async function runAnvil(rpcUrl: string) {
const anvilProcess = spawn(
'anvil',
[
'--fork-url',
rpcUrl,
'--fork-retry-backoff',
'3',
'--compute-units-per-second',
'200',
'--gas-price',
'1',
],
{
detached: false,
stdio: ['ignore', 'ignore', 'ignore'],
},
);
anvilProcess.on('exit', (code) => {
console.log(`Anvil process exited with code ${code}`);
process.exit(code || 0);
});
anvilProcess.on('error', (err) => {
console.error(`Failed to start Anvil: ${err}`);
process.exit(1);
});
return anvilProcess.pid;
}
async function main() {
const rpcUrl = await getRpcUrl();
const anvilPid = await runAnvil(rpcUrl);
console.log(`Anvil PID: ${anvilPid}`);
}
main().catch((err) => {
console.error(err);
process.exit(1);
});

@ -29,7 +29,7 @@ export async function fetchProvider(
throw Error(`Unsupported chain: ${chainName}`);
}
const chainId = chainMetadata.chainId;
let rpcData = chainMetadata.rpcUrls.map((url) => url.http);
const rpcData = chainMetadata.rpcUrls.map((url) => url.http);
if (rpcData.length === 0) {
throw Error(`No RPC URLs found for chain: ${chainName}`);
}
@ -81,10 +81,12 @@ export async function getRegistryForEnvironment(
useSecrets: boolean = true,
): Promise<IRegistry> {
let overrides = defaultChainMetadataOverrides;
if (useSecrets && !inCIMode()) {
if (useSecrets) {
overrides = objMerge(
overrides,
await getSecretMetadataOverrides(deployEnv, chains),
!inCIMode()
? await getSecretMetadataOverrides(deployEnv, chains)
: await getSecretMetadataOverridesFromGitHubSecrets(deployEnv, chains),
);
}
const registry = getRegistryWithOverrides(overrides);
@ -128,3 +130,33 @@ export async function getSecretMetadataOverrides(
return chainMetadataOverrides;
}
/**
* Gets chain metadata overrides from GitHub secrets.
* This function is intended to be used when running in CI/CD environments,
* where secrets are injected as environment variables.
* @param deployEnv The deploy environment.
* @param chains The chains to get metadata overrides for.
* @returns A partial chain metadata map with the secret overrides.
*/
export async function getSecretMetadataOverridesFromGitHubSecrets(
deployEnv: DeployEnvironment,
chains: string[],
): Promise<ChainMap<Partial<ChainMetadata>>> {
const chainMetadataOverrides: ChainMap<Partial<ChainMetadata>> = {};
for (const chain of chains) {
const rpcUrlsEnv = `${deployEnv.toUpperCase()}_${chain.toUpperCase()}_RPC_URLS`;
const rpcUrls = process.env[rpcUrlsEnv];
if (rpcUrls) {
const metadataRpcUrls = rpcUrls
.split(',')
.map((rpcUrl) => ({ http: rpcUrl })) as ChainMetadata['rpcUrls'];
chainMetadataOverrides[chain] = {
rpcUrls: metadataRpcUrls,
};
}
}
return chainMetadataOverrides;
}

Loading…
Cancel
Save