Browse Source

Merge #910

910: Use generic completed enum inside collaborative settlement actors r=klochowicz a=klochowicz

Continue the roll-out of generic completed enum.

Co-authored-by: Mariusz Klochowicz <mariusz@klochowicz.com>
update-blockstream-electrum-server-url
bors[bot] 3 years ago
committed by GitHub
parent
commit
8be8744049
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 37
      daemon/src/collab_settlement_maker.rs
  2. 39
      daemon/src/collab_settlement_taker.rs
  3. 16
      daemon/src/maker_cfd.rs
  4. 17
      daemon/src/model/cfd.rs
  5. 15
      daemon/src/taker_cfd.rs

37
daemon/src/collab_settlement_maker.rs

@ -3,7 +3,7 @@ use crate::address_map::Stopping;
use crate::maker_inc_connections; use crate::maker_inc_connections;
use crate::model::cfd::Cfd; use crate::model::cfd::Cfd;
use crate::model::cfd::CollaborativeSettlement; use crate::model::cfd::CollaborativeSettlement;
use crate::model::cfd::OrderId; use crate::model::cfd::MakerSettlementCompleted;
use crate::model::cfd::Role; use crate::model::cfd::Role;
use crate::model::cfd::SettlementKind; use crate::model::cfd::SettlementKind;
use crate::model::cfd::SettlementProposal; use crate::model::cfd::SettlementProposal;
@ -12,7 +12,6 @@ use crate::projection;
use crate::xtra_ext::LogFailure; use crate::xtra_ext::LogFailure;
use anyhow::Context; use anyhow::Context;
use async_trait::async_trait; use async_trait::async_trait;
use bdk::bitcoin::Script;
use maia::secp256k1_zkp::Signature; use maia::secp256k1_zkp::Signature;
use xtra::prelude::MessageChannel; use xtra::prelude::MessageChannel;
use xtra_productivity::xtra_productivity; use xtra_productivity::xtra_productivity;
@ -20,28 +19,13 @@ use xtra_productivity::xtra_productivity;
pub struct Actor { pub struct Actor {
cfd: Cfd, cfd: Cfd,
projection: xtra::Address<projection::Actor>, projection: xtra::Address<projection::Actor>,
on_completed: Box<dyn MessageChannel<Completed>>, on_completed: Box<dyn MessageChannel<MakerSettlementCompleted>>,
proposal: SettlementProposal, proposal: SettlementProposal,
taker_id: Identity, taker_id: Identity,
connections: Box<dyn MessageChannel<maker_inc_connections::settlement::Response>>, connections: Box<dyn MessageChannel<maker_inc_connections::settlement::Response>>,
on_stopping: Vec<Box<dyn MessageChannel<Stopping<Self>>>>, on_stopping: Vec<Box<dyn MessageChannel<Stopping<Self>>>>,
} }
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 Accepted;
pub struct Rejected; pub struct Rejected;
pub struct Initiated { pub struct Initiated {
@ -90,14 +74,13 @@ impl Actor {
self.update_proposal(None).await; self.update_proposal(None).await;
anyhow::Ok(Completed::Confirmed { anyhow::Ok(MakerSettlementCompleted::Succeeded {
order_id: self.cfd.id(), order_id: self.cfd.id(),
settlement, payload: (settlement, dlc.script_pubkey_for(Role::Maker)),
script_pubkey: dlc.script_pubkey_for(Role::Maker),
}) })
} }
.await .await
.unwrap_or_else(|e| Completed::Failed { .unwrap_or_else(|e| MakerSettlementCompleted::Failed {
order_id: self.cfd.id(), order_id: self.cfd.id(),
error: e, error: e,
}); });
@ -137,7 +120,7 @@ impl Actor {
cfd: Cfd, cfd: Cfd,
proposal: SettlementProposal, proposal: SettlementProposal,
projection: xtra::Address<projection::Actor>, projection: xtra::Address<projection::Actor>,
on_completed: &(impl MessageChannel<Completed> + 'static), on_completed: &(impl MessageChannel<MakerSettlementCompleted> + 'static),
taker_id: Identity, taker_id: Identity,
connections: &(impl MessageChannel<maker_inc_connections::settlement::Response> + 'static), connections: &(impl MessageChannel<maker_inc_connections::settlement::Response> + 'static),
(on_stopping0, on_stopping1): ( (on_stopping0, on_stopping1): (
@ -172,7 +155,11 @@ impl Actor {
}; };
} }
async fn complete(&mut self, completed: Completed, ctx: &mut xtra::Context<Self>) { async fn complete(
&mut self,
completed: MakerSettlementCompleted,
ctx: &mut xtra::Context<Self>,
) {
let _ = self let _ = self
.on_completed .on_completed
.send(completed) .send(completed)
@ -213,7 +200,7 @@ impl Actor {
.await .await
.context("Failed inform taker about settlement decision") .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; .await;
} }
} }

