Skip to content

Commit

Permalink
chore(channel): Ignore invalid collab close offers
Browse files Browse the repository at this point in the history
  • Loading branch information
holzeis committed Mar 5, 2024
1 parent fad15d4 commit b38188b
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 22 deletions.
14 changes: 9 additions & 5 deletions coordinator/src/dlc_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,20 +118,24 @@ impl DlcHandler {
channel_id,
state:
SignedChannelState::CollaborativeCloseOffered {
is_offer: false, ..
is_offer: false,
counter_payout: offered_payout,
..
},
..
}) = signed_dlc_channels
.iter()
.find(|c| c.counter_party == to_secp_pk_29(peer))
{
tracing::info!("Accepting pending dlc channel close offer.");
tracing::info!("Processing pending dlc channel close offer.");
// Pending dlc channel close offer with the intend to close the dlc channel
// on-chain

// TODO(bonomat): we should verify that the proposed amount is acceptable
self.node
.accept_dlc_channel_collaborative_close(channel_id)?;
self.node.process_dlc_channel_collaborative_close(
&peer,
channel_id,
*offered_payout,
)?;

return Ok(());
}
Expand Down
8 changes: 5 additions & 3 deletions coordinator/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,11 @@ impl Node {
"Received an offer to collaboratively close a channel"
);

// TODO(bonomat): we should verify that the proposed amount is acceptable
self.inner
.accept_dlc_channel_collaborative_close(&close_offer.channel_id)?;
self.inner.process_dlc_channel_collaborative_close(
&node_id,
&close_offer.channel_id,
close_offer.counter_payout,
)?;
}
ChannelMessage::Accept(AcceptChannel {
temporary_channel_id,
Expand Down
28 changes: 24 additions & 4 deletions crates/ln-dlc-node/src/node/dlc_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,33 @@ impl<D: BdkStorage, S: TenTenOneStorage + 'static, N: LnDlcStorage + Sync + Send
.await?
}

pub fn accept_dlc_channel_collaborative_close(&self, channel_id: &DlcChannelId) -> Result<()> {
pub fn process_dlc_channel_collaborative_close(
&self,
trader_id: &PublicKey,
channel_id: &DlcChannelId,
offered_payout: u64,
) -> Result<()> {
let channel_id_hex = hex::encode(channel_id);

tracing::info!(channel_id = %channel_id_hex, "Accepting DLC channel collaborative close offer");
let channel = self.get_dlc_channel_by_id(channel_id)?;

let dlc_manager = self.dlc_manager.clone();
dlc_manager.accept_collaborative_close(channel_id)?;
if let Channel::Signed(SignedChannel {
state: SignedChannelState::Settled { own_payout, .. },
..
}) = channel
{
if own_payout != offered_payout {
tracing::warn!(%trader_id, channel_id = %channel_id_hex, own_payout, offered_payout,
"Received DLC channel collaborative close offer with an invalid payout");

// TODO(holzeis): Implement reject collaborative close offer flow https://github.com/get10101/10101/issues/2019
tracing::debug!(%trader_id, channel_id = %channel_id_hex, "Ignoring dlc channel collaborative close offer");
} else {
tracing::info!(%trader_id, channel_id = %channel_id_hex, "Accepting DLC channel collaborative close offer");
let dlc_manager = self.dlc_manager.clone();
dlc_manager.accept_collaborative_close(channel_id)?;
}
}

Ok(())
}
Expand Down
15 changes: 13 additions & 2 deletions crates/ln-dlc-node/src/tests/dlc_channel.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::bitcoin_conversion::to_secp_pk_29;
use crate::bitcoin_conversion::to_secp_pk_30;
use crate::node::dlc_channel::estimated_dlc_channel_fee_reserve;
use crate::node::InMemoryStore;
use crate::node::Node;
Expand All @@ -12,6 +13,7 @@ use crate::tests::new_reference_id;
use crate::tests::wait_until;
use bitcoin::Amount;
use dlc_manager::channel::signed_channel::SignedChannel;
use dlc_manager::channel::signed_channel::SignedChannelState;
use dlc_manager::channel::signed_channel::SignedChannelStateType;
use dlc_manager::contract::Contract;
use dlc_manager::Storage;
Expand Down Expand Up @@ -166,8 +168,17 @@ async fn can_open_and_collaboratively_close_channel() {

tracing::debug!("Accepting collaborative close offer");

app.accept_dlc_channel_collaborative_close(&coordinator_signed_channel.channel_id)
.unwrap();
let offered_payout = match coordinator_signed_channel.state {
SignedChannelState::Settled { counter_payout, .. } => counter_payout,
_ => panic!("Only a channel in state Settled can be closed collaboratively"),
};

app.process_dlc_channel_collaborative_close(
&to_secp_pk_30(coordinator_signed_channel.counter_party),
&coordinator_signed_channel.channel_id,
offered_payout,
)
.unwrap();

wait_until(Duration::from_secs(10), || async {
mine(1).await.unwrap();
Expand Down
13 changes: 8 additions & 5 deletions mobile/native/src/dlc_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,18 +185,21 @@ impl DlcHandler {
channel_id,
state:
SignedChannelState::CollaborativeCloseOffered {
is_offer: false, ..
is_offer: false,
counter_payout: offered_payout,
..
},
..
} => {
tracing::info!("Accepting pending dlc channel close offer.");
// Pending dlc channel close offer with the intend to close the dlc channel
// on-chain

// TODO(bonomat): we should verify that the proposed amount is acceptable
self.node
.inner
.accept_dlc_channel_collaborative_close(channel_id)?;
self.node.inner.process_dlc_channel_collaborative_close(
&peer,
channel_id,
*offered_payout,
)?;

return Ok(());
}
Expand Down
8 changes: 5 additions & 3 deletions mobile/native/src/ln_dlc/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,9 +323,11 @@ impl Node {
"Received an offer to collaboratively close a channel"
);

// TODO(bonomat): we should verify that the proposed amount is acceptable
self.inner
.accept_dlc_channel_collaborative_close(&close_offer.channel_id)?;
self.inner.process_dlc_channel_collaborative_close(
&node_id,
&close_offer.channel_id,
close_offer.counter_payout,
)?;
}
_ => (),
}
Expand Down

0 comments on commit b38188b

Please sign in to comment.