X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;ds=sidebyside;f=src%2Fln%2Fchannel.rs;h=68a23dc67b9348dac07e795e5b8c9d50555470ed;hb=e7526112470409c921e08da91dee8eeeaf0b0563;hp=fc67085250a3e1f429d53dcffa06f7ff3455a0d9;hpb=e571c7ac4011c458970e780fa1b7b92136f06f2d;p=rust-lightning diff --git a/src/ln/channel.rs b/src/ln/channel.rs index fc670852..68a23dc6 100644 --- a/src/ln/channel.rs +++ b/src/ln/channel.rs @@ -14,7 +14,7 @@ use crypto::digest::Digest; use crypto::hkdf::{hkdf_extract,hkdf_expand}; use ln::msgs; -use ln::msgs::{HandleError, MsgEncodable}; +use ln::msgs::{ErrorAction, HandleError, MsgEncodable}; use ln::channelmonitor::ChannelMonitor; use ln::channelmanager::{PendingForwardHTLCInfo, HTLCFailReason}; use ln::chan_utils::{TxCreationKeys,HTLCOutputInCommitment,HTLC_SUCCESS_TX_WEIGHT,HTLC_TIMEOUT_TX_WEIGHT}; @@ -136,7 +136,7 @@ enum HTLCState { /// anyway). LocalRemoved, /// Removed by us, sent a new commitment_signed and got a revoke_and_ack. Just waiting on an - /// updated local commitment transaction. + /// updated local commitment transaction. Implies local_removed_fulfilled. LocalRemovedAwaitingCommitment, } @@ -271,7 +271,7 @@ pub struct Channel { /// to detect unconfirmation after a serialize-unserialize roudtrip where we may not see a full /// series of block_connected/block_disconnected calls. Obviously this is not a guarantee as we /// could miss the funding_tx_confirmed_in block as well, but it serves as a useful fallback. - funding_tx_confirmed_in: Sha256dHash, + funding_tx_confirmed_in: Option, short_channel_id: Option, /// Used to deduplicate block_connected callbacks last_block_connected: Sha256dHash, @@ -401,7 +401,7 @@ impl Channel { last_sent_closing_fee: None, - funding_tx_confirmed_in: Default::default(), + funding_tx_confirmed_in: None, short_channel_id: None, last_block_connected: Default::default(), funding_tx_confirmations: 0, @@ -519,7 +519,7 @@ impl Channel { last_sent_closing_fee: None, - funding_tx_confirmed_in: Default::default(), + funding_tx_confirmed_in: None, short_channel_id: None, last_block_connected: Default::default(), funding_tx_confirmations: 0, @@ -663,7 +663,7 @@ impl Channel { } else { match htlc.state { HTLCState::AwaitingRemoteRevokeToRemove|HTLCState::AwaitingRemovedRemoteRevoke => { - if generated_by_local && htlc.fail_reason.is_none() { + if htlc.fail_reason.is_none() { value_to_self_msat_offset -= htlc.amount_msat as i64; } }, @@ -678,6 +678,7 @@ impl Channel { } }, HTLCState::LocalRemovedAwaitingCommitment => { + assert!(htlc.local_removed_fulfilled); value_to_self_msat_offset += htlc.amount_msat as i64; }, _ => {}, @@ -985,7 +986,7 @@ impl Channel { } let htlc_id = { - let mut htlc = &mut self.pending_htlcs[pending_idx]; + let htlc = &mut self.pending_htlcs[pending_idx]; if htlc.state == HTLCState::Committed { htlc.state = HTLCState::LocalRemoved; htlc.local_removed_fulfilled = true; @@ -1356,7 +1357,7 @@ impl Channel { Err(HandleError{err: "Remote tried to fulfill/fail an HTLC we couldn't find", action: None}) } - pub fn update_fulfill_htlc(&mut self, msg: &msgs::UpdateFulfillHTLC) -> Result { + pub fn update_fulfill_htlc(&mut self, msg: &msgs::UpdateFulfillHTLC) -> Result<(), HandleError> { if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) { return Err(HandleError{err: "Got add HTLC message when channel was not in an operational state", action: None}); } @@ -1366,9 +1367,8 @@ impl Channel { let mut payment_hash = [0; 32]; sha.result(&mut payment_hash); - self.channel_monitor.provide_payment_preimage(&payment_hash, &msg.payment_preimage); self.mark_outbound_htlc_removed(msg.htlc_id, Some(payment_hash), None)?; - Ok(self.channel_monitor.clone()) + Ok(()) } pub fn update_fail_htlc(&mut self, msg: &msgs::UpdateFailHTLC, fail_reason: HTLCFailReason) -> Result<[u8; 32], HandleError> { @@ -1827,6 +1827,7 @@ impl Channel { self.user_id } + /// May only be called after funding has been initiated (ie is_funding_initiated() is true) pub fn channel_monitor(&self) -> ChannelMonitor { if self.channel_state < ChannelState::FundingCreated as u32 { panic!("Can't get a channel monitor until funding has been created"); @@ -1904,6 +1905,11 @@ impl Channel { self.is_usable() } + /// Returns true if funding_created was sent/received. + pub fn is_funding_initiated(&self) -> bool { + self.channel_state >= ChannelState::FundingCreated as u32 + } + /// Returns true if this channel is fully shut down. True here implies that no further actions /// may/will be taken on this channel, and thus this object should be freed. Any future changes /// will be handled appropriately by the chain monitor. @@ -1917,34 +1923,48 @@ impl Channel { /// Called by channelmanager based on chain blocks being connected. /// Note that we only need to use this to detect funding_signed, anything else is handled by /// the channel_monitor. - pub fn block_connected(&mut self, header: &BlockHeader, height: u32, txn_matched: &[&Transaction], indexes_of_txn_matched: &[u32]) -> Option { + /// In case of Err, the channel may have been closed, at which point the standard requirements + /// apply - no calls may be made except those explicitly stated to be allowed post-shutdown. + /// Only returns an ErrorAction of DisconnectPeer, if Err. + pub fn block_connected(&mut self, header: &BlockHeader, height: u32, txn_matched: &[&Transaction], indexes_of_txn_matched: &[u32]) -> Result, HandleError> { let non_shutdown_state = self.channel_state & (!BOTH_SIDES_SHUTDOWN_MASK); if self.funding_tx_confirmations > 0 { if header.bitcoin_hash() != self.last_block_connected { self.last_block_connected = header.bitcoin_hash(); self.funding_tx_confirmations += 1; if self.funding_tx_confirmations == CONF_TARGET as u64 { - if non_shutdown_state == ChannelState::FundingSent as u32 { + let need_commitment_update = if non_shutdown_state == ChannelState::FundingSent as u32 { self.channel_state |= ChannelState::OurFundingLocked as u32; + true } else if non_shutdown_state == (ChannelState::FundingSent as u32 | ChannelState::TheirFundingLocked as u32) { self.channel_state = ChannelState::ChannelFunded as u32 | (self.channel_state & BOTH_SIDES_SHUTDOWN_MASK); self.channel_update_count += 1; - //TODO: Something about a state where we "lost confirmation" + true + } else if self.channel_state == (ChannelState::FundingSent as u32 | ChannelState::OurFundingLocked as u32) { + // We got a reorg but not enough to trigger a force close, just update + // funding_tx_confirmed_in and return. + false } else if self.channel_state < ChannelState::ChannelFunded as u32 { - panic!("Started confirming a channel in a state pre-FundingSent?"); - } - self.funding_tx_confirmed_in = header.bitcoin_hash(); + panic!("Started confirming a channel in a state pre-FundingSent?: {}", self.channel_state); + } else { + // We got a reorg but not enough to trigger a force close, just update + // funding_tx_confirmed_in and return. + false + }; + self.funding_tx_confirmed_in = Some(header.bitcoin_hash()); //TODO: Note that this must be a duplicate of the previous commitment point they sent us, //as otherwise we will have a commitment transaction that they can't revoke (well, kinda, //they can by sending two revoke_and_acks back-to-back, but not really). This appears to be //a protocol oversight, but I assume I'm just missing something. - let next_per_commitment_secret = self.build_local_commitment_secret(self.cur_local_commitment_transaction_number); - let next_per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &next_per_commitment_secret).unwrap(); - return Some(msgs::FundingLocked { - channel_id: self.channel_id, - next_per_commitment_point: next_per_commitment_point, - }); + if need_commitment_update { + let next_per_commitment_secret = self.build_local_commitment_secret(self.cur_local_commitment_transaction_number); + let next_per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &next_per_commitment_secret).unwrap(); + return Ok(Some(msgs::FundingLocked { + channel_id: self.channel_id, + next_per_commitment_point: next_per_commitment_point, + })); + } } } } @@ -1956,6 +1976,7 @@ impl Channel { tx.output[txo_idx].value != self.channel_value_satoshis { self.channel_state = ChannelState::ShutdownComplete as u32; self.channel_update_count += 1; + return Err(HandleError{err: "funding tx had wrong script/value", action: Some(ErrorAction::DisconnectPeer{msg: None})}); } else { self.funding_tx_confirmations = 1; self.short_channel_id = Some(((height as u64) << (5*8)) | @@ -1965,7 +1986,7 @@ impl Channel { } } } - None + Ok(None) } /// Called by channelmanager based on chain blocks being disconnected. @@ -1978,7 +1999,7 @@ impl Channel { return true; } } - if header.bitcoin_hash() == self.funding_tx_confirmed_in { + if Some(header.bitcoin_hash()) == self.funding_tx_confirmed_in { self.funding_tx_confirmations = CONF_TARGET as u64 - 1; } false