Skip to content

Commit

Permalink
Merge pull request #136 from benthecarman/ldk-116
Browse files Browse the repository at this point in the history
Update ldk to v0.0.116
  • Loading branch information
Tibo-lg authored Aug 15, 2023
2 parents c87136f + c053d4e commit 2c5a9a3
Show file tree
Hide file tree
Showing 14 changed files with 71 additions and 63 deletions.
2 changes: 1 addition & 1 deletion bitcoin-rpc-provider/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ bitcoin = {version = "0.29.2"}
bitcoincore-rpc = {version = "0.16.0"}
bitcoincore-rpc-json = {version = "0.16.0"}
dlc-manager = {path = "../dlc-manager"}
lightning = {version = "0.0.113"}
lightning = {version = "0.0.116"}
log = "0.4.14"
rust-bitcoin-coin-selection = {version = "0.1.0", git = "https://github.com/p2pderivatives/rust-bitcoin-coin-selection", features = ["rand"]}
simple-wallet = {path = "../simple-wallet"}
54 changes: 17 additions & 37 deletions bitcoin-rpc-provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,11 @@ use rust_bitcoin_coin_selection::select_coins;
/// The minimum feerate we are allowed to send, as specify by LDK.
const MIN_FEERATE: u32 = 253;

#[derive(Clone, Eq, Hash, PartialEq)]
pub enum Target {
Background,
Normal,
HighPriority,
}

pub struct BitcoinCoreProvider {
client: Arc<Mutex<Client>>,
// Used to implement the FeeEstimator interface, heavily inspired by
// https://github.com/lightningdevkit/ldk-sample/blob/main/src/bitcoind_client.rs#L26
fees: Arc<HashMap<Target, AtomicU32>>,
fees: Arc<HashMap<ConfirmationTarget, AtomicU32>>,
}

