Feature/distributed transaction coordinator - #62
Merged
akargi merged 5 commits intoAug 30, 2026
Conversation
…batches Add TransactionBatch, BatchLeg, and BatchAuditLog TypeORM entities with enums for batch/leg lifecycle states. Include CreateBatchDto, QueryBatchDto, ConditionalLegDto, and BatchDetailResponse for API validation and response shapes. 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
…, and retry logic TransactionGraphBuilder: constructs dependency graphs from swap legs, performs topological sort with cycle detection, and builds parallel execution layers for sub-millisecond graph construction. StateConsistencyChecker: validates invariants before/after each 2PC phase — no negative amounts, price bounds respected, conditional legs properly configured, and actual outputs meet minimums. RetryLogicService: exponential backoff with decorrelated jitter for transient network/transport failures, with configurable max retries, base delay, and jitter factor. 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
…tocol Implements Two-Phase Commit (2PC) for coordinated multi-leg swaps: - Prepare phase: simulates all legs via Soroban RPC, optimizes gas, evaluates conditional legs against market data - Commit phase: executes prepared legs in parallel layers, rolls back all on any failure to ensure atomicity - MEV-resistant ordering via timestamp-based sequence numbers - Audit logging for every state transition 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
…nd module TransactionCoordinatorService: orchestrates the full 2PC lifecycle with timeout handling, batch cancellation, partial fill recovery, and statistics. Supports both automatic (executeBatch) and manual (prepareBatch + commitBatch) two-phase commit workflows. TransactionCoordinatorController: REST API with Swagger docs for creating, executing, preparing, committing, and cancelling batches, plus audit trail and statistics endpoints. TransactionCoordinatorModule: registered in AppModule with TypeORM entities and StellarModule dependency for Soroban contract interaction. 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
…istency checker, and retry logic 50 tests covering: - TransactionGraphBuilder: linear/parallel/chain graphs, cycle detection, dependency resolution, self-dependency rejection, ready-leg calculation - StateConsistencyChecker: pre-execution/post-prepare/post-commit/post-rollback checks, condition evaluation (price_gt/lt/gte/lte), edge cases - RetryLogicService: retry on transient errors, backoff calculation, jitter behavior, non-retryable error detection, summary computation 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
1 task
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.
Summary
Implements a distributed transaction coordinator that enables atomic multi-leg swaps (e.g., USD → EUR → JPY) using a Two-Phase Commit protocol on the Stellar/Soroban network. This eliminates partial fills, stranded assets, and failed arbitrage opportunities when executing complex multi-pool aggregations.
Related Issues
Closes #47
Implements the full acceptance criteria for the distributed transaction coordinator with atomic swap support.
Type of Change
Changes
Entities & DTOs (3 files, 294 lines)
TransactionBatch— batch lifecycle with statuses: created → preparing → prepared → committing → committed | rolled_back | failed | expiredBatchLeg— individual swap legs with dependency tracking, conditional execution support, and execution metadataBatchAuditLog— immutable audit trail for every state transition (17 action types)CreateBatchDto,QueryBatchDto,ConditionalLegDto,BatchDetailResponsewith class-validator + Swagger decoratorsTransaction Graph Builder (299 lines)
State Consistency Checker (391 lines)
price_gt,price_lt,price_gte,price_lte,amount_gt,amount_ltRetry Logic (188 lines)
Atomic Batch Executor (734 lines) — Two-Phase Commit protocol:
GasOptimizationEngine, evaluates conditional legs against current market data, records prepared stateContractInvocationService, automatic rollback of all committed legs on any failureCoordinator Service (535 lines)
executeBatch()(automatic) orprepareBatch()+commitBatch()(manual)Controller & Module (170 + 37 lines)
TransactionCoordinatorModuleregistered inAppModulewithStellarModuledependencyTests (947 lines, 50 tests across 3 suites)
TransactionGraphBuilderService: 12 tests — linear/parallel/chain graphs, cycle detection, self-dependency, invalid index, implicit deps, validation, ready-leg calculationStateConsistencyCheckerService: 20 tests — pre-execution/post-prepare/post-commit/post-rollback checks, condition evaluation for all operators, edge casesRetryLogicService: 18 tests — retry on transient errors, backoff calculation, jitter behavior, non-retryable detection, summary computationChecklist
npm run lintpasses (new files clean; pre-existing lint errors unchanged).npx tsc --noEmitpasses (zero type errors).npm run buildsucceeds.npm testpasses — 50 new tests added for graph builder, consistency checker, and retry logic.StellarModuleservices (ContractInvocationService,GasOptimizationEngine) — no direct Horizon/RPC calls.Notes for Reviewers
Architecture: Follows existing NestJS patterns (TypeORM entities, module/service/controller, Swagger). The new
TransactionCoordinatorModuleis self-contained with a single dependency onStellarModule.Key design decisions:
Date.now()to enforce timestamp-based orderingAtomicBatchExecutorproduces mock tx hashes (consistent with existingStellarService.executeSettlementpattern) until real signing is wired upTesting: The 3 tested services (graph builder, consistency checker, retry logic) are pure logic with no external dependencies. The executor and coordinator services would need mocked Stellar/Soroban services for integration tests (recommended as follow-up).