Skip to content

Commit

Permalink
Add possibility to reject sub channel offer and close
Browse files Browse the repository at this point in the history
  • Loading branch information
Tibo-lg committed Mar 22, 2023
1 parent e0f24fc commit 15766af
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 14 deletions.
88 changes: 85 additions & 3 deletions dlc-manager/src/sub_channel_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ use dlc_messages::{
channel::{AcceptChannel, OfferChannel},
oracle_msgs::OracleAnnouncement,
sub_channel::{
SubChannelAccept, SubChannelCloseAccept, SubChannelCloseConfirm, SubChannelCloseFinalize,
SubChannelCloseOffer, SubChannelConfirm, SubChannelFinalize, SubChannelOffer,
Reject, SubChannelAccept, SubChannelCloseAccept, SubChannelCloseConfirm,
SubChannelCloseFinalize, SubChannelCloseOffer, SubChannelConfirm, SubChannelFinalize,
SubChannelOffer,
},
FundingSignatures, SubChannelMessage,
};
Expand Down Expand Up @@ -179,7 +180,10 @@ where
self.on_sub_channel_close_finalize(f, sender)?;
Ok(None)
}
SubChannelMessage::CloseReject(_) => todo!(),
SubChannelMessage::Reject(r) => {
self.on_sub_channel_reject(r, sender)?;
Ok(None)
}
}
}

Expand Down Expand Up @@ -940,6 +944,42 @@ where
Ok((close_accept, sub_channel.counter_party))
}

/// Reject an offer to establish a sub channel.
pub fn reject_sub_channel_offer(&self, channel_id: ChannelId) -> Result<Reject, Error> {
let (mut sub_channel, _) = get_sub_channel_in_state!(
self.dlc_channel_manager,
channel_id,
Offered,
None::<PublicKey>
)?;

sub_channel.state = SubChannelState::Rejected;

self.dlc_channel_manager
.get_store()
.upsert_sub_channel(&sub_channel)?;

Ok(Reject { channel_id })
}

/// Reject an offer to collaboratively close a sub channel off chain.
pub fn reject_sub_channel_close_offer(&self, channel_id: ChannelId) -> Result<Reject, Error> {
let (mut sub_channel, state) = get_sub_channel_in_state!(
self.dlc_channel_manager,
channel_id,
CloseOffered,
None::<PublicKey>
)?;

sub_channel.state = SubChannelState::Signed(state.signed_subchannel);

self.dlc_channel_manager
.get_store()
.upsert_sub_channel(&sub_channel)?;

Ok(Reject { channel_id })
}

fn on_subchannel_offer(
&self,
sub_channel_offer: &SubChannelOffer,
Expand Down Expand Up @@ -2148,6 +2188,48 @@ where

Ok(())
}

fn on_sub_channel_reject(&self, reject: &Reject, peer_id: &PublicKey) -> Result<(), Error> {
let sub_channel = self
.dlc_channel_manager
.get_store()
.get_sub_channel(reject.channel_id)?;

match sub_channel {
None => {
return Err(Error::InvalidParameters(format!(
"No such subchannel: {:?}",
reject.channel_id
)))
}
Some(mut s) => {
if s.counter_party != *peer_id {
return Err(Error::InvalidParameters(
"Message from invalid peer".to_string(),
));
}
match s.state {
SubChannelState::Offered(_) => {
s.state = SubChannelState::Rejected;
}
SubChannelState::CloseOffered(o) => {
s.state = SubChannelState::Signed(o.signed_subchannel);
}
_ => {
return Err(Error::InvalidParameters(
"Not in a state to be rejected".to_string(),
))
}
};

self.dlc_channel_manager
.get_store()
.upsert_sub_channel(&s)?;
}
}

Ok(())
}
}

