From c68f3356211c4db1307922e0f4b54b817a4470af Mon Sep 17 00:00:00 2001 From: "S. Santos" Date: Thu, 19 Dec 2024 05:41:43 -0300 Subject: [PATCH] Add timelock test --- clients/tests/rust/src/main.rs | 9 +- clients/tests/rust/src/tb05_timelock.rs | 109 ++++++++++++++++++++ clients/tests/rust/src/{tv05.rs => tv01.rs} | 2 +- 3 files changed, 115 insertions(+), 5 deletions(-) create mode 100644 clients/tests/rust/src/tb05_timelock.rs rename clients/tests/rust/src/{tv05.rs => tv01.rs} (98%) diff --git a/clients/tests/rust/src/main.rs b/clients/tests/rust/src/main.rs index 90556314..6010d862 100644 --- a/clients/tests/rust/src/main.rs +++ b/clients/tests/rust/src/main.rs @@ -8,8 +8,9 @@ pub mod tb01_simple_transfer; pub mod tb02_transfer_address_reuse; pub mod tb03_simple_atomic_transfer; pub mod tb04_simple_lightning_latch; +pub mod tb05_timelock; pub mod tm01_sender_double_spends; -mod tv05; +mod tv01; use anyhow::{Result, Ok}; #[tokio::main(flavor = "current_thread")] @@ -19,12 +20,12 @@ async fn main() -> Result<()> { tb02_transfer_address_reuse::execute().await?; tb03_simple_atomic_transfer::execute().await?; tb04_simple_lightning_latch::execute().await?; + tb05_timelock::execute().await?; tm01_sender_double_spends::execute().await?; ta01_sign_second_not_called::execute().await?; ta02_duplicate_deposits::execute().await?; - tv05::execute().await?; - ta03_multiple_deposits::execute().await?; - + tv01::execute().await?; + Ok(()) } diff --git a/clients/tests/rust/src/tb05_timelock.rs b/clients/tests/rust/src/tb05_timelock.rs new file mode 100644 index 00000000..d95da487 --- /dev/null +++ b/clients/tests/rust/src/tb05_timelock.rs @@ -0,0 +1,109 @@ +use std::{env, process::Command, thread, time::Duration}; + +use anyhow::{Result, Ok}; +use electrum_client::ElectrumApi; +use mercuryrustlib::{client_config::ClientConfig, CoinStatus, Wallet}; + +use crate::{bitcoin_core, electrs}; + +pub async fn old_state_broadcasted(client_config: &ClientConfig, wallet1: &Wallet, wallet2: &Wallet) -> Result<()> { + + let amount = 1000; + + // Create first deposit address + + let token_id = mercuryrustlib::deposit::get_token(client_config).await?; + + let deposit_address = mercuryrustlib::deposit::get_deposit_bitcoin_address(&client_config, &wallet1.name, &token_id, amount).await?; + + let _ = bitcoin_core::sendtoaddress(amount, &deposit_address)?; + + let core_wallet_address = bitcoin_core::getnewaddress()?; + let remaining_blocks = client_config.confirmation_target; + let _ = bitcoin_core::generatetoaddress(remaining_blocks, &core_wallet_address)?; + + // It appears that Electrs takes a few seconds to index the transaction + let mut is_tx_indexed = false; + + while !is_tx_indexed { + is_tx_indexed = electrs::check_address(client_config, &deposit_address, amount).await?; + thread::sleep(Duration::from_secs(1)); + } + + let wallet1_transfer_adress = mercuryrustlib::transfer_receiver::new_transfer_address(&client_config, &wallet1.name).await?; + + mercuryrustlib::coin_status::update_coins(&client_config, &wallet1.name).await?; + let wallet1: mercuryrustlib::Wallet = mercuryrustlib::sqlite_manager::get_wallet(&client_config.pool, &wallet1.name).await?; + let new_coin = wallet1.coins.iter().find(|&coin| coin.aggregated_address == Some(deposit_address.clone()) && coin.status == CoinStatus::CONFIRMED).unwrap(); + let statechain_id_1 = new_coin.statechain_id.as_ref().unwrap(); + + let force_send = false; + + let result = mercuryrustlib::transfer_sender::execute(&client_config, &wallet1_transfer_adress, &wallet1.name, &statechain_id_1.clone(), None, force_send, None).await; + + assert!(result.is_ok()); + + let wallet2_transfer_adress = mercuryrustlib::transfer_receiver::new_transfer_address(&client_config, &wallet2.name).await?; + + mercuryrustlib::coin_status::update_coins(&client_config, &wallet1.name).await?; + let wallet1: mercuryrustlib::Wallet = mercuryrustlib::sqlite_manager::get_wallet(&client_config.pool, &wallet1.name).await?; + + let result = mercuryrustlib::transfer_sender::execute(&client_config, &wallet2_transfer_adress, &wallet1.name, &statechain_id_1.clone(), None, force_send, None).await; + + assert!(result.is_ok()); + + let backup_transactions = mercuryrustlib::sqlite_manager::get_backup_txs(&client_config.pool, &wallet1.name, &statechain_id_1).await?; + + assert!(backup_transactions.len() == 3); + + let bkp_tx = backup_transactions.get(1).unwrap(); + + assert!(bkp_tx.tx_n == 2); + + let tx_bytes = hex::decode(&bkp_tx.tx)?; + let txid = client_config.electrum_client.transaction_broadcast_raw(&tx_bytes); + + assert!(txid.is_err()); + + let tx_lock_time = mercuryrustlib::get_blockheight(&bkp_tx)?; + + let current_blockheight = electrs::get_blockheight(&client_config).await? as u32; + + let height_diff = tx_lock_time - current_blockheight; + + let _ = bitcoin_core::generatetoaddress(height_diff, &core_wallet_address)?; + + let txid = client_config.electrum_client.transaction_broadcast_raw(&tx_bytes); + + assert!(txid.is_ok()); + + Ok(()) + +} + +pub async fn execute() -> Result<()> { + + let _ = Command::new("rm").arg("wallet.db").arg("wallet.db-shm").arg("wallet.db-wal").output().expect("failed to execute process"); + + env::set_var("ML_NETWORK", "regtest"); + + let client_config = mercuryrustlib::client_config::load().await; + + let wallet1 = mercuryrustlib::wallet::create_wallet( + "wallet1", + &client_config).await?; + + mercuryrustlib::sqlite_manager::insert_wallet(&client_config.pool, &wallet1).await?; + + let wallet2 = mercuryrustlib::wallet::create_wallet( + "wallet2", + &client_config).await?; + + mercuryrustlib::sqlite_manager::insert_wallet(&client_config.pool, &wallet2).await?; + + old_state_broadcasted(&client_config, &wallet1, &wallet2).await?; + + println!("TB05 - Timelock tests completed successfully"); + + Ok(()) +} \ No newline at end of file diff --git a/clients/tests/rust/src/tv05.rs b/clients/tests/rust/src/tv01.rs similarity index 98% rename from clients/tests/rust/src/tv05.rs rename to clients/tests/rust/src/tv01.rs index 43cd8e35..d27b52f8 100644 --- a/clients/tests/rust/src/tv05.rs +++ b/clients/tests/rust/src/tv01.rs @@ -97,7 +97,7 @@ pub async fn execute() -> Result<()> { w1_transfer_to_w2(&client_config, &wallet1, &wallet2).await?; - println!("TV05 - Result as reported."); + println!("TV01 - Result as reported."); Ok(()) } \ No newline at end of file