Browse Source

Merge #306

306: Borrow CfdState when updating state in the database r=klochowicz a=klochowicz

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.

Co-authored-by: Mariusz Klochowicz <mariusz@klochowicz.com>
refactor/no-log-handler
bors[bot] 3 years ago
committed by GitHub
parent
commit
2d16835758
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      daemon/src/db.rs
  2. 2
      daemon/src/housekeeping.rs
  3. 21
      daemon/src/maker_cfd.rs
  4. 19
      daemon/src/taker_cfd.rs

8
daemon/src/db.rs

@ -163,7 +163,7 @@ pub async fn insert_cfd(cfd: Cfd, conn: &mut PoolConnection<Sqlite>) -> 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<Sqlite>,
) -> 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();

2
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(),
},

21
daemon/src/maker_cfd.rs

@ -306,7 +306,7 @@ impl Actor {
cfd.handle(CfdStateChangeEvent::ProposalSigned(
TimestampedTransaction::new(tx.clone(), dlc.script_pubkey_for(cfd.role())),
))?;
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)?;
@ -321,7 +321,7 @@ impl Actor {
spend_tx,
dlc.script_pubkey_for(cfd.role()),
)))?;
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)
@ -421,7 +421,7 @@ impl Actor {
insert_new_cfd_state_by_order_id(
order_id,
CfdState::PendingOpen {
&CfdState::PendingOpen {
common: CfdStateCommon {
transition_timestamp: SystemTime::now(),
},
@ -467,7 +467,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(),
},
@ -576,7 +576,7 @@ impl Actor {
insert_new_cfd_state_by_order_id(
order_id,
CfdState::ContractSetup {
&CfdState::ContractSetup {
common: CfdStateCommon {
transition_timestamp: SystemTime::now(),
},
@ -660,7 +660,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(),
},
@ -703,7 +703,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
@ -876,7 +876,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?)?;
@ -926,7 +926,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?)?;
@ -952,8 +952,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?)?;

19
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, dlc.script_pubkey_for(cfd.role())),
))?;
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.cfd_feed_actor_inbox
.send(load_all_cfds(&mut conn).await?)?;
@ -501,7 +501,7 @@ impl Actor {
insert_new_cfd_state_by_order_id(
order_id,
CfdState::PendingOpen {
&CfdState::PendingOpen {
common: CfdStateCommon {
transition_timestamp: SystemTime::now(),
},
@ -547,7 +547,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(),
},
@ -586,7 +586,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?)?;
@ -623,7 +623,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?)?;
@ -661,7 +661,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?)?;
@ -687,8 +687,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?)?;

Loading…
Cancel
Save