Skip to content

Commit

Permalink
Add confirmed state for sub channel
Browse files Browse the repository at this point in the history
  • Loading branch information
Tibo-lg committed Mar 27, 2023
1 parent eb22d66 commit 89f36af
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 15 deletions.
12 changes: 9 additions & 3 deletions dlc-manager/src/sub_channel_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1371,7 +1371,7 @@ where

offered_sub_channel.counter_base_points = Some(accept_points);

offered_sub_channel.state = SubChannelState::Signed(signed_sub_channel);
offered_sub_channel.state = SubChannelState::Confirmed(signed_sub_channel);

self.dlc_channel_manager.get_store().upsert_channel(
Channel::Signed(signed_channel),
Expand Down Expand Up @@ -1550,10 +1550,10 @@ where
sub_channel_finalize: &SubChannelFinalize,
counter_party: &PublicKey,
) -> Result<(), Error> {
let (signed_sub_channel, _) = get_sub_channel_in_state!(
let (mut signed_sub_channel, state) = get_sub_channel_in_state!(
self.dlc_channel_manager,
sub_channel_finalize.channel_id,
Signed,
Confirmed,
Some(*counter_party)
)?;

Expand Down Expand Up @@ -1595,6 +1595,12 @@ where
Some(Contract::Confirmed(contract)),
)?;

signed_sub_channel.state = SubChannelState::Signed(state);

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

Ok(())
}

Expand Down
6 changes: 5 additions & 1 deletion dlc-manager/src/subchannel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ impl SubChannel {
match &self.state {
SubChannelState::Offered(_) => Some(temporary_channel_id),
SubChannelState::Accepted(a) => Some(a.get_dlc_channel_id(temporary_channel_id, index)),
SubChannelState::Signed(s) => Some(s.get_dlc_channel_id(temporary_channel_id, index)),
SubChannelState::Signed(s) | SubChannelState::Confirmed(s) => {
Some(s.get_dlc_channel_id(temporary_channel_id, index))
}
SubChannelState::Closing(c) => Some(
c.signed_sub_channel
.get_dlc_channel_id(temporary_channel_id, index),
Expand Down Expand Up @@ -104,6 +106,8 @@ pub enum SubChannelState {
Offered(OfferedSubChannel),
/// The sub channel was accepted.
Accepted(AcceptedSubChannel),
/// The sub channel was confirmed.
Confirmed(SignedSubChannel),
/// The sub channel transactions have been signed.
Signed(SignedSubChannel),
/// The sub channel is closing.
Expand Down
21 changes: 11 additions & 10 deletions dlc-manager/src/subchannel/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,18 @@ impl_dlc_writeable!(SubChannel, {
impl_dlc_writeable_enum!(SubChannelState,
(0, Offered),
(1, Accepted),
(2, Signed),
(3, Closing),
(4, CloseOffered),
(5, CloseAccepted),
(6, CloseConfirmed),
(7, ClosedPunished)
(2, Confirmed),
(3, Signed),
(4, Closing),
(5, CloseOffered),
(6, CloseAccepted),
(7, CloseConfirmed),
(8, ClosedPunished)
;;;
(8, OnChainClosed),
(9, CounterOnChainClosed),
(10, OffChainClosed),
(11, Rejected)
(9, OnChainClosed),
(10, CounterOnChainClosed),
(11, OffChainClosed),
(12, Rejected)
);

impl_dlc_writeable!(OfferedSubChannel, { (per_split_point, writeable) });
Expand Down
9 changes: 8 additions & 1 deletion dlc-manager/tests/ln_dlc_channel_execution_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,9 @@ fn ln_dlc_test(test_path: TestPath) {
CollaborativelyClosed
);

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

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

if let TestPath::SplitCheat = test_path {
Expand Down Expand Up @@ -1345,7 +1348,7 @@ fn offer_sub_channel(
.unwrap()
.unwrap();

assert_sub_channel_state!(alice_node.sub_channel_manager, channel_id, Signed);
assert_sub_channel_state!(alice_node.sub_channel_manager, channel_id, Confirmed);

alice_node.process_events();
let finalize = bob_node
Expand All @@ -1357,10 +1360,14 @@ fn offer_sub_channel(
assert_sub_channel_state!(bob_node.sub_channel_manager, channel_id, Signed);

bob_node.process_events();
assert_sub_channel_state!(alice_node.sub_channel_manager, channel_id, Confirmed);
alice_node
.sub_channel_manager
.on_sub_channel_message(&finalize, &bob_node.channel_manager.get_our_node_id())
.unwrap();

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

alice_node.process_events();
}

Expand Down
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 @@ -154,6 +154,7 @@ convertible_enum!(
enum SubChannelPrefix {;
Offered = 1,
Accepted,
Confirmed,
Signed,
Closing,
OnChainClosed,
Expand Down

0 comments on commit 89f36af

Please sign in to comment.