Browse Source

Use generic completed enum inside collaborative settlement actors

Continue the roll-out of generic completed enum.
update-blockstream-electrum-server-url
Mariusz Klochowicz 3 years ago
parent
commit
0816da8cd2
No known key found for this signature in database GPG Key ID: 470C865699C8D4D
  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::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<projection::Actor>,
on_completed: Box<dyn MessageChannel<Completed>>,
on_completed: Box<dyn MessageChannel<MakerSettlementCompleted>>,
proposal: SettlementProposal,
taker_id: Identity,
connections: Box<dyn MessageChannel<maker_inc_connections::settlement::Response>>,
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 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<projection::Actor>,
on_completed: &(impl MessageChannel<Completed> + 'static),
on_completed: &(impl MessageChannel<MakerSettlementCompleted> + 'static),
taker_id: Identity,
connections: &(impl MessageChannel<maker_inc_connections::settlement::Response> + 'static),
(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
.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;
}
}

39
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<projection::Actor>,
on_completed: Box<dyn MessageChannel<Completed>>,
on_completed: Box<dyn MessageChannel<TakerSettlementCompleted>>,
connection: xtra::Address<connection::Actor>,
proposal: SettlementProposal,
}
@ -28,7 +28,7 @@ impl Actor {
pub fn new(
cfd: Cfd,
projection: xtra::Address<projection::Actor>,
on_completed: impl MessageChannel<Completed> + 'static,
on_completed: impl MessageChannel<TakerSettlementCompleted> + 'static,
current_price: Price,
connection: xtra::Address<connection::Actor>,
n_payouts: usize,
@ -119,7 +119,11 @@ impl Actor {
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;
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)
}
}
};

16
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<monitor::CollaborativeSettlement>,
W: xtra::Handler<wallet::TryBroadcastTransaction>,
{
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(());
}

17
daemon/src/model/cfd.rs

@ -1851,6 +1851,14 @@ pub enum 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 {
Self::Rejected {
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 {
/// Marker type for contract setup completion
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::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(());
}

Loading…
Cancel
Save