Skip to content

Commit

Permalink
Add maker-cli testing
Browse files Browse the repository at this point in the history
refactor test_framework mod.rs to expose some common functions.

some other changes in maker to make things consistent.
  • Loading branch information
mojoX911 committed Dec 5, 2024
1 parent 6e5ce00 commit 4e0365f
Show file tree
Hide file tree
Showing 23 changed files with 489 additions and 115 deletions.
2 changes: 1 addition & 1 deletion src/bin/directoryd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::{path::PathBuf, str::FromStr, sync::Arc};
author = option_env ! ("CARGO_PKG_AUTHORS").unwrap_or(""))]
struct Cli {
/// Optional network type.
#[clap(long, short = 'n', default_value = "clearnet", possible_values = &["tor", "clearnet"])]
#[clap(long, short = 'n', default_value = "tor", possible_values = &["tor", "clearnet"])]
network: String,
/// Optional DNS data directory. Default value : "~/.coinswap/directory"
#[clap(long, short = 'd')]
Expand Down
13 changes: 10 additions & 3 deletions src/bin/maker-cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,18 @@ enum Commands {
FidelityBalance,
/// Gets a new address
NewAddress,
// Send to an external wallet address.
// Send to an external address and returns the transaction hex.
SendToAddress {
address: String,
amount: u64,
fee: u64,
},
/// Returns the tor address
GetTorAddress,
/// Returns the data dir
/// Returns the data directory path
GetDataDir,
/// Stops the maker server
Stop,
}

fn main() -> Result<(), MakerError> {
Expand Down Expand Up @@ -95,12 +97,17 @@ fn main() -> Result<(), MakerError> {
fee,
})?;
}
// TODO: Test Coverage
Commands::GetTorAddress => {
send_rpc_req(&RpcMsgReq::GetTorAddress)?;
}
// TODO: Test Coverage
Commands::GetDataDir => {
send_rpc_req(&RpcMsgReq::GetDataDir)?;
}
Commands::Stop => {
send_rpc_req(&RpcMsgReq::Stop)?;
}
}

Ok(())
Expand All @@ -116,7 +123,7 @@ fn send_rpc_req(req: &RpcMsgReq) -> Result<(), MakerError> {
let response_bytes = read_message(&mut stream)?;
let response: RpcMsgResp = serde_cbor::from_slice(&response_bytes)?;

println!("{:?}", response);
println!("{}", response);

Ok(())
}
2 changes: 1 addition & 1 deletion src/bin/makerd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::{path::PathBuf, str::FromStr, sync::Arc};
author = option_env ! ("CARGO_PKG_AUTHORS").unwrap_or(""))]
struct Cli {
/// Optional Connection Network Type
#[clap(long, default_value = "clearnet", possible_values = &["tor", "clearnet"])]
#[clap(long, default_value = "tor", possible_values = &["tor", "clearnet"])]
network: String,
/// Optional DNS data directory. Default value : "~/.coinswap/maker"
#[clap(long, short = 'd')]
Expand Down
17 changes: 8 additions & 9 deletions src/maker/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ pub struct Maker {
pub highest_fidelity_proof: RwLock<Option<FidelityProof>>,
/// Is setup complete
pub is_setup_complete: AtomicBool,
/// Path for the data directory.
pub data_dir: PathBuf,
}

#[allow(clippy::too_many_arguments)]
Expand Down Expand Up @@ -141,15 +143,7 @@ impl Maker {
};

// Get provided data directory or the default data directory.
let data_dir = if cfg!(feature = "integration-test") {
// We only append port number in data-dir for integration test
let port = port.expect("port value expected in Int tests");
data_dir.map_or(get_maker_dir().join(port.to_string()), |d| {
d.join("maker").join(port.to_string())
})
} else {
data_dir.unwrap_or(get_maker_dir())
};
let data_dir = data_dir.unwrap_or(get_maker_dir());

let wallet_dir = data_dir.join("wallets");

Expand Down Expand Up @@ -222,9 +216,14 @@ impl Maker {
connection_state: Mutex::new(HashMap::new()),
highest_fidelity_proof: RwLock::new(None),
is_setup_complete: AtomicBool::new(false),
data_dir,
})
}

