Remove one tab level when accessing a `peer_state`
[rust-lightning] / lightning / src / ln / outbound_payment.rs
index 68978c117c5d0b062c5011b1e9f1487ced5c8c9e..2f6431665b7ea5ec01bf4a7fe810d0ca9fc1b3c8 100644 (file)
@@ -13,7 +13,7 @@ use bitcoin::hashes::Hash;
 use bitcoin::hashes::sha256::Hash as Sha256;
 use bitcoin::secp256k1::{self, Secp256k1, SecretKey};
 
-use crate::chain::keysinterface::{KeysInterface, Recipient};
+use crate::chain::keysinterface::{EntropySource, KeysInterface, NodeSigner, Recipient};
 use crate::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
 use crate::ln::channelmanager::{HTLCSource, IDEMPOTENCY_TIMEOUT_TICKS, PaymentId};
 use crate::ln::msgs::DecodeError;
@@ -80,14 +80,14 @@ impl PendingOutboundPayment {
                        _ => false,
                }
        }
-       pub(super) fn get_pending_fee_msat(&self) -> Option<u64> {
+       fn get_pending_fee_msat(&self) -> Option<u64> {
                match self {
                        PendingOutboundPayment::Retryable { pending_fee_msat, .. } => pending_fee_msat.clone(),
                        _ => None,
                }
        }
 
-       pub(super) fn payment_hash(&self) -> Option<PaymentHash> {
+       fn payment_hash(&self) -> Option<PaymentHash> {
                match self {
                        PendingOutboundPayment::Legacy { .. } => None,
                        PendingOutboundPayment::Retryable { payment_hash, .. } => Some(*payment_hash),
@@ -96,7 +96,7 @@ impl PendingOutboundPayment {
                }
        }
 
-       pub(super) fn mark_fulfilled(&mut self) {
+       fn mark_fulfilled(&mut self) {
                let mut session_privs = HashSet::new();
                core::mem::swap(&mut session_privs, match self {
                        PendingOutboundPayment::Legacy { session_privs } |
@@ -109,7 +109,7 @@ impl PendingOutboundPayment {
                *self = PendingOutboundPayment::Fulfilled { session_privs, payment_hash, timer_ticks_without_htlcs: 0 };
        }
 
-       pub(super) fn mark_abandoned(&mut self) -> Result<(), ()> {
+       fn mark_abandoned(&mut self) -> Result<(), ()> {
                let mut session_privs = HashSet::new();
                let our_payment_hash;
                core::mem::swap(&mut session_privs, match self {
@@ -127,7 +127,7 @@ impl PendingOutboundPayment {
        }
 
        /// panics if path is None and !self.is_fulfilled
-       pub(super) fn remove(&mut self, session_priv: &[u8; 32], path: Option<&Vec<RouteHop>>) -> bool {
+       fn remove(&mut self, session_priv: &[u8; 32], path: Option<&Vec<RouteHop>>) -> bool {
                let remove_res = match self {
                        PendingOutboundPayment::Legacy { session_privs } |
                                PendingOutboundPayment::Retryable { session_privs, .. } |
@@ -271,10 +271,10 @@ impl OutboundPayments {
                }
        }
 
-       pub(super) fn send_payment<K: Deref, F>(&self, route: &Route, payment_hash: PaymentHash,
-               payment_secret: &Option<PaymentSecret>, payment_id: PaymentId, keys_manager: &K,
-               best_block_height: u32, send_payment_along_path: F)
-       -> Result<(), PaymentSendFailure>
+       pub(super) fn send_payment_with_route<K: Deref, F>(
+               &self, route: &Route, payment_hash: PaymentHash, payment_secret: &Option<PaymentSecret>,
+               payment_id: PaymentId, keys_manager: &K, best_block_height: u32, send_payment_along_path: F
+       -> Result<(), PaymentSendFailure>
        where
                K::Target: KeysInterface,
                F: Fn(&Vec<RouteHop>, &Option<PaymentParameters>, &PaymentHash, &Option<PaymentSecret>, u64,
@@ -284,10 +284,10 @@ impl OutboundPayments {
                self.send_payment_internal(route, payment_hash, payment_secret, None, payment_id, None, onion_session_privs, keys_manager, best_block_height, send_payment_along_path)
        }
 
-       pub(super) fn send_spontaneous_payment<K: Deref, F>(&self, route: &Route,
-               payment_preimage: Option<PaymentPreimage>, payment_id: PaymentId, keys_manager: &K,
-               best_block_height: u32, send_payment_along_path: F)
-       -> Result<PaymentHash, PaymentSendFailure>
+       pub(super) fn send_spontaneous_payment<K: Deref, F>(
+               &self, route: &Route, payment_preimage: Option<PaymentPreimage>, payment_id: PaymentId,
+               keys_manager: &K, best_block_height: u32, send_payment_along_path: F
+       -> Result<PaymentHash, PaymentSendFailure>
        where
                K::Target: KeysInterface,
                F: Fn(&Vec<RouteHop>, &Option<PaymentParameters>, &PaymentHash, &Option<PaymentSecret>, u64,
@@ -306,9 +306,10 @@ impl OutboundPayments {
                }
        }
 
-       pub(super) fn retry_payment<K: Deref, F>(&self, route: &Route, payment_id: PaymentId,
-               keys_manager: &K, best_block_height: u32, send_payment_along_path: F)
-       -> Result<(), PaymentSendFailure>
+       pub(super) fn retry_payment_with_route<K: Deref, F>(
+               &self, route: &Route, payment_id: PaymentId, keys_manager: &K, best_block_height: u32,
+               send_payment_along_path: F
+       ) -> Result<(), PaymentSendFailure>
        where
                K::Target: KeysInterface,
                F: Fn(&Vec<RouteHop>, &Option<PaymentParameters>, &PaymentHash, &Option<PaymentSecret>, u64,
@@ -374,9 +375,10 @@ impl OutboundPayments {
                self.send_payment_internal(route, payment_hash, &payment_secret, None, payment_id, Some(total_msat), onion_session_privs, keys_manager, best_block_height, send_payment_along_path)
        }
 
-       pub(super) fn send_probe<K: Deref, F>(&self, hops: Vec<RouteHop>, probing_cookie_secret: [u8; 32],
-               keys_manager: &K, best_block_height: u32, send_payment_along_path: F)
-       -> Result<(PaymentHash, PaymentId), PaymentSendFailure>
+       pub(super) fn send_probe<K: Deref, F>(
+               &self, hops: Vec<RouteHop>, probing_cookie_secret: [u8; 32], keys_manager: &K,
+               best_block_height: u32, send_payment_along_path: F
+       ) -> Result<(PaymentHash, PaymentId), PaymentSendFailure>
        where
                K::Target: KeysInterface,
                F: Fn(&Vec<RouteHop>, &Option<PaymentParameters>, &PaymentHash, &Option<PaymentSecret>, u64,
@@ -401,10 +403,18 @@ impl OutboundPayments {
                }
        }
 
-       pub(super) fn add_new_pending_payment<K: Deref>(&self, payment_hash: PaymentHash,
-               payment_secret: Option<PaymentSecret>, payment_id: PaymentId, route: &Route, keys_manager: &K,
-               best_block_height: u32)
-       -> Result<Vec<[u8; 32]>, PaymentSendFailure> where K::Target: KeysInterface {
+       #[cfg(test)]
+       pub(super) fn test_add_new_pending_payment<K: Deref>(
+               &self, payment_hash: PaymentHash, payment_secret: Option<PaymentSecret>, payment_id: PaymentId,
+               route: &Route, keys_manager: &K, best_block_height: u32
+       ) -> Result<Vec<[u8; 32]>, PaymentSendFailure> where K::Target: KeysInterface {
+               self.add_new_pending_payment(payment_hash, payment_secret, payment_id, route, keys_manager, best_block_height)
+       }
+
+       fn add_new_pending_payment<K: Deref>(
+               &self, payment_hash: PaymentHash, payment_secret: Option<PaymentSecret>, payment_id: PaymentId,
+               route: &Route, keys_manager: &K, best_block_height: u32
+       ) -> Result<Vec<[u8; 32]>, PaymentSendFailure> where K::Target: KeysInterface {
                let mut onion_session_privs = Vec::with_capacity(route.paths.len());
                for _ in 0..route.paths.len() {
                        onion_session_privs.push(keys_manager.get_secure_random_bytes());
@@ -433,11 +443,12 @@ impl OutboundPayments {
                }
        }
 
-       fn send_payment_internal<K: Deref, F>
-               (&self, route: &Route, payment_hash: PaymentHash, payment_secret: &Option<PaymentSecret>,
+       fn send_payment_internal<K: Deref, F>(
+               &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]>, keys_manager: &K, best_block_height: u32,
-               send_payment_along_path: F) -> Result<(), PaymentSendFailure>
+               send_payment_along_path: F
+       ) -> Result<(), PaymentSendFailure>
        where
                K::Target: KeysInterface,
                F: Fn(&Vec<RouteHop>, &Option<PaymentParameters>, &PaymentHash, &Option<PaymentSecret>, u64,
@@ -542,11 +553,12 @@ impl OutboundPayments {
        }
 
        #[cfg(test)]
-       pub(super) fn test_send_payment_internal<K: Deref, F>
-               (&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]>, keys_manager: &K, best_block_height: u32,
-                send_payment_along_path: F) -> Result<(), PaymentSendFailure>
+       pub(super) fn test_send_payment_internal<K: Deref, F>(
+               &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]>, keys_manager: &K, best_block_height: u32,
+               send_payment_along_path: F
+       ) -> Result<(), PaymentSendFailure>
        where
                K::Target: KeysInterface,
                F: Fn(&Vec<RouteHop>, &Option<PaymentParameters>, &PaymentHash, &Option<PaymentSecret>, u64,
@@ -557,10 +569,10 @@ impl OutboundPayments {
                        send_payment_along_path)
        }
 
-       pub(super) fn claim_htlc<L: Deref>(&self, payment_id: PaymentId,
-               payment_preimage: PaymentPreimage, session_priv: SecretKey, path: Vec<RouteHop>,
-               from_onchain: bool, pending_events: &Mutex<Vec<events::Event>>, logger: &L)
-        where L::Target: Logger {
+       pub(super) fn claim_htlc<L: Deref>(
+               &self, payment_id: PaymentId, payment_preimage: PaymentPreimage, session_priv: SecretKey,
+               path: Vec<RouteHop>, from_onchain: bool, pending_events: &Mutex<Vec<events::Event>>, logger: &L
+       ) where L::Target: Logger {
                let mut session_priv_bytes = [0; 32];
                session_priv_bytes.copy_from_slice(&session_priv[..]);
                let mut outbounds = self.pending_outbound_payments.lock().unwrap();
@@ -603,10 +615,10 @@ impl OutboundPayments {
                }
        }
 
-       pub(super) fn finalize_claims(&self, mut sources: Vec<HTLCSource>, pending_events: &Mutex<Vec<events::Event>>) {
+       pub(super) fn finalize_claims(&self, sources: Vec<HTLCSource>, pending_events: &Mutex<Vec<events::Event>>) {
                let mut outbounds = self.pending_outbound_payments.lock().unwrap();
                let mut pending_events = pending_events.lock().unwrap();
-               for source in sources.drain(..) {
+               for source in sources {
                        if let HTLCSource::OutboundRoute { session_priv, payment_id, path, .. } = source {
                                let mut session_priv_bytes = [0; 32];
                                session_priv_bytes.copy_from_slice(&session_priv[..]);
@@ -665,12 +677,12 @@ impl OutboundPayments {
                });
        }
 
-       pub(super) fn fail_htlc<L: Deref>(&self, source: &HTLCSource, payment_hash: &PaymentHash,
-               onion_error: &HTLCFailReason, path: &Vec<RouteHop>, session_priv: &SecretKey,
-               payment_id: &PaymentId, payment_params: &Option<PaymentParameters>,
-               probing_cookie_secret: [u8; 32], secp_ctx: &Secp256k1<secp256k1::All>,
-               pending_events: &Mutex<Vec<events::Event>>, logger: &L)
-       where L::Target: Logger {
+       pub(super) fn fail_htlc<L: Deref>(
+               &self, source: &HTLCSource, payment_hash: &PaymentHash, onion_error: &HTLCFailReason,
+               path: &Vec<RouteHop>, session_priv: &SecretKey, payment_id: &PaymentId,
+               payment_params: &Option<PaymentParameters>, probing_cookie_secret: [u8; 32],
+               secp_ctx: &Secp256k1<secp256k1::All>, pending_events: &Mutex<Vec<events::Event>>, logger: &L
+       where L::Target: Logger {
                let mut session_priv_bytes = [0; 32];
                session_priv_bytes.copy_from_slice(&session_priv[..]);
                let mut outbounds = self.pending_outbound_payments.lock().unwrap();
@@ -710,9 +722,9 @@ impl OutboundPayments {
                log_trace!(logger, "Failing outbound payment HTLC with payment_hash {}", log_bytes!(payment_hash.0));
 
                let path_failure = {
-       #[cfg(test)]
+                       #[cfg(test)]
                        let (network_update, short_channel_id, payment_retryable, onion_error_code, onion_error_data) = onion_error.decode_onion_failure(secp_ctx, logger, &source);
-       #[cfg(not(test))]
+                       #[cfg(not(test))]
                        let (network_update, short_channel_id, payment_retryable, _, _) = onion_error.decode_onion_failure(secp_ctx, logger, &source);
 
                        if payment_is_probe(payment_hash, &payment_id, probing_cookie_secret) {