Browse Source

Work in review comments

fix-bad-api-calls
Daniel Karzel 3 years ago
parent
commit
2fe376cb55
No known key found for this signature in database GPG Key ID: 30C3FC2E438ADB6E
  1. 13
      daemon/src/maker_cfd.rs
  2. 16
      daemon/src/model/cfd.rs
  3. 30
      daemon/src/monitor.rs
  4. 13
      daemon/src/taker_cfd.rs

13
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<CfdSetupCompleted> for Actor {
}
#[async_trait]
impl Handler<CfdMonitoringEvent> for Actor {
async fn handle(&mut self, msg: CfdMonitoringEvent, _ctx: &mut Context<Self>) {
impl Handler<monitor::Event> for Actor {
async fn handle(&mut self, msg: monitor::Event, _ctx: &mut Context<Self>) {
log_error!(self.handle_monitoring_event(msg))
}
}

16
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<CfdState> {
pub fn handle(&self, event: CfdStateChangeEvent) -> Result<CfdState> {
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(

30
daemon/src/monitor.rs

@ -23,7 +23,7 @@ pub struct MonitorParams {
impl<T> Actor<T>
where
T: xtra::Actor + xtra::Handler<CfdMonitoringEvent>,
T: xtra::Actor + xtra::Handler<Event>,
{
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<T> xtra::Actor for Actor<T> where T: xtra::Actor {}
#[async_trait]
impl<T> xtra::Handler<StartMonitoring> for Actor<T>
where
T: xtra::Actor + xtra::Handler<CfdMonitoringEvent>,
T: xtra::Actor + xtra::Handler<Event>,
{
async fn handle(&mut self, msg: StartMonitoring, _ctx: &mut xtra::Context<Self>) {
log_error!(self.handle_start_monitoring(msg));

13
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<CfdSetupCompleted> for Actor {
}
#[async_trait]
impl Handler<CfdMonitoringEvent> for Actor {
async fn handle(&mut self, msg: CfdMonitoringEvent, _ctx: &mut Context<Self>) {
impl Handler<monitor::Event> for Actor {
async fn handle(&mut self, msg: monitor::Event, _ctx: &mut Context<Self>) {
log_error!(self.handle_monitoring_event(msg))
}
}

Loading…
Cancel
Save