Skip to content

Commit

Permalink
WIP additional testing with small fixes + db problem
Browse files Browse the repository at this point in the history
  • Loading branch information
ozankaymak committed Sep 11, 2024
1 parent 9f8db31 commit 72523bb
Show file tree
Hide file tree
Showing 5 changed files with 420 additions and 34 deletions.
58 changes: 57 additions & 1 deletion core/src/database/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1139,6 +1139,62 @@ mod tests {
assert!(db_utxo.is_none());
}

#[tokio::test]
async fn test_deposit_kickoff_generator_tx_0() {
let config = create_test_config_with_thread_name("test_config.toml", None).await;
let db = Database::new(config).await.unwrap();

let raw_hex = "02000000000101eb87b1a80d47b7f5bd5082b77653f5ca37e566951742b80c361875ba0e5c478f0a00000000fdffffff0ca086010000000000225120b23da6d2e0390018b953f7d74e3582da4da30fd0fd157cc84a2d2753003d1ca3a086010000000000225120b23da6d2e0390018b953f7d74e3582da4da30fd0fd157cc84a2d2753003d1ca3a086010000000000225120b23da6d2e0390018b953f7d74e3582da4da30fd0fd157cc84a2d2753003d1ca3a086010000000000225120b23da6d2e0390018b953f7d74e3582da4da30fd0fd157cc84a2d2753003d1ca3a086010000000000225120b23da6d2e0390018b953f7d74e3582da4da30fd0fd157cc84a2d2753003d1ca3a086010000000000225120b23da6d2e0390018b953f7d74e3582da4da30fd0fd157cc84a2d2753003d1ca3a086010000000000225120b23da6d2e0390018b953f7d74e3582da4da30fd0fd157cc84a2d2753003d1ca3a086010000000000225120b23da6d2e0390018b953f7d74e3582da4da30fd0fd157cc84a2d2753003d1ca3a086010000000000225120b23da6d2e0390018b953f7d74e3582da4da30fd0fd157cc84a2d2753003d1ca3a086010000000000225120b23da6d2e0390018b953f7d74e3582da4da30fd0fd157cc84a2d2753003d1ca35c081777000000002251202a64b1ee3375f3bb4b367b8cb8384a47f73cf231717f827c6c6fbbf5aecf0c364a010000000000002200204ae81572f06e1b88fd5ced7a1a000945432e83e1551e6f721ee9c00b8cc33260014005a41e6f4a4bcfcc5cd3ef602687215f97c18949019a491df56af7413c5dce9292ba3966edc4564a39d9bc0d6c0faae19030f1cedf4d931a6cdc57cc5b83c8ef00000000".to_string();
let tx: bitcoin::Transaction =
bitcoin::consensus::deserialize(&hex::decode(raw_hex.clone()).unwrap()).unwrap();
let txid = tx.compute_txid();
let num_kickoffs = tx.output.len() - 2;
let funding_txid = tx.input[0].previous_output.txid;
db.add_deposit_kickoff_generator_tx(
None,
txid,
raw_hex.clone(),
num_kickoffs,
funding_txid,
)
.await
.unwrap();
for i in 0..num_kickoffs - 1 {
let (db_raw_hex, db_num_kickoffs, db_cur_unused_kickoff_index, db_funding_txid) = db
.get_deposit_kickoff_generator_tx(txid)
.await
.unwrap()
.unwrap();

// Sanity check
assert_eq!(db_raw_hex, raw_hex);
assert_eq!(db_num_kickoffs, num_kickoffs);
assert_eq!(db_cur_unused_kickoff_index, i + 1);
assert_eq!(db_funding_txid, funding_txid);

let unused_utxo = db
.get_unused_kickoff_utxo_and_increase_idx(None)
.await
.unwrap()
.unwrap();
tracing::info!("unused_utxo: {:?}", unused_utxo);

// Sanity check
assert_eq!(unused_utxo.outpoint.txid, txid);
assert_eq!(unused_utxo.outpoint.vout, i as u32 + 1);
}
}

