Browse Source

Merge #690

690: Move settlement oracle event id into the Dlc r=da-kami a=da-kami

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.

Note: This would theoretically allow us to land auto-rollover, because we don't now pass the correct event-id through. However, we decided that it's better to pull the architecture refactoring through before landing the rollover feature 😬

Co-authored-by: Daniel Karzel <daniel@comit.network>
debug-collab-settlement
bors[bot] 3 years ago
committed by GitHub
parent
commit
2c154f5854
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 32
      daemon/src/maker_cfd.rs
  2. 10
      daemon/src/model/cfd.rs
  3. 14
      daemon/src/monitor.rs
  4. 12
      daemon/src/oracle.rs
  5. 3
      daemon/src/setup_contract.rs
  6. 32
      daemon/src/taker_cfd.rs
  7. 5
      daemon/src/to_sse_event.rs

32
daemon/src/maker_cfd.rs

@ -724,17 +724,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?;
@ -793,12 +789,6 @@ where
})
.await??;
self.oracle_actor
.send(oracle::MonitorAttestation {
event_id: announcement.id,
})
.await?;
let (sender, receiver) = mpsc::unbounded();
let contract_future = setup_contract::roll_over(
self.takers.clone().into_sink().with(move |msg| {
@ -849,8 +839,9 @@ where
impl<O, M, T, W> Actor<O, M, T, W>
where
M: xtra::Handler<monitor::StartMonitoring>,
O: xtra::Handler<oracle::MonitorAttestation>,
{
async fn handle_cfd_roll_over_completed(
async fn handle_roll_over_completed(
&mut self,
order_id: OrderId,
dlc: Result<Dlc>,
@ -871,11 +862,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?;
@ -1067,9 +1060,10 @@ impl<O: 'static, M: 'static, T: 'static, W: 'static> Handler<CfdRollOverComplete
for Actor<O, M, T, W>
where
M: xtra::Handler<monitor::StartMonitoring>,
O: xtra::Handler<oracle::MonitorAttestation>,
{
async fn handle(&mut self, msg: CfdRollOverCompleted, _ctx: &mut Context<Self>) {
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));
}
}

10
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<OffsetDateTime> {
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<RevokedCommit>,
// 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 {

14
daemon/src/monitor.rs

@ -78,12 +78,12 @@ impl Actor<bdk::electrum_client::Client> {
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(&params, 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(&params, cfd.order.id);
@ -99,7 +99,7 @@ impl Actor<bdk::electrum_client::Client> {
}
}
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<bdk::electrum_client::Client> {
}
}
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<bdk::electrum_client::Client> {
actor.monitor_refund_finality(&params,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(&params, 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,
}
}
}

12
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

3
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,
})
}

32
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<O: 'static, M: 'static, W: 'static> Actor<O, M, W>
where
M: xtra::Handler<monitor::StartMonitoring>,
O: xtra::Handler<oracle::MonitorAttestation>,
{
async fn handle_cfd_roll_over_completed(
async fn handle_roll_over_completed(
&mut self,
order_id: OrderId,
dlc: Result<Dlc>,
@ -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<O: 'static, M: 'static, W: 'static> Handler<CfdRollOverCompleted> for Actor<O, M, W>
where
M: xtra::Handler<monitor::StartMonitoring>,
O: xtra::Handler<oracle::MonitorAttestation>,
{
async fn handle(&mut self, msg: CfdRollOverCompleted, _ctx: &mut Context<Self>) {
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));
}
}

5
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::<Vec<Cfd>>();

Loading…
Cancel
Save