@aztec/ethereum 0.0.1-commit.9badcec54 → 0.0.1-commit.9ebd450e8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -15,6 +15,7 @@ import {
15
15
  type Account,
16
16
  type GetContractReturnType,
17
17
  type Hex,
18
+ type Log,
18
19
  type StateOverride,
19
20
  type WatchContractEventReturnType,
20
21
  encodeAbiParameters,
@@ -1224,7 +1225,7 @@ export class RollupContract {
1224
1225
  }
1225
1226
 
1226
1227
  public listenToCheckpointInvalidated(
1227
- callback: (args: { checkpointNumber: CheckpointNumber }) => unknown,
1228
+ callback: (args: { checkpointNumber: CheckpointNumber; event: Log }) => unknown,
1228
1229
  ): WatchContractEventReturnType {
1229
1230
  return this.rollup.watchEvent.CheckpointInvalidated(
1230
1231
  {},
@@ -1233,7 +1234,7 @@ export class RollupContract {
1233
1234
  for (const log of logs) {
1234
1235
  const args = log.args;
1235
1236
  if (args.checkpointNumber !== undefined) {
1236
- callback({ checkpointNumber: CheckpointNumber.fromBigInt(args.checkpointNumber) });
1237
+ callback({ checkpointNumber: CheckpointNumber.fromBigInt(args.checkpointNumber), event: log });
1237
1238
  }
1238
1239
  }
1239
1240
  },
@@ -1266,6 +1267,17 @@ export class RollupContract {
1266
1267
  );
1267
1268
  }
1268
1269
 
1270
+ /**
1271
+ * Fetches OwnershipTransferred events emitted on the L1 block this rollup was deployed on.
1272
+ * The Rollup inherits from Ownable and emits this event in its constructor, so the event
1273
+ * is guaranteed to exist on `l1StartBlock` for any correctly deployed rollup. Used as a
1274
+ * probe to detect RPC nodes that prune historical logs.
1275
+ */
1276
+ async getOwnershipTransferredEventsAtDeploy() {
1277
+ const l1StartBlock = await this.getL1StartBlock();
1278
+ return await this.rollup.getEvents.OwnershipTransferred({}, { fromBlock: l1StartBlock, toBlock: l1StartBlock });
1279
+ }
1280
+
1269
1281
  /** Fetches CheckpointProposed events within the given block range. */
1270
1282
  async getCheckpointProposedEvents(fromBlock: bigint, toBlock: bigint): Promise<CheckpointProposedLog[]> {
1271
1283
  const logs = await this.rollup.getEvents.CheckpointProposed({}, { fromBlock, toBlock });
package/src/queries.ts CHANGED
@@ -1,11 +1,77 @@
1
1
  import { EthAddress } from '@aztec/foundation/eth-address';
2
2
 
3
+ import { BaseError, type Block } from 'viem';
4
+
3
5
  import { DefaultL1ContractsConfig, type L1ContractsConfig } from './config.js';
4
6
  import { ReadOnlyGovernanceContract } from './contracts/governance.js';
5
7
  import { GovernanceProposerContract } from './contracts/governance_proposer.js';
6
8
  import { InboxContract } from './contracts/inbox.js';
7
9
  import { RollupContract } from './contracts/rollup.js';
8
- import type { ViemPublicClient } from './types.js';
10
+ import type { ViemClient, ViemPublicClient } from './types.js';
11
+
12
+ /**
13
+ * Returns the L1 finalized block, or `undefined` if the chain does not yet have one
14
+ * (common on freshly started devnets). Rethrows any other RPC error.
15
+ */
16
+ export async function getFinalizedL1Block(client: ViemClient): Promise<Block<bigint, false, 'finalized'> | undefined> {
17
+ try {
18
+ return await client.getBlock({ blockTag: 'finalized', includeTransactions: false });
19
+ } catch (err) {
20
+ if (isFinalizedBlockTagNotFoundError(err)) {
21
+ return undefined;
22
+ }
23
+ throw err;
24
+ }
25
+ }
26
+
27
+ // Error messages returned by popular Ethereum execution clients when
28
+ // eth_getBlockByNumber / eth_call is called with blockTag "finalized" or
29
+ // "safe" and no such block has been produced yet:
30
+ //
31
+ // geth "finalized block not found"
32
+ // "safe block not found"
33
+ // reth "block not found: finalized"
34
+ // "block not found: safe"
35
+ // nethermind "Unknown block error" (same for both tags)
36
+ // besu "Unknown block"
37
+ // erigon 'block "finalized" not available (head block: N)'
38
+ // 'block "safe" not available (head block: N)'
39
+ //
40
+ // A combined regex covers all five:
41
+ const FINALIZED_BLOCK_TAG_NOT_FOUND_MESSAGE_RE =
42
+ /(finalized|safe) block not found|block not found: (finalized|safe)|unknown block|block "(finalized|safe)" not available/i;
43
+
44
+ /**
45
+ * Returns true if the error originates from an RPC call that failed because
46
+ * the "finalized" (or "safe") block tag is not yet available on the chain.
47
+ */
48
+ export function isFinalizedBlockTagNotFoundError(err: unknown): boolean {
49
+ if (!err) {
50
+ return false;
51
+ }
52
+
53
+ if (err instanceof BaseError) {
54
+ const hit = err.walk((e: any) => matchesFinalizedBlockTagNotFound(e));
55
+ if (hit) {
56
+ return true;
57
+ }
58
+ }
59
+
60
+ for (let cur: any = err, i = 0; cur && i < 10; cur = cur.cause, i++) {
61
+ if (matchesFinalizedBlockTagNotFound(cur)) {
62
+ return true;
63
+ }
64
+ }
65
+ return false;
66
+ }
67
+
68
+ function matchesFinalizedBlockTagNotFound(e: any): boolean {
69
+ if (!e) {
70
+ return false;
71
+ }
72
+ const text = `${e.details ?? ''} ${e.message ?? ''} ${e.shortMessage ?? ''}`;
73
+ return FINALIZED_BLOCK_TAG_NOT_FOUND_MESSAGE_RE.test(text);
74
+ }
9
75
 
10
76
  /** Reads the L1ContractsConfig from L1 contracts. */
11
77
  export async function getL1ContractsConfig(
@@ -221,6 +221,11 @@ export class ChainMonitor extends EventEmitter<ChainMonitorEventMap> {
221
221
  });
222
222
  }
223
223
 
224
+ public async waitUntilNextL2Slot(): Promise<void> {
225
+ const targetSlot = SlotNumber.add((await this.run()).l2SlotNumber, 1);
226
+ return this.waitUntilL2Slot(targetSlot);
227
+ }
228
+
224
229
  public waitUntilL1Block(block: number | bigint): Promise<void> {
225
230
  const targetBlock = typeof block === 'bigint' ? block.valueOf() : block;
226
231
  if (this.l1BlockNumber >= targetBlock) {