#[tokio::test]
async fn test_deposit_kickoff_generator_tx_2() {
let config = create_test_config_with_thread_name("test_config.toml", None).await;
let db = Database::new(config).await.unwrap();

let txid = Txid::from_byte_array([1u8; 32]);
let res = db.get_deposit_kickoff_generator_tx(txid).await.unwrap();
assert!(res.is_none());
}

#[tokio::test]
async fn test_deposit_kickoff_generator_tx_1() {
let config = create_test_config_with_thread_name("test_config.toml", None).await;
Expand Down Expand Up @@ -1190,7 +1246,7 @@ mod tests {
}

#[tokio::test]
async fn test_deposit_kickoff_generator_tx_2() {
async fn test_deposit_kickoff_generator_tx_3() {
let config = create_test_config_with_thread_name("test_config.toml", None).await;
let db = Database::new(config).await.unwrap();

Expand Down
12 changes: 12 additions & 0 deletions core/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,18 @@ pub enum BridgeError {
#[error("NotEnoughFeeForOperator")]
NotEnoughFeeForOperator,

#[error("KickoffGeneratorTxNotFound")]
KickoffGeneratorTxNotFound,

#[error("KickoffGeneratorTxsTooManyIterations")]
KickoffGeneratorTxsTooManyIterations,

#[error("OperatorSlashOrTakeSigNotFound")]
OperatorSlashOrTakeSigNotFound,

#[error("OperatorTakesSigNotFound")]
OperatorTakesSigNotFound,

#[error("Musig2 error: {0}")]
Musig2Error(#[from] musig2::secp256k1::Error),
}
Expand Down
16 changes: 11 additions & 5 deletions core/src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ where
None
};

tracing::debug!(
"Operator idx: {:?}, db created with name: {:?}",
idx,
config.db_name
);

Ok(Self {
rpc,
db,
Expand Down Expand Up @@ -513,7 +519,7 @@ where
.get_kickoff_utxo(None, deposit_outpoint)
.await?
.ok_or(BridgeError::KickoffOutpointsNotFound)?;
tracing::debug!("Kickoff UTXO FOUND: {:?}", kickoff_utxo);
tracing::debug!("Kickoff UTXO FOUND after withdrawal: {:?}", kickoff_utxo);
let mut txs_to_be_sent = vec![];
let mut current_searching_txid = kickoff_utxo.outpoint.txid;
let mut found_txid = false;
Expand All @@ -534,7 +540,7 @@ where
.db
.get_deposit_kickoff_generator_tx(current_searching_txid)
.await?
.ok_or(BridgeError::KickoffOutpointsNotFound)?; // TODO: Fix this error
.ok_or(BridgeError::KickoffGeneratorTxNotFound)?;

txs_to_be_sent.push(raw_signed_tx);
current_searching_txid = funding_txid;
Expand All @@ -544,7 +550,7 @@ where

// Handle the case where no transaction was found in 25 iterations
if !found_txid {
return Err(BridgeError::KickoffOutpointsNotFound); // TODO: Fix this error
return Err(BridgeError::KickoffGeneratorTxsTooManyIterations); // TODO: Fix this error
}
// tracing::debug!("Found txs to be sent: {:?}", txs_to_be_sent);

Expand Down Expand Up @@ -576,7 +582,7 @@ where
.db
.get_slash_or_take_sig(deposit_outpoint, kickoff_utxo.clone())
.await?
.ok_or(BridgeError::KickoffOutpointsNotFound)?; // TODO: Fix this error
.ok_or(BridgeError::OperatorSlashOrTakeSigNotFound)?;

// tracing::debug!("Found nofn sig: {:?}", nofn_sig);

Expand Down Expand Up @@ -633,7 +639,7 @@ where
.db
.get_operator_take_sig(deposit_outpoint, kickoff_utxo)
.await?
.ok_or(BridgeError::KickoffOutpointsNotFound)?; // TODO: Fix this error
.ok_or(BridgeError::OperatorTakesSigNotFound)?;
tracing::debug!("Operator Found nofn sig: {:?}", operator_takes_nofn_sig);

let our_sig = self
Expand Down
Loading

0 comments on commit 72523bb

Please sign in to comment.