From f9cd6cb5d5a8500da9ac696b71fc44c2eec3169c Mon Sep 17 00:00:00 2001 From: Lucas Soriano del Pino Date: Fri, 26 Nov 2021 11:36:48 +1100 Subject: [PATCH] Handle incoming InvalidOrderId in setup_taker::Actor --- daemon/src/connection.rs | 12 +++++++++++- daemon/src/setup_taker.rs | 36 +++++++++++++++++++++++++++++++----- daemon/src/taker_cfd.rs | 14 ++------------ 3 files changed, 44 insertions(+), 18 deletions(-) diff --git a/daemon/src/connection.rs b/daemon/src/connection.rs index 143675b..c03ad64 100644 --- a/daemon/src/connection.rs +++ b/daemon/src/connection.rs @@ -191,7 +191,7 @@ impl Actor { }, wire::MakerToTaker::RejectOrder(order_id) => match self.setup_actors.get(&order_id) { Some(addr) => { - let _ = addr.send(setup_taker::Rejected).await; + let _ = addr.send(setup_taker::Rejected::without_reason()).await; } None => { tracing::warn!(%order_id, "No active contract setup"); @@ -207,6 +207,16 @@ impl Actor { } } } + wire::MakerToTaker::InvalidOrderId(order_id) => { + match self.setup_actors.get(&order_id) { + Some(addr) => { + let _ = addr.send(setup_taker::Rejected::invalid_order_id()).await; + } + None => { + tracing::warn!(%order_id, "No active contract setup"); + } + } + } other => { // this one should go to the taker cfd actor log_error!(self.maker_to_taker.send(other)); diff --git a/daemon/src/setup_taker.rs b/daemon/src/setup_taker.rs index 848edfe..d2cb9c4 100644 --- a/daemon/src/setup_taker.rs +++ b/daemon/src/setup_taker.rs @@ -107,11 +107,16 @@ impl Actor { Ok(()) } - fn handle(&mut self, _: Rejected, ctx: &mut xtra::Context) -> Result<()> { + fn handle(&mut self, msg: Rejected, ctx: &mut xtra::Context) -> Result<()> { + let order_id = self.order.id; + tracing::info!(%order_id, "Order got rejected"); + + if msg.is_invalid_order { + tracing::error!(%order_id, "Rejection reason: Invalid order ID"); + } + self.on_completed - .send(Completed::Rejected { - order_id: self.order.id, - }) + .send(Completed::Rejected { order_id }) .await?; ctx.stop(); @@ -190,7 +195,11 @@ pub struct Started(pub OrderId); /// Message sent from the `connection::Actor` to the /// `setup_taker::Actor` to notify that the order taken was rejected /// by the maker. -pub struct Rejected; +pub struct Rejected { + /// Used to indicate whether the rejection stems from the order ID + /// not being recognised by the maker. + is_invalid_order: bool, +} /// Message sent from the spawned task to `setup_taker::Actor` to /// notify that the contract setup has finished successfully. @@ -206,6 +215,23 @@ pub struct SetupFailed { error: anyhow::Error, } +impl Rejected { + /// Order was rejected by the maker for not specific reason. + pub fn without_reason() -> Self { + Rejected { + is_invalid_order: false, + } + } + + /// Order was rejected by the maker because it did not recognise + /// the order ID provided. + pub fn invalid_order_id() -> Self { + Rejected { + is_invalid_order: true, + } + } +} + impl xtra::Message for Started { type Result = (); } diff --git a/daemon/src/taker_cfd.rs b/daemon/src/taker_cfd.rs index b6cd509..8a21dee 100644 --- a/daemon/src/taker_cfd.rs +++ b/daemon/src/taker_cfd.rs @@ -227,14 +227,6 @@ where Ok(()) } - - async fn handle_invalid_order_id(&mut self, order_id: OrderId) -> Result<()> { - tracing::debug!(%order_id, "Invalid order ID"); - - self.append_cfd_state_rejected(order_id).await?; - - Ok(()) - } } impl Actor { @@ -719,9 +711,6 @@ where wire::MakerToTaker::RejectSettlement(order_id) => { log_error!(self.handle_settlement_rejected(order_id)) } - wire::MakerToTaker::InvalidOrderId(order_id) => { - log_error!(self.handle_invalid_order_id(order_id)) - } wire::MakerToTaker::ConfirmRollOver { order_id, oracle_event_id, @@ -739,7 +728,8 @@ where } wire::MakerToTaker::ConfirmOrder(_) | wire::MakerToTaker::RejectOrder(_) - | wire::MakerToTaker::Protocol { .. } => { + | wire::MakerToTaker::Protocol { .. } + | wire::MakerToTaker::InvalidOrderId(_) => { unreachable!("These messages should be sent to the `setup_taker::Actor`") } }