Skip to content

Commit

Permalink
refactor: Split up propose dlc channel update into propose reopen or …
Browse files Browse the repository at this point in the history
…resize and rollover
  • Loading branch information
holzeis committed May 2, 2024
1 parent ce3f9f7 commit 9abef5c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 20 deletions.
2 changes: 1 addition & 1 deletion coordinator/src/node/rollover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ impl Node {

let contract_id = self
.inner
.propose_dlc_channel_update(None, dlc_channel_id, contract_input, protocol_id.into())
.propose_rollover(dlc_channel_id, contract_input, protocol_id.into())
.await?;

let protocol_executor = dlc_protocol::DlcProtocolExecutor::new(self.pool.clone());
Expand Down
12 changes: 6 additions & 6 deletions coordinator/src/trade/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,14 +544,14 @@ impl TradeExecutor {
let temporary_contract_id = self
.node
.inner
.propose_dlc_channel_update(
Some(trade_params.filled_with.clone()),
.propose_reopen_or_resize(
trade_params.filled_with.clone(),
&dlc_channel_id,
contract_input,
protocol_id.into(),
)
.await
.context("Could not propose DLC channel update")?;
.context("Could not propose reopen DLC channel update")?;

let protocol_executor = dlc_protocol::DlcProtocolExecutor::new(self.node.pool.clone());
protocol_executor.start_dlc_protocol(
Expand Down Expand Up @@ -717,14 +717,14 @@ impl TradeExecutor {
let temporary_contract_id = self
.node
.inner
.propose_dlc_channel_update(
Some(trade_params.filled_with.clone()),
.propose_reopen_or_resize(
trade_params.filled_with.clone(),
&dlc_channel_id,
contract_input,
protocol_id.into(),
)
.await
.context("Could not propose DLC channel update")?;
.context("Could not propose resize DLC channel update")?;

let protocol_executor = dlc_protocol::DlcProtocolExecutor::new(self.node.pool.clone());
protocol_executor.start_dlc_protocol(
Expand Down
65 changes: 54 additions & 11 deletions crates/xxi-node/src/node/dlc_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,15 +291,15 @@ impl<D: BdkStorage, S: TenTenOneStorage + 'static, N: LnDlcStorage + Sync + Send
}

/// Propose an update to the DLC channel based on the provided [`ContractInput`]. A
/// [`RenewOffer`] is sent to the counterparty, kickstarting the renew protocol.
pub async fn propose_dlc_channel_update(
/// [`TenTenOneRenewOffer`] is sent to the counterparty, kickstarting the dlc renew protocol.
pub async fn propose_reopen_or_resize(
&self,
filled_with: Option<commons::FilledWith>,
filled_with: commons::FilledWith,
dlc_channel_id: &DlcChannelId,
contract_input: ContractInput,
protocol_id: ReferenceId,
) -> Result<ContractId> {
tracing::info!(channel_id = %hex::encode(dlc_channel_id), "Proposing a DLC channel update");
tracing::info!(channel_id = %hex::encode(dlc_channel_id), "Proposing a DLC channel reopen or resize");
spawn_blocking({
let dlc_manager = self.dlc_manager.clone();
let dlc_channel_id = *dlc_channel_id;
Expand All @@ -315,18 +315,61 @@ impl<D: BdkStorage, S: TenTenOneStorage + 'static, N: LnDlcStorage + Sync + Send
Some(protocol_id),
)?;

// TODO(holzeis): Separate into different functions for renew and rollover.
let msg = match filled_with {
Some(filled_with) => TenTenOneMessage::RenewOffer(TenTenOneRenewOffer {
event_handler.publish(NodeEvent::StoreDlcMessage {
msg: TenTenOneMessage::RenewOffer(TenTenOneRenewOffer {
renew_offer,
filled_with,
}),
// if no filled with is provided we are rolling over.
None => TenTenOneMessage::RolloverOffer(TenTenOneRolloverOffer { renew_offer }),
};
peer: to_secp_pk_30(counterparty_pubkey),
});

let offered_contracts = dlc_manager.get_store().get_contract_offers()?;

// We assume that the first `OfferedContract` we find here is the one we just
// proposed when renewing the DLC channel.
//
// TODO: Change `renew_offer` API to return the `temporary_contract_id`, like
// `offer_channel` does.
let offered_contract = offered_contracts
.iter()
.find(|contract| contract.counter_party == counterparty_pubkey)
.context(
"Could not find offered contract after proposing DLC channel update",
)?;

Ok(offered_contract.id)
}
})
.await
.map_err(|e| anyhow!("{e:#}"))?
}

/// Propose an update to the DLC channel based on the provided [`ContractInput`]. A
/// [`TenTenOneRolloverOffer`] is sent to the counterparty, kickstarting the dlc renew protocol.
pub async fn propose_rollover(
&self,
dlc_channel_id: &DlcChannelId,
contract_input: ContractInput,
protocol_id: ReferenceId,
) -> Result<ContractId> {
tracing::info!(channel_id = %hex::encode(dlc_channel_id), "Proposing a DLC channel rollover");
spawn_blocking({
let dlc_manager = self.dlc_manager.clone();
let dlc_channel_id = *dlc_channel_id;
let event_handler = self.event_handler.clone();
move || {
// Not actually needed. See https://github.com/p2pderivatives/rust-dlc/issues/149.
let counter_payout = 0;

let (renew_offer, counterparty_pubkey) = dlc_manager.renew_offer(
&dlc_channel_id,
counter_payout,
&contract_input,
Some(protocol_id),
)?;

event_handler.publish(NodeEvent::StoreDlcMessage {
msg,
msg: TenTenOneMessage::RolloverOffer(TenTenOneRolloverOffer { renew_offer }),
peer: to_secp_pk_30(counterparty_pubkey),
});

Expand Down
4 changes: 2 additions & 2 deletions crates/xxi-node/src/tests/dlc_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ async fn can_open_and_settle_offchain() {
let contract_input = dummy_contract_input(15_000, 5_000, oracle_pk, None);

coordinator
.propose_dlc_channel_update(
Some(dummy_filled_with()),
.propose_reopen_or_resize(
dummy_filled_with(),
&coordinator_signed_channel.channel_id,
contract_input,
new_reference_id(),
Expand Down

0 comments on commit 9abef5c

Please sign in to comment.