From 36c04140f6e5f5b4680e32f168ac09efe7eceedc Mon Sep 17 00:00:00 2001 From: Mariusz Klochowicz Date: Wed, 6 Oct 2021 10:15:15 +1030 Subject: [PATCH] Optimise data structure used for settlement proposals New HashMap can store both incoming and outgoing settlement proposals at the same time and resulted in some code simplification, as there are less types needed to convey the intention. Provide a type alias for convenience (this also ensures that both binaries store settlement proposals in the same way) --- daemon/src/maker.rs | 2 +- daemon/src/maker_cfd.rs | 10 ++++---- daemon/src/model/cfd.rs | 10 ++++---- daemon/src/taker.rs | 2 +- daemon/src/taker_cfd.rs | 14 +++++------ daemon/src/to_sse_event.rs | 48 +++++++------------------------------- 6 files changed, 28 insertions(+), 58 deletions(-) diff --git a/daemon/src/maker.rs b/daemon/src/maker.rs index d39afc6..5935a8c 100644 --- a/daemon/src/maker.rs +++ b/daemon/src/maker.rs @@ -114,7 +114,7 @@ async fn main() -> Result<()> { let (order_feed_sender, order_feed_receiver) = watch::channel::>(None); let (wallet_feed_sender, wallet_feed_receiver) = watch::channel::(wallet_info); let (settlement_feed_sender, settlement_feed_receiver) = - watch::channel::(SettlementProposals::Incoming(HashMap::new())); + watch::channel::(HashMap::new()); let figment = rocket::Config::figment() .merge(("databases.maker.url", data_dir.join("maker.sqlite"))) diff --git a/daemon/src/maker_cfd.rs b/daemon/src/maker_cfd.rs index d23ad4b..02593cf 100644 --- a/daemon/src/maker_cfd.rs +++ b/daemon/src/maker_cfd.rs @@ -6,7 +6,7 @@ use crate::db::{ use crate::maker_inc_connections::TakerCommand; use crate::model::cfd::{ Cfd, CfdState, CfdStateChangeEvent, CfdStateCommon, Dlc, Order, OrderId, Origin, Role, - SettlementProposal, SettlementProposals, + SettlementKind, SettlementProposal, SettlementProposals, }; use crate::model::{OracleEventId, TakerId, Usd}; use crate::monitor::MonitorParams; @@ -76,7 +76,7 @@ pub struct Actor { setup_state: SetupState, latest_announcements: Option>, oracle_actor: Address>>, - current_settlement_proposals: HashMap, + current_settlement_proposals: SettlementProposals, } enum SetupState { @@ -120,9 +120,7 @@ impl Actor { fn send_current_settlement_proposals(&self) -> Result<()> { Ok(self .settlements_feed_sender - .send(SettlementProposals::Incoming( - self.current_settlement_proposals.clone(), - ))?) + .send(self.current_settlement_proposals.clone())?) } async fn handle_new_order( @@ -192,7 +190,7 @@ impl Actor { proposal ); self.current_settlement_proposals - .insert(proposal.order_id, proposal); + .insert(proposal.order_id, (proposal, SettlementKind::Incoming)); self.send_current_settlement_proposals()?; Ok(()) diff --git a/daemon/src/model/cfd.rs b/daemon/src/model/cfd.rs index b2b22f3..af574d2 100644 --- a/daemon/src/model/cfd.rs +++ b/daemon/src/model/cfd.rs @@ -355,12 +355,14 @@ pub struct SettlementProposal { } #[derive(Debug, Clone)] -#[allow(dead_code)] // Variants used by different binaries -pub enum SettlementProposals { - Incoming(HashMap), - Outgoing(HashMap), +#[allow(dead_code)] // Variants (for now) used by different binaries. +pub enum SettlementKind { + Incoming, + Outgoing, } +pub type SettlementProposals = HashMap; + /// Represents a cfd (including state) #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] pub struct Cfd { diff --git a/daemon/src/taker.rs b/daemon/src/taker.rs index e2ac692..940e1cf 100644 --- a/daemon/src/taker.rs +++ b/daemon/src/taker.rs @@ -109,7 +109,7 @@ async fn main() -> Result<()> { let (order_feed_sender, order_feed_receiver) = watch::channel::>(None); let (wallet_feed_sender, wallet_feed_receiver) = watch::channel::(wallet_info); let (settlement_feed_sender, settlement_feed_receiver) = - watch::channel::(SettlementProposals::Outgoing(HashMap::new())); + watch::channel::(HashMap::new()); let (read, write) = loop { let socket = tokio::net::TcpSocket::new_v4()?; diff --git a/daemon/src/taker_cfd.rs b/daemon/src/taker_cfd.rs index 265ff02..95a22c2 100644 --- a/daemon/src/taker_cfd.rs +++ b/daemon/src/taker_cfd.rs @@ -5,7 +5,7 @@ use crate::db::{ }; use crate::model::cfd::{ Cfd, CfdState, CfdStateChangeEvent, CfdStateCommon, Dlc, Order, OrderId, Origin, Role, - SettlementProposal, SettlementProposals, + SettlementKind, SettlementProposals, }; use crate::model::{OracleEventId, Usd}; use crate::monitor::{self, MonitorParams}; @@ -65,7 +65,7 @@ pub struct Actor { setup_state: SetupState, latest_announcements: Option>, oracle_actor: Address>>, - current_settlement_proposals: HashMap, + current_settlement_proposals: SettlementProposals, } impl Actor { @@ -100,9 +100,7 @@ impl Actor { fn send_current_settlement_proposals(&self) -> Result<()> { Ok(self .settlements_feed_sender - .send(SettlementProposals::Outgoing( - self.current_settlement_proposals.clone(), - ))?) + .send(self.current_settlement_proposals.clone())?) } async fn handle_take_offer(&mut self, order_id: OrderId, quantity: Usd) -> Result<()> { @@ -153,8 +151,10 @@ impl Actor { ) } - self.current_settlement_proposals - .insert(proposal.order_id, proposal.clone()); + self.current_settlement_proposals.insert( + proposal.order_id, + (proposal.clone(), SettlementKind::Outgoing), + ); self.send_current_settlement_proposals()?; self.send_to_maker diff --git a/daemon/src/to_sse_event.rs b/daemon/src/to_sse_event.rs index 0bff7ee..dce8895 100644 --- a/daemon/src/to_sse_event.rs +++ b/daemon/src/to_sse_event.rs @@ -1,4 +1,4 @@ -use crate::model::cfd::{OrderId, Role, SettlementProposals}; +use crate::model::cfd::{OrderId, Role, SettlementKind, SettlementProposals}; use crate::model::{Leverage, Position, TradingPair, Usd}; use crate::{bitmex_price_feed, model}; use bdk::bitcoin::{Amount, SignedAmount}; @@ -103,12 +103,6 @@ pub struct CfdsWithAuxData { pub settlement_proposals: SettlementProposals, } -enum SettlementProposalStatus { - Incoming, - Outgoing, - None, -} - impl CfdsWithAuxData { pub fn new( rx_cfds: &watch::Receiver>, @@ -124,20 +118,6 @@ impl CfdsWithAuxData { let settlement_proposals = rx_settlement.borrow().clone(); - // Test whether the correct settlement proposals were sent - match settlement_proposals { - SettlementProposals::Incoming(_) => { - if role == Role::Taker { - panic!("Taker should never receive incoming settlement proposals"); - } - } - SettlementProposals::Outgoing(_) => { - if role == Role::Maker { - panic!("Maker should never receive outgoing settlement proposals"); - } - } - } - CfdsWithAuxData { cfds: rx_cfds.borrow().clone(), current_price, @@ -146,20 +126,10 @@ impl CfdsWithAuxData { } /// Check whether given CFD has any active settlement proposals - fn settlement_proposal_status(&self, cfd: &model::cfd::Cfd) -> SettlementProposalStatus { - match &self.settlement_proposals { - SettlementProposals::Incoming(proposals) => { - if proposals.contains_key(&cfd.order.id) { - return SettlementProposalStatus::Incoming; - } - } - SettlementProposals::Outgoing(proposals) => { - if proposals.contains_key(&cfd.order.id) { - return SettlementProposalStatus::Outgoing; - } - } - } - SettlementProposalStatus::None + fn settlement_proposal_status(&self, cfd: &model::cfd::Cfd) -> Option { + self.settlement_proposals + .get(&cfd.order.id) + .map(|(_, kind)| kind.clone()) } } @@ -258,12 +228,12 @@ impl ToSseEvent for model::WalletInfo { fn to_cfd_state( cfd_state: &model::cfd::CfdState, - proposal_status: SettlementProposalStatus, + proposal_status: Option, ) -> CfdState { match proposal_status { - SettlementProposalStatus::Incoming => CfdState::IncomingSettlementProposal, - SettlementProposalStatus::Outgoing => CfdState::OutgoingSettlementProposal, - SettlementProposalStatus::None => match cfd_state { + Some(SettlementKind::Incoming) => CfdState::IncomingSettlementProposal, + Some(SettlementKind::Outgoing) => CfdState::OutgoingSettlementProposal, + None => match cfd_state { model::cfd::CfdState::OutgoingOrderRequest { .. } => CfdState::OutgoingOrderRequest, model::cfd::CfdState::IncomingOrderRequest { .. } => CfdState::IncomingOrderRequest, model::cfd::CfdState::Accepted { .. } => CfdState::Accepted,