Skip to content

Commit

Permalink
Fix sample cli and bitcoin rpc provider
Browse files Browse the repository at this point in the history
  • Loading branch information
Tibo-lg committed Apr 3, 2024
1 parent e98f0c4 commit 983c10b
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 13 deletions.
13 changes: 8 additions & 5 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
on: [push, pull_request]
on:
push:
branches:
- master
- 'testci/**'
pull_request:

name: Continuous integration

Expand Down Expand Up @@ -70,7 +75,7 @@ jobs:
path: target/debug/deps
key: test-cache-${{ github.run_id }}-${{ github.run_number }}
- name: Start bitcoin node
run: docker-compose up -d
run: docker compose up -d
- name: Wait for container to run
run: ./scripts/wait_for_container.sh bitcoin-node
- name: Wait for electrs to be ready
Expand All @@ -85,10 +90,8 @@ jobs:
timeout-minutes: 30
steps:
- uses: actions/checkout@v2
- name: Set permission
run: docker-compose run oracle-db bash -c "chown postgres:postgres /certs/db.key && chgrp postgres /certs/db.key && chmod 600 /certs/db.key"
- name: Start environment
run: docker-compose --profile oracle up -d
run: docker compose --profile oracle up -d
- name: Wait for container to run
run: ./scripts/wait_for_container.sh oracle-server
- name: Wait for electrs to be ready
Expand Down
23 changes: 15 additions & 8 deletions bitcoin-rpc-provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,12 @@ impl ContractSignerProvider for BitcoinCoreProvider {
"getaddressesbylabel",
&[Value::String(keys_id.to_lower_hex_string())],
)
.map_err(rpc_err_to_manager_err)?;
.unwrap_or_default();

if let Some(address) = label_map.keys().next() {
// we should only have one address per keys_id
// if not something has gone wrong
assert_eq!(label_map.len(), 1);
// note: importing a private key seem to generate three different addresses, we thus
// check that we have exactly three addresses for a single `keys_id`.
assert_eq!(label_map.len(), 3);

let sk = self
.client
Expand Down Expand Up @@ -274,7 +274,7 @@ impl Wallet for BitcoinCoreProvider {
.unwrap()
.call::<Address<NetworkUnchecked>>(
"getrawchangeaddress",
&[Value::Null, opt_into_json(Some(AddressType::Bech32))?],
&[opt_into_json(Some(AddressType::Bech32))?],
)
.map_err(rpc_err_to_manager_err)?
.assume_checked())
Expand Down Expand Up @@ -391,11 +391,18 @@ impl Wallet for BitcoinCoreProvider {
}

fn unreserve_utxos(&self, outpoints: &[OutPoint]) -> Result<(), ManagerError> {
match self.client.lock().unwrap().unlock_unspent(outpoints).map_err(rpc_err_to_manager_err)? {
match self
.client
.lock()
.unwrap()
.unlock_unspent(outpoints)
.map_err(rpc_err_to_manager_err)?
{
true => Ok(()),
false => Err(ManagerError::StorageError(format!("Failed to unlock utxos: {outpoints:?}")))
false => Err(ManagerError::StorageError(format!(
"Failed to unlock utxos: {outpoints:?}"
))),
}

}
}

Expand Down
78 changes: 78 additions & 0 deletions sample/tests/cli_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
use std::process::Command;

use assert_cmd::cargo::cargo_bin;
use dlc_manager::contract::contract_input::ContractInput;
use rexpect::session::spawn_command;
use time::Duration;

#[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_seconds_f32() as u32;
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_char('>').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_char('>').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));

bob_cli.send_line("listoffers").unwrap();

bob_cli.exp_string("Offer").unwrap();
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();

std::thread::sleep(std::time::Duration::from_secs(5));

alice_cli.send_line("listcontracts").unwrap();
alice_cli.exp_string("Signed contract").unwrap();
alice_cli.exp_char('>').unwrap();

std::thread::sleep(std::time::Duration::from_secs(5));

bob_cli.send_line("listcontracts").unwrap();
bob_cli.exp_string("Signed contract").unwrap();
bob_cli.exp_char('>').unwrap();
}

0 comments on commit 983c10b

Please sign in to comment.