Repository navigation
Description
Overview
EscrowService.fund's error handling assumes any exception thrown inside its try block means the on-chain call failed — but the try block covers both the on-chain call and the subsequent DB write, and conflating the two produces a wrong, financially dangerous conclusion when it's actually the DB write that failed:
// src/escrow/escrow.service.ts:66-91
await this.escrowRepo.save(escrow); // PENDING row created
try {
const referenceId = input.bountyId ?? input.milestoneId ?? input.maintenancePoolId ?? escrow.id;
const result = await this.soroban.invoke('fund', [
input.funderAddress,
referenceId,
this.toStroops(input.amount),
]);
escrow.status = EscrowStatus.LOCKED;
escrow.fundTxHash = result.txHash;
escrow.lockedAt = new Date();
escrow.metadata = { fund: result };
return this.escrowRepo.save(escrow); // <-- if THIS throws...
} catch (err) {
escrow.status = EscrowStatus.FAILED; // <-- ...we land here anyway
escrow.metadata = { error: (err as Error).message };
await this.escrowRepo.save(escrow);
throw err;
}If this.soroban.invoke('fund', ...) succeeds — the on-chain transaction lands, funds are genuinely locked in the contract — but the second this.escrowRepo.save(escrow) call three lines later throws (a Postgres connection drop, a pool exhaustion, a transient network blip to the DB — nothing to do with Soroban at all), execution falls into the catch block. The catch block has no way to distinguish "the on-chain call itself failed" from "the on-chain call succeeded but the follow-up write failed" — it unconditionally sets escrow.status = EscrowStatus.FAILED and persists that. The result: a row that says FAILED (implying no funds were ever locked, safe to retry funding from scratch) for an escrow that is, in on-chain reality, LOCKED — with result.txHash never even recorded anywhere, since the object holding it (escrow) never got its fundTxHash/lockedAt/metadata fields persisted before the throw.
This is a narrower, more specific bug than "no DB transactions" or "no reconciliation job" (the two related open issues in this repo) — it's not merely that the write can fail non-atomically with the on-chain call (true, and those issues track fixing that generally); it's that this specific catch block actively records the wrong, misleading status for exactly the failure mode it's supposed to handle gracefully. A generic transaction wrapper wouldn't fix this on its own — the write that needs to happen after a DB-layer failure (mark LOCKED+flag for reconciliation) is different from the write that needs to happen after a Soroban-layer failure (mark FAILED), and today the code can't tell them apart because both paths funnel through one catch.
Requirements
- Split the
try/catchso the Soroban call and the follow-up DB write have distinguishable failure handling: ifsoroban.invokeitself throws,FAILEDis correct (nothing happened on-chain, matches today's behavior). Ifsoroban.invokesucceeds but the subsequentescrowRepo.savethrows, the escrow must not be markedFAILED— at minimum, log this as a distinct, loudly-flagged condition ("on-chain fund succeeded, local persist failed") including the already-knownresult.txHash, so it's discoverable rather than silently mislabeled. - Since the whole point of
escrow.statusis to tell the rest of the system (sponsor dashboards, refund eligibility, reconciliation) what's true on-chain, a mislabeledFAILEDrow is actively worse than no row at all — it could lead a sponsor or an admin to believe it's safe to re-fund the same bounty (creating a duplicate on-chain lock; see the companion "duplicate escrow per parent" issue for why nothing stops that today) when funds are already locked. - Apply the same split to
release,splitRelease,releasePartial, andrefundin the same file — none of them wrap the post-invoke DB write in a way that distinguishes "Soroban failed" from "Soroban succeeded, DB write failed," andrefundin particular has notry/catchat all around its post-invoke write, so a DB failure there throws an unlabeled error with no status update whatsoever, leaving the escrow silently stuck atLOCKEDwith a completed on-chain refund the local ledger has no record of.
Acceptance Criteria
- A DB write failure that occurs after a successful
soroban.invoke('fund', ...)no longer results inescrow.status = FAILED. - The failure mode "on-chain succeeded, local persist failed" is distinguishably logged/flagged (even if full self-healing is out of scope and left to the reconciliation-job issue) — someone reading the logs or querying the DB can tell this happened without cross-referencing Soroban directly.
- The same audit and fix is applied to
release,splitRelease,releasePartial, andrefund. - A test simulates
soroban.invokeresolving successfully followed byescrowRepo.saverejecting, and asserts the escrow's final persisted status is notFAILED.
Additional Notes
Precise references: src/escrow/escrow.service.ts:53-91 (fund), :94-127 (release, no catch at all around the post-invoke save/payment-create), :133-171 (splitRelease, same), :180-230 (releasePartial, same), :232-248 (refund, no try/catch at all).
Test/reproduction plan:
soroban.invoke.mockResolvedValue({ txHash: 'real-tx', ledger: 1, returnValue: null, status: 'SUCCESS' });
escrowRepo.save
.mockResolvedValueOnce(pendingEscrow) // first save (PENDING) succeeds
.mockRejectedValueOnce(new Error('ECONNRESET')); // second save (LOCKED) fails
await expect(service.fund(input)).rejects.toThrow();
// assert: whatever WAS persisted (or logged) does not claim status FAILED,
// and ideally captures result.txHash somewhere discoverable.Cross-references: related to, but distinct from, the open "no database transactions anywhere" and "outbox/reconciliation job" issues in this repo — those track the general non-atomicity and the lack of a self-healing sweep; this issue is about the catch block's own logic actively producing a wrong status label for one specific, common failure ordering, which a reconciliation job would have to specifically know to distrust (FAILED rows would normally be assumed to mean "no on-chain effect," which this bug falsifies). Fixing this narrows what the reconciliation job has to worry about. Also related to the companion "duplicate escrow per parent" issue: a sponsor who sees FAILED and re-funds via this exact bug path is the most likely real-world trigger for that duplicate-lock scenario.
Activity
test: duplicate issue reference no longer produces a spurious FAILED
fix(escrow): stop mislabeling a funded escrow as FAILED on a post-inv…