Skip to content

feat: add event worker deduplication and request ID tracing#400

Open
WISDOM-WOKE22 wants to merge 1 commit intoLabsCrypt:mainfrom
WISDOM-WOKE22:main
Open

feat: add event worker deduplication and request ID tracing#400
WISDOM-WOKE22 wants to merge 1 commit intoLabsCrypt:mainfrom
WISDOM-WOKE22:main

Conversation

@WISDOM-WOKE22
Copy link
Copy Markdown

Summary

Implements two critical hardening features to improve system reliability and observability:

1. Event Worker Deduplication (Addresses #383)

  • Adds unique constraint on StreamEvent(transactionHash, eventType) to prevent duplicate event processing
  • Replaces cursor-based pagination vulnerability where crashes between DB write and cursor update caused duplicate records
  • Implements idempotent event processing with upsert pattern and warning logs for detected duplicates
  • New test validates replay safety: processing same batch twice produces one record

2. Request ID Tracing (Addresses #381)

  • Adds end-to-end request correlation via AsyncLocalStorage and UUID generation
  • Every log entry automatically includes requestId field for trace correlation
  • Response includes X-Request-ID header (echoes client-provided ID if valid, up to 128 chars)
  • SSE connected event includes requestId for client-side connection tracking
  • Logs request received/response sent with method, path, status, duration, and requestId

Changes

Backend Schema & Migrations:

  • backend/prisma/schema.prisma — Added unique constraint
  • backend/prisma/migrations/20260428000000_add_stream_event_unique_constraint/ — Migration SQL

Event Worker Deduplication:

  • backend/src/workers/soroban-event-worker.ts — Updated 6 event handlers (CREATED, TOPPED_UP, WITHDRAWN, CANCELLED, COMPLETED, FEE_COLLECTED) to use upsert pattern with duplicate detection
  • backend/tests/soroban-event-worker.test.ts — New test suite validating idempotency

Request ID Middleware:

  • backend/src/middleware/requestId.ts — New middleware (AsyncLocalStorage + logging)
  • backend/src/logger.ts — Added requestContext export and Winston format enhancement
  • backend/src/app.ts — Mounted requestId middleware in request chain
  • backend/src/controllers/sse.controller.ts — Added requestId to SSE connected event

Test Results

✅ All event worker idempotency tests pass
✅ All previously passing tests still pass
✅ Zero impact on existing functionality

closes #383, closes #381

🤖 Generated with Claude Code

- Add unique constraint on StreamEvent(transactionHash, eventType) to prevent duplicate event processing on worker restart
- Replace streamEvent.create with upsert pattern that detects and logs duplicate events
- Add migration for the unique constraint
- Add test_event_processing_is_idempotent_on_restart test verifying no duplicates on replay

- Add AsyncLocalStorage-based request ID middleware for end-to-end correlation
- Include requestId in all Winston log entries automatically
- Add X-Request-ID response header (reads X-Request-ID from request if valid)
- Include requestId in SSE connected event payload for client-side tracking
- Log request received/response sent with method, path, status, duration, and requestId

closes LabsCrypt#383, closes LabsCrypt#381

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
@ogazboiz
Copy link
Copy Markdown
Contributor

hey, main was failing CI from broken auth imports + frontend parse errors. fixed and pushed to main now. please rebase to pick up the fixes:

git fetch upstream
git rebase upstream/main
git push --force-with-lease

if there's a conflict, resolve it locally and we'll review once CI is green.

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.

Add request ID tracing across API logs Add event worker deduplication for restart safety

2 participants