From 801b05cf5b5f9fa75fd0bce01755b091e2b8aaf8 Mon Sep 17 00:00:00 2001 From: Mariusz Klochowicz Date: Thu, 14 Oct 2021 12:24:19 +1030 Subject: [PATCH] Borrow CfdState when updating state in the database The function signature didn't need to take the whole state, as it was only borrowed internally. Removes the need for many `.clone()` calls sprinkled in the codebase. --- daemon/src/db.rs | 8 ++++---- daemon/src/housekeeping.rs | 2 +- daemon/src/maker_cfd.rs | 21 ++++++++++----------- daemon/src/taker_cfd.rs | 19 +++++++++---------- 4 files changed, 24 insertions(+), 26 deletions(-) diff --git a/daemon/src/db.rs b/daemon/src/db.rs index cf96ac8..7be7918 100644 --- a/daemon/src/db.rs +++ b/daemon/src/db.rs @@ -163,7 +163,7 @@ pub async fn insert_cfd(cfd: Cfd, conn: &mut PoolConnection) -> anyhow:: #[allow(dead_code)] pub async fn insert_new_cfd_state_by_order_id( order_id: OrderId, - new_state: CfdState, + new_state: &CfdState, conn: &mut PoolConnection, ) -> anyhow::Result<()> { let cfd_id = load_cfd_id_by_order_uuid(order_id, conn).await?; @@ -171,7 +171,7 @@ pub async fn insert_new_cfd_state_by_order_id( .await .context("loading latest state failed")?; - if mem::discriminant(&latest_cfd_state_in_db) == mem::discriminant(&new_state) { + if mem::discriminant(&latest_cfd_state_in_db) == mem::discriminant(new_state) { // Since we have states where we add information this happens quite frequently tracing::trace!( "Same state transition for cfd with order_id {}: {}", @@ -180,7 +180,7 @@ pub async fn insert_new_cfd_state_by_order_id( ); } - let cfd_state = serde_json::to_string(&new_state)?; + let cfd_state = serde_json::to_string(new_state)?; sqlx::query!( r#" @@ -637,7 +637,7 @@ mod tests { transition_timestamp: SystemTime::now(), }, }; - insert_new_cfd_state_by_order_id(cfd.order.id, cfd.state.clone(), &mut conn) + insert_new_cfd_state_by_order_id(cfd.order.id, &cfd.state, &mut conn) .await .unwrap(); diff --git a/daemon/src/housekeeping.rs b/daemon/src/housekeeping.rs index 2a4a51c..db43c80 100644 --- a/daemon/src/housekeeping.rs +++ b/daemon/src/housekeeping.rs @@ -14,7 +14,7 @@ pub async fn transition_non_continue_cfds_to_setup_failed( for cfd in cfds.iter().filter(|cfd| Cfd::is_cleanup(cfd)) { insert_new_cfd_state_by_order_id( cfd.order.id, - CfdState::SetupFailed { + &CfdState::SetupFailed { common: CfdStateCommon { transition_timestamp: SystemTime::now(), }, diff --git a/daemon/src/maker_cfd.rs b/daemon/src/maker_cfd.rs index a2ec805..185c33b 100644 --- a/daemon/src/maker_cfd.rs +++ b/daemon/src/maker_cfd.rs @@ -306,7 +306,7 @@ impl Actor { cfd.handle(CfdStateChangeEvent::ProposalSigned( TimestampedTransaction::new(tx.clone()), ))?; - insert_new_cfd_state_by_order_id(cfd.order.id, cfd.state.clone(), &mut conn).await?; + insert_new_cfd_state_by_order_id(cfd.order.id, &cfd.state, &mut conn).await?; let spend_tx = dlc.finalize_spend_transaction((tx, sig_maker), sig_taker)?; @@ -320,7 +320,7 @@ impl Actor { cfd.handle(CfdStateChangeEvent::CloseSent(TimestampedTransaction::new( spend_tx, )))?; - insert_new_cfd_state_by_order_id(cfd.order.id, cfd.state, &mut conn).await?; + insert_new_cfd_state_by_order_id(cfd.order.id, &cfd.state, &mut conn).await?; self.current_agreed_proposals .remove(&order_id) @@ -420,7 +420,7 @@ impl Actor { insert_new_cfd_state_by_order_id( order_id, - CfdState::PendingOpen { + &CfdState::PendingOpen { common: CfdStateCommon { transition_timestamp: SystemTime::now(), }, @@ -466,7 +466,7 @@ impl Actor { let mut conn = self.db.acquire().await?; insert_new_cfd_state_by_order_id( order_id, - CfdState::Open { + &CfdState::Open { common: CfdStateCommon { transition_timestamp: SystemTime::now(), }, @@ -575,7 +575,7 @@ impl Actor { insert_new_cfd_state_by_order_id( order_id, - CfdState::ContractSetup { + &CfdState::ContractSetup { common: CfdStateCommon { transition_timestamp: SystemTime::now(), }, @@ -659,7 +659,7 @@ impl Actor { // Update order in db insert_new_cfd_state_by_order_id( order_id, - CfdState::Rejected { + &CfdState::Rejected { common: CfdStateCommon { transition_timestamp: SystemTime::now(), }, @@ -702,7 +702,7 @@ impl Actor { tracing::info!("Commit transaction published on chain: {}", txid); if let Some(new_state) = cfd.handle(CfdStateChangeEvent::CommitTxSent)? { - insert_new_cfd_state_by_order_id(cfd.order.id, new_state, &mut conn).await?; + insert_new_cfd_state_by_order_id(cfd.order.id, &new_state, &mut conn).await?; } self.cfd_feed_actor_inbox @@ -875,7 +875,7 @@ impl Actor { return Ok(()); } - insert_new_cfd_state_by_order_id(order_id, cfd.state.clone(), &mut conn).await?; + insert_new_cfd_state_by_order_id(order_id, &cfd.state, &mut conn).await?; self.cfd_feed_actor_inbox .send(load_all_cfds(&mut conn).await?)?; @@ -925,7 +925,7 @@ impl Actor { continue; } - insert_new_cfd_state_by_order_id(cfd.order.id, cfd.state.clone(), &mut conn).await?; + insert_new_cfd_state_by_order_id(cfd.order.id, &cfd.state, &mut conn).await?; self.cfd_feed_actor_inbox .send(load_all_cfds(&mut conn).await?)?; @@ -951,8 +951,7 @@ impl Actor { bail!("If we can get the CET we should be able to transition") } - insert_new_cfd_state_by_order_id(cfd.order.id, cfd.state.clone(), &mut conn) - .await?; + insert_new_cfd_state_by_order_id(cfd.order.id, &cfd.state, &mut conn).await?; self.cfd_feed_actor_inbox .send(load_all_cfds(&mut conn).await?)?; diff --git a/daemon/src/taker_cfd.rs b/daemon/src/taker_cfd.rs index 05fe4d7..446ad05 100644 --- a/daemon/src/taker_cfd.rs +++ b/daemon/src/taker_cfd.rs @@ -273,7 +273,7 @@ impl Actor { let mut conn = self.db.acquire().await?; insert_new_cfd_state_by_order_id( order_id, - CfdState::ContractSetup { + &CfdState::ContractSetup { common: CfdStateCommon { transition_timestamp: SystemTime::now(), }, @@ -332,7 +332,7 @@ impl Actor { let mut conn = self.db.acquire().await?; insert_new_cfd_state_by_order_id( order_id, - CfdState::Rejected { + &CfdState::Rejected { common: CfdStateCommon { transition_timestamp: SystemTime::now(), }, @@ -372,7 +372,7 @@ impl Actor { cfd.handle(CfdStateChangeEvent::ProposalSigned( TimestampedTransaction::new(tx), ))?; - insert_new_cfd_state_by_order_id(cfd.order.id, cfd.state, &mut conn).await?; + insert_new_cfd_state_by_order_id(cfd.order.id, &cfd.state, &mut conn).await?; self.remove_pending_proposal(&order_id)?; @@ -498,7 +498,7 @@ impl Actor { insert_new_cfd_state_by_order_id( order_id, - CfdState::PendingOpen { + &CfdState::PendingOpen { common: CfdStateCommon { transition_timestamp: SystemTime::now(), }, @@ -544,7 +544,7 @@ impl Actor { let mut conn = self.db.acquire().await?; insert_new_cfd_state_by_order_id( order_id, - CfdState::Open { + &CfdState::Open { common: CfdStateCommon { transition_timestamp: SystemTime::now(), }, @@ -583,7 +583,7 @@ impl Actor { return Ok(()); } - insert_new_cfd_state_by_order_id(order_id, cfd.state.clone(), &mut conn).await?; + insert_new_cfd_state_by_order_id(order_id, &cfd.state, &mut conn).await?; self.cfd_feed_actor_inbox .send(load_all_cfds(&mut conn).await?)?; @@ -620,7 +620,7 @@ impl Actor { bail!("If we can get the commit tx we should be able to transition") } - insert_new_cfd_state_by_order_id(cfd.order.id, cfd.state.clone(), &mut conn).await?; + insert_new_cfd_state_by_order_id(cfd.order.id, &cfd.state, &mut conn).await?; self.cfd_feed_actor_inbox .send(load_all_cfds(&mut conn).await?)?; @@ -658,7 +658,7 @@ impl Actor { continue; } - insert_new_cfd_state_by_order_id(cfd.order.id, cfd.state.clone(), &mut conn).await?; + insert_new_cfd_state_by_order_id(cfd.order.id, &cfd.state, &mut conn).await?; self.cfd_feed_actor_inbox .send(load_all_cfds(&mut conn).await?)?; @@ -684,8 +684,7 @@ impl Actor { bail!("If we can get the CET we should be able to transition") } - insert_new_cfd_state_by_order_id(cfd.order.id, cfd.state.clone(), &mut conn) - .await?; + insert_new_cfd_state_by_order_id(cfd.order.id, &cfd.state, &mut conn).await?; self.cfd_feed_actor_inbox .send(load_all_cfds(&mut conn).await?)?;