Skip to content

fix: periodically recheck unconfirmed txs so they end up confirmed or orphaned - #1175

Open
Klakurka wants to merge 3 commits into
masterfrom
ccr-b62297cf-awgd35-recheck-unconfirmed-txs
Open

Klakurka wants to merge 3 commits into
masterfrom
ccr-b62297cf-awgd35-recheck-unconfirmed-txs

Conversation

@Klakurka

@Klakurka Klakurka commented Dec 3, 2026

Copy link
Copy Markdown
Member

Related to #889

Description

Adds a periodic job (every 30 minutes) that rechecks txs stored as unconfirmed, so they end up either confirmed or orphaned even when the chronik websocket missed the TX_CONFIRMED / TX_REMOVED_FROM_MEMPOOL message (disconnects, restarts, etc.).

For each tx it looks the txid up on chronik:

  • in a block: the tx is upserted for every address of ours it touches through the same path as the websocket/block handlers (upsertTransaction), so confirmed and the timestamp (moved to the block's) are updated together with prices, caches and payment summaries, the tx is broadcast, and the client payments it pays are marked CONFIRMED;
  • unknown to chronik (404): the tx is marked orphaned, as master's TX_CONFIRMED handler already does;
  • still in the mempool: left as is, with a warning if it has been there for over a day.

This is #1102 (by Klakurka) rebased onto master, with his two commits kept as they were (conflicts resolved against the newer job infra), plus a follow-up commit that:

  • confirms txs through upsertTransaction instead of a raw updateMany, which left caches and payment summaries pointing at the old (first-seen) timestamp;
  • moves the chronik lookup into ChronikBlockchainClient.recheckUnconfirmedTransaction (shares a persistChronikTransaction helper with syncBlockTransactions);
  • adds isChronikTxNotFoundError: the previous /not found in the index|404/ check matched any error whose message contained a txid with 404 in it (e.g. a 500 for /tx/ab404…) and would have orphaned that tx. The TX_CONFIRMED handler uses it too now;
  • fetches one entry per tx (not every row of every address), oldest first, at most 500 per run and only txs first seen over 30 minutes ago (newer ones are left to the websocket handlers). Lookups are sequential, so a backlog is worked through over several runs without hammering chronik. The one-week window is gone, so older stuck txs get resolved too;
  • replaces the confirmed-only index with (confirmed, orphaned, timestamp), which matches the job's filter and order, in a migration dated after master's latest one.

Test plan

  • make github-test-unit: 19 suites, 420 tests pass. New tests cover:
    • verifyUnconfirmedTransactions: cutoff and cap passed to the query, each tx rechecked on its own network one at a time, outcomes counted, a failed lookup doesn't stop the run, warning for txs over a day in the mempool;
    • recheckUnconfirmedTransaction: confirmed (upserted, client payments confirmed), still in mempool (untouched), not found (orphaned), other chronik errors (thrown, untouched); MultiBlockchainClient routes by network;
    • isChronikTxNotFoundError, including txids containing 404; TX_CONFIRMED orphaning only on a real not-found;
    • fetchUnconfirmedTransactionsToRecheck query and per-tx dedupe.
  • prisma migrate diff between master's schema and this one produces exactly the SQL in the new migration.
  • tsc --noEmit: no new errors in the changed files (only the pre-existing redisBullMQ / Decimal ones).
  • To try it: with the jobs running, set a confirmed tx back to confirmed = false with a timestamp over 30 minutes old and wait for the next run (or lower UNCONFIRMED_TX_CHECK_INTERVAL); it should be confirmed again at its block timestamp. A made-up txid should be marked orphaned.

Remarks


Generated by Claude Code

Klakurka and others added 3 commits December 3, 2026 03:08
- Confirm txs with upsertTransaction (via a persistChronikTransaction helper
  shared with syncBlockTransactions) instead of a raw updateMany, so prices,
  caches and payment summaries follow the tx to its block timestamp, and mark
  the client payments it pays as CONFIRMED, as the TX_CONFIRMED handler does.
- Move the chronik lookup into ChronikBlockchainClient.recheckUnconfirmedTransaction,
  which returns confirmed / orphaned / unconfirmed; MultiBlockchainClient waits
  for startup and rejects unknown networks.
- Add isChronikTxNotFoundError: the old /not found in the index|404/ match also
  hit any error for a txid containing "404" and would have orphaned it. Use it
  in the TX_CONFIRMED handler too.
- Fetch one entry per tx (hash, network, first seen), oldest first, at most
  UNCONFIRMED_TX_CHECK_MAX_PER_RUN per run and only txs older than
  UNCONFIRMED_TX_CHECK_MIN_AGE_SECONDS, instead of every row of the last week:
  a backlog is worked through over several runs and older stuck txs are no
  longer skipped forever. Lookups stay sequential.
- Replace the confirmed-only index with (confirmed, orphaned, timestamp), which
  serves the job's query and order, in a migration dated after master's latest.
- Unit tests for the job, the chronik recheck, the not-found matcher and the
  DB query.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019G564i2sNiHJYQR95m8zGq
@coderabbitai

coderabbitai Bot commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 56 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 1f382400-8bd3-49f6-8f2b-8eea7f7a8cb0
Commits

Reviewing files that changed from the base of the PR and between f9d06f2 and a8bf120.

Files selected for processing: 11

  • constants/index.ts
  • jobs/initJobs.ts
  • jobs/workers.ts
  • prisma-local/migrations/20261203000000_transaction_unconfirmed_recheck_index/migration.sql
  • prisma-local/schema.prisma
  • services/chronikService.ts
  • services/transactionService.ts
  • tests/unittests/chronikService.test.ts
  • tests/unittests/transactionService.test.ts
  • tests/unittests/workers.test.ts
  • types/chronikTypes.ts
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants