Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/add missing sample test #232

Merged
merged 2 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion sample/examples/configurations/alice.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@ bitcoinInfo:
storageDirPath: './dlc_sample_alice'
networkConfiguration:
peerListeningPort: 9000
network: regtest
oracleConfig:
host: 'http://localhost:8080/'
1 change: 0 additions & 1 deletion sample/examples/configurations/bob.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@ bitcoinInfo:
storageDirPath: './dlc_sample_bob'
networkConfiguration:
peerListeningPort: 9001
network: regtest
oracleConfig:
host: 'http://localhost:8080/'
30 changes: 1 addition & 29 deletions sample/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -44,7 +41,6 @@ pub struct OracleConfig {
#[derive(Debug)]
pub struct NetworkConfig {
pub peer_listening_port: u16,
pub announced_listen_addr: Option<SocketAddress>,
}

#[derive(Debug, Deserialize)]
Expand All @@ -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,
}

Expand All @@ -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,
})
}

Expand Down
84 changes: 84 additions & 0 deletions sample/tests/cli_tests.rs
Original file line number Diff line number Diff line change
@@ -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);
}
Loading