- fn send_payment_internal(&self, route: &Route, payment_hash: PaymentHash, payment_secret: &Option<PaymentSecret>, keysend_preimage: Option<PaymentPreimage>, payment_id: PaymentId, recv_value_msat: Option<u64>, onion_session_privs: Vec<[u8; 32]>) -> Result<(), PaymentSendFailure> {
- if route.paths.len() < 1 {
- return Err(PaymentSendFailure::ParameterError(APIError::InvalidRoute{err: "There must be at least one path to send over"}));
- }
- if payment_secret.is_none() && route.paths.len() > 1 {
- return Err(PaymentSendFailure::ParameterError(APIError::APIMisuseError{err: "Payment secret is required for multi-path payments".to_string()}));
- }
- let mut total_value = 0;
- let our_node_id = self.get_our_node_id();
- let mut path_errs = Vec::with_capacity(route.paths.len());
- 'path_check: for path in route.paths.iter() {
- if path.len() < 1 || path.len() > 20 {
- path_errs.push(Err(APIError::InvalidRoute{err: "Path didn't go anywhere/had bogus size"}));
- continue 'path_check;
- }
- for (idx, hop) in path.iter().enumerate() {
- if idx != path.len() - 1 && hop.pubkey == our_node_id {
- path_errs.push(Err(APIError::InvalidRoute{err: "Path went through us but wasn't a simple rebalance loop to us"}));
- continue 'path_check;
- }
- }
- total_value += path.last().unwrap().fee_msat;
- path_errs.push(Ok(()));
- }
- if path_errs.iter().any(|e| e.is_err()) {
- return Err(PaymentSendFailure::PathParameterError(path_errs));
- }
- if let Some(amt_msat) = recv_value_msat {
- debug_assert!(amt_msat >= total_value);
- total_value = amt_msat;
- }
-
- let cur_height = self.best_block.read().unwrap().height() + 1;
- let mut results = Vec::new();
- debug_assert_eq!(route.paths.len(), onion_session_privs.len());
- for (path, session_priv) in route.paths.iter().zip(onion_session_privs.into_iter()) {
- let mut path_res = self.send_payment_along_path(&path, &route.payment_params, &payment_hash, payment_secret, total_value, cur_height, payment_id, &keysend_preimage, session_priv);
- match path_res {
- Ok(_) => {},
- Err(APIError::MonitorUpdateInProgress) => {
- // While a MonitorUpdateInProgress is an Err(_), the payment is still
- // considered "in flight" and we shouldn't remove it from the
- // PendingOutboundPayment set.
- },
- Err(_) => {
- let mut pending_outbounds = self.pending_outbound_payments.lock().unwrap();
- if let Some(payment) = pending_outbounds.get_mut(&payment_id) {
- let removed = payment.remove(&session_priv, Some(path));
- debug_assert!(removed, "This can't happen as the payment has an entry for this path added by callers");
- } else {
- debug_assert!(false, "This can't happen as the payment was added by callers");
- path_res = Err(APIError::APIMisuseError { err: "Internal error: payment disappeared during processing. Please report this bug!".to_owned() });
- }
- }
- }
- results.push(path_res);
- }
- let mut has_ok = false;
- let mut has_err = false;
- let mut pending_amt_unsent = 0;
- let mut max_unsent_cltv_delta = 0;
- for (res, path) in results.iter().zip(route.paths.iter()) {
- if res.is_ok() { has_ok = true; }
- if res.is_err() { has_err = true; }
- if let &Err(APIError::MonitorUpdateInProgress) = res {
- // MonitorUpdateInProgress is inherently unsafe to retry, so we call it a
- // PartialFailure.
- has_err = true;
- has_ok = true;
- } else if res.is_err() {
- pending_amt_unsent += path.last().unwrap().fee_msat;
- max_unsent_cltv_delta = cmp::max(max_unsent_cltv_delta, path.last().unwrap().cltv_expiry_delta);
- }
- }
- if has_err && has_ok {
- Err(PaymentSendFailure::PartialFailure {
- results,
- payment_id,
- failed_paths_retry: if pending_amt_unsent != 0 {
- if let Some(payment_params) = &route.payment_params {
- Some(RouteParameters {
- payment_params: payment_params.clone(),
- final_value_msat: pending_amt_unsent,
- final_cltv_expiry_delta: max_unsent_cltv_delta,
- })
- } else { None }
- } else { None },
- })
- } else if has_err {
- // If we failed to send any paths, we should remove the new PaymentId from the
- // `pending_outbound_payments` map, as the user isn't expected to `abandon_payment`.
- let removed = self.pending_outbound_payments.lock().unwrap().remove(&payment_id).is_some();
- debug_assert!(removed, "We should always have a pending payment to remove here");
- Err(PaymentSendFailure::AllFailedResendSafe(results.drain(..).map(|r| r.unwrap_err()).collect()))
- } else {
- Ok(())
- }
- }