-
Notifications
You must be signed in to change notification settings - Fork 598
feat: add network config #17312
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: add network config #17312
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import { createLogger } from '@aztec/aztec.js'; | ||
|
|
||
| import { mkdir, readFile, stat, writeFile } from 'fs/promises'; | ||
| import { dirname } from 'path'; | ||
|
|
||
| export interface CachedFetchOptions { | ||
| /** Cache duration in milliseconds */ | ||
| cacheDurationMs: number; | ||
| /** The cache file */ | ||
| cacheFile?: string; | ||
| } | ||
|
|
||
| /** | ||
| * Fetches data from a URL with file-based caching support. | ||
| * This utility can be used by both remote config and bootnodes fetching. | ||
| * | ||
| * @param url - The URL to fetch from | ||
| * @param networkName - Network name for cache directory structure | ||
| * @param options - Caching and error handling options | ||
| * @param cacheDir - Optional cache directory (defaults to no caching) | ||
| * @returns The fetched and parsed JSON data, or undefined if fetch fails and throwOnError is false | ||
| */ | ||
| export async function cachedFetch<T = any>( | ||
| url: string, | ||
| options: CachedFetchOptions, | ||
| fetch = globalThis.fetch, | ||
| log = createLogger('cached_fetch'), | ||
| ): Promise<T | undefined> { | ||
| const { cacheDurationMs, cacheFile } = options; | ||
|
|
||
| // Try to read from cache first | ||
| try { | ||
| if (cacheFile) { | ||
| const info = await stat(cacheFile); | ||
| if (info.mtimeMs + cacheDurationMs > Date.now()) { | ||
| const cachedData = JSON.parse(await readFile(cacheFile, 'utf-8')); | ||
| return cachedData; | ||
| } | ||
| } | ||
| } catch { | ||
| log.trace('Failed to read data from cache'); | ||
| } | ||
|
|
||
| try { | ||
| const response = await fetch(url); | ||
| if (!response.ok) { | ||
| log.warn(`Failed to fetch from ${url}: ${response.status} ${response.statusText}`); | ||
| return undefined; | ||
| } | ||
|
|
||
| const data = await response.json(); | ||
|
|
||
| try { | ||
| if (cacheFile) { | ||
| await mkdir(dirname(cacheFile), { recursive: true }); | ||
| await writeFile(cacheFile, JSON.stringify(data), 'utf-8'); | ||
| } | ||
| } catch (err) { | ||
| log.warn('Failed to cache data on disk: ' + cacheFile, { cacheFile, err }); | ||
| } | ||
|
|
||
| return data; | ||
| } catch (err) { | ||
| log.warn(`Failed to fetch from ${url}`, { err }); | ||
| return undefined; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import { EthAddress } from '@aztec/aztec.js'; | ||
| import type { EnvVar } from '@aztec/foundation/config'; | ||
|
|
||
| export function enrichVar(envVar: EnvVar, value: string | undefined) { | ||
| // Don't override | ||
| if (process.env[envVar] || value === undefined) { | ||
| return; | ||
| } | ||
| process.env[envVar] = value; | ||
| } | ||
|
|
||
| export function enrichEthAddressVar(envVar: EnvVar, value: string) { | ||
| // EthAddress doesn't like being given empty strings | ||
| enrichVar(envVar, value || EthAddress.ZERO.toString()); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,4 @@ | ||
| export * from './cached_fetch.js'; | ||
| export * from './chain_l2_config.js'; | ||
| export * from './get_l1_config.js'; | ||
| export * from './network_config.js'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| import { type NetworkConfig, NetworkConfigMapSchema, type NetworkNames } from '@aztec/foundation/config'; | ||
|
|
||
| import { readFile } from 'fs/promises'; | ||
| import { join } from 'path'; | ||
|
|
||
| import { cachedFetch } from './cached_fetch.js'; | ||
| import { enrichEthAddressVar, enrichVar } from './enrich_env.js'; | ||
|
|
||
| const DEFAULT_CONFIG_URL = | ||
| 'https://raw.githubusercontent.com/AztecProtocol/networks/refs/heads/main/network_config.json'; | ||
| const NETWORK_CONFIG_CACHE_DURATION_MS = 60 * 60 * 1000; // 1 hour | ||
|
|
||
| /** | ||
| * Fetches remote network configuration from GitHub with caching support. | ||
| * Uses the reusable cachedFetch utility. | ||
| * | ||
| * @param networkName - The network name to fetch config for | ||
| * @param cacheDir - Optional cache directory for storing fetched config | ||
| * @returns Remote configuration for the specified network, or undefined if not found/error | ||
| */ | ||
| export async function getNetworkConfig( | ||
| networkName: NetworkNames, | ||
| cacheDir?: string, | ||
| ): Promise<NetworkConfig | undefined> { | ||
| let url: URL | undefined; | ||
| const configLocation = process.env.NETWORK_CONFIG_LOCATION || DEFAULT_CONFIG_URL; | ||
|
|
||
| if (!configLocation) { | ||
| return undefined; | ||
| } | ||
|
|
||
| try { | ||
| if (configLocation.includes('://')) { | ||
| url = new URL(configLocation); | ||
| } else { | ||
| url = new URL(`file://${configLocation}`); | ||
| } | ||
| } catch { | ||
| /* no-op */ | ||
| } | ||
|
|
||
| if (!url) { | ||
| return undefined; | ||
| } | ||
|
|
||
| try { | ||
| let rawConfig: any; | ||
|
|
||
| if (url.protocol === 'http:' || url.protocol === 'https:') { | ||
| rawConfig = await cachedFetch(url.href, { | ||
| cacheDurationMs: NETWORK_CONFIG_CACHE_DURATION_MS, | ||
| cacheFile: cacheDir ? join(cacheDir, networkName, 'network_config.json') : undefined, | ||
| }); | ||
| } else if (url.protocol === 'file:') { | ||
| rawConfig = JSON.parse(await readFile(url.pathname, 'utf-8')); | ||
| } else { | ||
| throw new Error('Unsupported Aztec network config protocol: ' + url.href); | ||
| } | ||
|
|
||
| if (!rawConfig) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const networkConfigMap = NetworkConfigMapSchema.parse(rawConfig); | ||
| if (networkName in networkConfigMap) { | ||
| return networkConfigMap[networkName]; | ||
| } else { | ||
| return undefined; | ||
| } | ||
| } catch { | ||
| return undefined; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Enriches environment variables with remote network configuration. | ||
| * This function is called before node config initialization to set env vars | ||
| * from the remote config, following the same pattern as enrichEnvironmentWithChainConfig(). | ||
| * | ||
| * @param networkName - The network name to fetch remote config for | ||
| */ | ||
| export async function enrichEnvironmentWithNetworkConfig(networkName: NetworkNames) { | ||
| if (networkName === 'local') { | ||
| return; // No remote config for local development | ||
| } | ||
|
|
||
| const cacheDir = process.env.DATA_DIRECTORY ? join(process.env.DATA_DIRECTORY, 'cache') : undefined; | ||
| const networkConfig = await getNetworkConfig(networkName, cacheDir); | ||
|
|
||
| if (!networkConfig) { | ||
| return; | ||
| } | ||
|
|
||
| enrichVar('BOOTSTRAP_NODES', networkConfig.bootnodes.join(',')); | ||
| enrichVar('L1_CHAIN_ID', String(networkConfig.l1ChainId)); | ||
|
|
||
| // Snapshot synch only supports a single source. Take the first | ||
| // See A-101 for more details | ||
| const firstSource = networkConfig[0]; | ||
| if (firstSource) { | ||
| enrichVar('SYNC_SNAPSHOTS_URL', networkConfig.snapshots.join(',')); | ||
| } | ||
|
|
||
| enrichEthAddressVar('REGISTRY_CONTRACT_ADDRESS', networkConfig.registryAddress.toString()); | ||
| if (networkConfig.feeAssetHandlerAddress) { | ||
| enrichEthAddressVar('FEE_ASSET_HANDLER_CONTRACT_ADDRESS', networkConfig.feeAssetHandlerAddress.toString()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.