Make htlc_minimum_msat configurable
[rust-lightning] / lightning / src / ln / channel.rs
index 3dc5dcd832d4a7f99c1d4590a31a7acb79faf670..86074627523ca3c7b7ea4f2c5f55b43a756ffbfd 100644 (file)
@@ -295,7 +295,7 @@ pub(super) struct Channel<ChanSigner: ChannelKeys> {
        holding_cell_update_fee: Option<u64>,
        next_local_htlc_id: u64,
        next_remote_htlc_id: u64,
-       channel_update_count: u32,
+       update_time_counter: u32,
        feerate_per_kw: u64,
 
        #[cfg(debug_assertions)]
@@ -433,10 +433,6 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                cmp::max(at_open_background_feerate * B_OUTPUT_PLUS_SPENDING_INPUT_WEIGHT / 1000, 546) //TODO
        }
 
-       fn derive_our_htlc_minimum_msat(_at_open_channel_feerate_per_kw: u64) -> u64 {
-               1000 // TODO
-       }
-
        // Constructors:
        pub fn new_outbound<K: Deref, F: Deref>(fee_estimator: &F, keys_provider: &K, their_node_id: PublicKey, channel_value_satoshis: u64, push_msat: u64, user_id: u64, logger: Arc<Logger>, config: &UserConfig) -> Result<Channel<ChanSigner>, APIError>
        where K::Target: KeysInterface<ChanKeySigner = ChanSigner>,
@@ -490,7 +486,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                        holding_cell_update_fee: None,
                        next_local_htlc_id: 0,
                        next_remote_htlc_id: 0,
-                       channel_update_count: 1,
+                       update_time_counter: 1,
 
                        resend_order: RAACommitmentOrder::CommitmentFirst,
 
@@ -519,7 +515,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                        their_max_htlc_value_in_flight_msat: 0,
                        their_channel_reserve_satoshis: 0,
                        their_htlc_minimum_msat: 0,
-                       our_htlc_minimum_msat: Channel::<ChanSigner>::derive_our_htlc_minimum_msat(feerate),
+                       our_htlc_minimum_msat: if config.own_channel_config.our_htlc_minimum_msat == 0 { 1 } else { config.own_channel_config.our_htlc_minimum_msat },
                        their_to_self_delay: 0,
                        our_to_self_delay: config.own_channel_config.our_to_self_delay,
                        their_max_accepted_htlcs: 0,
@@ -714,7 +710,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                        holding_cell_update_fee: None,
                        next_local_htlc_id: 0,
                        next_remote_htlc_id: 0,
-                       channel_update_count: 1,
+                       update_time_counter: 1,
 
                        resend_order: RAACommitmentOrder::CommitmentFirst,
 
@@ -744,7 +740,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                        their_max_htlc_value_in_flight_msat: cmp::min(msg.max_htlc_value_in_flight_msat, msg.funding_satoshis * 1000),
                        their_channel_reserve_satoshis: msg.channel_reserve_satoshis,
                        their_htlc_minimum_msat: msg.htlc_minimum_msat,
-                       our_htlc_minimum_msat: Channel::<ChanSigner>::derive_our_htlc_minimum_msat(msg.feerate_per_kw as u64),
+                       our_htlc_minimum_msat: if config.own_channel_config.our_htlc_minimum_msat == 0 { 1 } else { config.own_channel_config.our_htlc_minimum_msat },
                        their_to_self_delay: msg.to_self_delay,
                        our_to_self_delay: config.own_channel_config.our_to_self_delay,
                        their_max_accepted_htlcs: msg.max_accepted_htlcs,
@@ -1586,7 +1582,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                        self.channel_state |= ChannelState::TheirFundingLocked as u32;
                } else if non_shutdown_state == (ChannelState::FundingSent as u32 | ChannelState::OurFundingLocked as u32) {
                        self.channel_state = ChannelState::ChannelFunded as u32 | (self.channel_state & MULTI_STATE_FLAGS);
-                       self.channel_update_count += 1;
+                       self.update_time_counter += 1;
                } else if (self.channel_state & (ChannelState::ChannelFunded as u32) != 0 &&
                                 // Note that funding_signed/funding_created will have decremented both by 1!
                                 self.cur_local_commitment_transaction_number == INITIAL_COMMITMENT_NUMBER - 1 &&
@@ -1656,6 +1652,9 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                if msg.amount_msat > self.channel_value_satoshis * 1000 {
                        return Err(ChannelError::Close("Remote side tried to send more than the total value of the channel"));
                }
+               if msg.amount_msat == 0 {
+                       return Err(ChannelError::Close("Remote side tried to send a 0-msat HTLC"));
+               }
                if msg.amount_msat < self.our_htlc_minimum_msat {
                        return Err(ChannelError::Close("Remote side tried to send less than our minimum HTLC value"));
                }
@@ -2480,7 +2479,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                }
                Channel::<ChanSigner>::check_remote_fee(fee_estimator, msg.feerate_per_kw)?;
                self.pending_update_fee = Some(msg.feerate_per_kw as u64);
