From 61d62e72630e82a39dae53eac9375a9f20c5e477 Mon Sep 17 00:00:00 2001 From: Daniel Karzel Date: Tue, 5 Oct 2021 20:31:47 +1100 Subject: [PATCH] Taker uses latest announcements from oracle to start contract setup Opted not to implement an additional message to fetch a specific announcement from the Oracle actor because it would have made the oracle actor unnecessarily complex (We would have to split it into different functionality by trait). The taker cfd actor holds a sliding window of all the announcements received from the oracle actor. --- daemon/src/maker_cfd.rs | 2 +- daemon/src/oracle.rs | 1 + daemon/src/taker_cfd.rs | 38 +++++++++++++++++++++----------------- 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/daemon/src/maker_cfd.rs b/daemon/src/maker_cfd.rs index 2a9374e..85dae13 100644 --- a/daemon/src/maker_cfd.rs +++ b/daemon/src/maker_cfd.rs @@ -12,7 +12,7 @@ use crate::model::{TakerId, Usd}; use crate::monitor::MonitorParams; use crate::wallet::Wallet; use crate::{maker_inc_connections, monitor, oracle, setup_contract, wire}; -use anyhow::{Context as _, Result}; +use anyhow::{bail, Context as _, Result}; use async_trait::async_trait; use bdk::bitcoin::secp256k1::schnorrsig; use futures::channel::mpsc; diff --git a/daemon/src/oracle.rs b/daemon/src/oracle.rs index 95e5e15..38fc04a 100644 --- a/daemon/src/oracle.rs +++ b/daemon/src/oracle.rs @@ -208,6 +208,7 @@ pub struct Announcement { pub nonce_pks: Vec, } +#[derive(Debug, Clone)] pub struct Announcements(pub [Announcement; 24]); // TODO: Implement real deserialization once price attestation is diff --git a/daemon/src/taker_cfd.rs b/daemon/src/taker_cfd.rs index ad92ca7..8a056c8 100644 --- a/daemon/src/taker_cfd.rs +++ b/daemon/src/taker_cfd.rs @@ -63,8 +63,8 @@ pub struct Actor { send_to_maker: Address>, monitor_actor: Address>, setup_state: SetupState, - latest_announcement: Option, - _oracle_actor: Address>>, + latest_announcements: Option, + oracle_actor: Address>>, current_settlement_proposals: HashMap, } @@ -91,8 +91,8 @@ impl Actor { send_to_maker, monitor_actor, setup_state: SetupState::None, - latest_announcement: None, - _oracle_actor: oracle_actor, + oracle_actor, + latest_announcements: None, current_settlement_proposals: HashMap::new(), } } @@ -213,18 +213,23 @@ impl Actor { .send(load_all_cfds(&mut conn).await?)?; let cfd = load_cfd_by_order_id(order_id, &mut conn).await?; - // let latest_announcement = self - // .latest_announcement - // .to_owned() - // .context("Unaware of oracle's latest announcement.")?; - - // self.oracle_actor - // .do_send_async(oracle::MonitorEvent { - // event_id: latest_announcement.id, - // }) - // .await?; + let offer_announcements = self + .latest_announcements + .clone() + .context("No oracle announcements available")?; + let offer_announcement = offer_announcements + .0 + .iter() + .find(|announcement| announcement.id == cfd.order.oracle_event_id) + .context("Order's announcement not found in list of current oracle announcements")?; + + self.oracle_actor + .do_send_async(oracle::MonitorEvent { + event_id: offer_announcement.id.clone(), + }) + .await?; - let nonce_pks = Vec::new(); + let nonce_pks = offer_announcement.nonce_pks.clone(); let contract_future = setup_contract::new( self.send_to_maker @@ -392,8 +397,7 @@ impl Actor { &mut self, announcements: oracle::Announcements, ) -> Result<()> { - tracing::debug!("Updating latest oracle announcements"); - self.latest_announcement = Some(announcements.0.last().unwrap().clone()); + self.latest_announcements = Some(announcements); Ok(()) }