- Err(_) => panic!("unexpected error"),
- Ok(_) => panic!("expected invoice error"),
- }
- }
-
- #[test]
- fn pays_pubkey_with_amount() {
- let event_handled = core::cell::RefCell::new(false);
- let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
-
- let pubkey = pubkey();
- let payment_preimage = PaymentPreimage([1; 32]);
- let payment_hash = PaymentHash(Sha256::hash(&payment_preimage.0).into_inner());
- let final_value_msat = 100;
- let final_cltv_expiry_delta = 42;
-
- let payer = TestPayer::new()
- .expect_send(Amount::Spontaneous(final_value_msat))
- .expect_send(Amount::OnRetry(final_value_msat));
- let router = TestRouter {};
- let scorer = RefCell::new(TestScorer::new());
- let logger = TestLogger::new();
- let invoice_payer =
- InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
-
- let payment_id = Some(invoice_payer.pay_pubkey(
- pubkey, payment_preimage, final_value_msat, final_cltv_expiry_delta
- ).unwrap());
- assert_eq!(*payer.attempts.borrow(), 1);
-
- let retry = RouteParameters {
- payee: Payee::for_keysend(pubkey),
- final_value_msat,
- final_cltv_expiry_delta,
- };
- let event = Event::PaymentPathFailed {
- payment_id,
- payment_hash,
- network_update: None,
- rejected_by_dest: false,
- all_paths_failed: false,
- path: vec![],
- short_channel_id: None,
- retry: Some(retry),
- };
- invoice_payer.handle_event(&event);
- assert_eq!(*event_handled.borrow(), false);
- assert_eq!(*payer.attempts.borrow(), 2);
-
- invoice_payer.handle_event(&Event::PaymentSent {
- payment_id, payment_preimage, payment_hash, fee_paid_msat: None
- });
- assert_eq!(*event_handled.borrow(), true);
- assert_eq!(*payer.attempts.borrow(), 2);
- }
-
- #[test]
- fn scores_failed_channel() {
- let event_handled = core::cell::RefCell::new(false);
- let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
-
- let payment_preimage = PaymentPreimage([1; 32]);
- let invoice = invoice(payment_preimage);
- let payment_hash = PaymentHash(invoice.payment_hash().clone().into_inner());
- let final_value_msat = invoice.amount_milli_satoshis().unwrap();
- let path = TestRouter::path_for_value(final_value_msat);
- let short_channel_id = Some(path[0].short_channel_id);
-
- // Expect that scorer is given short_channel_id upon handling the event.
- let payer = TestPayer::new()
- .expect_send(Amount::ForInvoice(final_value_msat))
- .expect_send(Amount::OnRetry(final_value_msat / 2));
- let router = TestRouter {};
- let scorer = RefCell::new(TestScorer::new().expect_channel_failure(short_channel_id.unwrap()));
- let logger = TestLogger::new();
- let invoice_payer =
- InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
-
- let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
- let event = Event::PaymentPathFailed {
- payment_id,
- payment_hash,
- network_update: None,
- rejected_by_dest: false,
- all_paths_failed: false,
- path,
- short_channel_id,
- retry: Some(TestRouter::retry_for_invoice(&invoice)),
- };
- invoice_payer.handle_event(&event);
- }
-
- struct TestRouter;
-
- impl TestRouter {
- fn route_for_value(final_value_msat: u64) -> Route {
- Route {
- paths: vec![
- vec![RouteHop {
- pubkey: PublicKey::from_slice(&hex::decode("02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619").unwrap()[..]).unwrap(),
- channel_features: ChannelFeatures::empty(),
- node_features: NodeFeatures::empty(),
- short_channel_id: 0, fee_msat: final_value_msat / 2, cltv_expiry_delta: 144
- }],
- vec![RouteHop {
- pubkey: PublicKey::from_slice(&hex::decode("0324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c").unwrap()[..]).unwrap(),
- channel_features: ChannelFeatures::empty(),
- node_features: NodeFeatures::empty(),
- short_channel_id: 1, fee_msat: final_value_msat / 2, cltv_expiry_delta: 144
- }],
- ],
- payee: None,
- }
- }
-
- fn path_for_value(final_value_msat: u64) -> Vec<RouteHop> {
- TestRouter::route_for_value(final_value_msat).paths[0].clone()
- }
-
- fn retry_for_invoice(invoice: &Invoice) -> RouteParameters {
- let mut payee = Payee::from_node_id(invoice.recover_payee_pub_key())
- .with_expiry_time(expiry_time_from_unix_epoch(invoice).as_secs())
- .with_route_hints(invoice.route_hints());
- if let Some(features) = invoice.features() {
- payee = payee.with_features(features.clone());
- }
- let final_value_msat = invoice.amount_milli_satoshis().unwrap() / 2;
- RouteParameters {
- payee,
- final_value_msat,
- final_cltv_expiry_delta: invoice.min_final_cltv_expiry() as u32,
- }
- }
- }
-
- impl<S: routing::Score> Router<S> for TestRouter {
- fn find_route(
- &self, _payer: &PublicKey, params: &RouteParameters, _payment_hash: &PaymentHash,
- _first_hops: Option<&[&ChannelDetails]>, _scorer: &S
- ) -> Result<Route, LightningError> {
- Ok(Route {
- payee: Some(params.payee.clone()), ..Self::route_for_value(params.final_value_msat)
- })
- }
- }
-
- struct FailingRouter;
-
- impl<S: routing::Score> Router<S> for FailingRouter {
- fn find_route(
- &self, _payer: &PublicKey, _params: &RouteParameters, _payment_hash: &PaymentHash,
- _first_hops: Option<&[&ChannelDetails]>, _scorer: &S
- ) -> Result<Route, LightningError> {
- Err(LightningError { err: String::new(), action: ErrorAction::IgnoreError })
- }
- }
-
- struct TestScorer {
- expectations: VecDeque<u64>,
- }
-
- impl TestScorer {
- fn new() -> Self {
- Self {
- expectations: VecDeque::new(),
- }
- }
-
- fn expect_channel_failure(mut self, short_channel_id: u64) -> Self {
- self.expectations.push_back(short_channel_id);
- self
- }
- }
-
- impl routing::Score for TestScorer {
- fn channel_penalty_msat(
- &self, _short_channel_id: u64, _send_amt: u64, _chan_amt: Option<u64>, _source: &NodeId, _target: &NodeId
- ) -> u64 { 0 }
-
- fn payment_path_failed(&mut self, _path: &[&RouteHop], short_channel_id: u64) {
- if let Some(expected_short_channel_id) = self.expectations.pop_front() {
- assert_eq!(short_channel_id, expected_short_channel_id);
- }
- }
- }
-
- impl Drop for TestScorer {
- fn drop(&mut self) {
- if std::thread::panicking() {
- return;
- }
-
- if !self.expectations.is_empty() {
- panic!("Unsatisfied channel failure expectations: {:?}", self.expectations);
- }
- }
- }
-
- struct TestPayer {
- expectations: core::cell::RefCell<VecDeque<Amount>>,
- attempts: core::cell::RefCell<usize>,
- failing_on_attempt: core::cell::RefCell<HashMap<usize, PaymentSendFailure>>,
- }
-
- #[derive(Clone, Debug, PartialEq, Eq)]
- enum Amount {
- ForInvoice(u64),
- Spontaneous(u64),
- OnRetry(u64),
- }
-
- struct OnAttempt(usize);
-
- impl TestPayer {
- fn new() -> Self {
- Self {
- expectations: core::cell::RefCell::new(VecDeque::new()),
- attempts: core::cell::RefCell::new(0),
- failing_on_attempt: core::cell::RefCell::new(HashMap::new()),
- }
- }
-
- fn expect_send(self, value_msat: Amount) -> Self {
- self.expectations.borrow_mut().push_back(value_msat);
- self
- }
-
- fn fails_on_attempt(self, attempt: usize) -> Self {
- let failure = PaymentSendFailure::ParameterError(APIError::MonitorUpdateFailed);
- self.fails_with(failure, OnAttempt(attempt))
- }
-
- fn fails_with_partial_failure(self, retry: RouteParameters, attempt: OnAttempt) -> Self {
- self.fails_with(PaymentSendFailure::PartialFailure {
- results: vec![],
- failed_paths_retry: Some(retry),
- payment_id: PaymentId([1; 32]),
- }, attempt)
- }
-
- fn fails_with(self, failure: PaymentSendFailure, attempt: OnAttempt) -> Self {
- self.failing_on_attempt.borrow_mut().insert(attempt.0, failure);
- self
- }
-
- fn check_attempts(&self) -> Result<PaymentId, PaymentSendFailure> {
- let mut attempts = self.attempts.borrow_mut();
- *attempts += 1;
-
- match self.failing_on_attempt.borrow_mut().remove(&*attempts) {
- Some(failure) => Err(failure),
- None => Ok(PaymentId([1; 32])),
- }
- }
-
- fn check_value_msats(&self, actual_value_msats: Amount) {
- let expected_value_msats = self.expectations.borrow_mut().pop_front();
- if let Some(expected_value_msats) = expected_value_msats {
- assert_eq!(actual_value_msats, expected_value_msats);
- } else {
- panic!("Unexpected amount: {:?}", actual_value_msats);
- }
- }
- }
-
- impl Drop for TestPayer {
- fn drop(&mut self) {
- if std::thread::panicking() {
- return;
- }
-
- if !self.expectations.borrow().is_empty() {
- panic!("Unsatisfied payment expectations: {:?}", self.expectations.borrow());
- }
- }
- }
-
- impl Payer for TestPayer {
- fn node_id(&self) -> PublicKey {
- let secp_ctx = Secp256k1::new();
- PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap())
- }
-
- fn first_hops(&self) -> Vec<ChannelDetails> {
- Vec::new()
- }
-
- fn send_payment(
- &self, route: &Route, _payment_hash: PaymentHash,
- _payment_secret: &Option<PaymentSecret>
- ) -> Result<PaymentId, PaymentSendFailure> {
- self.check_value_msats(Amount::ForInvoice(route.get_total_amount()));
- self.check_attempts()
- }
-
- fn send_spontaneous_payment(
- &self, route: &Route, _payment_preimage: PaymentPreimage,
- ) -> Result<PaymentId, PaymentSendFailure> {
- self.check_value_msats(Amount::Spontaneous(route.get_total_amount()));
- self.check_attempts()
- }
-
- fn retry_payment(
- &self, route: &Route, _payment_id: PaymentId
- ) -> Result<(), PaymentSendFailure> {
- self.check_value_msats(Amount::OnRetry(route.get_total_amount()));
- self.check_attempts().map(|_| ())
- }
- }
-
- // *** Full Featured Functional Tests with a Real ChannelManager ***
- struct ManualRouter(RefCell<VecDeque<Result<Route, LightningError>>>);
-
- impl<S: routing::Score> Router<S> for ManualRouter {
- fn find_route(
- &self, _payer: &PublicKey, _params: &RouteParameters, _payment_hash: &PaymentHash,
- _first_hops: Option<&[&ChannelDetails]>, _scorer: &S
- ) -> Result<Route, LightningError> {
- self.0.borrow_mut().pop_front().unwrap()
- }
- }
- impl ManualRouter {
- fn expect_find_route(&self, result: Result<Route, LightningError>) {
- self.0.borrow_mut().push_back(result);
- }
- }
- impl Drop for ManualRouter {
- fn drop(&mut self) {
- if std::thread::panicking() {
- return;
- }
- assert!(self.0.borrow_mut().is_empty());