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 952d18c
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 22 deletions.
38 changes: 24 additions & 14 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 All @@ -8,7 +13,7 @@ jobs:
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: install clippy
run: rustup component add clippy
- name: Run clippy
Expand All @@ -18,7 +23,7 @@ jobs:
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: install clippy
run: rustup component add clippy
- name: Run clippy dlc
Expand All @@ -32,7 +37,7 @@ jobs:
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Build
run: cargo build --verbose
- name: Test
Expand All @@ -44,13 +49,13 @@ jobs:
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
steps:
- uses: actions/cache@v2
- uses: actions/cache@v3
env:
cache-name: test-cache
with:
path: target/debug/deps
key: test-cache-${{ github.run_id }}-${{ github.run_number }}
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- id: set-matrix
run: cargo test --no-run && echo "::set-output name=matrix::$(scripts/get_test_list.sh execution manager channel_execution)"
integration_tests:
Expand All @@ -62,15 +67,15 @@ jobs:
matrix:
tests: ${{ fromJson(needs.integration_tests_prepare.outputs.matrix) }}
steps:
- uses: actions/checkout@v2
- uses: actions/cache@v2
- uses: actions/checkout@v4
- uses: actions/cache@v3
env:
cache-name: test-cache
with:
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 @@ -84,14 +89,19 @@ jobs:
runs-on: ubuntu-latest
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"
- uses: actions/checkout@v4
- 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
run: ./scripts/wait_for_electrs.sh
- name: Create wallets
run: docker exec bitcoin-node /scripts/create_wallets.sh
- name: Run test
run: cargo test -- --ignored sample
run: |
if ! cargo test -- --ignored sample; then
cat sample/dlc_sample_alice/.dlc/logs/logs.txt
cat sample/dlc_sample_bob/.dlc/logs/logs.txt
exit 1
fi
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
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ services:
volumes:
- bitcoind-data:/home/bitcoin/.bitcoin
- ./testconfig/config:/config
- ./scripts:/scripts
electrs:
image: ghcr.io/cryptogarageinc/electrs:v0.4.12-bitcoin
command: |
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);
}

0 comments on commit 952d18c

Please sign in to comment.