|
|
@ -7,7 +7,6 @@ use crate::collab_settlement_maker; |
|
|
|
use crate::maker_inc_connections; |
|
|
|
use crate::model; |
|
|
|
use crate::model::cfd::Cfd; |
|
|
|
use crate::model::cfd::CfdEvent; |
|
|
|
use crate::model::cfd::CollaborativeSettlement; |
|
|
|
use crate::model::cfd::Order; |
|
|
|
use crate::model::cfd::OrderId; |
|
|
@ -21,7 +20,6 @@ use crate::model::Position; |
|
|
|
use crate::model::Price; |
|
|
|
use crate::model::Usd; |
|
|
|
use crate::monitor; |
|
|
|
use crate::monitor::MonitorParams; |
|
|
|
use crate::oracle; |
|
|
|
use crate::process_manager; |
|
|
|
use crate::projection; |
|
|
@ -34,7 +32,6 @@ use crate::wallet; |
|
|
|
use crate::wire; |
|
|
|
use crate::wire::TakerToMaker; |
|
|
|
use crate::Tasks; |
|
|
|
use anyhow::bail; |
|
|
|
use anyhow::Context as _; |
|
|
|
use anyhow::Result; |
|
|
|
use async_trait::async_trait; |
|
|
@ -91,7 +88,7 @@ pub struct FromTaker { |
|
|
|
pub msg: wire::TakerToMaker, |
|
|
|
} |
|
|
|
|
|
|
|
pub struct Actor<O, M, T, W> { |
|
|
|
pub struct Actor<O, T, W> { |
|
|
|
db: sqlx::SqlitePool, |
|
|
|
wallet: Address<W>, |
|
|
|
settlement_interval: Duration, |
|
|
@ -101,7 +98,6 @@ pub struct Actor<O, M, T, W> { |
|
|
|
rollover_actors: AddressMap<OrderId, rollover_maker::Actor>, |
|
|
|
takers: Address<T>, |
|
|
|
current_order: Option<Order>, |
|
|
|
monitor_actor: Address<M>, |
|
|
|
setup_actors: AddressMap<OrderId, setup_maker::Actor>, |
|
|
|
settlement_actors: AddressMap<OrderId, collab_settlement_maker::Actor>, |
|
|
|
oracle_actor: Address<O>, |
|
|
@ -110,7 +106,7 @@ pub struct Actor<O, M, T, W> { |
|
|
|
tasks: Tasks, |
|
|
|
} |
|
|
|
|
|
|
|
impl<O, M, T, W> Actor<O, M, T, W> { |
|
|
|
impl<O, T, W> Actor<O, T, W> { |
|
|
|
#[allow(clippy::too_many_arguments)] |
|
|
|
pub fn new( |
|
|
|
db: sqlx::SqlitePool, |
|
|
@ -120,7 +116,6 @@ impl<O, M, T, W> Actor<O, M, T, W> { |
|
|
|
projection_actor: Address<projection::Actor>, |
|
|
|
process_manager_actor: Address<process_manager::Actor>, |
|
|
|
takers: Address<T>, |
|
|
|
monitor_actor: Address<M>, |
|
|
|
oracle_actor: Address<O>, |
|
|
|
n_payouts: usize, |
|
|
|
) -> Self { |
|
|
@ -134,7 +129,6 @@ impl<O, M, T, W> Actor<O, M, T, W> { |
|
|
|
rollover_actors: AddressMap::default(), |
|
|
|
takers, |
|
|
|
current_order: None, |
|
|
|
monitor_actor, |
|
|
|
setup_actors: AddressMap::default(), |
|
|
|
oracle_actor, |
|
|
|
n_payouts, |
|
|
@ -157,7 +151,7 @@ impl<O, M, T, W> Actor<O, M, T, W> { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
impl<O, M, T, W> Actor<O, M, T, W> |
|
|
|
impl<O, T, W> Actor<O, T, W> |
|
|
|
where |
|
|
|
T: xtra::Handler<maker_inc_connections::TakerMessage>, |
|
|
|
{ |
|
|
@ -186,7 +180,7 @@ where |
|
|
|
} |
|
|
|
|
|
|
|
#[xtra_productivity] |
|
|
|
impl<O, M, T, W> Actor<O, M, T, W> { |
|
|
|
impl<O, T, W> Actor<O, T, W> { |
|
|
|
async fn handle_accept_rollover(&mut self, msg: AcceptRollOver) -> Result<()> { |
|
|
|
if self |
|
|
|
.rollover_actors |
|
|
@ -214,10 +208,9 @@ impl<O, M, T, W> Actor<O, M, T, W> { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
impl<O, M, T, W> Actor<O, M, T, W> |
|
|
|
impl<O, T, W> Actor<O, T, W> |
|
|
|
where |
|
|
|
O: xtra::Handler<oracle::GetAnnouncement> + xtra::Handler<oracle::MonitorAttestation>, |
|
|
|
M: xtra::Handler<monitor::StartMonitoring>, |
|
|
|
T: xtra::Handler<maker_inc_connections::TakerMessage> |
|
|
|
+ xtra::Handler<Stopping<rollover_maker::Actor>> |
|
|
|
+ xtra::Handler<RollOverProposed>, |
|
|
@ -276,16 +269,15 @@ where |
|
|
|
} |
|
|
|
|
|
|
|
#[xtra_productivity(message_impl = false)] |
|
|
|
impl<O, M, T, W> Actor<O, M, T, W> { |
|
|
|
impl<O, T, W> Actor<O, T, W> { |
|
|
|
async fn handle_rollover_actor_stopping(&mut self, msg: Stopping<rollover_maker::Actor>) { |
|
|
|
self.rollover_actors.gc(msg); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
impl<O, M, T, W> Actor<O, M, T, W> |
|
|
|
impl<O, T, W> Actor<O, T, W> |
|
|
|
where |
|
|
|
O: xtra::Handler<oracle::GetAnnouncement> + xtra::Handler<oracle::MonitorAttestation>, |
|
|
|
M: xtra::Handler<monitor::StartMonitoring>, |
|
|
|
T: xtra::Handler<maker_inc_connections::ConfirmOrder> |
|
|
|
+ xtra::Handler<maker_inc_connections::TakerMessage> |
|
|
|
+ xtra::Handler<maker_inc_connections::BroadcastOrder> |
|
|
@ -390,7 +382,7 @@ where |
|
|
|
} |
|
|
|
|
|
|
|
#[xtra_productivity] |
|
|
|
impl<O, M, T, W> Actor<O, M, T, W> { |
|
|
|
impl<O, T, W> Actor<O, T, W> { |
|
|
|
async fn handle_accept_order(&mut self, msg: AcceptOrder) -> Result<()> { |
|
|
|
let AcceptOrder { order_id } = msg; |
|
|
|
|
|
|
@ -441,7 +433,7 @@ impl<O, M, T, W> Actor<O, M, T, W> { |
|
|
|
} |
|
|
|
|
|
|
|
#[xtra_productivity(message_impl = false)] |
|
|
|
impl<O, M, T, W> Actor<O, M, T, W> { |
|
|
|
impl<O, T, W> Actor<O, T, W> { |
|
|
|
async fn handle_setup_actor_stopping(&mut self, message: Stopping<setup_maker::Actor>) { |
|
|
|
self.setup_actors.gc(message); |
|
|
|
} |
|
|
@ -455,11 +447,7 @@ impl<O, M, T, W> Actor<O, M, T, W> { |
|
|
|
} |
|
|
|
|
|
|
|
#[xtra_productivity(message_impl = false)] |
|
|
|
impl<O, M, T, W> Actor<O, M, T, W> |
|
|
|
where |
|
|
|
M: xtra::Handler<monitor::CollaborativeSettlement>, |
|
|
|
W: xtra::Handler<wallet::TryBroadcastTransaction>, |
|
|
|
{ |
|
|
|
impl<O, T, W> Actor<O, T, W> { |
|
|
|
async fn handle_settlement_completed( |
|
|
|
&mut self, |
|
|
|
msg: model::cfd::Completed<CollaborativeSettlement>, |
|
|
@ -477,80 +465,24 @@ where |
|
|
|
tracing::error!("Sending event to process manager failed: {:#}", e); |
|
|
|
} |
|
|
|
|
|
|
|
match event.event { |
|
|
|
CfdEvent::CollaborativeSettlementCompleted { |
|
|
|
spend_tx, script, .. |
|
|
|
} => { |
|
|
|
let txid = self |
|
|
|
.wallet |
|
|
|
.send(wallet::TryBroadcastTransaction { tx: spend_tx }) |
|
|
|
.await? |
|
|
|
.context("Broadcasting close transaction")?; |
|
|
|
|
|
|
|
tracing::info!(%order_id, "Close transaction published with txid {}", txid); |
|
|
|
|
|
|
|
self.monitor_actor |
|
|
|
.send(monitor::CollaborativeSettlement { |
|
|
|
order_id, |
|
|
|
tx: (txid, script), |
|
|
|
}) |
|
|
|
.await?; |
|
|
|
} |
|
|
|
CfdEvent::CollaborativeSettlementRejected { commit_tx } => { |
|
|
|
let txid = self |
|
|
|
.wallet |
|
|
|
.send(wallet::TryBroadcastTransaction { tx: commit_tx }) |
|
|
|
.await? |
|
|
|
.context("Broadcasting commit transaction")?; |
|
|
|
|
|
|
|
tracing::info!( |
|
|
|
"Closing non-collaboratively. Commit tx published with txid {}", |
|
|
|
txid |
|
|
|
) |
|
|
|
} |
|
|
|
CfdEvent::CollaborativeSettlementFailed { commit_tx } => { |
|
|
|
let txid = self |
|
|
|
.wallet |
|
|
|
.send(wallet::TryBroadcastTransaction { tx: commit_tx }) |
|
|
|
.await? |
|
|
|
.context("Broadcasting commit transaction")?; |
|
|
|
|
|
|
|
tracing::warn!( |
|
|
|
"Closing non-collaboratively. Commit tx published with txid {}", |
|
|
|
txid |
|
|
|
) |
|
|
|
} |
|
|
|
_ => bail!("Unexpected event {:?}", event.event), |
|
|
|
} |
|
|
|
|
|
|
|
Ok(()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
#[xtra_productivity] |
|
|
|
impl<O, M, T, W> Actor<O, M, T, W> |
|
|
|
where |
|
|
|
W: xtra::Handler<wallet::TryBroadcastTransaction>, |
|
|
|
{ |
|
|
|
impl<O, T, W> Actor<O, T, W> { |
|
|
|
async fn handle_commit(&mut self, msg: Commit) -> Result<()> { |
|
|
|
let Commit { order_id } = msg; |
|
|
|
|
|
|
|
let mut conn = self.db.acquire().await?; |
|
|
|
cfd_actors::handle_commit( |
|
|
|
order_id, |
|
|
|
&mut conn, |
|
|
|
&self.wallet, |
|
|
|
&self.process_manager_actor, |
|
|
|
) |
|
|
|
.await?; |
|
|
|
cfd_actors::handle_commit(order_id, &mut conn, &self.process_manager_actor).await?; |
|
|
|
|
|
|
|
Ok(()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
impl<O, M, T, W> Actor<O, M, T, W> |
|
|
|
impl<O, T, W> Actor<O, T, W> |
|
|
|
where |
|
|
|
M: xtra::Handler<monitor::StartMonitoring>, |
|
|
|
O: xtra::Handler<oracle::MonitorAttestation>, |
|
|
|
{ |
|
|
|
async fn handle_roll_over_completed(&mut self, _: Completed) -> Result<()> { |
|
|
@ -560,10 +492,9 @@ where |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
impl<O, M, T, W> Actor<O, M, T, W> |
|
|
|
impl<O, T, W> Actor<O, T, W> |
|
|
|
where |
|
|
|
O: xtra::Handler<oracle::MonitorAttestation>, |
|
|
|
M: xtra::Handler<monitor::StartMonitoring> + xtra::Handler<monitor::CollaborativeSettlement>, |
|
|
|
T: xtra::Handler<maker_inc_connections::settlement::Response> |
|
|
|
+ xtra::Handler<Stopping<collab_settlement_maker::Actor>>, |
|
|
|
W: xtra::Handler<wallet::TryBroadcastTransaction>, |
|
|
@ -608,7 +539,7 @@ where |
|
|
|
} |
|
|
|
|
|
|
|
#[xtra_productivity] |
|
|
|
impl<O, M, T, W> Actor<O, M, T, W> |
|
|
|
impl<O, T, W> Actor<O, T, W> |
|
|
|
where |
|
|
|
T: xtra::Handler<maker_inc_connections::BroadcastOrder>, |
|
|
|
{ |
|
|
@ -652,12 +583,7 @@ where |
|
|
|
} |
|
|
|
|
|
|
|
#[xtra_productivity(message_impl = false)] |
|
|
|
impl<O, M, T, W> Actor<O, M, T, W> |
|
|
|
where |
|
|
|
O: xtra::Handler<oracle::MonitorAttestation>, |
|
|
|
M: xtra::Handler<monitor::StartMonitoring>, |
|
|
|
W: xtra::Handler<wallet::TryBroadcastTransaction>, |
|
|
|
{ |
|
|
|
impl<O, T, W> Actor<O, T, W> { |
|
|
|
async fn handle_setup_completed(&mut self, msg: SetupCompleted) -> Result<()> { |
|
|
|
let order_id = msg.order_id(); |
|
|
|
let mut conn = self.db.acquire().await?; |
|
|
@ -672,44 +598,12 @@ where |
|
|
|
tracing::error!("Sending event to process manager failed: {:#}", e); |
|
|
|
} |
|
|
|
|
|
|
|
let dlc = match event.event { |
|
|
|
CfdEvent::ContractSetupCompleted { dlc } => dlc, |
|
|
|
CfdEvent::OfferRejected | CfdEvent::ContractSetupFailed => { |
|
|
|
return Ok(()); |
|
|
|
} |
|
|
|
_ => bail!("Unexpected event {:?}", event.event), |
|
|
|
}; |
|
|
|
|
|
|
|
tracing::info!("Setup complete, publishing on chain now"); |
|
|
|
|
|
|
|
let txid = self |
|
|
|
.wallet |
|
|
|
.send(wallet::TryBroadcastTransaction { |
|
|
|
tx: dlc.lock.0.clone(), |
|
|
|
}) |
|
|
|
.await??; |
|
|
|
|
|
|
|
tracing::info!("Lock transaction published with txid {}", txid); |
|
|
|
|
|
|
|
self.monitor_actor |
|
|
|
.send(monitor::StartMonitoring { |
|
|
|
id: order_id, |
|
|
|
params: MonitorParams::new(dlc.clone()), |
|
|
|
}) |
|
|
|
.await?; |
|
|
|
|
|
|
|
self.oracle_actor |
|
|
|
.send(oracle::MonitorAttestation { |
|
|
|
event_id: dlc.settlement_event_id, |
|
|
|
}) |
|
|
|
.await?; |
|
|
|
|
|
|
|
Ok(()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
#[async_trait] |
|
|
|
impl<O: 'static, M: 'static, T: 'static, W: 'static> Handler<TakerConnected> for Actor<O, M, T, W> |
|
|
|
impl<O: 'static, T: 'static, W: 'static> Handler<TakerConnected> for Actor<O, T, W> |
|
|
|
where |
|
|
|
T: xtra::Handler<maker_inc_connections::TakerMessage>, |
|
|
|
{ |
|
|
@ -719,8 +613,7 @@ where |
|
|
|
} |
|
|
|
|
|
|
|
#[async_trait] |
|
|
|
impl<O: 'static, M: 'static, T: 'static, W: 'static> Handler<TakerDisconnected> |
|
|
|
for Actor<O, M, T, W> |
|
|
|
impl<O: 'static, T: 'static, W: 'static> Handler<TakerDisconnected> for Actor<O, T, W> |
|
|
|
where |
|
|
|
T: xtra::Handler<maker_inc_connections::TakerMessage>, |
|
|
|
{ |
|
|
@ -730,9 +623,8 @@ where |
|
|
|
} |
|
|
|
|
|
|
|
#[async_trait] |
|
|
|
impl<O: 'static, M: 'static, T: 'static, W: 'static> Handler<Completed> for Actor<O, M, T, W> |
|
|
|
impl<O: 'static, T: 'static, W: 'static> Handler<Completed> for Actor<O, T, W> |
|
|
|
where |
|
|
|
M: xtra::Handler<monitor::StartMonitoring>, |
|
|
|
O: xtra::Handler<oracle::MonitorAttestation>, |
|
|
|
{ |
|
|
|
async fn handle(&mut self, msg: Completed, _ctx: &mut Context<Self>) -> Result<()> { |
|
|
@ -741,31 +633,18 @@ where |
|
|
|
} |
|
|
|
|
|
|
|
#[xtra_productivity(message_impl = false)] |
|
|
|
impl<O, M, T, W> Actor<O, M, T, W> |
|
|
|
where |
|
|
|
W: xtra::Handler<wallet::TryBroadcastTransaction>, |
|
|
|
{ |
|
|
|
impl<O, T, W> Actor<O, T, W> { |
|
|
|
async fn handle_monitor(&mut self, msg: monitor::Event) { |
|
|
|
if let Err(e) = cfd_actors::handle_monitoring_event( |
|
|
|
msg, |
|
|
|
&self.db, |
|
|
|
&self.wallet, |
|
|
|
&self.process_manager_actor, |
|
|
|
) |
|
|
|
.await |
|
|
|
if let Err(e) = |
|
|
|
cfd_actors::handle_monitoring_event(msg, &self.db, &self.process_manager_actor).await |
|
|
|
{ |
|
|
|
tracing::error!("Unable to handle monotoring event: {:#}", e) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
async fn handle_attestation(&mut self, msg: oracle::Attestation) { |
|
|
|
if let Err(e) = cfd_actors::handle_oracle_attestation( |
|
|
|
msg, |
|
|
|
&self.db, |
|
|
|
&self.wallet, |
|
|
|
&self.process_manager_actor, |
|
|
|
) |
|
|
|
.await |
|
|
|
if let Err(e) = |
|
|
|
cfd_actors::handle_oracle_attestation(msg, &self.db, &self.process_manager_actor).await |
|
|
|
{ |
|
|
|
tracing::warn!("Failed to handle oracle attestation: {:#}", e) |
|
|
|
} |
|
|
@ -773,10 +652,9 @@ where |
|
|
|
} |
|
|
|
|
|
|
|
#[async_trait] |
|
|
|
impl<O: 'static, M: 'static, T: 'static, W: 'static> Handler<FromTaker> for Actor<O, M, T, W> |
|
|
|
impl<O: 'static, T: 'static, W: 'static> Handler<FromTaker> for Actor<O, T, W> |
|
|
|
where |
|
|
|
O: xtra::Handler<oracle::GetAnnouncement> + xtra::Handler<oracle::MonitorAttestation>, |
|
|
|
M: xtra::Handler<monitor::StartMonitoring> + xtra::Handler<monitor::CollaborativeSettlement>, |
|
|
|
T: xtra::Handler<maker_inc_connections::ConfirmOrder> |
|
|
|
+ xtra::Handler<maker_inc_connections::TakerMessage> |
|
|
|
+ xtra::Handler<maker_inc_connections::BroadcastOrder> |
|
|
@ -879,4 +757,4 @@ impl Message for FromTaker { |
|
|
|
type Result = (); |
|
|
|
} |
|
|
|
|
|
|
|
impl<O: 'static, M: 'static, T: 'static, W: 'static> xtra::Actor for Actor<O, M, T, W> {} |
|
|
|
impl<O: 'static, T: 'static, W: 'static> xtra::Actor for Actor<O, T, W> {} |
|
|
|