-               self.channel_update_count += 1;
+               self.update_time_counter += 1;
                Ok(())
        }
 
@@ -2763,7 +2762,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                // From here on out, we may not fail!
 
                self.channel_state |= ChannelState::RemoteShutdownSent as u32;
-               self.channel_update_count += 1;
+               self.update_time_counter += 1;
 
                // We can't send our shutdown until we've committed all of our pending HTLCs, but the
                // remote side is unlikely to accept any new HTLCs, so we go ahead and "free" any holding
@@ -2793,7 +2792,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                };
 
                self.channel_state |= ChannelState::LocalShutdownSent as u32;
-               self.channel_update_count += 1;
+               self.update_time_counter += 1;
 
                Ok((our_shutdown, self.maybe_propose_first_closing_signed(fee_estimator), dropped_outbound_htlcs))
        }
@@ -2860,7 +2859,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                        if last_fee == msg.fee_satoshis {
                                self.build_signed_closing_transaction(&mut closing_tx, &msg.signature, &our_sig);
                                self.channel_state = ChannelState::ShutdownComplete as u32;
-                               self.channel_update_count += 1;
+                               self.update_time_counter += 1;
                                return Ok((None, Some(closing_tx)));
                        }
                }
@@ -2910,7 +2909,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                self.build_signed_closing_transaction(&mut closing_tx, &msg.signature, &our_sig);
 
                self.channel_state = ChannelState::ShutdownComplete as u32;
