X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Frouting%2Fgossip.rs;h=7ef49b875be4d02983976bd8aab552bda847e46e;hb=c1eda4ba3d9189ea3831c705505c3e3f2edcdbbe;hp=49a76328659faa379b88d904d07db2a6ba3848be;hpb=db489baee24cf53647d1a78360b2e643cc8d2542;p=rust-lightning diff --git a/lightning/src/routing/gossip.rs b/lightning/src/routing/gossip.rs index 49a76328..7ef49b87 100644 --- a/lightning/src/routing/gossip.rs +++ b/lightning/src/routing/gossip.rs @@ -9,41 +9,42 @@ //! The [`NetworkGraph`] stores the network gossip and [`P2PGossipSync`] fetches it from peers +use bitcoin::amount::Amount; use bitcoin::blockdata::constants::ChainHash; +use bitcoin::secp256k1; use bitcoin::secp256k1::constants::PUBLIC_KEY_SIZE; -use bitcoin::secp256k1::{PublicKey, Verification}; use bitcoin::secp256k1::Secp256k1; -use bitcoin::secp256k1; +use bitcoin::secp256k1::{PublicKey, Verification}; use bitcoin::hashes::sha256d::Hash as Sha256dHash; use bitcoin::hashes::Hash; -use bitcoin::network::constants::Network; +use bitcoin::network::Network; use crate::events::{MessageSendEvent, MessageSendEventsProvider}; -use crate::ln::types::ChannelId; -use crate::ln::features::{ChannelFeatures, NodeFeatures, InitFeatures}; -use crate::ln::msgs::{DecodeError, ErrorAction, Init, LightningError, RoutingMessageHandler, SocketAddress, MAX_VALUE_MSAT}; -use crate::ln::msgs::{ChannelAnnouncement, ChannelUpdate, NodeAnnouncement, GossipTimestampFilter}; -use crate::ln::msgs::{QueryChannelRange, ReplyChannelRange, QueryShortChannelIds, ReplyShortChannelIdsEnd}; +use crate::ln::features::{ChannelFeatures, InitFeatures, NodeFeatures}; use crate::ln::msgs; +use crate::ln::msgs::{ChannelAnnouncement, ChannelUpdate, GossipTimestampFilter, NodeAnnouncement}; +use crate::ln::msgs::{DecodeError, ErrorAction, Init, LightningError, RoutingMessageHandler, SocketAddress, MAX_VALUE_MSAT}; +use crate::ln::msgs::{QueryChannelRange, QueryShortChannelIds, ReplyChannelRange, ReplyShortChannelIdsEnd}; +use crate::ln::types::ChannelId; use crate::routing::utxo::{self, UtxoLookup, UtxoResolver}; -use crate::util::ser::{Readable, ReadableArgs, Writeable, Writer, MaybeReadable}; -use crate::util::logger::{Logger, Level}; +use crate::util::indexed_map::{Entry as IndexedMapEntry, IndexedMap}; +use crate::util::logger::{Level, Logger}; use crate::util::scid_utils::{block_from_scid, scid_from_parts, MAX_SCID_BLOCK}; +use crate::util::ser::{MaybeReadable, Readable, ReadableArgs, RequiredWrapper, Writeable, Writer}; use crate::util::string::PrintableString; -use crate::util::indexed_map::{IndexedMap, Entry as IndexedMapEntry}; use crate::io; use crate::io_extras::{copy, sink}; use crate::prelude::*; -use core::{cmp, fmt}; -use crate::sync::{RwLock, RwLockReadGuard, LockTestExt}; -#[cfg(feature = "std")] -use core::sync::atomic::{AtomicUsize, Ordering}; use crate::sync::Mutex; +use crate::sync::{LockTestExt, RwLock, RwLockReadGuard}; use core::ops::{Bound, Deref}; use core::str::FromStr; +#[cfg(feature = "std")] +use core::sync::atomic::{AtomicUsize, Ordering}; +use core::{cmp, fmt}; #[cfg(feature = "std")] use std::time::{SystemTime, UNIX_EPOCH}; @@ -218,12 +219,6 @@ pub struct ReadOnlyNetworkGraph<'a> { /// [BOLT #4]: https://github.com/lightning/bolts/blob/master/04-onion-routing.md #[derive(Clone, Debug, PartialEq, Eq)] pub enum NetworkUpdate { - /// An error indicating a `channel_update` messages should be applied via - /// [`NetworkGraph::update_channel`]. - ChannelUpdateMessage { - /// The update to apply via [`NetworkGraph::update_channel`]. - msg: ChannelUpdate, - }, /// An error indicating that a channel failed to route a payment, which should be applied via /// [`NetworkGraph::channel_failed_permanent`] if permanent. ChannelFailure { @@ -244,19 +239,69 @@ pub enum NetworkUpdate { } } -impl_writeable_tlv_based_enum_upgradable!(NetworkUpdate, - (0, ChannelUpdateMessage) => { - (0, msg, required), - }, - (2, ChannelFailure) => { - (0, short_channel_id, required), - (2, is_permanent, required), - }, - (4, NodeFailure) => { - (0, node_id, required), - (2, is_permanent, required), - }, -); +impl Writeable for NetworkUpdate { + fn write(&self, writer: &mut W) -> Result<(), io::Error> { + match self { + Self::ChannelFailure { short_channel_id, is_permanent } => { + 2u8.write(writer)?; + write_tlv_fields!(writer, { + (0, short_channel_id, required), + (2, is_permanent, required), + }); + }, + Self::NodeFailure { node_id, is_permanent } => { + 4u8.write(writer)?; + write_tlv_fields!(writer, { + (0, node_id, required), + (2, is_permanent, required), + }); + } + } + Ok(()) + } +} + +impl MaybeReadable for NetworkUpdate { + fn read(reader: &mut R) -> Result, DecodeError> { + let id: u8 = Readable::read(reader)?; + match id { + 0 => { + // 0 was previously used for network updates containing a channel update, subsequently + // removed in LDK version 0.0.124. + let mut msg: RequiredWrapper = RequiredWrapper(None); + read_tlv_fields!(reader, { + (0, msg, required), + }); + Ok(Some(Self::ChannelFailure { + short_channel_id: msg.0.unwrap().contents.short_channel_id, + is_permanent: false + })) + }, + 2 => { + _init_and_read_len_prefixed_tlv_fields!(reader, { + (0, short_channel_id, required), + (2, is_permanent, required), + }); + Ok(Some(Self::ChannelFailure { + short_channel_id: short_channel_id.0.unwrap(), + is_permanent: is_permanent.0.unwrap(), + })) + }, + 4 => { + _init_and_read_len_prefixed_tlv_fields!(reader, { + (0, node_id, required), + (2, is_permanent, required), + }); + Ok(Some(Self::NodeFailure { + node_id: node_id.0.unwrap(), + is_permanent: is_permanent.0.unwrap(), + })) + } + t if t % 2 == 0 => Err(DecodeError::UnknownRequiredFeature), + _ => Ok(None), + } + } +} /// Receives and validates network updates from peers, /// stores authentic and relevant data as a network graph. @@ -353,19 +398,10 @@ where U::Target: UtxoLookup, L::Target: Logger impl NetworkGraph where L::Target: Logger { /// Handles any network updates originating from [`Event`]s. - // - /// Note that this will skip applying any [`NetworkUpdate::ChannelUpdateMessage`] to avoid - /// leaking possibly identifying information of the sender to the public network. /// /// [`Event`]: crate::events::Event pub fn handle_network_update(&self, network_update: &NetworkUpdate) { match *network_update { - NetworkUpdate::ChannelUpdateMessage { ref msg } => { - let short_channel_id = msg.contents.short_channel_id; - let is_enabled = msg.contents.flags & (1 << 1) != (1 << 1); - let status = if is_enabled { "enabled" } else { "disabled" }; - log_debug!(self.logger, "Skipping application of a channel update from a payment failure. Channel {} is {}.", short_channel_id, status); - }, NetworkUpdate::ChannelFailure { short_channel_id, is_permanent } => { if is_permanent { log_debug!(self.logger, "Removing channel graph entry for {} due to a payment failure.", short_channel_id); @@ -879,6 +915,7 @@ impl ChannelInfo { /// Returns a [`DirectedChannelInfo`] for the channel directed to the given `target` from a /// returned `source`, or `None` if `target` is not one of the channel's counterparties. pub fn as_directed_to(&self, target: &NodeId) -> Option<(DirectedChannelInfo, &NodeId)> { + if self.one_to_two.is_none() || self.two_to_one.is_none() { return None; } let (direction, source, outbound) = { if target == &self.node_one { (self.two_to_one.as_ref(), &self.node_two, false) @@ -888,12 +925,14 @@ impl ChannelInfo { return None; } }; - direction.map(|dir| (DirectedChannelInfo::new(self, dir, outbound), source)) + let dir = direction.expect("We checked that both directions are available at the start"); + Some((DirectedChannelInfo::new(self, dir, outbound), source)) } /// Returns a [`DirectedChannelInfo`] for the channel directed from the given `source` to a /// returned `target`, or `None` if `source` is not one of the channel's counterparties. pub fn as_directed_from(&self, source: &NodeId) -> Option<(DirectedChannelInfo, &NodeId)> { + if self.one_to_two.is_none() || self.two_to_one.is_none() { return None; } let (direction, target, outbound) = { if source == &self.node_one { (self.one_to_two.as_ref(), &self.node_two, true) @@ -903,7 +942,8 @@ impl ChannelInfo { return None; } }; - direction.map(|dir| (DirectedChannelInfo::new(self, dir, outbound), target)) + let dir = direction.expect("We checked that both directions are available at the start"); + Some((DirectedChannelInfo::new(self, dir, outbound), target)) } /// Returns a [`ChannelUpdateInfo`] based on the direction implied by the channel_flag. @@ -1689,7 +1729,7 @@ impl NetworkGraph where L::Target: Logger { self.add_channel_between_nodes(short_channel_id, channel_info, None) } - fn add_channel_between_nodes(&self, short_channel_id: u64, channel_info: ChannelInfo, utxo_value: Option) -> Result<(), LightningError> { + fn add_channel_between_nodes(&self, short_channel_id: u64, channel_info: ChannelInfo, utxo_value: Option) -> Result<(), LightningError> { let mut channels = self.channels.write().unwrap(); let mut nodes = self.nodes.write().unwrap(); @@ -1818,7 +1858,7 @@ impl NetworkGraph where L::Target: Logger { one_to_two: None, node_two: msg.node_id_2, two_to_one: None, - capacity_sats: utxo_value, + capacity_sats: utxo_value.map(|a| a.to_sat()), announcement_message: if msg.excess_data.len() <= MAX_EXCESS_BYTES_FOR_RELAY { full_msg.cloned() } else { None }, announcement_received_time, @@ -2231,7 +2271,8 @@ pub(crate) mod tests { use bitcoin::hashes::sha256d::Hash as Sha256dHash; use bitcoin::hashes::Hash; use bitcoin::hashes::hex::FromHex; - use bitcoin::network::constants::Network; + use bitcoin::network::Network; + use bitcoin::amount::Amount; use bitcoin::blockdata::constants::ChainHash; use bitcoin::blockdata::script::ScriptBuf; use bitcoin::blockdata::transaction::TxOut; @@ -2324,7 +2365,7 @@ pub(crate) mod tests { let node_1_btckey = SecretKey::from_slice(&[40; 32]).unwrap(); let node_2_btckey = SecretKey::from_slice(&[39; 32]).unwrap(); make_funding_redeemscript(&PublicKey::from_secret_key(secp_ctx, &node_1_btckey), - &PublicKey::from_secret_key(secp_ctx, &node_2_btckey)).to_v0_p2wsh() + &PublicKey::from_secret_key(secp_ctx, &node_2_btckey)).to_p2wsh() } pub(crate) fn get_signed_channel_update(f: F, node_key: &SecretKey, secp_ctx: &Secp256k1) -> ChannelUpdate { @@ -2457,7 +2498,7 @@ pub(crate) mod tests { // Now test if the transaction is found in the UTXO set and the script is correct. *chain_source.utxo_ret.lock().unwrap() = - UtxoResult::Sync(Ok(TxOut { value: 0, script_pubkey: good_script.clone() })); + UtxoResult::Sync(Ok(TxOut { value: Amount::ZERO, script_pubkey: good_script.clone() })); let valid_announcement = get_signed_channel_announcement(|unsigned_announcement| { unsigned_announcement.short_channel_id += 2; }, node_1_privkey, node_2_privkey, &secp_ctx); @@ -2476,7 +2517,7 @@ pub(crate) mod tests { // If we receive announcement for the same channel, once we've validated it against the // chain, we simply ignore all new (duplicate) announcements. *chain_source.utxo_ret.lock().unwrap() = - UtxoResult::Sync(Ok(TxOut { value: 0, script_pubkey: good_script })); + UtxoResult::Sync(Ok(TxOut { value: Amount::ZERO, script_pubkey: good_script })); match gossip_sync.handle_channel_announcement(&valid_announcement) { Ok(_) => panic!(), Err(e) => assert_eq!(e.err, "Already have chain-validated channel") @@ -2553,7 +2594,7 @@ pub(crate) mod tests { let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap(); let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap(); - let amount_sats = 1000_000; + let amount_sats = Amount::from_sat(1000_000); let short_channel_id; { @@ -2617,7 +2658,7 @@ pub(crate) mod tests { }; let valid_channel_update = get_signed_channel_update(|unsigned_channel_update| { - unsigned_channel_update.htlc_maximum_msat = amount_sats * 1000 + 1; + unsigned_channel_update.htlc_maximum_msat = amount_sats.to_sat() * 1000 + 1; unsigned_channel_update.timestamp += 110; }, node_1_privkey, &secp_ctx); match gossip_sync.handle_channel_update(&valid_channel_update) { @@ -2675,8 +2716,7 @@ pub(crate) mod tests { let short_channel_id; { - // Check we won't apply an update via `handle_network_update` for privacy reasons, but - // can continue fine if we manually apply it. + // Check that we can manually apply a channel update. let valid_channel_announcement = get_signed_channel_announcement(|_| {}, node_1_privkey, node_2_privkey, &secp_ctx); short_channel_id = valid_channel_announcement.contents.short_channel_id; let chain_source: Option<&test_utils::TestChainSource> = None; @@ -2684,14 +2724,10 @@ pub(crate) mod tests { assert!(network_graph.read_only().channels().get(&short_channel_id).is_some()); let valid_channel_update = get_signed_channel_update(|_| {}, node_1_privkey, &secp_ctx); - assert!(network_graph.read_only().channels().get(&short_channel_id).unwrap().one_to_two.is_none()); - - network_graph.handle_network_update(&NetworkUpdate::ChannelUpdateMessage { - msg: valid_channel_update.clone(), - }); assert!(network_graph.read_only().channels().get(&short_channel_id).unwrap().one_to_two.is_none()); network_graph.update_channel(&valid_channel_update).unwrap(); + assert!(network_graph.read_only().channels().get(&short_channel_id).unwrap().one_to_two.is_some()); } // Non-permanent failure doesn't touch the channel at all @@ -3710,7 +3746,7 @@ pub mod benches { pub fn read_network_graph(bench: &mut Criterion) { let logger = crate::util::test_utils::TestLogger::new(); - let mut d = crate::routing::router::bench_utils::get_route_file().unwrap(); + let (mut d, _) = crate::routing::router::bench_utils::get_graph_scorer_file().unwrap(); let mut v = Vec::new(); d.read_to_end(&mut v).unwrap(); bench.bench_function("read_network_graph", |b| b.iter(|| @@ -3720,7 +3756,7 @@ pub mod benches { pub fn write_network_graph(bench: &mut Criterion) { let logger = crate::util::test_utils::TestLogger::new(); - let mut d = crate::routing::router::bench_utils::get_route_file().unwrap(); + let (mut d, _) = crate::routing::router::bench_utils::get_graph_scorer_file().unwrap(); let net_graph = NetworkGraph::read(&mut d, &logger).unwrap(); bench.bench_function("write_network_graph", |b| b.iter(|| black_box(&net_graph).encode()