#[derive(Debug)]
Expand Down Expand Up @@ -107,10 +100,10 @@ impl BitcoinCoreProvider {

pub fn new_from_rpc_client(rpc_client: Client) -> Self {
let client = Arc::new(Mutex::new(rpc_client));
let mut fees: HashMap<Target, AtomicU32> = HashMap::new();
fees.insert(Target::Background, AtomicU32::new(MIN_FEERATE));
fees.insert(Target::Normal, AtomicU32::new(2000));
fees.insert(Target::HighPriority, AtomicU32::new(5000));
let mut fees: HashMap<ConfirmationTarget, AtomicU32> = HashMap::new();
fees.insert(ConfirmationTarget::Background, AtomicU32::new(MIN_FEERATE));
fees.insert(ConfirmationTarget::Normal, AtomicU32::new(2000));
fees.insert(ConfirmationTarget::HighPriority, AtomicU32::new(5000));
let fees = Arc::new(fees);
poll_for_fee_estimates(client.clone(), fees.clone());
BitcoinCoreProvider { client, fees }
Expand Down Expand Up @@ -372,35 +365,22 @@ impl Blockchain for BitcoinCoreProvider {
}

impl FeeEstimator for BitcoinCoreProvider {
fn get_est_sat_per_1000_weight(
&self,
confirmation_target: lightning::chain::chaininterface::ConfirmationTarget,
) -> u32 {
match confirmation_target {
ConfirmationTarget::Background => self
.fees
.get(&Target::Background)
.unwrap()
.load(Ordering::Acquire),
ConfirmationTarget::Normal => self
.fees
.get(&Target::Normal)
.unwrap()
.load(Ordering::Acquire),
ConfirmationTarget::HighPriority => self
.fees
.get(&Target::HighPriority)
.unwrap()
.load(Ordering::Acquire),
}
fn get_est_sat_per_1000_weight(&self, confirmation_target: ConfirmationTarget) -> u32 {
self.fees
.get(&confirmation_target)
.unwrap()
.load(Ordering::Acquire)
}
}

fn poll_for_fee_estimates(client: Arc<Mutex<Client>>, fees: Arc<HashMap<Target, AtomicU32>>) {
fn poll_for_fee_estimates(
client: Arc<Mutex<Client>>,
fees: Arc<HashMap<ConfirmationTarget, AtomicU32>>,
) {
std::thread::spawn(move || loop {
match query_fee_estimate(&client, 144, EstimateMode::Economical) {
Ok(fee_rate) => {
fees.get(&Target::Background)
fees.get(&ConfirmationTarget::Background)
.unwrap()
.store(fee_rate, Ordering::Release);
}
Expand All @@ -410,7 +390,7 @@ fn poll_for_fee_estimates(client: Arc<Mutex<Client>>, fees: Arc<HashMap<Target,
};
match query_fee_estimate(&client, 18, EstimateMode::Conservative) {
Ok(fee_rate) => {
fees.get(&Target::Normal)
fees.get(&ConfirmationTarget::Normal)
.unwrap()
.store(fee_rate, Ordering::Release);
}
Expand All @@ -420,7 +400,7 @@ fn poll_for_fee_estimates(client: Arc<Mutex<Client>>, fees: Arc<HashMap<Target,
};
match query_fee_estimate(&client, 6, EstimateMode::Conservative) {
Ok(fee_rate) => {
fees.get(&Target::HighPriority)
fees.get(&ConfirmationTarget::HighPriority)
.unwrap()
.store(fee_rate, Ordering::Release);
}
Expand Down
2 changes: 1 addition & 1 deletion dlc-manager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ bitcoin = {version = "0.29.2"}
dlc = {version = "0.4.0", path = "../dlc"}
dlc-messages = {version = "0.4.0", path = "../dlc-messages"}
dlc-trie = {version = "0.4.0", path = "../dlc-trie"}
lightning = {version = "0.0.113"}
lightning = {version = "0.0.116" }
log = "0.4.14"
rand_chacha = {version = "0.3.1", optional = true}
secp256k1-zkp = {version = "0.7.0", features = ["bitcoin_hashes", "rand", "rand-std"]}
Expand Down
2 changes: 1 addition & 1 deletion dlc-messages/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use-serde = ["serde", "secp256k1-zkp/use-serde"]
[dependencies]
bitcoin = {version = "0.29.2"}
dlc = {version = "0.4.0", path = "../dlc"}
lightning = {version = "0.0.113" }
lightning = {version = "0.0.116" }
secp256k1-zkp = {version = "0.7.0", features = ["bitcoin_hashes", "rand", "rand-std"]}
serde = {version = "1.0", features = ["derive"], optional = true}

Expand Down
9 changes: 9 additions & 0 deletions dlc-messages/src/message_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::{
sync::Mutex,
};

use lightning::ln::features::{InitFeatures, NodeFeatures};
use lightning::{
ln::{
msgs::{DecodeError, LightningError},
Expand Down Expand Up @@ -208,6 +209,14 @@ impl CustomMessageHandler for MessageHandler {
fn get_and_clear_pending_msg(&self) -> Vec<(PublicKey, Self::CustomMessage)> {
self.msg_events.lock().unwrap().drain(..).collect()
}

fn provided_node_features(&self) -> NodeFeatures {
NodeFeatures::empty()
}

fn provided_init_features(&self, _their_node_id: &PublicKey) -> InitFeatures {
InitFeatures::empty()
}
}

#[inline]
Expand Down
2 changes: 1 addition & 1 deletion dlc-sled-storage-provider/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ wallet = ["bitcoin", "secp256k1-zkp", "simple-wallet", "lightning"]
[dependencies]
bitcoin = {version = "0.29", optional = true}
dlc-manager = {path = "../dlc-manager"}
lightning = {version = "0.0.113", optional = true}
lightning = {version = "0.0.116", optional = true}
secp256k1-zkp = {version = "0.7", optional = true}
simple-wallet = {path = "../simple-wallet", optional = true}
sled = "0.34"
4 changes: 2 additions & 2 deletions electrs-blockchain-provider/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ version = "0.1.0"
bitcoin = {version = "0.29"}
bitcoin-test-utils = {path = "../bitcoin-test-utils"}
dlc-manager = {path = "../dlc-manager"}
lightning = {version = "0.0.113"}
lightning-block-sync = {version = "0.0.113"}
lightning = {version = "0.0.116"}
lightning-block-sync = {version = "0.0.116"}
reqwest = {version = "0.11", features = ["blocking", "json"]}
rust-bitcoin-coin-selection = {version = "0.1.0", git = "https://github.com/p2pderivatives/rust-bitcoin-coin-selection", features = ["rand"]}
serde = {version = "*", features = ["derive"]}
Expand Down
31 changes: 21 additions & 10 deletions electrs-blockchain-provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const MIN_FEERATE: u32 = 253;

#[derive(Clone, Eq, Hash, PartialEq)]
pub enum Target {
Minimum = 1008,
Background = 144,
Normal = 18,
HighPriority = 6,
Expand Down Expand Up @@ -202,6 +203,11 @@ impl simple_wallet::WalletBlockchainProvider for ElectrsBlockchainProvider {
impl FeeEstimator for ElectrsBlockchainProvider {
fn get_est_sat_per_1000_weight(&self, confirmation_target: ConfirmationTarget) -> u32 {
let est = match confirmation_target {
ConfirmationTarget::MempoolMinimum => self
.fees
.get(&Target::Minimum)
.unwrap()
.load(Ordering::Acquire),
ConfirmationTarget::Background => self
.fees
.get(&Target::Background)
Expand Down Expand Up @@ -304,20 +310,25 @@ impl BlockSource for ElectrsBlockchainProvider {
}

impl BroadcasterInterface for ElectrsBlockchainProvider {
fn broadcast_transaction(&self, tx: &Transaction) {
fn broadcast_transactions(&self, txs: &[&Transaction]) {
let client = self.client.clone();
let host = self.host.clone();
let body = bitcoin_test_utils::tx_to_string(tx);
let bodies = txs
.iter()
.map(|tx| bitcoin_test_utils::tx_to_string(tx))
.collect::<Vec<_>>();
std::thread::spawn(move || {
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
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
}
}
}
};
};
}
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ cargo-fuzz = true
[dependencies]
dlc-messages = {path = "../dlc-messages"}
honggfuzz = "0.5"
lightning = {version = "0.0.113" }
lightning = {version = "0.0.116" }

[workspace]
members = ["."]
2 changes: 1 addition & 1 deletion mocks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ bitcoin = "0.29"
dlc = {path = "../dlc"}
dlc-manager = {path = "../dlc-manager"}
dlc-messages = {path = "../dlc-messages"}
lightning = {version = "0.0.113"}
lightning = {version = "0.0.116"}
secp256k1-zkp = {version = "0.7.0", features = ["bitcoin_hashes", "global-context", "rand", "rand-std"]}
simple-wallet = {path = "../simple-wallet"}
4 changes: 2 additions & 2 deletions sample/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ dlc-manager = {path = "../dlc-manager", features = ["use-serde", "parallel"]}
dlc-messages = {path = "../dlc-messages"}
dlc-sled-storage-provider = {path = "../dlc-sled-storage-provider"}
futures = "0.3"
lightning = {version = "0.0.113"}
lightning-net-tokio = {version = "0.0.113" }
lightning = {version = "0.0.116"}
lightning-net-tokio = {version = "0.0.116" }
p2pd-oracle-client = {path = "../p2pd-oracle-client"}
serde = "1.0"
serde_json = "1.0"
Expand Down
6 changes: 3 additions & 3 deletions sample/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ fn help() {

fn list_peers(peer_manager: Arc<PeerManager>) {
println!("\t{{");
for pubkey in peer_manager.get_peer_node_ids() {
for (pubkey, _) in peer_manager.get_peer_node_ids() {
println!("\t\t pubkey: {}", pubkey);
}
println!("\t}},");
Expand All @@ -546,7 +546,7 @@ pub(crate) async fn connect_peer_if_necessary(
peer_addr: SocketAddr,
peer_manager: Arc<PeerManager>,
) -> Result<(), ()> {
for node_pubkey in peer_manager.get_peer_node_ids() {
for (node_pubkey, _) in peer_manager.get_peer_node_ids() {
if node_pubkey == pubkey {
return Ok(());
}
Expand All @@ -567,7 +567,7 @@ pub(crate) async fn connect_peer_if_necessary(
match peer_manager
.get_peer_node_ids()
.iter()
.find(|id| **id == pubkey)
.find(|id| id.0 == pubkey)
{
Some(_) => break,
None => tokio::time::sleep(Duration::from_millis(10)).await,
Expand Down
12 changes: 10 additions & 2 deletions sample/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use dlc_messages::message_handler::MessageHandler as DlcMessageHandler;
use lightning::ln::peer_handler::{
ErroringMessageHandler, IgnoringMessageHandler, MessageHandler, PeerManager as LdkPeerManager,
};
use lightning::sign::KeysManager;
use lightning_net_tokio::SocketDescriptor;
use p2pd_oracle_client::P2PDOracleClient;
use std::collections::hash_map::HashMap;
Expand All @@ -27,6 +28,7 @@ pub(crate) type PeerManager = LdkPeerManager<
Arc<IgnoringMessageHandler>,
Arc<FilesystemLogger>,
Arc<DlcMessageHandler>,
Arc<KeysManager>,
>;

pub(crate) type DlcManager = dlc_manager::manager::Manager<
Expand Down Expand Up @@ -122,18 +124,24 @@ async fn main() {
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap();

let km = Arc::new(KeysManager::new(
&sk.secret_bytes(),
time.as_secs(),
time.as_nanos() as u32,
));

// The peer manager helps us establish connections and communicate with our peers.
let peer_manager: Arc<PeerManager> = Arc::new(PeerManager::new(
MessageHandler {
chan_handler: Arc::new(ErroringMessageHandler::new()),
route_handler: Arc::new(IgnoringMessageHandler {}),
onion_message_handler: Arc::new(IgnoringMessageHandler {}),
custom_message_handler: dlc_message_handler.clone(),
},
sk,
time.as_secs() as u32,
&ephemeral_bytes,
logger.clone(),
dlc_message_handler.clone(),
km,
));

let peer_manager_connection_handler = peer_manager.clone();
Expand Down
2 changes: 1 addition & 1 deletion simple-wallet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ version = "0.1.0"
bitcoin = "0.29"
dlc = {path = "../dlc"}
dlc-manager = {path = "../dlc-manager"}
lightning = {version = "0.0.113"}
lightning = {version = "0.0.116"}
rust-bitcoin-coin-selection = {version = "0.1.0", git = "https://github.com/p2pderivatives/rust-bitcoin-coin-selection", features = ["rand"]}
secp256k1-zkp = {version = "0.7.0"}

Expand Down

0 comments on commit 2c5a9a3

Please sign in to comment.