Skip to content

Commit

Permalink
fix: Set timeout on RenewOffered state
Browse files Browse the repository at this point in the history
Otherwise the channel gets force closed immediately if the periodic check happens to run before the channel has advanced to the next state.
  • Loading branch information
holzeis committed Jan 31, 2024
1 parent 661e170 commit 8c13abf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
10 changes: 7 additions & 3 deletions dlc-manager/src/channel_updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1483,10 +1483,14 @@ where
/// Update the state of the given [`SignedChannel`] from the given [`RenewOffer`].
/// Expects the channel to be in one of [`SignedChannelState::Settled`] or
/// [`SignedChannelState::Established`] state.
pub fn on_renew_offer(
pub fn on_renew_offer<T: Deref>(
signed_channel: &mut SignedChannel,
renew_offer: &RenewOffer,
) -> Result<OfferedContract, Error> {
peer_timeout: u64,
time: &T,
) -> Result<OfferedContract, Error> where
T::Target: Time,
{
if let SignedChannelState::Settled { .. } | SignedChannelState::Established { .. } =
signed_channel.state
{
Expand Down Expand Up @@ -1523,7 +1527,7 @@ pub fn on_renew_offer(
counter_payout: renew_offer.counter_payout,
offer_next_per_update_point: renew_offer.next_per_update_point,
is_offer: false,
timeout: 0,
timeout: time.unix_time_now() + peer_timeout,
};

std::mem::swap(&mut signed_channel.state, &mut state);
Expand Down
12 changes: 11 additions & 1 deletion dlc-manager/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ macro_rules! check_for_timed_out_channels {
} else {
None
};

log::warn!("Force closing channel that timed out. {} < {}", timeout, $manager.time.unix_time_now());
match $manager.force_close_channel_internal(channel, sub_channel, true) {
Err(e) => error!("Error force closing channel {}", e),
_ => {}
Expand Down Expand Up @@ -227,6 +229,8 @@ where
msg: &DlcMessage,
counter_party: PublicKey,
) -> Result<Option<DlcMessage>, Error> {
log::info!("===========> Received dlc message IN RUST DLC!!!!");

match msg {
DlcMessage::OnChain(on_chain) => match on_chain {
OnChainMessage::Offer(o) => {
Expand Down Expand Up @@ -1616,7 +1620,7 @@ where
}

let offered_contract =
crate::channel_updater::on_renew_offer(&mut signed_channel, renew_offer)?;
crate::channel_updater::on_renew_offer(&mut signed_channel, renew_offer,PEER_TIMEOUT, &self.time)?;

self.store.create_contract(&offered_contract)?;
self.store
Expand Down Expand Up @@ -2375,6 +2379,8 @@ where
buffer_transaction,
..
} => {
warn!("Force closing established channel with id: {}", channel.channel_id.to_hex());

let counter_buffer_adaptor_signature = *counter_buffer_adaptor_signature;
let buffer_transaction = buffer_transaction.clone();
self.initiate_unilateral_close_established_channel(
Expand All @@ -2390,6 +2396,8 @@ where
offer_buffer_adaptor_signature,
..
} => {
warn!("Force closing renew finalized channel with id: {}", channel.channel_id.to_hex());

let offer_buffer_adaptor_signature = *offer_buffer_adaptor_signature;
let buffer_transaction = buffer_transaction.clone();
self.initiate_unilateral_close_established_channel(
Expand All @@ -2401,6 +2409,8 @@ where
)
}
SignedChannelState::Settled { .. } => {
warn!("Force closing settled channel with id: {}", channel.channel_id.to_hex());

self.close_settled_channel(channel, sub_channel, is_initiator)
}
SignedChannelState::SettledOffered { .. }
Expand Down

0 comments on commit 8c13abf

Please sign in to comment.