Skip to content

Commit

Permalink
Add retry tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ozankaymak committed Sep 4, 2024
1 parent 4a47c99 commit 7bc56e0
Show file tree
Hide file tree
Showing 5 changed files with 299 additions and 178 deletions.
17 changes: 9 additions & 8 deletions core/src/database/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,16 +438,17 @@ impl Database {
.fetch_optional(&self.connection)
.await?
.unwrap();

for agg_nonce in agg_nonces {
// After finding the idx deposit_outpoint might be unnecessary
// Only update the row if agg_nonce is NULL
sqlx::query(
"UPDATE nonces SET agg_nonce = $1 WHERE idx = $2 AND deposit_outpoint = $3;",
)
.bind(agg_nonce)
.bind(idx)
.bind(OutPointDB(deposit_outpoint))
.execute(&self.connection)
.await?;
"UPDATE nonces SET agg_nonce = $1 WHERE idx = $2 AND deposit_outpoint = $3 AND agg_nonce IS NULL;",
)
.bind(agg_nonce)
.bind(idx)
.bind(OutPointDB(deposit_outpoint))
.execute(&self.connection)
.await?;
idx += 1;
}

Expand Down
3 changes: 2 additions & 1 deletion core/src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ where
txid: kickoff_tx_handler.tx.compute_txid(),
vout: self.config.operator_num_kickoff_utxos_per_tx as u32,
},
txout: kickoff_tx_handler.tx.output[self.config.operator_num_kickoff_utxos_per_tx].clone(),
txout: kickoff_tx_handler.tx.output[self.config.operator_num_kickoff_utxos_per_tx]
.clone(),
};

