Skip to content

Commit bfabbef

Browse files
committed
f - More send expectation to prevent failures in next commit
1 parent 1ba5c16 commit bfabbef

File tree

1 file changed

+31
-36
lines changed

1 file changed

+31
-36
lines changed

lightning-invoice/src/payment.rs

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,8 @@ mod tests {
832832

833833
let payer = TestPayer::new()
834834
.fails_on_attempt(2)
835-
.expect_send(Amount::ForInvoice(final_value_msat));
835+
.expect_send(Amount::ForInvoice(final_value_msat))
836+
.expect_send(Amount::OnRetry(final_value_msat / 2));
836837
let router = TestRouter {};
837838
let scorer = RefCell::new(TestScorer::new());
838839
let logger = TestLogger::new();
@@ -896,15 +897,19 @@ mod tests {
896897
let event_handled = core::cell::RefCell::new(false);
897898
let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
898899

899-
let payer = TestPayer::new();
900+
let payment_preimage = PaymentPreimage([1; 32]);
901+
let invoice = invoice(payment_preimage);
902+
let final_value_msat = invoice.amount_milli_satoshis().unwrap();
903+
904+
let payer = TestPayer::new()
905+
.expect_send(Amount::ForInvoice(final_value_msat))
906+
.expect_send(Amount::ForInvoice(final_value_msat));
900907
let router = TestRouter {};
901908
let scorer = RefCell::new(TestScorer::new());
902909
let logger = TestLogger::new();
903910
let invoice_payer =
904911
InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(0));
905912

906-
let payment_preimage = PaymentPreimage([1; 32]);
907-
let invoice = invoice(payment_preimage);
908913
let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
909914

910915
// Cannot repay an invoice pending payment.
@@ -955,15 +960,19 @@ mod tests {
955960

956961
#[test]
957962
fn fails_paying_invoice_with_sending_errors() {
958-
let payer = TestPayer::new().fails_on_attempt(1);
963+
let payment_preimage = PaymentPreimage([1; 32]);
964+
let invoice = invoice(payment_preimage);
965+
let final_value_msat = invoice.amount_milli_satoshis().unwrap();
966+
967+
let payer = TestPayer::new()
968+
.fails_on_attempt(1)
969+
.expect_send(Amount::ForInvoice(final_value_msat));
959970
let router = TestRouter {};
960971
let scorer = RefCell::new(TestScorer::new());
961972
let logger = TestLogger::new();
962973
let invoice_payer =
963974
InvoicePayer::new(&payer, router, &scorer, &logger, |_: &_| {}, RetryAttempts(0));
964975

965-
let payment_preimage = PaymentPreimage([1; 32]);
966-
let invoice = invoice(payment_preimage);
967976
match invoice_payer.pay_invoice(&invoice) {
968977
Err(PaymentError::Sending(_)) => {},
969978
Err(_) => panic!("unexpected error"),
@@ -1086,7 +1095,9 @@ mod tests {
10861095
let short_channel_id = Some(path[0].short_channel_id);
10871096

10881097
// Expect that scorer is given short_channel_id upon handling the event.
1089-
let payer = TestPayer::new();
1098+
let payer = TestPayer::new()
1099+
.expect_send(Amount::ForInvoice(final_value_msat))
1100+
.expect_send(Amount::OnRetry(final_value_msat / 2));
10901101
let router = TestRouter {};
10911102
let scorer = RefCell::new(TestScorer::new().expect_channel_failure(short_channel_id.unwrap()));
10921103
let logger = TestLogger::new();
@@ -1254,13 +1265,13 @@ mod tests {
12541265
}
12551266
}
12561267

1257-
fn check_attempts(&self) -> bool {
1268+
fn check_attempts(&self) -> Result<PaymentId, PaymentSendFailure> {
12581269
let mut attempts = self.attempts.borrow_mut();
12591270
*attempts += 1;
12601271
match self.failing_on_attempt {
1261-
None => true,
1262-
Some(attempt) if attempt != *attempts => true,
1263-
Some(_) => false,
1272+
None => Ok(PaymentId([1; 32])),
1273+
Some(attempt) if attempt != *attempts => Ok(PaymentId([1; 32])),
1274+
Some(_) => Err(PaymentSendFailure::ParameterError(APIError::MonitorUpdateFailed)),
12641275
}
12651276
}
12661277

@@ -1295,41 +1306,25 @@ mod tests {
12951306
}
12961307

12971308
fn send_payment(
1298-
&self,
1299-
route: &Route,
1300-
_payment_hash: PaymentHash,
1309+
&self, route: &Route, _payment_hash: PaymentHash,
13011310
_payment_secret: &Option<PaymentSecret>
13021311
) -> Result<PaymentId, PaymentSendFailure> {
1303-
if self.check_attempts() {
1304-
self.check_value_msats(Amount::ForInvoice(route.get_total_amount()));
1305-
Ok(PaymentId([1; 32]))
1306-
} else {
1307-
Err(PaymentSendFailure::ParameterError(APIError::MonitorUpdateFailed))
1308-
}
1312+
self.check_value_msats(Amount::ForInvoice(route.get_total_amount()));
1313+
self.check_attempts()
13091314
}
13101315

13111316
fn send_spontaneous_payment(
1312-
&self,
1313-
route: &Route,
1314-
_payment_preimage: PaymentPreimage,
1317+
&self, route: &Route, _payment_preimage: PaymentPreimage,
13151318
) -> Result<PaymentId, PaymentSendFailure> {
1316-
if self.check_attempts() {
1317-
self.check_value_msats(Amount::Spontaneous(route.get_total_amount()));
1318-
Ok(PaymentId([1; 32]))
1319-
} else {
1320-
Err(PaymentSendFailure::ParameterError(APIError::MonitorUpdateFailed))
1321-
}
1319+
self.check_value_msats(Amount::Spontaneous(route.get_total_amount()));
1320+
self.check_attempts()
13221321
}
13231322

13241323
fn retry_payment(
13251324
&self, route: &Route, _payment_id: PaymentId
13261325
) -> Result<(), PaymentSendFailure> {
1327-
if self.check_attempts() {
1328-
self.check_value_msats(Amount::OnRetry(route.get_total_amount()));
1329-
Ok(())
1330-
} else {
1331-
Err(PaymentSendFailure::ParameterError(APIError::MonitorUpdateFailed))
1332-
}
1326+
self.check_value_msats(Amount::OnRetry(route.get_total_amount()));
1327+
self.check_attempts().map(|_| ())
13331328
}
13341329
}
13351330

0 commit comments

Comments
 (0)