diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index ed5d4f4a..567f42cb 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -2,7 +2,7 @@ on: push: branches: - master - - 'testci/**' + - "testci/**" pull_request: name: Continuous integration @@ -26,7 +26,7 @@ jobs: - name: Install Rust toolchain uses: dtolnay/rust-toolchain@stable with: - components: rustfmt + components: rustfmt - name: Check format run: cargo fmt --all --check no-std: @@ -38,11 +38,11 @@ jobs: - name: install clippy run: rustup component add clippy - name: Run clippy dlc - run: cargo clippy --no-default-features --features no-std -p dlc -- -D warnings + run: cargo clippy --no-default-features --features no-std -p ddk-dlc -- -D warnings - name: Run clippy dlc-messages - run: cargo clippy --no-default-features --features no-std -p dlc-messages -- -D warnings + run: cargo clippy --no-default-features --features no-std -p ddk-messages -- -D warnings - name: Run clippy dlc-trie - run: cargo clippy --no-default-features --features no-std -p dlc-trie -- -D warnings + run: cargo clippy --no-default-features --features no-std -p ddk-trie -- -D warnings unit-tests: name: unit-tests runs-on: ubuntu-latest @@ -68,7 +68,7 @@ jobs: key: test-cache-${{ github.run_id }}-${{ github.run_number }} - 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)" + run: cargo test --no-run && echo "::set-output name=matrix::$(scripts/get_test_list.sh execution manager)" integration_tests: name: integration-tests needs: integration_tests_prepare diff --git a/Cargo.toml b/Cargo.toml index 4843100a..7d35f7a2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,10 +4,10 @@ members = [ "bitcoin-test-utils", "bitcoin-rpc-provider", "p2pd-oracle-client", - "dlc", - "dlc-messages", - "dlc-trie", - "dlc-manager", + "ddk-dlc", + "ddk-messages", + "ddk-trie", + "ddk-manager", "mocks", "sample", "simple-wallet", diff --git a/bitcoin-rpc-provider/Cargo.toml b/bitcoin-rpc-provider/Cargo.toml index 0da8a2de..4965c683 100644 --- a/bitcoin-rpc-provider/Cargo.toml +++ b/bitcoin-rpc-provider/Cargo.toml @@ -5,10 +5,11 @@ name = "bitcoin-rpc-provider" version = "0.1.0" [dependencies] +async-trait = "0.1.83" bitcoin = {version = "0.32.2"} bitcoincore-rpc = {version = "0.19.0"} bitcoincore-rpc-json = {version = "0.19.0"} -dlc-manager = {path = "../dlc-manager"} +ddk-manager = {path = "../ddk-manager"} hex = { package = "hex-conservative", version = "0.1" } lightning = { version = "0.0.125" } log = "0.4.14" diff --git a/bitcoin-rpc-provider/src/lib.rs b/bitcoin-rpc-provider/src/lib.rs index 7fa1c687..782aca54 100644 --- a/bitcoin-rpc-provider/src/lib.rs +++ b/bitcoin-rpc-provider/src/lib.rs @@ -17,8 +17,8 @@ use bitcoincore_rpc::jsonrpc::serde_json; use bitcoincore_rpc::jsonrpc::serde_json::Value; use bitcoincore_rpc::{json, Auth, Client, RpcApi}; use bitcoincore_rpc_json::AddressType; -use dlc_manager::error::Error as ManagerError; -use dlc_manager::{Blockchain, ContractSignerProvider, SimpleSigner, Utxo, Wallet}; +use ddk_manager::error::Error as ManagerError; +use ddk_manager::{Blockchain, ContractSignerProvider, SimpleSigner, Utxo, Wallet}; use hex::DisplayHex; use json::EstimateMode; use lightning::chain::chaininterface::{ConfirmationTarget, FeeEstimator}; @@ -396,8 +396,9 @@ impl Wallet for BitcoinCoreProvider { } } +#[async_trait::async_trait] impl Blockchain for BitcoinCoreProvider { - fn send_transaction(&self, transaction: &Transaction) -> Result<(), ManagerError> { + async fn send_transaction(&self, transaction: &Transaction) -> Result<(), ManagerError> { self.client .lock() .unwrap() @@ -418,7 +419,7 @@ impl Blockchain for BitcoinCoreProvider { Ok(network) } - fn get_blockchain_height(&self) -> Result { + async fn get_blockchain_height(&self) -> Result { self.client .lock() .unwrap() @@ -426,7 +427,7 @@ impl Blockchain for BitcoinCoreProvider { .map_err(rpc_err_to_manager_err) } - fn get_block_at_height(&self, height: u64) -> Result { + async fn get_block_at_height(&self, height: u64) -> Result { let client = self.client.lock().unwrap(); let hash = client .get_block_hash(height) @@ -434,7 +435,7 @@ impl Blockchain for BitcoinCoreProvider { client.get_block(&hash).map_err(rpc_err_to_manager_err) } - fn get_transaction(&self, tx_id: &Txid) -> Result { + async fn get_transaction(&self, tx_id: &Txid) -> Result { let tx_info = self .client .lock() @@ -446,7 +447,7 @@ impl Blockchain for BitcoinCoreProvider { Ok(tx) } - fn get_transaction_confirmations(&self, tx_id: &Txid) -> Result { + async fn get_transaction_confirmations(&self, tx_id: &Txid) -> Result { let tx_info_res = self.client.lock().unwrap().get_transaction(tx_id, None); match tx_info_res { Ok(tx_info) => Ok(tx_info.info.confirmations as u32), diff --git a/dlc/CHANGELOG.md b/ddk-dlc/CHANGELOG.md similarity index 100% rename from dlc/CHANGELOG.md rename to ddk-dlc/CHANGELOG.md diff --git a/dlc/Cargo.toml b/ddk-dlc/Cargo.toml similarity index 81% rename from dlc/Cargo.toml rename to ddk-dlc/Cargo.toml index 6fad3943..43a079ff 100644 --- a/dlc/Cargo.toml +++ b/ddk-dlc/Cargo.toml @@ -1,10 +1,10 @@ [package] -authors = ["Crypto Garage"] +authors = ["Crypto Garage", "benny b "] description = "Creation, signing and verification of Discreet Log Contracts (DLC) transactions." -homepage = "https://github.com/p2pderivatives/rust-dlc" +homepage = "https://github.com/bennyhodl/rust-dlc" license-file = "../LICENSE" -name = "dlc" -repository = "https://github.com/p2pderivatives/rust-dlc/tree/master/dlc" +name = "ddk-dlc" +repository = "https://github.com/bennyhodl/rust-dlc/tree/master/dlc" version = "0.7.0" [dependencies] diff --git a/dlc/Readme.md b/ddk-dlc/Readme.md similarity index 100% rename from dlc/Readme.md rename to ddk-dlc/Readme.md diff --git a/dlc/benches/Readme.md b/ddk-dlc/benches/Readme.md similarity index 100% rename from dlc/benches/Readme.md rename to ddk-dlc/benches/Readme.md diff --git a/dlc/benches/benchmarks.rs b/ddk-dlc/benches/benchmarks.rs similarity index 99% rename from dlc/benches/benchmarks.rs rename to ddk-dlc/benches/benchmarks.rs index f0f69adc..04e61784 100644 --- a/dlc/benches/benchmarks.rs +++ b/ddk-dlc/benches/benchmarks.rs @@ -2,7 +2,7 @@ extern crate bitcoin; extern crate bitcoin_test_utils; -extern crate dlc; +extern crate ddk_dlc; extern crate rayon; extern crate secp256k1_zkp; #[cfg(all(test, feature = "unstable"))] @@ -13,7 +13,7 @@ mod benches { use bitcoin::{ScriptBuf, Transaction}; use bitcoin_test_utils::tx_from_string; - use dlc::*; + use ddk_dlc::*; use rayon::prelude::*; use secp256k1_zkp::{ global::SECP256K1, rand::thread_rng, rand::RngCore, Keypair, Message, PublicKey, SecretKey, diff --git a/dlc/src/channel/mod.rs b/ddk-dlc/src/channel/mod.rs similarity index 100% rename from dlc/src/channel/mod.rs rename to ddk-dlc/src/channel/mod.rs diff --git a/dlc/src/lib.rs b/ddk-dlc/src/lib.rs similarity index 100% rename from dlc/src/lib.rs rename to ddk-dlc/src/lib.rs diff --git a/dlc/src/secp_utils.rs b/ddk-dlc/src/secp_utils.rs similarity index 100% rename from dlc/src/secp_utils.rs rename to ddk-dlc/src/secp_utils.rs diff --git a/dlc/src/util.rs b/ddk-dlc/src/util.rs similarity index 100% rename from dlc/src/util.rs rename to ddk-dlc/src/util.rs diff --git a/dlc-manager/CHANGELOG.md b/ddk-manager/CHANGELOG.md similarity index 100% rename from dlc-manager/CHANGELOG.md rename to ddk-manager/CHANGELOG.md diff --git a/dlc-manager/Cargo.toml b/ddk-manager/Cargo.toml similarity index 56% rename from dlc-manager/Cargo.toml rename to ddk-manager/Cargo.toml index 15bd6b7c..0e1eff20 100644 --- a/dlc-manager/Cargo.toml +++ b/ddk-manager/Cargo.toml @@ -1,26 +1,27 @@ [package] -authors = ["Crypto Garage"] +authors = ["Crypto Garage", "benny b "] description = "Creation and handling of Discrete Log Contracts (DLC)." -edition = "2018" -homepage = "https://github.com/p2pderivatives/rust-dlc" +edition = "2021" +homepage = "https://github.com/bennyhodl/rust-dlc" license-file = "../LICENSE" -name = "dlc-manager" -repository = "https://github.com/p2pderivatives/rust-dlc/tree/master/dlc-manager" +name = "ddk-manager" +repository = "https://github.com/bennyhodl/rust-dlc/tree/master/dlc-manager" version = "0.7.0" [features] default = ["std"] -std = ["dlc/std", "dlc-messages/std", "dlc-trie/std", "bitcoin/std", "lightning/std"] +std = ["ddk-dlc/std", "ddk-messages/std", "ddk-trie/std", "bitcoin/std", "lightning/std"] fuzztarget = ["rand_chacha"] -parallel = ["dlc-trie/parallel"] -use-serde = ["serde", "dlc/use-serde", "dlc-messages/use-serde", "dlc-trie/use-serde"] +parallel = ["ddk-trie/parallel"] +use-serde = ["serde", "ddk-dlc/use-serde", "ddk-messages/use-serde", "ddk-trie/use-serde"] [dependencies] async-trait = "0.1.50" bitcoin = { version = "0.32.2", default-features = false } -dlc = { version = "0.7.0", default-features = false, path = "../dlc" } -dlc-messages = { version = "0.7.0", default-features = false, path = "../dlc-messages" } -dlc-trie = { version = "0.7.0", default-features = false, path = "../dlc-trie" } +ddk-dlc = { version = "0.7.0", default-features = false, path = "../ddk-dlc" } +ddk-messages = { version = "0.7.0", default-features = false, path = "../ddk-messages" } +ddk-trie = { version = "0.7.0", default-features = false, path = "../ddk-trie" } +futures = "0.3.31" hex = { package = "hex-conservative", version = "0.1" } lightning = { version = "0.0.125", default-features = false, features = ["grind_signatures"] } log = "0.4.14" @@ -34,8 +35,8 @@ bitcoin-test-utils = {path = "../bitcoin-test-utils"} bitcoincore-rpc = {version = "0.19"} bitcoincore-rpc-json = {version = "0.19"} criterion = "0.4.0" -dlc-manager = { path = ".", default-features = false, features = ["use-serde"] } -dlc-messages = { path = "../dlc-messages", default-features = false, features = ["serde"] } +ddk-manager = { path = ".", default-features = false, features = ["use-serde"] } +ddk-messages = { path = "../ddk-messages", default-features = false, features = ["serde"] } electrs-blockchain-provider = {path = "../electrs-blockchain-provider"} env_logger = "0.9.1" mocks = {path = "../mocks"} @@ -43,6 +44,7 @@ secp256k1-zkp = {version = "0.11.0", features = ["hashes", "rand", "rand-std", " serde = "1.0" serde_json = "1.0" simple-wallet = {path = "../simple-wallet"} +tokio = { version = "1.41.1", features = ["macros", "rt-multi-thread", "test-util", "fs"] } [[bench]] harness = false diff --git a/dlc-manager/Readme.md b/ddk-manager/Readme.md similarity index 100% rename from dlc-manager/Readme.md rename to ddk-manager/Readme.md diff --git a/dlc-manager/benches/Readme.md b/ddk-manager/benches/Readme.md similarity index 100% rename from dlc-manager/benches/Readme.md rename to ddk-manager/benches/Readme.md diff --git a/dlc-manager/benches/benchmarks.rs b/ddk-manager/benches/benchmarks.rs similarity index 91% rename from dlc-manager/benches/benchmarks.rs rename to ddk-manager/benches/benchmarks.rs index a8062f14..dacd6ea5 100644 --- a/dlc-manager/benches/benchmarks.rs +++ b/ddk-manager/benches/benchmarks.rs @@ -3,25 +3,25 @@ use bitcoin::OutPoint; use bitcoin::ScriptBuf; use bitcoin::WPubkeyHash; use criterion::{black_box, criterion_group, criterion_main, Criterion}; -use dlc::create_dlc_transactions; -use dlc::DlcTransactions; -use dlc::PartyParams; -use dlc::Payout; -use dlc::TxInputInfo; -use dlc_manager::contract::contract_info::ContractInfo; -use dlc_manager::contract::numerical_descriptor::DifferenceParams; -use dlc_manager::contract::numerical_descriptor::NumericalDescriptor; -use dlc_manager::contract::ContractDescriptor; -use dlc_manager::payout_curve::PayoutFunction; -use dlc_manager::payout_curve::PayoutFunctionPiece; -use dlc_manager::payout_curve::PayoutPoint; -use dlc_manager::payout_curve::PolynomialPayoutCurvePiece; -use dlc_manager::payout_curve::RoundingInterval; -use dlc_manager::payout_curve::RoundingIntervals; -use dlc_messages::oracle_msgs::DigitDecompositionEventDescriptor; -use dlc_messages::oracle_msgs::EventDescriptor; -use dlc_messages::oracle_msgs::OracleAnnouncement; -use dlc_messages::oracle_msgs::OracleEvent; +use ddk_dlc::create_dlc_transactions; +use ddk_dlc::DlcTransactions; +use ddk_dlc::PartyParams; +use ddk_dlc::Payout; +use ddk_dlc::TxInputInfo; +use ddk_manager::contract::contract_info::ContractInfo; +use ddk_manager::contract::numerical_descriptor::DifferenceParams; +use ddk_manager::contract::numerical_descriptor::NumericalDescriptor; +use ddk_manager::contract::ContractDescriptor; +use ddk_manager::payout_curve::PayoutFunction; +use ddk_manager::payout_curve::PayoutFunctionPiece; +use ddk_manager::payout_curve::PayoutPoint; +use ddk_manager::payout_curve::PolynomialPayoutCurvePiece; +use ddk_manager::payout_curve::RoundingInterval; +use ddk_manager::payout_curve::RoundingIntervals; +use ddk_messages::oracle_msgs::DigitDecompositionEventDescriptor; +use ddk_messages::oracle_msgs::EventDescriptor; +use ddk_messages::oracle_msgs::OracleAnnouncement; +use ddk_messages::oracle_msgs::OracleEvent; use secp256k1_zkp::{ global::SECP256K1, rand::thread_rng, schnorr::Signature, Keypair, SecretKey, XOnlyPublicKey, }; @@ -120,7 +120,7 @@ fn create_contract_descriptor() -> ContractDescriptor { rounding_mod: ROUNDING_MOD, }], }, - oracle_numeric_infos: dlc_trie::OracleNumericInfo { + oracle_numeric_infos: ddk_trie::OracleNumericInfo { base: BASE as usize, nb_digits: std::iter::repeat(NB_DIGITS) .take(NB_ORACLES) diff --git a/dlc-manager/src/chain_monitor.rs b/ddk-manager/src/chain_monitor.rs similarity index 99% rename from dlc-manager/src/chain_monitor.rs rename to ddk-manager/src/chain_monitor.rs index 5344da2c..dc50c74f 100644 --- a/dlc-manager/src/chain_monitor.rs +++ b/ddk-manager/src/chain_monitor.rs @@ -3,7 +3,7 @@ use std::collections::HashMap; use bitcoin::{Block, OutPoint, Transaction, Txid}; -use dlc_messages::ser_impls::{ +use ddk_messages::ser_impls::{ read_ecdsa_adaptor_signature, read_hash_map, write_ecdsa_adaptor_signature, write_hash_map, }; use lightning::ln::msgs::DecodeError; diff --git a/dlc-manager/src/channel/accepted_channel.rs b/ddk-manager/src/channel/accepted_channel.rs similarity index 98% rename from dlc-manager/src/channel/accepted_channel.rs rename to ddk-manager/src/channel/accepted_channel.rs index 24a43f56..c40dd9af 100644 --- a/dlc-manager/src/channel/accepted_channel.rs +++ b/ddk-manager/src/channel/accepted_channel.rs @@ -1,7 +1,7 @@ //! # Structure and methods for channels that have been accepted. use bitcoin::{ScriptBuf, Transaction}; -use dlc_messages::channel::AcceptChannel; +use ddk_messages::channel::AcceptChannel; use secp256k1_zkp::{EcdsaAdaptorSignature, PublicKey}; use crate::{contract::accepted_contract::AcceptedContract, ChannelId, ContractId}; diff --git a/dlc-manager/src/channel/mod.rs b/ddk-manager/src/channel/mod.rs similarity index 94% rename from dlc-manager/src/channel/mod.rs rename to ddk-manager/src/channel/mod.rs index 93b1c938..ad007d08 100644 --- a/dlc-manager/src/channel/mod.rs +++ b/ddk-manager/src/channel/mod.rs @@ -1,7 +1,7 @@ //! # Module containing structures and methods for working with DLC channels. use bitcoin::{hashes::Hash, Transaction, Txid}; -use dlc_messages::channel::{AcceptChannel, SignChannel}; +use ddk_messages::channel::{AcceptChannel, SignChannel}; use secp256k1_zkp::PublicKey; use crate::{ChannelId, ContractId}; @@ -29,10 +29,10 @@ pub enum Channel { /// A channel whose fund outputs have been signed by the offer party. Signed(SignedChannel), /// A channel that failed when validating an - /// [`dlc_messages::channel::AcceptChannel`] message. + /// [`ddk_messages::channel::AcceptChannel`] message. FailedAccept(FailedAccept), /// A channel that failed when validating an - /// [`dlc_messages::channel::SignChannel`] message. + /// [`ddk_messages::channel::SignChannel`] message. FailedSign(FailedSign), /// A [`OfferedChannel`] that got rejected by the counterparty. Cancelled(OfferedChannel), @@ -95,7 +95,7 @@ impl Channel { } /// A channel that failed when validating an -/// [`dlc_messages::channel::AcceptChannel`] message. +/// [`ddk_messages::channel::AcceptChannel`] message. #[derive(Clone)] pub struct FailedAccept { /// The [`secp256k1_zkp::PublicKey`] of the counter party. @@ -103,14 +103,14 @@ pub struct FailedAccept { /// The temporary [`crate::ChannelId`] of the channel. pub temporary_channel_id: ChannelId, /// An message describing the error encountered while validating the - /// [`dlc_messages::channel::AcceptChannel`] message. + /// [`ddk_messages::channel::AcceptChannel`] message. pub error_message: String, - /// The [`dlc_messages::channel::AcceptChannel`] that was received. + /// The [`ddk_messages::channel::AcceptChannel`] that was received. pub accept_message: AcceptChannel, } /// A channel that failed when validating an -/// [`dlc_messages::channel::SignChannel`] message. +/// [`ddk_messages::channel::SignChannel`] message. #[derive(Clone)] pub struct FailedSign { /// The [`secp256k1_zkp::PublicKey`] of the counter party. @@ -118,9 +118,9 @@ pub struct FailedSign { /// The [`crate::ChannelId`] of the channel. pub channel_id: ChannelId, /// An message describing the error encountered while validating the - /// [`dlc_messages::channel::SignChannel`] message. + /// [`ddk_messages::channel::SignChannel`] message. pub error_message: String, - /// The [`dlc_messages::channel::SignChannel`] that was received. + /// The [`ddk_messages::channel::SignChannel`] that was received. pub sign_message: SignChannel, } diff --git a/dlc-manager/src/channel/offered_channel.rs b/ddk-manager/src/channel/offered_channel.rs similarity index 95% rename from dlc-manager/src/channel/offered_channel.rs rename to ddk-manager/src/channel/offered_channel.rs index 6e082b1f..82dab988 100644 --- a/dlc-manager/src/channel/offered_channel.rs +++ b/ddk-manager/src/channel/offered_channel.rs @@ -1,9 +1,9 @@ //! # A channel is offered when an offer was made or received. This module contains //! the model for it and method for working with it. -use dlc::PartyParams; -use dlc_messages::channel::OfferChannel; -// use dlc_messages::channel::OfferChannel; +use ddk_dlc::PartyParams; +use ddk_messages::channel::OfferChannel; +// use ddk_messages::channel::OfferChannel; use secp256k1_zkp::PublicKey; use crate::{ @@ -19,7 +19,7 @@ use super::party_points::PartyBasePoints; derive(serde::Serialize, serde::Deserialize), serde(rename_all = "camelCase") )] -/// A DLC channel for which an [`dlc_messages::channel::OfferChannel`] message +/// A DLC channel for which an [`ddk_messages::channel::OfferChannel`] message /// was sent or received. pub struct OfferedChannel { /// The temporary [`crate::ContractId`] of the contract that was offered for @@ -73,7 +73,7 @@ impl OfferedChannel { } /// Creates an [`OfferedChannel`] and [`crate::contract::offered_contract::OfferedContract`] - /// from an [`dlc_messages::channel::OfferChannel`] message. Fails if the + /// from an [`ddk_messages::channel::OfferChannel`] message. Fails if the /// transactions provided for funding cannot be decoded or the UTXO information /// are invalid, or if the contract information is invalid. pub fn from_offer_channel( diff --git a/dlc-manager/src/channel/party_points.rs b/ddk-manager/src/channel/party_points.rs similarity index 99% rename from dlc-manager/src/channel/party_points.rs rename to ddk-manager/src/channel/party_points.rs index 88add5a2..73c794f0 100644 --- a/dlc-manager/src/channel/party_points.rs +++ b/ddk-manager/src/channel/party_points.rs @@ -4,7 +4,7 @@ use super::utils::{derive_public_key, derive_public_revocation_key}; use bitcoin::PublicKey as BitcoinPublicKey; -use dlc::channel::RevokeParams; +use ddk_dlc::channel::RevokeParams; use secp256k1_zkp::{All, PublicKey, Secp256k1, Signing, Verification}; /// Base points used by a party of a DLC channel to derive public and private diff --git a/dlc-manager/src/channel/ser.rs b/ddk-manager/src/channel/ser.rs similarity index 95% rename from dlc-manager/src/channel/ser.rs rename to ddk-manager/src/channel/ser.rs index 62d79c2c..ff14249b 100644 --- a/dlc-manager/src/channel/ser.rs +++ b/ddk-manager/src/channel/ser.rs @@ -5,7 +5,7 @@ use super::party_points::PartyBasePoints; use super::signed_channel::{SignedChannel, SignedChannelState}; use super::{ClosedChannel, ClosedPunishedChannel, ClosingChannel, FailedAccept, FailedSign}; -use dlc_messages::ser_impls::{ +use ddk_messages::ser_impls::{ read_ecdsa_adaptor_signature, read_string, write_ecdsa_adaptor_signature, write_string, }; use lightning::ln::msgs::DecodeError; @@ -33,11 +33,11 @@ impl_dlc_writeable!(SignedChannel, { (temporary_channel_id, writeable), (fund_output_index, usize), (own_points, writeable), - (own_params, { cb_writeable, dlc_messages::ser_impls::party_params::write, dlc_messages::ser_impls::party_params::read }), + (own_params, { cb_writeable, ddk_messages::ser_impls::party_params::write, ddk_messages::ser_impls::party_params::read }), (own_per_update_point, writeable), (counter_points, writeable), (counter_per_update_point, writeable), - (counter_params, { cb_writeable, dlc_messages::ser_impls::party_params::write, dlc_messages::ser_impls::party_params::read }), + (counter_params, { cb_writeable, ddk_messages::ser_impls::party_params::write, ddk_messages::ser_impls::party_params::read }), (state, writeable), (update_idx, writeable), (fund_tx, writeable), diff --git a/dlc-manager/src/channel/signed_channel.rs b/ddk-manager/src/channel/signed_channel.rs similarity index 99% rename from dlc-manager/src/channel/signed_channel.rs rename to ddk-manager/src/channel/signed_channel.rs index 82f8b271..ea332392 100644 --- a/dlc-manager/src/channel/signed_channel.rs +++ b/ddk-manager/src/channel/signed_channel.rs @@ -3,7 +3,7 @@ //! the possible states in which it can be as well as methods to work with it. use bitcoin::{ScriptBuf, Transaction}; -use dlc::PartyParams; +use ddk_dlc::PartyParams; use lightning::ln::chan_utils::CounterpartyCommitmentSecrets; use secp256k1_zkp::{ecdsa::Signature, EcdsaAdaptorSignature, PublicKey}; diff --git a/dlc-manager/src/channel/utils.rs b/ddk-manager/src/channel/utils.rs similarity index 100% rename from dlc-manager/src/channel/utils.rs rename to ddk-manager/src/channel/utils.rs diff --git a/dlc-manager/src/channel_updater.rs b/ddk-manager/src/channel_updater.rs similarity index 98% rename from dlc-manager/src/channel_updater.rs rename to ddk-manager/src/channel_updater.rs index e55d475c..927adc27 100644 --- a/dlc-manager/src/channel_updater.rs +++ b/ddk-manager/src/channel_updater.rs @@ -26,11 +26,11 @@ use crate::{ Wallet, }; use bitcoin::{OutPoint, Script, ScriptBuf, Sequence, Transaction, TxIn, Witness}; -use dlc::{ +use ddk_dlc::{ channel::{get_tx_adaptor_signature, verify_tx_adaptor_signature, DlcChannelTransactions}, PartyParams, }; -use dlc_messages::{ +use ddk_messages::{ channel::{ AcceptChannel, CollaborativeCloseOffer, Reject, RenewAccept, RenewConfirm, RenewFinalize, RenewOffer, RenewRevoke, SettleAccept, SettleConfirm, SettleFinalize, SettleOffer, @@ -70,7 +70,7 @@ pub(crate) use get_signed_channel_state; /// Creates an [`OfferedChannel`] and an associated [`OfferedContract`] using /// the given parameter. -pub fn offer_channel( +pub async fn offer_channel( secp: &Secp256k1, contract: &ContractInput, counter_party: &PublicKey, @@ -99,7 +99,8 @@ where wallet, &signer, blockchain, - )?; + ) + .await?; let party_points = crate::utils::get_party_base_points(secp, signer_provider)?; let offered_contract = OfferedContract::new( @@ -142,7 +143,7 @@ where /// Move the given [`OfferedChannel`] and [`OfferedContract`] to an [`AcceptedChannel`] /// and [`AcceptedContract`], returning them as well as the [`AcceptChannel`] /// message to be sent to the counter party. -pub fn accept_channel_offer( +pub async fn accept_channel_offer( secp: &Secp256k1, offered_channel: &OfferedChannel, offered_contract: &OfferedContract, @@ -167,7 +168,8 @@ where wallet, &signer, blockchain, - )?; + ) + .await?; let per_update_seed = signer_provider.get_new_secret_key()?; @@ -200,7 +202,7 @@ where buffer_transaction, buffer_script_pubkey, dlc_transactions, - } = dlc::channel::create_channel_transactions( + } = ddk_dlc::channel::create_channel_transactions( &offered_contract.offer_params, &accept_params, &offer_revoke_params, @@ -334,7 +336,7 @@ where buffer_transaction, dlc_transactions, buffer_script_pubkey, - } = dlc::channel::create_channel_transactions( + } = ddk_dlc::channel::create_channel_transactions( &offered_contract.offer_params, &accept_params, &offer_revoke_params, @@ -1291,7 +1293,7 @@ where buffer_transaction, buffer_script_pubkey, dlc_transactions, - } = dlc::channel::create_renewal_channel_transactions( + } = ddk_dlc::channel::create_renewal_channel_transactions( &offered_contract.offer_params, &signed_channel.own_params, &offer_revoke_params, @@ -1393,7 +1395,7 @@ where buffer_transaction, dlc_transactions, buffer_script_pubkey, - } = dlc::channel::create_renewal_channel_transactions( + } = ddk_dlc::channel::create_renewal_channel_transactions( &offered_contract.offer_params, &signed_channel.counter_params, &offer_revoke_params, @@ -1797,7 +1799,7 @@ where let offer_payout = total_collateral - counter_payout; let fund_output_value = signed_channel.fund_tx.output[signed_channel.fund_output_index].value; - let close_tx = dlc::channel::create_collaborative_close_transaction( + let close_tx = ddk_dlc::channel::create_collaborative_close_transaction( &signed_channel.own_params, offer_payout, &signed_channel.counter_params, @@ -1814,7 +1816,7 @@ where .ok_or(Error::InvalidState("No keys_id available".to_string()))?; let contract_signer = signer_provider.derive_contract_signer(keys_id)?; - let close_signature = dlc::util::get_raw_sig_for_tx_input( + let close_signature = ddk_dlc::util::get_raw_sig_for_tx_input( secp, &close_tx, 0, @@ -1872,7 +1874,7 @@ where let offer_payout = total_collateral - close_offer.counter_payout; let fund_output_value = signed_channel.fund_tx.output[signed_channel.fund_output_index].value; - let close_tx = dlc::channel::create_collaborative_close_transaction( + let close_tx = ddk_dlc::channel::create_collaborative_close_transaction( &signed_channel.counter_params, offer_payout, &signed_channel.own_params, @@ -1923,7 +1925,7 @@ where let mut close_tx = close_tx.clone(); - dlc::util::sign_multi_sig_input( + ddk_dlc::util::sign_multi_sig_input( secp, &mut close_tx, offer_signature, @@ -1989,7 +1991,7 @@ fn get_settle_tx_and_adaptor_sig( accept_per_update_point, ); - let settle_tx = dlc::channel::create_settle_transaction( + let settle_tx = ddk_dlc::channel::create_settle_transaction( &fund_tx_in, &offer_revoke_params, &accept_revoke_params, @@ -2019,7 +2021,7 @@ fn get_settle_tx_and_adaptor_sig( offer_revoke_params.publish_pk.inner }; - let settle_adaptor_signature = dlc::channel::get_tx_adaptor_signature( + let settle_adaptor_signature = ddk_dlc::channel::get_tx_adaptor_signature( secp, &settle_tx, fund_tx.output[fund_vout].value.to_sat(), @@ -2088,7 +2090,7 @@ where let buffer_input_sk = signer.get_secret_key_for_pubkey(&signed_channel.own_params.fund_pubkey)?; - dlc::util::sign_multi_sig_input( + ddk_dlc::util::sign_multi_sig_input( secp, &mut buffer_transaction, &counter_buffer_signature, @@ -2200,7 +2202,7 @@ where let base_secret = signer.get_secret_key_for_pubkey(own_basepoint)?; let own_sk = derive_private_key(secp, own_per_update_point, &base_secret); - dlc::channel::sign_cet( + ddk_dlc::channel::sign_cet( secp, &mut cet, buffer_transaction.output[0].value.to_sat(), @@ -2257,7 +2259,7 @@ where let fund_sk = signer.get_secret_key_for_pubkey(&signed_channel.own_params.fund_pubkey)?; - dlc::util::sign_multi_sig_input( + ddk_dlc::util::sign_multi_sig_input( secp, &mut settle_tx, &counter_settle_signature, diff --git a/dlc-manager/src/contract/accepted_contract.rs b/ddk-manager/src/contract/accepted_contract.rs similarity index 98% rename from dlc-manager/src/contract/accepted_contract.rs rename to ddk-manager/src/contract/accepted_contract.rs index 6e507170..5279136b 100644 --- a/dlc-manager/src/contract/accepted_contract.rs +++ b/ddk-manager/src/contract/accepted_contract.rs @@ -3,8 +3,8 @@ use super::offered_contract::OfferedContract; use super::AdaptorInfo; use bitcoin::Transaction; -use dlc::{DlcTransactions, PartyParams}; -use dlc_messages::{AcceptDlc, FundingInput}; +use ddk_dlc::{DlcTransactions, PartyParams}; +use ddk_messages::{AcceptDlc, FundingInput}; use secp256k1_zkp::ecdsa::Signature; use secp256k1_zkp::EcdsaAdaptorSignature; diff --git a/dlc-manager/src/contract/contract_info.rs b/ddk-manager/src/contract/contract_info.rs similarity index 98% rename from dlc-manager/src/contract/contract_info.rs rename to ddk-manager/src/contract/contract_info.rs index 8c1ece79..827750bf 100644 --- a/dlc-manager/src/contract/contract_info.rs +++ b/ddk-manager/src/contract/contract_info.rs @@ -6,9 +6,9 @@ use crate::error::Error; use crate::ContractSigner; use bitcoin::hashes::Hash; use bitcoin::{Script, Transaction}; -use dlc::{OracleInfo, Payout}; -use dlc_messages::oracle_msgs::{EventDescriptor, OracleAnnouncement}; -use dlc_trie::{DlcTrie, RangeInfo}; +use ddk_dlc::{OracleInfo, Payout}; +use ddk_messages::oracle_msgs::{EventDescriptor, OracleAnnouncement}; +use ddk_trie::{DlcTrie, RangeInfo}; use secp256k1_zkp::{ All, EcdsaAdaptorSignature, Message, PublicKey, Secp256k1, SecretKey, Verification, }; @@ -298,7 +298,7 @@ impl ContractInfo { bitcoin::hashes::sha256::Hash::hash(j.to_string().as_bytes()) .to_byte_array(); let msg = Message::from_digest(hash); - let sig_point = dlc::secp_utils::schnorrsig_compute_sig_point( + let sig_point = ddk_dlc::secp_utils::schnorrsig_compute_sig_point( secp, pubkey, nonce, &msg, )?; points.push(sig_point); diff --git a/dlc-manager/src/contract/contract_input.rs b/ddk-manager/src/contract/contract_input.rs similarity index 98% rename from dlc-manager/src/contract/contract_input.rs rename to ddk-manager/src/contract/contract_input.rs index ad5ed1af..821c0f6f 100644 --- a/dlc-manager/src/contract/contract_input.rs +++ b/ddk-manager/src/contract/contract_input.rs @@ -96,14 +96,14 @@ impl ContractInput { contract_info.oracles.validate()?; } - dlc::util::validate_fee_rate(self.fee_rate) + ddk_dlc::util::validate_fee_rate(self.fee_rate) .map_err(|_| Error::InvalidParameters("Fee rate too high.".to_string())) } } #[cfg(test)] mod tests { - use dlc::{EnumerationPayout, Payout}; + use ddk_dlc::{EnumerationPayout, Payout}; use secp256k1_zkp::{Keypair, SecretKey, SECP256K1}; use crate::contract::enum_descriptor::EnumDescriptor; diff --git a/dlc-manager/src/contract/enum_descriptor.rs b/ddk-manager/src/contract/enum_descriptor.rs similarity index 92% rename from dlc-manager/src/contract/enum_descriptor.rs rename to ddk-manager/src/contract/enum_descriptor.rs index bb5c4bff..5cfda297 100644 --- a/dlc-manager/src/contract/enum_descriptor.rs +++ b/ddk-manager/src/contract/enum_descriptor.rs @@ -6,10 +6,10 @@ use super::AdaptorInfo; use crate::error::Error; use bitcoin::hashes::Hash; use bitcoin::{Script, Transaction}; -use dlc::OracleInfo; -use dlc::{EnumerationPayout, Payout}; -use dlc_messages::oracle_msgs::EnumEventDescriptor; -use dlc_trie::{combination_iterator::CombinationIterator, RangeInfo}; +use ddk_dlc::OracleInfo; +use ddk_dlc::{EnumerationPayout, Payout}; +use ddk_messages::oracle_msgs::EnumEventDescriptor; +use ddk_trie::{combination_iterator::CombinationIterator, RangeInfo}; use secp256k1_zkp::{ All, EcdsaAdaptorSignature, Message, PublicKey, Secp256k1, SecretKey, Verification, }; @@ -121,13 +121,13 @@ impl EnumDescriptor { cets: &[Transaction], adaptor_sigs: &[EcdsaAdaptorSignature], adaptor_sig_start: usize, - ) -> Result { + ) -> Result { let mut adaptor_sig_index = adaptor_sig_start; let mut callback = - |adaptor_point: &PublicKey, cet_index: usize| -> Result<(), dlc::Error> { + |adaptor_point: &PublicKey, cet_index: usize| -> Result<(), ddk_dlc::Error> { let sig = adaptor_sigs[adaptor_sig_index]; adaptor_sig_index += 1; - dlc::verify_cet_adaptor_sig_from_point( + ddk_dlc::verify_cet_adaptor_sig_from_point( secp, &sig, &cets[cet_index], @@ -156,7 +156,7 @@ impl EnumDescriptor { cets: &[Transaction], adaptor_sigs: &[EcdsaAdaptorSignature], adaptor_sig_start: usize, - ) -> Result<(AdaptorInfo, usize), dlc::Error> { + ) -> Result<(AdaptorInfo, usize), ddk_dlc::Error> { let adaptor_sig_index = self.verify_adaptor_info( secp, oracle_infos, @@ -209,8 +209,8 @@ impl EnumDescriptor { ) -> Result, Error> { let mut adaptor_sigs = Vec::new(); let mut callback = - |adaptor_point: &PublicKey, cet_index: usize| -> Result<(), dlc::Error> { - let sig = dlc::create_cet_adaptor_sig_from_point( + |adaptor_point: &PublicKey, cet_index: usize| -> Result<(), ddk_dlc::Error> { + let sig = ddk_dlc::create_cet_adaptor_sig_from_point( secp, &cets[cet_index], adaptor_point, @@ -233,9 +233,9 @@ impl EnumDescriptor { oracle_infos: &[OracleInfo], threshold: usize, callback: &mut F, - ) -> Result<(), dlc::Error> + ) -> Result<(), ddk_dlc::Error> where - F: FnMut(&PublicKey, usize) -> Result<(), dlc::Error>, + F: FnMut(&PublicKey, usize) -> Result<(), ddk_dlc::Error>, { let messages: Vec>> = self .outcome_payouts @@ -263,7 +263,7 @@ impl EnumDescriptor { } }) .collect(); - let adaptor_point = dlc::get_adaptor_point_from_oracle_info( + let adaptor_point = ddk_dlc::get_adaptor_point_from_oracle_info( secp, &cur_oracle_infos, outcome_messages, diff --git a/dlc-manager/src/contract/mod.rs b/ddk-manager/src/contract/mod.rs similarity index 98% rename from dlc-manager/src/contract/mod.rs rename to ddk-manager/src/contract/mod.rs index 155cee03..9f4a35ca 100644 --- a/dlc-manager/src/contract/mod.rs +++ b/ddk-manager/src/contract/mod.rs @@ -3,12 +3,12 @@ use crate::error::Error; use crate::ContractId; use bitcoin::Transaction; -use dlc_messages::{ +use ddk_messages::{ oracle_msgs::{EventDescriptor, OracleAnnouncement, OracleAttestation}, AcceptDlc, SignDlc, }; -use dlc_trie::multi_oracle_trie::MultiOracleTrie; -use dlc_trie::multi_oracle_trie_with_diff::MultiOracleTrieWithDiff; +use ddk_trie::multi_oracle_trie::MultiOracleTrie; +use ddk_trie::multi_oracle_trie_with_diff::MultiOracleTrieWithDiff; use secp256k1_zkp::PublicKey; #[cfg(feature = "use-serde")] use serde::{Deserialize, Serialize}; diff --git a/dlc-manager/src/contract/numerical_descriptor.rs b/ddk-manager/src/contract/numerical_descriptor.rs similarity index 97% rename from dlc-manager/src/contract/numerical_descriptor.rs rename to ddk-manager/src/contract/numerical_descriptor.rs index 6ae535b7..075efa30 100644 --- a/dlc-manager/src/contract/numerical_descriptor.rs +++ b/ddk-manager/src/contract/numerical_descriptor.rs @@ -4,10 +4,10 @@ use super::AdaptorInfo; use crate::error::Error; use crate::payout_curve::{PayoutFunction, RoundingIntervals}; use bitcoin::{Script, Transaction}; -use dlc::{Payout, RangePayout}; -use dlc_trie::multi_oracle_trie::MultiOracleTrie; -use dlc_trie::multi_oracle_trie_with_diff::MultiOracleTrieWithDiff; -use dlc_trie::{DlcTrie, OracleNumericInfo}; +use ddk_dlc::{Payout, RangePayout}; +use ddk_trie::multi_oracle_trie::MultiOracleTrie; +use ddk_trie::multi_oracle_trie_with_diff::MultiOracleTrieWithDiff; +use ddk_trie::{DlcTrie, OracleNumericInfo}; use secp256k1_zkp::{All, EcdsaAdaptorSignature, PublicKey, Secp256k1, SecretKey}; #[cfg(feature = "use-serde")] use serde::{Deserialize, Serialize}; diff --git a/dlc-manager/src/contract/offered_contract.rs b/ddk-manager/src/contract/offered_contract.rs similarity index 97% rename from dlc-manager/src/contract/offered_contract.rs rename to ddk-manager/src/contract/offered_contract.rs index 6f40badb..66efa8ab 100644 --- a/dlc-manager/src/contract/offered_contract.rs +++ b/ddk-manager/src/contract/offered_contract.rs @@ -9,9 +9,9 @@ use super::contract_info::ContractInfo; use super::contract_input::ContractInput; use super::ContractDescriptor; use crate::{ContractId, KeysId}; -use dlc::PartyParams; -use dlc_messages::oracle_msgs::OracleAnnouncement; -use dlc_messages::{FundingInput, OfferDlc}; +use ddk_dlc::PartyParams; +use ddk_messages::oracle_msgs::OracleAnnouncement; +use ddk_messages::{FundingInput, OfferDlc}; use secp256k1_zkp::PublicKey; /// Contains information about a contract that was offered. @@ -53,7 +53,7 @@ impl OfferedContract { /// Validate that the contract info covers all the possible outcomes that /// can be attested by the oracle(s). pub fn validate(&self) -> Result<(), crate::error::Error> { - dlc::util::validate_fee_rate(self.fee_rate_per_vb).map_err(|_| { + ddk_dlc::util::validate_fee_rate(self.fee_rate_per_vb).map_err(|_| { crate::error::Error::InvalidParameters("Fee rate is too high".to_string()) })?; diff --git a/dlc-manager/src/contract/ser.rs b/ddk-manager/src/contract/ser.rs similarity index 94% rename from dlc-manager/src/contract/ser.rs rename to ddk-manager/src/contract/ser.rs index d05655ab..e3efe374 100644 --- a/dlc-manager/src/contract/ser.rs +++ b/ddk-manager/src/contract/ser.rs @@ -15,16 +15,16 @@ use crate::payout_curve::{ HyperbolaPayoutCurvePiece, PayoutFunction, PayoutFunctionPiece, PayoutPoint, PolynomialPayoutCurvePiece, RoundingInterval, RoundingIntervals, }; -use dlc::DlcTransactions; -use dlc_messages::ser_impls::{ +use ddk_dlc::DlcTransactions; +use ddk_messages::ser_impls::{ read_ecdsa_adaptor_signatures, read_option_cb, read_usize, read_vec, read_vec_cb, write_ecdsa_adaptor_signatures, write_option_cb, write_usize, write_vec, write_vec_cb, }; -use dlc_trie::digit_trie::{DigitNodeData, DigitTrieDump}; -use dlc_trie::multi_oracle_trie::{MultiOracleTrie, MultiOracleTrieDump}; -use dlc_trie::multi_oracle_trie_with_diff::{MultiOracleTrieWithDiff, MultiOracleTrieWithDiffDump}; -use dlc_trie::multi_trie::{MultiTrieDump, MultiTrieNodeData, TrieNodeInfo}; -use dlc_trie::{OracleNumericInfo, RangeInfo}; +use ddk_trie::digit_trie::{DigitNodeData, DigitTrieDump}; +use ddk_trie::multi_oracle_trie::{MultiOracleTrie, MultiOracleTrieDump}; +use ddk_trie::multi_oracle_trie_with_diff::{MultiOracleTrieWithDiff, MultiOracleTrieWithDiffDump}; +use ddk_trie::multi_trie::{MultiTrieDump, MultiTrieNodeData, TrieNodeInfo}; +use ddk_trie::{OracleNumericInfo, RangeInfo}; use lightning::io::Read; use lightning::ln::msgs::DecodeError; use lightning::util::ser::{Readable, Writeable, Writer}; @@ -83,14 +83,14 @@ impl_dlc_writeable!(ContractInfo, { (contract_descriptor, writeable), (oracle_an impl_dlc_writeable!(EnumDescriptor, { ( outcome_payouts, - {vec_cb, dlc_messages::ser_impls::enum_payout::write, dlc_messages::ser_impls::enum_payout::read} + {vec_cb, ddk_messages::ser_impls::enum_payout::write, ddk_messages::ser_impls::enum_payout::read} ) }); impl_dlc_writeable!(OfferedContract, { (id, writeable), (is_offer_party, writeable), (contract_info, vec), - (offer_params, { cb_writeable, dlc_messages::ser_impls::party_params::write, dlc_messages::ser_impls::party_params::read }), + (offer_params, { cb_writeable, ddk_messages::ser_impls::party_params::write, ddk_messages::ser_impls::party_params::read }), (total_collateral, writeable), (funding_inputs, vec), (fund_output_serial_id, writeable), @@ -111,7 +111,7 @@ impl_dlc_writeable_external!( ); impl_dlc_writeable!(AcceptedContract, { (offered_contract, writeable), - (accept_params, { cb_writeable, dlc_messages::ser_impls::party_params::write, dlc_messages::ser_impls::party_params::read }), + (accept_params, { cb_writeable, ddk_messages::ser_impls::party_params::write, ddk_messages::ser_impls::party_params::read }), (funding_inputs, vec), (adaptor_infos, vec), (adaptor_signatures, {option_cb, write_ecdsa_adaptor_signatures, read_ecdsa_adaptor_signatures }), diff --git a/dlc-manager/src/contract/signed_contract.rs b/ddk-manager/src/contract/signed_contract.rs similarity index 91% rename from dlc-manager/src/contract/signed_contract.rs rename to ddk-manager/src/contract/signed_contract.rs index f392a892..bcc8bc61 100644 --- a/dlc-manager/src/contract/signed_contract.rs +++ b/ddk-manager/src/contract/signed_contract.rs @@ -4,10 +4,10 @@ use crate::conversion_utils::PROTOCOL_VERSION; use crate::ChannelId; use super::accepted_contract::AcceptedContract; -use dlc_messages::CetAdaptorSignature; -use dlc_messages::CetAdaptorSignatures; -use dlc_messages::FundingSignatures; -use dlc_messages::SignDlc; +use ddk_messages::CetAdaptorSignature; +use ddk_messages::CetAdaptorSignatures; +use ddk_messages::FundingSignatures; +use ddk_messages::SignDlc; use secp256k1_zkp::ecdsa::Signature; use secp256k1_zkp::EcdsaAdaptorSignature; diff --git a/dlc-manager/src/contract/utils.rs b/ddk-manager/src/contract/utils.rs similarity index 100% rename from dlc-manager/src/contract/utils.rs rename to ddk-manager/src/contract/utils.rs diff --git a/dlc-manager/src/contract_updater.rs b/ddk-manager/src/contract_updater.rs similarity index 95% rename from dlc-manager/src/contract_updater.rs rename to ddk-manager/src/contract_updater.rs index 939da0c3..7fcc06fb 100644 --- a/dlc-manager/src/contract_updater.rs +++ b/ddk-manager/src/contract_updater.rs @@ -4,9 +4,9 @@ use std::ops::Deref; use bitcoin::psbt::Psbt; use bitcoin::{consensus::Decodable, Script, Transaction, Witness}; -use dlc::{DlcTransactions, PartyParams}; -use dlc_messages::FundingInput; -use dlc_messages::{ +use ddk_dlc::{DlcTransactions, PartyParams}; +use ddk_messages::FundingInput; +use ddk_messages::{ oracle_msgs::{OracleAnnouncement, OracleAttestation}, AcceptDlc, FundingSignature, FundingSignatures, OfferDlc, SignDlc, WitnessElement, }; @@ -27,7 +27,14 @@ use crate::{ /// Creates an [`OfferedContract`] and [`OfferDlc`] message from the provided /// contract and oracle information. -pub fn offer_contract( +pub async fn offer_contract< + W: Deref, + B: Deref, + T: Deref, + X: ContractSigner, + SP: Deref, + C: Signing, +>( secp: &Secp256k1, contract_input: &ContractInput, oracle_announcements: Vec>, @@ -56,7 +63,8 @@ where wallet, &signer, blockchain, - )?; + ) + .await?; let offered_contract = OfferedContract::new( id, @@ -77,7 +85,7 @@ where /// Creates an [`AcceptedContract`] and produces /// the accepting party's cet adaptor signatures. -pub fn accept_contract( +pub async fn accept_contract( secp: &Secp256k1, offered_contract: &OfferedContract, wallet: &W, @@ -99,9 +107,10 @@ where wallet, &signer, blockchain, - )?; + ) + .await?; - let dlc_transactions = dlc::create_dlc_transactions( + let dlc_transactions = ddk_dlc::create_dlc_transactions( &offered_contract.offer_params, &accept_params, &offered_contract.contract_info[0].get_payouts(total_collateral)?, @@ -171,7 +180,7 @@ pub(crate) fn accept_contract_internal( for contract_info in offered_contract.contract_info.iter().skip(1) { let payouts = contract_info.get_payouts(total_collateral)?; - let tmp_cets = dlc::create_cets( + let tmp_cets = ddk_dlc::create_cets( &cet_input, &offered_contract.offer_params.payout_script_pubkey, offered_contract.offer_params.payout_serial_id, @@ -197,7 +206,7 @@ pub(crate) fn accept_contract_internal( adaptor_sigs.extend(adaptor_sig); } - let refund_signature = dlc::util::get_raw_sig_for_tx_input( + let refund_signature = ddk_dlc::util::get_raw_sig_for_tx_input( secp, refund, 0, @@ -262,7 +271,7 @@ where let total_collateral = offered_contract.total_collateral; - let dlc_transactions = dlc::create_dlc_transactions( + let dlc_transactions = ddk_dlc::create_dlc_transactions( &offered_contract.offer_params, &accept_params, &offered_contract.contract_info[0].get_payouts(total_collateral)?, @@ -348,7 +357,7 @@ where let input_script_pubkey = input_script_pubkey.unwrap_or_else(|| funding_script_pubkey); let counter_adaptor_pk = counter_adaptor_pk.unwrap_or(accept_params.fund_pubkey); - dlc::verify_tx_input_sig( + ddk_dlc::verify_tx_input_sig( secp, refund_signature, refund, @@ -379,7 +388,7 @@ where for contract_info in offered_contract.contract_info.iter().skip(1) { let payouts = contract_info.get_payouts(total_collateral)?; - let tmp_cets = dlc::create_cets( + let tmp_cets = ddk_dlc::create_cets( &cet_input, &offered_contract.offer_params.payout_script_pubkey, offered_contract.offer_params.payout_serial_id, @@ -477,7 +486,7 @@ where }) .collect::, Error>>()?; - let offer_refund_signature = dlc::util::get_raw_sig_for_tx_input( + let offer_refund_signature = ddk_dlc::util::get_raw_sig_for_tx_input( secp, refund, 0, @@ -566,7 +575,7 @@ where let counter_adaptor_pk = counter_adaptor_pk.unwrap_or(accepted_contract.offered_contract.offer_params.fund_pubkey); - dlc::verify_tx_input_sig( + ddk_dlc::verify_tx_input_sig( secp, refund_signature, &accepted_contract.dlc_transactions.refund, @@ -696,7 +705,7 @@ where let funding_sk = signer.get_secret_key()?; - dlc::sign_cet( + ddk_dlc::sign_cet( secp, &mut cet, &adaptor_sigs[range_info.adaptor_index], @@ -745,7 +754,7 @@ where let fund_priv_key = signer.get_secret_key()?; let mut refund = accepted_contract.dlc_transactions.refund.clone(); - dlc::util::sign_multi_sig_input( + ddk_dlc::util::sign_multi_sig_input( secp, &mut refund, other_sig, @@ -762,11 +771,11 @@ where mod tests { use std::rc::Rc; - use mocks::dlc_manager::contract::offered_contract::OfferedContract; + use mocks::ddk_manager::contract::offered_contract::OfferedContract; use secp256k1_zkp::PublicKey; - #[test] - fn accept_contract_test() { + #[tokio::test] + async fn accept_contract_test() { let offer_dlc = serde_json::from_str(include_str!("../test_inputs/offer_contract.json")).unwrap(); let dummy_pubkey: PublicKey = @@ -780,18 +789,17 @@ mod tests { let utxo_value: u64 = offered_contract.total_collateral - offered_contract.offer_params.collateral + crate::utils::get_half_common_fee(fee_rate).unwrap(); - let wallet = Rc::new(mocks::mock_wallet::MockWallet::new( - &blockchain, - &[utxo_value, 10000], - )); + let wallet = + Rc::new(mocks::mock_wallet::MockWallet::new(&blockchain, &[utxo_value, 10000]).await); - mocks::dlc_manager::contract_updater::accept_contract( + mocks::ddk_manager::contract_updater::accept_contract( secp256k1_zkp::SECP256K1, &offered_contract, &wallet, &wallet, &blockchain, ) + .await .expect("Not to fail"); } } diff --git a/dlc-manager/src/conversion_utils.rs b/ddk-manager/src/conversion_utils.rs similarity index 99% rename from dlc-manager/src/conversion_utils.rs rename to ddk-manager/src/conversion_utils.rs index b7f15202..c72011ae 100644 --- a/dlc-manager/src/conversion_utils.rs +++ b/ddk-manager/src/conversion_utils.rs @@ -10,12 +10,12 @@ use crate::payout_curve::{ PolynomialPayoutCurvePiece, RoundingInterval, RoundingIntervals, }; use bitcoin::{consensus::encode::Decodable, Amount, OutPoint, Transaction}; -use dlc::{EnumerationPayout, Payout, TxInputInfo}; -use dlc_messages::oracle_msgs::{ +use ddk_dlc::{EnumerationPayout, Payout, TxInputInfo}; +use ddk_messages::oracle_msgs::{ MultiOracleInfo, OracleInfo as SerOracleInfo, OracleParams, SingleOracleInfo, }; -use dlc_messages::FundingInput; -use dlc_messages::{ +use ddk_messages::FundingInput; +use ddk_messages::{ contract_msgs::{ ContractDescriptor as SerContractDescriptor, ContractInfo as SerContractInfo, ContractInfoInner, ContractOutcome, DisjointContractInfo, EnumeratedContractDescriptor, @@ -28,7 +28,7 @@ use dlc_messages::{ }, oracle_msgs::EventDescriptor, }; -use dlc_trie::OracleNumericInfo; +use ddk_trie::OracleNumericInfo; use std::fmt; pub(crate) const BITCOIN_CHAINHASH: [u8; 32] = [ diff --git a/dlc-manager/src/error.rs b/ddk-manager/src/error.rs similarity index 96% rename from dlc-manager/src/error.rs rename to ddk-manager/src/error.rs index 9f0ff5c4..325bf901 100644 --- a/dlc-manager/src/error.rs +++ b/ddk-manager/src/error.rs @@ -24,7 +24,7 @@ pub enum Error { /// The oracle component encountered an error. OracleError(String), /// An error occurred in the DLC library. - DlcError(dlc::Error), + DlcError(ddk_dlc::Error), /// An error occurred in the Secp library. SecpError(secp256k1_zkp::Error), } @@ -53,8 +53,8 @@ impl From for Error { } } -impl From for Error { - fn from(e: dlc::Error) -> Error { +impl From for Error { + fn from(e: ddk_dlc::Error) -> Error { Error::DlcError(e) } } diff --git a/dlc-manager/src/lib.rs b/ddk-manager/src/lib.rs similarity index 92% rename from dlc-manager/src/lib.rs rename to ddk-manager/src/lib.rs index b5250c00..cd372523 100644 --- a/dlc-manager/src/lib.rs +++ b/ddk-manager/src/lib.rs @@ -1,7 +1,7 @@ //! # Library providing data structures and functions supporting the execution //! and management of DLC. -#![crate_name = "dlc_manager"] +#![crate_name = "ddk_manager"] // Coding conventions #![forbid(unsafe_code)] #![deny(non_upper_case_globals)] @@ -14,11 +14,11 @@ extern crate async_trait; extern crate bitcoin; -extern crate dlc; +extern crate ddk_dlc; #[macro_use] -extern crate dlc_messages; +extern crate ddk_messages; extern crate core; -extern crate dlc_trie; +extern crate ddk_trie; extern crate lightning; extern crate log; #[cfg(feature = "fuzztarget")] @@ -44,8 +44,8 @@ use channel::signed_channel::{SignedChannel, SignedChannelStateType}; use channel::Channel; use contract::PreClosedContract; use contract::{offered_contract::OfferedContract, signed_contract::SignedContract, Contract}; -use dlc_messages::oracle_msgs::{OracleAnnouncement, OracleAttestation}; -use dlc_messages::ser_impls::{read_address, write_address}; +use ddk_messages::oracle_msgs::{OracleAnnouncement, OracleAttestation}; +use ddk_messages::ser_impls::{read_address, write_address}; use error::Error; use lightning::ln::msgs::DecodeError; use lightning::util::ser::{Readable, Writeable, Writer}; @@ -167,20 +167,21 @@ pub trait Wallet { fn unreserve_utxos(&self, outpoints: &[OutPoint]) -> Result<(), Error>; } +#[async_trait::async_trait] /// Blockchain trait provides access to the bitcoin blockchain. pub trait Blockchain { /// Broadcast the given transaction to the bitcoin network. - fn send_transaction(&self, transaction: &Transaction) -> Result<(), Error>; + async fn send_transaction(&self, transaction: &Transaction) -> Result<(), Error>; /// Returns the network currently used (mainnet, testnet or regtest). fn get_network(&self) -> Result; /// Returns the height of the blockchain - fn get_blockchain_height(&self) -> Result; + async fn get_blockchain_height(&self) -> Result; /// Returns the block at given height - fn get_block_at_height(&self, height: u64) -> Result; + async fn get_block_at_height(&self, height: u64) -> Result; /// Get the transaction with given id. - fn get_transaction(&self, tx_id: &Txid) -> Result; + async fn get_transaction(&self, tx_id: &Txid) -> Result; /// Get the number of confirmation for the transaction with given id. - fn get_transaction_confirmations(&self, tx_id: &Txid) -> Result; + async fn get_transaction_confirmations(&self, tx_id: &Txid) -> Result; } /// Storage trait provides functionalities to store and retrieve DLCs. @@ -225,14 +226,15 @@ pub trait Storage { fn get_chain_monitor(&self) -> Result, Error>; } +#[async_trait::async_trait] /// Oracle trait provides access to oracle information. pub trait Oracle { /// Returns the public key of the oracle. fn get_public_key(&self) -> XOnlyPublicKey; /// Returns the announcement for the event with the given id if found. - fn get_announcement(&self, event_id: &str) -> Result; + async fn get_announcement(&self, event_id: &str) -> Result; /// Returns the attestation for the event with the given id if found. - fn get_attestation(&self, event_id: &str) -> Result; + async fn get_attestation(&self, event_id: &str) -> Result; } /// Represents a UTXO. diff --git a/dlc-manager/src/manager.rs b/ddk-manager/src/manager.rs similarity index 90% rename from dlc-manager/src/manager.rs rename to ddk-manager/src/manager.rs index 8662d15f..50792530 100644 --- a/dlc-manager/src/manager.rs +++ b/ddk-manager/src/manager.rs @@ -24,13 +24,16 @@ use bitcoin::consensus::encode::serialize_hex; use bitcoin::consensus::Decodable; use bitcoin::Address; use bitcoin::{OutPoint, Transaction}; -use dlc_messages::channel::{ +use ddk_messages::channel::{ AcceptChannel, CollaborativeCloseOffer, OfferChannel, Reject, RenewAccept, RenewConfirm, RenewFinalize, RenewOffer, RenewRevoke, SettleAccept, SettleConfirm, SettleFinalize, SettleOffer, SignChannel, }; -use dlc_messages::oracle_msgs::{OracleAnnouncement, OracleAttestation}; -use dlc_messages::{AcceptDlc, Message as DlcMessage, OfferDlc, SignDlc}; +use ddk_messages::oracle_msgs::{OracleAnnouncement, OracleAttestation}; +use ddk_messages::{AcceptDlc, Message as DlcMessage, OfferDlc, SignDlc}; +use futures::stream; +use futures::stream::FuturesUnordered; +use futures::{StreamExt, TryStreamExt}; use hex::DisplayHex; use lightning::chain::chaininterface::FeeEstimator; use lightning::ln::chan_utils::{ @@ -137,7 +140,7 @@ macro_rules! check_for_timed_out_channels { if let SignedChannelState::$state { timeout, .. } = channel.state { let is_timed_out = timeout < $manager.time.unix_time_now(); if is_timed_out { - match $manager.force_close_channel_internal(channel, true) { + match $manager.force_close_channel_internal(channel, true).await { Err(e) => error!("Error force closing channel {}", e), _ => {} } @@ -159,7 +162,7 @@ where F::Target: FeeEstimator, { /// Create a new Manager struct. - pub fn new( + pub async fn new( wallet: W, signer_provider: SP, blockchain: B, @@ -168,7 +171,7 @@ where time: T, fee_estimator: F, ) -> Result { - let init_height = blockchain.get_blockchain_height()?; + let init_height = blockchain.get_blockchain_height().await?; let chain_monitor = Mutex::new( store .get_chain_monitor()? @@ -196,7 +199,7 @@ where } /// Function called to pass a DlcMessage to the Manager. - pub fn on_dlc_message( + pub async fn on_dlc_message( &self, msg: &DlcMessage, counter_party: PublicKey, @@ -208,7 +211,7 @@ where } DlcMessage::Accept(a) => Ok(Some(self.on_accept_message(a, &counter_party)?)), DlcMessage::Sign(s) => { - self.on_sign_message(s, &counter_party)?; + self.on_sign_message(s, &counter_party).await?; Ok(None) } DlcMessage::OfferChannel(o) => { @@ -219,7 +222,7 @@ where self.on_accept_channel(a, &counter_party)?, ))), DlcMessage::SignChannel(s) => { - self.on_sign_channel(s, &counter_party)?; + self.on_sign_channel(s, &counter_party).await?; Ok(None) } DlcMessage::SettleOffer(s) => match self.on_settle_offer(s, &counter_party)? { @@ -269,18 +272,16 @@ where /// and an OfferDlc message returned. /// /// This function will fetch the oracle announcements from the oracle. - pub fn send_offer( + pub async fn send_offer( &self, contract_input: &ContractInput, counter_party: PublicKey, ) -> Result { - let oracle_announcements = contract_input - .contract_infos - .iter() - .map(|x| self.get_oracle_announcements(&x.oracles)) - .collect::, Error>>()?; + // If the oracle announcement fails to retrieve, then log and continue. + let oracle_announcements = self.oracle_announcements(contract_input).await?; self.send_offer_with_announcements(contract_input, counter_party, oracle_announcements) + .await } /// Function called to create a new DLC. The offered contract will be stored @@ -288,7 +289,7 @@ where /// /// This function allows to pass the oracle announcements directly instead of /// fetching them from the oracle. - pub fn send_offer_with_announcements( + pub async fn send_offer_with_announcements( &self, contract_input: &ContractInput, counter_party: PublicKey, @@ -304,7 +305,8 @@ where &self.blockchain, &self.time, &self.signer_provider, - )?; + ) + .await?; offered_contract.validate()?; @@ -314,7 +316,7 @@ where } /// Function to call to accept a DLC for which an offer was received. - pub fn accept_contract_offer( + pub async fn accept_contract_offer( &self, contract_id: &ContractId, ) -> Result<(ContractId, PublicKey, AcceptDlc), Error> { @@ -329,7 +331,8 @@ where &self.wallet, &self.signer_provider, &self.blockchain, - )?; + ) + .await?; self.wallet.import_address(&Address::p2wsh( &accepted_contract.dlc_transactions.funding_script_pubkey, @@ -349,8 +352,8 @@ where /// /// Consumers **MUST** call this periodically in order to /// determine when pending transactions reach confirmation. - pub fn periodic_chain_monitor(&self) -> Result<(), Error> { - let cur_height = self.blockchain.get_blockchain_height()?; + pub async fn periodic_chain_monitor(&self) -> Result<(), Error> { + let cur_height = self.blockchain.get_blockchain_height().await?; let last_height = self.chain_monitor.lock().unwrap().last_height; // TODO(luckysori): We could end up reprocessing a block at @@ -362,7 +365,7 @@ where } for height in last_height + 1..=cur_height { - let block = self.blockchain.get_block_at_height(height)?; + let block = self.blockchain.get_block_at_height(height).await?; self.chain_monitor .lock() @@ -375,13 +378,13 @@ where /// Function to call to check the state of the currently executing DLCs and /// update them if possible. - pub fn periodic_check(&self, check_channels: bool) -> Result<(), Error> { - self.check_signed_contracts()?; - self.check_confirmed_contracts()?; - self.check_preclosed_contracts()?; + pub async fn periodic_check(&self, check_channels: bool) -> Result<(), Error> { + self.check_signed_contracts().await?; + self.check_confirmed_contracts().await?; + self.check_preclosed_contracts().await?; if check_channels { - self.channel_checks()?; + self.channel_checks().await?; } Ok(()) @@ -448,7 +451,11 @@ where Ok(DlcMessage::Sign(signed_msg)) } - fn on_sign_message(&self, sign_message: &SignDlc, peer_id: &PublicKey) -> Result<(), Error> { + async fn on_sign_message( + &self, + sign_message: &SignDlc, + peer_id: &PublicKey, + ) -> Result<(), Error> { let accepted_contract = get_contract_in_state!(self, &sign_message.contract_id, Accepted, Some(*peer_id))?; @@ -465,12 +472,12 @@ where self.store .update_contract(&Contract::Signed(signed_contract))?; - self.blockchain.send_transaction(&fund_tx)?; + self.blockchain.send_transaction(&fund_tx).await?; Ok(()) } - fn get_oracle_announcements( + async fn get_oracle_announcements( &self, oracle_inputs: &OracleInput, ) -> Result, Error> { @@ -480,7 +487,8 @@ where .oracles .get(pubkey) .ok_or_else(|| Error::InvalidParameters("Unknown oracle public key".to_string()))?; - announcements.push(oracle.get_announcement(&oracle_inputs.event_id)?.clone()); + let announcement = oracle.get_announcement(&oracle_inputs.event_id).await?; + announcements.push(announcement); } Ok(announcements) @@ -518,14 +526,17 @@ where Err(e) } - fn check_signed_contract(&self, contract: &SignedContract) -> Result<(), Error> { - let confirmations = self.blockchain.get_transaction_confirmations( - &contract - .accepted_contract - .dlc_transactions - .fund - .compute_txid(), - )?; + async fn check_signed_contract(&self, contract: &SignedContract) -> Result<(), Error> { + let confirmations = self + .blockchain + .get_transaction_confirmations( + &contract + .accepted_contract + .dlc_transactions + .fund + .compute_txid(), + ) + .await?; if confirmations >= NB_CONFIRMATIONS { self.store .update_contract(&Contract::Confirmed(contract.clone()))?; @@ -533,9 +544,9 @@ where Ok(()) } - fn check_signed_contracts(&self) -> Result<(), Error> { + async fn check_signed_contracts(&self) -> Result<(), Error> { for c in self.store.get_signed_contracts()? { - if let Err(e) = self.check_signed_contract(&c) { + if let Err(e) = self.check_signed_contract(&c).await { error!( "Error checking confirmed contract {}: {}", c.accepted_contract.get_contract_id_string(), @@ -547,13 +558,13 @@ where Ok(()) } - fn check_confirmed_contracts(&self) -> Result<(), Error> { + async fn check_confirmed_contracts(&self) -> Result<(), Error> { for c in self.store.get_confirmed_contracts()? { // Confirmed contracts from channel are processed in channel specific methods. if c.channel_id.is_some() { continue; } - if let Err(e) = self.check_confirmed_contract(&c) { + if let Err(e) = self.check_confirmed_contract(&c).await { error!( "Error checking confirmed contract {}: {}", c.accepted_contract.get_contract_id_string(), @@ -565,7 +576,7 @@ where Ok(()) } - fn get_closable_contract_info<'a>( + async fn get_closable_contract_info<'a>( &'a self, contract: &'a SignedContract, ) -> ClosableContractInfo<'a> { @@ -581,26 +592,54 @@ where .enumerate() .collect(); if matured.len() >= contract_info.threshold { - let attestations: Vec<_> = matured - .iter() - .filter_map(|(i, announcement)| { - let oracle = self.oracles.get(&announcement.oracle_public_key)?; - let attestation = oracle + let attestations = stream::iter(matured.iter()) + .map(|(i, announcement)| async move { + // First try to get the oracle + let oracle = match self.oracles.get(&announcement.oracle_public_key) { + Some(oracle) => oracle, + None => { + log::debug!( + "Oracle not found for key: {}", + announcement.oracle_public_key + ); + return None; + } + }; + + // Then try to get the attestation + let attestation = match oracle .get_attestation(&announcement.oracle_event.event_id) - .ok()?; - attestation - .validate(&self.secp, announcement) - .map_err(|_| { + .await + { + Ok(attestation) => attestation, + Err(e) => { log::error!( - "Oracle attestation is not valid. pubkey={} event_id={}", - announcement.oracle_public_key, - announcement.oracle_event.event_id - ) - }) - .ok()?; + "Attestation not found for event. id={} error={}", + announcement.oracle_event.event_id, + e.to_string() + ); + return None; + } + }; + + // Validate the attestation + if let Err(e) = attestation.validate(&self.secp, announcement) { + log::error!( + "Oracle attestation is not valid. pubkey={} event_id={}, error={:?}", + announcement.oracle_public_key, + announcement.oracle_event.event_id, + e + ); + return None; + } + Some((*i, attestation)) }) - .collect(); + .collect::>() + .await + .filter_map(|result| async move { result }) // Filter out None values + .collect::>() + .await; if attestations.len() >= contract_info.threshold { return Some((contract_info, adaptor_info, attestations)); } @@ -609,8 +648,8 @@ where None } - fn check_confirmed_contract(&self, contract: &SignedContract) -> Result<(), Error> { - let closable_contract_info = self.get_closable_contract_info(contract); + async fn check_confirmed_contract(&self, contract: &SignedContract) -> Result<(), Error> { + let closable_contract_info = self.get_closable_contract_info(contract).await; if let Some((contract_info, adaptor_info, attestations)) = closable_contract_info { let offer = &contract.accepted_contract.offered_contract; let signer = self.signer_provider.derive_contract_signer(offer.keys_id)?; @@ -622,11 +661,14 @@ where &attestations, &signer, )?; - match self.close_contract( - contract, - cet, - attestations.iter().map(|x| x.1.clone()).collect(), - ) { + match self + .close_contract( + contract, + cet, + attestations.iter().map(|x| x.1.clone()).collect(), + ) + .await + { Ok(closed_contract) => { self.store.update_contract(&closed_contract)?; return Ok(()); @@ -642,13 +684,13 @@ where } } - self.check_refund(contract)?; + self.check_refund(contract).await?; Ok(()) } /// Manually close a contract with the oracle attestations. - pub fn close_confirmed_contract( + pub async fn close_confirmed_contract( &self, contract_id: &ContractId, attestations: Vec<(usize, OracleAttestation)>, @@ -684,8 +726,9 @@ where // Check that the lock time has passed let time = bitcoin::absolute::Time::from_consensus(self.time.unix_time_now() as u32) .expect("Time is not in valid range. This should never happen."); - let height = Height::from_consensus(self.blockchain.get_blockchain_height()? as u32) - .expect("Height is not in valid range. This should never happen."); + let height = + Height::from_consensus(self.blockchain.get_blockchain_height().await? as u32) + .expect("Height is not in valid range. This should never happen."); let locktime = cet.lock_time; if !locktime.is_satisfied_by(height, time) { @@ -694,11 +737,14 @@ where )); } - match self.close_contract( - &contract, - cet, - attestations.into_iter().map(|x| x.1).collect(), - ) { + match self + .close_contract( + &contract, + cet, + attestations.into_iter().map(|x| x.1).collect(), + ) + .await + { Ok(closed_contract) => { self.store.update_contract(&closed_contract)?; Ok(closed_contract) @@ -718,9 +764,9 @@ where } } - fn check_preclosed_contracts(&self) -> Result<(), Error> { + async fn check_preclosed_contracts(&self) -> Result<(), Error> { for c in self.store.get_preclosed_contracts()? { - if let Err(e) = self.check_preclosed_contract(&c) { + if let Err(e) = self.check_preclosed_contract(&c).await { error!( "Error checking pre-closed contract {}: {}", c.signed_contract.accepted_contract.get_contract_id_string(), @@ -732,11 +778,12 @@ where Ok(()) } - fn check_preclosed_contract(&self, contract: &PreClosedContract) -> Result<(), Error> { + async fn check_preclosed_contract(&self, contract: &PreClosedContract) -> Result<(), Error> { let broadcasted_txid = contract.signed_cet.compute_txid(); let confirmations = self .blockchain - .get_transaction_confirmations(&broadcasted_txid)?; + .get_transaction_confirmations(&broadcasted_txid) + .await?; if confirmations >= NB_CONFIRMATIONS { let closed_contract = ClosedContract { attestations: contract.attestations.clone(), @@ -764,7 +811,7 @@ where Ok(()) } - fn close_contract( + async fn close_contract( &self, contract: &SignedContract, signed_cet: Transaction, @@ -772,14 +819,15 @@ where ) -> Result { let confirmations = self .blockchain - .get_transaction_confirmations(&signed_cet.compute_txid())?; + .get_transaction_confirmations(&signed_cet.compute_txid()) + .await?; if confirmations < 1 { // TODO(tibo): if this fails because another tx is already in // mempool or blockchain, we might have been cheated. There is // not much to be done apart from possibly extracting a fraud // proof but ideally it should be handled. - self.blockchain.send_transaction(&signed_cet)?; + self.blockchain.send_transaction(&signed_cet).await?; let preclosed_contract = PreClosedContract { signed_contract: contract.clone(), @@ -810,7 +858,7 @@ where Ok(Contract::Closed(closed_contract)) } - fn check_refund(&self, contract: &SignedContract) -> Result<(), Error> { + async fn check_refund(&self, contract: &SignedContract) -> Result<(), Error> { // TODO(tibo): should check for confirmation of refund before updating state if contract .accepted_contract @@ -824,13 +872,14 @@ where let refund = accepted_contract.dlc_transactions.refund.clone(); let confirmations = self .blockchain - .get_transaction_confirmations(&refund.compute_txid())?; + .get_transaction_confirmations(&refund.compute_txid()) + .await?; if confirmations == 0 { let offer = &contract.accepted_contract.offered_contract; let signer = self.signer_provider.derive_contract_signer(offer.keys_id)?; let refund = crate::contract_updater::get_signed_refund(&self.secp, contract, &signer)?; - self.blockchain.send_transaction(&refund)?; + self.blockchain.send_transaction(&refund).await?; } self.store @@ -910,16 +959,12 @@ where { /// Create a new channel offer and return the [`dlc_messages::channel::OfferChannel`] /// message to be sent to the `counter_party`. - pub fn offer_channel( + pub async fn offer_channel( &self, contract_input: &ContractInput, counter_party: PublicKey, ) -> Result { - let oracle_announcements = contract_input - .contract_infos - .iter() - .map(|x| self.get_oracle_announcements(&x.oracles)) - .collect::, Error>>()?; + let oracle_announcements = self.oracle_announcements(contract_input).await?; let (offered_channel, offered_contract) = crate::channel_updater::offer_channel( &self.secp, @@ -933,7 +978,8 @@ where &self.blockchain, &self.time, crate::utils::get_new_temporary_id(), - )?; + ) + .await?; let msg = offered_channel.get_offer_channel_msg(&offered_contract); @@ -979,7 +1025,7 @@ where /// Accept a channel that was offered. Returns the [`dlc_messages::channel::AcceptChannel`] /// message to be sent, the updated [`crate::ChannelId`] and [`crate::ContractId`], /// as well as the public key of the offering node. - pub fn accept_channel( + pub async fn accept_channel( &self, channel_id: &ChannelId, ) -> Result<(AcceptChannel, ChannelId, ContractId, PublicKey), Error> { @@ -1007,7 +1053,8 @@ where &self.wallet, &self.signer_provider, &self.blockchain, - )?; + ) + .await?; self.wallet.import_address(&Address::p2wsh( &accepted_contract.dlc_transactions.funding_script_pubkey, @@ -1027,10 +1074,10 @@ where } /// Force close the channel with given [`crate::ChannelId`]. - pub fn force_close_channel(&self, channel_id: &ChannelId) -> Result<(), Error> { + pub async fn force_close_channel(&self, channel_id: &ChannelId) -> Result<(), Error> { let channel = get_channel_in_state!(self, channel_id, Signed, None as Option)?; - self.force_close_channel_internal(channel, true) + self.force_close_channel_internal(channel, true).await } /// Offer to settle the balance of a channel so that the counter party gets @@ -1092,7 +1139,7 @@ where /// Returns a [`RenewOffer`] message as well as the [`PublicKey`] of the /// counter party's node to offer the establishment of a new contract in the /// channel. - pub fn renew_offer( + pub async fn renew_offer( &self, channel_id: &ChannelId, counter_payout: u64, @@ -1101,11 +1148,7 @@ where let mut signed_channel = get_channel_in_state!(self, channel_id, Signed, None as Option)?; - let oracle_announcements = contract_input - .contract_infos - .iter() - .map(|x| self.get_oracle_announcements(&x.oracles)) - .collect::, Error>>()?; + let oracle_announcements = self.oracle_announcements(contract_input).await?; let (msg, offered_contract) = crate::channel_updater::renew_offer( &self.secp, @@ -1259,7 +1302,7 @@ where /// Accept an offer to collaboratively close the channel. The close transaction /// will be broadcast and the state of the channel updated. - pub fn accept_collaborative_close(&self, channel_id: &ChannelId) -> Result<(), Error> { + pub async fn accept_collaborative_close(&self, channel_id: &ChannelId) -> Result<(), Error> { let signed_channel = get_channel_in_state!(self, channel_id, Signed, None as Option)?; @@ -1288,7 +1331,7 @@ where &self.signer_provider, )?; - self.blockchain.send_transaction(&close_tx)?; + self.blockchain.send_transaction(&close_tx).await?; self.store.upsert_channel(closed_channel, None)?; @@ -1300,7 +1343,7 @@ where Ok(()) } - fn try_finalize_closing_established_channel( + async fn try_finalize_closing_established_channel( &self, signed_channel: SignedChannel, ) -> Result<(), Error> { @@ -1314,7 +1357,8 @@ where if self .blockchain - .get_transaction_confirmations(&buffer_tx.compute_txid())? + .get_transaction_confirmations(&buffer_tx.compute_txid()) + .await? >= CET_NSEQUENCE { log::info!( @@ -1327,6 +1371,7 @@ where let (contract_info, adaptor_info, attestations) = self .get_closable_contract_info(&confirmed_contract) + .await .ok_or_else(|| { Error::InvalidState("Could not get information to close contract".to_string()) })?; @@ -1343,11 +1388,13 @@ where is_initiator, )?; - let closed_contract = self.close_contract( - &confirmed_contract, - signed_cet, - attestations.iter().map(|x| &x.1).cloned().collect(), - )?; + let closed_contract = self + .close_contract( + &confirmed_contract, + signed_cet, + attestations.iter().map(|x| &x.1).cloned().collect(), + ) + .await?; self.chain_monitor .lock() @@ -1479,7 +1526,7 @@ where Ok(sign_channel) } - fn on_sign_channel( + async fn on_sign_channel( &self, sign_channel: &SignChannel, peer_id: &PublicKey, @@ -1534,7 +1581,7 @@ where unreachable!(); } - self.blockchain.send_transaction(&signed_fund_tx)?; + self.blockchain.send_transaction(&signed_fund_tx).await?; self.store.upsert_channel( Channel::Signed(signed_channel), @@ -2087,24 +2134,24 @@ where Ok(()) } - fn channel_checks(&self) -> Result<(), Error> { + async fn channel_checks(&self) -> Result<(), Error> { let established_closing_channels = self .store .get_signed_channels(Some(SignedChannelStateType::Closing))?; for channel in established_closing_channels { - if let Err(e) = self.try_finalize_closing_established_channel(channel) { + if let Err(e) = self.try_finalize_closing_established_channel(channel).await { error!("Error trying to close established channel: {}", e); } } - if let Err(e) = self.check_for_timed_out_channels() { + if let Err(e) = self.check_for_timed_out_channels().await { error!("Error checking timed out channels {}", e); } - self.check_for_watched_tx() + self.check_for_watched_tx().await } - fn check_for_timed_out_channels(&self) -> Result<(), Error> { + async fn check_for_timed_out_channels(&self) -> Result<(), Error> { check_for_timed_out_channels!(self, RenewOffered); check_for_timed_out_channels!(self, RenewAccepted); check_for_timed_out_channels!(self, RenewConfirmed); @@ -2115,7 +2162,7 @@ where Ok(()) } - pub(crate) fn process_watched_txs( + pub(crate) async fn process_watched_txs( &self, watched_txs: Vec<(Transaction, ChannelInfo)>, ) -> Result<(), Error> { @@ -2259,7 +2306,7 @@ where let signed_tx = match revoked_tx_type { RevokedTxType::Buffer => { - dlc::channel::create_and_sign_punish_buffer_transaction( + ddk_dlc::channel::create_and_sign_punish_buffer_transaction( &self.secp, offer_params, accept_params, @@ -2273,7 +2320,7 @@ where )? } RevokedTxType::Settle => { - dlc::channel::create_and_sign_punish_settle_transaction( + ddk_dlc::channel::create_and_sign_punish_settle_transaction( &self.secp, offer_params, accept_params, @@ -2290,7 +2337,7 @@ where } }; - self.blockchain.send_transaction(&signed_tx)?; + self.blockchain.send_transaction(&signed_tx).await?; let closed_channel = Channel::ClosedPunished(ClosedPunishedChannel { counter_party: signed_channel.counter_party, @@ -2420,10 +2467,10 @@ where Ok(()) } - fn check_for_watched_tx(&self) -> Result<(), Error> { + async fn check_for_watched_tx(&self) -> Result<(), Error> { let confirmed_txs = self.chain_monitor.lock().unwrap().confirmed_txs(); - self.process_watched_txs(confirmed_txs)?; + self.process_watched_txs(confirmed_txs).await?; self.get_store() .persist_chain_monitor(&self.chain_monitor.lock().unwrap())?; @@ -2431,7 +2478,7 @@ where Ok(()) } - fn force_close_channel_internal( + async fn force_close_channel_internal( &self, mut channel: SignedChannel, is_initiator: bool, @@ -2450,6 +2497,7 @@ where counter_buffer_adaptor_signature, buffer_transaction, ) + .await } SignedChannelState::RenewFinalized { buffer_transaction, @@ -2464,8 +2512,11 @@ where offer_buffer_adaptor_signature, buffer_transaction, ) + .await + } + SignedChannelState::Settled { .. } => { + self.close_settled_channel(channel, is_initiator).await } - SignedChannelState::Settled { .. } => self.close_settled_channel(channel, is_initiator), SignedChannelState::SettledOffered { .. } | SignedChannelState::SettledReceived { .. } | SignedChannelState::SettledAccepted { .. } @@ -2478,7 +2529,8 @@ where .roll_back_state .take() .expect("to have a rollback state"); - self.force_close_channel_internal(channel, is_initiator) + let channel_clone = channel.clone(); // Clone the channel to avoid moving it + Box::pin(self.force_close_channel_internal(channel_clone, is_initiator)).await } SignedChannelState::Closing { .. } => Err(Error::InvalidState( "Channel is already closing.".to_string(), @@ -2487,7 +2539,7 @@ where } /// Initiate the unilateral closing of a channel that has been established. - fn initiate_unilateral_close_established_channel( + async fn initiate_unilateral_close_established_channel( &self, mut signed_channel: SignedChannel, is_initiator: bool, @@ -2511,7 +2563,7 @@ where let buffer_transaction = get_signed_channel_state!(signed_channel, Closing, ref buffer_transaction)?; - self.blockchain.send_transaction(buffer_transaction)?; + self.blockchain.send_transaction(buffer_transaction).await?; self.chain_monitor .lock() @@ -2528,7 +2580,7 @@ where } /// Unilaterally close a channel that has been settled. - fn close_settled_channel( + async fn close_settled_channel( &self, signed_channel: SignedChannel, is_initiator: bool, @@ -2543,10 +2595,11 @@ where if self .blockchain .get_transaction_confirmations(&settle_tx.compute_txid()) + .await .unwrap_or(0) == 0 { - self.blockchain.send_transaction(&settle_tx)?; + self.blockchain.send_transaction(&settle_tx).await?; } self.chain_monitor @@ -2590,13 +2643,37 @@ where pnl, }) } + + async fn oracle_announcements( + &self, + contract_input: &ContractInput, + ) -> Result>, Error> { + let announcements = stream::iter(contract_input.contract_infos.iter()) + .map(|x| { + let future = self.get_oracle_announcements(&x.oracles); + async move { + match future.await { + Ok(result) => Ok(result), + Err(e) => { + log::error!("Failed to get oracle announcements: {}", e); + Err(e) + } + } + } + }) + .collect::>() + .await + .try_collect::>() + .await?; + Ok(announcements) + } } #[cfg(test)] mod test { - use dlc_messages::Message; + use ddk_messages::Message; use mocks::{ - dlc_manager::{manager::Manager, CachedContractSignerProvider, Oracle, SimpleSigner}, + ddk_manager::{manager::Manager, CachedContractSignerProvider, Oracle, SimpleSigner}, memory_storage_provider::MemoryStorage, mock_blockchain::MockBlockchain, mock_oracle_provider::MockOracle, @@ -2617,13 +2694,16 @@ mod test { SimpleSigner, >; - fn get_manager() -> TestManager { + async fn get_manager() -> TestManager { let blockchain = Rc::new(MockBlockchain::new()); let store = Rc::new(MemoryStorage::new()); - let wallet = Rc::new(MockWallet::new( - &blockchain, - &(0..100).map(|x| x as u64 * 1000000).collect::>(), - )); + let wallet = Rc::new( + MockWallet::new( + &blockchain, + &(0..100).map(|x| x as u64 * 1000000).collect::>(), + ) + .await, + ); let oracle_list = (0..5).map(|_| MockOracle::new()).collect::>(); let oracles: HashMap = oracle_list @@ -2643,6 +2723,7 @@ mod test { time, blockchain, ) + .await .unwrap() } @@ -2652,37 +2733,41 @@ mod test { .unwrap() } - #[test] - fn reject_offer_with_existing_contract_id() { + #[tokio::test] + async fn reject_offer_with_existing_contract_id() { let offer_message = Message::Offer( serde_json::from_str(include_str!("../test_inputs/offer_contract.json")).unwrap(), ); - let manager = get_manager(); + let manager = get_manager().await; manager .on_dlc_message(&offer_message, pubkey()) + .await .expect("To accept the first offer message"); manager .on_dlc_message(&offer_message, pubkey()) + .await .expect_err("To reject the second offer message"); } - #[test] - fn reject_channel_offer_with_existing_channel_id() { + #[tokio::test] + async fn reject_channel_offer_with_existing_channel_id() { let offer_message = Message::OfferChannel( serde_json::from_str(include_str!("../test_inputs/offer_channel.json")).unwrap(), ); - let manager = get_manager(); + let manager = get_manager().await; manager .on_dlc_message(&offer_message, pubkey()) + .await .expect("To accept the first offer message"); manager .on_dlc_message(&offer_message, pubkey()) + .await .expect_err("To reject the second offer message"); } } diff --git a/dlc-manager/src/payout_curve.rs b/ddk-manager/src/payout_curve.rs similarity index 99% rename from dlc-manager/src/payout_curve.rs rename to ddk-manager/src/payout_curve.rs index 6d06677c..abad38ed 100644 --- a/dlc-manager/src/payout_curve.rs +++ b/ddk-manager/src/payout_curve.rs @@ -3,7 +3,7 @@ use std::ops::Deref; use crate::error::Error; -use dlc::{Payout, RangePayout}; +use ddk_dlc::{Payout, RangePayout}; #[cfg(feature = "use-serde")] use serde::{Deserialize, Serialize}; diff --git a/dlc-manager/src/utils.rs b/ddk-manager/src/utils.rs similarity index 95% rename from dlc-manager/src/utils.rs rename to ddk-manager/src/utils.rs index f206b4fa..cd4dee12 100644 --- a/dlc-manager/src/utils.rs +++ b/ddk-manager/src/utils.rs @@ -2,12 +2,12 @@ use std::ops::Deref; use bitcoin::{consensus::Encodable, Amount, Txid}; -use dlc::{PartyParams, TxInputInfo}; -use dlc_messages::{ +use ddk_dlc::{PartyParams, TxInputInfo}; +use ddk_messages::{ oracle_msgs::{OracleAnnouncement, OracleAttestation}, FundingInput, }; -use dlc_trie::RangeInfo; +use ddk_trie::RangeInfo; #[cfg(not(feature = "fuzztarget"))] use secp256k1_zkp::rand::{thread_rng, Rng, RngCore}; use secp256k1_zkp::{PublicKey, Secp256k1, Signing}; @@ -89,7 +89,7 @@ pub(crate) fn compute_id( res } -pub(crate) fn get_party_params( +pub(crate) async fn get_party_params( secp: &Secp256k1, own_collateral: u64, fee_rate: u64, @@ -111,15 +111,16 @@ where let change_serial_id = get_new_serial_id(); // Add base cost of fund tx + CET / 2 and a CET output to the collateral. - let appr_required_amount = - own_collateral + get_half_common_fee(fee_rate)? + dlc::util::weight_to_fee(124, fee_rate)?; + let appr_required_amount = own_collateral + + get_half_common_fee(fee_rate)? + + ddk_dlc::util::weight_to_fee(124, fee_rate)?; let utxos = wallet.get_utxos_for_amount(appr_required_amount, fee_rate, true)?; let mut funding_inputs: Vec = Vec::new(); let mut funding_tx_info: Vec = Vec::new(); let mut total_input = Amount::ZERO; for utxo in utxos { - let prev_tx = blockchain.get_transaction(&utxo.outpoint.txid)?; + let prev_tx = blockchain.get_transaction(&utxo.outpoint.txid).await?; let mut writer = Vec::new(); prev_tx.consensus_encode(&mut writer)?; let prev_tx_vout = utxo.outpoint.vout; @@ -171,7 +172,7 @@ where } pub(crate) fn get_half_common_fee(fee_rate: u64) -> Result { - let common_fee = dlc::util::get_common_fee(fee_rate)?; + let common_fee = ddk_dlc::util::get_common_fee(fee_rate)?; Ok((common_fee as f64 / 2_f64).ceil() as u64) } @@ -218,7 +219,7 @@ pub(crate) fn get_latest_maturity_date( mod tests { use std::str::FromStr; - use dlc_messages::oracle_msgs::{EnumEventDescriptor, EventDescriptor, OracleEvent}; + use ddk_messages::oracle_msgs::{EnumEventDescriptor, EventDescriptor, OracleEvent}; use secp256k1_zkp::{ rand::{thread_rng, RngCore}, schnorr::Signature, diff --git a/dlc-manager/test_inputs/Accepted b/ddk-manager/test_inputs/Accepted similarity index 100% rename from dlc-manager/test_inputs/Accepted rename to ddk-manager/test_inputs/Accepted diff --git a/dlc-manager/test_inputs/offer_channel.json b/ddk-manager/test_inputs/offer_channel.json similarity index 100% rename from dlc-manager/test_inputs/offer_channel.json rename to ddk-manager/test_inputs/offer_channel.json diff --git a/dlc-manager/test_inputs/offer_contract.json b/ddk-manager/test_inputs/offer_contract.json similarity index 100% rename from dlc-manager/test_inputs/offer_contract.json rename to ddk-manager/test_inputs/offer_contract.json diff --git a/dlc-manager/test_inputs/offer_enum_collateral_not_equal_payout.json b/ddk-manager/test_inputs/offer_enum_collateral_not_equal_payout.json similarity index 100% rename from dlc-manager/test_inputs/offer_enum_collateral_not_equal_payout.json rename to ddk-manager/test_inputs/offer_enum_collateral_not_equal_payout.json diff --git a/dlc-manager/test_inputs/offer_enum_missing_payout.json b/ddk-manager/test_inputs/offer_enum_missing_payout.json similarity index 100% rename from dlc-manager/test_inputs/offer_enum_missing_payout.json rename to ddk-manager/test_inputs/offer_enum_missing_payout.json diff --git a/dlc-manager/test_inputs/offer_enum_oracle_with_diff_payout.json b/ddk-manager/test_inputs/offer_enum_oracle_with_diff_payout.json similarity index 100% rename from dlc-manager/test_inputs/offer_enum_oracle_with_diff_payout.json rename to ddk-manager/test_inputs/offer_enum_oracle_with_diff_payout.json diff --git a/dlc-manager/test_inputs/offer_numerical_bad_first_payout.json b/ddk-manager/test_inputs/offer_numerical_bad_first_payout.json similarity index 100% rename from dlc-manager/test_inputs/offer_numerical_bad_first_payout.json rename to ddk-manager/test_inputs/offer_numerical_bad_first_payout.json diff --git a/dlc-manager/test_inputs/offer_numerical_bad_last_payout.json b/ddk-manager/test_inputs/offer_numerical_bad_last_payout.json similarity index 100% rename from dlc-manager/test_inputs/offer_numerical_bad_last_payout.json rename to ddk-manager/test_inputs/offer_numerical_bad_last_payout.json diff --git a/dlc-manager/test_inputs/offer_numerical_collateral_less_than_payout.json b/ddk-manager/test_inputs/offer_numerical_collateral_less_than_payout.json similarity index 100% rename from dlc-manager/test_inputs/offer_numerical_collateral_less_than_payout.json rename to ddk-manager/test_inputs/offer_numerical_collateral_less_than_payout.json diff --git a/dlc-manager/test_inputs/offer_numerical_empty_rounding_interval.json b/ddk-manager/test_inputs/offer_numerical_empty_rounding_interval.json similarity index 100% rename from dlc-manager/test_inputs/offer_numerical_empty_rounding_interval.json rename to ddk-manager/test_inputs/offer_numerical_empty_rounding_interval.json diff --git a/dlc-manager/test_inputs/offer_numerical_invalid_rounding_interval.json b/ddk-manager/test_inputs/offer_numerical_invalid_rounding_interval.json similarity index 100% rename from dlc-manager/test_inputs/offer_numerical_invalid_rounding_interval.json rename to ddk-manager/test_inputs/offer_numerical_invalid_rounding_interval.json diff --git a/dlc-manager/test_inputs/offer_numerical_non_continuous.json b/ddk-manager/test_inputs/offer_numerical_non_continuous.json similarity index 100% rename from dlc-manager/test_inputs/offer_numerical_non_continuous.json rename to ddk-manager/test_inputs/offer_numerical_non_continuous.json diff --git a/dlc-manager/tests/channel_execution_tests.rs b/ddk-manager/tests/channel_execution_tests.rs similarity index 71% rename from dlc-manager/tests/channel_execution_tests.rs rename to ddk-manager/tests/channel_execution_tests.rs index 05da7bb5..0579b846 100644 --- a/dlc-manager/tests/channel_execution_tests.rs +++ b/ddk-manager/tests/channel_execution_tests.rs @@ -4,14 +4,14 @@ mod test_utils; use bitcoin::Amount; use bitcoin_test_utils::rpc_helpers::init_clients; use bitcoincore_rpc::RpcApi; -use dlc_manager::contract::contract_input::ContractInput; -use dlc_manager::manager::Manager; -use dlc_manager::{ +use ddk_manager::contract::contract_input::ContractInput; +use ddk_manager::manager::Manager; +use ddk_manager::{ channel::Channel, contract::Contract, Blockchain, CachedContractSignerProvider, Oracle, SimpleSigner, Storage, Wallet, }; -use dlc_manager::{ChannelId, ContractId}; -use dlc_messages::Message; +use ddk_manager::{ChannelId, ContractId}; +use ddk_messages::Message; use electrs_blockchain_provider::ElectrsBlockchainProvider; use lightning::util::ser::Writeable; use mocks::memory_storage_provider::MemoryStorage; @@ -21,19 +21,19 @@ use secp256k1_zkp::rand::{thread_rng, RngCore}; use secp256k1_zkp::EcdsaAdaptorSignature; use simple_wallet::SimpleWallet; use test_utils::{get_enum_test_params, TestParams}; +use tokio::time::sleep; -use std::sync::mpsc::{sync_channel, Receiver, Sender}; -use std::thread; - -use std::time::Duration; +use std::future::Future; +use std::pin::Pin; use std::{ collections::HashMap, sync::{ atomic::{AtomicBool, Ordering}, - mpsc::channel, - Arc, Mutex, + Arc, }, }; +use tokio::sync::mpsc::{channel, Receiver, Sender}; +use tokio::sync::Mutex; use crate::test_utils::{refresh_wallet, EVENT_MATURITY}; @@ -57,10 +57,13 @@ type DlcParty = Arc< >, >; -fn get_established_channel_contract_id(dlc_party: &DlcParty, channel_id: &ChannelId) -> ContractId { +async fn get_established_channel_contract_id( + dlc_party: &DlcParty, + channel_id: &ChannelId, +) -> ContractId { let channel = dlc_party .lock() - .unwrap() + .await .get_store() .get_channel(channel_id) .unwrap() @@ -82,11 +85,11 @@ fn alter_adaptor_sig(input: &EcdsaAdaptorSignature) -> EcdsaAdaptorSignature { /// We wrap updating the state of the chain monitor and calling the /// `Manager::periodic_check` because the latter will only be aware of /// newly confirmed transactions if the former processes new blocks. -fn periodic_check(dlc_party: DlcParty) { - let dlc_manager = dlc_party.lock().unwrap(); +async fn periodic_check(dlc_party: DlcParty) { + let dlc_manager = dlc_party.lock().await; - dlc_manager.periodic_chain_monitor().unwrap(); - dlc_manager.periodic_check(true).unwrap(); + dlc_manager.periodic_chain_monitor().await.unwrap(); + dlc_manager.periodic_check(true).await.unwrap(); } #[derive(Eq, PartialEq, Clone)] @@ -115,182 +118,195 @@ enum TestPath { CancelOffer, } -#[test] +#[tokio::test] #[ignore] -fn channel_established_close_test() { - channel_execution_test(get_enum_test_params(1, 1, None), TestPath::Close); +async fn channel_established_close_test() { + channel_execution_test(get_enum_test_params(1, 1, None), TestPath::Close).await; } -#[test] +#[tokio::test] #[ignore] -fn channel_bad_accept_buffer_adaptor_test() { +async fn channel_bad_accept_buffer_adaptor_test() { channel_execution_test( get_enum_test_params(1, 1, None), TestPath::BadAcceptBufferAdaptorSignature, - ); + ) + .await; } -#[test] +#[tokio::test] #[ignore] -fn channel_bad_sign_buffer_adaptor_test() { +async fn channel_bad_sign_buffer_adaptor_test() { channel_execution_test( get_enum_test_params(1, 1, None), TestPath::BadSignBufferAdaptorSignature, - ); + ) + .await; } -#[test] +#[tokio::test] #[ignore] -fn channel_settled_close_test() { - channel_execution_test(get_enum_test_params(1, 1, None), TestPath::SettleClose); +async fn channel_settled_close_test() { + channel_execution_test(get_enum_test_params(1, 1, None), TestPath::SettleClose).await; } -#[test] +#[tokio::test] #[ignore] -fn channel_punish_buffer_test() { - channel_execution_test(get_enum_test_params(1, 1, None), TestPath::BufferCheat); +async fn channel_punish_buffer_test() { + channel_execution_test(get_enum_test_params(1, 1, None), TestPath::BufferCheat).await; } -#[test] +#[tokio::test] #[ignore] -fn channel_renew_close_test() { - channel_execution_test(get_enum_test_params(1, 1, None), TestPath::RenewedClose); +async fn channel_renew_close_test() { + channel_execution_test(get_enum_test_params(1, 1, None), TestPath::RenewedClose).await; } -#[test] +#[tokio::test] #[ignore] -fn channel_renew_established_close_test() { +async fn channel_renew_established_close_test() { channel_execution_test( get_enum_test_params(1, 1, None), TestPath::RenewEstablishedClose, - ); + ) + .await; } -#[test] +#[tokio::test] #[ignore] -fn channel_settle_cheat_test() { - channel_execution_test(get_enum_test_params(1, 1, None), TestPath::SettleCheat); +async fn channel_settle_cheat_test() { + channel_execution_test(get_enum_test_params(1, 1, None), TestPath::SettleCheat).await; } -#[test] +#[tokio::test] #[ignore] -fn channel_collaborative_close_test() { +async fn channel_collaborative_close_test() { channel_execution_test( get_enum_test_params(1, 1, None), TestPath::CollaborativeClose, - ); + ) + .await; } -#[test] +#[tokio::test] #[ignore] -fn channel_settle_renew_settle_test() { +async fn channel_settle_renew_settle_test() { channel_execution_test( get_enum_test_params(1, 1, None), TestPath::SettleRenewSettle, - ); + ) + .await; } -#[test] +#[tokio::test] #[ignore] -fn channel_settle_offer_timeout_test() { +async fn channel_settle_offer_timeout_test() { channel_execution_test( get_enum_test_params(1, 1, None), TestPath::SettleOfferTimeout, - ); + ) + .await; } -#[test] +#[tokio::test] #[ignore] -fn channel_settle_accept_timeout_test() { +async fn channel_settle_accept_timeout_test() { channel_execution_test( get_enum_test_params(1, 1, None), TestPath::SettleAcceptTimeout, - ); + ) + .await; } -#[test] +#[tokio::test] #[ignore] -fn channel_settle_confirm_timeout_test() { +async fn channel_settle_confirm_timeout_test() { channel_execution_test( get_enum_test_params(1, 1, None), TestPath::SettleConfirmTimeout, - ); + ) + .await; } -#[test] +#[tokio::test] #[ignore] -fn channel_settle_reject_test() { - channel_execution_test(get_enum_test_params(1, 1, None), TestPath::SettleReject); +async fn channel_settle_reject_test() { + channel_execution_test(get_enum_test_params(1, 1, None), TestPath::SettleReject).await; } -#[test] +#[tokio::test] #[ignore] -fn channel_settle_race_test() { - channel_execution_test(get_enum_test_params(1, 1, None), TestPath::SettleRace); +async fn channel_settle_race_test() { + channel_execution_test(get_enum_test_params(1, 1, None), TestPath::SettleRace).await; } -#[test] +#[tokio::test] #[ignore] -fn channel_renew_offer_timeout_test() { +async fn channel_renew_offer_timeout_test() { channel_execution_test( get_enum_test_params(1, 1, None), TestPath::RenewOfferTimeout, - ); + ) + .await; } -#[test] +#[tokio::test] #[ignore] -fn channel_renew_accept_timeout_test() { +async fn channel_renew_accept_timeout_test() { channel_execution_test( get_enum_test_params(1, 1, None), TestPath::RenewAcceptTimeout, - ); + ) + .await; } -#[test] +#[tokio::test] #[ignore] -fn channel_renew_confirm_timeout_test() { +async fn channel_renew_confirm_timeout_test() { channel_execution_test( get_enum_test_params(1, 1, None), TestPath::RenewConfirmTimeout, - ); + ) + .await; } -#[test] +#[tokio::test] #[ignore] -fn channel_renew_finalize_timeout_test() { +async fn channel_renew_finalize_timeout_test() { channel_execution_test( get_enum_test_params(1, 1, None), TestPath::RenewFinalizeTimeout, - ); + ) + .await; } -#[test] +#[tokio::test] #[ignore] -fn channel_renew_reject_test() { - channel_execution_test(get_enum_test_params(1, 1, None), TestPath::RenewReject); +async fn channel_renew_reject_test() { + channel_execution_test(get_enum_test_params(1, 1, None), TestPath::RenewReject).await; } -#[test] +#[tokio::test] #[ignore] -fn channel_renew_race_test() { - channel_execution_test(get_enum_test_params(1, 1, None), TestPath::RenewRace); +async fn channel_renew_race_test() { + channel_execution_test(get_enum_test_params(1, 1, None), TestPath::RenewRace).await; } -#[test] +#[tokio::test] #[ignore] -fn channel_offer_reject_test() { - channel_execution_test(get_enum_test_params(1, 1, None), TestPath::CancelOffer); +async fn channel_offer_reject_test() { + channel_execution_test(get_enum_test_params(1, 1, None), TestPath::CancelOffer).await; } -fn channel_execution_test(test_params: TestParams, path: TestPath) { +async fn channel_execution_test(test_params: TestParams, path: TestPath) { env_logger::init(); - let (alice_send, bob_receive) = channel::>(); - let (bob_send, alice_receive) = channel::>(); - let (alice_sync_send, alice_sync_receive) = sync_channel::<()>(0); - let (bob_sync_send, bob_sync_receive) = sync_channel::<()>(0); + let (alice_send, mut bob_receive) = channel::>(100); + let (bob_send, mut alice_receive) = channel::>(100); + let (alice_sync_send, mut alice_sync_receive) = channel::<()>(100); + let (bob_sync_send, mut bob_sync_receive) = channel::<()>(100); let (_, _, sink_rpc) = init_clients(); + let sink = Arc::new(sink_rpc); let mut alice_oracles = HashMap::with_capacity(1); let mut bob_oracles = HashMap::with_capacity(1); @@ -306,10 +322,16 @@ fn channel_execution_test(test_params: TestParams, path: TestPath) { let mock_time = Arc::new(mocks::mock_time::MockTime {}); mocks::mock_time::set_time((EVENT_MATURITY as u64) - 1); - let electrs = Arc::new(ElectrsBlockchainProvider::new( - "http://localhost:3004/".to_string(), - bitcoin::Network::Regtest, - )); + let electrs = tokio::task::spawn_blocking(|| { + Arc::new(ElectrsBlockchainProvider::new( + "http://localhost:3004/".to_string(), + bitcoin::Network::Regtest, + )) + }) + .await + .unwrap(); + + println!("couldnt create electrs"); let alice_wallet = Arc::new(SimpleWallet::new( electrs.clone(), @@ -323,58 +345,66 @@ fn channel_execution_test(test_params: TestParams, path: TestPath) { bitcoin::Network::Regtest, )); + println!("getting address"); let alice_fund_address = alice_wallet.get_new_address().unwrap(); let bob_fund_address = bob_wallet.get_new_address().unwrap(); - sink_rpc - .send_to_address( - &alice_fund_address, - Amount::from_btc(2.0).unwrap(), - None, - None, - None, - None, - None, - None, - ) - .unwrap(); - - sink_rpc - .send_to_address( - &bob_fund_address, - Amount::from_btc(2.0).unwrap(), - None, - None, - None, - None, - None, - None, - ) - .unwrap(); - - let generate_blocks = |nb_blocks: u64| { - let prev_blockchain_height = electrs.get_blockchain_height().unwrap(); - - let sink_address = sink_rpc - .get_new_address(None, None) - .expect("RPC Error") - .assume_checked(); - sink_rpc - .generate_to_address(nb_blocks, &sink_address) - .expect("RPC Error"); - - // Wait for electrs to have processed the new blocks - let mut cur_blockchain_height = prev_blockchain_height; - while cur_blockchain_height < prev_blockchain_height + nb_blocks { - std::thread::sleep(std::time::Duration::from_millis(200)); - cur_blockchain_height = electrs.get_blockchain_height().unwrap(); - } + println!("funded address"); + sink.send_to_address( + &alice_fund_address, + Amount::from_btc(2.0).unwrap(), + None, + None, + None, + None, + None, + None, + ) + .unwrap(); + + println!("prolly not her."); + + sink.send_to_address( + &bob_fund_address, + Amount::from_btc(2.0).unwrap(), + None, + None, + None, + None, + None, + None, + ) + .unwrap(); + + let generate_blocks = |nb_blocks: u64| -> Pin + Send>> { + let electrs_clone = electrs.clone(); + let sink_clone = sink.clone(); + println!("failing"); + Box::pin(async move { + let prev_blockchain_height = electrs_clone.get_blockchain_height().await.unwrap(); + println!("Got block {}", prev_blockchain_height); + let sink_address = sink_clone + .get_new_address(None, None) + .expect("RPC Error") + .assume_checked(); + println!("got address"); + sink_clone + .generate_to_address(nb_blocks, &sink_address) + .expect("RPC Error"); + // Wait for electrs to have processed the new blocks + let mut cur_blockchain_height = prev_blockchain_height; + while cur_blockchain_height < prev_blockchain_height + nb_blocks { + sleep(std::time::Duration::from_millis(200)).await; + cur_blockchain_height = electrs_clone.get_blockchain_height().await.unwrap(); + } + }) }; - generate_blocks(6); - - refresh_wallet(&alice_wallet, 200000000); - refresh_wallet(&bob_wallet, 200000000); + println!("generating."); + generate_blocks(6).await; + println!("agh generate."); + refresh_wallet(&alice_wallet, 200000000).await; + refresh_wallet(&bob_wallet, 200000000).await; let alice_manager = Arc::new(Mutex::new( Manager::new( @@ -386,6 +416,7 @@ fn channel_execution_test(test_params: TestParams, path: TestPath) { Arc::clone(&mock_time), Arc::clone(&electrs), ) + .await .unwrap(), )); @@ -402,6 +433,7 @@ fn channel_execution_test(test_params: TestParams, path: TestPath) { Arc::clone(&mock_time), Arc::clone(&electrs), ) + .await .unwrap(), )); @@ -476,44 +508,53 @@ fn channel_execution_test(test_params: TestParams, path: TestPath) { let offer_msg = bob_manager_send .lock() - .unwrap() + .await .offer_channel( &test_params.contract_input, "0218845781f631c48f1c9709e23092067d06837f30aa0cd0544ac887fe91ddd166" .parse() .unwrap(), ) + .await .expect("Send offer error"); let temporary_channel_id = offer_msg.temporary_channel_id; bob_send .send(Some(Message::OfferChannel(offer_msg))) + .await .unwrap(); assert_channel_state!(bob_manager_send, temporary_channel_id, Offered); - alice_sync_receive.recv().expect("Error synchronizing"); + alice_sync_receive + .recv() + .await + .expect("Error synchronizing"); assert_channel_state!(alice_manager_send, temporary_channel_id, Offered); if let TestPath::CancelOffer = path { let (reject_msg, _) = alice_manager_send .lock() - .unwrap() + .await .reject_channel(&temporary_channel_id) .expect("Error rejecting contract offer"); assert_channel_state!(alice_manager_send, temporary_channel_id, Cancelled); - alice_send.send(Some(Message::Reject(reject_msg))).unwrap(); + alice_send + .send(Some(Message::Reject(reject_msg))) + .await + .unwrap(); - bob_sync_receive.recv().expect("Error synchronizing"); + bob_sync_receive.recv().await.expect("Error synchronizing"); assert_channel_state!(bob_manager_send, temporary_channel_id, Cancelled); return; } let (mut accept_msg, channel_id, contract_id, _) = alice_manager_send .lock() - .unwrap() + .await .accept_channel(&temporary_channel_id) + .await .expect("Error accepting contract offer"); assert_channel_state!(alice_manager_send, channel_id, Accepted); @@ -524,40 +565,49 @@ fn channel_execution_test(test_params: TestParams, path: TestPath) { bob_expect_error.store(true, Ordering::Relaxed); alice_send .send(Some(Message::AcceptChannel(accept_msg))) + .await .unwrap(); - bob_sync_receive.recv().expect("Error synchronizing"); + bob_sync_receive.recv().await.expect("Error synchronizing"); assert_channel_state!(bob_manager_send, temporary_channel_id, FailedAccept); } TestPath::BadSignBufferAdaptorSignature => { alice_expect_error.store(true, Ordering::Relaxed); alice_send .send(Some(Message::AcceptChannel(accept_msg))) + .await .unwrap(); // Bob receives accept message - bob_sync_receive.recv().expect("Error synchronizing"); + bob_sync_receive.recv().await.expect("Error synchronizing"); // Alice receives sign message - alice_sync_receive.recv().expect("Error synchronizing"); + alice_sync_receive + .recv() + .await + .expect("Error synchronizing"); assert_channel_state!(alice_manager_send, channel_id, FailedSign); } _ => { alice_send .send(Some(Message::AcceptChannel(accept_msg))) + .await .unwrap(); - bob_sync_receive.recv().expect("Error synchronizing"); + bob_sync_receive.recv().await.expect("Error synchronizing"); assert_channel_state!(bob_manager_send, channel_id, Signed, Established); - alice_sync_receive.recv().expect("Error synchronizing"); + alice_sync_receive + .recv() + .await + .expect("Error synchronizing"); assert_channel_state!(alice_manager_send, channel_id, Signed, Established); - generate_blocks(6); + generate_blocks(6).await; mocks::mock_time::set_time((EVENT_MATURITY as u64) + 1); - periodic_check(alice_manager_send.clone()); + periodic_check(alice_manager_send.clone()).await; - periodic_check(bob_manager_send.clone()); + periodic_check(bob_manager_send.clone()).await; assert_contract_state!(alice_manager_send, contract_id, Confirmed); assert_contract_state!(bob_manager_send, contract_id, Confirmed); @@ -568,25 +618,25 @@ fn channel_execution_test(test_params: TestParams, path: TestPath) { ( alice_manager_send, &alice_send, - &alice_sync_receive, + &mut alice_sync_receive, bob_manager_send, &bob_send, - &bob_sync_receive, + &mut bob_sync_receive, ) } else { ( bob_manager_send, &bob_send, - &bob_sync_receive, + &mut bob_sync_receive, alice_manager_send, &alice_send, - &alice_sync_receive, + &mut alice_sync_receive, ) }; match path { TestPath::Close => { - close_established_channel(first, second, channel_id, &generate_blocks); + close_established_channel(first, second, channel_id, &generate_blocks).await; } TestPath::CollaborativeClose => { collaborative_close( @@ -596,7 +646,8 @@ fn channel_execution_test(test_params: TestParams, path: TestPath) { channel_id, second_receive, &generate_blocks, - ); + ) + .await; } TestPath::SettleOfferTimeout | TestPath::SettleAcceptTimeout @@ -610,7 +661,8 @@ fn channel_execution_test(test_params: TestParams, path: TestPath) { second_receive, channel_id, path, - ); + ) + .await; } TestPath::SettleReject => { settle_reject( @@ -621,7 +673,8 @@ fn channel_execution_test(test_params: TestParams, path: TestPath) { second_send, second_receive, channel_id, - ); + ) + .await; } TestPath::SettleRace => { settle_race( @@ -632,7 +685,8 @@ fn channel_execution_test(test_params: TestParams, path: TestPath) { second_send, second_receive, channel_id, - ); + ) + .await; } _ => { // Shuffle positions @@ -657,7 +711,7 @@ fn channel_execution_test(test_params: TestParams, path: TestPath) { ) }; - first.lock().unwrap().get_store().save(); + first.lock().await.get_store().save(); if let TestPath::RenewEstablishedClose = path { } else { @@ -669,7 +723,8 @@ fn channel_execution_test(test_params: TestParams, path: TestPath) { second_send, second_receive, channel_id, - ); + ) + .await; } match path { @@ -682,12 +737,13 @@ fn channel_execution_test(test_params: TestParams, path: TestPath) { closer .lock() - .unwrap() + .await .force_close_channel(&channel_id) + .await .expect("to be able to unilaterally close the channel."); } TestPath::BufferCheat => { - cheat_punish(first, second, channel_id, &generate_blocks, true); + cheat_punish(first, second, channel_id, &generate_blocks, true).await; } TestPath::RenewOfferTimeout | TestPath::RenewAcceptTimeout @@ -703,7 +759,8 @@ fn channel_execution_test(test_params: TestParams, path: TestPath) { &test_params.contract_input, path, &generate_blocks, - ); + ) + .await; } TestPath::RenewReject => { renew_reject( @@ -715,7 +772,8 @@ fn channel_execution_test(test_params: TestParams, path: TestPath) { second_receive, channel_id, &test_params.contract_input, - ); + ) + .await; } TestPath::RenewRace => { renew_race( @@ -727,12 +785,13 @@ fn channel_execution_test(test_params: TestParams, path: TestPath) { second_receive, channel_id, &test_params.contract_input, - ); + ) + .await; } TestPath::RenewedClose | TestPath::SettleCheat | TestPath::RenewEstablishedClose => { - first.lock().unwrap().get_store().save(); + first.lock().await.get_store().save(); let check_prev_contract_close = if let TestPath::RenewEstablishedClose = path { @@ -751,7 +810,8 @@ fn channel_execution_test(test_params: TestParams, path: TestPath) { channel_id, &test_params.contract_input, check_prev_contract_close, - ); + ) + .await; if let TestPath::RenewedClose = path { close_established_channel( @@ -759,9 +819,11 @@ fn channel_execution_test(test_params: TestParams, path: TestPath) { second, channel_id, &generate_blocks, - ); + ) + .await; } else if let TestPath::SettleCheat = path { - cheat_punish(first, second, channel_id, &generate_blocks, false); + cheat_punish(first, second, channel_id, &generate_blocks, false) + .await; } } TestPath::SettleRenewSettle => { @@ -775,7 +837,8 @@ fn channel_execution_test(test_params: TestParams, path: TestPath) { channel_id, &test_params.contract_input, false, - ); + ) + .await; settle_channel( first, @@ -785,7 +848,8 @@ fn channel_execution_test(test_params: TestParams, path: TestPath) { second_send, second_receive, channel_id, - ); + ) + .await; } _ => (), } @@ -794,119 +858,125 @@ fn channel_execution_test(test_params: TestParams, path: TestPath) { } } - alice_send.send(None).unwrap(); - bob_send.send(None).unwrap(); + alice_send.send(None).await.unwrap(); + bob_send.send(None).await.unwrap(); - alice_handle.join().unwrap(); - bob_handle.join().unwrap(); + alice_handle.await.unwrap(); + bob_handle.await.unwrap(); } -fn close_established_channel( +async fn close_established_channel( first: DlcParty, second: DlcParty, channel_id: ChannelId, generate_blocks: &F, ) where - F: Fn(u64), + F: Fn(u64) -> Pin + Send>>, { first .lock() - .unwrap() + .await .force_close_channel(&channel_id) + .await .expect("to be able to unilaterally close."); assert_channel_state!(first, channel_id, Signed, Closing); - let contract_id = get_established_channel_contract_id(&first, &channel_id); + let contract_id = get_established_channel_contract_id(&first, &channel_id).await; - periodic_check(first.clone()); + periodic_check(first.clone()).await; - let wait = dlc_manager::manager::CET_NSEQUENCE; + let wait = ddk_manager::manager::CET_NSEQUENCE; - generate_blocks(10); + generate_blocks(10).await; - periodic_check(second.clone()); + periodic_check(second.clone()).await; assert_channel_state!(second, channel_id, Signed, Closing); - periodic_check(first.clone()); + periodic_check(first.clone()).await; // Should not have changed state before the CET is spendable. assert_channel_state!(first, channel_id, Signed, Closing); - generate_blocks(wait as u64 - 9); + generate_blocks(wait as u64 - 9).await; - periodic_check(first.clone()); + periodic_check(first.clone()).await; assert_channel_state!(first, channel_id, Closed); assert_contract_state!(first, contract_id, PreClosed); - generate_blocks(1); + generate_blocks(1).await; - periodic_check(second.clone()); + periodic_check(second.clone()).await; assert_channel_state!(second, channel_id, CounterClosed); assert_contract_state!(second, contract_id, PreClosed); - generate_blocks(5); + generate_blocks(5).await; - periodic_check(first.clone()); - periodic_check(second.clone()); + periodic_check(first.clone()).await; + periodic_check(second.clone()).await; assert_contract_state!(first, contract_id, Closed); assert_contract_state!(second, contract_id, Closed); } -fn cheat_punish( +async fn cheat_punish( first: DlcParty, second: DlcParty, channel_id: ChannelId, generate_blocks: &F, established: bool, -) { - first.lock().unwrap().get_store().rollback(); +) where + F: Fn(u64) -> Pin + Send>>, +{ + first.lock().await.get_store().rollback(); if established { first .lock() - .unwrap() + .await .force_close_channel(&channel_id) + .await .expect("the cheater to be able to close on established"); } else { first .lock() - .unwrap() + .await .force_close_channel(&channel_id) + .await .expect("the cheater to be able to close on settled"); } - generate_blocks(2); + generate_blocks(2).await; - periodic_check(second.clone()); + periodic_check(second.clone()).await; assert_channel_state!(second, channel_id, ClosedPunished); } -fn settle_channel( +async fn settle_channel( first: DlcParty, first_send: &Sender>, - first_receive: &Receiver<()>, + first_receive: &mut Receiver<()>, second: DlcParty, second_send: &Sender>, - second_receive: &Receiver<()>, + second_receive: &mut Receiver<()>, channel_id: ChannelId, ) { let (settle_offer, _) = first .lock() - .unwrap() + .await .settle_offer(&channel_id, test_utils::ACCEPT_COLLATERAL) .expect("to be able to offer a settlement of the contract."); first_send .send(Some(Message::SettleOffer(settle_offer))) + .await .unwrap(); - second_receive.recv().expect("Error synchronizing"); + second_receive.recv().await.expect("Error synchronizing"); assert_channel_state!(first, channel_id, Signed, SettledOffered); @@ -914,46 +984,48 @@ fn settle_channel( let (settle_accept, _) = second .lock() - .unwrap() + .await .accept_settle_offer(&channel_id) .expect("to be able to accept a settlement offer"); second_send .send(Some(Message::SettleAccept(settle_accept))) + .await .unwrap(); // Process Accept - first_receive.recv().expect("Error synchronizing"); + first_receive.recv().await.expect("Error synchronizing"); // Process Confirm - second_receive.recv().expect("Error synchronizing"); + second_receive.recv().await.expect("Error synchronizing"); // Process Finalize - first_receive.recv().expect("Error synchronizing"); + first_receive.recv().await.expect("Error synchronizing"); assert_channel_state!(first, channel_id, Signed, Settled); assert_channel_state!(second, channel_id, Signed, Settled); } -fn settle_reject( +async fn settle_reject( first: DlcParty, first_send: &Sender>, - first_receive: &Receiver<()>, + first_receive: &mut Receiver<()>, second: DlcParty, second_send: &Sender>, - second_receive: &Receiver<()>, + second_receive: &mut Receiver<()>, channel_id: ChannelId, ) { let (settle_offer, _) = first .lock() - .unwrap() + .await .settle_offer(&channel_id, test_utils::ACCEPT_COLLATERAL) .expect("to be able to reject a settlement of the contract."); first_send .send(Some(Message::SettleOffer(settle_offer))) + .await .unwrap(); - second_receive.recv().expect("Error synchronizing"); + second_receive.recv().await.expect("Error synchronizing"); assert_channel_state!(first, channel_id, Signed, SettledOffered); @@ -961,128 +1033,126 @@ fn settle_reject( let (settle_reject, _) = second .lock() - .unwrap() + .await .reject_settle_offer(&channel_id) .expect("to be able to reject a settlement offer"); second_send .send(Some(Message::Reject(settle_reject))) + .await .unwrap(); - first_receive.recv().expect("Error synchronizing"); + first_receive.recv().await.expect("Error synchronizing"); assert_channel_state!(first, channel_id, Signed, Established); assert_channel_state!(second, channel_id, Signed, Established); } -fn settle_race( +async fn settle_race( first: DlcParty, first_send: &Sender>, - first_receive: &Receiver<()>, + first_receive: &mut Receiver<()>, second: DlcParty, second_send: &Sender>, - second_receive: &Receiver<()>, + second_receive: &mut Receiver<()>, channel_id: ChannelId, ) { let (settle_offer, _) = first .lock() - .unwrap() + .await .settle_offer(&channel_id, test_utils::ACCEPT_COLLATERAL) .expect("to be able to offer a settlement of the contract."); let (settle_offer_2, _) = second .lock() - .unwrap() + .await .settle_offer(&channel_id, test_utils::ACCEPT_COLLATERAL) .expect("to be able to offer a settlement of the contract."); first_send .send(Some(Message::SettleOffer(settle_offer))) + .await .unwrap(); second_send .send(Some(Message::SettleOffer(settle_offer_2))) + .await .unwrap(); // Process 2 offers + 2 rejects - first_receive - .recv_timeout(Duration::from_secs(2)) - .expect("Error synchronizing 1"); - second_receive - .recv_timeout(Duration::from_secs(2)) - .expect("Error synchronizing 2"); - first_receive - .recv_timeout(Duration::from_secs(2)) - .expect("Error synchronizing 3"); - second_receive - .recv_timeout(Duration::from_secs(2)) - .expect("Error synchronizing 4"); + first_receive.recv().await.expect("Error synchronizing 1"); + second_receive.recv().await.expect("Error synchronizing 2"); + first_receive.recv().await.expect("Error synchronizing 3"); + second_receive.recv().await.expect("Error synchronizing 4"); assert_channel_state!(first, channel_id, Signed, Established); assert_channel_state!(second, channel_id, Signed, Established); } -fn renew_channel( +async fn renew_channel( first: DlcParty, first_send: &Sender>, - first_receive: &Receiver<()>, + first_receive: &mut Receiver<()>, second: DlcParty, second_send: &Sender>, - second_receive: &Receiver<()>, + second_receive: &mut Receiver<()>, channel_id: ChannelId, contract_input: &ContractInput, check_prev_contract_close: bool, ) { let prev_contract_id = if check_prev_contract_close { - Some(get_established_channel_contract_id(&first, &channel_id)) + Some(get_established_channel_contract_id(&first, &channel_id).await) } else { None }; let (renew_offer, _) = first .lock() - .unwrap() + .await .renew_offer(&channel_id, test_utils::ACCEPT_COLLATERAL, contract_input) + .await .expect("to be able to renew channel contract"); first_send .send(Some(Message::RenewOffer(renew_offer))) + .await .expect("to be able to send the renew offer"); // Process Renew Offer - second_receive.recv().expect("Error synchronizing"); + second_receive.recv().await.expect("Error synchronizing"); assert_channel_state!(first, channel_id, Signed, RenewOffered); assert_channel_state!(second, channel_id, Signed, RenewOffered); let (accept_renew, _) = second .lock() - .unwrap() + .await .accept_renew_offer(&channel_id) .expect("to be able to accept the renewal"); second_send .send(Some(Message::RenewAccept(accept_renew))) + .await .expect("to be able to send the accept renew"); // Process Renew Accept - first_receive.recv().expect("Error synchronizing"); + first_receive.recv().await.expect("Error synchronizing"); assert_channel_state!(first, channel_id, Signed, RenewConfirmed); // Process Renew Confirm - second_receive.recv().expect("Error synchronizing"); + second_receive.recv().await.expect("Error synchronizing"); // Process Renew Finalize - first_receive.recv().expect("Error synchronizing"); + first_receive.recv().await.expect("Error synchronizing"); // Process Renew Revoke - second_receive.recv().expect("Error synchronizing"); + second_receive.recv().await.expect("Error synchronizing"); if let Some(prev_contract_id) = prev_contract_id { assert_contract_state!(first, prev_contract_id, Closed); assert_contract_state!(second, prev_contract_id, Closed); } - let new_contract_id = get_established_channel_contract_id(&first, &channel_id); + let new_contract_id = get_established_channel_contract_id(&first, &channel_id).await; assert_channel_state!(first, channel_id, Signed, Established); assert_contract_state!(first, new_contract_id, Confirmed); @@ -1090,62 +1160,66 @@ fn renew_channel( assert_contract_state!(second, new_contract_id, Confirmed); } -fn renew_reject( +async fn renew_reject( first: DlcParty, first_send: &Sender>, - first_receive: &Receiver<()>, + first_receive: &mut Receiver<()>, second: DlcParty, second_send: &Sender>, - second_receive: &Receiver<()>, + second_receive: &mut Receiver<()>, channel_id: ChannelId, contract_input: &ContractInput, ) { let (renew_offer, _) = first .lock() - .unwrap() + .await .renew_offer(&channel_id, test_utils::ACCEPT_COLLATERAL, contract_input) + .await .expect("to be able to renew channel contract"); first_send .send(Some(Message::RenewOffer(renew_offer))) + .await .expect("to be able to send the renew offer"); // Process Renew Offer - second_receive.recv().expect("Error synchronizing"); + second_receive.recv().await.expect("Error synchronizing"); assert_channel_state!(first, channel_id, Signed, RenewOffered); assert_channel_state!(second, channel_id, Signed, RenewOffered); let (renew_reject, _) = second .lock() - .unwrap() + .await .reject_renew_offer(&channel_id) .expect("to be able to reject the renewal"); second_send .send(Some(Message::Reject(renew_reject))) + .await .expect("to be able to send the renew reject"); // Process Renew Reject - first_receive.recv().expect("Error synchronizing"); + first_receive.recv().await.expect("Error synchronizing"); assert_channel_state!(first, channel_id, Signed, Settled); assert_channel_state!(second, channel_id, Signed, Settled); } -fn renew_race( +async fn renew_race( first: DlcParty, first_send: &Sender>, - first_receive: &Receiver<()>, + first_receive: &mut Receiver<()>, second: DlcParty, second_send: &Sender>, - second_receive: &Receiver<()>, + second_receive: &mut Receiver<()>, channel_id: ChannelId, contract_input: &ContractInput, ) { let (renew_offer, _) = first .lock() - .unwrap() + .await .renew_offer(&channel_id, test_utils::OFFER_COLLATERAL, contract_input) + .await .expect("to be able to renew channel contract"); let mut contract_input_2 = contract_input.clone(); @@ -1154,148 +1228,152 @@ fn renew_race( let (renew_offer_2, _) = second .lock() - .unwrap() + .await .renew_offer(&channel_id, test_utils::OFFER_COLLATERAL, &contract_input_2) + .await .expect("to be able to renew channel contract"); first_send .send(Some(Message::RenewOffer(renew_offer))) + .await .expect("to be able to send the renew offer"); second_send .send(Some(Message::RenewOffer(renew_offer_2))) + .await .expect("to be able to send the renew offer"); // Process 2 offers + 2 rejects - first_receive - .recv_timeout(Duration::from_secs(2)) - .expect("Error synchronizing 1"); - second_receive - .recv_timeout(Duration::from_secs(2)) - .expect("Error synchronizing 2"); - first_receive - .recv_timeout(Duration::from_secs(2)) - .expect("Error synchronizing 3"); - second_receive - .recv_timeout(Duration::from_secs(2)) - .expect("Error synchronizing 4"); + first_receive.recv().await.expect("Error synchronizing 1"); + second_receive.recv().await.expect("Error synchronizing 2"); + first_receive.recv().await.expect("Error synchronizing 3"); + second_receive.recv().await.expect("Error synchronizing 4"); assert_channel_state!(first, channel_id, Signed, Settled); assert_channel_state!(second, channel_id, Signed, Settled); } -fn collaborative_close( +async fn collaborative_close( first: DlcParty, first_send: &Sender>, second: DlcParty, channel_id: ChannelId, - sync_receive: &Receiver<()>, + sync_receive: &mut Receiver<()>, generate_blocks: &F, -) { - let contract_id = get_established_channel_contract_id(&first, &channel_id); +) where + F: Fn(u64) -> Pin + Send>>, +{ + let contract_id = get_established_channel_contract_id(&first, &channel_id).await; let close_offer = first .lock() - .unwrap() + .await .offer_collaborative_close(&channel_id, 100000000) .expect("to be able to propose a collaborative close"); first_send .send(Some(Message::CollaborativeCloseOffer(close_offer))) + .await .expect("to be able to send collaborative close"); - sync_receive.recv().expect("Error synchronizing"); + sync_receive.recv().await.expect("Error synchronizing"); assert_channel_state!(first, channel_id, Signed, CollaborativeCloseOffered); assert_channel_state!(second, channel_id, Signed, CollaborativeCloseOffered); second .lock() - .unwrap() + .await .accept_collaborative_close(&channel_id) + .await .expect("to be able to accept a collaborative close"); assert_channel_state!(second, channel_id, CollaborativelyClosed); assert_contract_state!(second, contract_id, Closed); - generate_blocks(2); + generate_blocks(2).await; - periodic_check(first.clone()); + periodic_check(first.clone()).await; assert_channel_state!(first, channel_id, CollaborativelyClosed); assert_contract_state!(first, contract_id, Closed); } -fn renew_timeout( +async fn renew_timeout( first: DlcParty, first_send: &Sender>, - first_receive: &Receiver<()>, + first_receive: &mut Receiver<()>, second: DlcParty, second_send: &Sender>, - second_receive: &Receiver<()>, + second_receive: &mut Receiver<()>, channel_id: ChannelId, contract_input: &ContractInput, path: TestPath, generate_blocks: &F, -) { +) where + F: Fn(u64) -> Pin + Send>>, +{ { let (renew_offer, _) = first .lock() - .unwrap() + .await .renew_offer(&channel_id, test_utils::ACCEPT_COLLATERAL, contract_input) + .await .expect("to be able to offer a settlement of the contract."); first_send .send(Some(Message::RenewOffer(renew_offer))) + .await .unwrap(); - second_receive.recv().expect("Error synchronizing"); + second_receive.recv().await.expect("Error synchronizing"); if let TestPath::RenewOfferTimeout = path { mocks::mock_time::set_time( - (EVENT_MATURITY as u64) + dlc_manager::manager::PEER_TIMEOUT + 2, + (EVENT_MATURITY as u64) + ddk_manager::manager::PEER_TIMEOUT + 2, ); - periodic_check(first.clone()); + periodic_check(first.clone()).await; assert_channel_state!(first, channel_id, Closed); } else { let (renew_accept, _) = second .lock() - .unwrap() + .await .accept_renew_offer(&channel_id) .expect("to be able to accept a settlement offer"); second_send .send(Some(Message::RenewAccept(renew_accept))) + .await .unwrap(); // Process Accept - first_receive.recv().expect("Error synchronizing"); + first_receive.recv().await.expect("Error synchronizing"); if let TestPath::RenewAcceptTimeout = path { mocks::mock_time::set_time( - (EVENT_MATURITY as u64) + dlc_manager::manager::PEER_TIMEOUT + 2, + (EVENT_MATURITY as u64) + ddk_manager::manager::PEER_TIMEOUT + 2, ); - periodic_check(second.clone()); + periodic_check(second.clone()).await; assert_channel_state!(second, channel_id, Closed); } else if let TestPath::RenewConfirmTimeout = path { // Process Confirm - second_receive.recv().expect("Error synchronizing"); + second_receive.recv().await.expect("Error synchronizing"); mocks::mock_time::set_time( - (EVENT_MATURITY as u64) + dlc_manager::manager::PEER_TIMEOUT + 2, + (EVENT_MATURITY as u64) + ddk_manager::manager::PEER_TIMEOUT + 2, ); - periodic_check(first.clone()); + periodic_check(first.clone()).await; assert_channel_state!(first, channel_id, Closed); } else if let TestPath::RenewFinalizeTimeout = path { //Process confirm - second_receive.recv().expect("Error synchronizing"); + second_receive.recv().await.expect("Error synchronizing"); // Process Finalize - first_receive.recv().expect("Error synchronizing"); + first_receive.recv().await.expect("Error synchronizing"); mocks::mock_time::set_time( - (EVENT_MATURITY as u64) + dlc_manager::manager::PEER_TIMEOUT + 2, + (EVENT_MATURITY as u64) + ddk_manager::manager::PEER_TIMEOUT + 2, ); - periodic_check(second.clone()); - generate_blocks(289); - periodic_check(second.clone()); + periodic_check(second.clone()).await; + generate_blocks(289).await; + periodic_check(second.clone()).await; assert_channel_state!(second, channel_id, Closed); } @@ -1303,69 +1381,71 @@ fn renew_timeout( } } -fn settle_timeout( +async fn settle_timeout( first: DlcParty, first_send: &Sender>, - first_receive: &Receiver<()>, + first_receive: &mut Receiver<()>, second: DlcParty, second_send: &Sender>, - second_receive: &Receiver<()>, + second_receive: &mut Receiver<()>, channel_id: ChannelId, path: TestPath, ) { let (settle_offer, _) = first .lock() - .unwrap() + .await .settle_offer(&channel_id, test_utils::ACCEPT_COLLATERAL) .expect("to be able to offer a settlement of the contract."); first_send .send(Some(Message::SettleOffer(settle_offer))) + .await .unwrap(); - second_receive.recv().expect("Error synchronizing"); + second_receive.recv().await.expect("Error synchronizing"); if let TestPath::SettleOfferTimeout = path { mocks::mock_time::set_time( - (EVENT_MATURITY as u64) + dlc_manager::manager::PEER_TIMEOUT + 2, + (EVENT_MATURITY as u64) + ddk_manager::manager::PEER_TIMEOUT + 2, ); - periodic_check(first.clone()); + periodic_check(first.clone()).await; assert_channel_state!(first, channel_id, Signed, Closing); } else { let (settle_accept, _) = second .lock() - .unwrap() + .await .accept_settle_offer(&channel_id) .expect("to be able to accept a settlement offer"); second_send .send(Some(Message::SettleAccept(settle_accept))) + .await .unwrap(); // Process Accept - first_receive.recv().expect("Error synchronizing"); + first_receive.recv().await.expect("Error synchronizing"); if let TestPath::SettleAcceptTimeout = path { mocks::mock_time::set_time( - (EVENT_MATURITY as u64) + dlc_manager::manager::PEER_TIMEOUT + 2, + (EVENT_MATURITY as u64) + ddk_manager::manager::PEER_TIMEOUT + 2, ); - periodic_check(second.clone()); + periodic_check(second.clone()).await; second .lock() - .unwrap() + .await .get_store() .get_channel(&channel_id) .unwrap(); assert_channel_state!(second, channel_id, Signed, Closing); } else if let TestPath::SettleConfirmTimeout = path { // Process Confirm - second_receive.recv().expect("Error synchronizing"); + second_receive.recv().await.expect("Error synchronizing"); mocks::mock_time::set_time( - (EVENT_MATURITY as u64) + dlc_manager::manager::PEER_TIMEOUT + 2, + (EVENT_MATURITY as u64) + ddk_manager::manager::PEER_TIMEOUT + 2, ); - periodic_check(first.clone()); + periodic_check(first.clone()).await; assert_channel_state!(first, channel_id, Signed, Closing); } diff --git a/dlc-manager/tests/manager_execution_tests.rs b/ddk-manager/tests/manager_execution_tests.rs similarity index 63% rename from dlc-manager/tests/manager_execution_tests.rs rename to ddk-manager/tests/manager_execution_tests.rs index cc2743e1..41716bbe 100644 --- a/dlc-manager/tests/manager_execution_tests.rs +++ b/ddk-manager/tests/manager_execution_tests.rs @@ -2,47 +2,49 @@ extern crate bitcoin_rpc_provider; extern crate bitcoin_test_utils; extern crate bitcoincore_rpc; extern crate bitcoincore_rpc_json; -extern crate dlc_manager; +extern crate ddk_manager; #[macro_use] #[allow(dead_code)] mod test_utils; use bitcoin::Amount; -use dlc_manager::payout_curve::PayoutFunctionPiece; +use ddk_manager::payout_curve::PayoutFunctionPiece; use electrs_blockchain_provider::ElectrsBlockchainProvider; use simple_wallet::SimpleWallet; use test_utils::*; use bitcoin_test_utils::rpc_helpers::init_clients; use bitcoincore_rpc::RpcApi; -use dlc_manager::contract::{numerical_descriptor::DifferenceParams, Contract}; -use dlc_manager::manager::Manager; -use dlc_manager::{Blockchain, Oracle, Storage, Wallet}; -use dlc_messages::oracle_msgs::OracleAttestation; -use dlc_messages::{AcceptDlc, OfferDlc, SignDlc}; -use dlc_messages::{CetAdaptorSignatures, Message}; +use ddk_manager::contract::{numerical_descriptor::DifferenceParams, Contract}; +use ddk_manager::manager::Manager; +use ddk_manager::{Blockchain, Oracle, Storage, Wallet}; +use ddk_messages::oracle_msgs::OracleAttestation; +use ddk_messages::{AcceptDlc, OfferDlc, SignDlc}; +use ddk_messages::{CetAdaptorSignatures, Message}; use lightning::ln::wire::Type; use lightning::util::ser::Writeable; use secp256k1_zkp::rand::{thread_rng, RngCore}; use secp256k1_zkp::{ecdsa::Signature, EcdsaAdaptorSignature}; -use serde_json::{from_str, to_writer_pretty}; +use serde_json::from_str; use std::collections::HashMap; +use std::future::Future; +use std::pin::Pin; use std::sync::{ atomic::{AtomicBool, Ordering}, - mpsc::channel, - Arc, Mutex, + Arc, }; -use std::thread; - +use tokio::sync::mpsc::channel; +use tokio::sync::Mutex; +use tokio::time::sleep; #[derive(serde::Serialize, serde::Deserialize)] struct TestVectorPart { message: T, #[cfg_attr( feature = "use-serde", serde( - serialize_with = "dlc_messages::serde_utils::serialize_hex", - deserialize_with = "dlc_messages::serde_utils::deserialize_hex_string" + serialize_with = "ddk_messages::serde_utils::serialize_hex", + deserialize_with = "ddk_messages::serde_utils::deserialize_hex_string" ) )] serialized: Vec, @@ -55,50 +57,64 @@ struct TestVector { sign_message: TestVectorPart, } -fn write_message(msg_name: &str, s: T) { +fn write_message(_msg_name: &str, s: T) { if std::env::var("GENERATE_TEST_VECTOR").is_ok() { let mut buf = Vec::new(); s.type_id().write(&mut buf).unwrap(); s.write(&mut buf).unwrap(); - let t = TestVectorPart { + let _t = TestVectorPart { message: s, serialized: buf, }; - to_writer_pretty( - &std::fs::File::create(format!("{}.json", msg_name)).unwrap(), - &t, - ) - .unwrap(); + // to_writer_pretty( + // &std::fs::File::create(format!("{}.json", msg_name)).unwrap(), + // &t, + // ) + // .unwrap(); } } -fn create_test_vector() { +async fn create_test_vector() { if std::env::var("GENERATE_TEST_VECTOR").is_ok() { - let test_vector = TestVector { - offer_message: from_str(&std::fs::read_to_string("offer_message.json").unwrap()) - .unwrap(), - accept_message: from_str(&std::fs::read_to_string("accept_message.json").unwrap()) - .unwrap(), - sign_message: from_str(&std::fs::read_to_string("sign_message.json").unwrap()).unwrap(), + let _test_vector = TestVector { + offer_message: from_str( + &tokio::fs::read_to_string("offer_message.json") + .await + .unwrap(), + ) + .unwrap(), + accept_message: from_str( + &tokio::fs::read_to_string("accept_message.json") + .await + .unwrap(), + ) + .unwrap(), + sign_message: from_str( + &tokio::fs::read_to_string("sign_message.json") + .await + .unwrap(), + ) + .unwrap(), }; - let file_name = std::env::var("TEST_VECTOR_OUTPUT_NAME") + let _file_name = std::env::var("TEST_VECTOR_OUTPUT_NAME") .unwrap_or_else(|_| "test_vector.json".to_string()); - to_writer_pretty(std::fs::File::create(file_name).unwrap(), &test_vector).unwrap(); + // to_writer_pretty(std::fs::File::create(file_name).unwrap(), &test_vector).unwrap(); } } macro_rules! periodic_check { ($d:expr, $id:expr, $p:ident) => { $d.lock() - .unwrap() + .await .periodic_check(true) + .await .expect("Periodic check error"); assert_contract_state!($d, $id, $p); }; } -fn numerical_common( +async fn numerical_common( nb_oracles: usize, threshold: usize, payout_function_pieces_cb: F, @@ -124,10 +140,11 @@ fn numerical_common( ), TestPath::Close, manual_close, - ); + ) + .await; } -fn numerical_polynomial_common( +async fn numerical_polynomial_common( nb_oracles: usize, threshold: usize, difference_params: Option, @@ -139,10 +156,11 @@ fn numerical_polynomial_common( get_polynomial_payout_curve_pieces, difference_params, manual_close, - ); + ) + .await; } -fn numerical_common_diff_nb_digits( +async fn numerical_common_diff_nb_digits( nb_oracles: usize, threshold: usize, difference_params: Option, @@ -171,7 +189,8 @@ fn numerical_common_diff_nb_digits( ), TestPath::Close, manual_close, - ); + ) + .await; } #[derive(Eq, PartialEq, Clone)] @@ -184,330 +203,344 @@ enum TestPath { BadSignRefundSignature, } -#[test] +#[tokio::test] #[ignore] -fn single_oracle_numerical_test() { - numerical_polynomial_common(1, 1, None, false); +async fn single_oracle_numerical_test() { + numerical_polynomial_common(1, 1, None, false).await; } -#[test] +#[tokio::test] #[ignore] -fn single_oracle_numerical_manual_test() { - numerical_polynomial_common(1, 1, None, true); +async fn single_oracle_numerical_manual_test() { + numerical_polynomial_common(1, 1, None, true).await; } -#[test] +#[tokio::test] #[ignore] -fn single_oracle_numerical_hyperbola_test() { - numerical_common(1, 1, get_hyperbola_payout_curve_pieces, None, false); +async fn single_oracle_numerical_hyperbola_test() { + numerical_common(1, 1, get_hyperbola_payout_curve_pieces, None, false).await; } -#[test] +#[tokio::test] #[ignore] -fn three_of_three_oracle_numerical_test() { - numerical_polynomial_common(3, 3, None, false); +async fn three_of_three_oracle_numerical_test() { + numerical_polynomial_common(3, 3, None, false).await; } -#[test] +#[tokio::test] #[ignore] -fn two_of_five_oracle_numerical_test() { - numerical_polynomial_common(5, 2, None, false); +async fn two_of_five_oracle_numerical_test() { + numerical_polynomial_common(5, 2, None, false).await; } -#[test] +#[tokio::test] #[ignore] -fn two_of_five_oracle_numerical_manual_test() { - numerical_polynomial_common(5, 2, None, true); +async fn two_of_five_oracle_numerical_manual_test() { + numerical_polynomial_common(5, 2, None, true).await; } -#[test] +#[tokio::test] #[ignore] -fn three_of_three_oracle_numerical_with_diff_test() { - numerical_polynomial_common(3, 3, Some(get_difference_params()), false); +async fn three_of_three_oracle_numerical_with_diff_test() { + numerical_polynomial_common(3, 3, Some(get_difference_params()), false).await; } -#[test] +#[tokio::test] #[ignore] -fn two_of_five_oracle_numerical_with_diff_test() { - numerical_polynomial_common(5, 2, Some(get_difference_params()), false); +async fn two_of_five_oracle_numerical_with_diff_test() { + numerical_polynomial_common(5, 2, Some(get_difference_params()), false).await; } -#[test] +#[tokio::test] #[ignore] -fn three_of_five_oracle_numerical_with_diff_test() { - numerical_polynomial_common(5, 3, Some(get_difference_params()), false); +async fn three_of_five_oracle_numerical_with_diff_test() { + numerical_polynomial_common(5, 3, Some(get_difference_params()), false).await; } -#[test] +#[tokio::test] #[ignore] -fn three_of_five_oracle_numerical_with_diff_manual_test() { - numerical_polynomial_common(5, 3, Some(get_difference_params()), true); +async fn three_of_five_oracle_numerical_with_diff_manual_test() { + numerical_polynomial_common(5, 3, Some(get_difference_params()), true).await; } -#[test] +#[tokio::test] #[ignore] -fn enum_single_oracle_test() { - manager_execution_test(get_enum_test_params(1, 1, None), TestPath::Close, false); +async fn enum_single_oracle_test() { + manager_execution_test(get_enum_test_params(1, 1, None), TestPath::Close, false).await; } -#[test] +#[tokio::test] #[ignore] -fn enum_single_oracle_manual_test() { - manager_execution_test(get_enum_test_params(1, 1, None), TestPath::Close, true); +async fn enum_single_oracle_manual_test() { + manager_execution_test(get_enum_test_params(1, 1, None), TestPath::Close, true).await; } -#[test] +#[tokio::test] #[ignore] -fn enum_3_of_3_test() { - manager_execution_test(get_enum_test_params(3, 3, None), TestPath::Close, false); +async fn enum_3_of_3_test() { + manager_execution_test(get_enum_test_params(3, 3, None), TestPath::Close, false).await; } -#[test] +#[tokio::test] #[ignore] -fn enum_3_of_3_manual_test() { - manager_execution_test(get_enum_test_params(3, 3, None), TestPath::Close, true); +async fn enum_3_of_3_manual_test() { + manager_execution_test(get_enum_test_params(3, 3, None), TestPath::Close, true).await; } -#[test] +#[tokio::test] #[ignore] -fn enum_3_of_5_test() { - manager_execution_test(get_enum_test_params(5, 3, None), TestPath::Close, false); +async fn enum_3_of_5_test() { + manager_execution_test(get_enum_test_params(5, 3, None), TestPath::Close, false).await; } -#[test] +#[tokio::test] #[ignore] -fn enum_3_of_5_manual_test() { - manager_execution_test(get_enum_test_params(5, 3, None), TestPath::Close, true); +async fn enum_3_of_5_manual_test() { + manager_execution_test(get_enum_test_params(5, 3, None), TestPath::Close, true).await; } -#[test] +#[tokio::test] #[ignore] -fn enum_and_numerical_with_diff_3_of_5_test() { +async fn enum_and_numerical_with_diff_3_of_5_test() { manager_execution_test( get_enum_and_numerical_test_params(5, 3, true, Some(get_difference_params())), TestPath::Close, false, - ); + ) + .await; } -#[test] +#[tokio::test] #[ignore] -fn enum_and_numerical_with_diff_3_of_5_manual_test() { +async fn enum_and_numerical_with_diff_3_of_5_manual_test() { manager_execution_test( get_enum_and_numerical_test_params(5, 3, true, Some(get_difference_params())), TestPath::Close, true, - ); + ) + .await; } -#[test] +#[tokio::test] #[ignore] -fn enum_and_numerical_with_diff_5_of_5_test() { +async fn enum_and_numerical_with_diff_5_of_5_test() { manager_execution_test( get_enum_and_numerical_test_params(5, 5, true, Some(get_difference_params())), TestPath::Close, false, - ); + ) + .await; } -#[test] +#[tokio::test] #[ignore] -fn enum_and_numerical_with_diff_5_of_5_manual_test() { +async fn enum_and_numerical_with_diff_5_of_5_manual_test() { manager_execution_test( get_enum_and_numerical_test_params(5, 5, true, Some(get_difference_params())), TestPath::Close, true, - ); + ) + .await; } -#[test] +#[tokio::test] #[ignore] -fn enum_and_numerical_3_of_5_test() { +async fn enum_and_numerical_3_of_5_test() { manager_execution_test( get_enum_and_numerical_test_params(5, 3, false, None), TestPath::Close, false, - ); + ) + .await; } -#[test] +#[tokio::test] #[ignore] -fn enum_and_numerical_3_of_5_manual_test() { +async fn enum_and_numerical_3_of_5_manual_test() { manager_execution_test( get_enum_and_numerical_test_params(5, 3, false, None), TestPath::Close, true, - ); + ) + .await; } -#[test] +#[tokio::test] #[ignore] -fn enum_and_numerical_5_of_5_test() { +async fn enum_and_numerical_5_of_5_test() { manager_execution_test( get_enum_and_numerical_test_params(5, 5, false, None), TestPath::Close, false, - ); + ) + .await; } -#[test] +#[tokio::test] #[ignore] -fn enum_and_numerical_5_of_5_manual_test() { +async fn enum_and_numerical_5_of_5_manual_test() { manager_execution_test( get_enum_and_numerical_test_params(5, 5, false, None), TestPath::Close, true, - ); + ) + .await; } -#[test] +#[tokio::test] #[ignore] -fn enum_single_oracle_refund_test() { +async fn enum_single_oracle_refund_test() { manager_execution_test( get_enum_test_params(1, 1, Some(get_enum_oracles(1, 0))), TestPath::Refund, false, - ); + ) + .await; } -#[test] +#[tokio::test] #[ignore] -fn enum_single_oracle_refund_manual_test() { +async fn enum_single_oracle_refund_manual_test() { manager_execution_test( get_enum_test_params(1, 1, Some(get_enum_oracles(1, 0))), TestPath::Refund, true, - ); + ) + .await; } -#[test] +#[tokio::test] #[ignore] -fn enum_single_oracle_bad_accept_cet_sig_test() { +async fn enum_single_oracle_bad_accept_cet_sig_test() { manager_execution_test( get_enum_test_params(1, 1, Some(get_enum_oracles(1, 0))), TestPath::BadAcceptCetSignature, false, - ); + ) + .await; } -#[test] +#[tokio::test] #[ignore] -fn enum_single_oracle_bad_accept_refund_sig_test() { +async fn enum_single_oracle_bad_accept_refund_sig_test() { manager_execution_test( get_enum_test_params(1, 1, Some(get_enum_oracles(1, 0))), TestPath::BadAcceptRefundSignature, false, - ); + ) + .await; } -#[test] +#[tokio::test] #[ignore] -fn enum_single_oracle_bad_sign_cet_sig_test() { +async fn enum_single_oracle_bad_sign_cet_sig_test() { manager_execution_test( get_enum_test_params(1, 1, Some(get_enum_oracles(1, 0))), TestPath::BadSignCetSignature, false, - ); + ) + .await; } -#[test] +#[tokio::test] #[ignore] -fn enum_single_oracle_bad_sign_refund_sig_test() { +async fn enum_single_oracle_bad_sign_refund_sig_test() { manager_execution_test( get_enum_test_params(1, 1, Some(get_enum_oracles(1, 0))), TestPath::BadSignRefundSignature, false, - ); + ) + .await; } -#[test] +#[tokio::test] #[ignore] -fn two_of_two_oracle_numerical_diff_nb_digits_test() { - numerical_common_diff_nb_digits(2, 2, None, false, false); +async fn two_of_two_oracle_numerical_diff_nb_digits_test() { + numerical_common_diff_nb_digits(2, 2, None, false, false).await; } -#[test] +#[tokio::test] #[ignore] -fn two_of_two_oracle_numerical_diff_nb_digits_manual_test() { - numerical_common_diff_nb_digits(2, 2, None, false, true); +async fn two_of_two_oracle_numerical_diff_nb_digits_manual_test() { + numerical_common_diff_nb_digits(2, 2, None, false, true).await; } -#[test] +#[tokio::test] #[ignore] -fn two_of_five_oracle_numerical_diff_nb_digits_test() { - numerical_common_diff_nb_digits(5, 2, None, false, false); +async fn two_of_five_oracle_numerical_diff_nb_digits_test() { + numerical_common_diff_nb_digits(5, 2, None, false, false).await; } -#[test] +#[tokio::test] #[ignore] -fn two_of_five_oracle_numerical_diff_nb_digits_manual_test() { - numerical_common_diff_nb_digits(5, 2, None, false, true); +async fn two_of_five_oracle_numerical_diff_nb_digits_manual_test() { + numerical_common_diff_nb_digits(5, 2, None, false, true).await; } -#[test] +#[tokio::test] #[ignore] -fn two_of_two_oracle_numerical_with_diff_diff_nb_digits_test() { - numerical_common_diff_nb_digits(2, 2, Some(get_difference_params()), false, false); +async fn two_of_two_oracle_numerical_with_diff_diff_nb_digits_test() { + numerical_common_diff_nb_digits(2, 2, Some(get_difference_params()), false, false).await; } -#[test] +#[tokio::test] #[ignore] -fn three_of_three_oracle_numerical_with_diff_diff_nb_digits_test() { - numerical_common_diff_nb_digits(3, 3, Some(get_difference_params()), false, false); +async fn three_of_three_oracle_numerical_with_diff_diff_nb_digits_test() { + numerical_common_diff_nb_digits(3, 3, Some(get_difference_params()), false, false).await; } -#[test] +#[tokio::test] #[ignore] -fn two_of_five_oracle_numerical_with_diff_diff_nb_digits_test() { - numerical_common_diff_nb_digits(5, 2, Some(get_difference_params()), false, false); +async fn two_of_five_oracle_numerical_with_diff_diff_nb_digits_test() { + numerical_common_diff_nb_digits(5, 2, Some(get_difference_params()), false, false).await; } -#[test] +#[tokio::test] #[ignore] -fn two_of_two_oracle_numerical_with_diff_diff_nb_digits_max_value_test() { - numerical_common_diff_nb_digits(2, 2, Some(get_difference_params()), true, false); +async fn two_of_two_oracle_numerical_with_diff_diff_nb_digits_max_value_test() { + numerical_common_diff_nb_digits(2, 2, Some(get_difference_params()), true, false).await; } -#[test] +#[tokio::test] #[ignore] -fn two_of_three_oracle_numerical_with_diff_diff_nb_digits_max_value_test() { - numerical_common_diff_nb_digits(3, 2, Some(get_difference_params()), true, false); +async fn two_of_three_oracle_numerical_with_diff_diff_nb_digits_max_value_test() { + numerical_common_diff_nb_digits(3, 2, Some(get_difference_params()), true, false).await; } -#[test] +#[tokio::test] #[ignore] -fn two_of_five_oracle_numerical_with_diff_diff_nb_digits_max_value_test() { - numerical_common_diff_nb_digits(5, 2, Some(get_difference_params()), true, false); +async fn two_of_five_oracle_numerical_with_diff_diff_nb_digits_max_value_test() { + numerical_common_diff_nb_digits(5, 2, Some(get_difference_params()), true, false).await; } -#[test] +#[tokio::test] #[ignore] -fn two_of_five_oracle_numerical_with_diff_diff_nb_digits_max_value_manual_test() { - numerical_common_diff_nb_digits(5, 2, Some(get_difference_params()), true, true); +async fn two_of_five_oracle_numerical_with_diff_diff_nb_digits_max_value_manual_test() { + numerical_common_diff_nb_digits(5, 2, Some(get_difference_params()), true, true).await; } -#[test] +#[tokio::test] #[ignore] -fn two_of_two_oracle_numerical_diff_nb_digits_max_value_test() { - numerical_common_diff_nb_digits(2, 2, None, true, false); +async fn two_of_two_oracle_numerical_diff_nb_digits_max_value_test() { + numerical_common_diff_nb_digits(2, 2, None, true, false).await; } -#[test] +#[tokio::test] #[ignore] -fn two_of_three_oracle_numerical_diff_nb_digits_max_value_test() { - numerical_common_diff_nb_digits(3, 2, None, true, false); +async fn two_of_three_oracle_numerical_diff_nb_digits_max_value_test() { + numerical_common_diff_nb_digits(3, 2, None, true, false).await; } -#[test] +#[tokio::test] #[ignore] -fn two_of_five_oracle_numerical_diff_nb_digits_max_value_test() { - numerical_common_diff_nb_digits(5, 2, None, true, false); +async fn two_of_five_oracle_numerical_diff_nb_digits_max_value_test() { + numerical_common_diff_nb_digits(5, 2, None, true, false).await; } -#[test] +#[tokio::test] #[ignore] -fn two_of_five_oracle_numerical_diff_nb_digits_max_value_manual_test() { - numerical_common_diff_nb_digits(5, 2, None, true, true); +async fn two_of_five_oracle_numerical_diff_nb_digits_max_value_manual_test() { + numerical_common_diff_nb_digits(5, 2, None, true, true).await; } fn alter_adaptor_sig(input: &mut CetAdaptorSignatures) { @@ -530,24 +563,21 @@ fn alter_refund_sig(refund_signature: &Signature) -> Signature { Signature::from_compact(©).unwrap() } -fn get_attestations(test_params: &TestParams) -> Vec<(usize, OracleAttestation)> { +async fn get_attestations(test_params: &TestParams) -> Vec<(usize, OracleAttestation)> { + let mut attestations = Vec::new(); for contract_info in test_params.contract_input.contract_infos.iter() { - let attestations: Vec<_> = contract_info - .oracles - .public_keys - .iter() - .enumerate() - .filter_map(|(i, pk)| { - let oracle = test_params - .oracles - .iter() - .find(|x| x.get_public_key() == *pk); - - oracle - .and_then(|o| o.get_attestation(&contract_info.oracles.event_id).ok()) - .map(|a| (i, a)) - }) - .collect(); + attestations.clear(); + for (i, pk) in contract_info.oracles.public_keys.iter().enumerate() { + let oracle = test_params + .oracles + .iter() + .find(|x| x.get_public_key() == *pk); + if let Some(o) = oracle { + if let Ok(attestation) = o.get_attestation(&contract_info.oracles.event_id).await { + attestations.push((i, attestation)); + } + } + } if attestations.len() >= contract_info.oracles.threshold as usize { return attestations; } @@ -556,14 +586,15 @@ fn get_attestations(test_params: &TestParams) -> Vec<(usize, OracleAttestation)> panic!("No attestations found"); } -fn manager_execution_test(test_params: TestParams, path: TestPath, manual_close: bool) { +async fn manager_execution_test(test_params: TestParams, path: TestPath, manual_close: bool) { env_logger::try_init().ok(); - let (alice_send, bob_receive) = channel::>(); - let (bob_send, alice_receive) = channel::>(); - let (sync_send, sync_receive) = channel::<()>(); + let (alice_send, mut bob_receive) = channel::>(100); + let (bob_send, mut alice_receive) = channel::>(100); + let (sync_send, mut sync_receive) = channel::<()>(100); let alice_sync_send = sync_send.clone(); let bob_sync_send = sync_send; let (_, _, sink_rpc) = init_clients(); + let sink = Arc::new(sink_rpc); let mut alice_oracles = HashMap::with_capacity(1); let mut bob_oracles = HashMap::with_capacity(1); @@ -599,55 +630,55 @@ fn manager_execution_test(test_params: TestParams, path: TestPath, manual_close: let alice_fund_address = alice_wallet.get_new_address().unwrap(); let bob_fund_address = bob_wallet.get_new_address().unwrap(); - sink_rpc - .send_to_address( - &alice_fund_address, - Amount::from_btc(2.0).unwrap(), - None, - None, - None, - None, - None, - None, - ) - .unwrap(); - - sink_rpc - .send_to_address( - &bob_fund_address, - Amount::from_btc(2.0).unwrap(), - None, - None, - None, - None, - None, - None, - ) - .unwrap(); - - let generate_blocks = |nb_blocks: u64| { - let prev_blockchain_height = electrs.get_blockchain_height().unwrap(); - - let sink_address = sink_rpc - .get_new_address(None, None) - .expect("RPC Error") - .assume_checked(); - sink_rpc - .generate_to_address(nb_blocks, &sink_address) - .expect("RPC Error"); - - // Wait for electrs to have processed the new blocks - let mut cur_blockchain_height = prev_blockchain_height; - while cur_blockchain_height < prev_blockchain_height + nb_blocks { - std::thread::sleep(std::time::Duration::from_millis(200)); - cur_blockchain_height = electrs.get_blockchain_height().unwrap(); - } + sink.send_to_address( + &alice_fund_address, + Amount::from_btc(2.0).unwrap(), + None, + None, + None, + None, + None, + None, + ) + .unwrap(); + + sink.send_to_address( + &bob_fund_address, + Amount::from_btc(2.0).unwrap(), + None, + None, + None, + None, + None, + None, + ) + .unwrap(); + + let generate_blocks = |nb_blocks: u64| -> Pin + Send>> { + let electrs_clone = electrs.clone(); + let sink_clone = sink.clone(); + Box::pin(async move { + let prev_blockchain_height = electrs_clone.get_blockchain_height().await.unwrap(); + let sink_address = sink_clone + .get_new_address(None, None) + .expect("RPC Error") + .assume_checked(); + sink_clone + .generate_to_address(nb_blocks, &sink_address) + .expect("RPC Error"); + // Wait for electrs to have processed the new blocks + let mut cur_blockchain_height = prev_blockchain_height; + while cur_blockchain_height < prev_blockchain_height + nb_blocks { + sleep(std::time::Duration::from_millis(200)).await; + cur_blockchain_height = electrs_clone.get_blockchain_height().await.unwrap(); + } + }) }; - generate_blocks(6); + generate_blocks(6).await; - refresh_wallet(&alice_wallet, 200000000); - refresh_wallet(&bob_wallet, 200000000); + refresh_wallet(&alice_wallet, 200000000).await; + refresh_wallet(&bob_wallet, 200000000).await; let alice_manager = Arc::new(Mutex::new( Manager::new( @@ -659,6 +690,7 @@ fn manager_execution_test(test_params: TestParams, path: TestPath, manual_close: Arc::clone(&mock_time), Arc::clone(&electrs), ) + .await .unwrap(), )); @@ -675,6 +707,7 @@ fn manager_execution_test(test_params: TestParams, path: TestPath, manual_close: Arc::clone(&mock_time), Arc::clone(&electrs), ) + .await .unwrap(), )); @@ -734,29 +767,34 @@ fn manager_execution_test(test_params: TestParams, path: TestPath, manual_close: let offer_msg = bob_manager_send .lock() - .unwrap() + .await .send_offer( &test_params.contract_input, "0218845781f631c48f1c9709e23092067d06837f30aa0cd0544ac887fe91ddd166" .parse() .unwrap(), ) + .await .expect("Send offer error"); write_message("offer_message", offer_msg.clone()); let temporary_contract_id = offer_msg.temporary_contract_id; - bob_send.send(Some(Message::Offer(offer_msg))).unwrap(); + bob_send + .send(Some(Message::Offer(offer_msg))) + .await + .unwrap(); assert_contract_state!(bob_manager_send, temporary_contract_id, Offered); - sync_receive.recv().expect("Error synchronizing"); + sync_receive.recv().await.expect("Error synchronizing"); assert_contract_state!(alice_manager_send, temporary_contract_id, Offered); let (contract_id, _, mut accept_msg) = alice_manager_send .lock() - .unwrap() + .await .accept_contract_offer(&temporary_contract_id) + .await .expect("Error accepting contract offer"); write_message("accept_message", accept_msg.clone()); @@ -775,33 +813,42 @@ fn manager_execution_test(test_params: TestParams, path: TestPath, manual_close: _ => {} }; bob_expect_error.store(true, Ordering::Relaxed); - alice_send.send(Some(Message::Accept(accept_msg))).unwrap(); - sync_receive.recv().expect("Error synchronizing"); + alice_send + .send(Some(Message::Accept(accept_msg))) + .await + .unwrap(); + sync_receive.recv().await.expect("Error synchronizing"); assert_contract_state!(bob_manager_send, temporary_contract_id, FailedAccept); } TestPath::BadSignCetSignature | TestPath::BadSignRefundSignature => { alice_expect_error.store(true, Ordering::Relaxed); - alice_send.send(Some(Message::Accept(accept_msg))).unwrap(); + alice_send + .send(Some(Message::Accept(accept_msg))) + .await + .unwrap(); // Bob receives accept message - sync_receive.recv().expect("Error synchronizing"); + sync_receive.recv().await.expect("Error synchronizing"); // Alice receives sign message - sync_receive.recv().expect("Error synchronizing"); + sync_receive.recv().await.expect("Error synchronizing"); assert_contract_state!(alice_manager_send, contract_id, FailedSign); } TestPath::Close | TestPath::Refund => { - alice_send.send(Some(Message::Accept(accept_msg))).unwrap(); - sync_receive.recv().expect("Error synchronizing"); + alice_send + .send(Some(Message::Accept(accept_msg))) + .await + .unwrap(); + sync_receive.recv().await.expect("Error synchronizing"); assert_contract_state!(bob_manager_send, contract_id, Signed); // Should not change state and should not error periodic_check!(bob_manager_send, contract_id, Signed); - sync_receive.recv().expect("Error synchronizing"); + sync_receive.recv().await.expect("Error synchronizing"); assert_contract_state!(alice_manager_send, contract_id, Signed); - generate_blocks(6); + generate_blocks(6).await; periodic_check!(alice_manager_send, contract_id, Confirmed); periodic_check!(bob_manager_send, contract_id, Confirmed); @@ -831,15 +878,16 @@ fn manager_execution_test(test_params: TestParams, path: TestPath, manual_close: if manual_close { periodic_check!(first, contract_id, Confirmed); - let attestations = get_attestations(&test_params); + let attestations = get_attestations(&test_params).await; - let f = first.lock().unwrap(); + let f = first.lock().await; let contract = f .close_confirmed_contract(&contract_id, attestations) + .await .expect("Error closing contract"); if let Contract::PreClosed(contract) = contract { - let mut s = second.lock().unwrap(); + let mut s = second.lock().await; let second_contract = s.get_store().get_contract(&contract_id).unwrap().unwrap(); if let Contract::Confirmed(signed) = second_contract { @@ -861,7 +909,7 @@ fn manager_execution_test(test_params: TestParams, path: TestPath, manual_close: // mine blocks for the CET to be confirmed if let Some(b) = blocks { - generate_blocks(b as u64); + generate_blocks(b as u64).await; } // Randomly check with or without having the CET mined @@ -880,16 +928,16 @@ fn manager_execution_test(test_params: TestParams, path: TestPath, manual_close: periodic_check!(second, contract_id, Confirmed); mocks::mock_time::set_time( - ((EVENT_MATURITY + dlc_manager::manager::REFUND_DELAY) as u64) + 1, + ((EVENT_MATURITY + ddk_manager::manager::REFUND_DELAY) as u64) + 1, ); - generate_blocks(10); + generate_blocks(10).await; periodic_check!(first, contract_id, Refunded); // Randomly check with or without having the Refund mined. if thread_rng().next_u32() % 2 == 0 { - generate_blocks(1); + generate_blocks(1).await; } periodic_check!(second, contract_id, Refunded); @@ -899,11 +947,11 @@ fn manager_execution_test(test_params: TestParams, path: TestPath, manual_close: } } - alice_send.send(None).unwrap(); - bob_send.send(None).unwrap(); + alice_send.send(None).await.unwrap(); + bob_send.send(None).await.unwrap(); - alice_handle.join().unwrap(); - bob_handle.join().unwrap(); + alice_handle.await.unwrap(); + bob_handle.await.unwrap(); - create_test_vector(); + create_test_vector().await; } diff --git a/dlc-manager/tests/test_utils.rs b/ddk-manager/tests/test_utils.rs similarity index 88% rename from dlc-manager/tests/test_utils.rs rename to ddk-manager/tests/test_utils.rs index bb1a7cf8..fc1418b0 100644 --- a/dlc-manager/tests/test_utils.rs +++ b/ddk-manager/tests/test_utils.rs @@ -4,16 +4,16 @@ extern crate bitcoin_rpc_provider; extern crate bitcoin_test_utils; extern crate bitcoincore_rpc; extern crate bitcoincore_rpc_json; -extern crate dlc_manager; +extern crate ddk_manager; use std::ops::Deref; -use dlc::{EnumerationPayout, Payout}; -use dlc_manager::payout_curve::{ +use ddk_dlc::{EnumerationPayout, Payout}; +use ddk_manager::payout_curve::{ PayoutFunction, PayoutFunctionPiece, PayoutPoint, PolynomialPayoutCurvePiece, RoundingInterval, RoundingIntervals, }; -use dlc_manager::{ +use ddk_manager::{ contract::{ contract_input::{ContractInput, ContractInputInfo, OracleInput}, enum_descriptor::EnumDescriptor, @@ -22,11 +22,11 @@ use dlc_manager::{ }, payout_curve::HyperbolaPayoutCurvePiece, }; -use dlc_manager::{Blockchain, Oracle}; -use dlc_messages::oracle_msgs::{ +use ddk_manager::{Blockchain, Oracle}; +use ddk_messages::oracle_msgs::{ DigitDecompositionEventDescriptor, EnumEventDescriptor, EventDescriptor, }; -use dlc_trie::{digit_decomposition::decompose_value, OracleNumericInfo}; +use ddk_trie::{digit_decomposition::decompose_value, OracleNumericInfo}; use mocks::mock_oracle_provider::MockOracle; use secp256k1_zkp::rand::{seq::SliceRandom, thread_rng, RngCore}; use simple_wallet::{WalletBlockchainProvider, WalletStorage}; @@ -46,39 +46,46 @@ pub const ROUNDING_MOD: u64 = 1; #[macro_export] macro_rules! receive_loop { ($receive:expr, $manager:expr, $send:expr, $expect_err:expr, $sync_send:expr, $rcv_callback: expr, $msg_callback: expr) => { - thread::spawn(move || loop { - match $receive.recv() { - Ok(Some(msg)) => match $manager.lock().unwrap().on_dlc_message( - &msg, - "0218845781f631c48f1c9709e23092067d06837f30aa0cd0544ac887fe91ddd166" - .parse() - .unwrap(), - ) { - Ok(opt) => { - if $expect_err.load(Ordering::Relaxed) != false { - panic!("Expected error not raised"); - } - match opt { - Some(msg) => { - let msg_opt = $rcv_callback(msg); - if let Some(msg) = msg_opt { - #[allow(clippy::redundant_closure_call)] - $msg_callback(&msg); - (&$send).send(Some(msg)).expect("Error sending"); + tokio::spawn(async move { + loop { + match $receive.recv().await { + Some(Some(msg)) => match $manager + .lock() + .await + .on_dlc_message( + &msg, + "0218845781f631c48f1c9709e23092067d06837f30aa0cd0544ac887fe91ddd166" + .parse() + .unwrap(), + ) + .await + { + Ok(opt) => { + if $expect_err.load(Ordering::Relaxed) != false { + panic!("Expected error not raised"); + } + match opt { + Some(msg) => { + let msg_opt = $rcv_callback(msg); + if let Some(msg) = msg_opt { + #[allow(clippy::redundant_closure_call)] + $msg_callback(&msg); + (&$send).send(Some(msg)).await.expect("Error sending"); + } } + None => {} } - None => {} } - } - Err(e) => { - if $expect_err.load(Ordering::Relaxed) != true { - panic!("Unexpected error {}", e); + Err(e) => { + if $expect_err.load(Ordering::Relaxed) != true { + panic!("Unexpected error {}", e); + } } - } - }, - Ok(None) | Err(_) => return, - }; - $sync_send.send(()).expect("Error syncing"); + }, + None | Some(None) => return, + }; + $sync_send.send(()).await.expect("Error syncing"); + } }) }; } @@ -104,7 +111,7 @@ macro_rules! assert_contract_state { ($d:expr, $id:expr, $p:ident) => { let res = $d .lock() - .unwrap() + .await .get_store() .get_contract(&$id) .expect("Could not retrieve contract"); @@ -146,7 +153,7 @@ macro_rules! write_channel { #[macro_export] macro_rules! assert_channel_state { ($d:expr, $id:expr, $p:ident $(, $s: ident)?) => {{ - assert_channel_state_unlocked!($d.lock().unwrap(), $id, $p $(, $s)?) + assert_channel_state_unlocked!($d.lock().await, $id, $p $(, $s)?) }}; } @@ -158,7 +165,7 @@ macro_rules! assert_channel_state_unlocked { .get_channel(&$id) .expect("Could not retrieve channel"); if let Some(Channel::$p(c)) = res { - $(if let dlc_manager::channel::signed_channel::SignedChannelState::$s { .. } = c.state { + $(if let ddk_manager::channel::signed_channel::SignedChannelState::$s { .. } = c.state { } else { panic!("Unexpected signed channel state {:?}", c.state); })? @@ -568,7 +575,7 @@ pub fn get_variable_oracle_numeric_infos(nb_digits: &[usize]) -> OracleNumericIn } } -pub fn refresh_wallet( +pub async fn refresh_wallet( wallet: &simple_wallet::SimpleWallet, expected_funds: u64, ) where @@ -581,7 +588,7 @@ pub fn refresh_wallet( panic!("Wallet refresh taking too long.") } std::thread::sleep(std::time::Duration::from_millis(200)); - wallet.refresh().unwrap(); + wallet.refresh().await.unwrap(); retry += 1; } } diff --git a/dlc-messages/CHANGELOG.md b/ddk-messages/CHANGELOG.md similarity index 100% rename from dlc-messages/CHANGELOG.md rename to ddk-messages/CHANGELOG.md diff --git a/dlc-messages/Cargo.toml b/ddk-messages/Cargo.toml similarity index 61% rename from dlc-messages/Cargo.toml rename to ddk-messages/Cargo.toml index 27ac3fdb..36cbfbba 100644 --- a/dlc-messages/Cargo.toml +++ b/ddk-messages/Cargo.toml @@ -1,28 +1,28 @@ [package] -authors = ["Crypto Garage"] +authors = ["Crypto Garage", "benny b "] description = "Structs and serialization for the Discreet Log Contract (DLC) protocol." -homepage = "https://github.com/p2pderivatives/rust-dlc" +homepage = "https://github.com/bennyhodl/rust-dlc" license-file = "../LICENSE" -name = "dlc-messages" -repository = "https://github.com/p2pderivatives/rust-dlc/tree/master/dlc-messages" +name = "ddk-messages" +repository = "https://github.com/bennyhodl/rust-dlc/tree/master/dlc-messages" version = "0.7.0" [features] default = ["std"] -std = ["dlc/std", "bitcoin/std", "lightning/std"] -no-std = ["dlc/no-std", "lightning/no-std"] +std = ["ddk-dlc/std", "bitcoin/std", "lightning/std"] +no-std = ["ddk-dlc/no-std", "lightning/no-std"] use-serde = ["serde", "secp256k1-zkp/serde", "bitcoin/serde"] [dependencies] bitcoin = { version = "0.32.2", default-features = false } -dlc = { version = "0.7.0", path = "../dlc", default-features = false } +ddk-dlc = { version = "0.7.0", path = "../ddk-dlc", default-features = false } lightning = { version = "0.0.125", default-features = false } secp256k1-zkp = {version = "0.11.0"} serde = {version = "1.0", features = ["derive"], optional = true} [dev-dependencies] bitcoin = { version = "0.32.2", default-features = false, features = ["serde"] } -dlc-messages = {path = "./", default-features = false, features = ["use-serde"]} +ddk-messages = {path = "./", default-features = false, features = ["use-serde"]} secp256k1-zkp = {version = "0.11.0", features = ["serde", "global-context"]} serde = {version = "1.0", features = ["derive"]} serde_json = "1.0" diff --git a/dlc-messages/Readme.md b/ddk-messages/Readme.md similarity index 100% rename from dlc-messages/Readme.md rename to ddk-messages/Readme.md diff --git a/dlc-messages/src/channel.rs b/ddk-messages/src/channel.rs similarity index 99% rename from dlc-messages/src/channel.rs rename to ddk-messages/src/channel.rs index c631a154..98346989 100644 --- a/dlc-messages/src/channel.rs +++ b/ddk-messages/src/channel.rs @@ -1,7 +1,7 @@ //! Contains messages used for the establishment and update of DLC channels. use bitcoin::ScriptBuf; -use dlc::Error; +use ddk_dlc::Error; use lightning::ln::msgs::DecodeError; use lightning::util::ser::{Readable, Writeable, Writer}; use secp256k1_zkp::{ diff --git a/dlc-messages/src/compatibility_tests.rs b/ddk-messages/src/compatibility_tests.rs similarity index 99% rename from dlc-messages/src/compatibility_tests.rs rename to ddk-messages/src/compatibility_tests.rs index 6704e5b3..fbf86f04 100644 --- a/dlc-messages/src/compatibility_tests.rs +++ b/ddk-messages/src/compatibility_tests.rs @@ -1,6 +1,6 @@ extern crate bitcoin; extern crate bitcoin_test_utils; -extern crate dlc; +extern crate ddk_dlc; extern crate lightning; extern crate secp256k1; @@ -12,7 +12,7 @@ use bitcoin::consensus::encode::Decodable; use bitcoin::SigHashType; use bitcoin::{Address, OutPoint, Script, Transaction, Txid, VarInt}; use bitcoin_test_utils::*; -use dlc::{DlcTransactions, OracleInfo as DlcOracleInfo, PartyParams, Payout, TxInputInfo}; +use ddk_dlc::{DlcTransactions, OracleInfo as DlcOracleInfo, PartyParams, Payout, TxInputInfo}; use lightning::ln::wire::{write, Encode}; use lightning::util::ser::Writeable; use secp256k1::{ diff --git a/dlc-messages/src/contract_msgs.rs b/ddk-messages/src/contract_msgs.rs similarity index 100% rename from dlc-messages/src/contract_msgs.rs rename to ddk-messages/src/contract_msgs.rs diff --git a/dlc-messages/src/lib.rs b/ddk-messages/src/lib.rs similarity index 99% rename from dlc-messages/src/lib.rs rename to ddk-messages/src/lib.rs index 719350af..04846540 100644 --- a/dlc-messages/src/lib.rs +++ b/ddk-messages/src/lib.rs @@ -11,7 +11,7 @@ #![deny(missing_docs)] extern crate bitcoin; -extern crate dlc; +extern crate ddk_dlc; extern crate lightning; extern crate secp256k1_zkp; #[macro_use] @@ -44,7 +44,7 @@ use channel::{ SettleOffer, SignChannel, }; use contract_msgs::ContractInfo; -use dlc::{Error, TxInputInfo}; +use ddk_dlc::{Error, TxInputInfo}; use lightning::ln::msgs::DecodeError; use lightning::ln::wire::Type; use lightning::util::ser::{Readable, Writeable, Writer}; diff --git a/dlc-messages/src/message_handler.rs b/ddk-messages/src/message_handler.rs similarity index 100% rename from dlc-messages/src/message_handler.rs rename to ddk-messages/src/message_handler.rs diff --git a/dlc-messages/src/oracle_msgs.rs b/ddk-messages/src/oracle_msgs.rs similarity index 98% rename from dlc-messages/src/oracle_msgs.rs rename to ddk-messages/src/oracle_msgs.rs index 5b470c89..f10669b2 100644 --- a/dlc-messages/src/oracle_msgs.rs +++ b/ddk-messages/src/oracle_msgs.rs @@ -5,7 +5,7 @@ use crate::ser_impls::{ write_i32, write_schnorr_pubkey, write_schnorrsig, write_strings_u16, }; use bitcoin::hashes::Hash; -use dlc::{Error, OracleInfo as DlcOracleInfo}; +use ddk_dlc::{Error, OracleInfo as DlcOracleInfo}; use lightning::ln::msgs::DecodeError; use lightning::ln::wire::Type; use lightning::util::ser::{Readable, Writeable, Writer}; @@ -353,7 +353,7 @@ impl OracleAttestation { secp.verify_schnorr(sig, &msg, &self.oracle_public_key) .map_err(|_| Error::InvalidArgument)?; - Ok::<(), dlc::Error>(()) + Ok::<(), ddk_dlc::Error>(()) })?; if !self @@ -575,7 +575,7 @@ mod tests { let hash = bitcoin::hashes::sha256::Hash::hash("1".as_bytes()); let msg = Message::from_digest(hash.to_byte_array()); - let sig = dlc::secp_utils::schnorrsig_sign_with_nonce( + let sig = ddk_dlc::secp_utils::schnorrsig_sign_with_nonce( SECP256K1, &msg, &key_pair, @@ -624,7 +624,7 @@ mod tests { let hash = bitcoin::hashes::sha256::Hash::hash("1".as_bytes()); let msg = Message::from_digest(hash.to_byte_array()); - let sig = dlc::secp_utils::schnorrsig_sign_with_nonce( + let sig = ddk_dlc::secp_utils::schnorrsig_sign_with_nonce( SECP256K1, &msg, &key_pair, diff --git a/dlc-messages/src/segmentation/mod.rs b/ddk-messages/src/segmentation/mod.rs similarity index 100% rename from dlc-messages/src/segmentation/mod.rs rename to ddk-messages/src/segmentation/mod.rs diff --git a/dlc-messages/src/segmentation/segment_reader.rs b/ddk-messages/src/segmentation/segment_reader.rs similarity index 100% rename from dlc-messages/src/segmentation/segment_reader.rs rename to ddk-messages/src/segmentation/segment_reader.rs diff --git a/dlc-messages/src/ser_impls.rs b/ddk-messages/src/ser_impls.rs similarity index 99% rename from dlc-messages/src/ser_impls.rs rename to ddk-messages/src/ser_impls.rs index e34bb0d4..f8205fba 100644 --- a/dlc-messages/src/ser_impls.rs +++ b/ddk-messages/src/ser_impls.rs @@ -2,7 +2,7 @@ use bitcoin::Address; use bitcoin::Network; -use dlc::{EnumerationPayout, PartyParams, Payout, TxInputInfo}; +use ddk_dlc::{EnumerationPayout, PartyParams, Payout, TxInputInfo}; use lightning::io::Read; use lightning::ln::msgs::DecodeError; use lightning::ln::wire::Type; diff --git a/dlc-messages/src/ser_macros.rs b/ddk-messages/src/ser_macros.rs similarity index 100% rename from dlc-messages/src/ser_macros.rs rename to ddk-messages/src/ser_macros.rs diff --git a/dlc-messages/src/serde_utils.rs b/ddk-messages/src/serde_utils.rs similarity index 100% rename from dlc-messages/src/serde_utils.rs rename to ddk-messages/src/serde_utils.rs diff --git a/dlc-messages/src/test_inputs/accept_msg.json b/ddk-messages/src/test_inputs/accept_msg.json similarity index 100% rename from dlc-messages/src/test_inputs/accept_msg.json rename to ddk-messages/src/test_inputs/accept_msg.json diff --git a/dlc-messages/src/test_inputs/dlc_fee_test.json b/ddk-messages/src/test_inputs/dlc_fee_test.json similarity index 100% rename from dlc-messages/src/test_inputs/dlc_fee_test.json rename to ddk-messages/src/test_inputs/dlc_fee_test.json diff --git a/dlc-messages/src/test_inputs/dlc_fee_test_scripts.json b/ddk-messages/src/test_inputs/dlc_fee_test_scripts.json similarity index 100% rename from dlc-messages/src/test_inputs/dlc_fee_test_scripts.json rename to ddk-messages/src/test_inputs/dlc_fee_test_scripts.json diff --git a/dlc-messages/src/test_inputs/dlc_test.json b/ddk-messages/src/test_inputs/dlc_test.json similarity index 100% rename from dlc-messages/src/test_inputs/dlc_test.json rename to ddk-messages/src/test_inputs/dlc_test.json diff --git a/dlc-messages/src/test_inputs/dlc_tx_test.json b/ddk-messages/src/test_inputs/dlc_tx_test.json similarity index 100% rename from dlc-messages/src/test_inputs/dlc_tx_test.json rename to ddk-messages/src/test_inputs/dlc_tx_test.json diff --git a/dlc-messages/src/test_inputs/offer_msg.json b/ddk-messages/src/test_inputs/offer_msg.json similarity index 100% rename from dlc-messages/src/test_inputs/offer_msg.json rename to ddk-messages/src/test_inputs/offer_msg.json diff --git a/dlc-messages/src/test_inputs/offer_msg_disjoint.json b/ddk-messages/src/test_inputs/offer_msg_disjoint.json similarity index 100% rename from dlc-messages/src/test_inputs/offer_msg_disjoint.json rename to ddk-messages/src/test_inputs/offer_msg_disjoint.json diff --git a/dlc-messages/src/test_inputs/segment_chunk_msg.json b/ddk-messages/src/test_inputs/segment_chunk_msg.json similarity index 100% rename from dlc-messages/src/test_inputs/segment_chunk_msg.json rename to ddk-messages/src/test_inputs/segment_chunk_msg.json diff --git a/dlc-messages/src/test_inputs/segment_start_msg.json b/ddk-messages/src/test_inputs/segment_start_msg.json similarity index 100% rename from dlc-messages/src/test_inputs/segment_start_msg.json rename to ddk-messages/src/test_inputs/segment_start_msg.json diff --git a/dlc-messages/src/test_inputs/sign_msg.json b/ddk-messages/src/test_inputs/sign_msg.json similarity index 100% rename from dlc-messages/src/test_inputs/sign_msg.json rename to ddk-messages/src/test_inputs/sign_msg.json diff --git a/dlc-trie/CHANGELOG.md b/ddk-trie/CHANGELOG.md similarity index 100% rename from dlc-trie/CHANGELOG.md rename to ddk-trie/CHANGELOG.md diff --git a/dlc-trie/Cargo.toml b/ddk-trie/Cargo.toml similarity index 53% rename from dlc-trie/Cargo.toml rename to ddk-trie/Cargo.toml index 23bfbbba..91b60d71 100644 --- a/dlc-trie/Cargo.toml +++ b/ddk-trie/Cargo.toml @@ -1,22 +1,22 @@ [package] -authors = ["Crypto Garage"] +authors = ["Crypto Garage", "benny b "] description = "Data structures for storage and retrival of numerical Discreet Log Contracts (DLC)." -homepage = "https://github.com/p2pderivatives/rust-dlc" +homepage = "https://github.com/bennyhodl/rust-dlc" license-file = "../LICENSE" -name = "dlc-trie" -repository = "https://github.com/p2pderivatives/rust-dlc/tree/master/dlc-trie" +name = "ddk-trie" +repository = "https://github.com/bennyhodl/rust-dlc/tree/master/dlc-trie" version = "0.7.0" [features] default = ["std"] -std = ["dlc/std", "bitcoin/std"] -no-std = ["dlc/no-std"] +std = ["ddk-dlc/std", "bitcoin/std"] +no-std = ["ddk-dlc/no-std"] parallel = ["rayon"] -use-serde = ["serde", "dlc/use-serde"] +use-serde = ["serde", "ddk-dlc/use-serde"] [dependencies] bitcoin = { version = "0.32.2", default-features = false } -dlc = {version = "0.7.0", default-features = false, path = "../dlc"} +ddk-dlc = {version = "0.7.0", default-features = false, path = "../ddk-dlc"} rayon = {version = "1.5", optional = true} secp256k1-zkp = {version = "0.11.0" } serde = {version = "1.0", optional = true, default-features = false, features = ["derive"]} diff --git a/dlc-trie/Readme.md b/ddk-trie/Readme.md similarity index 100% rename from dlc-trie/Readme.md rename to ddk-trie/Readme.md diff --git a/dlc-trie/src/combination_iterator.rs b/ddk-trie/src/combination_iterator.rs similarity index 100% rename from dlc-trie/src/combination_iterator.rs rename to ddk-trie/src/combination_iterator.rs diff --git a/dlc-trie/src/digit_decomposition.rs b/ddk-trie/src/digit_decomposition.rs similarity index 99% rename from dlc-trie/src/digit_decomposition.rs rename to ddk-trie/src/digit_decomposition.rs index b40f50e6..22bbb38e 100644 --- a/dlc-trie/src/digit_decomposition.rs +++ b/ddk-trie/src/digit_decomposition.rs @@ -1,6 +1,6 @@ //! Utility functions to decompose numeric outcome values -use dlc::{Payout, RangePayout}; +use ddk_dlc::{Payout, RangePayout}; /// Decompose a numeric value into digits in the specified base. If the decomposed /// value contains less than `nb_digits`, zeroes will be prepended to reach `nb_digits` @@ -195,7 +195,7 @@ pub fn group_by_ignoring_digits( #[cfg(test)] mod tests { - use dlc::{Payout, RangePayout}; + use ddk_dlc::{Payout, RangePayout}; struct DecompositionTestCase { composed: usize, decomposed: Vec, diff --git a/dlc-trie/src/digit_trie.rs b/ddk-trie/src/digit_trie.rs similarity index 99% rename from dlc-trie/src/digit_trie.rs rename to ddk-trie/src/digit_trie.rs index 6f035a4d..ff357b78 100644 --- a/dlc-trie/src/digit_trie.rs +++ b/ddk-trie/src/digit_trie.rs @@ -2,7 +2,7 @@ //! Data structure to store and lookup digit decomposition data. use crate::{LookupResult, Node}; -use dlc::Error; +use ddk_dlc::Error; /// Structure to store data inserted and looked-up based on digit paths. #[derive(Clone)] diff --git a/dlc-trie/src/lib.rs b/ddk-trie/src/lib.rs similarity index 97% rename from dlc-trie/src/lib.rs rename to ddk-trie/src/lib.rs index 1522a922..38187d91 100644 --- a/dlc-trie/src/lib.rs +++ b/ddk-trie/src/lib.rs @@ -1,7 +1,7 @@ //! # Dlc-trie //! Package for storing and retrieving DLC data using tries. -#![crate_name = "dlc_trie"] +#![crate_name = "ddk_trie"] // Coding conventions #![forbid(unsafe_code)] #![deny(non_upper_case_globals)] @@ -13,7 +13,7 @@ #![deny(missing_docs)] extern crate bitcoin; -extern crate dlc; +extern crate ddk_dlc; #[cfg(feature = "parallel")] extern crate rayon; extern crate secp256k1_zkp; @@ -21,7 +21,7 @@ extern crate secp256k1_zkp; extern crate serde; use bitcoin::{Script, Transaction}; -use dlc::{Error, RangePayout}; +use ddk_dlc::{Error, RangePayout}; #[cfg(feature = "parallel")] use rayon::prelude::*; use secp256k1_zkp::{All, EcdsaAdaptorSignature, PublicKey, Secp256k1, SecretKey}; @@ -240,7 +240,7 @@ fn sign_helper>( &x.paths, precomputed_points, )?; - let adaptor_sig = dlc::create_cet_adaptor_sig_from_point( + let adaptor_sig = ddk_dlc::create_cet_adaptor_sig_from_point( secp, &cets[x.value.cet_index], &adaptor_point, @@ -274,7 +274,7 @@ fn sign_helper>( &x.paths, precomputed_points, )?; - let adaptor_sig = dlc::create_cet_adaptor_sig_from_point( + let adaptor_sig = ddk_dlc::create_cet_adaptor_sig_from_point( secp, &cets[x.value.cet_index], &adaptor_point, @@ -309,7 +309,7 @@ fn verify_helper>( if x.value.adaptor_index > max_adaptor_index { max_adaptor_index = x.value.adaptor_index; } - dlc::verify_cet_adaptor_sig_from_point( + ddk_dlc::verify_cet_adaptor_sig_from_point( secp, &adaptor_sig, cet, @@ -343,7 +343,7 @@ fn verify_helper>( utils::get_adaptor_point_for_indexed_paths(&x.indexes, &x.paths, precomputed_points)?; let adaptor_sig = adaptor_sigs[x.value.adaptor_index]; let cet = &cets[x.value.cet_index]; - dlc::verify_cet_adaptor_sig_from_point( + ddk_dlc::verify_cet_adaptor_sig_from_point( secp, &adaptor_sig, cet, diff --git a/dlc-trie/src/multi_oracle.rs b/ddk-trie/src/multi_oracle.rs similarity index 100% rename from dlc-trie/src/multi_oracle.rs rename to ddk-trie/src/multi_oracle.rs diff --git a/dlc-trie/src/multi_oracle_trie.rs b/ddk-trie/src/multi_oracle_trie.rs similarity index 99% rename from dlc-trie/src/multi_oracle_trie.rs rename to ddk-trie/src/multi_oracle_trie.rs index 20f979a2..ed71ccc8 100644 --- a/dlc-trie/src/multi_oracle_trie.rs +++ b/ddk-trie/src/multi_oracle_trie.rs @@ -9,7 +9,7 @@ use crate::digit_trie::{DigitTrie, DigitTrieDump, DigitTrieIter}; use crate::multi_trie::{MultiTrie, MultiTrieDump, MultiTrieIterator}; use crate::utils::{get_value_callback, pre_pad_vec}; use crate::{DlcTrie, IndexedPath, LookupResult, OracleNumericInfo, RangeInfo, TrieIterInfo}; -use dlc::{Error, RangePayout}; +use ddk_dlc::{Error, RangePayout}; /// Data structure used to store adaptor signature information for numerical /// outcome DLC with t of n oracles where at least t oracles need to sign the @@ -259,7 +259,7 @@ impl<'a> DlcTrie<'a, MultiOracleTrieIter<'a>> for MultiOracleTrie { Ok(trie_infos) } - fn iter(&'a self) -> MultiOracleTrieIter { + fn iter(&'a self) -> MultiOracleTrieIter<'a> { let digit_trie_iterator = DigitTrieIter::new(&self.digit_trie); let extra_cover_trie_iterator = self.extra_cover_trie.as_ref().map(MultiTrieIterator::new); MultiOracleTrieIter { @@ -358,7 +358,7 @@ impl<'a> Iterator for MultiOracleTrieIter<'a> { #[cfg(test)] mod tests { - use dlc::{Payout, RangePayout}; + use ddk_dlc::{Payout, RangePayout}; use crate::{test_utils::get_variable_oracle_numeric_infos, DlcTrie}; diff --git a/dlc-trie/src/multi_oracle_trie_with_diff.rs b/ddk-trie/src/multi_oracle_trie_with_diff.rs similarity index 99% rename from dlc-trie/src/multi_oracle_trie_with_diff.rs rename to ddk-trie/src/multi_oracle_trie_with_diff.rs index 67c87c76..dd1b4a8a 100644 --- a/dlc-trie/src/multi_oracle_trie_with_diff.rs +++ b/ddk-trie/src/multi_oracle_trie_with_diff.rs @@ -8,7 +8,7 @@ use crate::multi_trie::{MultiTrie, MultiTrieDump, MultiTrieIterator}; use crate::utils::get_value_callback; use crate::{DlcTrie, OracleNumericInfo, RangeInfo, TrieIterInfo}; -use dlc::{Error, RangePayout}; +use ddk_dlc::{Error, RangePayout}; /// Data structure used to store adaptor signature information for numerical /// outcome DLC with multiple oracles where some difference between the outcomes @@ -167,7 +167,7 @@ impl<'a> Iterator for MultiOracleTrieWithDiffIter<'a> { #[cfg(test)] mod tests { - use dlc::{Payout, RangePayout}; + use ddk_dlc::{Payout, RangePayout}; use crate::{test_utils::get_variable_oracle_numeric_infos, DlcTrie}; diff --git a/dlc-trie/src/multi_trie.rs b/ddk-trie/src/multi_trie.rs similarity index 99% rename from dlc-trie/src/multi_trie.rs rename to ddk-trie/src/multi_trie.rs index e7b8760a..77cba119 100644 --- a/dlc-trie/src/multi_trie.rs +++ b/ddk-trie/src/multi_trie.rs @@ -6,8 +6,8 @@ use crate::{ LookupResult, Node, OracleNumericInfo, }; use combination_iterator::CombinationIterator; +use ddk_dlc::Error; use digit_trie::{DigitTrie, DigitTrieDump, DigitTrieIter}; -use dlc::Error; use multi_oracle::compute_outcome_combinations; #[derive(Clone, Debug)] diff --git a/dlc-trie/src/test_utils.rs b/ddk-trie/src/test_utils.rs similarity index 100% rename from dlc-trie/src/test_utils.rs rename to ddk-trie/src/test_utils.rs diff --git a/dlc-trie/src/utils.rs b/ddk-trie/src/utils.rs similarity index 99% rename from dlc-trie/src/utils.rs rename to ddk-trie/src/utils.rs index e559fcff..0384baaa 100644 --- a/dlc-trie/src/utils.rs +++ b/ddk-trie/src/utils.rs @@ -1,6 +1,6 @@ //! Utility functions when working with DLC trie -use dlc::Error; +use ddk_dlc::Error; use secp256k1_zkp::PublicKey; use crate::{ diff --git a/dlc-sled-storage-provider/Cargo.toml b/dlc-sled-storage-provider/Cargo.toml index 6a747954..756244f7 100644 --- a/dlc-sled-storage-provider/Cargo.toml +++ b/dlc-sled-storage-provider/Cargo.toml @@ -13,7 +13,7 @@ wallet = ["bitcoin", "secp256k1-zkp", "simple-wallet"] [dependencies] bitcoin = {version = "0.32.2", optional = true} -dlc-manager = {path = "../dlc-manager"} +ddk-manager = {path = "../ddk-manager"} lightning = "0.0.125" secp256k1-zkp = {version = "0.11.0", optional = true} simple-wallet = {path = "../simple-wallet", optional = true} diff --git a/dlc-sled-storage-provider/src/lib.rs b/dlc-sled-storage-provider/src/lib.rs index 470e958f..100d02c3 100644 --- a/dlc-sled-storage-provider/src/lib.rs +++ b/dlc-sled-storage-provider/src/lib.rs @@ -11,28 +11,28 @@ #![deny(unused_imports)] #![deny(missing_docs)] -extern crate dlc_manager; +extern crate ddk_manager; extern crate sled; #[cfg(feature = "wallet")] use bitcoin::{address::NetworkUnchecked, Address, Txid}; -use dlc_manager::chain_monitor::ChainMonitor; -use dlc_manager::channel::accepted_channel::AcceptedChannel; -use dlc_manager::channel::offered_channel::OfferedChannel; -use dlc_manager::channel::signed_channel::{SignedChannel, SignedChannelStateType}; -use dlc_manager::channel::{ +use ddk_manager::chain_monitor::ChainMonitor; +use ddk_manager::channel::accepted_channel::AcceptedChannel; +use ddk_manager::channel::offered_channel::OfferedChannel; +use ddk_manager::channel::signed_channel::{SignedChannel, SignedChannelStateType}; +use ddk_manager::channel::{ Channel, ClosedChannel, ClosedPunishedChannel, ClosingChannel, FailedAccept, FailedSign, }; -use dlc_manager::contract::accepted_contract::AcceptedContract; -use dlc_manager::contract::offered_contract::OfferedContract; -use dlc_manager::contract::ser::Serializable; -use dlc_manager::contract::signed_contract::SignedContract; -use dlc_manager::contract::{ +use ddk_manager::contract::accepted_contract::AcceptedContract; +use ddk_manager::contract::offered_contract::OfferedContract; +use ddk_manager::contract::ser::Serializable; +use ddk_manager::contract::signed_contract::SignedContract; +use ddk_manager::contract::{ ClosedContract, Contract, FailedAcceptContract, FailedSignContract, PreClosedContract, }; #[cfg(feature = "wallet")] -use dlc_manager::Utxo; -use dlc_manager::{error::Error, ContractId, Storage}; +use ddk_manager::Utxo; +use ddk_manager::{error::Error, ContractId, Storage}; use lightning::io::{Cursor, Read}; #[cfg(feature = "wallet")] use lightning::util::ser::{Readable, Writeable}; @@ -343,14 +343,14 @@ impl Storage for SledStorageProvider { Ok(()) } - fn delete_channel(&self, channel_id: &dlc_manager::ChannelId) -> Result<(), Error> { + fn delete_channel(&self, channel_id: &ddk_manager::ChannelId) -> Result<(), Error> { self.channel_tree()? .remove(channel_id) .map_err(to_storage_error)?; Ok(()) } - fn get_channel(&self, channel_id: &dlc_manager::ChannelId) -> Result, Error> { + fn get_channel(&self, channel_id: &ddk_manager::ChannelId) -> Result, Error> { match self .channel_tree()? .get(channel_id) @@ -394,7 +394,7 @@ impl Storage for SledStorageProvider { .map_err(|e| Error::StorageError(format!("Error writing chain monitor: {}", e)))?; Ok(()) } - fn get_chain_monitor(&self) -> Result, dlc_manager::error::Error> { + fn get_chain_monitor(&self) -> Result, ddk_manager::error::Error> { let serialized = self .open_tree(&[CHAIN_MONITOR_TREE])? .get([CHAIN_MONITOR_KEY]) @@ -924,11 +924,11 @@ mod tests { let signed_channels = storage .get_signed_channels(Some( - dlc_manager::channel::signed_channel::SignedChannelStateType::Established, + ddk_manager::channel::signed_channel::SignedChannelStateType::Established, )) .expect("Error retrieving offered channels"); assert_eq!(1, signed_channels.len()); - if let dlc_manager::channel::signed_channel::SignedChannelState::Established { + if let ddk_manager::channel::signed_channel::SignedChannelState::Established { .. } = &signed_channels[0].state { diff --git a/electrs-blockchain-provider/Cargo.toml b/electrs-blockchain-provider/Cargo.toml index 4b639332..5768d25e 100644 --- a/electrs-blockchain-provider/Cargo.toml +++ b/electrs-blockchain-provider/Cargo.toml @@ -6,12 +6,13 @@ version = "0.1.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +async-trait = "0.1.83" bitcoin = {version = "0.32.2"} bitcoin-test-utils = {path = "../bitcoin-test-utils"} -dlc-manager = {path = "../dlc-manager"} +ddk-manager = {path = "../ddk-manager"} lightning = {version = "0.0.125"} lightning-block-sync = {version = "0.0.125"} -reqwest = {version = "0.11", features = ["blocking", "json"]} +reqwest = {version = "0.11", features = ["json"]} serde = {version = "*", features = ["derive"]} simple-wallet = {path = "../simple-wallet"} tokio = "1" diff --git a/electrs-blockchain-provider/src/lib.rs b/electrs-blockchain-provider/src/lib.rs index 91960966..6b22020e 100644 --- a/electrs-blockchain-provider/src/lib.rs +++ b/electrs-blockchain-provider/src/lib.rs @@ -10,10 +10,10 @@ use bitcoin::{ block::Header, Block, BlockHash, Network, OutPoint, ScriptBuf, Transaction, TxOut, Txid, }; use bitcoin_test_utils::tx_to_string; -use dlc_manager::{error::Error, Blockchain, Utxo}; +use ddk_manager::{error::Error, Blockchain, Utxo}; use lightning::chain::chaininterface::{BroadcasterInterface, ConfirmationTarget, FeeEstimator}; use lightning_block_sync::{BlockData, BlockHeaderData, BlockSource, BlockSourceError}; -use reqwest::blocking::Response; +use reqwest::Response; use serde::Deserialize; use serde::Serialize; @@ -29,8 +29,7 @@ pub enum Target { pub struct ElectrsBlockchainProvider { host: String, - client: reqwest::blocking::Client, - async_client: reqwest::Client, + client: reqwest::Client, network: Network, fees: Arc>, } @@ -46,18 +45,18 @@ impl ElectrsBlockchainProvider { Self { host, network, - client: reqwest::blocking::Client::new(), - async_client: reqwest::Client::new(), + client: reqwest::Client::new(), fees, } } - fn get(&self, sub_url: &str) -> Result { + async fn get(&self, sub_url: &str) -> Result { self.client .get(format!("{}{}", self.host, sub_url)) .send() + .await .map_err(|x| { - dlc_manager::error::Error::IOError(lightning::io::Error::new( + ddk_manager::error::Error::IOError(lightning::io::Error::new( lightning::io::ErrorKind::Other, x, )) @@ -65,100 +64,115 @@ impl ElectrsBlockchainProvider { } async fn get_async(&self, sub_url: &str) -> Result { - self.async_client + self.client .get(format!("{}{}", self.host, sub_url)) .send() .await } - fn get_text(&self, sub_url: &str) -> Result { - self.get(sub_url)?.text().map_err(|x| { - dlc_manager::error::Error::IOError(lightning::io::Error::new( + async fn get_text(&self, sub_url: &str) -> Result { + self.get(sub_url).await?.text().await.map_err(|x| { + ddk_manager::error::Error::IOError(lightning::io::Error::new( lightning::io::ErrorKind::Other, x, )) }) } - fn get_u64(&self, sub_url: &str) -> Result { - self.get_text(sub_url)? + async fn get_u64(&self, sub_url: &str) -> Result { + self.get_text(sub_url) + .await? .parse() .map_err(|e: std::num::ParseIntError| Error::BlockchainError(e.to_string())) } - fn get_bytes(&self, sub_url: &str) -> Result, Error> { - let bytes = self.get(sub_url)?.bytes(); + async fn get_bytes(&self, sub_url: &str) -> Result, Error> { + let bytes = self.get(sub_url).await?.bytes().await; Ok(bytes .map_err(|e| Error::BlockchainError(e.to_string()))? .into_iter() .collect::>()) } - fn get_from_json(&self, sub_url: &str) -> Result + async fn get_from_json(&self, sub_url: &str) -> Result where T: serde::de::DeserializeOwned, { - self.get(sub_url)? + self.get(sub_url) + .await? .json::() + .await .map_err(|e| Error::BlockchainError(e.to_string())) } - pub fn get_outspends(&self, txid: &Txid) -> Result, Error> { - self.get_from_json(&format!("tx/{txid}/outspends")) + pub async fn get_outspends(&self, txid: &Txid) -> Result, Error> { + self.get_from_json(&format!("tx/{txid}/outspends")).await } } +#[async_trait::async_trait] impl Blockchain for ElectrsBlockchainProvider { - fn send_transaction(&self, transaction: &Transaction) -> Result<(), dlc_manager::error::Error> { + async fn send_transaction( + &self, + transaction: &Transaction, + ) -> Result<(), ddk_manager::error::Error> { let res = self .client .post(format!("{}tx", self.host)) .body(tx_to_string(transaction)) .send() + .await .map_err(|x| { - dlc_manager::error::Error::IOError(lightning::io::Error::new( + ddk_manager::error::Error::IOError(lightning::io::Error::new( lightning::io::ErrorKind::Other, x, )) })?; if let Err(error) = res.error_for_status_ref() { - let body = res.text().unwrap_or_default(); - return Err(dlc_manager::error::Error::InvalidParameters(format!( + let body = res.text().await.unwrap_or_default(); + return Err(ddk_manager::error::Error::InvalidParameters(format!( "Server returned error: {error} {body}" ))); } Ok(()) } - fn get_network(&self) -> Result { + fn get_network(&self) -> Result { Ok(self.network) } - fn get_blockchain_height(&self) -> Result { - self.get_u64("blocks/tip/height") + async fn get_blockchain_height(&self) -> Result { + self.get_u64("blocks/tip/height").await } - fn get_block_at_height(&self, height: u64) -> Result { - let hash_at_height = self.get_text(&format!("block-height/{height}"))?; - let raw_block = self.get_bytes(&format!("block/{hash_at_height}/raw"))?; + async fn get_block_at_height(&self, height: u64) -> Result { + let hash_at_height = self.get_text(&format!("block-height/{height}")).await?; + let raw_block = self + .get_bytes(&format!("block/{hash_at_height}/raw")) + .await?; // TODO: Bitcoin IO for all Block::consensus_decode(&mut bitcoin::io::Cursor::new(&*raw_block)) .map_err(|e| Error::BlockchainError(e.to_string())) } - fn get_transaction(&self, tx_id: &Txid) -> Result { - let raw_tx = self.get_bytes(&format!("tx/{tx_id}/raw"))?; + async fn get_transaction( + &self, + tx_id: &Txid, + ) -> Result { + let raw_tx = self.get_bytes(&format!("tx/{tx_id}/raw")).await?; Transaction::consensus_decode(&mut lightning::io::Cursor::new(&*raw_tx)) .map_err(|e| Error::BlockchainError(e.to_string())) } - fn get_transaction_confirmations( + async fn get_transaction_confirmations( &self, tx_id: &Txid, - ) -> Result { - let tx_status = self.get_from_json::(&format!("tx/{tx_id}/status"))?; + ) -> Result { + let tx_status = self + .get_from_json::(&format!("tx/{tx_id}/status")) + .await?; if tx_status.confirmed { - let block_chain_height = self.get_blockchain_height()?; + let block_chain_height = self.get_blockchain_height().await?; if let Some(block_height) = tx_status.block_height { return Ok((block_chain_height - block_height + 1) as u32); } @@ -168,9 +182,12 @@ impl Blockchain for ElectrsBlockchainProvider { } } +#[async_trait::async_trait] impl simple_wallet::WalletBlockchainProvider for ElectrsBlockchainProvider { - fn get_utxos_for_address(&self, address: &bitcoin::Address) -> Result, Error> { - let utxos: Vec = self.get_from_json(&format!("address/{address}/utxo"))?; + async fn get_utxos_for_address(&self, address: &bitcoin::Address) -> Result, Error> { + let utxos: Vec = self + .get_from_json(&format!("address/{address}/utxo")) + .await?; utxos .into_iter() @@ -197,8 +214,10 @@ impl simple_wallet::WalletBlockchainProvider for ElectrsBlockchainProvider { .collect::, Error>>() } - fn is_output_spent(&self, txid: &Txid, vout: u32) -> Result { - let is_spent: SpentResp = self.get_from_json(&format!("tx/{txid}/outspend/{vout}"))?; + async fn is_output_spent(&self, txid: &Txid, vout: u32) -> Result { + let is_spent: SpentResp = self + .get_from_json(&format!("tx/{txid}/outspend/{vout}")) + .await?; Ok(is_spent.spent) } } @@ -327,25 +346,25 @@ impl BlockSource for ElectrsBlockchainProvider { impl BroadcasterInterface for ElectrsBlockchainProvider { fn broadcast_transactions(&self, txs: &[&Transaction]) { - let client = self.client.clone(); - let host = self.host.clone(); - let bodies = txs + let _client = self.client.clone(); + let _host = self.host.clone(); + let _bodies = txs .iter() .map(|tx| bitcoin_test_utils::tx_to_string(tx)) .collect::>(); - std::thread::spawn(move || { - for body in bodies { - match client.post(format!("{host}tx")).body(body).send() { - Err(_) => {} - Ok(res) => { - if res.error_for_status_ref().is_err() { - // let body = res.text().unwrap_or_default(); - // TODO(tibo): log - } - } - }; - } - }); + // std::thread::spawn(move || { + // for body in bodies { + // match client.post(format!("{host}tx")).body(body).send() { + // Err(_) => {} + // Ok(res) => { + // if res.error_for_status_ref().is_err() { + // // let body = res.text().unwrap_or_default(); + // // TODO(tibo): log + // } + // } + // }; + // } + // }); } } @@ -399,16 +418,16 @@ fn store_estimate_for_target( fn poll_for_fee_estimates(fees: Arc>, host: &str) { let host = host.to_owned(); - std::thread::spawn(move || loop { - if let Ok(res) = reqwest::blocking::get(format!("{host}fee-estimates")) { - if let Ok(fee_estimates) = res.json::() { + tokio::spawn(async move { + if let Ok(res) = reqwest::get(format!("{host}fee-estimates")).await { + if let Ok(fee_estimates) = res.json::().await { store_estimate_for_target(&fees, &fee_estimates, Target::Background); store_estimate_for_target(&fees, &fee_estimates, Target::HighPriority); store_estimate_for_target(&fees, &fee_estimates, Target::Normal); } } - std::thread::sleep(Duration::from_secs(60)); + tokio::time::sleep(Duration::from_secs(60)).await; }); } diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml index 64d8183d..bdaa35cb 100644 --- a/fuzz/Cargo.toml +++ b/fuzz/Cargo.toml @@ -8,7 +8,7 @@ version = "0.1.0" cargo-fuzz = true [dependencies] -dlc-messages = {path = "../dlc-messages"} +ddk-messages = {path = "../ddk-messages"} honggfuzz = "0.5" lightning = {version = "0.0.125" } diff --git a/mocks/Cargo.toml b/mocks/Cargo.toml index b51fd906..28f40eeb 100644 --- a/mocks/Cargo.toml +++ b/mocks/Cargo.toml @@ -1,14 +1,15 @@ [package] authors = ["Crypto Garage"] -edition = "2018" +edition = "2021" name = "mocks" version = "0.1.0" [dependencies] +async-trait = "0.1.83" bitcoin = "0.32.2" -dlc = {path = "../dlc"} -dlc-manager = {path = "../dlc-manager"} -dlc-messages = {path = "../dlc-messages"} +ddk-dlc = {path = "../ddk-dlc"} +ddk-manager = {path = "../ddk-manager"} +ddk-messages = {path = "../ddk-messages"} lightning = {version = "0.0.125"} secp256k1-zkp = {version = "0.11.0", features = ["hashes", "global-context", "rand", "rand-std"]} simple-wallet = {path = "../simple-wallet"} diff --git a/mocks/src/lib.rs b/mocks/src/lib.rs index 4692f4cb..ab08782f 100644 --- a/mocks/src/lib.rs +++ b/mocks/src/lib.rs @@ -4,5 +4,5 @@ pub mod mock_oracle_provider; pub mod mock_time; pub mod mock_wallet; -pub use dlc_manager; +pub use ddk_manager; pub use simple_wallet; diff --git a/mocks/src/memory_storage_provider.rs b/mocks/src/memory_storage_provider.rs index c0a61762..e3270a2c 100644 --- a/mocks/src/memory_storage_provider.rs +++ b/mocks/src/memory_storage_provider.rs @@ -1,15 +1,15 @@ use bitcoin::{Address, OutPoint, Txid}; -use dlc_manager::chain_monitor::ChainMonitor; -use dlc_manager::channel::{ +use ddk_manager::chain_monitor::ChainMonitor; +use ddk_manager::channel::{ offered_channel::OfferedChannel, signed_channel::{SignedChannel, SignedChannelStateType}, Channel, }; -use dlc_manager::contract::{ +use ddk_manager::contract::{ offered_contract::OfferedContract, signed_contract::SignedContract, Contract, PreClosedContract, }; -use dlc_manager::Storage; -use dlc_manager::{error::Error as DaemonError, ChannelId, ContractId, Utxo}; +use ddk_manager::Storage; +use ddk_manager::{error::Error as DaemonError, ChannelId, ContractId, Utxo}; use secp256k1_zkp::SecretKey; use simple_wallet::WalletStorage; use std::collections::HashMap; diff --git a/mocks/src/mock_blockchain.rs b/mocks/src/mock_blockchain.rs index cdb6b7ac..01fac213 100644 --- a/mocks/src/mock_blockchain.rs +++ b/mocks/src/mock_blockchain.rs @@ -1,7 +1,7 @@ use std::sync::Mutex; use bitcoin::{Block, Transaction, Txid}; -use dlc_manager::{error::Error, Blockchain, Utxo}; +use ddk_manager::{error::Error, Blockchain, Utxo}; use lightning::chain::chaininterface::FeeEstimator; use simple_wallet::WalletBlockchainProvider; @@ -23,21 +23,22 @@ impl Default for MockBlockchain { } } +#[async_trait::async_trait] impl Blockchain for MockBlockchain { - fn send_transaction(&self, transaction: &Transaction) -> Result<(), Error> { + async fn send_transaction(&self, transaction: &Transaction) -> Result<(), Error> { self.transactions.lock().unwrap().push(transaction.clone()); Ok(()) } fn get_network(&self) -> Result { Ok(bitcoin::Network::Regtest) } - fn get_blockchain_height(&self) -> Result { + async fn get_blockchain_height(&self) -> Result { Ok(10) } - fn get_block_at_height(&self, _height: u64) -> Result { + async fn get_block_at_height(&self, _height: u64) -> Result { unimplemented!(); } - fn get_transaction(&self, tx_id: &Txid) -> Result { + async fn get_transaction(&self, tx_id: &Txid) -> Result { Ok(self .transactions .lock() @@ -47,17 +48,18 @@ impl Blockchain for MockBlockchain { .unwrap() .clone()) } - fn get_transaction_confirmations(&self, _tx_id: &Txid) -> Result { + async fn get_transaction_confirmations(&self, _tx_id: &Txid) -> Result { Ok(6) } } +#[async_trait::async_trait] impl WalletBlockchainProvider for MockBlockchain { - fn get_utxos_for_address(&self, _address: &bitcoin::Address) -> Result, Error> { + async fn get_utxos_for_address(&self, _address: &bitcoin::Address) -> Result, Error> { unimplemented!() } - fn is_output_spent(&self, _txid: &Txid, _vout: u32) -> Result { + async fn is_output_spent(&self, _txid: &Txid, _vout: u32) -> Result { unimplemented!() } } diff --git a/mocks/src/mock_oracle_provider.rs b/mocks/src/mock_oracle_provider.rs index 2bc03916..80dacafd 100644 --- a/mocks/src/mock_oracle_provider.rs +++ b/mocks/src/mock_oracle_provider.rs @@ -1,7 +1,7 @@ use bitcoin::hashes::Hash; -use dlc_manager::error::Error as DaemonError; -use dlc_manager::Oracle; -use dlc_messages::oracle_msgs::{ +use ddk_manager::error::Error as DaemonError; +use ddk_manager::Oracle; +use ddk_messages::oracle_msgs::{ EventDescriptor, OracleAnnouncement, OracleAttestation, OracleEvent, }; use lightning::util::ser::Writeable; @@ -55,12 +55,13 @@ impl Default for MockOracle { } } +#[async_trait::async_trait] impl Oracle for MockOracle { fn get_public_key(&self) -> XOnlyPublicKey { XOnlyPublicKey::from_keypair(&self.key_pair).0 } - fn get_announcement(&self, event_id: &str) -> Result { + async fn get_announcement(&self, event_id: &str) -> Result { let res = self .announcements .get(event_id) @@ -68,7 +69,7 @@ impl Oracle for MockOracle { Ok(res.clone()) } - fn get_attestation(&self, event_id: &str) -> Result { + async fn get_attestation(&self, event_id: &str) -> Result { let res = self .attestations .get(event_id) @@ -138,7 +139,7 @@ impl MockOracle { .map(|(x, nonce)| { let hash = bitcoin::hashes::sha256::Hash::hash(x.as_bytes()).to_byte_array(); let msg = Message::from_digest(hash); - dlc::secp_utils::schnorrsig_sign_with_nonce( + ddk_dlc::secp_utils::schnorrsig_sign_with_nonce( &self.secp, &msg, &self.key_pair, diff --git a/mocks/src/mock_time.rs b/mocks/src/mock_time.rs index 876fbcd6..d7ae032c 100644 --- a/mocks/src/mock_time.rs +++ b/mocks/src/mock_time.rs @@ -1,6 +1,6 @@ -extern crate dlc_manager; +extern crate ddk_manager; -use dlc_manager::Time; +use ddk_manager::Time; use std::cell::RefCell; thread_local! { diff --git a/mocks/src/mock_wallet.rs b/mocks/src/mock_wallet.rs index 5bb062fc..a54084d8 100644 --- a/mocks/src/mock_wallet.rs +++ b/mocks/src/mock_wallet.rs @@ -4,7 +4,7 @@ use bitcoin::psbt::Psbt; use bitcoin::transaction::Version; use bitcoin::{absolute::LockTime, Address, OutPoint, ScriptBuf, Transaction, TxOut}; use bitcoin::{Amount, CompressedPublicKey}; -use dlc_manager::{error::Error, Blockchain, ContractSignerProvider, SimpleSigner, Utxo, Wallet}; +use ddk_manager::{error::Error, Blockchain, ContractSignerProvider, SimpleSigner, Utxo, Wallet}; use secp256k1_zkp::{rand::seq::SliceRandom, PublicKey, SecretKey}; use crate::mock_blockchain::MockBlockchain; @@ -14,7 +14,7 @@ pub struct MockWallet { } impl MockWallet { - pub fn new(blockchain: &Rc, utxo_values: &[u64]) -> Self { + pub async fn new(blockchain: &Rc, utxo_values: &[u64]) -> Self { let mut utxos = Vec::with_capacity(utxo_values.len()); for utxo_value in utxo_values { @@ -28,7 +28,7 @@ impl MockWallet { input: vec![], output: vec![tx_out.clone()], }; - blockchain.send_transaction(&tx).unwrap(); + blockchain.send_transaction(&tx).await.unwrap(); let utxo = Utxo { tx_out, outpoint: bitcoin::OutPoint { @@ -68,11 +68,11 @@ impl ContractSignerProvider for MockWallet { } impl Wallet for MockWallet { - fn get_new_address(&self) -> Result { + fn get_new_address(&self) -> Result { Ok(get_address()) } - fn get_new_change_address(&self) -> Result { + fn get_new_change_address(&self) -> Result { Ok(get_address()) } @@ -81,7 +81,7 @@ impl Wallet for MockWallet { amount: u64, _fee_rate: u64, _lock_utxos: bool, - ) -> Result, Error> { + ) -> Result, Error> { let mut utxo_pool = self.utxos.clone(); let seed = 1; utxo_pool.shuffle(&mut secp256k1_zkp::rand::rngs::mock::StepRng::new( @@ -109,7 +109,7 @@ impl Wallet for MockWallet { Err(Error::InvalidParameters("Not enought UTXOs".to_string())) } - fn import_address(&self, _address: &Address) -> Result<(), dlc_manager::error::Error> { + fn import_address(&self, _address: &Address) -> Result<(), ddk_manager::error::Error> { Ok(()) } diff --git a/p2pd-oracle-client/Cargo.toml b/p2pd-oracle-client/Cargo.toml index f8211c6a..db811d52 100644 --- a/p2pd-oracle-client/Cargo.toml +++ b/p2pd-oracle-client/Cargo.toml @@ -6,14 +6,17 @@ license-file = "../LICENSE" name = "p2pd-oracle-client" repository = "https://github.com/p2pderivatives/rust-dlc/tree/master/p2pd-oracle-client" version = "0.1.0" +edition = "2021" [dependencies] +async-trait = "0.1.83" chrono = {version = "0.4.19", features = ["serde"]} -dlc-manager = {path = "../dlc-manager"} -dlc-messages = {path = "../dlc-messages", features = ["use-serde"]} -reqwest = {version = "0.11", features = ["blocking", "json"]} +ddk-manager = {path = "../ddk-manager"} +ddk-messages = {path = "../ddk-messages", features = ["use-serde"]} +reqwest = {version = "0.11", features = ["json"]} secp256k1-zkp = {version = "0.11.0" } serde = {version = "*", features = ["derive"]} [dev-dependencies] mockito = "0.31.0" +tokio = { version = "1.41.1", features = ["macros", "rt-multi-thread", "test-util"] } diff --git a/p2pd-oracle-client/src/lib.rs b/p2pd-oracle-client/src/lib.rs index 15038df4..b2e6f3b3 100644 --- a/p2pd-oracle-client/src/lib.rs +++ b/p2pd-oracle-client/src/lib.rs @@ -12,16 +12,16 @@ #![deny(missing_docs)] extern crate chrono; -extern crate dlc_manager; -extern crate dlc_messages; +extern crate ddk_manager; +extern crate ddk_messages; extern crate reqwest; extern crate secp256k1_zkp; extern crate serde; use chrono::{DateTime, SecondsFormat, Utc}; -use dlc_manager::error::Error as DlcManagerError; -use dlc_manager::Oracle; -use dlc_messages::oracle_msgs::{OracleAnnouncement, OracleAttestation}; +use ddk_manager::error::Error as DlcManagerError; +use ddk_manager::Oracle; +use ddk_messages::oracle_msgs::{OracleAnnouncement, OracleAttestation}; use secp256k1_zkp::{schnorr::Signature, XOnlyPublicKey}; /// Enables interacting with a DLC oracle. @@ -69,18 +69,20 @@ struct AttestationResponse { values: Vec, } -fn get(path: &str) -> Result +async fn get(path: &str) -> Result where T: serde::de::DeserializeOwned, { - reqwest::blocking::get(path) + reqwest::get(path) + .await .map_err(|x| { - dlc_manager::error::Error::IOError( + ddk_manager::error::Error::IOError( std::io::Error::new(std::io::ErrorKind::Other, x).into(), ) })? .json::() - .map_err(|e| dlc_manager::error::Error::OracleError(e.to_string())) + .await + .map_err(|e| ddk_manager::error::Error::OracleError(e.to_string())) } fn pubkey_path(host: &str) -> String { @@ -109,7 +111,7 @@ impl P2PDOracleClient { /// Try to create an instance of an oracle client connecting to the provided /// host. Returns an error if the host could not be reached. Panics if the /// oracle uses an incompatible format. - pub fn new(host: &str) -> Result { + pub async fn new(host: &str) -> Result { if host.is_empty() { return Err(DlcManagerError::InvalidParameters( "Invalid host".to_string(), @@ -121,7 +123,7 @@ impl P2PDOracleClient { host.to_string() }; let path = pubkey_path(&host); - let public_key = get::(&path)?.public_key; + let public_key = get::(&path).await?.public_key; Ok(P2PDOracleClient { host, public_key }) } } @@ -144,29 +146,33 @@ fn parse_event_id(event_id: &str) -> Result<(String, DateTime), DlcManagerE Ok((asset_id.to_string(), date_time)) } +#[async_trait::async_trait] impl Oracle for P2PDOracleClient { fn get_public_key(&self) -> XOnlyPublicKey { self.public_key } - fn get_announcement(&self, event_id: &str) -> Result { + async fn get_announcement( + &self, + event_id: &str, + ) -> Result { let (asset_id, date_time) = parse_event_id(event_id)?; let path = announcement_path(&self.host, &asset_id, &date_time); - let announcement = get(&path)?; + let announcement = get(&path).await?; Ok(announcement) } - fn get_attestation( + async fn get_attestation( &self, event_id: &str, - ) -> Result { + ) -> Result { let (asset_id, date_time) = parse_event_id(event_id)?; let path = attestation_path(&self.host, &asset_id, &date_time); let AttestationResponse { event_id: _, signatures, values, - } = get::(&path)?; + } = get::(&path).await?; Ok(OracleAttestation { event_id: event_id.to_string(), @@ -202,8 +208,8 @@ mod tests { ).create() } - #[test] - fn get_public_key_test() { + #[tokio::test] + async fn get_public_key_test() { let url = &mockito::server_url(); let _m = pubkey_mock(); let expected_pk: XOnlyPublicKey = @@ -211,13 +217,15 @@ mod tests { .parse() .unwrap(); - let client = P2PDOracleClient::new(url).expect("Error creating client instance."); + let client = P2PDOracleClient::new(url) + .await + .expect("Error creating client instance."); assert_eq!(expected_pk, client.get_public_key()); } - #[test] - fn get_announcement_test() { + #[tokio::test] + async fn get_announcement_test() { let url = &mockito::server_url(); let _pubkey_mock = pubkey_mock(); let path: &str = &announcement_path( @@ -229,15 +237,18 @@ mod tests { ); let _m = mock("GET", path).with_body(r#"{"announcementSignature":"f83db0ca25e4c209b55156737b0c65470a9702fe9d1d19a129994786384289397895e403ff37710095a04a0841a95738e3e8bc35bdef6bce50bf34eeb182bd9b","oraclePublicKey":"10dc8cf51ae3ee1c7967ffb9c9633a5ab06206535d8e1319f005a01ba33bc05d","oracleEvent":{"oracleNonces":["aca32fc8dead13983c655638ef921f1d38ef2f5286e58b2a1dab32b6e086e208","89603f8179830590fdce45eb17ba8bdf74e295a4633b58b46c9ede8274774164","5f3fcdfbba9ec75cb0868e04ec1f97089b4153fb2076bd1e017048e9df633aa1","8436d00f7331491dc6512e560a1f2414be42e893992eccb495642eefc7c5bf37","0d2593764c9c27eba0be3ca6c71a2de4e49a5f4aa1ce1e2cc379be3939547501","414318491e96919e67583db7a47eb1f8b4f1194bcb5b5dcc4fd10492d89926e4","b9a5ded7295e0343f385e5abedfd9e5f4137de8f67de0afa9396f7e0f996ef79","badf0bfe230ed605161630d8e3a092d7448461042db38912bc6c6a0ab195ff71","6e4780213cd7ed9de1300146079b897cae89dec7800065f615974193f58aa6db","7b12b48ad95634ee4ca476dd57e634fddc328e10276e71d27e0ae626fad7d699","a8058604adf590a1c38f8be19aa44175eb2d1130eb4d7f39a34f89f0a3fbed27","ffc3208f60b585cdc778be1290b352c34c22652d5348a87885816bcf17a80116","cb34c13f80b49e729e863035f30e1f8ea7777618eedb6d666c3b1c85a5b8a637","5000991f4631c0bba5d026f02125fdbe77e019dde57d31ce7f23ae3601a18623","094433a2432b81bbb6d6b7d65dc3498e2a7c9de5f35672d67097d54d920eadd2","11dff6b40b0938e1943c7888633d88871c2a2a1c16f412b22b80ba7ed8af8788","d5957f1a199b4abbc06894479c722ad0c4f120f0d5afeb76d589127213e33170","80e09bb453e6a0a444ec3ba222a62ecd59540b9dd8280566a17bebdfdfbd7a9e","0fe775b79b2172cb961e7c1aa54d521360903680680aaa55ea8be0404ee3768c","bfcdbb2cbcffba41048149d4bcf2a41cd5fd0a713df6f48104ade3022c284575"],"eventMaturityEpoch":1653865200,"eventDescriptor":{"digitDecompositionEvent":{"base":2,"isSigned":false,"unit":"usd/btc","precision":0,"nbDigits":20}},"eventId":"btcusd1653865200"}}"#).create(); - let client = P2PDOracleClient::new(url).expect("Error creating client instance"); + let client = P2PDOracleClient::new(url) + .await + .expect("Error creating client instance"); client .get_announcement("btcusd1624943400") + .await .expect("Error getting announcement"); } - #[test] - fn get_attestation_test() { + #[tokio::test] + async fn get_attestation_test() { let url = &mockito::server_url(); let _pubkey_mock = pubkey_mock(); let path: &str = &attestation_path( @@ -250,10 +261,13 @@ mod tests { let _m = mock("GET", path).with_body(r#"{"eventId":"btcusd1653517020","signatures":["ee05b1211d5f974732b10107dd302da062be47cd18f061c5080a50743412f9fd590cad90cfea762472e6fe865c4223bd388c877b7881a27892e15843ff1ac360","59ab83597089b48f5b3c2fd07c11edffa6b1180bdb6d9e7d6924979292d9c53fe79396ceb0782d5941c284d1642377136c06b2d9c2b85bda5969a773a971b5b0","d1f8c31a83bb34433da5b9808bb3692dd212b9022b7bc8f269fc817e96a7195db18262e934bebd4e68a3f2c96550826a5530350662df4c86c004f5cf1121ca67","e5cec554c39c4dd544d70175128271eecad77c1e3eaa6994c657e257d5c1c9dcd19b041ea8030e75448245b7f91705ad914c32761671a6172f928904b439ea6b","a209116d20f0931113c0880e8cd22d3f003609a32322ff8df241ef16e7d4efd1a9b723f582a22073e21188635f09f41f270f3126014542861be14b62b09c0ecc","f1da0b482f08f545a92338392b71cec33d948a5e5732ee4d5c0a87bd6b6cc12feeb1498da7afd93ae48ec4ce581ee79c0e92f338d3777c2ef06578e4ec1a853c","d9ab68244a3b47cc8cbd5a972f2f5059fc6b9711dba8d4a7a23607a99b9655593bab3abc1d3b02402cd0809c3c7016c741742efb363227de2bcfdcf290a053b3","c1146c1767a947f77794d05a2f58e50af824e3c8d70adde883e58d2dc1ddb157323b0aaf8cfb5b076a12395756bdcda64ab5d4799e43c88a41993659e6d49471","0d29d9383c9ee41055e1cb40104c9ca75280162779c0162cb6bf9aca2b223aba17de4b3f0f29ae6b749f22ba467b7e9f05456e8abb3ec328f62b7a924c6d4828","2bcc54002ceb271a940f24bc6dd0562b99c2d76cfb8f145f42ac37bc34fd3e94adba1194c5be91932b818c5715c73f287e066e228d796a373c4aec67fd777070","a91f77e3435c577682ff744d6f7da66c865a42e8645276dedf2ed2b8bc4c80285dff4b553b2231592e0fa8b4f242acb6888519fe82c457cc5204e5d9d511303a","546409d6bcdcfd5bef39957c8b1b09f7805b08ec2311bc73cf6927ae11f3567ffe8428aa7faa661518e9c02a702212ab05e494aab84624c3dd1a710f8c4c369b","9d601ee8a3d28dcdfdd05581f1b24d6e5a576f0b5544eb7c9921cb87a23fdb293c1edca89b43b5b84c1e305fbe52facbe6b03575aed8f95b4faccc90e0eb45ef","636b8028e9cd6cba6be5b3c1789b62aecfc17e9c28d7a621cfad2c3cf751046528028e1dbd6cee050d5d570cf5a3d8986471d73e7edca4093e36fc8e1097fb65","57c6337b52dc7fd8f49b29105f168fc9b4cb88ed2ba5f0e9a80a21e20836f87f875c3fe92afb437dd5647630b54eda6ba1be76ba6df8b641eb2e8be8ff1182dc","9e8843e32f9de4cd6d5bb9e938fd014babe11bb1faf35fc411d754259bc374f34dd841ed91f6bb3f030bc55a4791cdc41471c33b3f05fd35b9d1768fd381f953","97da4963747ab5e50534b93274065cba4fd24e6b7a9d3310db2596af24f70961fb03535e2a5ae272f7ea14e86daafa57073631596fecf7ceadf4ae3e6941b69e","94a414569743f87f1462a503be8cff1f229096d190b8b1349519c612b74eea872d5d763570aaaa54fad0605a43d742203bce489deea5570750030191e293c253","4d7117b89aad73eca7b341749bd54ffdd459b9b8b4ff128344d09273f66a3d2c01d2c86b61f7642d6e81f488580b456685cd68660458cff83b8858a05c9a1f4d","b12153a393a4fddac3079c1878cb89afccfe0ac8f539743c0608049f445e49ac7c89e33fcf832cda8d7e8a4f4dae94a303170f16c697feed8b78015873bd5ffc"],"values":["0","0","0","0","0","1","1","1","0","1","0","0","0","0","1","1","1","0","1","0"]}"#).create(); - let client = P2PDOracleClient::new(url).expect("Error creating client instance"); + let client = P2PDOracleClient::new(url) + .await + .expect("Error creating client instance"); client .get_attestation("btcusd1624943400") + .await .expect("Error getting attestation"); } } diff --git a/sample/Cargo.toml b/sample/Cargo.toml index 8219d04c..5a7a6d6d 100644 --- a/sample/Cargo.toml +++ b/sample/Cargo.toml @@ -7,9 +7,9 @@ version = "0.1.0" [dependencies] bitcoin = {version = "0.32.2"} bitcoin-rpc-provider = {path = "../bitcoin-rpc-provider"} -dlc = {path = "../dlc", features = ["use-serde"]} -dlc-manager = {path = "../dlc-manager", features = ["use-serde", "parallel"]} -dlc-messages = {path = "../dlc-messages"} +ddk-dlc = {path = "../ddk-dlc", features = ["use-serde"]} +ddk-manager = {path = "../ddk-manager", features = ["use-serde", "parallel"]} +ddk-messages = {path = "../ddk-messages"} dlc-sled-storage-provider = {path = "../dlc-sled-storage-provider"} futures = "0.3" lightning = {version = "0.0.125"} diff --git a/sample/src/cli.rs b/sample/src/cli.rs index c55b3795..72a22ae7 100644 --- a/sample/src/cli.rs +++ b/sample/src/cli.rs @@ -4,12 +4,12 @@ use crate::DlcManager; use crate::DlcMessageHandler; use crate::PeerManager; use bitcoin::secp256k1::PublicKey; -use dlc_manager::channel::signed_channel::SignedChannelState; -use dlc_manager::channel::signed_channel::SignedChannelStateType; -use dlc_manager::contract::contract_input::ContractInput; -use dlc_manager::contract::Contract; -use dlc_manager::Storage; -use dlc_messages::Message as DlcMessage; +use ddk_manager::channel::signed_channel::SignedChannelState; +use ddk_manager::channel::signed_channel::SignedChannelStateType; +use ddk_manager::contract::contract_input::ContractInput; +use ddk_manager::contract::Contract; +use ddk_manager::Storage; +use ddk_messages::Message as DlcMessage; use hex_utils::{hex_str, to_slice}; use serde::Deserialize; use serde_json::Value; @@ -19,8 +19,9 @@ use std::io; use std::io::{BufRead, Write}; use std::net::{SocketAddr, ToSocketAddrs}; use std::str::SplitWhitespace; -use std::sync::{Arc, Mutex}; +use std::sync::Arc; use std::time::Duration; +use tokio::sync::Mutex; #[derive(Debug, Deserialize)] #[serde(rename_all = "camelCase")] @@ -97,7 +98,7 @@ pub(crate) async fn poll_for_user_input( print!("> "); io::stdout().flush().unwrap(); // Without flushing, the `>` doesn't print for line in stdin.lock().lines() { - process_incoming_messages(&peer_manager, &dlc_manager, &dlc_message_handler); + process_incoming_messages(&peer_manager, &dlc_manager, &dlc_message_handler).await; let line = line.unwrap(); let mut words = line.split_whitespace(); if let Some(word) = words.next() { @@ -164,32 +165,30 @@ pub(crate) async fn poll_for_user_input( .expect("Error deserializing contract input."); let manager_clone = dlc_manager.clone(); let is_contract = o == "offercontract"; - let offer = tokio::task::spawn_blocking(move || { - if is_contract { - DlcMessage::Offer( - manager_clone - .lock() - .unwrap() - .send_offer(&contract_input, pubkey) - .expect("Error sending offer"), - ) - } else { - DlcMessage::OfferChannel( - manager_clone - .lock() - .unwrap() - .offer_channel(&contract_input, pubkey) - .expect("Error sending offer channel"), - ) - } - }) - .await - .unwrap(); + let offer = if is_contract { + DlcMessage::Offer( + manager_clone + .lock() + .await + .send_offer(&contract_input, pubkey) + .await + .expect("Error sending offer"), + ) + } else { + DlcMessage::OfferChannel( + manager_clone + .lock() + .await + .offer_channel(&contract_input, pubkey) + .await + .expect("Error sending offer channel"), + ) + }; dlc_message_handler.send_message(pubkey, offer); peer_manager.process_events(); } "listoffers" => { - let locked_manager = dlc_manager.lock().unwrap(); + let locked_manager = dlc_manager.lock().await; for offer in locked_manager .get_store() .get_contract_offers() @@ -213,8 +212,9 @@ pub(crate) async fn poll_for_user_input( let (_, node_id, msg) = dlc_manager .lock() - .unwrap() + .await .accept_contract_offer(&contract_id) + .await .expect("Error accepting contract."); dlc_message_handler.send_message(node_id, DlcMessage::Accept(msg)); peer_manager.process_events(); @@ -222,62 +222,60 @@ pub(crate) async fn poll_for_user_input( "listcontracts" => { let manager_clone = dlc_manager.clone(); // Because the oracle client is currently blocking we need to use `spawn_blocking` here. - tokio::task::spawn_blocking(move || { - manager_clone - .lock() - .unwrap() - .periodic_check(true) - .expect("Error doing periodic check."); - let contracts = manager_clone - .lock() - .unwrap() - .get_store() - .get_contracts() - .expect("Error retrieving contract list."); - for contract in contracts { - let id = hex_str(&contract.get_id()); - match contract { - Contract::Offered(_) => { - println!("Offered contract: {}", id); - } - Contract::Accepted(_) => { - println!("Accepted contract: {}", id); - } - Contract::Confirmed(_) => { - println!("Confirmed contract: {}", id); - } - Contract::Signed(_) => { - println!("Signed contract: {}", id); - } - Contract::Closed(closed) => { - println!("Closed contract: {}", id); - if let Some(attestations) = closed.attestations { - println!( - "Outcomes: {:?}", - attestations - .iter() - .map(|x| x.outcomes.clone()) - .collect::>() - ); - } - println!("PnL: {} sats", closed.pnl) - } - Contract::Refunded(_) => { - println!("Refunded contract: {}", id); - } - Contract::FailedAccept(_) | Contract::FailedSign(_) => { - println!("Failed contract: {}", id); + + manager_clone + .lock() + .await + .periodic_check(true) + .await + .expect("Error doing periodic check."); + let contracts = manager_clone + .lock() + .await + .get_store() + .get_contracts() + .expect("Error retrieving contract list."); + for contract in contracts { + let id = hex_str(&contract.get_id()); + match contract { + Contract::Offered(_) => { + println!("Offered contract: {}", id); + } + Contract::Accepted(_) => { + println!("Accepted contract: {}", id); + } + Contract::Confirmed(_) => { + println!("Confirmed contract: {}", id); + } + Contract::Signed(_) => { + println!("Signed contract: {}", id); + } + Contract::Closed(closed) => { + println!("Closed contract: {}", id); + if let Some(attestations) = closed.attestations { + println!( + "Outcomes: {:?}", + attestations + .iter() + .map(|x| x.outcomes.clone()) + .collect::>() + ); } - Contract::Rejected(_) => println!("Rejected contract: {}", id), - Contract::PreClosed(_) => println!("Pre-closed contract: {}", id), + println!("PnL: {} sats", closed.pnl) + } + Contract::Refunded(_) => { + println!("Refunded contract: {}", id); + } + Contract::FailedAccept(_) | Contract::FailedSign(_) => { + println!("Failed contract: {}", id); } + Contract::Rejected(_) => println!("Rejected contract: {}", id), + Contract::PreClosed(_) => println!("Pre-closed contract: {}", id), } - }) - .await - .expect("Error listing contract info"); + } } "listchanneloffers" => { - let locked_manager = dlc_manager.lock().unwrap(); + let locked_manager = dlc_manager.lock().await; for offer in locked_manager .get_store() .get_offered_channels() @@ -305,8 +303,9 @@ pub(crate) async fn poll_for_user_input( let (msg, _, _, node_id) = dlc_manager .lock() - .unwrap() + .await .accept_channel(&channel_id) + .await .expect("Error accepting channel."); dlc_message_handler.send_message(node_id, DlcMessage::AcceptChannel(msg)); peer_manager.process_events(); @@ -323,7 +322,7 @@ pub(crate) async fn poll_for_user_input( let (msg, node_id) = dlc_manager .lock() - .unwrap() + .await .settle_offer(&channel_id, counter_payout) .expect("Error getting settle offer message."); dlc_message_handler.send_message(node_id, DlcMessage::SettleOffer(msg)); @@ -333,7 +332,7 @@ pub(crate) async fn poll_for_user_input( let channel_id = read_id_or_continue!(words, l, "channel id"); let (msg, node_id) = dlc_manager .lock() - .unwrap() + .await .accept_settle_offer(&channel_id) .expect("Error accepting settle channel offer."); dlc_message_handler.send_message(node_id, DlcMessage::SettleAccept(msg)); @@ -343,14 +342,14 @@ pub(crate) async fn poll_for_user_input( let channel_id = read_id_or_continue!(words, l, "channel id"); let (msg, node_id) = dlc_manager .lock() - .unwrap() + .await .reject_settle_offer(&channel_id) .expect("Error rejecting settle channel offer."); dlc_message_handler.send_message(node_id, DlcMessage::Reject(msg)); peer_manager.process_events(); } "listsettlechanneloffers" => { - let locked_manager = dlc_manager.lock().unwrap(); + let locked_manager = dlc_manager.lock().await; for channel in locked_manager .get_store() .get_signed_channels(Some(SignedChannelStateType::SettledReceived)) @@ -380,20 +379,17 @@ pub(crate) async fn poll_for_user_input( let contract_input: ContractInput = serde_json::from_str(&contract_input_str) .expect("Error deserializing contract input."); let manager_clone = dlc_manager.clone(); - let (renew_offer, node_id) = tokio::task::spawn_blocking(move || { - manager_clone - .lock() - .unwrap() - .renew_offer(&channel_id, counter_payout, &contract_input) - .expect("Error sending offer") - }) - .await - .unwrap(); + let (renew_offer, node_id) = manager_clone + .lock() + .await + .renew_offer(&channel_id, counter_payout, &contract_input) + .await + .expect("Error sending offer"); dlc_message_handler.send_message(node_id, DlcMessage::RenewOffer(renew_offer)); peer_manager.process_events(); } "listrenewchanneloffers" => { - let locked_manager = dlc_manager.lock().unwrap(); + let locked_manager = dlc_manager.lock().await; for channel in locked_manager .get_store() .get_signed_channels(Some(SignedChannelStateType::RenewOffered)) @@ -426,7 +422,7 @@ pub(crate) async fn poll_for_user_input( let channel_id = read_id_or_continue!(words, l, "channel id"); let (msg, node_id) = dlc_manager .lock() - .unwrap() + .await .accept_renew_offer(&channel_id) .expect("Error accepting channel."); dlc_message_handler.send_message(node_id, DlcMessage::RenewAccept(msg)); @@ -436,14 +432,14 @@ pub(crate) async fn poll_for_user_input( let channel_id = read_id_or_continue!(words, l, "channel id"); let (msg, node_id) = dlc_manager .lock() - .unwrap() + .await .reject_renew_offer(&channel_id) .expect("Error rejecting settle channel offer."); dlc_message_handler.send_message(node_id, DlcMessage::Reject(msg)); peer_manager.process_events(); } "listsignedchannels" => { - let locked_manager = dlc_manager.lock().unwrap(); + let locked_manager = dlc_manager.lock().await; for channel in locked_manager .get_store() .get_signed_channels(None) @@ -589,7 +585,7 @@ pub(crate) fn parse_peer_info( Ok((pubkey.unwrap(), peer_addr.unwrap().unwrap())) } -fn process_incoming_messages( +async fn process_incoming_messages( peer_manager: &Arc, dlc_manager: &Arc>, dlc_message_handler: &Arc, @@ -601,8 +597,9 @@ fn process_incoming_messages( println!("Processing message from {}", node_id); let resp = dlc_manager .lock() - .unwrap() + .await .on_dlc_message(&message, node_id) + .await .expect("Error processing message"); if let Some(msg) = resp { println!("Sending message to {}", node_id); diff --git a/sample/src/main.rs b/sample/src/main.rs index 5bea995f..545ceef0 100644 --- a/sample/src/main.rs +++ b/sample/src/main.rs @@ -7,8 +7,8 @@ use disk::FilesystemLogger; use bitcoin::secp256k1::rand::{thread_rng, RngCore}; use bitcoin::secp256k1::SecretKey; use bitcoin_rpc_provider::BitcoinCoreProvider; -use dlc_manager::{CachedContractSignerProvider, Oracle, SimpleSigner, SystemTimeProvider}; -use dlc_messages::message_handler::MessageHandler as DlcMessageHandler; +use ddk_manager::{CachedContractSignerProvider, Oracle, SimpleSigner, SystemTimeProvider}; +use ddk_messages::message_handler::MessageHandler as DlcMessageHandler; use lightning::ln::peer_handler::{ ErroringMessageHandler, IgnoringMessageHandler, MessageHandler, PeerManager as LdkPeerManager, }; @@ -18,8 +18,9 @@ use p2pd_oracle_client::P2PDOracleClient; use std::collections::hash_map::HashMap; use std::env; use std::fs; -use std::sync::{Arc, Mutex}; +use std::sync::Arc; use std::time::SystemTime; +use tokio::sync::Mutex; pub(crate) type PeerManager = LdkPeerManager< SocketDescriptor, @@ -31,7 +32,7 @@ pub(crate) type PeerManager = LdkPeerManager< Arc, >; -pub(crate) type DlcManager = dlc_manager::manager::Manager< +pub(crate) type DlcManager = ddk_manager::manager::Manager< Arc, Arc, SimpleSigner>>, Arc, @@ -72,17 +73,15 @@ async fn main() { // client uses reqwest in blocking mode to satisfy the non async oracle interface // so we need to use `spawn_blocking`. let oracle_host = config.oracle_config.host; - let oracle = tokio::task::spawn_blocking(move || { - P2PDOracleClient::new(&oracle_host).expect("Error creating oracle client") - }) - .await - .unwrap(); + let oracle = P2PDOracleClient::new(&oracle_host) + .await + .expect("Error creating oracle client"); let mut oracles = HashMap::new(); oracles.insert(oracle.get_public_key(), Box::new(oracle)); // Instantiate a DlcManager. let dlc_manager = Arc::new(Mutex::new( - dlc_manager::manager::Manager::new( + ddk_manager::manager::Manager::new( bitcoind_provider.clone(), bitcoind_provider.clone(), bitcoind_provider.clone(), @@ -91,9 +90,10 @@ async fn main() { .expect("Error creating storage."), ), oracles, - Arc::new(dlc_manager::SystemTimeProvider {}), + Arc::new(ddk_manager::SystemTimeProvider {}), bitcoind_provider.clone(), ) + .await .expect("Could not create manager."), )); diff --git a/sample/tests/cli_tests.rs b/sample/tests/cli_tests.rs index e3d1cbe7..018712de 100644 --- a/sample/tests/cli_tests.rs +++ b/sample/tests/cli_tests.rs @@ -1,7 +1,7 @@ use std::{process::Command, thread::sleep, time::Duration}; use assert_cmd::cargo::cargo_bin; -use dlc_manager::contract::contract_input::ContractInput; +use ddk_manager::contract::contract_input::ContractInput; use rexpect::session::{spawn_command, PtySession}; #[test] diff --git a/simple-wallet/Cargo.toml b/simple-wallet/Cargo.toml index 268e296e..686ed301 100644 --- a/simple-wallet/Cargo.toml +++ b/simple-wallet/Cargo.toml @@ -6,10 +6,11 @@ version = "0.1.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +async-trait = "0.1.83" bdk_wallet = "1.0.0-beta.2" bitcoin = "0.32.2" -dlc = {path = "../dlc"} -dlc-manager = {path = "../dlc-manager"} +ddk-dlc = {path = "../ddk-dlc"} +ddk-manager = {path = "../ddk-manager"} lightning = {version = "0.0.125"} secp256k1-zkp = {version = "0.11.0"} diff --git a/simple-wallet/src/lib.rs b/simple-wallet/src/lib.rs index a2ed414b..2ac6f04e 100644 --- a/simple-wallet/src/lib.rs +++ b/simple-wallet/src/lib.rs @@ -10,7 +10,7 @@ use bitcoin::{ Sequence, Transaction, TxIn, TxOut, Txid, Weight, Witness, }; use bitcoin::{psbt::Psbt, ScriptBuf}; -use dlc_manager::{ +use ddk_manager::{ error::Error, Blockchain, ContractSignerProvider, KeysId, SimpleSigner, Utxo, Wallet, }; use lightning::chain::chaininterface::{ConfirmationTarget, FeeEstimator}; @@ -19,10 +19,11 @@ use secp256k1_zkp::{rand::thread_rng, All, PublicKey, Secp256k1, SecretKey}; type Result = core::result::Result; +#[async_trait::async_trait] /// Trait providing blockchain information to the wallet. pub trait WalletBlockchainProvider: Blockchain + FeeEstimator { - fn get_utxos_for_address(&self, address: &Address) -> Result>; - fn is_output_spent(&self, txid: &Txid, vout: u32) -> Result; + async fn get_utxos_for_address(&self, address: &Address) -> Result>; + async fn is_output_spent(&self, txid: &Txid, vout: u32) -> Result; } /// Trait enabling the wallet to persist data. @@ -68,13 +69,14 @@ where } /// Refresh the wallet checking and updating the UTXO states. - pub fn refresh(&self) -> Result<()> { + pub async fn refresh(&self) -> Result<()> { let utxos: Vec = self.storage.get_utxos()?; for utxo in &utxos { let is_spent = self .blockchain - .is_output_spent(&utxo.outpoint.txid, utxo.outpoint.vout)?; + .is_output_spent(&utxo.outpoint.txid, utxo.outpoint.vout) + .await?; if is_spent { self.storage.delete_utxo(utxo)?; } @@ -83,7 +85,7 @@ where let addresses = self.storage.get_addresses()?; for address in &addresses { - let utxos = self.blockchain.get_utxos_for_address(address)?; + let utxos = self.blockchain.get_utxos_for_address(address).await?; for utxo in &utxos { if !self.storage.has_utxo(utxo)? { @@ -117,7 +119,7 @@ where /// Creates a transaction with all wallet UTXOs as inputs and a single output /// sending everything to the given address. - pub fn empty_to_address(&self, address: &Address) -> Result<()> { + pub async fn empty_to_address(&self, address: &Address) -> Result<()> { let utxos = self .storage .get_utxos() @@ -172,7 +174,7 @@ where .extract_tx() .expect("could not extract transaction from psbt"); - self.blockchain.send_transaction(&tx) + self.blockchain.send_transaction(&tx).await } } @@ -341,7 +343,7 @@ where .expect("to have the requested private key"); let mut tx = psbt.unsigned_tx.clone(); - dlc::util::sign_p2wpkh_input( + ddk_dlc::util::sign_p2wpkh_input( &self.secp_ctx, &seckey, &mut tx, @@ -361,7 +363,7 @@ where mod tests { use std::rc::Rc; - use dlc_manager::ContractSignerProvider; + use ddk_manager::ContractSignerProvider; use mocks::simple_wallet::SimpleWallet; use mocks::{memory_storage_provider::MemoryStorage, mock_blockchain::MockBlockchain}; use secp256k1_zkp::{PublicKey, SECP256K1};