Skip to content

Commit

Permalink
feat(dlc_protocol): Add dlc protocol entry for rollovers
Browse files Browse the repository at this point in the history
  • Loading branch information
holzeis committed Feb 29, 2024
1 parent 9f562d1 commit f5d8706
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 43 deletions.
4 changes: 2 additions & 2 deletions coordinator/src/db/dlc_protocols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ pub(crate) fn set_dlc_protocol_state_to_failed(
pub(crate) fn set_dlc_protocol_state_to_success(
conn: &mut PgConnection,
protocol_id: ProtocolId,
contract_id: ContractId,
channel_id: DlcChannelId,
contract_id: &ContractId,
channel_id: &DlcChannelId,
) -> QueryResult<()> {
let affected_rows = diesel::update(dlc_protocols::table)
.filter(dlc_protocols::protocol_id.eq(protocol_id.to_uuid()))
Expand Down
34 changes: 30 additions & 4 deletions coordinator/src/dlc_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,20 +222,20 @@ impl DlcProtocolExecutor {
Ok(())
}

/// Completes the dlc protocol as successful and updates the 10101 meta data
/// Completes the trade dlc protocol as successful and updates the 10101 meta data
/// accordingly in a single database transaction.
/// - Set dlc protocol to success
/// - If not closing: Updates the `[PostionState::Proposed`] position state to
/// `[PostionState::Open]`
/// - If closing: Calculates the pnl and sets the `[PositionState::Closing`] position state to
/// `[PositionState::Closed`]
/// - Creates and inserts the new trade
pub fn finish_dlc_protocol(
pub fn finish_trade_dlc_protocol(
&self,
protocol_id: ProtocolId,
closing: bool,
contract_id: ContractId,
channel_id: DlcChannelId,
contract_id: &ContractId,
channel_id: &DlcChannelId,
) -> Result<()> {
let mut conn = self.pool.get()?;

Expand Down Expand Up @@ -342,6 +342,32 @@ impl DlcProtocolExecutor {

Ok(())
}

/// Completes the rollover dlc protocol as successful and updates the 10101 meta data
/// accordingly in a single database transaction.
pub fn finish_rollover_dlc_protocol(
&self,
protocol_id: ProtocolId,
contract_id: &ContractId,
channel_id: &DlcChannelId,
trader: &PublicKey,
) -> Result<()> {
tracing::debug!(%trader, %protocol_id, "Finalizing rollover");
let mut conn = self.pool.get()?;

conn.transaction(|conn| {
db::dlc_protocols::set_dlc_protocol_state_to_success(
conn,
protocol_id,
contract_id,
channel_id,
)?;

db::positions::Position::set_position_to_open(conn, trader.to_string(), *contract_id)
})?;

Ok(())
}
}

#[cfg(test)]
Expand Down
37 changes: 21 additions & 16 deletions coordinator/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,20 +287,25 @@ impl Node {
"DLC channel renew protocol was finalized"
);

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());
if self.is_in_rollover(node_id)? {
self.finalize_rollover(channel_id)?;
protocol_executor.finish_rollover_dlc_protocol(
protocol_id,
&contract_id,
&channel.get_id(),
&channel.get_counter_party_id(),
)?;
} else {
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_executor.finish_trade_dlc_protocol(
protocol_id,
false,
contract_id,
channel.get_id(),
&contract_id,
&channel.get_id(),
)?;
}
}
Expand Down Expand Up @@ -362,11 +367,11 @@ impl Node {

let protocol_executor =
dlc_protocol::DlcProtocolExecutor::new(self.pool.clone());
protocol_executor.finish_dlc_protocol(
protocol_executor.finish_trade_dlc_protocol(
protocol_id,
true,
contract_id,
*channel_id,
&contract_id,
channel_id,
)?;
}
ChannelMessage::CollaborativeCloseOffer(close_offer) => {
Expand Down Expand Up @@ -422,11 +427,11 @@ impl Node {

let protocol_executor =
dlc_protocol::DlcProtocolExecutor::new(self.pool.clone());
protocol_executor.finish_dlc_protocol(
protocol_executor.finish_trade_dlc_protocol(
protocol_id,
false,
contract_id,
channel_id,
&contract_id,
&channel_id,
)?;
}
ChannelMessage::Reject(reject) => {
Expand Down
44 changes: 23 additions & 21 deletions coordinator/src/node/rollover.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::db;
use crate::db::positions;
use crate::dlc_protocol;
use crate::dlc_protocol::DlcProtocolType;
use crate::dlc_protocol::ProtocolId;
use crate::message::NewUserMessage;
use crate::message::OrderbookMessage;
Expand All @@ -9,7 +11,6 @@ use anyhow::anyhow;
use anyhow::bail;
use anyhow::Context;
use anyhow::Result;
use bitcoin::hashes::hex::ToHex;
use bitcoin::secp256k1::PublicKey;
use bitcoin::Network;
use bitcoin::XOnlyPublicKey;
Expand Down Expand Up @@ -222,16 +223,34 @@ impl Node {
) -> Result<()> {
let contract = self.inner.get_contract_by_dlc_channel_id(dlc_channel_id)?;
let rollover = Rollover::new(contract, network)?;
let protocol_id = ProtocolId::new();

tracing::debug!(node_id=%rollover.counterparty_pubkey, "Rollover dlc channel");
tracing::debug!(node_id=%rollover.counterparty_pubkey, %protocol_id, "Rollover dlc channel");

let contract_input: ContractInput = rollover.clone().into();

let protocol_id = ProtocolId::new();
self.inner
let channel = self.inner.get_dlc_channel_by_id(dlc_channel_id)?;
let previous_id = match channel.get_reference_id() {
Some(reference_id) => Some(ProtocolId::try_from(reference_id)?),
None => None,
};

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

let protocol_executor = dlc_protocol::DlcProtocolExecutor::new(self.pool.clone());
protocol_executor.start_dlc_protocol(
protocol_id,
previous_id,
&contract_id,
dlc_channel_id,
DlcProtocolType::Rollover {
trader: rollover.counterparty_pubkey,
},
)?;

// Sets the position state to rollover indicating that a rollover is in progress.
let mut connection = self.pool.get()?;
db::positions::Position::rollover_position(
Expand All @@ -252,23 +271,6 @@ impl Node {
Ok(position.is_some())
}

/// Finalizes the rollover protocol with the app setting the position to open.
pub fn finalize_rollover(&self, dlc_channel_id: &DlcChannelId) -> Result<()> {
let contract = self.inner.get_contract_by_dlc_channel_id(dlc_channel_id)?;
let trader_id = contract.get_counter_party_id();
tracing::debug!(%trader_id,
"Finalizing rollover for dlc channel: {}",
dlc_channel_id.to_hex()
);

let mut connection = self.pool.get()?;
db::positions::Position::set_position_to_open(
&mut connection,
contract.get_counter_party_id().to_string(),
contract.get_temporary_id(),
)
}

fn rollback_channel_if_needed(
&self,
connection: &mut PgConnection,
Expand Down

0 comments on commit f5d8706

Please sign in to comment.