pub fn get_data_dir(&self) -> &PathBuf {
&self.data_dir
}

/// Returns a reference to the Maker's wallet.
pub fn get_wallet(&self) -> &RwLock<Wallet> {
&self.wallet
Expand Down
2 changes: 1 addition & 1 deletion src/maker/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl Default for MakerConfig {
time_relative_fee_ppb: Amount::from_sat(100_000),
min_size: 10_000,
socks_port: 19050,
directory_server_address: "directoryhiddenserviceaddress.onion:8080".to_string(),
directory_server_address: "127.0.0.1:8080".to_string(),
fidelity_value: 5_000_000, // 5 million sats
fidelity_timelock: 26_000, // Approx 6 months of blocks
connection_type: ConnectionType::TOR,
Expand Down
27 changes: 26 additions & 1 deletion src/maker/rpc/messages.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::{fmt::Display, path::PathBuf};

use bitcoind::bitcoincore_rpc::json::ListUnspentResultEntry;
use serde::{Deserialize, Serialize};

Expand All @@ -20,6 +22,7 @@ pub enum RpcMsgReq {
},
GetTorAddress,
GetDataDir,
Stop,
}

#[derive(Serialize, Deserialize, Debug)]
Expand All @@ -36,5 +39,27 @@ pub enum RpcMsgResp {
NewAddressResp(String),
SendToAddressResp(String),
GetTorAddressResp(String),
GetDataDirResp(String),
GetDataDirResp(PathBuf),
Shutdown,
}

impl Display for RpcMsgResp {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Pong => write!(f, "Pong"),
Self::NewAddressResp(addr) => write!(f, "{}", addr),
Self::SeedBalanceResp(bal) => write!(f, "{} sats", bal),
Self::ContractBalanceResp(bal) => write!(f, "{} sats", bal),
Self::SwapBalanceResp(bal) => write!(f, "{} sats", bal),
Self::FidelityBalanceResp(bal) => write!(f, "{} sats", bal),
Self::SeedUtxoResp { utxos } => write!(f, "{:?}", utxos),
Self::SwapUtxoResp { utxos } => write!(f, "{:?}", utxos),
Self::FidelityUtxoResp { utxos } => write!(f, "{:?}", utxos),
Self::ContractUtxoResp { utxos } => write!(f, "{:?}", utxos),
Self::SendToAddressResp(tx_hex) => write!(f, "{:?}", tx_hex),
Self::GetTorAddressResp(addr) => write!(f, "{:?}", addr),
Self::GetDataDirResp(path) => write!(f, "{:?}", path),
Self::Shutdown => write!(f, "Shutdown Initiated"),
}
}
}
37 changes: 30 additions & 7 deletions src/maker/rpc/server.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::{
io::ErrorKind,
fs::File,
io::{ErrorKind, Read},
net::{TcpListener, TcpStream},
path::PathBuf,
sync::{atomic::Ordering::Relaxed, Arc},
thread::sleep,
time::Duration,
Expand All @@ -10,7 +12,7 @@ use bitcoin::{Address, Amount};

use crate::{
maker::{error::MakerError, rpc::messages::RpcMsgResp, Maker},
utill::{get_maker_dir, get_tor_addrs, read_message, send_message},
utill::{read_message, send_message, ConnectionType},
wallet::{Destination, SendAmount},
};
use std::str::FromStr;
Expand Down Expand Up @@ -144,16 +146,37 @@ fn handle_request(maker: &Arc<Maker>, socket: &mut TcpStream) -> Result<(), Make
};
}
RpcMsgReq::GetDataDir => {
let path = get_maker_dir().display().to_string();
let resp = RpcMsgResp::GetDataDirResp(path);
let path = maker.get_data_dir();
let resp = RpcMsgResp::GetDataDirResp(path.clone());
if let Err(e) = send_message(socket, &resp) {
log::info!("Error sending RPC response {:?}", e);
};
}
RpcMsgReq::GetTorAddress => {
let path = get_maker_dir().join("tor");
let resp = RpcMsgResp::GetTorAddressResp(get_tor_addrs(&path)?);
if let Err(e) = send_message(socket, &resp) {
if maker.config.connection_type == ConnectionType::CLEARNET {
let resp = RpcMsgResp::GetTorAddressResp("Maker Tor is not running".to_string());
if let Err(e) = send_message(socket, &resp) {
log::info!("Error sending RPC response {:?}", e);
};
} else {
let maker_hs_path_str =
format!("/tmp/tor-rust-maker{}/hs-dir/hostname", maker.config.port);
let maker_hs_path = PathBuf::from(maker_hs_path_str);
let mut maker_file = File::open(maker_hs_path)?;
let mut maker_onion_addr: String = String::new();
maker_file.read_to_string(&mut maker_onion_addr)?;
maker_onion_addr.pop();
let maker_address = format!("{}:{}", maker_onion_addr, maker.config.port);

let resp = RpcMsgResp::GetTorAddressResp(maker_address);
if let Err(e) = send_message(socket, &resp) {
log::info!("Error sending RPC response {:?}", e);
};
}
}
RpcMsgReq::Stop => {
maker.shutdown.store(true, Relaxed);
if let Err(e) = send_message(socket, &RpcMsgResp::Shutdown) {
log::info!("Error sending RPC response {:?}", e);
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/fidelity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ impl Wallet {
break ht;
} else {
log::info!(
"Fildelity Transaction {} seen in mempool, waiting for confirmation.",
"Fidelity Transaction {} seen in mempool, waiting for confirmation.",
txid
);

Expand Down
1 change: 0 additions & 1 deletion tests/abort1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ fn test_stop_taker_after_setup() {
// Initiate test framework, Makers.
// Taker has a special behavior DropConnectionAfterFullSetup.
let (test_framework, taker, makers, directory_server_instance) = TestFramework::init(
None,
makers_config_map.into(),
Some(TakerBehavior::DropConnectionAfterFullSetup),
ConnectionType::CLEARNET,
Expand Down
8 changes: 2 additions & 6 deletions tests/abort2_case1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,8 @@ fn test_abort_case_2_move_on_with_other_makers() {

// Initiate test framework, Makers.
// Taker has normal behavior.
let (test_framework, taker, makers, directory_server_instance) = TestFramework::init(
None,
makers_config_map.into(),
None,
ConnectionType::CLEARNET,
);
let (test_framework, taker, makers, directory_server_instance) =
TestFramework::init(makers_config_map.into(), None, ConnectionType::CLEARNET);

warn!(
"Running Test: Maker 6102 closes before sending sender's sigs. Taker moves on with other Makers."
Expand Down
8 changes: 2 additions & 6 deletions tests/abort2_case2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,8 @@ fn test_abort_case_2_recover_if_no_makers_found() {

// Initiate test framework, Makers.
// Taker has normal behavior.
let (test_framework, taker, makers, directory_server_instance) = TestFramework::init(
None,
makers_config_map.into(),
None,
ConnectionType::CLEARNET,
);
let (test_framework, taker, makers, directory_server_instance) =
TestFramework::init(makers_config_map.into(), None, ConnectionType::CLEARNET);

// Fund the Taker and Makers with 3 utxos of 0.05 btc each.
for _ in 0..3 {
Expand Down
8 changes: 2 additions & 6 deletions tests/abort2_case3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,8 @@ fn maker_drops_after_sending_senders_sigs() {

// Initiate test framework, Makers.
// Taker has normal behavior.
let (test_framework, taker, makers, directory_server_instance) = TestFramework::init(
None,
makers_config_map.into(),
None,
ConnectionType::CLEARNET,
);
let (test_framework, taker, makers, directory_server_instance) =
TestFramework::init(makers_config_map.into(), None, ConnectionType::CLEARNET);

warn!(
"Running Test: Maker 6102 Closes after sending sender's signature. This is really bad. Recovery is the only option."
Expand Down
8 changes: 2 additions & 6 deletions tests/abort3_case1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,8 @@ fn abort3_case1_close_at_contract_sigs_for_recvr_and_sender() {

// Initiate test framework, Makers.
// Taker has normal behavior.
let (test_framework, taker, makers, directory_server_instance) = TestFramework::init(
None,
makers_config_map.into(),
None,
ConnectionType::CLEARNET,
);
let (test_framework, taker, makers, directory_server_instance) =
TestFramework::init(makers_config_map.into(), None, ConnectionType::CLEARNET);

warn!("Running Test: Maker closes connection after receiving a ContractSigsForRecvrAndSender");

Expand Down
8 changes: 2 additions & 6 deletions tests/abort3_case2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,8 @@ fn abort3_case2_close_at_contract_sigs_for_recvr() {

// Initiate test framework, Makers.
// Taker has normal behavior.
let (test_framework, taker, makers, directory_server_instance) = TestFramework::init(
None,
makers_config_map.into(),
None,
ConnectionType::CLEARNET,
);
let (test_framework, taker, makers, directory_server_instance) =
TestFramework::init(makers_config_map.into(), None, ConnectionType::CLEARNET);

warn!("Running Test: Maker closes connection after sending a ContractSigsForRecvr");

Expand Down
8 changes: 2 additions & 6 deletions tests/abort3_case3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,8 @@ fn abort3_case3_close_at_hash_preimage_handover() {

// Initiate test framework, Makers.
// Taker has normal behavior.
let (test_framework, taker, makers, directory_server_instance) = TestFramework::init(
None,
makers_config_map.into(),
None,
ConnectionType::CLEARNET,
);
let (test_framework, taker, makers, directory_server_instance) =
TestFramework::init(makers_config_map.into(), None, ConnectionType::CLEARNET);

warn!("Running Test: Maker closes conneciton at hash preimage handling");

Expand Down
22 changes: 15 additions & 7 deletions tests/dns.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg(feature = "integration-test")]
use std::{
io::{BufRead, BufReader, Write},
net::TcpStream,
Expand All @@ -13,35 +14,42 @@ use std::{
fn start_server() -> (Child, Receiver<String>) {
let (log_sender, log_receiver): (Sender<String>, Receiver<String>) = mpsc::channel();
let mut directoryd_process = Command::new("./target/debug/directoryd")
.args(&["-n", "clearnet"])
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.spawn()
.unwrap();

let stdout = directoryd_process.stdout.take().unwrap();
let std_err = directoryd_process.stderr.take().unwrap();
thread::spawn(move || {
let reader = BufReader::new(stdout);
reader.lines().map_while(Result::ok).for_each(|line| {
println!("{}", line);
log_sender.send(line).unwrap_or_else(|e| {
println!("Failed to send log: {}", e);
});
});
});

thread::spawn(move || {
let reader = BufReader::new(std_err);
reader.lines().map_while(Result::ok).for_each(|line| {
panic!("Error : {}", line);
})
});

(directoryd_process, log_receiver)
}

fn wait_for_server_start(log_receiver: &Receiver<String>) {
let mut server_started = false;
while let Ok(log_message) = log_receiver.recv_timeout(Duration::from_secs(5)) {
loop {
let log_message = log_receiver.recv().unwrap();
if log_message.contains("RPC socket binding successful") {
server_started = true;
log::info!("DNS server started");
break;
}
}
assert!(
server_started,
"Server did not start within the expected time"
);
}

fn send_addresses(addresses: &[&str]) {
Expand Down
8 changes: 2 additions & 6 deletions tests/fidelity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,8 @@ fn test_fidelity() {
// ---- Setup ----
let makers_config_map = [((6102, None), MakerBehavior::Normal)];

let (test_framework, _, makers, directory_server_instance) = TestFramework::init(
None,
makers_config_map.into(),
None,
ConnectionType::CLEARNET,
);
let (test_framework, _, makers, directory_server_instance) =
TestFramework::init(makers_config_map.into(), None, ConnectionType::CLEARNET);

let maker = makers.first().unwrap();

Expand Down
Loading

0 comments on commit 4e0365f

Please sign in to comment.