From 2fe376cb557bb7a166f8a0fcd359dc9ef8230d29 Mon Sep 17 00:00:00 2001 From: Daniel Karzel Date: Mon, 27 Sep 2021 17:22:12 +1000 Subject: [PATCH] Work in review comments --- daemon/src/maker_cfd.rs | 13 +++++++------ daemon/src/model/cfd.rs | 16 ++++++++-------- daemon/src/monitor.rs | 30 +++++++++++++++--------------- daemon/src/taker_cfd.rs | 13 +++++++------ 4 files changed, 37 insertions(+), 35 deletions(-) diff --git a/daemon/src/maker_cfd.rs b/daemon/src/maker_cfd.rs index 08d2e50..d0eca64 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}; use crate::model::{TakerId, Usd}; -use crate::monitor::{CfdMonitoringEvent, MonitorParams}; +use crate::monitor::MonitorParams; use crate::wallet::Wallet; use crate::wire::SetupMsg; use crate::{maker_inc_connections, monitor, setup_contract_actor}; @@ -408,18 +408,19 @@ impl Actor { Ok(()) } - async fn handle_monitoring_event(&mut self, event: CfdMonitoringEvent) -> Result<()> { + async fn handle_monitoring_event(&mut self, event: monitor::Event) -> Result<()> { let order_id = event.order_id(); let mut conn = self.db.acquire().await?; let cfd = load_cfd_by_order_id(order_id, &mut conn).await?; - let new_state = cfd.transition_to(CfdStateChangeEvent::Monitor(event))?; + let new_state = cfd.handle(CfdStateChangeEvent::Monitor(event))?; insert_new_cfd_state_by_order_id(order_id, new_state.clone(), &mut conn).await?; // TODO: Not sure that should be done here... - // Consider sending a message to ourselves to trigger broadcasting refund? + // Consider bubbling the refund availability up to the user, and let user trigger + // transaction publication if let CfdState::MustRefund { .. } = new_state { let signed_refund_tx = cfd.refund_tx()?; let txid = self @@ -484,8 +485,8 @@ impl Handler for Actor { } #[async_trait] -impl Handler for Actor { - async fn handle(&mut self, msg: CfdMonitoringEvent, _ctx: &mut Context) { +impl Handler for Actor { + async fn handle(&mut self, msg: monitor::Event, _ctx: &mut Context) { log_error!(self.handle_monitoring_event(msg)) } } diff --git a/daemon/src/model/cfd.rs b/daemon/src/model/cfd.rs index f8cf1cd..8a6a8e3 100644 --- a/daemon/src/model/cfd.rs +++ b/daemon/src/model/cfd.rs @@ -1,5 +1,5 @@ use crate::model::{Leverage, Position, TakerId, TradingPair, Usd}; -use crate::monitor::CfdMonitoringEvent; +use crate::monitor; use anyhow::{bail, Result}; use bdk::bitcoin::secp256k1::{SecretKey, Signature}; use bdk::bitcoin::{Address, Amount, PublicKey, Transaction}; @@ -366,7 +366,7 @@ impl Cfd { #[allow(dead_code)] pub const CET_TIMELOCK: u32 = 12; - pub fn transition_to(&self, event: CfdStateChangeEvent) -> Result { + pub fn handle(&self, event: CfdStateChangeEvent) -> Result { use CfdState::*; // TODO: Display impl @@ -376,7 +376,7 @@ impl Cfd { let new_state = match event { CfdStateChangeEvent::Monitor(event) => match event { - CfdMonitoringEvent::LockFinality(_) => match self.state.clone() { + monitor::Event::LockFinality(_) => match self.state.clone() { PendingOpen { dlc, .. } => CfdState::Open { common: CfdStateCommon { transition_timestamp: SystemTime::now(), @@ -392,7 +392,7 @@ impl Cfd { bail!("State already assumes lock finality: ignoring") } }, - CfdMonitoringEvent::CommitFinality(_) => { + monitor::Event::CommitFinality(_) => { let dlc = match self.state.clone() { Open { dlc, .. } => dlc, PendingOpen { dlc, .. } => { @@ -419,7 +419,7 @@ impl Cfd { cet_status: CetStatus::Unprepared, } } - CfdMonitoringEvent::CetTimelockExpired(_) => match self.state.clone() { + monitor::Event::CetTimelockExpired(_) => match self.state.clone() { CfdState::OpenCommitted { dlc, cet_status: CetStatus::Unprepared, @@ -481,7 +481,7 @@ impl Cfd { bail!("Refund path does not care about CET timelock expiry: ignoring") } }, - CfdMonitoringEvent::RefundTimelockExpired(_) => { + monitor::Event::RefundTimelockExpired(_) => { let dlc = match self.state.clone() { OpenCommitted { dlc, .. } => dlc, MustRefund { .. } | Refunded { .. } => { @@ -511,7 +511,7 @@ impl Cfd { dlc, } } - CfdMonitoringEvent::RefundFinality(_) => { + monitor::Event::RefundFinality(_) => { match self.state { MustRefund { .. } => (), OutgoingOrderRequest { .. } => unreachable!("taker-only state"), @@ -579,7 +579,7 @@ impl Cfd { pub enum CfdStateChangeEvent { // TODO: groupd other events by actors into enums and add them here so we can bundle all // transitions into cfd.transition_to(...) - Monitor(CfdMonitoringEvent), + Monitor(monitor::Event), } fn calculate_profit( diff --git a/daemon/src/monitor.rs b/daemon/src/monitor.rs index edbdb11..7567734 100644 --- a/daemon/src/monitor.rs +++ b/daemon/src/monitor.rs @@ -23,7 +23,7 @@ pub struct MonitorParams { impl Actor where - T: xtra::Actor + xtra::Handler, + T: xtra::Actor + xtra::Handler, { pub fn new( electrum_rpc_url: &str, @@ -54,7 +54,7 @@ where lock_subscription.wait_until_final().await.unwrap(); cfd_actor_addr - .do_send_async(CfdMonitoringEvent::LockFinality(id)) + .do_send_async(Event::LockFinality(id)) .await .unwrap(); } @@ -72,7 +72,7 @@ where commit_subscription.wait_until_final().await.unwrap(); cfd_actor_addr - .do_send_async(CfdMonitoringEvent::CommitFinality(id)) + .do_send_async(Event::CommitFinality(id)) .await .unwrap(); } @@ -88,7 +88,7 @@ where .unwrap(); cfd_actor_addr - .do_send_async(CfdMonitoringEvent::CetTimelockExpired(id)) + .do_send_async(Event::CetTimelockExpired(id)) .await .unwrap(); } @@ -105,7 +105,7 @@ where .unwrap(); cfd_actor_addr - .do_send_async(CfdMonitoringEvent::RefundTimelockExpired(id)) + .do_send_async(Event::RefundTimelockExpired(id)) .await .unwrap(); } @@ -121,7 +121,7 @@ where refund_subscription.wait_until_final().await.unwrap(); cfd_actor_addr - .do_send_async(CfdMonitoringEvent::RefundFinality(id)) + .do_send_async(Event::RefundFinality(id)) .await .unwrap(); } @@ -143,7 +143,7 @@ impl xtra::Message for StartMonitoring { } #[derive(Debug, Clone)] -pub enum CfdMonitoringEvent { +pub enum Event { LockFinality(OrderId), CommitFinality(OrderId), CetTimelockExpired(OrderId), @@ -151,21 +151,21 @@ pub enum CfdMonitoringEvent { RefundFinality(OrderId), } -impl CfdMonitoringEvent { +impl Event { pub fn order_id(&self) -> OrderId { let order_id = match self { - CfdMonitoringEvent::LockFinality(order_id) => order_id, - CfdMonitoringEvent::CommitFinality(order_id) => order_id, - CfdMonitoringEvent::CetTimelockExpired(order_id) => order_id, - CfdMonitoringEvent::RefundTimelockExpired(order_id) => order_id, - CfdMonitoringEvent::RefundFinality(order_id) => order_id, + Event::LockFinality(order_id) => order_id, + Event::CommitFinality(order_id) => order_id, + Event::CetTimelockExpired(order_id) => order_id, + Event::RefundTimelockExpired(order_id) => order_id, + Event::RefundFinality(order_id) => order_id, }; *order_id } } -impl xtra::Message for CfdMonitoringEvent { +impl xtra::Message for Event { type Result = (); } @@ -184,7 +184,7 @@ impl xtra::Actor for Actor where T: xtra::Actor {} #[async_trait] impl xtra::Handler for Actor where - T: xtra::Actor + xtra::Handler, + T: xtra::Actor + xtra::Handler, { async fn handle(&mut self, msg: StartMonitoring, _ctx: &mut xtra::Context) { log_error!(self.handle_start_monitoring(msg)); diff --git a/daemon/src/taker_cfd.rs b/daemon/src/taker_cfd.rs index 445bc35..a619023 100644 --- a/daemon/src/taker_cfd.rs +++ b/daemon/src/taker_cfd.rs @@ -6,7 +6,7 @@ use crate::db::{ use crate::actors::log_error; use crate::model::cfd::{Cfd, CfdState, CfdStateChangeEvent, CfdStateCommon, Dlc, Order, OrderId}; use crate::model::Usd; -use crate::monitor::{CfdMonitoringEvent, MonitorParams}; +use crate::monitor::MonitorParams; use crate::wallet::Wallet; use crate::wire::SetupMsg; use crate::{monitor, send_to_socket, setup_contract_actor, wire}; @@ -270,18 +270,19 @@ impl Actor { Ok(()) } - async fn handle_monitoring_event(&mut self, event: CfdMonitoringEvent) -> Result<()> { + async fn handle_monitoring_event(&mut self, event: monitor::Event) -> Result<()> { let order_id = event.order_id(); let mut conn = self.db.acquire().await?; let cfd = load_cfd_by_order_id(order_id, &mut conn).await?; - let new_state = cfd.transition_to(CfdStateChangeEvent::Monitor(event))?; + let new_state = cfd.handle(CfdStateChangeEvent::Monitor(event))?; insert_new_cfd_state_by_order_id(order_id, new_state.clone(), &mut conn).await?; // TODO: Not sure that should be done here... - // Consider sending a message to ourselves to trigger broadcasting refund? + // Consider bubbling the refund availability up to the user, and let user trigger + // transaction publication if let CfdState::MustRefund { .. } = new_state { let signed_refund_tx = cfd.refund_tx()?; let txid = self @@ -339,8 +340,8 @@ impl Handler for Actor { } #[async_trait] -impl Handler for Actor { - async fn handle(&mut self, msg: CfdMonitoringEvent, _ctx: &mut Context) { +impl Handler for Actor { + async fn handle(&mut self, msg: monitor::Event, _ctx: &mut Context) { log_error!(self.handle_monitoring_event(msg)) } }