From 38ee2b1d7c1f344da4f32cb5631b27f5c9cfb4eb Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Mon, 18 Oct 2021 16:54:39 +1100 Subject: [PATCH] Prefetch announcement upon proposing rollover Fixes #361. --- daemon/src/taker_cfd.rs | 67 ++++++++++++++++++++++++----------------- 1 file changed, 39 insertions(+), 28 deletions(-) diff --git a/daemon/src/taker_cfd.rs b/daemon/src/taker_cfd.rs index 0705bd7..fd1c4b1 100644 --- a/daemon/src/taker_cfd.rs +++ b/daemon/src/taker_cfd.rs @@ -17,6 +17,7 @@ use futures::channel::mpsc; use futures::{future, SinkExt}; use std::collections::HashMap; use std::time::SystemTime; +use time::OffsetDateTime; use tokio::sync::watch; use xtra::prelude::*; use xtra::KeepRunning; @@ -200,33 +201,6 @@ impl Actor { Ok(()) } - async fn handle_propose_roll_over(&mut self, order_id: OrderId) -> Result<()> { - if self.current_pending_proposals.contains_key(&order_id) { - anyhow::bail!("An update for order id {} is already in progress", order_id) - } - - let proposal = RollOverProposal { - order_id, - timestamp: SystemTime::now(), - }; - - self.current_pending_proposals.insert( - proposal.order_id, - UpdateCfdProposal::RollOverProposal { - proposal: proposal.clone(), - direction: SettlementKind::Outgoing, - }, - ); - self.send_pending_update_proposals()?; - - self.send_to_maker - .do_send(wire::TakerToMaker::ProposeRollOver { - order_id: proposal.order_id, - timestamp: proposal.timestamp, - })?; - Ok(()) - } - async fn handle_order_rejected(&mut self, order_id: OrderId) -> Result<()> { tracing::debug!(%order_id, "Order rejected"); @@ -343,6 +317,40 @@ where } Ok(()) } + + async fn handle_propose_roll_over(&mut self, order_id: OrderId) -> Result<()> { + if self.current_pending_proposals.contains_key(&order_id) { + anyhow::bail!("An update for order id {} is already in progress", order_id) + } + + let proposal = RollOverProposal { + order_id, + timestamp: SystemTime::now(), + }; + + self.current_pending_proposals.insert( + proposal.order_id, + UpdateCfdProposal::RollOverProposal { + proposal: proposal.clone(), + direction: SettlementKind::Outgoing, + }, + ); + self.send_pending_update_proposals()?; + + // we are likely going to need this one + self.oracle_actor + .send(oracle::FetchAnnouncement(oracle::next_announcement_after( + OffsetDateTime::now_utc() + Order::TERM, + )?)) + .await?; + + self.send_to_maker + .do_send(wire::TakerToMaker::ProposeRollOver { + order_id: proposal.order_id, + timestamp: proposal.timestamp, + })?; + Ok(()) + } } impl Actor @@ -608,7 +616,10 @@ impl Handler for Actor { } #[async_trait] -impl Handler for Actor { +impl Handler for Actor +where + O: xtra::Handler, +{ async fn handle(&mut self, msg: CfdAction, _ctx: &mut Context) { use CfdAction::*;