let kickoff_utxo = UTXO {
Expand Down
1 change: 1 addition & 0 deletions core/src/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ where
// Check if we already have pub_nonces for this deposit_outpoint.
let pub_nonces_from_db = self.db.get_pub_nonces(deposit_outpoint).await?;
if let Some(pub_nonces) = pub_nonces_from_db {
tracing::debug!("AAAAAAAA");
if !pub_nonces.is_empty() {
if pub_nonces.len() != num_required_nonces {
return Err(BridgeError::NoncesNotFound);
Expand Down
286 changes: 286 additions & 0 deletions core/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,289 @@ pub async fn run_single_deposit(
tracing::debug!("Move txid: {:?}", move_txid);
Ok((verifiers, operators, config, deposit_outpoint))
}

#[cfg(test)]
mod tests {
use clementine_core::actor::Actor;
use clementine_core::database::common::Database;
use clementine_core::extended_rpc::ExtendedRpc;
use clementine_core::mock::common;
use clementine_core::servers::*;
use clementine_core::traits::rpc::AggregatorClient;
use clementine_core::traits::rpc::OperatorRpcClient;
use clementine_core::traits::rpc::VerifierRpcClient;
use clementine_core::user::User;
use clementine_core::EVMAddress;
use clementine_core::{
create_extended_rpc, create_test_config, create_test_config_with_thread_name,
};
use std::thread;

#[tokio::test]
async fn test_deposit_retry() {
let mut config = create_test_config_with_thread_name!("test_config.toml");
let rpc = create_extended_rpc!(config);

let (verifiers, operators, aggregator) =
create_verifiers_and_operators("test_config.toml").await;

// println!("Operators: {:#?}", operators);
// println!("Verifiers: {:#?}", verifiers);

let secret_key = secp256k1::SecretKey::new(&mut secp256k1::rand::thread_rng());

let signer_address = Actor::new(secret_key, config.network)
.address
.as_unchecked()
.clone();
let user = User::new(rpc.clone(), secret_key, config.clone());

let evm_address = EVMAddress([1u8; 20]);
let deposit_address = user.get_deposit_address(evm_address).unwrap();
let deposit_outpoint = rpc
.send_to_address(&deposit_address, config.bridge_amount_sats)
.unwrap();

rpc.mine_blocks(18).unwrap();

// for every verifier, we call new_deposit
// aggregate nonces
let mut pub_nonces = Vec::new();

for (client, _, _) in verifiers.iter() {
let musig_pub_nonces = client
.verifier_new_deposit_rpc(deposit_outpoint, signer_address.clone(), evm_address)
.await
.unwrap();

// tracing::info!("Musig Pub Nonces: {:?}", musig_pub_nonces);

pub_nonces.push(musig_pub_nonces);
}

let agg_nonces = aggregator
.0
.aggregate_pub_nonces_rpc(pub_nonces)
.await
.unwrap();

// Oops, we lost the pub_nonces, need to call the verifiers again
let mut pub_nonces_retry = Vec::new();

for (client, _, _) in verifiers.iter() {
let musig_pub_nonces = client
.verifier_new_deposit_rpc(deposit_outpoint, signer_address.clone(), evm_address)
.await
.unwrap();

// tracing::info!("Musig Pub Nonces: {:?}", musig_pub_nonces);

pub_nonces_retry.push(musig_pub_nonces);
}

let agg_nonces_retry = aggregator
.0
.aggregate_pub_nonces_rpc(pub_nonces_retry)
.await
.unwrap();

// Sanity check
assert_eq!(agg_nonces, agg_nonces_retry);

// call operators' new_deposit
let mut kickoff_utxos = Vec::new();
let mut signatures = Vec::new();

for (client, _, _) in operators.iter() {
// Create deposit kickoff transaction
let (kickoff_utxo, signature) = client
.new_deposit_rpc(deposit_outpoint, signer_address.clone(), evm_address)
.await
.unwrap();

kickoff_utxos.push(kickoff_utxo);
signatures.push(signature);
}

// Oops, we lost the kickoff_utxos, need to call the operators again
let mut kickoff_utxos_retry = Vec::new();
let mut signatures_retry = Vec::new();

for (client, _, _) in operators.iter() {
// Create deposit kickoff transaction
let (kickoff_utxo, signature) = client
.new_deposit_rpc(deposit_outpoint, signer_address.clone(), evm_address)
.await
.unwrap();

kickoff_utxos_retry.push(kickoff_utxo);
signatures_retry.push(signature);
}

// Sanity check
assert_eq!(kickoff_utxos, kickoff_utxos_retry);

tracing::debug!("Now the verifiers sequence starts");
let mut slash_or_take_partial_sigs = Vec::new();

for (client, ..) in verifiers.iter() {
let (partial_sigs, _) = client
.operator_kickoffs_generated_rpc(
deposit_outpoint,
kickoff_utxos_retry.clone(),
signatures_retry.clone(),
agg_nonces.clone(),
)
.await
.unwrap();

slash_or_take_partial_sigs.push(partial_sigs);
}

let slash_or_take_sigs = aggregator
.0
.aggregate_slash_or_take_sigs_rpc(
deposit_outpoint,
kickoff_utxos.clone(),
agg_nonces[config.num_operators + 1..2 * config.num_operators + 1].to_vec(),
slash_or_take_partial_sigs,
)
.await
.unwrap();

// tracing::debug!("Slash or take sigs: {:#?}", slash_or_take_sigs);

// Oops, we lost the slash_or_take_sigs, need to call the verifiers again

let mut slash_or_take_partial_sigs_retry = Vec::new();

for (client, ..) in verifiers.iter() {
let (partial_sigs, _) = client
.operator_kickoffs_generated_rpc(
deposit_outpoint,
kickoff_utxos_retry.clone(),
signatures_retry.clone(),
agg_nonces.clone(),
)
.await
.unwrap();

slash_or_take_partial_sigs_retry.push(partial_sigs);
}

let slash_or_take_sigs_retry = aggregator
.0
.aggregate_slash_or_take_sigs_rpc(
deposit_outpoint,
kickoff_utxos.clone(),
agg_nonces[config.num_operators + 1..2 * config.num_operators + 1].to_vec(),
slash_or_take_partial_sigs_retry,
)
.await
.unwrap();

// call burn_txs_signed_rpc
let mut operator_take_partial_sigs: Vec<Vec<[u8; 32]>> = Vec::new();
for (client, ..) in verifiers.iter() {
let partial_sigs = client
.burn_txs_signed_rpc(deposit_outpoint, vec![], slash_or_take_sigs.clone())
.await
.unwrap();
operator_take_partial_sigs.push(partial_sigs);
}
// tracing::debug!(
// "Operator take partial sigs: {:#?}",
// operator_take_partial_sigs
// );
let operator_take_sigs = aggregator
.0
.aggregate_operator_take_sigs_rpc(
deposit_outpoint,
kickoff_utxos.clone(),
agg_nonces[1..config.num_operators + 1].to_vec(),
operator_take_partial_sigs,
)
.await
.unwrap();

// Oops, we lost the operator_take_sigs, need to call the verifiers again

let mut operator_take_partial_sigs_retry = Vec::new();

for (client, ..) in verifiers.iter() {
let partial_sigs = client
.burn_txs_signed_rpc(deposit_outpoint, vec![], slash_or_take_sigs_retry.clone())
.await
.unwrap();
operator_take_partial_sigs_retry.push(partial_sigs);
}

let operator_take_sigs_retry = aggregator
.0
.aggregate_operator_take_sigs_rpc(
deposit_outpoint,
kickoff_utxos.clone(),
agg_nonces[1..config.num_operators + 1].to_vec(),
operator_take_partial_sigs_retry,
)
.await
.unwrap();

// tracing::debug!("Operator take sigs: {:#?}", operator_take_sigs);
// call operator_take_txs_signed_rpc
let mut move_tx_partial_sigs = Vec::new();
for (client, _, _) in verifiers.iter() {
let move_tx_partial_sig = client
.operator_take_txs_signed_rpc(deposit_outpoint, operator_take_sigs.clone())
.await
.unwrap();
move_tx_partial_sigs.push(move_tx_partial_sig);
}

// Oops, we lost the move_tx_partial_sigs, need to call the verifiers again

let mut move_tx_partial_sigs_retry = Vec::new();

for (client, _, _) in verifiers.iter() {
let move_tx_partial_sig = client
.operator_take_txs_signed_rpc(deposit_outpoint, operator_take_sigs_retry.clone())
.await
.unwrap();
move_tx_partial_sigs_retry.push(move_tx_partial_sig);
}

// tracing::debug!("Move tx partial sigs: {:#?}", move_tx_partial_sigs);

// aggreagte move_tx_partial_sigs

let _move_tx = aggregator
.0
.aggregate_move_tx_sigs_rpc(
deposit_outpoint,
signer_address.clone(),
evm_address,
agg_nonces[0].clone(),
move_tx_partial_sigs,
)
.await
.unwrap();

let move_tx_retry = aggregator
.0
.aggregate_move_tx_sigs_rpc(
deposit_outpoint,
signer_address,
evm_address,
agg_nonces[0].clone(),
move_tx_partial_sigs_retry,
)
.await
.unwrap();

// tracing::debug!("Move tx: {:#?}", move_tx);
// tracing::debug!("Move tx_hex: {:?}", move_tx_handler.tx.raw_hex());
tracing::debug!("Move tx retry weight: {:?}", move_tx_retry.weight());
let move_txid = rpc.send_raw_transaction(&move_tx_retry).unwrap();
tracing::debug!("Move txid: {:?}", move_txid);
}
}
Loading

0 comments on commit 7bc56e0

Please sign in to comment.