Skip to content

Commit

Permalink
Add timelock test
Browse files Browse the repository at this point in the history
  • Loading branch information
ssantos21 committed Dec 19, 2024
1 parent 60011f5 commit c68f335
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 5 deletions.
9 changes: 5 additions & 4 deletions clients/tests/rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -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(())
}
109 changes: 109 additions & 0 deletions clients/tests/rust/src/tb05_timelock.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}

0 comments on commit c68f335

Please sign in to comment.