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

10
daemon/src/housekeeping.rs

@ -1,5 +1,5 @@
use crate::db::{append_cfd_state, load_all_cfds}; 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 crate::wallet::Wallet;
use anyhow::Result; use anyhow::Result;
use sqlx::pool::PoolConnection; 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?; let mut cfds = load_all_cfds(conn).await?;
for cfd in cfds.iter_mut().filter(|cfd| Cfd::is_cleanup(cfd)) { for cfd in cfds.iter_mut().filter(|cfd| Cfd::is_cleanup(cfd)) {
cfd.state = CfdState::SetupFailed { cfd.state = CfdState::setup_failed(format!(
common: CfdStateCommon::default(), "Was in state {} which cannot be continued.",
info: format!("Was in state {} which cannot be continued.", cfd.state), cfd.state
}; ));
append_cfd_state(cfd, conn).await?; 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))?; .with_context(|| format!("Announcement {} not found", cfd.order.oracle_event_id))?;
// 3. Insert that we are in contract setup and refresh our own feed // 3. Insert that we are in contract setup and refresh our own feed
cfd.state = CfdState::ContractSetup { cfd.state = CfdState::contract_setup();
common: CfdStateCommon::default(),
};
append_cfd_state(&cfd, &mut conn, &self.cfd_feed_actor_inbox).await?; append_cfd_state(&cfd, &mut conn, &self.cfd_feed_actor_inbox).await?;
@ -649,10 +647,7 @@ impl Actor {
mut conn: PoolConnection<Sqlite>, mut conn: PoolConnection<Sqlite>,
) -> Result<()> { ) -> Result<()> {
// Update order in db // Update order in db
cfd.state = CfdState::Rejected { cfd.state = CfdState::rejected();
common: CfdStateCommon::default(),
};
append_cfd_state(&cfd, &mut conn, &self.cfd_feed_actor_inbox).await?; append_cfd_state(&cfd, &mut conn, &self.cfd_feed_actor_inbox).await?;
self.takers 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)] #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum Payout { pub enum Payout {
CollaborativeClose(CollaborativeSettlement), CollaborativeClose(CollaborativeSettlement),
@ -718,12 +772,7 @@ impl Cfd {
monitor::Event::CloseFinality(_) => { monitor::Event::CloseFinality(_) => {
let collaborative_close = self.collaborative_close().context("No collaborative close after reaching collaborative close finality")?; let collaborative_close = self.collaborative_close().context("No collaborative close after reaching collaborative close finality")?;
CfdState::Closed { CfdState::closed(Payout::CollaborativeClose(collaborative_close))
common: CfdStateCommon {
transition_timestamp: SystemTime::now(),
},
payout: Payout::CollaborativeClose(collaborative_close)
}
}, },
monitor::Event::CetTimelockExpired(_) => match self.state.clone() { monitor::Event::CetTimelockExpired(_) => match self.state.clone() {
@ -788,36 +837,21 @@ impl Cfd {
) )
}; };
MustRefund { CfdState::must_refund(dlc)
common: CfdStateCommon {
transition_timestamp: SystemTime::now(),
},
dlc,
}
} }
monitor::Event::RefundFinality(_) => { monitor::Event::RefundFinality(_) => {
let dlc = self let dlc = self
.dlc() .dlc()
.context("No dlc available when reaching refund finality")?; .context("No dlc available when reaching refund finality")?;
Refunded { CfdState::refunded(dlc)
common: CfdStateCommon {
transition_timestamp: SystemTime::now(),
},
dlc,
}
} }
monitor::Event::CetFinality(_) => { monitor::Event::CetFinality(_) => {
let attestation = self let attestation = self
.attestation() .attestation()
.context("No attestation available when reaching CET finality")?; .context("No attestation available when reaching CET finality")?;
CfdState::Closed { CfdState::closed(Payout::Cet(attestation))
common: CfdStateCommon {
transition_timestamp: SystemTime::now(),
},
payout: Payout::Cet(attestation)
}
} }
monitor::Event::RevokedTransactionFound(_) => { monitor::Event::RevokedTransactionFound(_) => {
todo!("Punish bad counterparty") todo!("Punish bad counterparty")

14
daemon/src/taker_cfd.rs

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

Loading…
Cancel
Save