-
Notifications
You must be signed in to change notification settings - Fork 615
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,10 +4,11 @@ import { EthAddress } from '@aztec/foundation/eth-address'; | |
| import type { SharedNodeConfig } from '@aztec/node-lib/config'; | ||
| import type { SlasherConfig } from '@aztec/stdlib/interfaces/server'; | ||
|
|
||
| import { mkdir, readFile, stat, writeFile } from 'fs/promises'; | ||
| import path, { dirname, join } from 'path'; | ||
| import path, { join } from 'path'; | ||
|
|
||
| import publicIncludeMetrics from '../../public_include_metric_prefixes.json' with { type: 'json' }; | ||
| import { cachedFetch } from './cached_fetch.js'; | ||
| import { enrichEthAddressVar, enrichVar } from './enrich_env.js'; | ||
|
|
||
| const SNAPSHOT_URL = 'https://pub-f4a8c34d4bb7441ebf8f48d904512180.r2.dev/snapshots'; | ||
|
|
||
|
|
@@ -79,9 +80,9 @@ export const stagingIgnitionL2ChainConfig: L2ChainConfig = { | |
| sponsoredFPC: false, | ||
| p2pEnabled: true, | ||
| p2pBootstrapNodes: [], | ||
| registryAddress: '0x5f85fa0f40bc4b5ccd53c9f34258aa55d25cdde8', | ||
| slashFactoryAddress: '0x257db2ca1471b7f76f414d2997404bfbe916c8c9', | ||
| feeAssetHandlerAddress: '0x67d645b0a3e053605ea861d7e8909be6669812c4', | ||
| registryAddress: '0x53e2c2148da04fd0e8dd282f016f627a187c292c', | ||
| slashFactoryAddress: '0x56448efb139ef440438dfa445333734ea5ed60a0', | ||
| feeAssetHandlerAddress: '0x3613b834544030c166a4d47eca14b910f4816f57', | ||
| seqMinTxsPerBlock: 0, | ||
| seqMaxTxsPerBlock: 0, | ||
| realProofs: true, | ||
|
|
@@ -160,9 +161,9 @@ export const stagingPublicL2ChainConfig: L2ChainConfig = { | |
| sponsoredFPC: true, | ||
| p2pEnabled: true, | ||
| p2pBootstrapNodes: [], | ||
| registryAddress: '0x2e48addca360da61e4d6c21ff2b1961af56eb83b', | ||
| slashFactoryAddress: '0xe19410632fd00695bc5a08dd82044b7b26317742', | ||
| feeAssetHandlerAddress: '0xb46dc3d91f849999330b6dd93473fa29fc45b076', | ||
| registryAddress: '0xe83067689f3cf837ccbf8a3966f0e0fe985dcb3e', | ||
| slashFactoryAddress: '0x8b87a1812162d4890f01bb40f410047f37d3ceb8', | ||
| feeAssetHandlerAddress: '0xa8159159a9e2a57c6e8c59fd5b3dd94c6dbddfe3', | ||
| seqMinTxsPerBlock: 0, | ||
| seqMaxTxsPerBlock: 20, | ||
| realProofs: true, | ||
|
|
@@ -265,37 +266,13 @@ export const testnetL2ChainConfig: L2ChainConfig = { | |
| const BOOTNODE_CACHE_DURATION_MS = 60 * 60 * 1000; // 1 hour; | ||
|
|
||
| export async function getBootnodes(networkName: NetworkNames, cacheDir?: string) { | ||
| const cacheFile = cacheDir ? join(cacheDir, networkName, 'bootnodes.json') : undefined; | ||
| try { | ||
| if (cacheFile) { | ||
| const info = await stat(cacheFile); | ||
| if (info.mtimeMs + BOOTNODE_CACHE_DURATION_MS > Date.now()) { | ||
| return JSON.parse(await readFile(cacheFile, 'utf-8'))['bootnodes']; | ||
| } | ||
| } | ||
| } catch { | ||
| // no-op. Get the remote-file | ||
| } | ||
|
|
||
| const url = `http://static.aztec.network/${networkName}/bootnodes.json`; | ||
| const response = await fetch(url); | ||
| if (!response.ok) { | ||
| throw new Error( | ||
| `Failed to fetch basic contract addresses from ${url}. Check you are using a correct network name.`, | ||
| ); | ||
| } | ||
| const json = await response.json(); | ||
|
|
||
| try { | ||
| if (cacheFile) { | ||
| await mkdir(dirname(cacheFile), { recursive: true }); | ||
| await writeFile(cacheFile, JSON.stringify(json), 'utf-8'); | ||
| } | ||
| } catch { | ||
| // no-op | ||
| } | ||
| const data = await cachedFetch(url, { | ||
| cacheDurationMs: BOOTNODE_CACHE_DURATION_MS, | ||
| cacheFile: cacheDir ? join(cacheDir, networkName, 'bootnodes.json') : undefined, | ||
| }); | ||
|
|
||
| return json['bootnodes']; | ||
| return data?.bootnodes; | ||
| } | ||
|
Comment on lines
+270
to
276
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We no longer crash if the bootnodes file is not available (fix A-61), so I've added a warning if starting a P2P client with 0 boot nodes defined. |
||
|
|
||
| export async function getL2ChainConfig( | ||
|
|
@@ -321,23 +298,6 @@ export async function getL2ChainConfig( | |
| return config; | ||
| } | ||
|
|
||
| function enrichVar(envVar: EnvVar, value: string | undefined) { | ||
| // Don't override | ||
| if (process.env[envVar] || value === undefined) { | ||
| return; | ||
| } | ||
| process.env[envVar] = value; | ||
| } | ||
|
|
||
| function enrichEthAddressVar(envVar: EnvVar, value: string) { | ||
| // EthAddress doesn't like being given empty strings | ||
| if (value === '') { | ||
| enrichVar(envVar, EthAddress.ZERO.toString()); | ||
| return; | ||
| } | ||
| enrichVar(envVar, value); | ||
| } | ||
|
|
||
| function getDefaultDataDir(networkName: NetworkNames): string { | ||
| return path.join(process.env.HOME || '~', '.aztec', networkName, 'data'); | ||
| } | ||
|
|
||
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.