Skip to content

Commit

Permalink
chore: Move protocol id to xxi node crate
Browse files Browse the repository at this point in the history
  • Loading branch information
holzeis committed May 9, 2024
1 parent 4d50d3b commit 3cdb0ad
Show file tree
Hide file tree
Showing 12 changed files with 112 additions and 132 deletions.
2 changes: 1 addition & 1 deletion coordinator/src/db/dlc_channels.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::dlc_protocol::ProtocolId;
use crate::node::channel;
use crate::schema::dlc_channels;
use crate::schema::sql_types::DlcChannelStateType;
Expand All @@ -24,6 +23,7 @@ use std::any::TypeId;
use std::str::FromStr;
use time::OffsetDateTime;
use uuid::Uuid;
use xxi_node::node::ProtocolId;

#[derive(Debug, Clone, Copy, PartialEq, FromSqlRow, AsExpression)]
#[diesel(sql_type = DlcChannelStateType)]
Expand Down
2 changes: 1 addition & 1 deletion coordinator/src/db/dlc_protocols.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::db;
use crate::dlc_protocol;
use crate::dlc_protocol::ProtocolId;
use crate::schema::dlc_protocols;
use crate::schema::sql_types::ProtocolStateType;
use crate::schema::sql_types::ProtocolTypeType;
Expand All @@ -21,6 +20,7 @@ use std::any::TypeId;
use std::str::FromStr;
use time::OffsetDateTime;
use uuid::Uuid;
use xxi_node::node::ProtocolId;

#[derive(Debug, Clone, Copy, PartialEq, FromSqlRow, AsExpression, Eq, Hash)]
#[diesel(sql_type = ProtocolStateType)]
Expand Down
2 changes: 1 addition & 1 deletion coordinator/src/db/trade_params.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::dlc_protocol;
use crate::dlc_protocol::ProtocolId;
use crate::orderbook::db::custom_types::Direction;
use crate::schema::trade_params;
use bitcoin::secp256k1::PublicKey;
Expand All @@ -14,6 +13,7 @@ use diesel::RunQueryDsl;
use std::str::FromStr;
use uuid::Uuid;
use xxi_node::commons;
use xxi_node::node::ProtocolId;

#[derive(Queryable, Debug)]
#[diesel(table_name = trade_params)]
Expand Down
82 changes: 2 additions & 80 deletions coordinator/src/dlc_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,93 +14,16 @@ use diesel::Connection;
use diesel::PgConnection;
use diesel::QueryResult;
use dlc_manager::ContractId;
use dlc_manager::ReferenceId;
use rust_decimal::prelude::FromPrimitive;
use rust_decimal::prelude::ToPrimitive;
use rust_decimal::Decimal;
use std::fmt::Display;
use std::fmt::Formatter;
use std::str::from_utf8;
use time::OffsetDateTime;
use tokio::sync::broadcast::Sender;
use uuid::Uuid;
use xxi_node::cfd::calculate_pnl;
use xxi_node::commons;
use xxi_node::commons::Direction;
use xxi_node::node::rust_dlc_manager::DlcChannelId;

#[derive(Debug, Copy, Clone, PartialEq)]
pub struct ProtocolId(Uuid);

impl ProtocolId {
pub fn new() -> Self {
ProtocolId(Uuid::new_v4())
}

pub fn to_uuid(&self) -> Uuid {
self.0
}
}

impl Default for ProtocolId {
fn default() -> Self {
Self::new()
}
}

impl Display for ProtocolId {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
self.0.to_string().fmt(f)
}
}

impl From<ProtocolId> for ReferenceId {
fn from(value: ProtocolId) -> Self {
let uuid = value.to_uuid();

// 16 bytes.
let uuid_bytes = uuid.as_bytes();

// 32-digit hex string.
let hex = hex::encode(uuid_bytes);

// Derived `ReferenceId`: 32-bytes.
let hex_bytes = hex.as_bytes();

let mut array = [0u8; 32];
array.copy_from_slice(hex_bytes);

array
}
}

impl TryFrom<ReferenceId> for ProtocolId {
type Error = anyhow::Error;

fn try_from(value: ReferenceId) -> Result<Self> {
// 32-digit hex string.
let hex = from_utf8(&value)?;

// 16 bytes.
let uuid_bytes = hex::decode(hex)?;

let uuid = Uuid::from_slice(&uuid_bytes)?;

Ok(ProtocolId(uuid))
}
}

impl From<Uuid> for ProtocolId {
fn from(value: Uuid) -> Self {
ProtocolId(value)
}
}

impl From<ProtocolId> for Uuid {
fn from(value: ProtocolId) -> Self {
value.0
}
}
use xxi_node::node::ProtocolId;

