Skip to content

Commit 60a74b6

Browse files
koenmtb1claude
andcommitted
fix: cache contract start blocks and fail over between L1 endpoints
The previous commit made discovery derive a deployment block per contract, which turned one ~25-call binary search into seven. Against Sepolia's public Tenderly gateway that exhausted the rate limit outright and the deploy failed at `aztec_node : Discover` — mainnet succeeded, testnet did not. Retrying does not help once an endpoint's budget is gone; that endpoint is currently answering 429 to a single eth_blockNumber. Two changes so routine deploys stop paying for information that cannot change. Cache the searches. A contract's deployment block is immutable, so it now lives in the committed config.json and is only re-derived when that contract's address actually changes — i.e. on an upgrade, for the one or two contracts that were redeployed. Verified against a mock: a steady-state run issues zero eth_getCode calls, and a simulated upgrade re-derives exactly the two changed contracts while the five survivors stay cached. This reverses the earlier decision to commit zeroed addresses. Discovery still verifies every address against the node on each deploy and rewrites any that disagree, so a stale value self-corrects; an absent one costs ~175 RPC calls. The zero-address guard in scripts/env/utils.js still blocks propagation if discovery has not run. Fail over between endpoints. Discovery used only rpc.ethereumUrls[0]. Calls at `latest` now try each configured endpoint in turn, which matters on testnet where the second is a pruning node: pruning only affects historical state, so it serves eth_call and eth_blockNumber fine. The deployment-block search opts out and stays on the primary, since it is the one caller that needs history. Testnet start blocks are seeded from the 9500000 floor this repo used before per-contract blocks existed (commit 6ed974f) rather than measured, because its L1 endpoint cannot currently sustain the search. Correct but wasteful — worth replacing with measured values once testnet has an endpoint that can. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent fbdacdc commit 60a74b6

5 files changed

Lines changed: 162 additions & 46 deletions

File tree

