From b4b5cd7f570f2b181636237a5f4d0be7b648397e Mon Sep 17 00:00:00 2001 From: Daniel Karzel Date: Wed, 24 Nov 2021 12:41:58 +1100 Subject: [PATCH] Move settlement oracle event id into the Dlc Oracle event-id and Dlc have to exist together, one without the other is incomplete, hence we move the event id inside. Note that the ID is actually already know to the Dlc as part of the `cet` hashmap, but this was not cleaned up in this patch. This patch goes towards getting the model boundaries right to transition the architecture towards an event model. --- daemon/src/maker_cfd.rs | 26 +++++++++++++------------- daemon/src/model/cfd.rs | 10 ++++++++-- daemon/src/monitor.rs | 14 +++++++------- daemon/src/oracle.rs | 12 ++++++------ daemon/src/setup_contract.rs | 3 +++ daemon/src/taker_cfd.rs | 32 +++++++++++++------------------- daemon/src/to_sse_event.rs | 5 ++++- 7 files changed, 54 insertions(+), 48 deletions(-) diff --git a/daemon/src/maker_cfd.rs b/daemon/src/maker_cfd.rs index 1a5ea9c..a31acdb 100644 --- a/daemon/src/maker_cfd.rs +++ b/daemon/src/maker_cfd.rs @@ -693,17 +693,13 @@ where self.monitor_actor .send(monitor::StartMonitoring { id: order_id, - params: MonitorParams::new( - dlc, - cfd.refund_timelock_in_blocks(), - cfd.order.oracle_event_id, - ), + params: MonitorParams::new(dlc.clone(), cfd.refund_timelock_in_blocks()), }) .await?; self.oracle_actor .send(oracle::MonitorAttestation { - event_id: cfd.order.oracle_event_id, + event_id: dlc.settlement_event_id, }) .await?; @@ -818,8 +814,9 @@ where impl Actor where M: xtra::Handler, + O: xtra::Handler, { - async fn handle_cfd_roll_over_completed( + async fn handle_roll_over_completed( &mut self, order_id: OrderId, dlc: Result, @@ -840,11 +837,13 @@ where self.monitor_actor .send(monitor::StartMonitoring { id: order_id, - params: MonitorParams::new( - dlc, - cfd.refund_timelock_in_blocks(), - cfd.order.oracle_event_id, - ), + params: MonitorParams::new(dlc.clone(), cfd.refund_timelock_in_blocks()), + }) + .await?; + + self.oracle_actor + .send(oracle::MonitorAttestation { + event_id: dlc.settlement_event_id, }) .await?; @@ -1025,9 +1024,10 @@ impl Handler where M: xtra::Handler, + O: xtra::Handler, { async fn handle(&mut self, msg: CfdRollOverCompleted, _ctx: &mut Context) { - log_error!(self.handle_cfd_roll_over_completed(msg.order_id, msg.dlc)); + log_error!(self.handle_roll_over_completed(msg.order_id, msg.dlc)); } } diff --git a/daemon/src/model/cfd.rs b/daemon/src/model/cfd.rs index 17dc8ba..daa2796 100644 --- a/daemon/src/model/cfd.rs +++ b/daemon/src/model/cfd.rs @@ -676,8 +676,8 @@ impl Cfd { .ceil() as u32 } - pub fn expiry_timestamp(&self) -> OffsetDateTime { - self.order.oracle_event_id.timestamp() + pub fn expiry_timestamp(&self) -> Option { + self.dlc().map(|dlc| dlc.settlement_event_id.timestamp) } /// A factor to be added to the CFD order settlement_interval for calculating the @@ -1506,6 +1506,12 @@ pub struct Dlc { pub taker_lock_amount: Amount, pub revoked_commit: Vec, + + // TODO: For now we store this seperately - it is a duplicate of what is stored in the cets + // hashmap. The cet hashmap allows storing cets for event-ids with different concern + // (settlement and liquidation-point). We should NOT make these fields public on the Dlc + // and create an internal structure that depicts this properly and avoids duplication. + pub settlement_event_id: BitMexPriceEventId, } impl Dlc { diff --git a/daemon/src/monitor.rs b/daemon/src/monitor.rs index f93613a..2685d1e 100644 --- a/daemon/src/monitor.rs +++ b/daemon/src/monitor.rs @@ -78,12 +78,12 @@ impl Actor { match cfd.state.clone() { // In PendingOpen we know the complete dlc setup and assume that the lock transaction will be published CfdState::PendingOpen { dlc, .. } => { - let params = MonitorParams::new(dlc.clone(), cfd.refund_timelock_in_blocks(), cfd.order.oracle_event_id); + let params = MonitorParams::new(dlc.clone(), cfd.refund_timelock_in_blocks()); actor.cfds.insert(cfd.order.id, params.clone()); actor.monitor_all(¶ms, cfd.order.id); } CfdState::Open { dlc, .. } | CfdState::PendingCommit { dlc, .. } => { - let params = MonitorParams::new(dlc.clone(), cfd.refund_timelock_in_blocks(), cfd.order.oracle_event_id); + let params = MonitorParams::new(dlc.clone(), cfd.refund_timelock_in_blocks()); actor.cfds.insert(cfd.order.id, params.clone()); actor.monitor_commit_finality(¶ms, cfd.order.id); @@ -99,7 +99,7 @@ impl Actor { } } CfdState::OpenCommitted { dlc, cet_status, .. } => { - let params = MonitorParams::new(dlc.clone(), cfd.refund_timelock_in_blocks(), cfd.order.oracle_event_id); + let params = MonitorParams::new(dlc.clone(), cfd.refund_timelock_in_blocks()); actor.cfds.insert(cfd.order.id, params.clone()); match cet_status { @@ -126,7 +126,7 @@ impl Actor { } } CfdState::PendingCet { dlc, attestation, .. } => { - let params = MonitorParams::new(dlc.clone(), cfd.refund_timelock_in_blocks(), cfd.order.oracle_event_id); + let params = MonitorParams::new(dlc.clone(), cfd.refund_timelock_in_blocks()); actor.cfds.insert(cfd.order.id, params.clone()); actor.monitor_cet_finality(map_cets(dlc.cets), attestation.into(), cfd.order.id)?; @@ -134,7 +134,7 @@ impl Actor { actor.monitor_refund_finality(¶ms,cfd.order.id); } CfdState::PendingRefund { dlc, .. } => { - let params = MonitorParams::new(dlc.clone(), cfd.refund_timelock_in_blocks(), cfd.order.oracle_event_id); + let params = MonitorParams::new(dlc.clone(), cfd.refund_timelock_in_blocks()); actor.cfds.insert(cfd.order.id, params.clone()); actor.monitor_commit_refund_timelock(¶ms, cfd.order.id); @@ -561,7 +561,7 @@ impl Event { } impl MonitorParams { - pub fn new(dlc: Dlc, refund_timelock_in_blocks: u32, event_id: BitMexPriceEventId) -> Self { + pub fn new(dlc: Dlc, refund_timelock_in_blocks: u32) -> Self { let script_pubkey = dlc.maker_address.script_pubkey(); MonitorParams { lock: (dlc.lock.0.txid(), dlc.lock.1), @@ -577,7 +577,7 @@ impl MonitorParams { .iter() .map(|rev_commit| (rev_commit.txid, rev_commit.script_pubkey.clone())) .collect(), - event_id, + event_id: dlc.settlement_event_id, } } } diff --git a/daemon/src/oracle.rs b/daemon/src/oracle.rs index edf2f0a..3e0f8b6 100644 --- a/daemon/src/oracle.rs +++ b/daemon/src/oracle.rs @@ -68,13 +68,13 @@ impl Actor { for cfd in cfds { match cfd.state.clone() { - CfdState::PendingOpen { .. } - | CfdState::Open { .. } - | CfdState::PendingCommit { .. } - | CfdState::OpenCommitted { .. } - | CfdState::PendingCet { .. } => + CfdState::PendingOpen { dlc, ..} + | CfdState::Open { dlc, .. } + | CfdState::PendingCommit { dlc, .. } + | CfdState::OpenCommitted { dlc, .. } + | CfdState::PendingCet { dlc, .. } => { - pending_attestations.insert(cfd.order.oracle_event_id); + pending_attestations.insert(dlc.settlement_event_id); } // Irrelevant for restart diff --git a/daemon/src/setup_contract.rs b/daemon/src/setup_contract.rs index e589e87..853740f 100644 --- a/daemon/src/setup_contract.rs +++ b/daemon/src/setup_contract.rs @@ -111,6 +111,7 @@ where ) } + let settlement_event_id = announcement.id; let payouts = HashMap::from_iter([( announcement.into(), payout_curve::calculate( @@ -289,6 +290,7 @@ where maker_lock_amount: params.maker().lock_amount, taker_lock_amount: params.taker().lock_amount, revoked_commit: Vec::new(), + settlement_event_id, }) } @@ -575,6 +577,7 @@ pub async fn roll_over( maker_lock_amount, taker_lock_amount, revoked_commit, + settlement_event_id: announcement.id, }) } diff --git a/daemon/src/taker_cfd.rs b/daemon/src/taker_cfd.rs index e8a5dd9..8d19dfc 100644 --- a/daemon/src/taker_cfd.rs +++ b/daemon/src/taker_cfd.rs @@ -434,17 +434,13 @@ where self.monitor_actor .send(monitor::StartMonitoring { id: order_id, - params: MonitorParams::new( - dlc, - cfd.refund_timelock_in_blocks(), - cfd.order.oracle_event_id, - ), + params: MonitorParams::new(dlc.clone(), cfd.refund_timelock_in_blocks()), }) .await?; self.oracle_actor .send(oracle::MonitorAttestation { - event_id: cfd.order.oracle_event_id, + event_id: dlc.settlement_event_id, }) .await?; @@ -483,12 +479,6 @@ where .await? .with_context(|| format!("Announcement {} not found", cfd.order.oracle_event_id))?; - self.oracle_actor - .send(oracle::MonitorAttestation { - event_id: offer_announcement.id, - }) - .await?; - let contract_future = setup_contract::new( self.send_to_maker .sink() @@ -609,8 +599,9 @@ where impl Actor where M: xtra::Handler, + O: xtra::Handler, { - async fn handle_cfd_roll_over_completed( + async fn handle_roll_over_completed( &mut self, order_id: OrderId, dlc: Result, @@ -632,11 +623,13 @@ where self.monitor_actor .send(monitor::StartMonitoring { id: order_id, - params: MonitorParams::new( - dlc, - cfd.refund_timelock_in_blocks(), - cfd.order.oracle_event_id, - ), + params: MonitorParams::new(dlc.clone(), cfd.refund_timelock_in_blocks()), + }) + .await?; + + self.oracle_actor + .send(oracle::MonitorAttestation { + event_id: dlc.settlement_event_id, }) .await?; @@ -796,9 +789,10 @@ where impl Handler for Actor where M: xtra::Handler, + O: xtra::Handler, { async fn handle(&mut self, msg: CfdRollOverCompleted, _ctx: &mut Context) { - log_error!(self.handle_cfd_roll_over_completed(msg.order_id, msg.dlc)); + log_error!(self.handle_roll_over_completed(msg.order_id, msg.dlc)); } } diff --git a/daemon/src/to_sse_event.rs b/daemon/src/to_sse_event.rs index 85c20ba..7c86f4b 100644 --- a/daemon/src/to_sse_event.rs +++ b/daemon/src/to_sse_event.rs @@ -318,7 +318,10 @@ impl ToSseEvent for CfdsWithAuxData { margin: cfd.margin().expect("margin to be available"), margin_counterparty: cfd.counterparty_margin().expect("margin to be available"), details, - expiry_timestamp: cfd.expiry_timestamp(), + expiry_timestamp: match cfd.expiry_timestamp() { + None => cfd.order.oracle_event_id.timestamp(), + Some(timestamp) => timestamp, + }, } }) .collect::>();