Skip to content

Commit bf3d804

Browse files
committed
hotfix: tx-effects working in UI and l2_proven is stored in DB
1 parent 75e38d6 commit bf3d804

File tree

6 files changed

+79
-31
lines changed

6 files changed

+79
-31
lines changed

services/aztec-listener/src/events/emitted/index.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ import { onL2RpcNodeAlive } from "./on-node-alive.js";
1717

1818
export const onBlock = async (
1919
block: L2Block,
20-
finalizationStatus: ChicmozL2BlockFinalizationStatus
20+
finalizationStatus: ChicmozL2BlockFinalizationStatus,
2121
) => {
2222
const height = Number(block.header.globalVariables.blockNumber);
23+
const finalizationStatusStr =
24+
ChicmozL2BlockFinalizationStatus[finalizationStatus];
2325
logger.info(
24-
`🦊 publishing block ${height} (hash: ${(
26+
`🦊 publishing (${finalizationStatusStr}) block ${height} (hash: ${(
2527
await block.hash()
26-
).toString()})...`
28+
).toString()})...`,
2729
);
2830
const blockStr = block.toString();
2931
await publishMessage("NEW_BLOCK_EVENT", {
@@ -35,7 +37,7 @@ export const onBlock = async (
3537

3638
export const onCatchupBlock = async (
3739
block: L2Block,
38-
finalizationStatus: ChicmozL2BlockFinalizationStatus
40+
finalizationStatus: ChicmozL2BlockFinalizationStatus,
3941
) => {
4042
const blockStr = block.toString();
4143
await publishMessage("CATCHUP_BLOCK_EVENT", {
@@ -47,7 +49,7 @@ export const onCatchupBlock = async (
4749
// TODO: onCatchupRequestFromExplorerApi
4850

4951
export const onPendingTxs = async (txs: TxHash[]) => {
50-
if (!txs || txs.length === 0) return;
52+
if (!txs || txs.length === 0) {return;}
5153

5254
await publishMessage("PENDING_TXS_EVENT", {
5355
txs: txs.map((tx) => {
@@ -75,7 +77,7 @@ export const onL2RpcNodeError = (
7577
rpcNodeError: Omit<
7678
ChicmozL2RpcNodeError,
7779
"rpcUrl" | "count" | "createdAt" | "lastSeenAt"
78-
>
80+
>,
7981
) => {
8082
let event;
8183
try {

services/explorer-api/src/events/received/on-block/index.ts

+16-7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { SERVICE_NAME } from "../../../constants.js";
1111
import { L2_NETWORK_ID } from "../../../environment.js";
1212
import { logger } from "../../../logger.js";
1313
import { deleteL2BlockByHeight } from "../../../svcs/database/controllers/l2block/delete.js";
14+
import { ensureFinalizationStatusStored } from "../../../svcs/database/controllers/l2block/store.js";
1415
import { controllers } from "../../../svcs/database/index.js";
1516
import { emit } from "../../index.js";
1617
import { handleDuplicateBlockError } from "../utils.js";
@@ -27,7 +28,7 @@ const hackyLogBlock = (b: L2Block) => {
2728
const logString = blockString
2829
.split(":")
2930
.map((v) => {
30-
if (v.length > 200 && v.includes(",")) return truncateString(v);
31+
if (v.length > 200 && v.includes(",")) {return truncateString(v);}
3132

3233
return v;
3334
})
@@ -59,7 +60,7 @@ const onBlock = async ({
5960
} catch (e) {
6061
logger.error(
6162
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
62-
`Failed to parse block ${blockNumber}: ${(e as Error)?.stack ?? e}`
63+
`Failed to parse block ${blockNumber}: ${(e as Error)?.stack ?? e}`,
6364
);
6465
hackyLogBlock(b);
6566
return;
@@ -71,27 +72,35 @@ const onBlock = async ({
7172

7273
const storeBlock = async (parsedBlock: ChicmozL2Block, haveRetried = false) => {
7374
logger.info(
74-
`🧢 Storing block ${parsedBlock.height} (hash: ${parsedBlock.hash})`
75+
`🧢 Storing block ${parsedBlock.height} (hash: ${parsedBlock.hash})`,
7576
);
7677
const storeRes = await controllers.l2Block
7778
.store(parsedBlock)
7879
.catch(async (e) => {
7980
if (haveRetried) {
8081
throw new Error(
81-
`Failed to store block ${parsedBlock.height} after retry: ${e}`
82+
`Failed to store block ${parsedBlock.height} after retry: ${e}`,
8283
);
8384
}
8485
const shouldRetry = await handleDuplicateBlockError(
8586
e as Error,
8687
`block ${parsedBlock.height}`,
8788
async () => {
8889
logger.warn(
89-
`Deleting block ${parsedBlock.height} (hash: ${parsedBlock.hash})`
90+
`Deleting block ${parsedBlock.height} (hash: ${parsedBlock.hash})`,
9091
);
9192
await deleteL2BlockByHeight(parsedBlock.height);
92-
}
93+
},
9394
);
94-
if (shouldRetry) return storeBlock(parsedBlock, true);
95+
if (shouldRetry) {
96+
return storeBlock(parsedBlock, true);
97+
} else {
98+
await ensureFinalizationStatusStored( // NOTE: this is currently assuming that the error is a duplicate error
99+
parsedBlock.hash,
100+
parsedBlock.height,
101+
parsedBlock.finalizationStatus,
102+
);
103+
}
95104
});
96105
await emit.l2BlockFinalizationUpdate(storeRes?.finalizationUpdate ?? null);
97106
};

services/explorer-api/src/svcs/database/controllers/l2block/add_l1_data.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export const addL1L2BlockProposed = async (
6666
};
6767
};
6868

69-
export const ensureFinalizationStatusStored = async (
69+
export const ensureL1FinalizationIsStored = async (
7070
l2BlockHash: ChicmozL2Block["hash"],
7171
l2BlockNumber: ChicmozL2Block["height"],
7272
archiveRoot: string,

services/explorer-api/src/svcs/database/controllers/l2block/store.ts

+32-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { ChicmozL2BlockFinalizationUpdateEvent } from "@chicmoz-pkg/message-registry";
22
import { getDb as db } from "@chicmoz-pkg/postgres-helper";
3-
import { HexString, type ChicmozL2Block } from "@chicmoz-pkg/types";
3+
import {
4+
ChicmozL2BlockFinalizationStatus,
5+
HexString,
6+
type ChicmozL2Block,
7+
} from "@chicmoz-pkg/types";
48
import { v4 as uuidv4 } from "uuid";
59
import {
610
archive,
@@ -21,10 +25,10 @@ import {
2125
txEffect,
2226
} from "../../../database/schema/l2block/index.js";
2327
import { l2BlockFinalizationStatusTable } from "../../schema/l2block/finalization-status.js";
24-
import { ensureFinalizationStatusStored } from "./add_l1_data.js";
28+
import { ensureL1FinalizationIsStored } from "./add_l1_data.js";
2529

2630
export const store = async (
27-
block: ChicmozL2Block
31+
block: ChicmozL2Block,
2832
): Promise<{
2933
finalizationUpdate: ChicmozL2BlockFinalizationUpdateEvent | null;
3034
}> => {
@@ -155,7 +159,7 @@ export const store = async (
155159

156160
// Insert txEffects and create junction entries
157161
for (const [i, txEff] of Object.entries(block.body.txEffects)) {
158-
if (isNaN(Number(i))) throw new Error("Invalid txEffect index");
162+
if (isNaN(Number(i))) {throw new Error("Invalid txEffect index");}
159163
await dbTx.insert(txEffect).values({
160164
txHash: txEff.txHash,
161165
bodyId,
@@ -182,22 +186,34 @@ export const store = async (
182186
value: pdw.value,
183187
});
184188
}
185-
if (block.finalizationStatus.valueOf() >= 0) {
186-
await db() // NOTE: purposly not using dbTx, it should always be stored
187-
.insert(l2BlockFinalizationStatusTable)
188-
.values({
189-
l2BlockHash: block.hash,
190-
l2BlockNumber: block.height,
191-
status: block.finalizationStatus,
192-
})
193-
.onConflictDoNothing();
194-
}
189+
await ensureFinalizationStatusStored(
190+
block.hash,
191+
block.height,
192+
block.finalizationStatus,
193+
);
195194
}
196-
const finalizationUpdate = await ensureFinalizationStatusStored(
195+
const finalizationUpdate = await ensureL1FinalizationIsStored(
197196
block.hash,
198197
block.height,
199-
block.archive.root
198+
block.archive.root,
200199
);
201200
return { finalizationUpdate };
202201
});
203202
};
203+
204+
export const ensureFinalizationStatusStored = async (
205+
l2BlockHash: HexString,
206+
l2BlockNumber: bigint,
207+
status: ChicmozL2BlockFinalizationStatus,
208+
): Promise<void> => {
209+
if (status.valueOf() >= 0) {
210+
await db()
211+
.insert(l2BlockFinalizationStatusTable)
212+
.values({
213+
l2BlockHash,
214+
l2BlockNumber,
215+
status,
216+
})
217+
.onConflictDoNothing();
218+
}
219+
};

services/explorer-ui/src/hooks/api/tx-effect.ts

+21-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { type ChicmozL2TxEffectDeluxe } from "@chicmoz-pkg/types";
22
import {
33
useQueries,
44
useQuery,
5+
useQueryClient,
56
type UseQueryResult,
67
} from "@tanstack/react-query";
78
import { TxEffectsAPI } from "~/api";
@@ -50,8 +51,27 @@ export const useGetLatestTxEffects = (): UseQueryResult<
5051
ChicmozL2TxEffectDeluxe[] | undefined,
5152
Error
5253
> => {
54+
const queryClient = useQueryClient();
5355
return useQuery<ChicmozL2TxEffectDeluxe[] | undefined, Error>({
5456
queryKey: queryKeyGenerator.latestTxEffects,
55-
queryFn: () => TxEffectsAPI.getLatestTxEffects(),
57+
queryFn: async () => {
58+
const latestTxEffects = await TxEffectsAPI.getLatestTxEffects();
59+
if (!latestTxEffects) return [];
60+
if (latestTxEffects.length === 0) return [];
61+
let blockTxEffects: ChicmozL2TxEffectDeluxe[] = [];
62+
let currentBlockHeight = latestTxEffects[0].blockHeight;
63+
latestTxEffects.forEach((txEffect) => {
64+
if (txEffect.blockHeight !== currentBlockHeight) {
65+
queryClient.setQueryData(
66+
queryKeyGenerator.txEffectsByBlockHeight(currentBlockHeight),
67+
blockTxEffects,
68+
);
69+
blockTxEffects = [];
70+
currentBlockHeight = txEffect.blockHeight;
71+
}
72+
blockTxEffects.push(txEffect);
73+
});
74+
return latestTxEffects;
75+
},
5676
});
5777
};

services/explorer-ui/src/hooks/websocket.ts

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const updateTxEffects = (
3939
blockHeight: block.height,
4040
timestamp: block.header.globalVariables.timestamp,
4141
});
42+
return effect;
4243
});
4344
queryClient.setQueryData(
4445
queryKeyGenerator.txEffectsByBlockHeight(block.height),

0 commit comments

Comments
 (0)