Skip to content
This repository was archived by the owner on Feb 23, 2021. It is now read-only.

Commit 5f142ce

Browse files
valentinewallacetanx
authored andcommitted
Add timeout to payLightning so we go to PaymentFailed on timeout.
The timeout needs to be in the sendPayment promise to ensure that we resolve the promise on timeout, otherwise it might resolve/reject later on. This could cause undesirable side effects such as navigating to the success screen or showing an unexpected notification.
1 parent 6755c48 commit 5f142ce

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

src/action/payment.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* call the corresponding GRPC apis for payment management.
44
*/
55

6-
import { PREFIX_URI } from '../config';
6+
import { PREFIX_URI, PAYMENT_TIMEOUT } from '../config';
77
import {
88
toSatoshis,
99
toAmount,
@@ -174,17 +174,26 @@ class PaymentAction {
174174
this._nav.goWait();
175175
const invoice = this._store.payment.address.replace(PREFIX_URI, '');
176176
const stream = this._grpc.sendStreamCommand('sendPayment');
177-
await new Promise((resolve, reject) => {
177+
const success = await new Promise((resolve, reject) => {
178+
const timeout = setTimeout(() => resolve(false), PAYMENT_TIMEOUT);
178179
stream.on('data', data => {
179180
if (data.payment_error) {
181+
clearTimeout(timeout);
180182
reject(new Error(`Lightning payment error: ${data.payment_error}`));
181183
} else {
182-
resolve();
184+
clearTimeout(timeout);
185+
resolve(true);
183186
}
184187
});
185-
stream.on('error', reject);
188+
stream.on('error', () => {
189+
clearTimeout(timeout);
190+
reject();
191+
});
186192
stream.write(JSON.stringify({ payment_request: invoice }), 'utf8');
187193
});
194+
if (!success) {
195+
this._nav.goPaymentFailed();
196+
}
188197
this._nav.goPayLightningDone();
189198
} catch (err) {
190199
this._nav.goPayLightningConfirm();

src/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module.exports.RETRY_DELAY = 1000;
66
module.exports.LND_INIT_DELAY = 5000;
77
module.exports.NOTIFICATION_DELAY = 5000;
88
module.exports.RATE_DELAY = 15 * 60 * 1000;
9+
module.exports.PAYMENT_TIMEOUT = 60 * 1000;
910

1011
module.exports.LND_PORT = 10006;
1112
module.exports.LND_PEER_PORT = 10016;

test/unit/action/payment.spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ describe('Action Payments Unit Tests', () => {
2222
store = new Store();
2323
store.settings.displayFiat = false;
2424
require('../../../src/config').RETRY_DELAY = 1;
25+
require('../../../src/config').PAYMENT_TIMEOUT = 500;
2526
grpc = sinon.createStubInstance(GrpcAction);
2627
notification = sinon.createStubInstance(NotificationAction);
2728
nav = sinon.createStubInstance(NavAction);
@@ -240,5 +241,11 @@ describe('Action Payments Unit Tests', () => {
240241
expect(nav.goPayLightningConfirm, 'was called once');
241242
expect(notification.display, 'was called once');
242243
});
244+
245+
it('should go to error page on timeout', async () => {
246+
await payment.payLightning({ invoice: 'some-invoice' });
247+
expect(nav.goPaymentFailed, 'was called once');
248+
expect(nav.goPayLightningConfirm, 'was not called');
249+
});
243250
});
244251
});

0 commit comments

Comments
 (0)