From 952d18cccbeccce62961c147b682f7461825ce1b Mon Sep 17 00:00:00 2001 From: Tibo-lg Date: Wed, 3 Apr 2024 14:31:33 +0900 Subject: [PATCH 1/2] Fix sample cli and bitcoin rpc provider --- .github/workflows/rust.yml | 38 +++++++++------ bitcoin-rpc-provider/src/lib.rs | 23 +++++---- docker-compose.yml | 1 + sample/tests/cli_tests.rs | 84 +++++++++++++++++++++++++++++++++ 4 files changed, 124 insertions(+), 22 deletions(-) create mode 100644 sample/tests/cli_tests.rs diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index b23d41ed..3d17132f 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -1,4 +1,9 @@ -on: [push, pull_request] +on: + push: + branches: + - master + - 'testci/**' + pull_request: name: Continuous integration @@ -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 @@ -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 @@ -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 @@ -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: @@ -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 @@ -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 diff --git a/bitcoin-rpc-provider/src/lib.rs b/bitcoin-rpc-provider/src/lib.rs index 43b2e217..9f44be59 100644 --- a/bitcoin-rpc-provider/src/lib.rs +++ b/bitcoin-rpc-provider/src/lib.rs @@ -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 @@ -274,7 +274,7 @@ impl Wallet for BitcoinCoreProvider { .unwrap() .call::>( "getrawchangeaddress", - &[Value::Null, opt_into_json(Some(AddressType::Bech32))?], + &[opt_into_json(Some(AddressType::Bech32))?], ) .map_err(rpc_err_to_manager_err)? .assume_checked()) @@ -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:?}" + ))), } - } } diff --git a/docker-compose.yml b/docker-compose.yml index 565fe9e5..a4301603 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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: | 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 5f405a72547aad34eb4d46239953b52b46b09dd0 Mon Sep 17 00:00:00 2001 From: bennyhodl Date: Wed, 3 Apr 2024 11:19:16 -0400 Subject: [PATCH 2/2] Postgres version to 12.2 --- testconfig/oracle/oracledb.dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testconfig/oracle/oracledb.dockerfile b/testconfig/oracle/oracledb.dockerfile index 99630ae5..08e49473 100644 --- a/testconfig/oracle/oracledb.dockerfile +++ b/testconfig/oracle/oracledb.dockerfile @@ -1,4 +1,4 @@ -FROM postgres:16.2 +FROM postgres:12.2 RUN mkdir certs COPY ./testconfig/oracle/certs/db/db.crt /certs/