Skip to content

Commit

Permalink
database: Remove Option from DatabaseTransaction type.
Browse files Browse the repository at this point in the history
  • Loading branch information
ceyhunsen committed Jan 30, 2025
1 parent 9db1821 commit 8626332
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 44 deletions.
14 changes: 7 additions & 7 deletions core/src/database/header_chain_prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl Database {
/// Saves a new block to the database, later to be updated by a proof.
pub async fn save_new_block(
&self,
tx: DatabaseTransaction<'_, '_>,
tx: Option<DatabaseTransaction<'_, '_>>,
block_hash: block::BlockHash,
block_header: block::Header,
block_height: u64,
Expand All @@ -37,7 +37,7 @@ impl Database {
/// Sets a block's proof by referring to it by it's hash.
pub async fn save_block_proof(
&self,
tx: DatabaseTransaction<'_, '_>,
tx: Option<DatabaseTransaction<'_, '_>>,
hash: block::BlockHash,
proof: Receipt,
) -> Result<(), BridgeError> {
Expand All @@ -55,7 +55,7 @@ impl Database {
/// Gets a block's proof by referring to it by it's hash.
pub async fn get_block_proof_by_hash(
&self,
tx: DatabaseTransaction<'_, '_>,
tx: Option<DatabaseTransaction<'_, '_>>,
hash: block::BlockHash,
) -> Result<Option<Receipt>, BridgeError> {
let query = sqlx::query_as("SELECT proof FROM header_chain_proofs WHERE block_hash = $1;")
Expand All @@ -76,7 +76,7 @@ impl Database {
/// Returns a block's hash and header, referring it to by it's height.
pub async fn get_block_info_by_height(
&self,
tx: DatabaseTransaction<'_, '_>,
tx: Option<DatabaseTransaction<'_, '_>>,
height: u64,
) -> Result<(block::BlockHash, block::Header), BridgeError> {
let query = sqlx::query_as(
Expand Down Expand Up @@ -107,7 +107,7 @@ impl Database {
/// Returns a block's hash and header, referring it to by it's height.
pub async fn get_block_header(
&self,
tx: DatabaseTransaction<'_, '_>,
tx: Option<DatabaseTransaction<'_, '_>>,
block_height: u64,
block_hash: BlockHash,
) -> Result<Option<block::Header>, BridgeError> {
Expand All @@ -128,7 +128,7 @@ impl Database {

pub async fn get_latest_block_info(
&self,
tx: DatabaseTransaction<'_, '_>,
tx: Option<DatabaseTransaction<'_, '_>>,
) -> Result<(u64, BlockHash), BridgeError> {
let query = sqlx::query_as(
"SELECT height, block_hash FROM header_chain_proofs ORDER BY height DESC;",
Expand All @@ -145,7 +145,7 @@ impl Database {

pub async fn get_non_proven_block(
&self,
tx: DatabaseTransaction<'_, '_>,
tx: Option<DatabaseTransaction<'_, '_>>,
) -> Result<(BlockHash, Header, i32, Receipt), BridgeError> {
let query = sqlx::query_as(
"SELECT h1.block_hash,
Expand Down
4 changes: 2 additions & 2 deletions core/src/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ pub struct Database {
connection: Pool<Postgres>,
}

/// Optional database transaction.
pub type DatabaseTransaction<'a, 'b> = Option<&'a mut sqlx::Transaction<'b, Postgres>>;
/// Database transaction for Postgres.
pub type DatabaseTransaction<'a, 'b> = &'a mut sqlx::Transaction<'b, Postgres>;

/// Executes a query with a transaction if it is provided.
///
Expand Down
40 changes: 20 additions & 20 deletions core/src/database/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl Database {
/// Sets a sequential collateral tx details for an operator.
pub async fn set_sequential_collateral_tx(
&self,
tx: DatabaseTransaction<'_, '_>,
tx: Option<DatabaseTransaction<'_, '_>>,
operator_idx: i32,
idx: i32,
sequential_collateral_txid: Txid,
Expand All @@ -50,7 +50,7 @@ impl Database {
/// [`Txid`], and block height for a sequential collateral tx.
pub async fn get_sequential_collateral_txs(
&self,
tx: DatabaseTransaction<'_, '_>,
tx: Option<DatabaseTransaction<'_, '_>>,
operator_idx: i32,
) -> Result<Vec<(i32, Txid, i32)>, BridgeError> {
let query = sqlx::query_as("SELECT idx, sequential_collateral_txid, block_height FROM operator_sequential_collateral_txs WHERE operator_idx = $1 ORDER BY idx;").bind(operator_idx);
Expand All @@ -66,7 +66,7 @@ impl Database {

pub async fn set_operator(
&self,
tx: DatabaseTransaction<'_, '_>,
tx: Option<DatabaseTransaction<'_, '_>>,
operator_idx: i32,
xonly_pubkey: XOnlyPublicKey,
wallet_address: String,
Expand All @@ -87,7 +87,7 @@ impl Database {

pub async fn get_operators(
&self,
tx: DatabaseTransaction<'_, '_>,
tx: Option<DatabaseTransaction<'_, '_>>,
) -> Result<Vec<(XOnlyPublicKey, bitcoin::Address, Txid)>, BridgeError> {
let query = sqlx::query_as(
"SELECT operator_idx, xonly_pk, wallet_reimburse_address, collateral_funding_txid FROM operators ORDER BY operator_idx;"
Expand Down Expand Up @@ -124,7 +124,7 @@ impl Database {

pub async fn set_timeout_tx_sigs(
&self,
tx: DatabaseTransaction<'_, '_>,
tx: Option<DatabaseTransaction<'_, '_>>,
operator_idx: u32,
timeout_tx_sigs: Vec<schnorr::Signature>,
) -> Result<(), BridgeError> {
Expand All @@ -141,7 +141,7 @@ impl Database {

pub async fn get_timeout_tx_sigs(
&self,
tx: DatabaseTransaction<'_, '_>,
tx: Option<DatabaseTransaction<'_, '_>>,
operator_idx: u32,
) -> Result<Vec<schnorr::Signature>, BridgeError> {
let query = sqlx::query_as(
Expand All @@ -168,7 +168,7 @@ impl Database {
/// Set the kickoff UTXO for this deposit UTXO.
pub async fn set_kickoff_utxo(
&self,
tx: DatabaseTransaction<'_, '_>,
tx: Option<DatabaseTransaction<'_, '_>>,
deposit_outpoint: OutPoint,
kickoff_utxo: UTXO,
) -> Result<(), BridgeError> {
Expand All @@ -189,7 +189,7 @@ impl Database {
/// If operator already created a kickoff UTXO for this deposit UTXO, return it.
pub async fn get_kickoff_utxo(
&self,
tx: DatabaseTransaction<'_, '_>,
tx: Option<DatabaseTransaction<'_, '_>>,
deposit_outpoint: OutPoint,
) -> Result<Option<UTXO>, BridgeError> {
let query = sqlx::query_as(
Expand All @@ -213,7 +213,7 @@ impl Database {
/// Get unused kickoff_utxo at ready if there are any.
pub async fn get_unused_kickoff_utxo_and_increase_idx(
&self,
tx: DatabaseTransaction<'_, '_>,
tx: Option<DatabaseTransaction<'_, '_>>,
) -> Result<Option<UTXO>, BridgeError> {
// Attempt to fetch the latest transaction details
let query = sqlx::query_as(
Expand Down Expand Up @@ -264,7 +264,7 @@ impl Database {
/// Sets the funding UTXO for kickoffs.
pub async fn set_funding_utxo(
&self,
tx: DatabaseTransaction<'_, '_>,
tx: Option<DatabaseTransaction<'_, '_>>,
funding_utxo: UTXO,
) -> Result<(), BridgeError> {
let query = sqlx::query("INSERT INTO funding_utxos (funding_utxo) VALUES ($1);").bind(
Expand All @@ -282,7 +282,7 @@ impl Database {
/// Gets the funding UTXO for kickoffs
pub async fn get_funding_utxo(
&self,
tx: DatabaseTransaction<'_, '_>,
tx: Option<DatabaseTransaction<'_, '_>>,
) -> Result<Option<UTXO>, BridgeError> {
let query =
sqlx::query_as("SELECT funding_utxo FROM funding_utxos ORDER BY id DESC LIMIT 1;");
Expand Down Expand Up @@ -361,7 +361,7 @@ impl Database {
/// Sets Winternitz public keys for an operator.
pub async fn set_operator_winternitz_public_keys(
&self,
tx: DatabaseTransaction<'_, '_>,
tx: Option<DatabaseTransaction<'_, '_>>,
operator_id: u32,
winternitz_public_key: Vec<WinternitzPublicKey>,
) -> Result<(), BridgeError> {
Expand All @@ -382,7 +382,7 @@ impl Database {
/// operator and a watchtower.
pub async fn get_operator_winternitz_public_keys(
&self,
tx: DatabaseTransaction<'_, '_>,
tx: Option<DatabaseTransaction<'_, '_>>,
operator_id: u32,
) -> Result<Vec<winternitz::PublicKey>, BridgeError> {
let query = sqlx::query_as(
Expand All @@ -403,7 +403,7 @@ impl Database {
/// will be overwritten by the new hashes.
pub async fn set_operator_challenge_ack_hashes(
&self,
tx: DatabaseTransaction<'_, '_>,
tx: Option<DatabaseTransaction<'_, '_>>,
operator_idx: i32,
sequential_collateral_tx_idx: i32,
kickoff_idx: i32,
Expand Down Expand Up @@ -435,7 +435,7 @@ impl Database {
/// tx and kickoff index combination.
pub async fn get_operators_challenge_ack_hashes(
&self,
tx: DatabaseTransaction<'_, '_>,
tx: Option<DatabaseTransaction<'_, '_>>,
operator_idx: i32,
sequential_collateral_tx_idx: i32,
kickoff_idx: i32,
Expand Down Expand Up @@ -534,7 +534,7 @@ impl Database {
/// - funding_txid: the txid of the input[0].
pub async fn add_deposit_kickoff_generator_tx(
&self,
tx: DatabaseTransaction<'_, '_>,
tx: Option<DatabaseTransaction<'_, '_>>,
txid: Txid,
raw_hex: String,
num_kickoffs: usize,
Expand Down Expand Up @@ -578,7 +578,7 @@ impl Database {
/// which determines the order of the sighashes that are signed.
pub async fn set_deposit_signatures(
&self,
tx: DatabaseTransaction<'_, '_>,
tx: Option<DatabaseTransaction<'_, '_>>,
deposit_outpoint: OutPoint,
operator_idx: u32,
signatures: Vec<schnorr::Signature>,
Expand All @@ -601,7 +601,7 @@ impl Database {
/// which determines the order of the sighashes that are signed.
pub async fn get_deposit_signatures(
&self,
tx: DatabaseTransaction<'_, '_>,
tx: Option<DatabaseTransaction<'_, '_>>,
deposit_outpoint: OutPoint,
operator_idx: u32,
) -> Result<Option<Vec<schnorr::Signature>>, BridgeError> {
Expand All @@ -624,7 +624,7 @@ impl Database {
/// Saves BitVM setup data for a specific operator, sequential collateral tx and kickoff index combination
pub async fn set_bitvm_setup(
&self,
tx: DatabaseTransaction<'_, '_>,
tx: Option<DatabaseTransaction<'_, '_>>,
operator_idx: i32,
sequential_collateral_tx_idx: i32,
kickoff_idx: i32,
Expand Down Expand Up @@ -662,7 +662,7 @@ impl Database {
/// Retrieves BitVM setup data for a specific operator, sequential collateral tx and kickoff index combination
pub async fn get_bitvm_setup(
&self,
tx: DatabaseTransaction<'_, '_>,
tx: Option<DatabaseTransaction<'_, '_>>,
operator_idx: i32,
sequential_collateral_tx_idx: i32,
kickoff_idx: i32,
Expand Down
16 changes: 8 additions & 8 deletions core/src/database/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl Database {
/// order as the verifiers' indexes.
pub async fn set_verifiers_public_keys(
&self,
tx: DatabaseTransaction<'_, '_>,
tx: Option<DatabaseTransaction<'_, '_>>,
public_keys: &[PublicKey],
) -> Result<(), BridgeError> {
let mut query = QueryBuilder::new("INSERT INTO verifier_public_keys (idx, public_key) ");
Expand All @@ -39,7 +39,7 @@ impl Database {

pub async fn get_verifiers_public_keys(
&self,
tx: DatabaseTransaction<'_, '_>,
tx: Option<DatabaseTransaction<'_, '_>>,
) -> Result<Vec<PublicKey>, BridgeError> {
let query = sqlx::query_as("SELECT * FROM verifier_public_keys ORDER BY idx;");

Expand All @@ -53,7 +53,7 @@ impl Database {
/// it does nothing.
pub async fn set_kickoff_utxos(
&self,
tx: DatabaseTransaction<'_, '_>,
tx: Option<DatabaseTransaction<'_, '_>>,
deposit_outpoint: OutPoint,
kickoff_utxos: &[UTXO],
) -> Result<(), BridgeError> {
Expand Down Expand Up @@ -110,7 +110,7 @@ impl Database {
/// Sets the generated pub nonces for a verifier.
pub async fn set_nonces(
&self,
tx: DatabaseTransaction<'_, '_>,
tx: Option<DatabaseTransaction<'_, '_>>,
deposit_outpoint: OutPoint,
pub_nonces: &[MusigPubNonce],
) -> Result<(), BridgeError> {
Expand All @@ -135,7 +135,7 @@ impl Database {
/// Gets the public nonces for a deposit UTXO.
pub async fn get_pub_nonces(
&self,
tx: DatabaseTransaction<'_, '_>,
tx: Option<DatabaseTransaction<'_, '_>>,
deposit_outpoint: OutPoint,
) -> Result<Option<Vec<MusigPubNonce>>, BridgeError> {
let query = sqlx::query_as(
Expand All @@ -157,7 +157,7 @@ impl Database {
/// Sets the deposit info to use later.
pub async fn set_deposit_info(
&self,
tx: DatabaseTransaction<'_, '_>,
tx: Option<DatabaseTransaction<'_, '_>>,
deposit_outpoint: OutPoint,
recovery_taproot_address: Address<NetworkUnchecked>,
evm_address: EVMAddress,
Expand Down Expand Up @@ -190,7 +190,7 @@ impl Database {
/// TODO: no test
pub async fn set_sighashes_and_get_nonces(
&self,
tx: DatabaseTransaction<'_, '_>,
tx: Option<DatabaseTransaction<'_, '_>>,
deposit_outpoint: OutPoint,
index: usize,
sighashes: &[Message],
Expand Down Expand Up @@ -238,7 +238,7 @@ impl Database {
/// TODO: no test nor getter
pub async fn set_agg_nonces(
&self,
tx: DatabaseTransaction<'_, '_>,
tx: Option<DatabaseTransaction<'_, '_>>,
deposit_outpoint: OutPoint,
agg_nonces: impl IntoIterator<Item = &MusigAggNonce>,
) -> Result<(), BridgeError> {
Expand Down
14 changes: 7 additions & 7 deletions core/src/database/watchtower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl Database {
/// Sets winternitz public keys of a watchtower for an operator.
pub async fn set_watchtower_winternitz_public_keys(
&self,
tx: DatabaseTransaction<'_, '_>,
tx: Option<DatabaseTransaction<'_, '_>>,
watchtower_id: u32,
operator_id: u32,
winternitz_public_key: Vec<WinternitzPublicKey>,
Expand All @@ -39,7 +39,7 @@ impl Database {
/// collateral tx and operator combination.
pub async fn get_watchtower_winternitz_public_keys(
&self,
tx: DatabaseTransaction<'_, '_>,
tx: Option<DatabaseTransaction<'_, '_>>,
watchtower_id: u32,
operator_id: u32,
) -> Result<Vec<winternitz::PublicKey>, BridgeError> {
Expand All @@ -61,7 +61,7 @@ impl Database {
/// existing entry, it overwrites it with the new addresses.
pub async fn set_watchtower_challenge_addresses(
&self,
tx: DatabaseTransaction<'_, '_>,
tx: Option<DatabaseTransaction<'_, '_>>,
watchtower_id: u32,
operator_id: u32,
watchtower_challenge_addresses: impl AsRef<[ScriptBuf]>,
Expand All @@ -84,7 +84,7 @@ impl Database {
/// Gets the challenge addresses of a watchtower for an operator.
pub async fn get_watchtower_challenge_addresses(
&self,
tx: DatabaseTransaction<'_, '_>,
tx: Option<DatabaseTransaction<'_, '_>>,
watchtower_id: u32,
operator_id: u32,
) -> Result<Vec<ScriptBuf>, BridgeError> {
Expand Down Expand Up @@ -116,7 +116,7 @@ impl Database {
/// Sets xonly public key of a watchtower.
pub async fn set_watchtower_xonly_pk(
&self,
tx: DatabaseTransaction<'_, '_>,
tx: Option<DatabaseTransaction<'_, '_>>,
watchtower_id: u32,
xonly_pk: &XOnlyPublicKey,
) -> Result<(), BridgeError> {
Expand All @@ -134,7 +134,7 @@ impl Database {
/// Gets xonly public key of a watchtower.
pub async fn get_watchtower_xonly_pk(
&self,
tx: DatabaseTransaction<'_, '_>,
tx: Option<DatabaseTransaction<'_, '_>>,
watchtower_id: u32,
) -> Result<XOnlyPublicKey, BridgeError> {
let query = sqlx::query_as(
Expand All @@ -150,7 +150,7 @@ impl Database {
/// Gets xonly public keys of all watchtowers.
pub async fn get_all_watchtowers_xonly_pks(
&self,
tx: DatabaseTransaction<'_, '_>,
tx: Option<DatabaseTransaction<'_, '_>>,
) -> Result<Vec<XOnlyPublicKey>, BridgeError> {
let query = sqlx::query_as(
"SELECT xonly_pk FROM watchtower_xonly_public_keys ORDER BY watchtower_id;",
Expand Down

0 comments on commit 8626332

Please sign in to comment.