Skip to content

Commit

Permalink
feat: Add is offer flag to collaborative close offered channel
Browse files Browse the repository at this point in the history
This is needed in case of a disconnect to know who offered the collaborative close. or in other words who needs to accept the collab close offer.
  • Loading branch information
holzeis committed Feb 9, 2024
1 parent 0786724 commit ba68a7b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
2 changes: 1 addition & 1 deletion dlc-manager/src/channel/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl_dlc_writeable_enum!(
(8, RenewConfirmed, {(contract_id, writeable), (offer_per_update_point, writeable), (accept_per_update_point, writeable), (buffer_transaction, writeable), (buffer_script_pubkey, writeable), (offer_buffer_adaptor_signature, {cb_writeable, write_ecdsa_adaptor_signature, read_ecdsa_adaptor_signature}), (timeout, writeable), (own_payout, writeable), (total_collateral, writeable)}),
(10, RenewFinalized, {(contract_id, writeable), (prev_offer_per_update_point, writeable), (buffer_transaction, writeable), (buffer_script_pubkey, writeable), (offer_buffer_adaptor_signature, {cb_writeable, write_ecdsa_adaptor_signature, read_ecdsa_adaptor_signature}), (accept_buffer_adaptor_signature, {cb_writeable, write_ecdsa_adaptor_signature, read_ecdsa_adaptor_signature}), (timeout, writeable), (own_payout, writeable), (total_collateral, writeable)}),
(9, Closing, {(buffer_transaction, writeable), (contract_id, writeable), (is_initiator, writeable)}),
(11, CollaborativeCloseOffered, { (counter_payout, writeable), (offer_signature, writeable), (close_tx, writeable), (timeout, writeable) })
(11, CollaborativeCloseOffered, { (counter_payout, writeable), (offer_signature, writeable), (close_tx, writeable), (timeout, writeable), (is_offer, writeable) })
;;
);

Expand Down
2 changes: 2 additions & 0 deletions dlc-manager/src/channel/signed_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,8 @@ typed_enum!(
/// The UNIX epoch at which the counter party will be considered
/// unresponsive and the channel will be forced closed.
timeout: u64,
/// Indicates whether the local party offered the collaborative close or not.
is_offer: bool,
},
},
/// Enum automatically generated associating a number to each signed channel
Expand Down
12 changes: 10 additions & 2 deletions dlc-manager/src/channel_updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2227,6 +2227,7 @@ where
offer_signature: close_signature,
close_tx: close_tx.clone(),
timeout: time.unix_time_now() + super::manager::PEER_TIMEOUT,
is_offer: true,
};
std::mem::swap(&mut state, &mut signed_channel.state);
signed_channel.roll_back_state = Some(state);
Expand Down Expand Up @@ -2285,6 +2286,7 @@ where
offer_signature: close_offer.close_signature,
close_tx,
timeout: time.unix_time_now() + peer_timeout,
is_offer: false
};

std::mem::swap(&mut state, &mut signed_channel.state);
Expand All @@ -2303,12 +2305,18 @@ pub fn accept_collaborative_close_offer<C: Signing, S: Deref>(
where
S::Target: Signer,
{
let (offer_signature, close_tx) = get_signed_channel_state!(
let (offer_signature, close_tx, is_offer) = get_signed_channel_state!(
signed_channel,
CollaborativeCloseOffered,
offer_signature | close_tx
offer_signature | close_tx, is_offer
)?;

if *is_offer {
return Err(Error::InvalidState(
"Cannot accept own collaborative close offer".to_string(),
));
}

let fund_out_amount = signed_channel.fund_tx.output[signed_channel.fund_output_index].value;

let own_fund_sk = signer.get_secret_key_for_pubkey(&signed_channel.own_params.fund_pubkey)?;
Expand Down
30 changes: 27 additions & 3 deletions dlc-manager/tests/channel_execution_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ enum TestPath {
BufferCheat,
RenewedClose,
SettleCheat,
CollaborativeClose,
CollaborativeClose{
accept_own_offer: bool
},
SettleRenewSettle,
SettleOfferTimeout,
SettleAcceptTimeout,
Expand Down Expand Up @@ -170,7 +172,16 @@ fn channel_settle_cheat_test() {
fn channel_collaborative_close_test() {
channel_execution_test(
get_enum_test_params(1, 1, None),
TestPath::CollaborativeClose,
TestPath::CollaborativeClose{ accept_own_offer: false},
);
}

#[test]
#[ignore]
fn channel_collaborative_close_own_offer_test() {
channel_execution_test(
get_enum_test_params(1, 1, None),
TestPath::CollaborativeClose{ accept_own_offer: true},
);
}

Expand Down Expand Up @@ -578,14 +589,15 @@ fn channel_execution_test(test_params: TestParams, path: TestPath) {
TestPath::Close => {
close_established_channel(first, second, channel_id, &generate_blocks);
}
TestPath::CollaborativeClose => {
TestPath::CollaborativeClose { accept_own_offer } => {
collaborative_close(
first,
first_send,
second,
channel_id,
second_receive,
&generate_blocks,
accept_own_offer
);
}
TestPath::SettleOfferTimeout
Expand Down Expand Up @@ -1204,6 +1216,7 @@ fn collaborative_close<F: Fn(u64)>(
channel_id: DlcChannelId,
sync_receive: &Receiver<()>,
generate_blocks: &F,
accept_own_offer: bool
) {
let contract_id = get_established_channel_contract_id(&first, &channel_id);
let close_offer = first
Expand All @@ -1221,6 +1234,17 @@ fn collaborative_close<F: Fn(u64)>(
assert_channel_state!(first, channel_id, Signed, CollaborativeCloseOffered);
assert_channel_state!(second, channel_id, Signed, CollaborativeCloseOffered);

if accept_own_offer {
if let Err(e) = first.lock().unwrap().accept_collaborative_close(&channel_id) {
assert_eq!("Invalid state: Cannot accept own collaborative close offer", e.to_string());
} else {
panic!("It should not be possible to accept own collaborative close offer");
}

return;
}


second
.lock()
.unwrap()
Expand Down

0 comments on commit ba68a7b

Please sign in to comment.