Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
46e2cbc
feat(explorer-api): add block link indexes and reorg record tables
alejoamiras Nov 26, 2026
77d6c70
test(explorer-api): give built blocks their own archives and a parent…
alejoamiras Nov 26, 2026
4d0647c
feat(explorer-api): decide the canonical chain from ordered blocks
alejoamiras Nov 26, 2026
fb5e5a3
test(explorer-api): cover a replaced block that stays stored
alejoamiras Nov 26, 2026
d1c655e
test(explorer-api): cover the chain rules, their bounds and repair re…
alejoamiras Nov 26, 2026
380de54
style(explorer-api): format the chain files with prettier
alejoamiras Nov 26, 2026
139630f
fix(explorer-api): heartbeat through a chain change's settlement
alejoamiras Nov 26, 2026
ce3e488
fix(explorer-api): demote every stale holder of an ordered block's txs
alejoamiras Nov 26, 2026
8d616f7
fix(explorer-api): keep catch-up blocks above the canonical head off …
alejoamiras Nov 26, 2026
0f876a7
fix(explorer-api): apply the ordered rule below a catch-up block join…
alejoamiras Nov 26, 2026
5000fd0
fix(explorer-api): flag a new off-chain block whose stored parent is …
alejoamiras Nov 26, 2026
a2ee6df
fix(explorer-api): close a repair in the change that makes its block …
alejoamiras Nov 26, 2026
ca0d378
test(explorer-api): observe the chain lock wait in the serialization …
alejoamiras Nov 26, 2026
0656b3f
docs(explorer-api): state what the chain lock covers
alejoamiras Nov 26, 2026
fefe4b1
test(explorer-api): pin how the next ordered block heals a stale catc…
alejoamiras Nov 26, 2026
0cad20d
fix(explorer-api): clear the height a walk down stops at before reque…
alejoamiras Nov 26, 2026
74adf5e
fix(explorer-api): keep an older rollup version's block from closing …
alejoamiras Nov 26, 2026
8982fd3
fix(explorer-api): let a walked block's catch-up take its txs from st…
alejoamiras Nov 26, 2026
834f640
test(explorer-api): name the repair chaining test for what it delivers
alejoamiras Nov 26, 2026
85ceb79
fix(explorer-api): heartbeat inside a chain change's per-row write loops
alejoamiras Nov 26, 2026
4e1f587
docs(explorer-api): say which blocks a catch-up block can demote
alejoamiras Nov 26, 2026
42dc469
revert(explorer-api): close a repair by its height again, whatever th…
alejoamiras Nov 27, 2026
8c688da
fix(explorer-api): ask for the empty height below a final catch-up block
alejoamiras Nov 27, 2026
ee93c45
fix(explorer-api): heartbeat before each tx effect a block change writes
alejoamiras Nov 27, 2026
b86fb80
docs(explorer-api): state the catch-up block rule in four sentences
alejoamiras Nov 27, 2026
9b76282
refactor(explorer-api): read the ordered block's proposedTip and the …
alejoamiras Nov 27, 2026
654cab2
refactor(explorer-api): record a reorg's cause instead of its derivab…
alejoamiras Nov 27, 2026
03592bd
feat(explorer-api): hold one canonical block per height with a unique…
alejoamiras Nov 27, 2026
6096e8d
docs(types): say what a reorg record's count and lowest block cover
alejoamiras Nov 27, 2026
751e31a
fix(explorer-api): create the one-canonical-block index without IF NO…
alejoamiras Nov 27, 2026
092a783
refactor(explorer-api): adapt the reorg response schema to zod 4
alejoamiras Nov 27, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions packages/types/src/aztec/reorg.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import { z } from "zod";
import { hexStringSchema } from "../general.js";

export const chicmozReorgSchema = z.object({
orphanedBlockHash: hexStringSchema,
height: z.coerce.bigint().nonnegative(),
timestamp: z.coerce.date(),
nbrOfOrphanedBlocks: z.number().int().nonnegative(),
});
export const chicmozReorgSchema = z
.object({
orphanedBlockHash: hexStringSchema.describe(
"The lowest of the record's blocks that are still orphaned.",
),
height: z.coerce.bigint().nonnegative(),
timestamp: z.coerce.date(),
nbrOfOrphanedBlocks: z
.number()
.int()
.nonnegative()
.describe("How many of the record's blocks are still orphaned."),
})
.describe(
"One chain change that orphaned canonical blocks. Its count and lowest block cover only the blocks still orphaned, and the record disappears once all its blocks are back on the chain.",
);