pub struct DlcProtocol {
pub id: ProtocolId,
Expand Down Expand Up @@ -605,8 +528,7 @@ impl DlcProtocolExecutor {
Ok(())
}

/// Completes the rollover dlc protocol as successful and updates the 10101 meta data
/// accordingly in a single database transaction.
/// Completes the collab close dlc protocol as successful
fn finish_close_channel_dlc_protocol(
&self,
conn: &mut PgConnection,
Expand Down
11 changes: 5 additions & 6 deletions coordinator/src/node.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::db;
use crate::dlc_protocol;
use crate::dlc_protocol::DlcProtocolType;
use crate::dlc_protocol::ProtocolId;
use crate::message::OrderbookMessage;
use crate::node::storage::NodeStorage;
use crate::position::models::PositionState;
Expand Down Expand Up @@ -45,6 +44,7 @@ use xxi_node::message_handler::TenTenOneSignChannel;
use xxi_node::node;
use xxi_node::node::event::NodeEvent;
use xxi_node::node::tentenone_message_name;
use xxi_node::node::ProtocolId;
use xxi_node::node::RunningNode;

pub mod channel;
Expand Down Expand Up @@ -580,11 +580,10 @@ impl Node {
let protocol_id = close_offer.reference_id.context("Missing reference id")?;
let protocol_id = ProtocolId::try_from(protocol_id)?;

let previous_id = channel.get_reference_id();
let previous_id = match previous_id {
Some(previous_id) => Some(ProtocolId::try_from(previous_id)?),
None => None,
};
let previous_id = channel
.get_reference_id()
.map(ProtocolId::try_from)
.transpose()?;

let protocol_executor = dlc_protocol::DlcProtocolExecutor::new(self.pool.clone());
protocol_executor.start_dlc_protocol(
Expand Down
14 changes: 6 additions & 8 deletions coordinator/src/node/channel.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::db;
use crate::dlc_protocol;
use crate::dlc_protocol::DlcProtocolType;
use crate::dlc_protocol::ProtocolId;
use crate::node::Node;
use crate::position::models::PositionState;
use anyhow::bail;
Expand Down Expand Up @@ -32,6 +31,7 @@ use tokio::sync::broadcast::error::RecvError;
use xxi_node::bitcoin_conversion::to_secp_pk_30;
use xxi_node::bitcoin_conversion::to_txid_30;
use xxi_node::node::event::NodeEvent;
use xxi_node::node::ProtocolId;
use xxi_node::storage::DlcChannelEvent;

pub enum DlcChannelState {
Expand Down Expand Up @@ -68,17 +68,15 @@ impl Node {
is_force_close: bool,
) -> Result<()> {
let channel = self.inner.get_dlc_channel_by_id(&channel_id)?;
let previous_id = channel.get_reference_id();
let previous_id = match previous_id {
Some(previous_id) => Some(ProtocolId::try_from(previous_id)?),
None => None,
};
let previous_id = channel
.get_reference_id()
.map(ProtocolId::try_from)
.transpose()?;

let reference_id = self
let protocol_id = self
.inner
.close_dlc_channel(channel_id, is_force_close)
.await?;
let protocol_id = ProtocolId::try_from(reference_id)?;

let protocol_executor = dlc_protocol::DlcProtocolExecutor::new(self.pool.clone());
protocol_executor.start_dlc_protocol(
Expand Down
2 changes: 1 addition & 1 deletion coordinator/src/node/rollover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::db;
use crate::db::positions;
use crate::dlc_protocol;
use crate::dlc_protocol::DlcProtocolType;
use crate::dlc_protocol::ProtocolId;
use crate::node::Node;
use crate::notifications::Notification;
use crate::notifications::NotificationKind;
Expand Down Expand Up @@ -37,6 +36,7 @@ use xxi_node::bitcoin_conversion::to_xonly_pk_30;
use xxi_node::commons;
use xxi_node::commons::ContractSymbol;
use xxi_node::node::event::NodeEvent;
use xxi_node::node::ProtocolId;

#[derive(Debug, Clone)]
struct Rollover {
Expand Down
2 changes: 1 addition & 1 deletion coordinator/src/routes/admin.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::collaborative_revert;
use crate::db;
use crate::dlc_protocol::ProtocolId;
use crate::parse_dlc_channel_id;
use crate::referrals;
use crate::routes::AppState;
Expand Down Expand Up @@ -42,6 +41,7 @@ use xxi_node::bitcoin_conversion::to_secp_pk_30;
use xxi_node::bitcoin_conversion::to_txid_30;
use xxi_node::commons;
use xxi_node::commons::CollaborativeRevertCoordinatorRequest;
use xxi_node::node::ProtocolId;

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Balance {
Expand Down
10 changes: 5 additions & 5 deletions coordinator/src/trade/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::db;
use crate::decimal_from_f32;
use crate::dlc_protocol;
use crate::dlc_protocol::DlcProtocolType;
use crate::dlc_protocol::ProtocolId;
use crate::message::OrderbookMessage;
use crate::node::Node;
use crate::orderbook::db::matches;
Expand Down Expand Up @@ -52,6 +51,7 @@ use xxi_node::commons::TradeAndChannelParams;
use xxi_node::commons::TradeParams;
use xxi_node::node::event::NodeEvent;
use xxi_node::node::signed_channel_state_name;
use xxi_node::node::ProtocolId;

pub mod models;
pub mod websocket;
Expand Down Expand Up @@ -369,7 +369,7 @@ impl TradeExecutor {
trade_params.filled_with.clone(),
contract_input,
trade_params.pubkey,
protocol_id.into(),
protocol_id,
)
.await
.context("Could not propose DLC channel")?;
Expand Down Expand Up @@ -548,7 +548,7 @@ impl TradeExecutor {
trade_params.filled_with.clone(),
&dlc_channel_id,
contract_input,
protocol_id.into(),
protocol_id,
)
.await
.context("Could not propose reopen DLC channel update")?;
Expand Down Expand Up @@ -721,7 +721,7 @@ impl TradeExecutor {
trade_params.filled_with.clone(),
&dlc_channel_id,
contract_input,
protocol_id.into(),
protocol_id,
)
.await
.context("Could not propose resize DLC channel update")?;
Expand Down Expand Up @@ -878,7 +878,7 @@ impl TradeExecutor {
trade_params.filled_with.clone(),
&channel_id,
settlement_amount_trader,
protocol_id.into(),
protocol_id,
)
.await?;

Expand Down
Loading

0 comments on commit 3cdb0ad

Please sign in to comment.