X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fln%2Fchannel.rs;h=263d0dbd22959170b27ebab3b1300ac109c3af50;hb=bc273a293949df9e749bca4ee1d07fa20ffd8fa0;hp=a61a8de82debecd5906c0ee95bdcdb420aae48f4;hpb=d007d1b79f51460d97c17cb95a5dc6f7993f9504;p=rust-lightning diff --git a/lightning/src/ln/channel.rs b/lightning/src/ln/channel.rs index a61a8de8..263d0dbd 100644 --- a/lightning/src/ln/channel.rs +++ b/lightning/src/ln/channel.rs @@ -7,9 +7,11 @@ // You may not use this file except in accordance with one or both of these // licenses. -use bitcoin::blockdata::script::{Script,Builder}; -use bitcoin::blockdata::transaction::{Transaction, EcdsaSighashType}; -use bitcoin::util::sighash; +use bitcoin::blockdata::constants::ChainHash; +use bitcoin::blockdata::script::{Script, ScriptBuf, Builder}; +use bitcoin::blockdata::transaction::Transaction; +use bitcoin::sighash; +use bitcoin::sighash::EcdsaSighashType; use bitcoin::consensus::encode; use bitcoin::hashes::Hash; @@ -38,7 +40,7 @@ use crate::chain::transaction::{OutPoint, TransactionData}; use crate::sign::{EcdsaChannelSigner, WriteableEcdsaChannelSigner, EntropySource, ChannelSigner, SignerProvider, NodeSigner, Recipient}; use crate::events::ClosureReason; use crate::routing::gossip::NodeId; -use crate::util::ser::{Readable, ReadableArgs, Writeable, Writer, VecWriter}; +use crate::util::ser::{Readable, ReadableArgs, Writeable, Writer}; use crate::util::logger::Logger; use crate::util::errors::APIError; use crate::util::config::{UserConfig, ChannelConfig, LegacyChannelConfig, ChannelHandshakeConfig, ChannelHandshakeLimits, MaxDustHTLCExposure}; @@ -47,12 +49,14 @@ use crate::util::scid_utils::scid_from_parts; use crate::io; use crate::prelude::*; use core::{cmp,mem,fmt}; +use core::convert::TryInto; use core::ops::Deref; #[cfg(any(test, fuzzing, debug_assertions))] use crate::sync::Mutex; -use bitcoin::hashes::hex::ToHex; use crate::sign::type_resolver::ChannelSignerType; +use super::channel_keys::{DelayedPaymentBasepoint, HtlcBasepoint, RevocationBasepoint}; + #[cfg(test)] pub struct ChannelValueStat { pub value_to_self_msat: u64, @@ -532,6 +536,15 @@ pub(super) struct MonitorRestoreUpdates { pub announcement_sigs: Option, } +/// The return value of `signer_maybe_unblocked` +#[allow(unused)] +pub(super) struct SignerResumeUpdates { + pub commitment_update: Option, + pub funding_signed: Option, + pub funding_created: Option, + pub channel_ready: Option, +} + /// The return value of `channel_reestablish` pub(super) struct ReestablishResponses { pub channel_ready: Option, @@ -542,18 +555,17 @@ pub(super) struct ReestablishResponses { pub shutdown_msg: Option, } -/// The return type of `force_shutdown` -/// -/// Contains a tuple with the following: -/// - An optional (counterparty_node_id, funding_txo, [`ChannelMonitorUpdate`]) tuple -/// - A list of HTLCs to fail back in the form of the (source, payment hash, and this channel's -/// counterparty_node_id and channel_id). -/// - An optional transaction id identifying a corresponding batch funding transaction. -pub(crate) type ShutdownResult = ( - Option<(PublicKey, OutPoint, ChannelMonitorUpdate)>, - Vec<(HTLCSource, PaymentHash, PublicKey, ChannelId)>, - Option -); +/// The result of a shutdown that should be handled. +#[must_use] +pub(crate) struct ShutdownResult { + /// A channel monitor update to apply. + pub(crate) monitor_update: Option<(PublicKey, OutPoint, ChannelMonitorUpdate)>, + /// A list of dropped outbound HTLCs that can safely be failed backwards immediately. + pub(crate) dropped_outbound_htlcs: Vec<(HTLCSource, PaymentHash, PublicKey, ChannelId)>, + /// An unbroadcasted batch funding transaction id. The closure of this channel should be + /// propagated to the remainder of the batch. + pub(crate) unbroadcasted_batch_funding_txid: Option, +} /// If the majority of the channels funds are to the fundee and the initiator holds only just /// enough funds to cover their reserve value, channels are at risk of getting "stuck". Because the @@ -715,7 +727,7 @@ pub(super) struct ChannelContext where SP::Target: SignerProvider { holder_signer: ChannelSignerType<::Signer>, shutdown_scriptpubkey: Option, - destination_script: Script, + destination_script: ScriptBuf, // Our commitment numbers start at 2^48-1 and count down, whereas the ones used in transaction // generation start at 0 and count up...this simplifies some parts of implementation at the @@ -723,7 +735,7 @@ pub(super) struct ChannelContext where SP::Target: SignerProvider { cur_holder_commitment_transaction_number: u64, cur_counterparty_commitment_transaction_number: u64, - value_to_self_msat: u64, // Excluding all pending_htlcs, excluding fees + value_to_self_msat: u64, // Excluding all pending_htlcs, fees, and anchor outputs pending_inbound_htlcs: Vec, pending_outbound_htlcs: Vec, holding_cell_htlc_updates: Vec, @@ -749,6 +761,18 @@ pub(super) struct ChannelContext where SP::Target: SignerProvider { monitor_pending_failures: Vec<(HTLCSource, PaymentHash, HTLCFailReason)>, monitor_pending_finalized_fulfills: Vec, + /// If we went to send a commitment update (ie some messages then [`msgs::CommitmentSigned`]) + /// but our signer (initially) refused to give us a signature, we should retry at some point in + /// the future when the signer indicates it may have a signature for us. + /// + /// This flag is set in such a case. Note that we don't need to persist this as we'll end up + /// setting it again as a side-effect of [`Channel::channel_reestablish`]. + signer_pending_commitment_update: bool, + /// Similar to [`Self::signer_pending_commitment_update`] but we're waiting to send either a + /// [`msgs::FundingCreated`] or [`msgs::FundingSigned`] depending on if this channel is + /// outbound or inbound. + signer_pending_funding: bool, + // pending_update_fee is filled when sending and receiving update_fee. // // Because it follows the same commitment flow as HTLCs, `FeeUpdateState` is either `Outbound` @@ -794,6 +818,19 @@ pub(super) struct ChannelContext where SP::Target: SignerProvider { #[cfg(not(test))] closing_fee_limits: Option<(u64, u64)>, + /// If we remove an HTLC (or fee update), commit, and receive our counterparty's + /// `revoke_and_ack`, we remove all knowledge of said HTLC (or fee update). However, the latest + /// local commitment transaction that we can broadcast still contains the HTLC (or old fee) + /// until we receive a further `commitment_signed`. Thus we are not eligible for initiating the + /// `closing_signed` negotiation if we're expecting a counterparty `commitment_signed`. + /// + /// To ensure we don't send a `closing_signed` too early, we track this state here, waiting + /// until we see a `commitment_signed` before doing so. + /// + /// We don't bother to persist this - we anticipate this state won't last longer than a few + /// milliseconds, so any accidental force-closes here should be exceedingly rare. + expecting_peer_commitment_signed: bool, + /// The hash of the block in which the funding transaction was included. funding_tx_confirmed_in: Option, funding_tx_confirmation_height: u32, @@ -847,7 +884,7 @@ pub(super) struct ChannelContext where SP::Target: SignerProvider { counterparty_prev_commitment_point: Option, counterparty_node_id: PublicKey, - counterparty_shutdown_scriptpubkey: Option