Browse Source

Make it less verbose to construct simple states

testing
Thomas Eizinger 3 years ago
parent
commit
bfabf0c7b1
No known key found for this signature in database GPG Key ID: 651AC83A6C6C8B96
  1. 17
      daemon/src/db.rs
  2. 10
      daemon/src/housekeeping.rs
  3. 9
      daemon/src/maker_cfd.rs
  4. 82
      daemon/src/model/cfd.rs
  5. 14
      daemon/src/taker_cfd.rs

17
daemon/src/db.rs

@ -493,7 +493,6 @@ pub async fn load_cfds_by_oracle_event_id(
#[cfg(test)]
mod tests {
use std::fs::File;
use std::time::SystemTime;
use pretty_assertions::assert_eq;
use rust_decimal_macros::dec;
@ -503,7 +502,7 @@ mod tests {
use time::OffsetDateTime;
use crate::db::insert_order;
use crate::model::cfd::{Cfd, CfdState, CfdStateCommon, Order};
use crate::model::cfd::{Cfd, CfdState, Order};
use crate::model::Usd;
use super::*;
@ -591,9 +590,7 @@ mod tests {
let mut cfd_1 = Cfd::dummy().insert(&mut conn).await;
cfd_1.state = CfdState::Accepted {
common: CfdStateCommon::default(),
};
cfd_1.state = CfdState::accepted();
append_cfd_state(&cfd_1, &mut conn).await.unwrap();
let cfds_from_db = load_all_cfds(&mut conn).await.unwrap();
@ -604,9 +601,7 @@ mod tests {
let cfds_from_db = load_all_cfds(&mut conn).await.unwrap();
assert_eq!(vec![cfd_1.clone(), cfd_2.clone()], cfds_from_db);
cfd_2.state = CfdState::Rejected {
common: CfdStateCommon::default(),
};
cfd_2.state = CfdState::rejected();
append_cfd_state(&cfd_2, &mut conn).await.unwrap();
let cfds_from_db = load_all_cfds(&mut conn).await.unwrap();
@ -643,11 +638,7 @@ mod tests {
Cfd::new(
Order::dummy(),
Usd(dec!(1000)),
CfdState::OutgoingOrderRequest {
common: CfdStateCommon {
transition_timestamp: SystemTime::now(),
},
},
CfdState::outgoing_order_request(),
)
}

10
daemon/src/housekeeping.rs

@ -1,5 +1,5 @@
use crate::db::{append_cfd_state, load_all_cfds};
use crate::model::cfd::{Cfd, CfdState, CfdStateCommon};
use crate::model::cfd::{Cfd, CfdState};
use crate::wallet::Wallet;
use anyhow::Result;
use sqlx::pool::PoolConnection;
@ -11,10 +11,10 @@ pub async fn transition_non_continue_cfds_to_setup_failed(
let mut cfds = load_all_cfds(conn).await?;
for cfd in cfds.iter_mut().filter(|cfd| Cfd::is_cleanup(cfd)) {
cfd.state = CfdState::SetupFailed {
common: CfdStateCommon::default(),
info: format!("Was in state {} which cannot be continued.", cfd.state),
};
cfd.state = CfdState::setup_failed(format!(
"Was in state {} which cannot be continued.",
cfd.state
));
append_cfd_state(cfd, conn).await?;
}

9
daemon/src/maker_cfd.rs

@ -563,9 +563,7 @@ impl Actor {
.with_context(|| format!("Announcement {} not found", cfd.order.oracle_event_id))?;
// 3. Insert that we are in contract setup and refresh our own feed
cfd.state = CfdState::ContractSetup {
common: CfdStateCommon::default(),
};
cfd.state = CfdState::contract_setup();
append_cfd_state(&cfd, &mut conn, &self.cfd_feed_actor_inbox).await?;
@ -649,10 +647,7 @@ impl Actor {
mut conn: PoolConnection<Sqlite>,
) -> Result<()> {
// Update order in db
cfd.state = CfdState::Rejected {
common: CfdStateCommon::default(),
};
cfd.state = CfdState::rejected();
append_cfd_state(&cfd, &mut conn, &self.cfd_feed_actor_inbox).await?;
self.takers

82
daemon/src/model/cfd.rs

@ -293,6 +293,60 @@ pub enum CfdState {
},
}
impl CfdState {
pub fn outgoing_order_request() -> Self {
Self::OutgoingOrderRequest {
common: CfdStateCommon::default(),
}
}
pub fn accepted() -> Self {
Self::Accepted {
common: CfdStateCommon::default(),
}
}
pub fn rejected() -> Self {
Self::Rejected {
common: CfdStateCommon::default(),
}
}
pub fn contract_setup() -> Self {
Self::ContractSetup {
common: CfdStateCommon::default(),
}
}
pub fn closed(payout: Payout) -> Self {
Self::Closed {
common: CfdStateCommon::default(),
payout,
}
}
pub fn must_refund(dlc: Dlc) -> Self {
Self::MustRefund {
common: CfdStateCommon::default(),
dlc,
}
}
pub fn refunded(dlc: Dlc) -> Self {
Self::Refunded {
common: CfdStateCommon::default(),
dlc,
}
}
pub fn setup_failed(info: String) -> Self {
Self::SetupFailed {
common: CfdStateCommon::default(),
info,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum Payout {
CollaborativeClose(CollaborativeSettlement),
@ -718,12 +772,7 @@ impl Cfd {
monitor::Event::CloseFinality(_) => {
let collaborative_close = self.collaborative_close().context("No collaborative close after reaching collaborative close finality")?;
CfdState::Closed {
common: CfdStateCommon {
transition_timestamp: SystemTime::now(),
},
payout: Payout::CollaborativeClose(collaborative_close)
}
CfdState::closed(Payout::CollaborativeClose(collaborative_close))
},
monitor::Event::CetTimelockExpired(_) => match self.state.clone() {
@ -788,36 +837,21 @@ impl Cfd {
)
};
MustRefund {
common: CfdStateCommon {
transition_timestamp: SystemTime::now(),
},
dlc,
}
CfdState::must_refund(dlc)
}
monitor::Event::RefundFinality(_) => {
let dlc = self
.dlc()
.context("No dlc available when reaching refund finality")?;
Refunded {
common: CfdStateCommon {
transition_timestamp: SystemTime::now(),
},
dlc,
}
CfdState::refunded(dlc)
}
monitor::Event::CetFinality(_) => {
let attestation = self
.attestation()
.context("No attestation available when reaching CET finality")?;
CfdState::Closed {
common: CfdStateCommon {
transition_timestamp: SystemTime::now(),
},
payout: Payout::Cet(attestation)
}
CfdState::closed(Payout::Cet(attestation))
}
monitor::Event::RevokedTransactionFound(_) => {
todo!("Punish bad counterparty")

14
daemon/src/taker_cfd.rs

@ -149,11 +149,7 @@ impl Actor {
let cfd = Cfd::new(
current_order.clone(),
quantity,
CfdState::OutgoingOrderRequest {
common: CfdStateCommon {
transition_timestamp: SystemTime::now(),
},
},
CfdState::outgoing_order_request(),
);
insert_cfd(&cfd, &mut conn, &self.cfd_feed_actor_inbox).await?;
@ -269,9 +265,7 @@ impl Actor {
let mut conn = self.db.acquire().await?;
let mut cfd = load_cfd_by_order_id(order_id, &mut conn).await?;
cfd.state = CfdState::ContractSetup {
common: CfdStateCommon::default(),
};
cfd.state = CfdState::contract_setup();
append_cfd_state(&cfd, &mut conn, &self.cfd_feed_actor_inbox).await?;
@ -320,9 +314,7 @@ impl Actor {
let mut conn = self.db.acquire().await?;
let mut cfd = load_cfd_by_order_id(order_id, &mut conn).await?;
cfd.state = CfdState::Rejected {
common: CfdStateCommon::default(),
};
cfd.state = CfdState::rejected();
append_cfd_state(&cfd, &mut conn, &self.cfd_feed_actor_inbox).await?;

Loading…
Cancel
Save