export type ChicmozReorg = z.infer<typeof chicmozReorgSchema>;
4 changes: 4 additions & 0 deletions services/explorer-api/migrations/0038_block_link_indexes.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE INDEX IF NOT EXISTS "archive_root_idx" ON "archive" USING btree ("root");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "last_archive_root_idx" ON "last_archive" USING btree ("root");--> statement-breakpoint
ALTER TABLE "l2_tip_boundary_mismatch" ADD COLUMN IF NOT EXISTS "rollup_version" bigint;--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "l2_tip_boundary_mismatch_unresolved_idx" ON "l2_tip_boundary_mismatch" USING btree ("l2_network_id","height") WHERE "resolved_at" IS NULL;
17 changes: 17 additions & 0 deletions services/explorer-api/migrations/0039_l2_reorg.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
CREATE TABLE IF NOT EXISTS "l2_reorg" (
"id" bigint PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
"l2_network_id" varchar NOT NULL,
"rollup_version" bigint NOT NULL,
"cause_hash" varchar NOT NULL,
"cause_kind" varchar NOT NULL,
"observed_at" timestamp NOT NULL,
"revived_at" timestamp
);--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "l2_reorg_network_observed_idx" ON "l2_reorg" USING btree ("l2_network_id","observed_at" DESC);--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "l2_reorg_block" (
"reorg_id" bigint NOT NULL REFERENCES "l2_reorg"("id") ON DELETE CASCADE,
"height" bigint NOT NULL,
"hash" varchar NOT NULL,
CONSTRAINT "l2_reorg_block_pkey" PRIMARY KEY ("reorg_id","hash")
);--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "l2_reorg_block_hash_idx" ON "l2_reorg_block" USING btree ("hash");
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE UNIQUE INDEX "l2block_one_canonical_per_height_idx" ON "l2Block" USING btree ("version","height") WHERE "l2Block"."orphan_timestamp" IS NULL;
21 changes: 21 additions & 0 deletions services/explorer-api/migrations/meta/_journal.json
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,27 @@
"when": 1779960000010,
"tag": "0037_reset_open_gap_request_history",
"breakpoints": true
},
{
"idx": 38,
"version": "7",
"when": 1779960000011,
"tag": "0038_block_link_indexes",
"breakpoints": true
},
{
"idx": 39,
"version": "7",
"when": 1779960000012,
"tag": "0039_l2_reorg",
"breakpoints": true
},
{
"idx": 40,
"version": "7",
"when": 1779960000013,
"tag": "0040_one_canonical_block_per_height",
"breakpoints": true
}
]
}
75 changes: 10 additions & 65 deletions services/explorer-api/src/events/received/on-block/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,10 @@ import {
chicmozL2ContractInstanceUpdatedEventSchema,
type ChicmozL2ContractClassRegisteredEvent,
type ChicmozL2ContractInstanceDeployedEvent,
type ChicmozL2PrivateFunctionBroadcastedEvent,
type ChicmozL2UtilityFunctionBroadcastedEvent,
} from "@chicmoz-pkg/types";
import { z } from "zod";
import { logger } from "../../../logger.js";
import { controllers } from "../../../svcs/database/index.js";
import { handleDuplicateError } from "../utils.js";
import { type BlockContracts } from "../../../svcs/database/controllers/l2contract/store.js";

const AZTEC_CONTRACT_CLASS_VERSION = 1;

Expand Down Expand Up @@ -45,22 +42,11 @@ const parseObjs = <ParsedType, AztecType>(
return parsedObjs;
};

const storeObj = async <T>(
objs: T[],
storeFn: (obj: T) => Promise<void>,
objType: string,
objId: keyof T,
) => {
for (const obj of objs) {
await storeFn(obj).catch((e) => {
const duplicateErrorId = obj[objId] as string;
handleDuplicateError(e as Error, `${objType} ${duplicateErrorId}`);
});
}
};

// NOTE: reference for parsing in aztec-packages: yarn-project/archiver/src/archiver/archiver.ts
export const storeContracts = async (b: L2Block, blockHash: string) => {
export const parseBlockContracts = async (
b: L2Block,
blockHash: string,
): Promise<BlockContracts> => {
const privateLogs = b.body.txEffects.flatMap(
(txEffect) => txEffect.privateLogs,
);
Expand Down Expand Up @@ -99,9 +85,6 @@ export const storeContracts = async (b: L2Block, blockHash: string) => {
contractClassRegisteredEvents.map((e) => e.toContractClassPublic()),
);

const privateFnEvents: unknown[] = [];
const utilityFnEvents: unknown[] = [];

if (contractClasses.length > 0) {
logger.info(
`📜 Parsing and storing ${contractClasses.length} contract classes`,
Expand All @@ -117,16 +100,6 @@ export const storeContracts = async (b: L2Block, blockHash: string) => {
`⬆️ Parsing and storing ${contractInstanceUpdated.length} contract instances updated`,
);
}
if (privateFnEvents.length > 0) {
logger.info(
`🔒 Parsing and storing ${privateFnEvents.length} private function events`,
);
}
if (utilityFnEvents.length > 0) {
logger.info(
`💪 Parsing and storing ${utilityFnEvents.length} utility function events`,
);
}

const contractClassesWithId = contractClasses.map((contractClass) => {
return {
Expand Down Expand Up @@ -189,37 +162,9 @@ export const storeContracts = async (b: L2Block, blockHash: string) => {
newContractClassId: contractInstance.newContractClassId.toString(),
} as ChicmozL2ContractInstanceUpdatedEvent),
);
const parsedPrivateFnEvents: ChicmozL2PrivateFunctionBroadcastedEvent[] = [];
const parsedUtilityFnEvents: ChicmozL2UtilityFunctionBroadcastedEvent[] = [];

await storeObj(
parsedContractClasses,
controllers.l2Contract.storeContractClass,
"contractClass",
"contractClassId",
);
await storeObj(
parsedContractInstancesDeployed,
controllers.l2Contract.storeContractInstanceDeployed,
"contractInstanceDeployed",
"address",
);
await storeObj(
parsedContractInstanceUpdate,
controllers.l2Contract.storeContractInstanceUpdated,
"contractInstanceUpdated",
"address",
);
await storeObj(
parsedPrivateFnEvents,
controllers.l2Contract.storePrivateFunction,
"privateFunction",
"artifactMetadataHash",
);
await storeObj(
parsedUtilityFnEvents,
controllers.l2Contract.storeUtilityFunction,
"utilityFunction",
"artifactMetadataHash",
);
return {
classes: parsedContractClasses,
instancesDeployed: parsedContractInstancesDeployed,
instancesUpdated: parsedContractInstanceUpdate,
};
};
Loading
Loading