Add UserConfig::manually_handle_bolt12_invoices
[rust-lightning] / lightning / src / routing / utxo.rs
index 164974672380b1272ffe23cc0bbb2d6ea26347c9..b4b8bf5144ee8c148305d1fd29ccc6346526805e 100644 (file)
 //! order to announce a channel. This module handles that checking.
 
 use bitcoin::TxOut;
+use bitcoin::amount::Amount;
 use bitcoin::blockdata::constants::ChainHash;
-use bitcoin::hashes::hex::ToHex;
+
+use hex::DisplayHex;
 
 use crate::events::MessageSendEvent;
 use crate::ln::chan_utils::make_funding_redeemscript_from_slices;
 use crate::ln::msgs::{self, LightningError, ErrorAction};
 use crate::routing::gossip::{NetworkGraph, NodeId, P2PGossipSync};
 use crate::util::logger::{Level, Logger};
-use crate::util::ser::Writeable;
 
 use crate::prelude::*;
 
@@ -307,7 +308,7 @@ pub(super) struct PendingChecks {
 impl PendingChecks {
        pub(super) fn new() -> Self {
                PendingChecks { internal: Mutex::new(PendingChecksContext {
-                       channels: HashMap::new(), nodes: HashMap::new(),
+                       channels: new_hash_map(), nodes: new_hash_map(),
                }) }
        }
 
@@ -455,16 +456,16 @@ impl PendingChecks {
        pub(super) fn check_channel_announcement<U: Deref>(&self,
                utxo_lookup: &Option<U>, msg: &msgs::UnsignedChannelAnnouncement,
                full_msg: Option<&msgs::ChannelAnnouncement>
-       ) -> Result<Option<u64>, msgs::LightningError> where U::Target: UtxoLookup {
+       ) -> Result<Option<Amount>, msgs::LightningError> where U::Target: UtxoLookup {
                let handle_result = |res| {
                        match res {
                                Ok(TxOut { value, script_pubkey }) => {
                                        let expected_script =
-                                               make_funding_redeemscript_from_slices(msg.bitcoin_key_1.as_slice(), msg.bitcoin_key_2.as_slice()).to_v0_p2wsh();
+                                               make_funding_redeemscript_from_slices(msg.bitcoin_key_1.as_array(), msg.bitcoin_key_2.as_array()).to_p2wsh();
                                        if script_pubkey != expected_script {
                                                return Err(LightningError{
                                                        err: format!("Channel announcement key ({}) didn't match on-chain script ({})",
-                                                               expected_script.to_hex(), script_pubkey.to_hex()),
+                                                               expected_script.to_hex_string(), script_pubkey.to_hex_string()),
                                                        action: ErrorAction::IgnoreError
                                                });
                                        }
@@ -473,7 +474,7 @@ impl PendingChecks {
                                Err(UtxoLookupError::UnknownChain) => {
                                        Err(LightningError {
                                                err: format!("Channel announced on an unknown chain ({})",
-                                                       msg.chain_hash.encode().to_hex()),
+                                                       msg.chain_hash.to_bytes().as_hex()),
                                                action: ErrorAction::IgnoreError
                                        })
                                },
@@ -563,8 +564,8 @@ mod tests {
        use super::*;
        use crate::routing::gossip::tests::*;
        use crate::util::test_utils::{TestChainSource, TestLogger};
-       use crate::ln::msgs;
 
+       use bitcoin::amount::Amount;
        use bitcoin::secp256k1::{Secp256k1, SecretKey};
 
        use core::sync::atomic::Ordering;
@@ -578,7 +579,7 @@ mod tests {
        }
 
        fn get_test_objects() -> (msgs::ChannelAnnouncement, TestChainSource,
-               NetworkGraph<Box<TestLogger>>, bitcoin::Script, msgs::NodeAnnouncement,
+               NetworkGraph<Box<TestLogger>>, bitcoin::ScriptBuf, msgs::NodeAnnouncement,
                msgs::NodeAnnouncement, msgs::ChannelUpdate, msgs::ChannelUpdate, msgs::ChannelUpdate)
        {
                let secp_ctx = Secp256k1::new();
@@ -611,7 +612,7 @@ mod tests {
 
                let future = UtxoFuture::new();
                future.resolve_without_forwarding(&network_graph,
-                       Ok(TxOut { value: 1_000_000, script_pubkey: good_script }));
+                       Ok(TxOut { value: Amount::from_sat(1_000_000), script_pubkey: good_script }));
                *chain_source.utxo_ret.lock().unwrap() = UtxoResult::Async(future.clone());
 
                network_graph.update_channel_from_announcement(&valid_announcement, &Some(&chain_source)).unwrap();
@@ -633,7 +634,7 @@ mod tests {
                assert!(network_graph.read_only().channels().get(&valid_announcement.contents.short_channel_id).is_none());
 
                future.resolve_without_forwarding(&network_graph,
-                       Ok(TxOut { value: 0, script_pubkey: good_script }));
+                       Ok(TxOut { value: Amount::ZERO, script_pubkey: good_script }));
                network_graph.read_only().channels().get(&valid_announcement.contents.short_channel_id).unwrap();
                network_graph.read_only().channels().get(&valid_announcement.contents.short_channel_id).unwrap();
 
@@ -661,7 +662,7 @@ mod tests {
                assert!(network_graph.read_only().channels().get(&valid_announcement.contents.short_channel_id).is_none());
 
                future.resolve_without_forwarding(&network_graph,
-                       Ok(TxOut { value: 1_000_000, script_pubkey: bitcoin::Script::new() }));
+                       Ok(TxOut { value: Amount::from_sat(1_000_000), script_pubkey: bitcoin::ScriptBuf::new() }));
                assert!(network_graph.read_only().channels().get(&valid_announcement.contents.short_channel_id).is_none());
        }
 
@@ -710,7 +711,7 @@ mod tests {
                        "Awaiting channel_announcement validation to accept channel_update");
 
                future.resolve_without_forwarding(&network_graph,
-                       Ok(TxOut { value: 1_000_000, script_pubkey: good_script }));
+                       Ok(TxOut { value: Amount::from_sat(1_000_000), script_pubkey: good_script }));
 
                assert!(network_graph.read_only().channels()
                        .get(&valid_announcement.contents.short_channel_id).unwrap().one_to_two.is_some());
@@ -746,7 +747,7 @@ mod tests {
                        "Awaiting channel_announcement validation to accept channel_update");
 
                future.resolve_without_forwarding(&network_graph,
-                       Ok(TxOut { value: 1_000_000, script_pubkey: good_script }));
+                       Ok(TxOut { value: Amount::from_sat(1_000_000), script_pubkey: good_script }));
 
                assert_eq!(chan_update_a.contents.timestamp, chan_update_b.contents.timestamp);
                let graph_lock = network_graph.read_only();
@@ -793,7 +794,7 @@ mod tests {
 
                // Still, if we resolve the original future, the original channel will be accepted.
                future.resolve_without_forwarding(&network_graph,
-                       Ok(TxOut { value: 1_000_000, script_pubkey: good_script }));
+                       Ok(TxOut { value: Amount::from_sat(1_000_000), script_pubkey: good_script }));
                assert!(!network_graph.read_only().channels()
                        .get(&valid_announcement.contents.short_channel_id).unwrap()
                        .announcement_message.as_ref().unwrap()