Skip to content

Commit 274230a

Browse files
committed
fix(swaps): failed payment for completed swap
This handles an edge case described below that causes xud to crash due to an assertion error when a payment fails as taker for a swap that has already completed, as described below. 1. Taker sends payment. 2. Maker sees payment, and sends payment to Taker. 3. Taker receives payment, and marks the swap as complete. 4. Maker fails to claim payment, so the attempt to send payment (from step 1) fails. 5. Error handling logic when Taker's payment attempt doesn't go through causes it to "fail" the deal. 6. But the swap actually succeeded - from Taker's perspective - in step 3, and Taker hits an assertion error trying to fail a deal that has completed. Fixes #1569.
1 parent 9875e00 commit 274230a

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

Diff for: lib/swaps/Swaps.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,13 @@ class Swaps extends EventEmitter {
759759
try {
760760
await makerSwapClient.sendPayment(deal);
761761
} catch (err) {
762+
// first we must handle the edge case where the maker has paid us but failed to claim our payment
763+
// in this case, we've already marked the swap as having been paid and completed
764+
if (deal.state !== SwapState.Completed) {
765+
this.logger.warn(`maker was unable to claim payment for ${deal.rHash} but has already paid us`);
766+
return;
767+
}
768+
762769
if (err.code === errorCodes.PAYMENT_REJECTED) {
763770
// if the maker rejected our payment, the swap failed due to an error on their side
764771
// and we don't need to send them a SwapFailedPacket
@@ -776,7 +783,6 @@ class Swaps extends EventEmitter {
776783
errorMessage: err.message,
777784
});
778785
}
779-
return;
780786
}
781787
}
782788

Diff for: test/simulation/custom-xud.patch

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
diff --git a/lib/Xud.ts b/lib/Xud.ts
2-
index 810345d90..9585a2761 100644
2+
index b50acec47..bfd7a86d6 100644
33
--- a/lib/Xud.ts
44
+++ b/lib/Xud.ts
55
@@ -87,6 +87,11 @@ class Xud extends EventEmitter {
@@ -36,7 +36,7 @@ index f077b3ca4..7398a97b4 100644
3636
}
3737

3838
diff --git a/lib/swaps/Swaps.ts b/lib/swaps/Swaps.ts
39-
index fd7024fa2..d469ce7ac 100644
39+
index 5ee168fa9..f0a3b879d 100644
4040
--- a/lib/swaps/Swaps.ts
4141
+++ b/lib/swaps/Swaps.ts
4242
@@ -205,9 +205,28 @@ class Swaps extends EventEmitter {
@@ -110,9 +110,9 @@ index fd7024fa2..d469ce7ac 100644
110110
+ return;
111111
+ }
112112
} catch (err) {
113-
if (err.code === errorCodes.PAYMENT_REJECTED) {
114-
// if the maker rejected our payment, the swap failed due to an error on their side
115-
@@ -933,12 +972,29 @@ class Swaps extends EventEmitter {
113+
// first we must handle the edge case where the maker has paid us but failed to claim our payment
114+
// in this case, we've already marked the swap as having been paid and completed
115+
@@ -939,12 +978,29 @@ class Swaps extends EventEmitter {
116116

117117
this.logger.debug('Executing maker code to resolve hash');
118118

@@ -142,7 +142,7 @@ index fd7024fa2..d469ce7ac 100644
142142
deal.rPreimage = await swapClient.sendPayment(deal);
143143
return deal.rPreimage;
144144
} catch (err) {
145-
@@ -968,6 +1024,16 @@ class Swaps extends EventEmitter {
145+
@@ -974,6 +1030,16 @@ class Swaps extends EventEmitter {
146146
assert(htlcCurrency === undefined || htlcCurrency === deal.takerCurrency, 'incoming htlc does not match expected deal currency');
147147
this.logger.debug('Executing taker code to resolve hash');
148148

0 commit comments

Comments
 (0)