From f08147a8b0cc812b396fe8a0d03218ff78776b22 Mon Sep 17 00:00:00 2001 From: Tibo-lg Date: Wed, 10 Jul 2024 14:59:30 +0900 Subject: [PATCH 1/2] Add missing sample test --- sample/tests/cli_tests.rs | 84 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 sample/tests/cli_tests.rs diff --git a/sample/tests/cli_tests.rs b/sample/tests/cli_tests.rs new file mode 100644 index 00000000..e3d1cbe7 --- /dev/null +++ b/sample/tests/cli_tests.rs @@ -0,0 +1,84 @@ +use std::{process::Command, thread::sleep, time::Duration}; + +use assert_cmd::cargo::cargo_bin; +use dlc_manager::contract::contract_input::ContractInput; +use rexpect::session::{spawn_command, PtySession}; + +#[test] +#[ignore] +fn sample_cli_test() { + let contract_str = include_str!("../examples/contracts/numerical_contract_input.json"); + let mut contract: ContractInput = serde_json::from_str(&contract_str).unwrap(); + let time_now = std::time::SystemTime::now(); + let unix_time = (time_now + .duration_since(std::time::SystemTime::UNIX_EPOCH) + .unwrap() + + Duration::new(300, 0)) + .as_secs(); + contract.contract_infos[0].oracles.event_id = format!("btcusd{}", unix_time); + + let alice_config_str = include_str!("../examples/configurations/alice.yml"); + let bob_config_str = include_str!("../examples/configurations/bob.yml"); + std::fs::write( + "./numerical_contract_input.json", + serde_json::to_string(&contract).unwrap(), + ) + .unwrap(); + std::fs::write("./alice.yml", alice_config_str).unwrap(); + std::fs::write("./bob.yml", bob_config_str).unwrap(); + + let bin_path = cargo_bin("sample"); + let mut command = Command::new(bin_path.to_str().unwrap()); + command.arg("./alice.yml"); + let mut alice_cli = spawn_command(command, Some(5000)).unwrap(); + + alice_cli.exp_regex("[a-f0-9]{66}").unwrap(); + alice_cli.exp_regex("> $").unwrap(); + + let mut command = Command::new(bin_path.to_str().unwrap()); + command.arg("./bob.yml"); + let mut bob_cli = spawn_command(command, Some(5000)).unwrap(); + + let (_, bob_ip) = bob_cli.exp_regex("[a-f0-9]{66}").unwrap(); + bob_cli.exp_regex("> $").unwrap(); + + alice_cli + .send_line(&format!( + "offercontract {}@127.0.0.1:9001 ./numerical_contract_input.json", + bob_ip + )) + .unwrap(); + + alice_cli.exp_char('>').unwrap(); + + std::thread::sleep(std::time::Duration::from_secs(5)); + + try_send_until(&mut bob_cli, "listoffers", "Offer"); + + let (_, offer_id) = bob_cli.exp_regex("[a-f0-9]{64}").unwrap(); + + bob_cli + .send_line(&format!("acceptoffer {}", offer_id)) + .unwrap(); + bob_cli.exp_char('>').unwrap(); + + try_send_until(&mut alice_cli, "listcontracts", "Signed contract"); + alice_cli.exp_char('>').unwrap(); + + try_send_until(&mut bob_cli, "listcontracts", "Signed contract"); + bob_cli.exp_char('>').unwrap(); +} + +fn try_send_until(session: &mut PtySession, to_send: &str, expected: &str) { + const RETRY: u8 = 5; + + for _ in 0..RETRY { + session.send_line(to_send).unwrap(); + if let Ok(_) = session.exp_string(expected) { + return; + } + sleep(Duration::from_secs(1)); + } + + panic!("Did not receive expected output after {} tries", RETRY); +} From f946eb97273bf6953165b35f50e09af2a5691f1b Mon Sep 17 00:00:00 2001 From: Tibo-lg Date: Wed, 10 Jul 2024 14:44:34 +0900 Subject: [PATCH 2/2] Make clippy happy --- sample/examples/configurations/alice.yml | 1 - sample/examples/configurations/bob.yml | 1 - sample/src/cli.rs | 30 +----------------------- 3 files changed, 1 insertion(+), 31 deletions(-) diff --git a/sample/examples/configurations/alice.yml b/sample/examples/configurations/alice.yml index 71524ea6..e9d0150a 100644 --- a/sample/examples/configurations/alice.yml +++ b/sample/examples/configurations/alice.yml @@ -7,6 +7,5 @@ bitcoinInfo: storageDirPath: './dlc_sample_alice' networkConfiguration: peerListeningPort: 9000 -network: regtest oracleConfig: host: 'http://localhost:8080/' diff --git a/sample/examples/configurations/bob.yml b/sample/examples/configurations/bob.yml index 40ddd2fb..b5228f94 100644 --- a/sample/examples/configurations/bob.yml +++ b/sample/examples/configurations/bob.yml @@ -7,6 +7,5 @@ bitcoinInfo: storageDirPath: './dlc_sample_bob' networkConfiguration: peerListeningPort: 9001 -network: regtest oracleConfig: host: 'http://localhost:8080/' diff --git a/sample/src/cli.rs b/sample/src/cli.rs index fc1484b7..c76b235f 100644 --- a/sample/src/cli.rs +++ b/sample/src/cli.rs @@ -3,7 +3,6 @@ use crate::hex_utils; use crate::DlcManager; use crate::DlcMessageHandler; use crate::PeerManager; -use bitcoin::network::constants::Network; use bitcoin::secp256k1::PublicKey; use dlc_manager::channel::signed_channel::SignedChannelState; use dlc_manager::channel::signed_channel::SignedChannelStateType; @@ -12,15 +11,13 @@ use dlc_manager::contract::Contract; use dlc_manager::Storage; use dlc_messages::Message as DlcMessage; use hex_utils::{hex_str, to_slice}; -use lightning::ln::msgs::SocketAddress; use serde::Deserialize; use serde_json::Value; use std::convert::TryInto; use std::fs; use std::io; use std::io::{BufRead, Write}; -use std::net::{IpAddr, SocketAddr, ToSocketAddrs}; -use std::str::FromStr; +use std::net::{SocketAddr, ToSocketAddrs}; use std::str::SplitWhitespace; use std::sync::{Arc, Mutex}; use std::time::Duration; @@ -44,7 +41,6 @@ pub struct OracleConfig { #[derive(Debug)] pub struct NetworkConfig { pub peer_listening_port: u16, - pub announced_listen_addr: Option, } #[derive(Debug, Deserialize)] @@ -54,9 +50,6 @@ pub struct Configuration { pub storage_dir_path: String, #[serde(deserialize_with = "deserialize_network_configuration")] pub network_configuration: NetworkConfig, - #[serde(default)] - pub announced_node_name: [u8; 32], - pub network: Network, pub oracle_config: OracleConfig, } @@ -72,29 +65,8 @@ where .try_into() .expect("Could not fit port in u16"); - let announced_listen_addr = if let Some(announced_listen_addr) = val.get("announcedListenAddr") - { - let buf = announced_listen_addr - .as_str() - .expect("Error parsing announcedListenAddr"); - match IpAddr::from_str(buf) { - Ok(IpAddr::V4(a)) => Some(SocketAddress::TcpIpV4 { - addr: a.octets(), - port: peer_listening_port, - }), - Ok(IpAddr::V6(a)) => Some(SocketAddress::TcpIpV6 { - addr: a.octets(), - port: peer_listening_port, - }), - Err(_) => panic!("Failed to parse announced-listen-addr into an IP address"), - } - } else { - None - }; - Ok(NetworkConfig { peer_listening_port, - announced_listen_addr, }) }