From 9abef5c0d48352df6a9c34e47b0cb35ee74aa210 Mon Sep 17 00:00:00 2001 From: Richard Holzeis Date: Wed, 1 May 2024 21:54:55 +0200 Subject: [PATCH] refactor: Split up propose dlc channel update into propose reopen or resize and rollover --- coordinator/src/node/rollover.rs | 2 +- coordinator/src/trade/mod.rs | 12 ++--- crates/xxi-node/src/node/dlc_channel.rs | 65 ++++++++++++++++++++---- crates/xxi-node/src/tests/dlc_channel.rs | 4 +- 4 files changed, 63 insertions(+), 20 deletions(-) diff --git a/coordinator/src/node/rollover.rs b/coordinator/src/node/rollover.rs index 57806db5e..608f31c99 100644 --- a/coordinator/src/node/rollover.rs +++ b/coordinator/src/node/rollover.rs @@ -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()); diff --git a/coordinator/src/trade/mod.rs b/coordinator/src/trade/mod.rs index 89f2d256a..2397a00b9 100644 --- a/coordinator/src/trade/mod.rs +++ b/coordinator/src/trade/mod.rs @@ -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( @@ -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( diff --git a/crates/xxi-node/src/node/dlc_channel.rs b/crates/xxi-node/src/node/dlc_channel.rs index 884999307..d8ca4c55e 100644 --- a/crates/xxi-node/src/node/dlc_channel.rs +++ b/crates/xxi-node/src/node/dlc_channel.rs @@ -291,15 +291,15 @@ impl, + filled_with: commons::FilledWith, dlc_channel_id: &DlcChannelId, contract_input: ContractInput, protocol_id: ReferenceId, ) -> Result { - 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; @@ -315,18 +315,61 @@ impl 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 { + 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), }); diff --git a/crates/xxi-node/src/tests/dlc_channel.rs b/crates/xxi-node/src/tests/dlc_channel.rs index d102b1e35..2f5b86c5c 100644 --- a/crates/xxi-node/src/tests/dlc_channel.rs +++ b/crates/xxi-node/src/tests/dlc_channel.rs @@ -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(),