39
daemon/src/collab_settlement_taker.rs

@ -3,9 +3,9 @@ use crate::address_map::Stopping;
use crate::connection; use crate::connection;
use crate::model::cfd::Cfd; use crate::model::cfd::Cfd;
use crate::model::cfd::CollaborativeSettlement; use crate::model::cfd::CollaborativeSettlement;
use crate::model::cfd::OrderId;
use crate::model::cfd::SettlementKind; use crate::model::cfd::SettlementKind;
use crate::model::cfd::SettlementProposal; use crate::model::cfd::SettlementProposal;
use crate::model::cfd::TakerSettlementCompleted;
use crate::model::Price; use crate::model::Price;
use crate::projection; use crate::projection;
use crate::send_async_safe::SendAsyncSafe; use crate::send_async_safe::SendAsyncSafe;
@ -19,7 +19,7 @@ use xtra_productivity::xtra_productivity;
pub struct Actor { pub struct Actor {
cfd: Cfd, cfd: Cfd,
projection: xtra::Address<projection::Actor>, projection: xtra::Address<projection::Actor>,
on_completed: Box<dyn MessageChannel<Completed>>, on_completed: Box<dyn MessageChannel<TakerSettlementCompleted>>,
connection: xtra::Address<connection::Actor>, connection: xtra::Address<connection::Actor>,
proposal: SettlementProposal, proposal: SettlementProposal,
} }
@ -28,7 +28,7 @@ impl Actor {
pub fn new( pub fn new(
cfd: Cfd, cfd: Cfd,
projection: xtra::Address<projection::Actor>, projection: xtra::Address<projection::Actor>,
on_completed: impl MessageChannel<Completed> + 'static, on_completed: impl MessageChannel<TakerSettlementCompleted> + 'static,
current_price: Price, current_price: Price,
connection: xtra::Address<connection::Actor>, connection: xtra::Address<connection::Actor>,
n_payouts: usize, n_payouts: usize,
@ -119,7 +119,11 @@ impl Actor {
Ok(()) Ok(())
} }
async fn complete(&mut self, completed: Completed, ctx: &mut xtra::Context<Self>) { async fn complete(
&mut self,
completed: TakerSettlementCompleted,
ctx: &mut xtra::Context<Self>,
) {
let _ = self.on_completed.send(completed).await; let _ = self.on_completed.send(completed).await;
ctx.stop(); ctx.stop();
@ -133,7 +137,7 @@ impl xtra::Actor for Actor {
if let Err(e) = self.propose(this).await { if let Err(e) = self.propose(this).await {
self.complete( self.complete(
Completed::Failed { TakerSettlementCompleted::Failed {
order_id: self.cfd.id(), order_id: self.cfd.id(),
error: e, 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] #[xtra_productivity]
impl Actor { impl Actor {
async fn handle( async fn handle(
@ -177,17 +167,18 @@ impl Actor {
let completed = match msg { let completed = match msg {
wire::maker_to_taker::Settlement::Confirm => match self.handle_confirmed().await { wire::maker_to_taker::Settlement::Confirm => match self.handle_confirmed().await {
Ok(settlement) => Completed::Confirmed { Ok(settlement) => TakerSettlementCompleted::Succeeded {
settlement,
order_id, 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 => { wire::maker_to_taker::Settlement::Reject => {
if let Err(e) = self.handle_rejected().await { 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 { } else {
Completed::Rejected { order_id } TakerSettlementCompleted::rejected(order_id)
} }
} }
}; };

16
daemon/src/maker_cfd.rs

@ -9,6 +9,7 @@ use crate::maker_inc_connections;
use crate::model::cfd::Cfd; use crate::model::cfd::Cfd;
use crate::model::cfd::CfdState; use crate::model::cfd::CfdState;
use crate::model::cfd::CfdStateCommon; use crate::model::cfd::CfdStateCommon;
use crate::model::cfd::MakerSettlementCompleted;
use crate::model::cfd::Order; use crate::model::cfd::Order;
use crate::model::cfd::OrderId; use crate::model::cfd::OrderId;
use crate::model::cfd::Origin; use crate::model::cfd::Origin;
@ -557,21 +558,16 @@ where
M: xtra::Handler<monitor::CollaborativeSettlement>, M: xtra::Handler<monitor::CollaborativeSettlement>,
W: xtra::Handler<wallet::TryBroadcastTransaction>, W: xtra::Handler<wallet::TryBroadcastTransaction>,
{ {
async fn handle_settlement_completed( async fn handle_settlement_completed(&mut self, msg: MakerSettlementCompleted) -> Result<()> {
&mut self,
msg: collab_settlement_maker::Completed,
) -> Result<()> {
use collab_settlement_maker::Completed::*;
let (order_id, settlement, script_pubkey) = match msg { let (order_id, settlement, script_pubkey) = match msg {
Confirmed { MakerSettlementCompleted::Succeeded {
order_id, order_id,
settlement, payload: (settlement, script_pubkey),
script_pubkey,
} => (order_id, settlement, script_pubkey), } => (order_id, settlement, script_pubkey),
Rejected { .. } => { MakerSettlementCompleted::Rejected { .. } => {
return Ok(()); return Ok(());
} }
Failed { order_id, error } => { MakerSettlementCompleted::Failed { order_id, error } => {
tracing::warn!(%order_id, "Collaborative settlement failed: {:#}", error); tracing::warn!(%order_id, "Collaborative settlement failed: {:#}", error);
return Ok(()); return Ok(());
} }

17
daemon/src/model/cfd.rs

@ -1851,6 +1851,14 @@ pub enum Completed<P> {
} }
impl<P> Completed<P> { impl<P> Completed<P> {
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 { pub fn rejected(order_id: OrderId) -> Self {
Self::Rejected { Self::Rejected {
order_id, order_id,
@ -1862,6 +1870,15 @@ impl<P> Completed<P> {
} }
} }
/// Message sent from a taker collab settlement actor to the
/// cfd actor to notify that the settlement has finished.
pub type TakerSettlementCompleted = Completed<CollaborativeSettlement>;
/// 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 { pub mod marker {
/// Marker type for contract setup completion /// Marker type for contract setup completion
pub struct Setup; pub struct Setup;

15
daemon/src/taker_cfd.rs

@ -13,6 +13,7 @@ use crate::model::cfd::OrderId;
use crate::model::cfd::Origin; use crate::model::cfd::Origin;
use crate::model::cfd::Role; use crate::model::cfd::Role;
use crate::model::cfd::SetupCompleted; use crate::model::cfd::SetupCompleted;
use crate::model::cfd::TakerSettlementCompleted;
use crate::model::Identity; use crate::model::Identity;
use crate::model::Price; use crate::model::Price;
use crate::model::Usd; use crate::model::Usd;
@ -153,19 +154,17 @@ where
Ok(()) Ok(())
} }
async fn handle_settlement_completed( async fn handle_settlement_completed(&mut self, msg: TakerSettlementCompleted) -> Result<()> {
&mut self,
msg: collab_settlement_taker::Completed,
) -> Result<()> {
let (order_id, settlement) = match msg { let (order_id, settlement) = match msg {
collab_settlement_taker::Completed::Confirmed { TakerSettlementCompleted::Succeeded {
order_id, order_id,
settlement, payload: settlement,
} => (order_id, settlement), } => (order_id, settlement),
collab_settlement_taker::Completed::Rejected { .. } => { TakerSettlementCompleted::Rejected { order_id, reason } => {
tracing::debug!(%order_id, "Collaborative settlement failed: {:#}", reason);
return Ok(()); return Ok(());
} }
collab_settlement_taker::Completed::Failed { order_id, error } => { TakerSettlementCompleted::Failed { order_id, error } => {
tracing::warn!(%order_id, "Collaborative settlement failed: {:#}", error); tracing::warn!(%order_id, "Collaborative settlement failed: {:#}", error);
return Ok(()); return Ok(());
} }

Loading…
Cancel
Save