‎.environment/mainnet/config.example.json‎

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@
2626
"memLimit": "8g"
2727
},
2828
"contracts": {
29-
"rollupAddress": "0x0000000000000000000000000000000000000000",
30-
"governanceAddress": "0x0000000000000000000000000000000000000000",
31-
"governanceProposerAddress": "0x0000000000000000000000000000000000000000",
32-
"slashingProposerAddress": "0x0000000000000000000000000000000000000000",
33-
"gseAddress": "0x0000000000000000000000000000000000000000",
29+
"rollupAddress": "0x91ff8bbd8ebb07893010d50a48a1609e5ebd8e34",
30+
"governanceAddress": "0x1102471eb3378fee427121c9efcea452e4b6b75e",
31+
"governanceProposerAddress": "0x06ef1dcf87e419c48b94a331b252819fadbd63ef",
32+
"slashingProposerAddress": "0x8a36b8f2ca71d8d8bd98e03ebf8b4d0939daf0ba",
33+
"gseAddress": "0xa92ecfd0e70c9cd5e5cd76c50af0f7da93567a4f",
3434
"stakingRegistryAddress": "0x042dF8f42790d6943F41C25C2132400fd727f452",
35-
"registryAddress": "0x0000000000000000000000000000000000000000",
36-
"_comment": "Discovered by `pnpm env:discover mainnet` from the Aztec node (node_getL1ContractAddresses) plus Rollup.getSlasher()/Slasher.PROPOSER(). stakingRegistryAddress is not part of the Aztec core deployment and must be set by hand."
35+
"registryAddress": "0x35b22e09ee0390539439e24f06da43d83f90e298",
36+
"_comment": "Verified against the Aztec node on every deploy by `pnpm env:discover mainnet` and rewritten if the chain disagrees. Committed rather than zeroed so the deploy can skip the ~25-call deployment-block search per contract; a wrong value here is corrected automatically, an absent one costs ~175 RPC calls. stakingRegistryAddress is not part of the Aztec core deployment and is not discovered."
3737
},
3838
"collectors": {
3939
"validatorStats": {
@@ -69,8 +69,16 @@
6969
"ponder": {
7070
"port": 42069,
7171
"maxHealthcheckDuration": 240,
72-
"startBlock": 0,
73-
"startBlocks": {},
72+
"startBlock": 25428902,
73+
"startBlocks": {
74+
"governance": 23786829,
75+
"governanceProposer": 23786828,
76+
"gse": 23786826,
77+
"registry": 23786827,
78+
"rollup": 25428902,
79+
"slashingProposer": 25428902,
80+
"stakingRegistry": 23786843
81+
},
7482
"_comment": "startBlock and startBlocks are set by `pnpm env:discover mainnet`. A rollup upgrade redeploys only the rollup and its slashing proposer; the GSE, registry, governance, governance proposer and staking registry keep their addresses, so each is indexed from its own deployment block. Indexing them all from the new rollup discards their earlier events."
7583
},
7684
"app": {

‎.environment/mainnet/config.json‎

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,14 @@
2626
"memLimit": "8g"
2727
},
2828
"contracts": {
29-
"rollupAddress": "0x0000000000000000000000000000000000000000",
30-
"governanceAddress": "0x0000000000000000000000000000000000000000",
31-
"governanceProposerAddress": "0x0000000000000000000000000000000000000000",
32-
"slashingProposerAddress": "0x0000000000000000000000000000000000000000",
33-
"gseAddress": "0x0000000000000000000000000000000000000000",
29+
"rollupAddress": "0x91ff8bbd8ebb07893010d50a48a1609e5ebd8e34",
30+
"governanceAddress": "0x1102471eb3378fee427121c9efcea452e4b6b75e",
31+
"governanceProposerAddress": "0x06ef1dcf87e419c48b94a331b252819fadbd63ef",
32+
"slashingProposerAddress": "0x8a36b8f2ca71d8d8bd98e03ebf8b4d0939daf0ba",
33+
"gseAddress": "0xa92ecfd0e70c9cd5e5cd76c50af0f7da93567a4f",
3434
"stakingRegistryAddress": "0x042dF8f42790d6943F41C25C2132400fd727f452",
35-
"registryAddress": "0x0000000000000000000000000000000000000000"
35+
"registryAddress": "0x35b22e09ee0390539439e24f06da43d83f90e298",
36+
"_comment": "Verified against the Aztec node on every deploy by `pnpm env:discover mainnet` and rewritten if the chain disagrees. Committed rather than zeroed so the deploy can skip the ~25-call deployment-block search per contract; a wrong value here is corrected automatically, an absent one costs ~175 RPC calls. stakingRegistryAddress is not part of the Aztec core deployment and is not discovered."
3637
},
3738
"collectors": {
3839
"validatorStats": {
@@ -68,8 +69,16 @@
6869
"ponder": {
6970
"port": 42069,
7071
"maxHealthcheckDuration": 240,
71-
"startBlock": 0,
72-
"startBlocks": {},
72+
"startBlock": 25428902,
73+
"startBlocks": {
74+
"governance": 23786829,
75+
"governanceProposer": 23786828,
76+
"gse": 23786826,
77+
"registry": 23786827,
78+
"rollup": 25428902,
79+
"slashingProposer": 25428902,
80+
"stakingRegistry": 23786843
81+
},
7382
"_comment": "startBlock and startBlocks are set by `pnpm env:discover mainnet`. A rollup upgrade redeploys only the rollup and its slashing proposer; the GSE, registry, governance, governance proposer and staking registry keep their addresses, so each is indexed from its own deployment block. Indexing them all from the new rollup discards their earlier events."
7483
},
7584
"app": {

‎.environment/testnet/config.example.json‎

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@
2626
"memLimit": "6g"
2727
},
2828
"contracts": {
29-
"rollupAddress": "0x0000000000000000000000000000000000000000",
30-
"governanceAddress": "0x0000000000000000000000000000000000000000",
31-
"governanceProposerAddress": "0x0000000000000000000000000000000000000000",
29+
"rollupAddress": "0xd73a91bdcf6891c7642f3e460036e1ef2cc23178",
30+
"governanceAddress": "0xcaf7447721447b22cd0076ac7c63877c3afd329f",
31+
"governanceProposerAddress": "0x01c7d4ca153748d2377968fef22894cb162e9480",
3232
"slashingProposerAddress": "0x0000000000000000000000000000000000000000",
33-
"gseAddress": "0x0000000000000000000000000000000000000000",
33+
"gseAddress": "0xb6a38a51a6c1de9012f9d8ea9745ef957212eaac",
3434
"stakingRegistryAddress": "0xa65f52BD0889BbE7DF0cE14709865e3E3bDDbEEa",
35-
"registryAddress": "0x0000000000000000000000000000000000000000",
36-
"_comment": "Discovered by `pnpm env:discover testnet` from the Aztec node (node_getL1ContractAddresses) plus Rollup.getSlasher()/Slasher.PROPOSER(). stakingRegistryAddress is not part of the Aztec core deployment and must be set by hand."
35+
"registryAddress": "0xa0bfb1b494fb49041e5c6e8c2c1be09cd171c6ba",
36+
"_comment": "Verified against the Aztec node on every deploy by `pnpm env:discover testnet` and rewritten if the chain disagrees. Committed rather than zeroed so the deploy can skip the ~25-call deployment-block search per contract. slashingProposerAddress is derived on-chain each run (Rollup.getSlasher -> Slasher.PROPOSER). stakingRegistryAddress is not part of the Aztec core deployment and is not discovered."
3737
},
3838
"collectors": {
3939
"validatorStats": {
@@ -69,9 +69,17 @@
6969
"ponder": {
7070
"port": 42069,
7171
"maxHealthcheckDuration": 240,
72-
"startBlock": 0,
73-
"startBlocks": {},
74-
"_comment": "startBlock and startBlocks are set by `pnpm env:discover testnet`. A rollup upgrade redeploys only the rollup and its slashing proposer; the GSE, registry, governance, governance proposer and staking registry keep their addresses, so each is indexed from its own deployment block. Indexing them all from the new rollup discards their earlier events."
72+
"startBlock": 11250166,
73+
"startBlocks": {
74+
"governance": 9500000,
75+
"governanceProposer": 9500000,
76+
"gse": 9500000,
77+
"registry": 9500000,
78+
"rollup": 11250166,
79+
"slashingProposer": 11250166,
80+
"stakingRegistry": 9500000
81+
},
82+
"_comment": "startBlock/startBlocks are verified by `pnpm env:discover testnet` and only re-derived when a contract address changes. Survivor blocks are the historical 9500000 floor rather than measured deployments \u2014 correct but wasteful; see contracts._comment."
7583
},
7684
"app": {
7785
"url": "https://testnet.dashtec.xyz",

‎.environment/testnet/config.json‎

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,14 @@
2626
"memLimit": "6g"
2727
},
2828
"contracts": {
29-
"rollupAddress": "0x0000000000000000000000000000000000000000",
30-
"governanceAddress": "0x0000000000000000000000000000000000000000",
31-
"governanceProposerAddress": "0x0000000000000000000000000000000000000000",
29+
"rollupAddress": "0xd73a91bdcf6891c7642f3e460036e1ef2cc23178",
30+
"governanceAddress": "0xcaf7447721447b22cd0076ac7c63877c3afd329f",
31+
"governanceProposerAddress": "0x01c7d4ca153748d2377968fef22894cb162e9480",
3232
"slashingProposerAddress": "0x0000000000000000000000000000000000000000",
33-
"gseAddress": "0x0000000000000000000000000000000000000000",
33+
"gseAddress": "0xb6a38a51a6c1de9012f9d8ea9745ef957212eaac",
3434
"stakingRegistryAddress": "0xC6EcC1832c8BF6a41c927BEb4E9ec610FBeDd1C2",
35-
"registryAddress": "0x0000000000000000000000000000000000000000"
35+
"registryAddress": "0xa0bfb1b494fb49041e5c6e8c2c1be09cd171c6ba",
36+
"_comment": "Verified against the Aztec node on every deploy by `pnpm env:discover testnet` and rewritten if the chain disagrees. Committed rather than zeroed so the deploy can skip the ~25-call deployment-block search per contract. slashingProposerAddress is derived on-chain each run (Rollup.getSlasher -> Slasher.PROPOSER). stakingRegistryAddress is not part of the Aztec core deployment and is not discovered."
3637
},
3738
"collectors": {
3839
"validatorStats": {
@@ -68,9 +69,17 @@
6869
"ponder": {
6970
"port": 42069,
7071
"maxHealthcheckDuration": 240,
71-
"startBlock": 0,
72-
"startBlocks": {},
73-
"_comment": "startBlock and startBlocks are set by `pnpm env:discover testnet`. A rollup upgrade redeploys only the rollup and its slashing proposer; the GSE, registry, governance, governance proposer and staking registry keep their addresses, so each is indexed from its own deployment block. Indexing them all from the new rollup discards their earlier events."
72+
"startBlock": 11250166,
73+
"startBlocks": {
74+
"governance": 9500000,
75+
"governanceProposer": 9500000,
76+
"gse": 9500000,
77+
"registry": 9500000,
78+
"rollup": 11250166,
79+
"slashingProposer": 11250166,
80+
"stakingRegistry": 9500000
81+
},
82+
"_comment": "startBlock/startBlocks are verified by `pnpm env:discover testnet` and only re-derived when a contract address changes. Survivor blocks are the historical 9500000 floor rather than measured deployments \u2014 correct but wasteful; see contracts._comment."
7483
},
7584
"app": {
7685
"url": "https://testnet.dashtec.xyz",

‎scripts/env/discover-contracts.js‎

Lines changed: 94 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,36 @@ if (l1Urls.length === 0) {
6363
}
6464
const l1Url = l1Urls[0];
6565

66+
/**
67+
* An L1 call, tried against each configured endpoint in turn.
68+
*
69+
* `rpc()` already retries a single endpoint through throttling, but that does not
70+
* help when an endpoint's budget is simply exhausted — Sepolia's public Tenderly
71+
* gateway answers 429 to even a single eth_blockNumber under load. The fallbacks
72+
* are usually pruning nodes, which is fine for `latest` reads: pruning only
73+
* affects historical state, and the deployment-block search below is the one
74+
* caller that needs history, so it opts out.
75+
*/
76+
async function l1Rpc(method, params, { historical = false } = {}) {
77+
const urls = historical ? [l1Url] : l1Urls;
78+
let lastError;
79+
for (const url of urls) {
80+
try {
81+
return await rpc(url, method, params);
82+
} catch (error) {
83+
lastError = error;
84+
if (urls.length > 1) {
85+
console.log(` ! ${method} failed on ${redact(url)} (${error.message}) — trying the next endpoint`);
86+
}
87+
}
88+
}
89+
throw lastError;
90+
}
91+
92+
// Snapshot the committed addresses before anything overwrites them; the
93+
// start-block cache is only valid while an address is unchanged.
94+
const before = { ...(config.contracts ?? {}) };
95+
6696
let nextId = 1;
6797

6898
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
@@ -168,9 +198,10 @@ function requireAddress(value, label) {
168198
async function findDeploymentBlock(address) {
169199
const PROBE_SPACING_MS = 150;
170200

171-
const head = Number(await rpc(l1Url, 'eth_blockNumber', []));
201+
const head = Number(await l1Rpc('eth_blockNumber', []));
172202
const hasCode = async (block) => {
173-
const code = await rpc(l1Url, 'eth_getCode', [address, `0x${block.toString(16)}`]);
203+
// Historical state: only the primary endpoint can be trusted to retain it.
204+
const code = await l1Rpc('eth_getCode', [address, `0x${block.toString(16)}`], { historical: true });
174205
await sleep(PROBE_SPACING_MS);
175206
return typeof code === 'string' && code !== '0x' && code !== '0x0';
176207
};
@@ -222,17 +253,29 @@ discovered.gseAddress = requireAddress(addresses.gseAddress, 'gseAddress');
222253
// startBlock from the rollup's own deployment — both L1 reads.
223254
let slasher;
224255
let startBlock;
256+
let rollupStartWasCached = false;
225257
try {
226258
slasher = addressFromWord(
227-
await rpc(l1Url, 'eth_call', [{ to: discovered.rollupAddress, data: SELECTOR_GET_SLASHER }, 'latest']),
259+
await l1Rpc('eth_call', [{ to: discovered.rollupAddress, data: SELECTOR_GET_SLASHER }, 'latest']),
228260
);
229261
discovered.slashingProposerAddress = requireAddress(
230-
addressFromWord(await rpc(l1Url, 'eth_call', [{ to: slasher, data: SELECTOR_PROPOSER }, 'latest'])),
262+
addressFromWord(await l1Rpc('eth_call', [{ to: slasher, data: SELECTOR_PROPOSER }, 'latest'])),
231263
'slashingProposerAddress',
232264
);
233-
startBlock = await findDeploymentBlock(discovered.rollupAddress);
265+
// Same cache rule as the other contracts: only search when the rollup address
266+
// differs from the committed one, which is exactly an upgrade.
267+
const cachedRollupStart = config.ponder?.startBlock;
268+
if (
269+
Number.isInteger(cachedRollupStart) && cachedRollupStart > 0 &&
270+
String(before.rollupAddress ?? '').toLowerCase() === discovered.rollupAddress
271+
) {
272+
startBlock = cachedRollupStart;
273+
rollupStartWasCached = true;
274+
} else {
275+
startBlock = await findDeploymentBlock(discovered.rollupAddress);
276+
}
234277
} catch (error) {
235-
console.error(`❌ L1 lookup against ${redact(l1Url)} failed: ${error.message}`);
278+
console.error(`❌ L1 lookup failed across all ${l1Urls.length} configured endpoint(s): ${error.message}`);
236279
process.exit(1);
237280
}
238281

@@ -255,29 +298,60 @@ const CONTRACT_START_BLOCK_KEYS = {
255298
stakingRegistry: 'stakingRegistryAddress',
256299
};
257300

301+
// Searching for a deployment block costs ~25 sequential eth_getCode calls. Doing
302+
// that for every contract on every deploy is ~175 calls, which is enough to
303+
// exhaust a public endpoint's rate limit outright — retries do not help once the
304+
// budget is gone, and the deploy fails.
305+
//
306+
// A contract's deployment block cannot change, so the committed config.json
307+
// carries the answer and it is only recomputed when that contract's address
308+
// actually changes — i.e. on an upgrade, for the one or two contracts that were
309+
// redeployed. A steady-state deploy spends no calls here at all.
310+
const cachedStartBlocks = config.ponder?.startBlocks ?? {};
258311
const startBlocks = { rollup: startBlock };
312+
const searched = new Set();
313+
259314
try {
260315
for (const [key, addressKey] of Object.entries(CONTRACT_START_BLOCK_KEYS)) {
261316
if (key === 'rollup') continue;
317+
262318
const address = (discovered[addressKey] ?? config.contracts[addressKey] ?? '').toLowerCase();
263319
if (!/^0x[0-9a-f]{40}$/.test(address) || address === ZERO) {
264320
console.log(` ! ${key.padEnd(28)} no address configured — skipping`);
265321
continue;
266322
}
267-
startBlocks[key] = address === discovered.rollupAddress
268-
? startBlock
269-
: await findDeploymentBlock(address);
323+
324+
if (address === discovered.rollupAddress) {
325+
startBlocks[key] = startBlock;
326+
continue;
327+
}
328+
329+
// Reuse only when the cached block belongs to the address we just discovered;
330+
// otherwise the contract was redeployed and the old block is meaningless.
331+
const previousAddress = String(before[addressKey] ?? '').toLowerCase();
332+
const cached = cachedStartBlocks[key];
333+
if (Number.isInteger(cached) && cached > 0 && previousAddress === address) {
334+
startBlocks[key] = cached;
335+
continue;
336+
}
337+
338+
startBlocks[key] = await findDeploymentBlock(address);
339+
searched.add(key);
270340
}
271341
} catch (error) {
272342
console.error(`❌ Deployment-block search failed: ${error.message}`);
343+
if (searched.size === 0) {
344+
console.error(' No cached start blocks were usable, so every contract needed a search.');
345+
console.error(' Commit the discovered ponder.startBlocks to .environment/<network>/config.json');
346+
console.error(' so routine deploys stop re-deriving them.');
347+
}
273348
process.exit(1);
274349
}
275350

276351
// Aztec V5 deleted SlashFactory.sol, so drop the key from configs written by an
277352
// earlier dashtec rather than leaving a dead address behind.
278353
delete config.contracts.slashFactoryAddress;
279354

280-
const before = { ...config.contracts };
281355
config.contracts = { ...config.contracts, ...discovered };
282356
// `startBlock` stays the rollup's, as the default for anything without its own.
283357
config.ponder = { ...config.ponder, startBlock, startBlocks };
@@ -289,8 +363,16 @@ for (const [key, value] of Object.entries(discovered)) {
289363
}
290364
console.log('');
291365
for (const [key, value] of Object.entries(startBlocks)) {
292-
const marker = value === startBlock ? ' ' : '~';
293-
console.log(` ${marker} ${`startBlock.${key}`.padEnd(28)} ${value}`);
366+
const how = searched.has(key)
367+
? 'searched'
368+
: key === 'rollup'
369+
? (rollupStartWasCached ? 'cached' : 'searched')
370+
: 'cached';
371+
console.log(` ${`startBlock.${key}`.padEnd(28)} ${String(value).padEnd(10)} (${how})`);
372+
}
373+
if (searched.size > 0 || !rollupStartWasCached) {
374+
const n = searched.size + (rollupStartWasCached ? 0 : 1);
375+
console.log(`\n (${n} contract(s) re-derived — expected only when an address changes)`);
294376
}
295377
console.log(`\n (slasher ${slasher}, not stored — only used to reach the proposer)`);
296378
console.log(` (stakingRegistryAddress left as ${config.contracts.stakingRegistryAddress})`);

0 commit comments

Comments
 (0)