-               self.channel_update_count += 1;
+               self.update_time_counter += 1;
 
                Ok((Some(msgs::ClosingSigned {
                        channel_id: self.channel_id,
@@ -3022,8 +3021,8 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
        }
 
        /// Allowed in any state (including after shutdown)
-       pub fn get_channel_update_count(&self) -> u32 {
-               self.channel_update_count
+       pub fn get_update_time_counter(&self) -> u32 {
+               self.update_time_counter
        }
 
        pub fn get_latest_monitor_update_id(&self) -> u64 {
@@ -3149,7 +3148,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                                                        panic!("Client called ChannelManager::funding_transaction_generated with bogus transaction!");
                                                }
                                                self.channel_state = ChannelState::ShutdownComplete as u32;
-                                               self.channel_update_count += 1;
+                                               self.update_time_counter += 1;
                                                return Err(msgs::ErrorMessage {
                                                        channel_id: self.channel_id(),
                                                        data: "funding tx had wrong script/value".to_owned()
@@ -3175,6 +3174,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                }
                if header.bitcoin_hash() != self.last_block_connected {
                        self.last_block_connected = header.bitcoin_hash();
+                       self.update_time_counter = cmp::max(self.update_time_counter, header.time);
                        if let Some(channel_monitor) = self.channel_monitor.as_mut() {
                                channel_monitor.last_block_hash = self.last_block_connected;
                        }
@@ -3185,7 +3185,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                                                true
                                        } else if non_shutdown_state == (ChannelState::FundingSent as u32 | ChannelState::TheirFundingLocked as u32) {
                                                self.channel_state = ChannelState::ChannelFunded as u32 | (self.channel_state & MULTI_STATE_FLAGS);
-                                               self.channel_update_count += 1;
+                                               self.update_time_counter += 1;
                                                true
                                        } else if non_shutdown_state == (ChannelState::FundingSent as u32 | ChannelState::OurFundingLocked as u32) {
                                                // We got a reorg but not enough to trigger a force close, just update
@@ -3492,6 +3492,11 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                if amount_msat > self.channel_value_satoshis * 1000 {
                        return Err(ChannelError::Ignore("Cannot send more than the total value of the channel"));
                }
+
+               if amount_msat == 0 {
+                       return Err(ChannelError::Ignore("Cannot send 0-msat HTLC"));
+               }
+
                if amount_msat < self.their_htlc_minimum_msat {
                        return Err(ChannelError::Ignore("Cannot send less than their minimum HTLC value"));
                }
@@ -3728,7 +3733,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                } else {
                        self.channel_state |= ChannelState::LocalShutdownSent as u32;
                }
-               self.channel_update_count += 1;
+               self.update_time_counter += 1;
 
                // Go ahead and drop holding cell updates as we'd rather fail payments than wait to send
                // our shutdown until we've committed all of the pending changes.
@@ -3777,7 +3782,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                }
 
                self.channel_state = ChannelState::ShutdownComplete as u32;
-               self.channel_update_count += 1;
+               self.update_time_counter += 1;
                if self.channel_monitor.is_some() {
                        (self.channel_monitor.as_mut().unwrap().get_latest_local_commitment_txn(), dropped_outbound_htlcs)
                } else {
@@ -3883,18 +3888,6 @@ impl<ChanSigner: ChannelKeys + Writeable> Writeable for Channel<ChanSigner> {
                        }
                }
 
-               macro_rules! write_option {
-                       ($thing: expr) => {
-                               match &$thing {
-                                       &None => 0u8.write(writer)?,
-                                       &Some(ref v) => {
-                                               1u8.write(writer)?;
-                                               v.write(writer)?;
-                                       },
-                               }
-                       }
-               }
-
                (self.pending_outbound_htlcs.len() as u64).write(writer)?;
                for htlc in self.pending_outbound_htlcs.iter() {
                        htlc.htlc_id.write(writer)?;
@@ -3912,15 +3905,15 @@ impl<ChanSigner: ChannelKeys + Writeable> Writeable for Channel<ChanSigner> {
                                },
                                &OutboundHTLCState::RemoteRemoved(ref fail_reason) => {
                                        2u8.write(writer)?;
-                                       write_option!(*fail_reason);
+                                       fail_reason.write(writer)?;
                                },
                                &OutboundHTLCState::AwaitingRemoteRevokeToRemove(ref fail_reason) => {
                                        3u8.write(writer)?;
-                                       write_option!(*fail_reason);
+                                       fail_reason.write(writer)?;
                                },
                                &OutboundHTLCState::AwaitingRemovedRemoteRevoke(ref fail_reason) => {
                                        4u8.write(writer)?;
-                                       write_option!(*fail_reason);
+                                       fail_reason.write(writer)?;
                                },
                        }
                }
@@ -3971,12 +3964,12 @@ impl<ChanSigner: ChannelKeys + Writeable> Writeable for Channel<ChanSigner> {
                        fail_reason.write(writer)?;
                }
 
-               write_option!(self.pending_update_fee);
-               write_option!(self.holding_cell_update_fee);
+               self.pending_update_fee.write(writer)?;
+               self.holding_cell_update_fee.write(writer)?;
 
                self.next_local_htlc_id.write(writer)?;
                (self.next_remote_htlc_id - dropped_inbound_htlcs).write(writer)?;
-               self.channel_update_count.write(writer)?;
+               self.update_time_counter.write(writer)?;
                self.feerate_per_kw.write(writer)?;
 
                match self.last_sent_closing_fee {
@@ -3989,9 +3982,9 @@ impl<ChanSigner: ChannelKeys + Writeable> Writeable for Channel<ChanSigner> {
                        None => 0u8.write(writer)?,
                }
 
-               write_option!(self.funding_txo);
-               write_option!(self.funding_tx_confirmed_in);
-               write_option!(self.short_channel_id);
+               self.funding_txo.write(writer)?;
+               self.funding_tx_confirmed_in.write(writer)?;
+               self.short_channel_id.write(writer)?;
 
                self.last_block_connected.write(writer)?;
                self.funding_tx_confirmations.write(writer)?;
@@ -4007,13 +4000,13 @@ impl<ChanSigner: ChannelKeys + Writeable> Writeable for Channel<ChanSigner> {
                self.their_max_accepted_htlcs.write(writer)?;
                self.minimum_depth.write(writer)?;
 
-               write_option!(self.their_pubkeys);
-               write_option!(self.their_cur_commitment_point);
+               self.their_pubkeys.write(writer)?;
+               self.their_cur_commitment_point.write(writer)?;
 
-               write_option!(self.their_prev_commitment_point);
+               self.their_prev_commitment_point.write(writer)?;
                self.their_node_id.write(writer)?;
 
-               write_option!(self.their_shutdown_scriptpubkey);
+               self.their_shutdown_scriptpubkey.write(writer)?;
 
                self.commitment_secrets.write(writer)?;
 
@@ -4136,7 +4129,7 @@ impl<ChanSigner: ChannelKeys + Readable> ReadableArgs<Arc<Logger>> for Channel<C
 
                let next_local_htlc_id = Readable::read(reader)?;
                let next_remote_htlc_id = Readable::read(reader)?;
-               let channel_update_count = Readable::read(reader)?;
+               let update_time_counter = Readable::read(reader)?;
                let feerate_per_kw = Readable::read(reader)?;
 
                let last_sent_closing_fee = match <u8 as Readable>::read(reader)? {
@@ -4215,7 +4208,7 @@ impl<ChanSigner: ChannelKeys + Readable> ReadableArgs<Arc<Logger>> for Channel<C
                        holding_cell_update_fee,
                        next_local_htlc_id,
                        next_remote_htlc_id,
-                       channel_update_count,
+                       update_time_counter,
                        feerate_per_kw,
 
                        #[cfg(debug_assertions)]