Browse Source

Reduce boilerplate in constructing `CfdStateCommon`

testing
Thomas Eizinger 3 years ago
parent
commit
44e62cff29
No known key found for this signature in database GPG Key ID: 651AC83A6C6C8B96
  1. 8
      daemon/src/db.rs
  2. 5
      daemon/src/housekeeping.rs
  3. 16
      daemon/src/maker_cfd.rs
  4. 8
      daemon/src/model/cfd.rs
  5. 16
      daemon/src/taker_cfd.rs

8
daemon/src/db.rs

@ -592,9 +592,7 @@ mod tests {
let mut cfd_1 = Cfd::dummy().insert(&mut conn).await;
cfd_1.state = CfdState::Accepted {
common: CfdStateCommon {
transition_timestamp: SystemTime::now(),
},
common: CfdStateCommon::default(),
};
append_cfd_state(&cfd_1, &mut conn).await.unwrap();
@ -607,9 +605,7 @@ mod tests {
assert_eq!(vec![cfd_1.clone(), cfd_2.clone()], cfds_from_db);
cfd_2.state = CfdState::Rejected {
common: CfdStateCommon {
transition_timestamp: SystemTime::now(),
},
common: CfdStateCommon::default(),
};
append_cfd_state(&cfd_2, &mut conn).await.unwrap();

5
daemon/src/housekeeping.rs

@ -4,7 +4,6 @@ use crate::wallet::Wallet;
use anyhow::Result;
use sqlx::pool::PoolConnection;
use sqlx::Sqlite;
use std::time::SystemTime;
pub async fn transition_non_continue_cfds_to_setup_failed(
conn: &mut PoolConnection<Sqlite>,
@ -13,9 +12,7 @@ pub async fn transition_non_continue_cfds_to_setup_failed(
for cfd in cfds.iter_mut().filter(|cfd| Cfd::is_cleanup(cfd)) {
cfd.state = CfdState::SetupFailed {
common: CfdStateCommon {
transition_timestamp: SystemTime::now(),
},
common: CfdStateCommon::default(),
info: format!("Was in state {} which cannot be continued.", cfd.state),
};

16
daemon/src/maker_cfd.rs

@ -401,9 +401,7 @@ impl Actor {
let mut cfd = load_cfd_by_order_id(order_id, &mut conn).await?;
cfd.state = CfdState::PendingOpen {
common: CfdStateCommon {
transition_timestamp: SystemTime::now(),
},
common: CfdStateCommon::default(),
dlc: dlc.clone(),
attestation: None,
};
@ -444,9 +442,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::Open {
common: CfdStateCommon {
transition_timestamp: SystemTime::now(),
},
common: CfdStateCommon::default(),
dlc: dlc.clone(),
attestation: None,
collaborative_close: None,
@ -568,9 +564,7 @@ impl Actor {
// 3. Insert that we are in contract setup and refresh our own feed
cfd.state = CfdState::ContractSetup {
common: CfdStateCommon {
transition_timestamp: SystemTime::now(),
},
common: CfdStateCommon::default(),
};
append_cfd_state(&cfd, &mut conn, &self.cfd_feed_actor_inbox).await?;
@ -656,9 +650,7 @@ impl Actor {
) -> Result<()> {
// Update order in db
cfd.state = CfdState::Rejected {
common: CfdStateCommon {
transition_timestamp: SystemTime::now(),
},
common: CfdStateCommon::default(),
};
append_cfd_state(&cfd, &mut conn, &self.cfd_feed_actor_inbox).await?;

8
daemon/src/model/cfd.rs

@ -168,6 +168,14 @@ pub struct CfdStateCommon {
pub transition_timestamp: SystemTime,
}
impl Default for CfdStateCommon {
fn default() -> Self {
Self {
transition_timestamp: SystemTime::now(),
}
}
}
// Note: De-/Serialize with type tag to make handling on UI easier
#[allow(clippy::large_enum_variant)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]

16
daemon/src/taker_cfd.rs

@ -270,9 +270,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 {
transition_timestamp: SystemTime::now(),
},
common: CfdStateCommon::default(),
};
append_cfd_state(&cfd, &mut conn, &self.cfd_feed_actor_inbox).await?;
@ -323,9 +321,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 {
transition_timestamp: SystemTime::now(),
},
common: CfdStateCommon::default(),
};
append_cfd_state(&cfd, &mut conn, &self.cfd_feed_actor_inbox).await?;
@ -477,9 +473,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::PendingOpen {
common: CfdStateCommon {
transition_timestamp: SystemTime::now(),
},
common: CfdStateCommon::default(),
dlc: dlc.clone(),
attestation: None,
};
@ -520,9 +514,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::Open {
common: CfdStateCommon {
transition_timestamp: SystemTime::now(),
},
common: CfdStateCommon::default(),
dlc: dlc.clone(),
attestation: None,
collaborative_close: None,

Loading…
Cancel
Save