Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make timer_tick_occurred a second long timer #3065

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lightning-background-processor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ pub struct BackgroundProcessor {
}

#[cfg(not(test))]
const FRESHNESS_TIMER: u64 = 60;
const FRESHNESS_TIMER: u64 = 1;
#[cfg(test)]
const FRESHNESS_TIMER: u64 = 1;

Expand Down Expand Up @@ -933,7 +933,7 @@ mod tests {
use lightning::events::{Event, PathFailure, MessageSendEventsProvider, MessageSendEvent};
use lightning::{get_event_msg, get_event};
use lightning::ln::types::{PaymentHash, ChannelId};
use lightning::ln::channelmanager;
use lightning::ln::channelmanager::{self, TICKS_PER_MINUTE};
use lightning::ln::channelmanager::{BREAKDOWN_TIMEOUT, ChainParameters, MIN_CLTV_EXPIRY_DELTA, PaymentId};
use lightning::ln::features::{ChannelFeatures, NodeFeatures};
use lightning::ln::functional_test_utils::*;
Expand All @@ -960,7 +960,7 @@ mod tests {
use lightning_rapid_gossip_sync::RapidGossipSync;
use super::{BackgroundProcessor, GossipSync, FRESHNESS_TIMER};

const EVENT_DEADLINE: u64 = 5 * FRESHNESS_TIMER;
const EVENT_DEADLINE: u64 = 5 * TICKS_PER_MINUTE as u64 * FRESHNESS_TIMER;

#[derive(Clone, Hash, PartialEq, Eq)]
struct TestDescriptor{}
Expand Down
27 changes: 19 additions & 8 deletions lightning/src/ln/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ use crate::sync::Mutex;
use crate::sign::type_resolver::ChannelSignerType;

use super::channel_keys::{DelayedPaymentBasepoint, HtlcBasepoint, RevocationBasepoint};
use super::channelmanager::TICKS_PER_MINUTE;

#[cfg(test)]
pub struct ChannelValueStat {
Expand Down Expand Up @@ -968,9 +969,9 @@ pub(super) enum ChannelUpdateStatus {
/// We've announced the channel as enabled and are connected to our peer.
Enabled,
/// Our channel is no longer live, but we haven't announced the channel as disabled yet.
DisabledStaged(u8),
DisabledStaged(u16),
/// Our channel is live again, but we haven't announced the channel as enabled yet.
EnabledStaged(u8),
EnabledStaged(u16),
/// We've announced the channel as disabled.
Disabled,
}
Expand Down Expand Up @@ -1160,23 +1161,23 @@ pub(crate) const MIN_AFFORDABLE_HTLC_COUNT: usize = 4;
/// number of ticks to allow forwarding HTLCs by nodes that have yet to receive the new
/// ChannelUpdate prompted by the config update. This value was determined as follows:
///
/// * The expected interval between ticks (1 minute).
/// * The expected interval between ticks (1 second).
/// * The average convergence delay of updates across the network, i.e., ~300 seconds on average
/// for a node to see an update as seen on `<https://arxiv.org/pdf/2205.12737.pdf>`.
/// * `EXPIRE_PREV_CONFIG_TICKS` = convergence_delay / tick_interval
pub(crate) const EXPIRE_PREV_CONFIG_TICKS: usize = 5;
pub(crate) const EXPIRE_PREV_CONFIG_TICKS: usize = 5 * TICKS_PER_MINUTE as usize;

/// The number of ticks that may elapse while we're waiting for a response to a
/// [`msgs::RevokeAndACK`] or [`msgs::ChannelReestablish`] message before we attempt to disconnect
/// them.
///
/// See [`ChannelContext::sent_message_awaiting_response`] for more information.
pub(crate) const DISCONNECT_PEER_AWAITING_RESPONSE_TICKS: usize = 2;
pub(crate) const DISCONNECT_PEER_AWAITING_RESPONSE_TICKS: usize = 2 * TICKS_PER_MINUTE as usize;

/// The number of ticks that may elapse while we're waiting for an unfunded outbound/inbound channel
/// to be promoted to a [`Channel`] since the unfunded channel was created. An unfunded channel
/// exceeding this age limit will be force-closed and purged from memory.
pub(crate) const UNFUNDED_CHANNEL_AGE_LIMIT_TICKS: usize = 60;
pub(crate) const UNFUNDED_CHANNEL_AGE_LIMIT_TICKS: usize = 60 * TICKS_PER_MINUTE as usize;

/// Number of blocks needed for an output from a coinbase transaction to be spendable.
pub(crate) const COINBASE_MATURITY: u32 = 100;
Expand Down Expand Up @@ -3601,8 +3602,15 @@ pub(super) struct Channel<SP: Deref> where SP::Target: SignerProvider {
pub context: ChannelContext<SP>,
#[cfg(any(dual_funding, splicing))]
pub dual_funding_channel_context: Option<DualFundingChannelContext>,
/// Number of ticks before channel is force closed if closing_signed negotiation stalls.
timer_ticks: i32,
}

/// The number of ticks before the channel is forced closed if
/// no progress on closing_signed negotiation is being made.
/// An unprogressed channel that exceeds this limit will be abandoned.
pub(crate) const UNPROGRESS_CLOSING_SIGNED_NEGOTIATION_AGE_LIMIT_TICKS: i32 = 1 * TICKS_PER_MINUTE as i32;

#[cfg(any(test, fuzzing))]
struct CommitmentTxInfoCached {
fee: u64,
Expand Down Expand Up @@ -5709,12 +5717,12 @@ impl<SP: Deref> Channel<SP> where

/// Checks if the closing_signed negotiation is making appropriate progress, possibly returning
/// an Err if no progress is being made and the channel should be force-closed instead.
/// Should be called on a one-minute timer.
/// Should be called on a one-second timer.
pub fn timer_check_closing_negotiation_progress(&mut self) -> Result<(), ChannelError> {
if self.closing_negotiation_ready() {
if self.context.closing_signed_in_flight {
return Err(ChannelError::Close("closing_signed negotiation failed to finish within two timer ticks".to_owned()));
} else {
} else if { self.timer_ticks -= 1 ; self.timer_ticks } <= 0 {
self.context.closing_signed_in_flight = true;
}
}
Expand Down Expand Up @@ -7790,6 +7798,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
context: self.context,
#[cfg(any(dual_funding, splicing))]
dual_funding_channel_context: None,
timer_ticks: UNPROGRESS_CLOSING_SIGNED_NEGOTIATION_AGE_LIMIT_TICKS,
};

let need_channel_ready = channel.check_get_channel_ready(0).is_some();
Expand Down Expand Up @@ -8080,6 +8089,7 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
context: self.context,
#[cfg(any(dual_funding, splicing))]
dual_funding_channel_context: None,
timer_ticks: UNPROGRESS_CLOSING_SIGNED_NEGOTIATION_AGE_LIMIT_TICKS,
};
let need_channel_ready = channel.check_get_channel_ready(0).is_some();
channel.monitor_updating_paused(false, false, need_channel_ready, Vec::new(), Vec::new(), Vec::new());
Expand Down Expand Up @@ -9389,6 +9399,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
},
#[cfg(any(dual_funding, splicing))]
dual_funding_channel_context: None,
timer_ticks: UNPROGRESS_CLOSING_SIGNED_NEGOTIATION_AGE_LIMIT_TICKS,
})
}
}
Expand Down
12 changes: 8 additions & 4 deletions lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -957,7 +957,7 @@ pub(super) struct InboundChannelRequest {

/// The number of ticks that may elapse while we're waiting for an unaccepted inbound channel to be
/// accepted. An unaccepted channel that exceeds this limit will be abandoned.
const UNACCEPTED_INBOUND_CHANNEL_AGE_LIMIT_TICKS: i32 = 2;
const UNACCEPTED_INBOUND_CHANNEL_AGE_LIMIT_TICKS: i32 = 2 * TICKS_PER_MINUTE as i32;

/// Stores a PaymentSecret and any other data we may need to validate an inbound payment is
/// actually ours and not some duplicate HTLC sent to us by a node along the route.
Expand Down Expand Up @@ -2259,16 +2259,20 @@ const CHECK_CLTV_EXPIRY_SANITY: u32 = MIN_CLTV_EXPIRY_DELTA as u32 - LATENCY_GRA
#[allow(dead_code)]
const CHECK_CLTV_EXPIRY_SANITY_2: u32 = MIN_CLTV_EXPIRY_DELTA as u32 - LATENCY_GRACE_PERIOD_BLOCKS - 2*CLTV_CLAIM_BUFFER;

/// Constant that keeps track of the number of times [`ChannelManager::timer_tick_occurred`]
/// is called every minute.
pub const TICKS_PER_MINUTE: u8 = 60;

/// The number of ticks of [`ChannelManager::timer_tick_occurred`] until expiry of incomplete MPPs
pub(crate) const MPP_TIMEOUT_TICKS: u8 = 3;
pub(crate) const MPP_TIMEOUT_TICKS: u8 = 3 * TICKS_PER_MINUTE;

/// The number of ticks of [`ChannelManager::timer_tick_occurred`] where a peer is disconnected
/// until we mark the channel disabled and gossip the update.
pub(crate) const DISABLE_GOSSIP_TICKS: u8 = 10;
pub(crate) const DISABLE_GOSSIP_TICKS: u16 = 10 * TICKS_PER_MINUTE as u16;

/// The number of ticks of [`ChannelManager::timer_tick_occurred`] where a peer is connected until
/// we mark the channel enabled and gossip the update.
pub(crate) const ENABLE_GOSSIP_TICKS: u8 = 5;
pub(crate) const ENABLE_GOSSIP_TICKS: u16 = 5 * TICKS_PER_MINUTE as u16;

/// The maximum number of unfunded channels we can have per-peer before we start rejecting new
/// (inbound) ones. The number of peers with unfunded channels is limited separately in
Expand Down
6 changes: 4 additions & 2 deletions lightning/src/ln/shutdown_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
//! Tests of our shutdown and closing_signed negotiation logic as well as some assorted force-close
//! handling tests.

use crate::ln::channel::UNPROGRESS_CLOSING_SIGNED_NEGOTIATION_AGE_LIMIT_TICKS;
use crate::sign::{EntropySource, SignerProvider};
use crate::chain::ChannelMonitorUpdateStatus;
use crate::chain::transaction::OutPoint;
Expand Down Expand Up @@ -1161,8 +1162,9 @@ fn do_test_closing_signed_reinit_timeout(timeout_step: TimeoutStep) {
assert_eq!(nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1);
}

nodes[1].node.timer_tick_occurred();
nodes[1].node.timer_tick_occurred();
for _ in 0..UNPROGRESS_CLOSING_SIGNED_NEGOTIATION_AGE_LIMIT_TICKS + 1 {
nodes[1].node.timer_tick_occurred();
}

let txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
assert_eq!(txn.len(), 1);
Expand Down
Loading