]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Move logic to check a `ChannelAnnouncement` to `gossip_checking`
authorMatt Corallo <git@bluematt.me>
Sat, 21 Jan 2023 03:48:46 +0000 (03:48 +0000)
committerMatt Corallo <git@bluematt.me>
Wed, 8 Feb 2023 23:54:00 +0000 (23:54 +0000)
This commit is deliberately move-only, though the code being moved
is somewhat crufty.

lightning/src/routing/gossip.rs
lightning/src/routing/utxo.rs

index e3645271b2bcdfa377980c66dfbbfba7ba123162..bfc3018643bf2711d06bdbcb4a711ed03dde147b 100644 (file)
@@ -16,16 +16,14 @@ use bitcoin::secp256k1;
 
 use bitcoin::hashes::sha256d::Hash as Sha256dHash;
 use bitcoin::hashes::Hash;
-use bitcoin::blockdata::transaction::TxOut;
 use bitcoin::hash_types::BlockHash;
 
-use crate::ln::chan_utils::make_funding_redeemscript_from_slices;
 use crate::ln::features::{ChannelFeatures, NodeFeatures, InitFeatures};
 use crate::ln::msgs::{DecodeError, ErrorAction, Init, LightningError, RoutingMessageHandler, NetAddress, MAX_VALUE_MSAT};
 use crate::ln::msgs::{ChannelAnnouncement, ChannelUpdate, NodeAnnouncement, GossipTimestampFilter};
 use crate::ln::msgs::{QueryChannelRange, ReplyChannelRange, QueryShortChannelIds, ReplyShortChannelIdsEnd};
 use crate::ln::msgs;
-use crate::routing::utxo::{UtxoLookup, UtxoLookupError};
+use crate::routing::utxo::{self, UtxoLookup};
 use crate::util::ser::{Readable, ReadableArgs, Writeable, Writer, MaybeReadable};
 use crate::util::logger::{Logger, Level};
 use crate::util::events::{MessageSendEvent, MessageSendEventsProvider};
@@ -42,7 +40,6 @@ use crate::sync::{RwLock, RwLockReadGuard};
 use core::sync::atomic::{AtomicUsize, Ordering};
 use crate::sync::Mutex;
 use core::ops::{Bound, Deref};
-use bitcoin::hashes::hex::ToHex;
 
 #[cfg(feature = "std")]
 use std::time::{SystemTime, UNIX_EPOCH};
@@ -1497,32 +1494,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
                        }
                }
 
-               let utxo_value = match &utxo_lookup {
-                       &None => {
-                               // Tentatively accept, potentially exposing us to DoS attacks
-                               None
-                       },
-                       &Some(ref utxo_lookup) => {
-                               match utxo_lookup.get_utxo(&msg.chain_hash, msg.short_channel_id) {
-                                       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();
-                                               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()), action: ErrorAction::IgnoreError});
-                                               }
-                                               //TODO: Check if value is worth storing, use it to inform routing, and compare it
-                                               //to the new HTLC max field in channel_update
-                                               Some(value)
-                                       },
-                                       Err(UtxoLookupError::UnknownChain) => {
-                                               return Err(LightningError{err: format!("Channel announced on an unknown chain ({})", msg.chain_hash.encode().to_hex()), action: ErrorAction::IgnoreError});
-                                       },
-                                       Err(UtxoLookupError::UnknownTx) => {
-                                               return Err(LightningError{err: "Channel announced without corresponding UTXO entry".to_owned(), action: ErrorAction::IgnoreError});
-                                       },
-                               }
-                       },
-               };
+               let utxo_value = utxo::check_channel_announcement(utxo_lookup, msg)?;
 
                #[allow(unused_mut, unused_assignments)]
                let mut announcement_received_time = 0;
index 1008e6a369eff5141adbcf8eda2a37ebf72e487d..828ea3764fb454f4ed8ef0d50719130ba941cb5c 100644 (file)
 //! order to announce a channel. This module handles that checking.
 
 use bitcoin::{BlockHash, TxOut};
+use bitcoin::hashes::hex::ToHex;
+
+use crate::ln::chan_utils::make_funding_redeemscript_from_slices;
+use crate::ln::msgs::{self, LightningError, ErrorAction};
+use crate::util::ser::Writeable;
+
+use crate::prelude::*;
+
+use core::ops::Deref;
 
 /// An error when accessing the chain via [`UtxoLookup`].
 #[derive(Clone, Debug)]
@@ -34,3 +43,35 @@ pub trait UtxoLookup {
        /// [`short_channel_id`]: https://github.com/lightning/bolts/blob/master/07-routing-gossip.md#definition-of-short_channel_id
        fn get_utxo(&self, genesis_hash: &BlockHash, short_channel_id: u64) -> Result<TxOut, UtxoLookupError>;
 }
+
+pub(crate) fn check_channel_announcement<U: Deref>(
+       utxo_lookup: &Option<U>, msg: &msgs::UnsignedChannelAnnouncement
+) -> Result<Option<u64>, msgs::LightningError> where U::Target: UtxoLookup {
+       let utxo_value = match utxo_lookup {
+               &None => {
+                       // Tentatively accept, potentially exposing us to DoS attacks
+                       None
+               },
+               &Some(ref utxo_lookup) => {
+                       match utxo_lookup.get_utxo(&msg.chain_hash, msg.short_channel_id) {
+                               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();
+                                       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()), action: ErrorAction::IgnoreError});
+                                       }
+                                       //TODO: Check if value is worth storing, use it to inform routing, and compare it
+                                       //to the new HTLC max field in channel_update
+                                       Some(value)
+                               },
+                               Err(UtxoLookupError::UnknownChain) => {
+                                       return Err(LightningError{err: format!("Channel announced on an unknown chain ({})", msg.chain_hash.encode().to_hex()), action: ErrorAction::IgnoreError});
+                               },
+                               Err(UtxoLookupError::UnknownTx) => {
+                                       return Err(LightningError{err: "Channel announced without corresponding UTXO entry".to_owned(), action: ErrorAction::IgnoreError});
+                               },
+                       }
+               }
+       };
+       Ok(utxo_value)
+}