From 0816da8cd2e352059a3f7b33b2bea4c60e59d1bf Mon Sep 17 00:00:00 2001 From: Mariusz Klochowicz Date: Thu, 16 Dec 2021 14:39:46 +1030 Subject: [PATCH] Use generic completed enum inside collaborative settlement actors Continue the roll-out of generic completed enum. --- daemon/src/collab_settlement_maker.rs | 37 +++++++++---------------- daemon/src/collab_settlement_taker.rs | 39 +++++++++++---------------- daemon/src/maker_cfd.rs | 16 +++++------ daemon/src/model/cfd.rs | 17 ++++++++++++ daemon/src/taker_cfd.rs | 15 +++++------ 5 files changed, 57 insertions(+), 67 deletions(-) diff --git a/daemon/src/collab_settlement_maker.rs b/daemon/src/collab_settlement_maker.rs index 30da89c..79f5b50 100644 --- a/daemon/src/collab_settlement_maker.rs +++ b/daemon/src/collab_settlement_maker.rs @@ -3,7 +3,7 @@ use crate::address_map::Stopping; use crate::maker_inc_connections; use crate::model::cfd::Cfd; use crate::model::cfd::CollaborativeSettlement; -use crate::model::cfd::OrderId; +use crate::model::cfd::MakerSettlementCompleted; use crate::model::cfd::Role; use crate::model::cfd::SettlementKind; use crate::model::cfd::SettlementProposal; @@ -12,7 +12,6 @@ use crate::projection; use crate::xtra_ext::LogFailure; use anyhow::Context; use async_trait::async_trait; -use bdk::bitcoin::Script; use maia::secp256k1_zkp::Signature; use xtra::prelude::MessageChannel; use xtra_productivity::xtra_productivity; @@ -20,28 +19,13 @@ use xtra_productivity::xtra_productivity; pub struct Actor { cfd: Cfd, projection: xtra::Address, - on_completed: Box>, + on_completed: Box>, proposal: SettlementProposal, taker_id: Identity, connections: Box>, on_stopping: Vec>>>, } -pub enum Completed { - Confirmed { - order_id: OrderId, - settlement: CollaborativeSettlement, - script_pubkey: Script, - }, - Rejected { - order_id: OrderId, - }, - Failed { - order_id: OrderId, - error: anyhow::Error, - }, -} - pub struct Accepted; pub struct Rejected; pub struct Initiated { @@ -90,14 +74,13 @@ impl Actor { self.update_proposal(None).await; - anyhow::Ok(Completed::Confirmed { + anyhow::Ok(MakerSettlementCompleted::Succeeded { order_id: self.cfd.id(), - settlement, - script_pubkey: dlc.script_pubkey_for(Role::Maker), + payload: (settlement, dlc.script_pubkey_for(Role::Maker)), }) } .await - .unwrap_or_else(|e| Completed::Failed { + .unwrap_or_else(|e| MakerSettlementCompleted::Failed { order_id: self.cfd.id(), error: e, }); @@ -137,7 +120,7 @@ impl Actor { cfd: Cfd, proposal: SettlementProposal, projection: xtra::Address, - on_completed: &(impl MessageChannel + 'static), + on_completed: &(impl MessageChannel + 'static), taker_id: Identity, connections: &(impl MessageChannel + 'static), (on_stopping0, on_stopping1): ( @@ -172,7 +155,11 @@ impl Actor { }; } - async fn complete(&mut self, completed: Completed, ctx: &mut xtra::Context) { + async fn complete( + &mut self, + completed: MakerSettlementCompleted, + ctx: &mut xtra::Context, + ) { let _ = self .on_completed .send(completed) @@ -213,7 +200,7 @@ impl Actor { .await .context("Failed inform taker about settlement decision") { - self.complete(Completed::Failed { order_id, error: e }, ctx) + self.complete(MakerSettlementCompleted::Failed { order_id, error: e }, ctx) .await; } } diff --git a/daemon/src/collab_settlement_taker.rs b/daemon/src/collab_settlement_taker.rs index 8605646..ab6dc96 100644 --- a/daemon/src/collab_settlement_taker.rs +++ b/daemon/src/collab_settlement_taker.rs @@ -3,9 +3,9 @@ use crate::address_map::Stopping; use crate::connection; use crate::model::cfd::Cfd; use crate::model::cfd::CollaborativeSettlement; -use crate::model::cfd::OrderId; use crate::model::cfd::SettlementKind; use crate::model::cfd::SettlementProposal; +use crate::model::cfd::TakerSettlementCompleted; use crate::model::Price; use crate::projection; use crate::send_async_safe::SendAsyncSafe; @@ -19,7 +19,7 @@ use xtra_productivity::xtra_productivity; pub struct Actor { cfd: Cfd, projection: xtra::Address, - on_completed: Box>, + on_completed: Box>, connection: xtra::Address, proposal: SettlementProposal, } @@ -28,7 +28,7 @@ impl Actor { pub fn new( cfd: Cfd, projection: xtra::Address, - on_completed: impl MessageChannel + 'static, + on_completed: impl MessageChannel + 'static, current_price: Price, connection: xtra::Address, n_payouts: usize, @@ -119,7 +119,11 @@ impl Actor { Ok(()) } - async fn complete(&mut self, completed: Completed, ctx: &mut xtra::Context) { + async fn complete( + &mut self, + completed: TakerSettlementCompleted, + ctx: &mut xtra::Context, + ) { let _ = self.on_completed.send(completed).await; ctx.stop(); @@ -133,7 +137,7 @@ impl xtra::Actor for Actor { if let Err(e) = self.propose(this).await { self.complete( - Completed::Failed { + TakerSettlementCompleted::Failed { order_id: self.cfd.id(), error: e, }, @@ -152,20 +156,6 @@ impl xtra::Actor for Actor { } } -pub enum Completed { - Confirmed { - order_id: OrderId, - settlement: CollaborativeSettlement, - }, - Rejected { - order_id: OrderId, - }, - Failed { - order_id: OrderId, - error: anyhow::Error, - }, -} - #[xtra_productivity] impl Actor { async fn handle( @@ -177,17 +167,18 @@ impl Actor { let completed = match msg { wire::maker_to_taker::Settlement::Confirm => match self.handle_confirmed().await { - Ok(settlement) => Completed::Confirmed { - settlement, + Ok(settlement) => TakerSettlementCompleted::Succeeded { order_id, + payload: settlement, }, - Err(e) => Completed::Failed { error: e, order_id }, + Err(e) => TakerSettlementCompleted::Failed { error: e, order_id }, }, wire::maker_to_taker::Settlement::Reject => { if let Err(e) = self.handle_rejected().await { - Completed::Failed { error: e, order_id } + // XXX: Should this be rejected_due_to(order_id, e) instead? + TakerSettlementCompleted::Failed { error: e, order_id } } else { - Completed::Rejected { order_id } + TakerSettlementCompleted::rejected(order_id) } } }; diff --git a/daemon/src/maker_cfd.rs b/daemon/src/maker_cfd.rs index 0e76659..e6bf1f5 100644 --- a/daemon/src/maker_cfd.rs +++ b/daemon/src/maker_cfd.rs @@ -9,6 +9,7 @@ use crate::maker_inc_connections; use crate::model::cfd::Cfd; use crate::model::cfd::CfdState; use crate::model::cfd::CfdStateCommon; +use crate::model::cfd::MakerSettlementCompleted; use crate::model::cfd::Order; use crate::model::cfd::OrderId; use crate::model::cfd::Origin; @@ -557,21 +558,16 @@ where M: xtra::Handler, W: xtra::Handler, { - async fn handle_settlement_completed( - &mut self, - msg: collab_settlement_maker::Completed, - ) -> Result<()> { - use collab_settlement_maker::Completed::*; + async fn handle_settlement_completed(&mut self, msg: MakerSettlementCompleted) -> Result<()> { let (order_id, settlement, script_pubkey) = match msg { - Confirmed { + MakerSettlementCompleted::Succeeded { order_id, - settlement, - script_pubkey, + payload: (settlement, script_pubkey), } => (order_id, settlement, script_pubkey), - Rejected { .. } => { + MakerSettlementCompleted::Rejected { .. } => { return Ok(()); } - Failed { order_id, error } => { + MakerSettlementCompleted::Failed { order_id, error } => { tracing::warn!(%order_id, "Collaborative settlement failed: {:#}", error); return Ok(()); } diff --git a/daemon/src/model/cfd.rs b/daemon/src/model/cfd.rs index 88b110f..07abc63 100644 --- a/daemon/src/model/cfd.rs +++ b/daemon/src/model/cfd.rs @@ -1851,6 +1851,14 @@ pub enum Completed

{ } impl

Completed

{ + pub fn order_id(&self) -> OrderId { + *match self { + Completed::Succeeded { order_id, .. } => order_id, + Completed::Rejected { order_id, .. } => order_id, + Completed::Failed { order_id, .. } => order_id, + } + } + pub fn rejected(order_id: OrderId) -> Self { Self::Rejected { order_id, @@ -1862,6 +1870,15 @@ impl

Completed

{ } } +/// Message sent from a taker collab settlement actor to the +/// cfd actor to notify that the settlement has finished. +pub type TakerSettlementCompleted = Completed; + +/// Message sent from a maker collab settlement actor to the +/// cfd actor to notify that the settlement has finished. +/// Payload contains CollaborativeSettlement and script pubkey. +pub type MakerSettlementCompleted = Completed<(CollaborativeSettlement, Script)>; + pub mod marker { /// Marker type for contract setup completion pub struct Setup; diff --git a/daemon/src/taker_cfd.rs b/daemon/src/taker_cfd.rs index d756920..7db23ec 100644 --- a/daemon/src/taker_cfd.rs +++ b/daemon/src/taker_cfd.rs @@ -13,6 +13,7 @@ use crate::model::cfd::OrderId; use crate::model::cfd::Origin; use crate::model::cfd::Role; use crate::model::cfd::SetupCompleted; +use crate::model::cfd::TakerSettlementCompleted; use crate::model::Identity; use crate::model::Price; use crate::model::Usd; @@ -153,19 +154,17 @@ where Ok(()) } - async fn handle_settlement_completed( - &mut self, - msg: collab_settlement_taker::Completed, - ) -> Result<()> { + async fn handle_settlement_completed(&mut self, msg: TakerSettlementCompleted) -> Result<()> { let (order_id, settlement) = match msg { - collab_settlement_taker::Completed::Confirmed { + TakerSettlementCompleted::Succeeded { order_id, - settlement, + payload: settlement, } => (order_id, settlement), - collab_settlement_taker::Completed::Rejected { .. } => { + TakerSettlementCompleted::Rejected { order_id, reason } => { + tracing::debug!(%order_id, "Collaborative settlement failed: {:#}", reason); return Ok(()); } - collab_settlement_taker::Completed::Failed { order_id, error } => { + TakerSettlementCompleted::Failed { order_id, error } => { tracing::warn!(%order_id, "Collaborative settlement failed: {:#}", error); return Ok(()); }