Skip to content

Commit

Permalink
async manager
Browse files Browse the repository at this point in the history
  • Loading branch information
bennyhodl committed Nov 26, 2024
1 parent 1409937 commit f34063c
Show file tree
Hide file tree
Showing 23 changed files with 1,227 additions and 950 deletions.
1 change: 1 addition & 0 deletions bitcoin-rpc-provider/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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"}
Expand Down
11 changes: 6 additions & 5 deletions bitcoin-rpc-provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -418,23 +419,23 @@ impl Blockchain for BitcoinCoreProvider {
Ok(network)
}

fn get_blockchain_height(&self) -> Result<u64, ManagerError> {
async fn get_blockchain_height(&self) -> Result<u64, ManagerError> {
self.client
.lock()
.unwrap()
.get_block_count()
.map_err(rpc_err_to_manager_err)
}

fn get_block_at_height(&self, height: u64) -> Result<bitcoin::Block, ManagerError> {
async fn get_block_at_height(&self, height: u64) -> Result<bitcoin::Block, ManagerError> {
let client = self.client.lock().unwrap();
let hash = client
.get_block_hash(height)
.map_err(rpc_err_to_manager_err)?;
client.get_block(&hash).map_err(rpc_err_to_manager_err)
}

fn get_transaction(&self, tx_id: &Txid) -> Result<Transaction, ManagerError> {
async fn get_transaction(&self, tx_id: &Txid) -> Result<Transaction, ManagerError> {
let tx_info = self
.client
.lock()
Expand All @@ -446,7 +447,7 @@ impl Blockchain for BitcoinCoreProvider {
Ok(tx)
}

fn get_transaction_confirmations(&self, tx_id: &Txid) -> Result<u32, ManagerError> {
async fn get_transaction_confirmations(&self, tx_id: &Txid) -> Result<u32, ManagerError> {
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),
Expand Down
4 changes: 3 additions & 1 deletion ddk-manager/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
authors = ["Crypto Garage", "benny b <ben@bitcoinbay.foundation>"]
description = "Creation and handling of Discrete Log Contracts (DLC)."
edition = "2018"
edition = "2021"
homepage = "https://github.com/bennyhodl/rust-dlc"
license-file = "../LICENSE"
name = "ddk-manager"
Expand All @@ -21,6 +21,7 @@ bitcoin = { version = "0.32.2", default-features = false }
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"
Expand All @@ -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
Expand Down
10 changes: 6 additions & 4 deletions ddk-manager/src/channel_updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<C: Signing, W: Deref, SP: Deref, B: Deref, T: Deref, X: ContractSigner>(
pub async fn offer_channel<C: Signing, W: Deref, SP: Deref, B: Deref, T: Deref, X: ContractSigner>(
secp: &Secp256k1<C>,
contract: &ContractInput,
counter_party: &PublicKey,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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<W: Deref, SP: Deref, B: Deref, X: ContractSigner>(
pub async fn accept_channel_offer<W: Deref, SP: Deref, B: Deref, X: ContractSigner>(
secp: &Secp256k1<All>,
offered_channel: &OfferedChannel,
offered_contract: &OfferedContract,
Expand All @@ -167,7 +168,8 @@ where
wallet,
&signer,
blockchain,
)?;
)
.await?;

let per_update_seed = signer_provider.get_new_secret_key()?;

Expand Down
28 changes: 18 additions & 10 deletions ddk-manager/src/contract_updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ use crate::{

/// Creates an [`OfferedContract`] and [`OfferDlc`] message from the provided
/// contract and oracle information.
pub fn offer_contract<W: Deref, B: Deref, T: Deref, X: ContractSigner, SP: Deref, C: Signing>(
pub async fn offer_contract<
W: Deref,
B: Deref,
T: Deref,
X: ContractSigner,
SP: Deref,
C: Signing,
>(
secp: &Secp256k1<C>,
contract_input: &ContractInput,
oracle_announcements: Vec<Vec<OracleAnnouncement>>,
Expand Down Expand Up @@ -56,7 +63,8 @@ where
wallet,
&signer,
blockchain,
)?;
)
.await?;

let offered_contract = OfferedContract::new(
id,
Expand All @@ -77,7 +85,7 @@ where

/// Creates an [`AcceptedContract`] and produces
/// the accepting party's cet adaptor signatures.
pub fn accept_contract<W: Deref, X: ContractSigner, SP: Deref, B: Deref>(
pub async fn accept_contract<W: Deref, X: ContractSigner, SP: Deref, B: Deref>(
secp: &Secp256k1<All>,
offered_contract: &OfferedContract,
wallet: &W,
Expand All @@ -99,7 +107,8 @@ where
wallet,
&signer,
blockchain,
)?;
)
.await?;

let dlc_transactions = ddk_dlc::create_dlc_transactions(
&offered_contract.offer_params,
Expand Down Expand Up @@ -765,8 +774,8 @@ mod tests {
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 =
Expand All @@ -780,10 +789,8 @@ 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::ddk_manager::contract_updater::accept_contract(
secp256k1_zkp::SECP256K1,
Expand All @@ -792,6 +799,7 @@ mod tests {
&wallet,
&blockchain,
)
.await
.expect("Not to fail");
}
}
16 changes: 9 additions & 7 deletions ddk-manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bitcoin::Network, Error>;
/// Returns the height of the blockchain
fn get_blockchain_height(&self) -> Result<u64, Error>;
async fn get_blockchain_height(&self) -> Result<u64, Error>;
/// Returns the block at given height
fn get_block_at_height(&self, height: u64) -> Result<Block, Error>;
async fn get_block_at_height(&self, height: u64) -> Result<Block, Error>;
/// Get the transaction with given id.
fn get_transaction(&self, tx_id: &Txid) -> Result<Transaction, Error>;
async fn get_transaction(&self, tx_id: &Txid) -> Result<Transaction, Error>;
/// Get the number of confirmation for the transaction with given id.
fn get_transaction_confirmations(&self, tx_id: &Txid) -> Result<u32, Error>;
async fn get_transaction_confirmations(&self, tx_id: &Txid) -> Result<u32, Error>;
}

/// Storage trait provides functionalities to store and retrieve DLCs.
Expand Down Expand Up @@ -225,14 +226,15 @@ pub trait Storage {
fn get_chain_monitor(&self) -> Result<Option<ChainMonitor>, 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<OracleAnnouncement, Error>;
async fn get_announcement(&self, event_id: &str) -> Result<OracleAnnouncement, Error>;
/// Returns the attestation for the event with the given id if found.
fn get_attestation(&self, event_id: &str) -> Result<OracleAttestation, Error>;
async fn get_attestation(&self, event_id: &str) -> Result<OracleAttestation, Error>;
}

/// Represents a UTXO.
Expand Down
Loading

0 comments on commit f34063c

Please sign in to comment.