feat: blockchain reorganization detection for transaction confirmations (#514)#537
Open
primexk5 wants to merge 1 commit into
Open
feat: blockchain reorganization detection for transaction confirmations (#514)#537primexk5 wants to merge 1 commit into
primexk5 wants to merge 1 commit into
Conversation
…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>
|
@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. |
|
@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! 🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this PR does
ConfirmationTracker— per-network in-memory tracking with configurable safety thresholds (Ethereum=12, Polygon=64, Stellar=1, env-overridable viaCONFIRMATION_THRESHOLD_*)ReorgDetector— polls EVM chains via ethers.jsJsonRpcProviderand Stellar via Horizon REST API; detectsparentHashmismatches and same-height sibling blocks; atomically persistsReorgEvent+TransactionReorgrows viaprisma.$transaction; marks affected paymentspending_review; enqueues BullMQ re-verification jobs (5× exponential backoff); fires webhook alerts when depth exceeds safety thresholdGET /api/v1/chain/reorgs,/dashboard,/history,/:id,POST /simulate) all protected byverifyInternalSignatureHMAC middlewareReorgEventandTransactionReorgPrisma models +pending_reviewpayment statusChanges
backend/prisma/schema.prisma— newReorgEvent,TransactionReorgmodels;pending_reviewpayment statusbackend/src/services/chain/confirmation-tracker.ts— new filebackend/src/services/chain/reorg-detector.ts— new filebackend/src/routes/reorg.ts— new file (5 routes)backend/src/index.ts— wired up router and detector lifecyclebackend/src/services/chain/__tests__/reorg-detector.test.ts— 20 unit testsWhy
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
For manual testing, POST to
/api/v1/chain/reorgs/simulatewith a validx-internal-signatureheader in dev mode.Notes
prisma.$transaction()ensures atomic writes across ReorgEvent + TransactionReorg + Paymentstart()is idempotent — safe to call multiple timesCloses #514