Skip to content

Commit

Permalink
perf: Batch update kickoff UTXOs with signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
lemonpartee committed Aug 29, 2024
1 parent 3126d91 commit dee9ad2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 23 deletions.
37 changes: 24 additions & 13 deletions core/src/database/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{EVMAddress, UTXO};
use bitcoin::address::NetworkUnchecked;
use bitcoin::{Address, OutPoint, Txid};
use secp256k1::schnorr;
use sqlx::{Pool, Postgres};
use sqlx::{Pool, Postgres, QueryBuilder};
use std::fs;

use super::wrapper::{AddressDB, EVMAddressDB, OutPointDB, SignatureDB, TxOutDB, TxidDB, UTXODB};
Expand Down Expand Up @@ -399,23 +399,34 @@ impl Database {
Ok(())
}

pub async fn save_slash_or_take_sig(
pub async fn save_slash_or_take_sigs(
&self,
deposit_outpoint: OutPoint,
kickoff_utxo: UTXO,
slash_or_take_sig: schnorr::Signature,
kickoff_utxos_and_sigs: impl IntoIterator<Item = (UTXO, schnorr::Signature)>,
) -> Result<(), BridgeError> {
sqlx::query(
QueryBuilder::new(
"UPDATE deposit_kickoff_utxos
SET slash_or_take_sig = $3
WHERE deposit_outpoint = $1 AND kickoff_utxo = $2;",
SET slash_or_take_sig = batch.sig
FROM (",
)
.bind(OutPointDB(deposit_outpoint))
.bind(sqlx::types::Json(UTXODB {
outpoint_db: OutPointDB(kickoff_utxo.outpoint),
txout_db: TxOutDB(kickoff_utxo.txout),
}))
.bind(SignatureDB(slash_or_take_sig))
.push_values(
kickoff_utxos_and_sigs,
|mut builder, (kickoff_utxo, slash_or_take_sig)| {
builder
.push_bind(sqlx::types::Json(UTXODB {
outpoint_db: OutPointDB(kickoff_utxo.outpoint),
txout_db: TxOutDB(kickoff_utxo.txout),
}))
.push_bind(SignatureDB(slash_or_take_sig));
},
)
.push(
") AS batch (kickoff_utxo, sig)
WHERE deposit_kickoff_utxos.deposit_outpoint = ",
)
.push_bind(OutPointDB(deposit_outpoint))
.push(" AND deposit_kickoff_utxos.kickoff_utxo = batch.kickoff_utxo;")
.build()
.execute(&self.connection)
.await?;

Expand Down
19 changes: 9 additions & 10 deletions core/src/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,16 +364,15 @@ where
})
.collect::<Vec<_>>();

for (index, kickoff_utxo) in kickoff_utxos.iter().enumerate() {
self.db
.save_slash_or_take_sig(
deposit_outpoint,
kickoff_utxo.clone(),
slash_or_take_sigs[index],
)
.await
.unwrap();
}
let kickoff_utxos_with_sigs = kickoff_utxos
.iter()
.enumerate()
.map(|(index, kickoff_utxo)| (kickoff_utxo.clone(), slash_or_take_sigs[index]));

self.db
.save_slash_or_take_sigs(deposit_outpoint, kickoff_utxos_with_sigs)
.await
.unwrap();

// println!("Operator takes sighashes: {:?}", operator_takes_sighashes);
let nonces = self
Expand Down

0 comments on commit dee9ad2

Please sign in to comment.