Skip to content

Commit

Permalink
Upgrade to LDK 0.0.116
Browse files Browse the repository at this point in the history
  • Loading branch information
tnull committed Jul 25, 2023
1 parent 30a9cb7 commit 10f32f7
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 64 deletions.
28 changes: 14 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
lightning = { version = "0.0.115", features = ["max_level_trace"] }
lightning-block-sync = { version = "0.0.115", features = [ "rpc-client" ] }
lightning-invoice = { version = "0.23" }
lightning-net-tokio = { version = "0.0.115" }
lightning-persister = { version = "0.0.115" }
lightning-background-processor = { version = "0.0.115", features = [ "futures" ] }
lightning-rapid-gossip-sync = { version = "0.0.115" }
lightning = { version = "0.0.116", features = ["max_level_trace"] }
lightning-block-sync = { version = "0.0.116", features = [ "rpc-client" ] }
lightning-invoice = { version = "0.24.0" }
lightning-net-tokio = { version = "0.0.116" }
lightning-persister = { version = "0.0.116" }
lightning-background-processor = { version = "0.0.116", features = [ "futures" ] }
lightning-rapid-gossip-sync = { version = "0.0.116" }

base64 = "0.13.0"
bitcoin = "0.29.0"
Expand Down
72 changes: 48 additions & 24 deletions src/bitcoind_client.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::convert::{BlockchainInfo, FeeResponse, FundedTx, NewAddress, RawTx, SignedTx};
use crate::convert::{
BlockchainInfo, FeeResponse, FundedTx, MempoolMinFeeResponse, NewAddress, RawTx, SignedTx,
};
use crate::disk::FilesystemLogger;
use base64;
use bitcoin::blockdata::transaction::Transaction;
Expand Down Expand Up @@ -32,6 +34,7 @@ pub struct BitcoindClient {

#[derive(Clone, Eq, Hash, PartialEq)]
pub enum Target {
MempoolMinimum,
Background,
Normal,
HighPriority,
Expand Down Expand Up @@ -75,6 +78,7 @@ impl BitcoindClient {
"Failed to make initial call to bitcoind - please check your RPC user/password and access settings")
})?;
let mut fees: HashMap<Target, AtomicU32> = HashMap::new();
fees.insert(Target::MempoolMinimum, AtomicU32::new(MIN_FEERATE));
fees.insert(Target::Background, AtomicU32::new(MIN_FEERATE));
fees.insert(Target::Normal, AtomicU32::new(2000));
fees.insert(Target::HighPriority, AtomicU32::new(5000));
Expand Down Expand Up @@ -102,6 +106,16 @@ impl BitcoindClient {
) {
handle.spawn(async move {
loop {
let mempoolmin_estimate = {
let resp = rpc_client
.call_method::<MempoolMinFeeResponse>("getmempoolinfo", &vec![])
.await
.unwrap();
match resp.feerate_sat_per_kw {
Some(feerate) => std::cmp::max(feerate, MIN_FEERATE),
None => MIN_FEERATE,
}
};
let background_estimate = {
let background_conf_target = serde_json::json!(144);
let background_estimate_mode = serde_json::json!("ECONOMICAL");
Expand Down Expand Up @@ -151,6 +165,9 @@ impl BitcoindClient {
}
};

fees.get(&Target::MempoolMinimum)
.unwrap()
.store(mempoolmin_estimate, Ordering::Release);
fees.get(&Target::Background)
.unwrap()
.store(background_estimate, Ordering::Release);
Expand Down Expand Up @@ -238,6 +255,9 @@ impl BitcoindClient {
impl FeeEstimator for BitcoindClient {
fn get_est_sat_per_1000_weight(&self, confirmation_target: ConfirmationTarget) -> u32 {
match confirmation_target {
ConfirmationTarget::MempoolMinimum => {
self.fees.get(&Target::MempoolMinimum).unwrap().load(Ordering::Acquire)
}
ConfirmationTarget::Background => {
self.fees.get(&Target::Background).unwrap().load(Ordering::Acquire)
}
Expand All @@ -252,29 +272,33 @@ impl FeeEstimator for BitcoindClient {
}

impl BroadcasterInterface for BitcoindClient {
fn broadcast_transaction(&self, tx: &Transaction) {
let bitcoind_rpc_client = self.bitcoind_rpc_client.clone();
let tx_serialized = encode::serialize_hex(tx);
let tx_json = serde_json::json!(tx_serialized);
let logger = Arc::clone(&self.logger);
self.handle.spawn(async move {
// This may error due to RL calling `broadcast_transaction` with the same transaction
// multiple times, but the error is safe to ignore.
match bitcoind_rpc_client
.call_method::<Txid>("sendrawtransaction", &vec![tx_json])
.await
{
Ok(_) => {}
Err(e) => {
let err_str = e.get_ref().unwrap().to_string();
log_error!(logger,
"Warning, failed to broadcast a transaction, this is likely okay but may indicate an error: {}\nTransaction: {}",
err_str,
tx_serialized);
print!("Warning, failed to broadcast a transaction, this is likely okay but may indicate an error: {}\n> ", err_str);
}
}
});
fn broadcast_transactions(&self, txs: &[&Transaction]) {
// TODO: Rather than calling `sendrawtransaction` in a a loop, we should probably use
// `submitpackage` once it becomes available.
for tx in txs {
let bitcoind_rpc_client = Arc::clone(&self.bitcoind_rpc_client);
let tx_serialized = encode::serialize_hex(tx);
let tx_json = serde_json::json!(tx_serialized);
let logger = Arc::clone(&self.logger);
self.handle.spawn(async move {
// This may error due to RL calling `broadcast_transactions` with the same transaction
// multiple times, but the error is safe to ignore.
match bitcoind_rpc_client
.call_method::<Txid>("sendrawtransaction", &vec![tx_json])
.await
{
Ok(_) => {}
Err(e) => {
let err_str = e.get_ref().unwrap().to_string();
log_error!(logger,
"Warning, failed to broadcast a transaction, this is likely okay but may indicate an error: {}\nTransaction: {}",
err_str,
tx_serialized);
print!("Warning, failed to broadcast a transaction, this is likely okay but may indicate an error: {}\n> ", err_str);
}
}
});
}
}
}

Expand Down
21 changes: 11 additions & 10 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@ use bitcoin::hashes::sha256::Hash as Sha256;
use bitcoin::hashes::Hash;
use bitcoin::network::constants::Network;
use bitcoin::secp256k1::PublicKey;
use lightning::chain::keysinterface::{EntropySource, KeysManager};
use lightning::ln::channelmanager::{PaymentId, RecipientOnionFields, Retry};
use lightning::ln::msgs::NetAddress;
use lightning::ln::{PaymentHash, PaymentPreimage};
use lightning::onion_message::OnionMessagePath;
use lightning::onion_message::{CustomOnionMessageContents, Destination, OnionMessageContents};
use lightning::routing::gossip::NodeId;
use lightning::routing::router::{PaymentParameters, RouteParameters};
use lightning::sign::{EntropySource, KeysManager};
use lightning::util::config::{ChannelHandshakeConfig, ChannelHandshakeLimits, UserConfig};
use lightning::util::ser::{Writeable, Writer};
use lightning_invoice::payment::pay_invoice;
use lightning_invoice::{utils, Currency, Invoice};
use lightning_invoice::{utils, Bolt11Invoice, Currency};
use std::env;
use std::io;
use std::io::Write;
Expand Down Expand Up @@ -149,7 +150,7 @@ pub(crate) async fn poll_for_user_input(
continue;
}

let invoice = match Invoice::from_str(invoice_str.unwrap()) {
let invoice = match Bolt11Invoice::from_str(invoice_str.unwrap()) {
Ok(inv) => inv,
Err(e) => {
println!("ERROR: invalid invoice: {:?}", e);
Expand Down Expand Up @@ -377,7 +378,7 @@ pub(crate) async fn poll_for_user_input(
);
continue;
}
let mut node_pks = Vec::new();
let mut intermediate_nodes = Vec::new();
let mut errored = false;
for pk_str in path_pks_str.unwrap().split(",") {
let node_pubkey_vec = match hex_utils::to_vec(pk_str) {
Expand All @@ -396,7 +397,7 @@ pub(crate) async fn poll_for_user_input(
break;
}
};
node_pks.push(node_pubkey);
intermediate_nodes.push(node_pubkey);
}
if errored {
continue;
Expand All @@ -415,10 +416,10 @@ pub(crate) async fn poll_for_user_input(
continue;
}
};
let destination_pk = node_pks.pop().unwrap();
let destination = Destination::Node(intermediate_nodes.pop().unwrap());
let message_path = OnionMessagePath { intermediate_nodes, destination };
match onion_messenger.send_onion_message(
&node_pks,
Destination::Node(destination_pk),
message_path,
OnionMessageContents::Custom(UserOnionMessageContents { tlv_type, data }),
None,
) {
Expand Down Expand Up @@ -658,7 +659,7 @@ fn open_channel(
}

fn send_payment(
channel_manager: &ChannelManager, invoice: &Invoice, payment_storage: PaymentInfoStorage,
channel_manager: &ChannelManager, invoice: &Bolt11Invoice, payment_storage: PaymentInfoStorage,
) {
let status =
match pay_invoice(invoice, Retry::Timeout(Duration::from_secs(10)), channel_manager) {
Expand Down Expand Up @@ -698,7 +699,7 @@ fn keysend<E: EntropySource>(
let payment_hash = PaymentHash(Sha256::hash(&payment_preimage.0[..]).into_inner());

let route_params = RouteParameters {
payment_params: PaymentParameters::for_keysend(payee_pubkey, 40),
payment_params: PaymentParameters::for_keysend(payee_pubkey, 40, false),
final_value_msat: amt_msat,
};
let status = match channel_manager.send_spontaneous_payment_with_retry(
Expand Down
25 changes: 25 additions & 0 deletions src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,31 @@ impl TryInto<FeeResponse> for JsonResponse {
}
}

pub struct MempoolMinFeeResponse {
pub feerate_sat_per_kw: Option<u32>,
pub errored: bool,
}

impl TryInto<MempoolMinFeeResponse> for JsonResponse {
type Error = std::io::Error;
fn try_into(self) -> std::io::Result<MempoolMinFeeResponse> {
let errored = !self.0["errors"].is_null();
assert_eq!(self.0["maxmempool"].as_u64(), Some(300000000));
Ok(MempoolMinFeeResponse {
errored,
feerate_sat_per_kw: match self.0["mempoolminfee"].as_f64() {
// Bitcoin Core gives us a feerate in BTC/KvB, which we need to convert to
// satoshis/KW. Thus, we first multiply by 10^8 to get satoshis, then divide by 4
// to convert virtual-bytes into weight units.
Some(feerate_btc_per_kvbyte) => {
Some((feerate_btc_per_kvbyte * 100_000_000.0 / 4.0).round() as u32)
}
None => None,
},
})
}
}

pub struct BlockchainInfo {
pub latest_height: usize,
pub latest_blockhash: BlockHash,
Expand Down
4 changes: 2 additions & 2 deletions src/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{cli, NetworkGraph};
use bitcoin::secp256k1::PublicKey;
use bitcoin::Network;
use chrono::Utc;
use lightning::routing::scoring::{ProbabilisticScorer, ProbabilisticScoringParameters};
use lightning::routing::scoring::{ProbabilisticScorer, ProbabilisticScoringDecayParameters};
use lightning::util::logger::{Logger, Record};
use lightning::util::ser::{ReadableArgs, Writer};
use std::collections::HashMap;
Expand Down Expand Up @@ -86,7 +86,7 @@ pub(crate) fn read_network(
pub(crate) fn read_scorer(
path: &Path, graph: Arc<NetworkGraph>, logger: Arc<FilesystemLogger>,
) -> ProbabilisticScorer<Arc<NetworkGraph>, Arc<FilesystemLogger>> {
let params = ProbabilisticScoringParameters::default();
let params = ProbabilisticScoringDecayParameters::default();
if let Ok(file) = File::open(path) {
let args = (params.clone(), Arc::clone(&graph), Arc::clone(&logger));
if let Ok(scorer) = ProbabilisticScorer::read(&mut BufReader::new(file), args) {
Expand Down
Loading

0 comments on commit 10f32f7

Please sign in to comment.