Browse Source

Merge #254

254: Minor cleanups in `oracle::Actor` r=thomaseizinger a=thomaseizinger



Co-authored-by: Thomas Eizinger <thomas@eizinger.io>
refactor/no-log-handler
bors[bot] 3 years ago
committed by GitHub
parent
commit
52fa57a144
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      Cargo.lock
  2. 59
      daemon/src/oracle.rs

2
Cargo.lock

@ -3124,7 +3124,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",

59
daemon/src/oracle.rs

@ -18,22 +18,14 @@ use time::ext::NumericalDuration;
const OLIVIA_EVENT_TIME_FORMAT: &[FormatItem] =
format_description!("[year]-[month]-[day]T[hour]:[minute]:[second]");
pub struct Actor<CFD, M>
where
CFD: xtra::Handler<Attestation>,
M: xtra::Handler<Attestation>,
{
pub struct Actor<CFD, M> {
latest_announcements: HashMap<OracleEventId, Announcement>,
pending_attestations: HashSet<OracleEventId>,
cfd_actor_address: xtra::Address<CFD>,
monitor_actor_address: xtra::Address<M>,
}
impl<CFD, M> Actor<CFD, M>
where
CFD: xtra::Handler<Attestation>,
M: xtra::Handler<Attestation>,
{
impl<CFD, M> Actor<CFD, M> {
pub fn new(
cfd_actor_address: xtra::Address<CFD>,
monitor_actor_address: xtra::Address<M>,
@ -73,7 +65,13 @@ where
monitor_actor_address,
}
}
}
impl<CFD, M> Actor<CFD, M>
where
CFD: xtra::Handler<Attestation>,
M: xtra::Handler<Attestation>,
{
async fn update_state(&mut self) -> Result<()> {
self.update_latest_announcements()
.await
@ -86,7 +84,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();
@ -105,11 +103,9 @@ where
Result::<_, anyhow::Error>::Ok((event_id, announcement))
})
.collect::<FuturesOrdered<_>>()
.try_collect::<HashMap<OracleEventId, Announcement>>()
.try_collect()
.await?;
self.latest_announcements = new_announcements;
Ok(())
}
@ -150,24 +146,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<Announcement> {
self.latest_announcements.get(&event_id).cloned()
}
}
impl<CFD, M> xtra::Actor for Actor<CFD, M>
where
CFD: xtra::Handler<Attestation>,
M: xtra::Handler<Attestation>,
{
}
impl<CFD: 'static, M: 'static> xtra::Actor for Actor<CFD, M> {}
pub struct Sync;
@ -195,13 +176,11 @@ impl xtra::Message for MonitorEvent {
}
#[async_trait]
impl<CFD, M> xtra::Handler<MonitorEvent> for Actor<CFD, M>
where
CFD: xtra::Handler<Attestation>,
M: xtra::Handler<Attestation>,
{
impl<CFD: 'static, M: 'static> xtra::Handler<MonitorEvent> for Actor<CFD, M> {
async fn handle(&mut self, msg: MonitorEvent, _ctx: &mut xtra::Context<Self>) {
self.monitor_event(msg.event_id)
if !self.pending_attestations.insert(msg.event_id.clone()) {
tracing::trace!("Event {} already being monitored", msg.event_id);
}
}
}
@ -209,17 +188,13 @@ where
pub struct GetAnnouncement(pub OracleEventId);
#[async_trait]
impl<CFD, M> xtra::Handler<GetAnnouncement> for Actor<CFD, M>
where
CFD: xtra::Handler<Attestation>,
M: xtra::Handler<Attestation>,
{
impl<CFD: 'static, M: 'static> xtra::Handler<GetAnnouncement> for Actor<CFD, M> {
async fn handle(
&mut self,
msg: GetAnnouncement,
_ctx: &mut xtra::Context<Self>,
) -> Option<Announcement> {
self.handle_get_announcement(msg.0)
self.latest_announcements.get(&msg.0).cloned()
}
}

Loading…
Cancel
Save