fn validate_and_get_ln_values_per_party(
Expand Down
2 changes: 2 additions & 0 deletions dlc-manager/src/subchannel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ pub enum SubChannelState {
OffChainClosed,
/// The sub channel was closed by broadcasting a punishment transaction.
ClosedPunished(Txid),
/// An offer to establish a sub channel was rejected.
Rejected,
}

#[derive(Debug, Clone)]
Expand Down
3 changes: 2 additions & 1 deletion dlc-manager/src/subchannel/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ impl_dlc_writeable_enum!(SubChannelState,
;;;
(8, OnChainClosed),
(9, CounterOnChainClosed),
(10, OffChainClosed)
(10, OffChainClosed),
(11, Rejected)
);

impl_dlc_writeable!(OfferedSubChannel, { (per_split_point, writeable) });
Expand Down
78 changes: 76 additions & 2 deletions dlc-manager/tests/ln_dlc_channel_execution_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ enum TestPath {
CheatPostSplitCommit,
OffChainClosed,
SplitCheat,
OfferRejected,
CloseRejected,
}

impl LnDlcParty {
Expand Down Expand Up @@ -523,6 +525,18 @@ fn ln_dlc_split_cheat() {
ln_dlc_test(TestPath::SplitCheat);
}

#[test]
#[ignore]
fn ln_dlc_rejected_offer() {
ln_dlc_test(TestPath::OfferRejected);
}

#[test]
#[ignore]
fn ln_dlc_rejected_close() {
ln_dlc_test(TestPath::CloseRejected);
}

// #[derive(Debug)]
// pub struct TestParams {
// pub oracles: Vec<p2pd_oracle_client::P2PDOracleClient>,
Expand Down Expand Up @@ -737,6 +751,11 @@ fn ln_dlc_test(test_path: TestPath) {
let bob_channel_details = bob_node.channel_manager.list_usable_channels().remove(0);
let channel_id = bob_channel_details.channel_id;

if let TestPath::OfferRejected = test_path {
reject_offer(&test_params, &alice_node, &bob_node, &channel_id);
return;
}

offer_sub_channel(&test_params, &alice_node, &bob_node, &channel_id);

if let TestPath::CheatPreSplitCommit = test_path {
Expand Down Expand Up @@ -848,7 +867,7 @@ fn ln_dlc_test(test_path: TestPath) {

mocks::mock_time::set_time(EVENT_MATURITY as u64);

if let TestPath::OffChainClosed | TestPath::SplitCheat = test_path {
if let TestPath::OffChainClosed | TestPath::SplitCheat | TestPath::CloseRejected = test_path {
if let TestPath::SplitCheat = test_path {
alice_node.dlc_manager.get_store().save();
}
Expand All @@ -869,6 +888,26 @@ fn ln_dlc_test(test_path: TestPath) {
)
.unwrap();

if let TestPath::CloseRejected = test_path {
let reject = bob_node
.sub_channel_manager
.reject_sub_channel_close_offer(channel_id)
.unwrap();

alice_node
.sub_channel_manager
.on_sub_channel_message(
&SubChannelMessage::Reject(reject),
&bob_node.channel_manager.get_our_node_id(),
)
.unwrap();

assert_sub_channel_state!(alice_node.sub_channel_manager, &channel_id, Signed);
assert_sub_channel_state!(bob_node.sub_channel_manager, &channel_id, Signed);

return;
}

let (close_accept, _) = bob_node
.sub_channel_manager
.accept_subchannel_close_offer(&channel_id)
Expand Down Expand Up @@ -1240,7 +1279,7 @@ fn ln_cheated_check<F>(
.any(|x| *x == receive_addr));
}

fn offer_sub_channel(
fn offer_common(
test_params: &TestParams,
alice_node: &LnDlcParty,
bob_node: &LnDlcParty,
Expand Down Expand Up @@ -1279,6 +1318,15 @@ fn offer_sub_channel(
.unwrap();

assert_sub_channel_state!(bob_node.sub_channel_manager, channel_id, Offered);
}

fn offer_sub_channel(
test_params: &TestParams,
alice_node: &LnDlcParty,
bob_node: &LnDlcParty,
channel_id: &ChannelId,
) {
offer_common(test_params, alice_node, bob_node, channel_id);

let (_, accept) = bob_node
.sub_channel_manager
Expand Down Expand Up @@ -1315,3 +1363,29 @@ fn offer_sub_channel(
.unwrap();
alice_node.process_events();
}

fn reject_offer(
test_params: &TestParams,
alice_node: &LnDlcParty,
bob_node: &LnDlcParty,
channel_id: &ChannelId,
) {
offer_common(test_params, alice_node, bob_node, channel_id);

let reject = bob_node
.sub_channel_manager
.reject_sub_channel_offer(*channel_id)
.unwrap();

assert_sub_channel_state!(bob_node.sub_channel_manager, channel_id; Rejected);

alice_node
.sub_channel_manager
.on_sub_channel_message(
&SubChannelMessage::Reject(reject),
&bob_node.channel_manager.get_our_node_id(),
)
.unwrap();

assert_sub_channel_state!(alice_node.sub_channel_manager, channel_id; Rejected);
}
10 changes: 5 additions & 5 deletions dlc-messages/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ use secp256k1_zkp::Verification;
use secp256k1_zkp::{ecdsa::Signature, EcdsaAdaptorSignature, PublicKey, Secp256k1};
use segmentation::{SegmentChunk, SegmentStart};
use sub_channel::{
SubChannelAccept, SubChannelCloseAccept, SubChannelCloseConfirm, SubChannelCloseFinalize,
SubChannelCloseOffer, SubChannelCloseReject, SubChannelConfirm, SubChannelFinalize,
Reject as SubChannelReject, SubChannelAccept, SubChannelCloseAccept, SubChannelCloseConfirm,
SubChannelCloseFinalize, SubChannelCloseOffer, SubChannelConfirm, SubChannelFinalize,
SubChannelOffer,
};

Expand Down Expand Up @@ -100,7 +100,7 @@ impl_type!(SUB_CHANNEL_CLOSE_OFFER, SubChannelCloseOffer, 43042);
impl_type!(SUB_CHANNEL_CLOSE_ACCEPT, SubChannelCloseAccept, 43044);
impl_type!(SUB_CHANNEL_CLOSE_CONFIRM, SubChannelCloseConfirm, 43046);
impl_type!(SUB_CHANNEL_CLOSE_FINALIZE, SubChannelCloseFinalize, 43048);
impl_type!(SUB_CHANNEL_CLOSE_REJECT, SubChannelCloseReject, 43050);
impl_type!(SUB_CHANNEL_REJECT, SubChannelReject, 43050);

#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(
Expand Down Expand Up @@ -560,7 +560,7 @@ pub enum SubChannelMessage {
CloseAccept(SubChannelCloseAccept),
CloseConfirm(SubChannelCloseConfirm),
CloseFinalize(SubChannelCloseFinalize),
CloseReject(SubChannelCloseReject),
Reject(SubChannelReject),
}

macro_rules! impl_type_writeable_for_enum {
Expand Down Expand Up @@ -624,7 +624,7 @@ impl_type_writeable_for_enum!(SubChannelMessage,
CloseAccept,
CloseConfirm,
CloseFinalize,
CloseReject
Reject
});

#[derive(Debug, Clone)]
Expand Down
2 changes: 1 addition & 1 deletion dlc-messages/src/message_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ fn read_dlc_message<R: ::std::io::Read>(
(SUB_CHANNEL_CLOSE_ACCEPT, CloseAccept),
(SUB_CHANNEL_CLOSE_CONFIRM, CloseConfirm),
(SUB_CHANNEL_CLOSE_FINALIZE, CloseFinalize),
(SUB_CHANNEL_CLOSE_REJECT, CloseReject)
(SUB_CHANNEL_REJECT, Reject)
)
)
}
Expand Down
4 changes: 2 additions & 2 deletions dlc-messages/src/sub_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,9 @@ impl_dlc_writeable!(SubChannelCloseFinalize, {
/// A message to reject an offer to collaboratively close a DLC channel embedded within a Lightning
/// channel.
#[derive(Clone, Debug)]
pub struct SubChannelCloseReject {
pub struct Reject {
/// The id of the Lightning channel the message relates to.
pub channel_id: [u8; 32],
}

impl_dlc_writeable!(SubChannelCloseReject, { (channel_id, writeable) });
impl_dlc_writeable!(Reject, { (channel_id, writeable) });
1 change: 1 addition & 0 deletions dlc-sled-storage-provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ convertible_enum!(
CloseConfirmed,
OffChainClosed,
ClosedPunished,
Rejected,
},
SubChannelState
);
Expand Down

0 comments on commit 15766af

Please sign in to comment.