Browse Source

Merge pull request #117 from comit-network/no-result-return-values

Cleanup MakerCfdActor
fix-bad-api-calls
Thomas Eizinger 4 years ago
committed by GitHub
parent
commit
b511f8e40c
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 170
      daemon/src/maker_cfd_actor.rs

170
daemon/src/maker_cfd_actor.rs

@ -17,64 +17,32 @@ use xtra::prelude::*;
pub struct Initialized(pub Address<MakerIncConnectionsActor>); pub struct Initialized(pub Address<MakerIncConnectionsActor>);
impl Message for Initialized {
type Result = Result<()>;
}
pub struct TakeOrder { pub struct TakeOrder {
pub taker_id: TakerId, pub taker_id: TakerId,
pub order_id: OrderId, pub order_id: OrderId,
pub quantity: Usd, pub quantity: Usd,
} }
impl Message for TakeOrder {
type Result = Result<()>;
}
pub struct NewOrder(pub Order); pub struct NewOrder(pub Order);
impl Message for NewOrder {
type Result = Result<()>;
}
pub struct StartContractSetup { pub struct StartContractSetup {
pub taker_id: TakerId, pub taker_id: TakerId,
pub order_id: OrderId, pub order_id: OrderId,
} }
impl Message for StartContractSetup {
type Result = Result<()>;
}
pub struct NewTakerOnline { pub struct NewTakerOnline {
pub id: TakerId, pub id: TakerId,
} }
impl Message for NewTakerOnline {
type Result = Result<()>;
}
pub struct IncProtocolMsg(pub SetupMsg); pub struct IncProtocolMsg(pub SetupMsg);
impl Message for IncProtocolMsg {
type Result = Result<()>;
}
pub struct CfdSetupCompleted { pub struct CfdSetupCompleted {
pub order_id: OrderId, pub order_id: OrderId,
pub dlc: Dlc, pub dlc: Dlc,
} }
impl Message for CfdSetupCompleted {
type Result = Result<()>;
}
pub struct SyncWallet; pub struct SyncWallet;
impl Message for SyncWallet {
type Result = Result<()>;
}
pub struct MakerCfdActor { pub struct MakerCfdActor {
db: sqlx::SqlitePool, db: sqlx::SqlitePool,
wallet: Wallet, wallet: Wallet,
@ -90,8 +58,6 @@ pub struct MakerCfdActor {
contract_setup_message_buffer: Vec<SetupMsg>, contract_setup_message_buffer: Vec<SetupMsg>,
} }
impl xtra::Actor for MakerCfdActor {}
impl MakerCfdActor { impl MakerCfdActor {
pub async fn new( pub async fn new(
db: sqlx::SqlitePool, db: sqlx::SqlitePool,
@ -195,7 +161,7 @@ impl MakerCfdActor {
self.current_contract_setup.replace(inbox.clone()); self.current_contract_setup.replace(inbox.clone());
for msg in self.contract_setup_message_buffer.drain(..) { for msg in self.contract_setup_message_buffer.drain(..) {
inbox.send(msg).unwrap(); inbox.send(msg)?;
} }
// TODO: Should we do this here or already earlier or after the spawn? // TODO: Should we do this here or already earlier or after the spawn?
@ -266,19 +232,38 @@ impl MakerCfdActor {
self.wallet_feed_sender.send(wallet_info)?; self.wallet_feed_sender.send(wallet_info)?;
Ok(()) Ok(())
} }
}
#[async_trait] async fn handle_cfd_setup_completed(&mut self, msg: CfdSetupCompleted) -> Result<()> {
impl Handler<Initialized> for MakerCfdActor { let mut conn = self.db.acquire().await?;
async fn handle(&mut self, msg: Initialized, _ctx: &mut Context<Self>) -> Result<()> {
self.takers.replace(msg.0); self.current_contract_setup = None;
self.contract_setup_message_buffer = vec![];
insert_new_cfd_state_by_order_id(
msg.order_id,
CfdState::PendingOpen {
common: CfdStateCommon {
transition_timestamp: SystemTime::now(),
},
dlc: msg.dlc.clone(),
},
&mut conn,
)
.await?;
self.cfd_feed_actor_inbox
.send(load_all_cfds(&mut conn).await?)?;
let txid = self.wallet.try_broadcast_transaction(msg.dlc.lock).await?;
tracing::info!("Lock transaction published with txid {}", txid);
// TODO: tx monitoring, once confirmed with x blocks transition the Cfd to
// Open
Ok(()) Ok(())
} }
}
#[async_trait] async fn handle_take_order(&mut self, msg: TakeOrder) -> Result<()> {
impl Handler<TakeOrder> for MakerCfdActor {
async fn handle(&mut self, msg: TakeOrder, _ctx: &mut Context<Self>) -> Result<()> {
tracing::debug!(%msg.taker_id, %msg.quantity, %msg.order_id, tracing::debug!(%msg.taker_id, %msg.quantity, %msg.order_id,
"Taker wants to take an order" "Taker wants to take an order"
); );
@ -288,7 +273,7 @@ impl Handler<TakeOrder> for MakerCfdActor {
// 1. Validate if order is still valid // 1. Validate if order is still valid
let current_order = match self.current_order_id { let current_order = match self.current_order_id {
Some(current_order_id) if current_order_id == msg.order_id => { Some(current_order_id) if current_order_id == msg.order_id => {
load_order_by_id(current_order_id, &mut conn).await.unwrap() load_order_by_id(current_order_id, &mut conn).await?
} }
_ => { _ => {
self.takers()? self.takers()?
@ -335,6 +320,13 @@ impl Handler<TakeOrder> for MakerCfdActor {
} }
} }
#[async_trait]
impl Handler<Initialized> for MakerCfdActor {
async fn handle(&mut self, msg: Initialized, _ctx: &mut Context<Self>) {
self.takers.replace(msg.0);
}
}
macro_rules! log_error { macro_rules! log_error {
($future:expr) => { ($future:expr) => {
if let Err(e) = $future.await { if let Err(e) = $future.await {
@ -343,79 +335,85 @@ macro_rules! log_error {
}; };
} }
#[async_trait]
impl Handler<TakeOrder> for MakerCfdActor {
async fn handle(&mut self, msg: TakeOrder, _ctx: &mut Context<Self>) {
log_error!(self.handle_take_order(msg))
}
}
#[async_trait] #[async_trait]
impl Handler<NewOrder> for MakerCfdActor { impl Handler<NewOrder> for MakerCfdActor {
async fn handle(&mut self, msg: NewOrder, _ctx: &mut Context<Self>) -> Result<()> { async fn handle(&mut self, msg: NewOrder, _ctx: &mut Context<Self>) {
log_error!(self.handle_new_order(msg)); log_error!(self.handle_new_order(msg));
Ok(())
} }
} }
#[async_trait] #[async_trait]
impl Handler<StartContractSetup> for MakerCfdActor { impl Handler<StartContractSetup> for MakerCfdActor {
async fn handle(&mut self, msg: StartContractSetup, ctx: &mut Context<Self>) -> Result<()> { async fn handle(&mut self, msg: StartContractSetup, ctx: &mut Context<Self>) {
log_error!(self.handle_start_contract_setup(msg, ctx)); log_error!(self.handle_start_contract_setup(msg, ctx));
Ok(())
} }
} }
#[async_trait] #[async_trait]
impl Handler<NewTakerOnline> for MakerCfdActor { impl Handler<NewTakerOnline> for MakerCfdActor {
async fn handle(&mut self, msg: NewTakerOnline, _ctx: &mut Context<Self>) -> Result<()> { async fn handle(&mut self, msg: NewTakerOnline, _ctx: &mut Context<Self>) {
log_error!(self.handle_new_taker_online(msg)); log_error!(self.handle_new_taker_online(msg));
Ok(())
} }
} }
#[async_trait] #[async_trait]
impl Handler<IncProtocolMsg> for MakerCfdActor { impl Handler<IncProtocolMsg> for MakerCfdActor {
async fn handle(&mut self, msg: IncProtocolMsg, _ctx: &mut Context<Self>) -> Result<()> { async fn handle(&mut self, msg: IncProtocolMsg, _ctx: &mut Context<Self>) {
log_error!(self.handle_inc_protocol_msg(msg)); log_error!(self.handle_inc_protocol_msg(msg));
Ok(())
} }
} }
#[async_trait] #[async_trait]
impl Handler<CfdSetupCompleted> for MakerCfdActor { impl Handler<CfdSetupCompleted> for MakerCfdActor {
async fn handle(&mut self, msg: CfdSetupCompleted, _ctx: &mut Context<Self>) -> Result<()> { async fn handle(&mut self, msg: CfdSetupCompleted, _ctx: &mut Context<Self>) {
let mut conn = self.db.acquire().await?; log_error!(self.handle_cfd_setup_completed(msg));
}
}
self.current_contract_setup = None; #[async_trait]
self.contract_setup_message_buffer = vec![]; impl Handler<SyncWallet> for MakerCfdActor {
async fn handle(&mut self, _msg: SyncWallet, _ctx: &mut Context<Self>) {
log_error!(self.handle_sync_wallet());
}
}
insert_new_cfd_state_by_order_id( impl Message for Initialized {
msg.order_id, type Result = ();
CfdState::PendingOpen { }
common: CfdStateCommon {
transition_timestamp: SystemTime::now(),
},
dlc: msg.dlc.clone(),
},
&mut conn,
)
.await?;
self.cfd_feed_actor_inbox impl Message for TakeOrder {
.send(load_all_cfds(&mut conn).await?)?; type Result = ();
}
let txid = self impl Message for NewOrder {
.wallet type Result = ();
.try_broadcast_transaction(msg.dlc.lock) }
.await
.unwrap();
tracing::info!("Lock transaction published with txid {}", txid); impl Message for StartContractSetup {
type Result = ();
}
// TODO: tx monitoring, once confirmed with x blocks transition the Cfd to impl Message for NewTakerOnline {
// Open type Result = ();
Ok(())
}
} }
#[async_trait] impl Message for IncProtocolMsg {
impl Handler<SyncWallet> for MakerCfdActor { type Result = ();
async fn handle(&mut self, _msg: SyncWallet, _ctx: &mut Context<Self>) -> Result<()> {
log_error!(self.handle_sync_wallet());
Ok(())
}
} }
impl Message for CfdSetupCompleted {
type Result = ();
}
impl Message for SyncWallet {
type Result = ();
}
impl xtra::Actor for MakerCfdActor {}

Loading…
Cancel
Save