Skip to content

Commit

Permalink
chore(settle): Disregard additional check if contract is actually clo…
Browse files Browse the repository at this point in the history
…sed.

This is the responsibility of `rust-dlc`. Removing this check as it adds unnecessary complexity. We can assume that if `rust-dlc` successfully processed the `ChannelMessage::SettleFinalize` that the contract got closed.
  • Loading branch information
holzeis committed Feb 29, 2024
1 parent 8e79dac commit 4e5e736
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 52 deletions.
47 changes: 30 additions & 17 deletions coordinator/src/dlc_protocol.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::db;
use crate::position::models::PositionState;
use crate::trade::models::NewTrade;
use anyhow::Context;
use anyhow::Result;
use bitcoin::secp256k1::PublicKey;
use diesel::r2d2::ConnectionManager;
Expand Down Expand Up @@ -228,7 +229,7 @@ impl DlcProtocolExecutor {
&self,
protocol_id: ProtocolId,
trader_id: &PublicKey,
contract_id: &ContractId,
contract_id: Option<ContractId>,
channel_id: &DlcChannelId,
) -> Result<()> {
let mut conn = self.pool.get()?;
Expand All @@ -237,29 +238,41 @@ impl DlcProtocolExecutor {

match dlc_protocol.protocol_type {
DlcProtocolType::Open { trade_params }
| DlcProtocolType::Renew { trade_params } => self.finish_trade_dlc_protocol(
conn,
trade_params,
protocol_id,
false,
contract_id,
channel_id,
),
| DlcProtocolType::Renew { trade_params } => {
let contract_id = contract_id
.context("missing contract id")
.map_err(|_| RollbackTransaction)?;
self.finish_trade_dlc_protocol(
conn,
trade_params,
protocol_id,
false,
&contract_id,
channel_id,
)
}
DlcProtocolType::Settle { trade_params } => self.finish_trade_dlc_protocol(
conn,
trade_params,
protocol_id,
true,
contract_id,
channel_id,
),
DlcProtocolType::Rollover { .. } => self.finish_rollover_dlc_protocol(
conn,
trader_id,
protocol_id,
contract_id,
// If the contract got settled, we do not get a new contract id, hence we copy
// the contract id of the settled contract.
&dlc_protocol.contract_id,
channel_id,
),
DlcProtocolType::Rollover { .. } => {
let contract_id = contract_id
.context("missing contract id")
.map_err(|_| RollbackTransaction)?;
self.finish_rollover_dlc_protocol(
conn,
trader_id,
protocol_id,
&contract_id,
channel_id,
)
}
_ => Ok(()),
}
})?;
Expand Down
38 changes: 3 additions & 35 deletions coordinator/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::node::storage::NodeStorage;
use crate::position::models::PositionState;
use crate::storage::CoordinatorTenTenOneStorage;
use crate::trade::TradeExecutor;
use anyhow::bail;
use anyhow::Context;
use anyhow::Result;
use bitcoin::hashes::hex::ToHex;
Expand Down Expand Up @@ -288,15 +287,13 @@ impl Node {
);

let channel = self.inner.get_dlc_channel_by_id(channel_id)?;
let contract_id =
channel.get_contract_id().context("missing contract id")?;

let protocol_executor =
dlc_protocol::DlcProtocolExecutor::new(self.pool.clone());
protocol_executor.finish_dlc_protocol(
protocol_id,
&channel.get_counter_party_id(),
&contract_id,
channel.get_contract_id(),
channel_id,
)?;
}
Expand Down Expand Up @@ -329,39 +326,12 @@ impl Node {
"DLC channel settle protocol was finalized"
);

let mut connection = self.pool.get()?;
let dlc_protocol =
db::dlc_protocols::get_dlc_protocol(&mut connection, protocol_id)?;

let trader_id = dlc_protocol.trader.to_string();
tracing::debug!(trader_id, ?protocol_id, "Finalize closing position",);

let contract_id = dlc_protocol.contract_id;

match self.inner.get_closed_contract(contract_id) {
Ok(Some(closed_contract)) => closed_contract,
Ok(None) => {
tracing::error!(
trader_id,
?protocol_id,
"Can't close position as contract is not closed."
);
bail!("Can't close position as contract is not closed.");
}
Err(e) => {
tracing::error!(
"Failed to get closed contract from DLC manager storage: {e:#}"
);
bail!(e);
}
};

let protocol_executor =
dlc_protocol::DlcProtocolExecutor::new(self.pool.clone());
protocol_executor.finish_dlc_protocol(
protocol_id,
&node_id,
&contract_id,
None,
channel_id,
)?;
}
Expand Down Expand Up @@ -413,15 +383,13 @@ impl Node {
);

let channel = self.inner.get_dlc_channel_by_id(&channel_id)?;
let contract_id =
channel.get_contract_id().context("missing contract id")?;

let protocol_executor =
dlc_protocol::DlcProtocolExecutor::new(self.pool.clone());
protocol_executor.finish_dlc_protocol(
protocol_id,
&node_id,
&contract_id,
channel.get_contract_id(),
&channel_id,
)?;
}
Expand Down

0 comments on commit 4e5e736

Please sign in to comment.