X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fln%2Fchannelmanager.rs;h=09798eb9bbab22b47c752605e97c71ce7f4e75bd;hb=9c587e53129eb1712735b9b5469aa9635e35c393;hp=939b61e4d178ff9bde50b080203acec091c16636;hpb=ad5f72894c9fa501e3d71fee7f12f8cfdc9dd55e;p=rust-lightning diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index 939b61e4..09798eb9 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -4,7 +4,7 @@ //! responsible for tracking which channels are open, HTLCs are in flight and reestablishing those //! upon reconnect to the relevant peer(s). //! -//! It does not manage routing logic (see ln::router for that) nor does it manage constructing +//! It does not manage routing logic (see routing::router::get_route for that) nor does it manage constructing //! on-chain transactions (it only monitors the chain to watch for any force-closes that might //! imply it needs to fail HTLCs/payments/channels it manages). @@ -14,30 +14,31 @@ use bitcoin::blockdata::constants::genesis_block; use bitcoin::network::constants::Network; use bitcoin::util::hash::BitcoinHash; -use bitcoin_hashes::{Hash, HashEngine}; -use bitcoin_hashes::hmac::{Hmac, HmacEngine}; -use bitcoin_hashes::sha256::Hash as Sha256; -use bitcoin_hashes::sha256d::Hash as Sha256dHash; -use bitcoin_hashes::cmp::fixed_time_eq; +use bitcoin::hashes::{Hash, HashEngine}; +use bitcoin::hashes::hmac::{Hmac, HmacEngine}; +use bitcoin::hashes::sha256::Hash as Sha256; +use bitcoin::hashes::sha256d::Hash as Sha256dHash; +use bitcoin::hashes::cmp::fixed_time_eq; +use bitcoin::hash_types::BlockHash; -use secp256k1::key::{SecretKey,PublicKey}; -use secp256k1::Secp256k1; -use secp256k1::ecdh::SharedSecret; -use secp256k1; +use bitcoin::secp256k1::key::{SecretKey,PublicKey}; +use bitcoin::secp256k1::Secp256k1; +use bitcoin::secp256k1::ecdh::SharedSecret; +use bitcoin::secp256k1; use chain::chaininterface::{BroadcasterInterface,ChainListener,FeeEstimator}; use chain::transaction::OutPoint; use ln::channel::{Channel, ChannelError}; use ln::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateErr, ManyChannelMonitor, HTLC_FAIL_BACK_BUFFER, CLTV_CLAIM_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS, ANTI_REORG_DELAY}; use ln::features::{InitFeatures, NodeFeatures}; -use ln::router::{Route, RouteHop}; +use routing::router::{Route, RouteHop}; use ln::msgs; use ln::onion_utils; use ln::msgs::{ChannelMessageHandler, DecodeError, LightningError}; use chain::keysinterface::{ChannelKeys, KeysInterface, KeysManager, InMemoryChannelKeys}; use util::config::UserConfig; use util::{byte_utils, events}; -use util::ser::{Readable, ReadableArgs, Writeable, Writer}; +use util::ser::{Readable, ReadableArgs, MaybeReadable, Writeable, Writer}; use util::chacha20::{ChaCha20, ChaChaReader}; use util::logger::Logger; use util::errors::APIError; @@ -251,7 +252,7 @@ impl MsgHandleErrInternal { }, }, }, - ChannelError::CloseDelayBroadcast { msg, .. } => LightningError { + ChannelError::CloseDelayBroadcast(msg) => LightningError { err: msg, action: msgs::ErrorAction::SendErrorMessage { msg: msgs::ErrorMessage { @@ -375,7 +376,7 @@ pub struct ChannelManager, + last_block_hash: Mutex, secp_ctx: Secp256k1, #[cfg(test)] @@ -574,8 +575,9 @@ macro_rules! break_chan_entry { if let Some(short_id) = chan.get_short_channel_id() { $channel_state.short_to_id.remove(&short_id); } - break Err(MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, chan.force_shutdown(true), $self.get_channel_update(&chan).ok())) }, - Err(ChannelError::CloseDelayBroadcast { .. }) => { panic!("Wait is only generated on receipt of channel_reestablish, which is handled by try_chan_entry, we don't bother to support it here"); } + break Err(MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, chan.force_shutdown(true), $self.get_channel_update(&chan).ok())) + }, + Err(ChannelError::CloseDelayBroadcast(_)) => { panic!("Wait is only generated on receipt of channel_reestablish, which is handled by try_chan_entry, we don't bother to support it here"); } } } } @@ -595,22 +597,12 @@ macro_rules! try_chan_entry { } return Err(MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, chan.force_shutdown(true), $self.get_channel_update(&chan).ok())) }, - Err(ChannelError::CloseDelayBroadcast { msg, update }) => { + Err(ChannelError::CloseDelayBroadcast(msg)) => { log_error!($self, "Channel {} need to be shutdown but closing transactions not broadcast due to {}", log_bytes!($entry.key()[..]), msg); let (channel_id, mut chan) = $entry.remove_entry(); if let Some(short_id) = chan.get_short_channel_id() { $channel_state.short_to_id.remove(&short_id); } - if let Err(e) = $self.monitor.update_monitor(chan.get_funding_txo().unwrap(), update) { - match e { - // Upstream channel is dead, but we want at least to fail backward HTLCs to save - // downstream channels. In case of PermanentFailure, we are not going to be able - // to claim back to_remote output on remote commitment transaction. Doesn't - // make a difference here, we are concern about HTLCs circuit, not onchain funds. - ChannelMonitorUpdateErr::PermanentFailure => {}, - ChannelMonitorUpdateErr::TemporaryFailure => {}, - } - } let shutdown_res = chan.force_shutdown(false); return Err(MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, shutdown_res, $self.get_channel_update(&chan).ok())) } @@ -827,7 +819,7 @@ impl ChannelMan } /// Gets the list of usable channels, in random order. Useful as an argument to - /// Router::get_route to ensure non-announced channels are used. + /// get_route to ensure non-announced channels are used. /// /// These are guaranteed to have their is_live value set to true, see the documentation for /// ChannelDetails::is_live for more info on exactly what the criteria are. @@ -1175,6 +1167,12 @@ impl ChannelMan if msg.cltv_expiry > cur_height + CLTV_FAR_FAR_AWAY as u32 { // expiry_too_far break Some(("CLTV expiry is too far in the future", 21, None)); } + // In theory, we would be safe against unitentional channel-closure, if we only required a margin of LATENCY_GRACE_PERIOD_BLOCKS. + // But, to be safe against policy reception, we use a longuer delay. + if (*outgoing_cltv_value) as u64 <= (cur_height + HTLC_FAIL_BACK_BUFFER) as u64 { + break Some(("Outgoing CLTV value is too soon", 0x1000 | 14, Some(self.get_channel_update(chan).unwrap()))); + } + break None; } { @@ -1501,7 +1499,7 @@ impl ChannelMan } let announcement = msgs::UnsignedNodeAnnouncement { - features: NodeFeatures::supported(), + features: NodeFeatures::known(), timestamp: self.last_node_announcement_serial.fetch_add(1, Ordering::AcqRel) as u32, node_id: self.get_our_node_id(), rgb, alias, addresses, @@ -1658,7 +1656,7 @@ impl ChannelMan } Err(MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, channel.force_shutdown(true), self.get_channel_update(&channel).ok())) }, - ChannelError::CloseDelayBroadcast { .. } => { panic!("Wait is only generated on receipt of channel_reestablish, which is handled by try_chan_entry, we don't bother to support it here"); } + ChannelError::CloseDelayBroadcast(_) => { panic!("Wait is only generated on receipt of channel_reestablish, which is handled by try_chan_entry, we don't bother to support it here"); } }; handle_errors.push((their_node_id, err)); continue; @@ -1871,7 +1869,7 @@ impl ChannelMan .. } => { // we get a fail_malformed_htlc from the first hop // TODO: We'd like to generate a PaymentFailureNetworkUpdate for temporary - // failures here, but that would be insufficient as Router::get_route + // failures here, but that would be insufficient as get_route // generally ignores its view of our own channels as we provide them via // ChannelDetails. // TODO: For non-temporary failures, we really should be closing the @@ -3610,6 +3608,12 @@ impl - ReadableArgs> for (Sha256dHash, Arc>) + ReadableArgs> for (BlockHash, Arc>) where M::Target: ManyChannelMonitor, T::Target: BroadcasterInterface, K::Target: KeysInterface, F::Target: FeeEstimator, { fn read(reader: &mut R, args: ChannelManagerReadArgs<'a, ChanSigner, M, T, K, F>) -> Result { - let (blockhash, chan_manager) = <(Sha256dHash, ChannelManager)>::read(reader, args)?; + let (blockhash, chan_manager) = <(BlockHash, ChannelManager)>::read(reader, args)?; Ok((blockhash, Arc::new(chan_manager))) } } impl<'a, ChanSigner: ChannelKeys + Readable, M: Deref, T: Deref, K: Deref, F: Deref> - ReadableArgs> for (Sha256dHash, ChannelManager) + ReadableArgs> for (BlockHash, ChannelManager) where M::Target: ManyChannelMonitor, T::Target: BroadcasterInterface, K::Target: KeysInterface, @@ -3706,9 +3710,9 @@ impl<'a, ChanSigner: ChannelKeys + Readable, M: Deref, T: Deref, K: Deref, F: De return Err(DecodeError::UnknownVersion); } - let genesis_hash: Sha256dHash = Readable::read(reader)?; + let genesis_hash: BlockHash = Readable::read(reader)?; let latest_block_height: u32 = Readable::read(reader)?; - let last_block_hash: Sha256dHash = Readable::read(reader)?; + let last_block_hash: BlockHash = Readable::read(reader)?; let mut failed_htlcs = Vec::new(); @@ -3756,12 +3760,13 @@ impl<'a, ChanSigner: ChannelKeys + Readable, M: Deref, T: Deref, K: Deref, F: De } } + const MAX_ALLOC_SIZE: usize = 1024 * 64; let forward_htlcs_count: u64 = Readable::read(reader)?; let mut forward_htlcs = HashMap::with_capacity(cmp::min(forward_htlcs_count as usize, 128)); for _ in 0..forward_htlcs_count { let short_channel_id = Readable::read(reader)?; let pending_forwards_count: u64 = Readable::read(reader)?; - let mut pending_forwards = Vec::with_capacity(cmp::min(pending_forwards_count as usize, 128)); + let mut pending_forwards = Vec::with_capacity(cmp::min(pending_forwards_count as usize, MAX_ALLOC_SIZE/mem::size_of::())); for _ in 0..pending_forwards_count { pending_forwards.push(Readable::read(reader)?); } @@ -3773,7 +3778,7 @@ impl<'a, ChanSigner: ChannelKeys + Readable, M: Deref, T: Deref, K: Deref, F: De for _ in 0..claimable_htlcs_count { let payment_hash = Readable::read(reader)?; let previous_hops_len: u64 = Readable::read(reader)?; - let mut previous_hops = Vec::with_capacity(cmp::min(previous_hops_len as usize, 2)); + let mut previous_hops = Vec::with_capacity(cmp::min(previous_hops_len as usize, MAX_ALLOC_SIZE/mem::size_of::())); for _ in 0..previous_hops_len { previous_hops.push(Readable::read(reader)?); } @@ -3781,7 +3786,7 @@ impl<'a, ChanSigner: ChannelKeys + Readable, M: Deref, T: Deref, K: Deref, F: De } let peer_count: u64 = Readable::read(reader)?; - let mut per_peer_state = HashMap::with_capacity(cmp::min(peer_count as usize, 128)); + let mut per_peer_state = HashMap::with_capacity(cmp::min(peer_count as usize, MAX_ALLOC_SIZE/mem::size_of::<(PublicKey, Mutex)>())); for _ in 0..peer_count { let peer_pubkey = Readable::read(reader)?; let peer_state = PeerState { @@ -3790,6 +3795,15 @@ impl<'a, ChanSigner: ChannelKeys + Readable, M: Deref, T: Deref, K: Deref, F: De per_peer_state.insert(peer_pubkey, Mutex::new(peer_state)); } + let event_count: u64 = Readable::read(reader)?; + let mut pending_events_read: Vec = Vec::with_capacity(cmp::min(event_count as usize, MAX_ALLOC_SIZE/mem::size_of::())); + for _ in 0..event_count { + match MaybeReadable::read(reader)? { + Some(event) => pending_events_read.push(event), + None => continue, + } + } + let last_node_announcement_serial: u32 = Readable::read(reader)?; let channel_manager = ChannelManager { @@ -3815,7 +3829,7 @@ impl<'a, ChanSigner: ChannelKeys + Readable, M: Deref, T: Deref, K: Deref, F: De per_peer_state: RwLock::new(per_peer_state), - pending_events: Mutex::new(Vec::new()), + pending_events: Mutex::new(pending_events_read), total_consistency_lock: RwLock::new(()), keys_manager: args.keys_manager, logger: args.logger,