Merge pull request #650 from TheBlueMatt/2020-06-fix-build
[rust-lightning] / lightning / src / ln / channel.rs
index b4a24833919b96fa26b9037ecea1844d253d0e0e..97c9fa6789b3c4a2045539281c27b1a28cf1d0b4 100644 (file)
@@ -184,9 +184,9 @@ enum HTLCUpdateAwaitingACK {
 /// move on to ShutdownComplete, at which point most calls into this channel are disallowed.
 enum ChannelState {
        /// Implies we have (or are prepared to) send our open_channel/accept_channel message
-       OurInitSent = (1 << 0),
+       OurInitSent = 1 << 0,
        /// Implies we have received their open_channel/accept_channel message
-       TheirInitSent = (1 << 1),
+       TheirInitSent = 1 << 1,
        /// We have sent funding_created and are awaiting a funding_signed to advance to FundingSent.
        /// Note that this is nonsense for an inbound channel as we immediately generate funding_signed
        /// upon receipt of funding_created, so simply skip this state.
@@ -197,35 +197,35 @@ enum ChannelState {
        FundingSent = 8,
        /// Flag which can be set on FundingSent to indicate they sent us a funding_locked message.
        /// Once both TheirFundingLocked and OurFundingLocked are set, state moves on to ChannelFunded.
-       TheirFundingLocked = (1 << 4),
+       TheirFundingLocked = 1 << 4,
        /// Flag which can be set on FundingSent to indicate we sent them a funding_locked message.
        /// Once both TheirFundingLocked and OurFundingLocked are set, state moves on to ChannelFunded.
-       OurFundingLocked = (1 << 5),
+       OurFundingLocked = 1 << 5,
        ChannelFunded = 64,
        /// Flag which is set on ChannelFunded and FundingSent indicating remote side is considered
        /// "disconnected" and no updates are allowed until after we've done a channel_reestablish
        /// dance.
-       PeerDisconnected = (1 << 7),
+       PeerDisconnected = 1 << 7,
        /// Flag which is set on ChannelFunded, FundingCreated, and FundingSent indicating the user has
        /// told us they failed to update our ChannelMonitor somewhere and we should pause sending any
        /// outbound messages until they've managed to do so.
-       MonitorUpdateFailed = (1 << 8),
+       MonitorUpdateFailed = 1 << 8,
        /// Flag which implies that we have sent a commitment_signed but are awaiting the responding
        /// revoke_and_ack message. During this time period, we can't generate new commitment_signed
        /// messages as then we will be unable to determine which HTLCs they included in their
        /// revoke_and_ack implicit ACK, so instead we have to hold them away temporarily to be sent
        /// later.
        /// Flag is set on ChannelFunded.
-       AwaitingRemoteRevoke = (1 << 9),
+       AwaitingRemoteRevoke = 1 << 9,
        /// Flag which is set on ChannelFunded or FundingSent after receiving a shutdown message from
        /// the remote end. If set, they may not add any new HTLCs to the channel, and we are expected
        /// to respond with our own shutdown message when possible.
-       RemoteShutdownSent = (1 << 10),
+       RemoteShutdownSent = 1 << 10,
        /// Flag which is set on ChannelFunded or FundingSent after sending a shutdown message. At this
        /// point, we may not add any new HTLCs to the channel.
        /// TODO: Investigate some kind of timeout mechanism by which point the remote end must provide
        /// us their shutdown.
-       LocalShutdownSent = (1 << 11),
+       LocalShutdownSent = 1 << 11,
        /// We've successfully negotiated a closing_signed dance. At this point ChannelManager is about
        /// to drop us, but we store this anyway.
        ShutdownComplete = 4096,
@@ -1755,8 +1755,8 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                self.commit_tx_fee_msat(their_acked_htlcs + addl_htlcs)
        }
 
-       pub fn update_add_htlc<F>(&mut self, msg: &msgs::UpdateAddHTLC, mut pending_forward_status: PendingHTLCStatus, create_pending_htlc_status: F) -> Result<(), ChannelError>
-       where F: for<'a> Fn(&'a Self, PendingHTLCStatus, u16) -> PendingHTLCStatus {
+       pub fn update_add_htlc<F, L: Deref>(&mut self, msg: &msgs::UpdateAddHTLC, mut pending_forward_status: PendingHTLCStatus, create_pending_htlc_status: F, logger: &L) -> Result<(), ChannelError>
+       where F: for<'a> Fn(&'a Self, PendingHTLCStatus, u16) -> PendingHTLCStatus, L::Target: Logger {
                // We can't accept HTLCs sent after we've sent a shutdown.
                let local_sent_shutdown = (self.channel_state & (ChannelState::ChannelFunded as u32 | ChannelState::LocalShutdownSent as u32)) != (ChannelState::ChannelFunded as u32);
                if local_sent_shutdown {
@@ -1845,6 +1845,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                        if pending_remote_value_msat - msg.amount_msat - chan_reserve_msat < remote_fee_cost_incl_stuck_buffer_msat {
                                // Note that if the pending_forward_status is not updated here, then it's because we're already failing
                                // the HTLC, i.e. its status is already set to failing.
+                               log_info!(logger, "Attempting to fail HTLC due to fee spike buffer violation");
                                pending_forward_status = create_pending_htlc_status(self, pending_forward_status, 0x1000|7);
                        }
                } else {
@@ -3299,7 +3300,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
        ///
        /// May return some HTLCs (and their payment_hash) which have timed out and should be failed
        /// back.
-       pub fn block_connected(&mut self, header: &BlockHeader, height: u32, txn_matched: &[&Transaction], indexes_of_txn_matched: &[u32]) -> Result<(Option<msgs::FundingLocked>, Vec<(HTLCSource, PaymentHash)>), msgs::ErrorMessage> {
+       pub fn block_connected(&mut self, header: &BlockHeader, height: u32, txn_matched: &[&Transaction], indexes_of_txn_matched: &[usize]) -> Result<(Option<msgs::FundingLocked>, Vec<(HTLCSource, PaymentHash)>), msgs::ErrorMessage> {
                let mut timed_out_htlcs = Vec::new();
                self.holding_cell_htlc_updates.retain(|htlc_update| {
                        match htlc_update {
@@ -3350,6 +3351,10 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                                                                }
                                                        }
                                                }
+                                               if height > 0xff_ff_ff || (*index_in_block) > 0xff_ff_ff {
+                                                       panic!("Block was bogus - either height 16 million or had > 16 million transactions");
+                                               }
+                                               assert!(txo_idx <= 0xffff); // txo_idx is a (u16 as usize), so this is just listed here for completeness
                                                self.funding_tx_confirmations = 1;
                                                self.short_channel_id = Some(((height as u64)          << (5*8)) |
                                                                             ((*index_in_block as u64) << (2*8)) |