From 826d2fc7bcd37a3ce558954cecc456dab7df8e5a Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Mon, 11 Oct 2021 09:35:03 +1100 Subject: [PATCH 1/5] Bump xtra version to reduce trait-bound noise See https://github.com/Restioson/xtra/pull/42. --- Cargo.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 628d528..dc44da2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3126,7 +3126,7 @@ dependencies = [ [[package]] name = "xtra" version = "0.6.0" -source = "git+https://github.com/Restioson/xtra#1ba8cb3f98502363fdea8008b47b9c9b5eba6b5f" +source = "git+https://github.com/Restioson/xtra#7ae9140dbc30127aaa5f701078b72ac143babeb4" dependencies = [ "async-trait", "barrage", From 232d80397902156f7863bbb28b2ce56c15776b26 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Fri, 8 Oct 2021 12:08:04 +1100 Subject: [PATCH 2/5] Simplify trait bounds on oracle::Actor --- daemon/src/oracle.rs | 57 ++++++++++++++++---------------------------- 1 file changed, 21 insertions(+), 36 deletions(-) diff --git a/daemon/src/oracle.rs b/daemon/src/oracle.rs index bf352a0..bda7e69 100644 --- a/daemon/src/oracle.rs +++ b/daemon/src/oracle.rs @@ -16,22 +16,14 @@ use std::collections::{HashMap, HashSet}; const OLIVIA_EVENT_TIME_FORMAT: &[FormatItem] = format_description!("[year]-[month]-[day]T[hour]:[minute]:[second]"); -pub struct Actor -where - CFD: xtra::Handler, - M: xtra::Handler, -{ +pub struct Actor { latest_announcements: HashMap, pending_attestations: HashSet, cfd_actor_address: xtra::Address, monitor_actor_address: xtra::Address, } -impl Actor -where - CFD: xtra::Handler, - M: xtra::Handler, -{ +impl Actor { pub fn new( cfd_actor_address: xtra::Address, monitor_actor_address: xtra::Address, @@ -72,6 +64,22 @@ where } } + fn monitor_event(&mut self, event_id: OracleEventId) { + if !self.pending_attestations.insert(event_id.clone()) { + tracing::trace!("Event {} already being monitored", event_id); + } + } + + fn handle_get_announcement(&self, event_id: OracleEventId) -> Option { + self.latest_announcements.get(&event_id).cloned() + } +} + +impl Actor +where + CFD: xtra::Handler, + M: xtra::Handler, +{ async fn update_state(&mut self) -> Result<()> { self.update_latest_announcements() .await @@ -148,24 +156,9 @@ where Ok(()) } - - fn monitor_event(&mut self, event_id: OracleEventId) { - if !self.pending_attestations.insert(event_id.clone()) { - tracing::trace!("Event {} already being monitored", event_id); - } - } - - pub fn handle_get_announcement(&self, event_id: OracleEventId) -> Option { - self.latest_announcements.get(&event_id).cloned() - } } -impl xtra::Actor for Actor -where - CFD: xtra::Handler, - M: xtra::Handler, -{ -} +impl xtra::Actor for Actor {} pub struct Sync; @@ -193,11 +186,7 @@ impl xtra::Message for MonitorEvent { } #[async_trait] -impl xtra::Handler for Actor -where - CFD: xtra::Handler, - M: xtra::Handler, -{ +impl xtra::Handler for Actor { async fn handle(&mut self, msg: MonitorEvent, _ctx: &mut xtra::Context) { self.monitor_event(msg.event_id) } @@ -207,11 +196,7 @@ where pub struct GetAnnouncement(pub OracleEventId); #[async_trait] -impl xtra::Handler for Actor -where - CFD: xtra::Handler, - M: xtra::Handler, -{ +impl xtra::Handler for Actor { async fn handle( &mut self, msg: GetAnnouncement, From ab3325e1ec0ef7f222815b31e24aafacff6c7c9b Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Fri, 8 Oct 2021 12:08:55 +1100 Subject: [PATCH 3/5] Let type-inference to its thing --- daemon/src/oracle.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/daemon/src/oracle.rs b/daemon/src/oracle.rs index bda7e69..86caf3a 100644 --- a/daemon/src/oracle.rs +++ b/daemon/src/oracle.rs @@ -92,7 +92,7 @@ where } async fn update_latest_announcements(&mut self) -> Result<()> { - let new_announcements = next_ids() + self.latest_announcements = next_ids() .into_iter() .map(|event_id| async move { let url = event_id.to_olivia_url(); @@ -111,11 +111,9 @@ where Result::<_, anyhow::Error>::Ok((event_id, announcement)) }) .collect::>() - .try_collect::>() + .try_collect() .await?; - self.latest_announcements = new_announcements; - Ok(()) } From b344a8503450b9134edea5c7f610ddda2012ac43 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Fri, 8 Oct 2021 12:09:39 +1100 Subject: [PATCH 4/5] Remove dbg! --- daemon/src/oracle.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/daemon/src/oracle.rs b/daemon/src/oracle.rs index 86caf3a..708081d 100644 --- a/daemon/src/oracle.rs +++ b/daemon/src/oracle.rs @@ -137,7 +137,7 @@ where } }; - let attestation = dbg!(res).json::().await?; + let attestation = res.json::().await?; self.cfd_actor_address .clone() From 7d5da90294118a9e87f017887c57d4d38bde67b6 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Fri, 8 Oct 2021 12:12:55 +1100 Subject: [PATCH 5/5] Inline simple message handlers --- daemon/src/oracle.rs | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/daemon/src/oracle.rs b/daemon/src/oracle.rs index 708081d..a997003 100644 --- a/daemon/src/oracle.rs +++ b/daemon/src/oracle.rs @@ -63,16 +63,6 @@ impl Actor { monitor_actor_address, } } - - fn monitor_event(&mut self, event_id: OracleEventId) { - if !self.pending_attestations.insert(event_id.clone()) { - tracing::trace!("Event {} already being monitored", event_id); - } - } - - fn handle_get_announcement(&self, event_id: OracleEventId) -> Option { - self.latest_announcements.get(&event_id).cloned() - } } impl Actor @@ -186,7 +176,9 @@ impl xtra::Message for MonitorEvent { #[async_trait] impl xtra::Handler for Actor { async fn handle(&mut self, msg: MonitorEvent, _ctx: &mut xtra::Context) { - self.monitor_event(msg.event_id) + if !self.pending_attestations.insert(msg.event_id.clone()) { + tracing::trace!("Event {} already being monitored", msg.event_id); + } } } @@ -200,7 +192,7 @@ impl xtra::Handler for Actor msg: GetAnnouncement, _ctx: &mut xtra::Context, ) -> Option { - self.handle_get_announcement(msg.0) + self.latest_announcements.get(&msg.0).cloned() } }