Skip to content

feat: blockchain reorganization detection for transaction confirmations (#514)#537

Open
primexk5 wants to merge 1 commit into
Smartdevs17:mainfrom
primexk5:feat/blockchain-reorg-detection-514
Open

feat: blockchain reorganization detection for transaction confirmations (#514)#537
primexk5 wants to merge 1 commit into
Smartdevs17:mainfrom
primexk5:feat/blockchain-reorg-detection-514

Conversation

@primexk5

Copy link
Copy Markdown

What this PR does

  • Implements blockchain reorganization detection per issue Security: Implement Blockchain Reorganization Detection for Transaction Confirmations #514
  • Adds ConfirmationTracker — per-network in-memory tracking with configurable safety thresholds (Ethereum=12, Polygon=64, Stellar=1, env-overridable via CONFIRMATION_THRESHOLD_*)
  • Adds ReorgDetector — polls EVM chains via ethers.js JsonRpcProvider and Stellar via Horizon REST API; detects parentHash mismatches and same-height sibling blocks; atomically persists ReorgEvent + TransactionReorg rows via prisma.$transaction; marks affected payments pending_review; enqueues BullMQ re-verification jobs (5× exponential backoff); fires webhook alerts when depth exceeds safety threshold
  • Adds REST routes (GET /api/v1/chain/reorgs, /dashboard, /history, /:id, POST /simulate) all protected by verifyInternalSignature HMAC middleware
  • Adds ReorgEvent and TransactionReorg Prisma models + pending_review payment status

Changes

  • backend/prisma/schema.prisma — new ReorgEvent, TransactionReorg models; pending_review payment status
  • backend/src/services/chain/confirmation-tracker.ts — new file
  • backend/src/services/chain/reorg-detector.ts — new file
  • backend/src/routes/reorg.ts — new file (5 routes)
  • backend/src/index.ts — wired up router and detector lifecycle
  • backend/src/services/chain/__tests__/reorg-detector.test.ts — 20 unit tests

Why

Resolves all acceptance criteria from issue #514: reorg detection service, confirmation tracker, Prisma models, BullMQ queue for re-verification, dashboard routes, and integration tests.

How to test

cd backend
npm run db:generate
npx vitest run src/services/chain/__tests__/reorg-detector.test.ts
# 20/20 tests should pass

For manual testing, POST to /api/v1/chain/reorgs/simulate with a valid x-internal-signature header in dev mode.

Notes

  • Stellar uses Horizon REST API, not EVM JSON-RPC (separate provider)
  • prisma.$transaction() ensures atomic writes across ReorgEvent + TransactionReorg + Payment
  • start() is idempotent — safe to call multiple times

Closes #514

…ns (Smartdevs17#514)

Implements the full reorg detection pipeline per issue Smartdevs17#514:

- **ConfirmationTracker** (`confirmation-tracker.ts`): in-memory per-network
  tracking with configurable thresholds (Ethereum=12, Polygon=64, Stellar=1,
  all env-overridable). Detects finality and exposes findAffected() for
  orphaned block ranges.

- **ReorgDetector** (`reorg-detector.ts`): polls EVM chains via ethers.js
  JsonRpcProvider and Stellar via Horizon REST API. Detects parentHash
  mismatches and same-height sibling blocks, walks back to common ancestor,
  atomically persists ReorgEvent + TransactionReorg rows via
  prisma.$transaction, marks affected payments as pending_review, and enqueues
  BullMQ re-verification jobs with 5× exponential backoff. Fires webhook
  alerts when reorg depth exceeds safety threshold.

- **Reorg routes** (`routes/reorg.ts`): GET /api/v1/chain/reorgs,
  /dashboard, /history, /:id, POST /simulate — all protected by
  verifyInternalSignature HMAC middleware.

- **Prisma schema**: adds ReorgEvent and TransactionReorg models plus
  pending_review payment status.

- **20 unit tests** covering ConfirmationTracker, simulateReorg (including
  rolled_back status), and pollChain (same-height reorg, idempotent start,
  idle-poll no-op, parentHash mismatch).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@vercel

vercel Bot commented Jun 26, 2026

Copy link
Copy Markdown

@primexk5 is attempting to deploy a commit to the smartdevs17's projects Team on Vercel.

A member of the Team first needs to authorize it.

@drips-wave

drips-wave Bot commented Jun 26, 2026

Copy link
Copy Markdown

@primexk5 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

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.

Security: Implement Blockchain Reorganization Detection for Transaction Confirmations

1 participant