From b390aaabb3595154beeba7aeaf26829584d1c2fd Mon Sep 17 00:00:00 2001 From: filip Date: Mon, 13 Jan 2025 09:59:55 +0100 Subject: [PATCH] hotfix: avoid stale pendingTxs --- k8s/local/skaffold.no_ui.yaml | 2 ++ .../src/database/controllers/l2Tx/delete.ts | 10 ++++++++++ .../explorer-api/src/event-handler/on-pending-txs.ts | 10 ++++++++++ services/explorer-api/src/start.ts | 3 ++- 4 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 services/explorer-api/src/database/controllers/l2Tx/delete.ts diff --git a/k8s/local/skaffold.no_ui.yaml b/k8s/local/skaffold.no_ui.yaml index 1b1a9c32..53512428 100644 --- a/k8s/local/skaffold.no_ui.yaml +++ b/k8s/local/skaffold.no_ui.yaml @@ -9,6 +9,7 @@ requires: - path: ./auth/image.yaml - path: ./event-cannon/image.yaml - path: ./aztec-listener/image.yaml + - path: ./ethereum-listener/image.yaml deploy: kubectl: flags: @@ -26,6 +27,7 @@ manifests: - k8s/local/auth/service.yaml - k8s/local/event-cannon/deployment.yaml - k8s/local/aztec-listener/deployment.yaml + - k8s/local/ethereum-listener/deployment.yaml - k8s/local/aztec-sandbox-node/ingress.yaml - k8s/local/aztec-sandbox-node/deployment.yaml - k8s/local/aztec-sandbox-node/service.yaml diff --git a/services/explorer-api/src/database/controllers/l2Tx/delete.ts b/services/explorer-api/src/database/controllers/l2Tx/delete.ts new file mode 100644 index 00000000..7bbe6c60 --- /dev/null +++ b/services/explorer-api/src/database/controllers/l2Tx/delete.ts @@ -0,0 +1,10 @@ +import { ChicmozL2PendingTx } from "@chicmoz-pkg/types"; +import { eq } from "drizzle-orm"; +import { getDb as db } from "../../../database/index.js"; +import { l2Tx } from "../../schema/index.js"; + +export const deleteTx = async ( + hash: ChicmozL2PendingTx["hash"] +): Promise => { + await db().delete(l2Tx).where(eq(l2Tx.hash, hash)).execute(); +}; diff --git a/services/explorer-api/src/event-handler/on-pending-txs.ts b/services/explorer-api/src/event-handler/on-pending-txs.ts index d69ff4bb..3fe5b8c8 100644 --- a/services/explorer-api/src/event-handler/on-pending-txs.ts +++ b/services/explorer-api/src/event-handler/on-pending-txs.ts @@ -3,8 +3,14 @@ import { chicmozL2PendingTxSchema } from "@chicmoz-pkg/types"; import { logger } from "../logger.js"; import { storeL2Tx } from "../database/controllers/l2Tx/store.js"; import { handleDuplicateError } from "./utils.js"; +import { getTxs } from "../database/controllers/l2Tx/get-tx.js"; +import { deleteTx } from "../database/controllers/l2Tx/delete.js"; export const onPendingTxs = async ({ txs }: PendingTxsEvent) => { + const dbTxs = await getTxs(); + const staleTxs = dbTxs.filter( + (dbTx) => !txs.some((tx) => tx.hash === dbTx.hash) + ); for (const tx of txs) { logger.info(`🕐 Pending tx: ${tx.hash}`); const res = chicmozL2PendingTxSchema.parse(tx); @@ -12,4 +18,8 @@ export const onPendingTxs = async ({ txs }: PendingTxsEvent) => { handleDuplicateError(e as Error, `tx ${res.hash}`); }); } + if (staleTxs.length > 0) { + logger.info(`🕐🕐🕐 Stale txs: ${staleTxs.length}. Deleting...`); + for (const tx of staleTxs) await deleteTx(tx.hash); + } }; diff --git a/services/explorer-api/src/start.ts b/services/explorer-api/src/start.ts index a66e511b..00ed6306 100644 --- a/services/explorer-api/src/start.ts +++ b/services/explorer-api/src/start.ts @@ -1,4 +1,5 @@ import * as cache from "./cache/index.js"; +import {deleteAllTxs} from "./database/controllers/l2Tx/delete-all-txs.js"; import * as db from "./database/index.js"; import { subscribeHandlers } from "./event-handler/index.js"; import { setComponentInitializing, setComponentUp } from "./health.js"; @@ -39,7 +40,7 @@ export const start = async () => { ID: mb.ID, init: mb.init, }); - // TODO: clear pending txs DB + await deleteAllTxs(); // TODO: perhaps a more specific deleteAllTxs should be created, also some logs could be good. setComponentInitializing("SUBSCRIPTIONS"); await subscribeHandlers(); setComponentUp("SUBSCRIPTIONS");