Skip to content

Commit

Permalink
Upgrade to LDK 0.0.124/rust-bitcoin 0.32
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBlueMatt committed Oct 14, 2024
1 parent 6d171e0 commit 9e6de1b
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 108 deletions.
16 changes: 8 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
lightning = { version = "0.0.123", features = ["max_level_trace"] }
lightning-block-sync = { version = "0.0.123", features = [ "rpc-client", "tokio" ] }
lightning-invoice = { version = "0.31.0" }
lightning-net-tokio = { version = "0.0.123" }
lightning-persister = { version = "0.0.123" }
lightning-background-processor = { version = "0.0.123", features = [ "futures" ] }
lightning-rapid-gossip-sync = { version = "0.0.123" }
lightning = { version = "0.0.124", features = ["max_level_trace"] }
lightning-block-sync = { version = "0.0.124", features = [ "rpc-client", "tokio" ] }
lightning-invoice = { version = "0.32.0" }
lightning-net-tokio = { version = "0.0.124" }
lightning-persister = { version = "0.0.124" }
lightning-background-processor = { version = "0.0.124", features = [ "futures" ] }
lightning-rapid-gossip-sync = { version = "0.0.124" }

base64 = "0.13.0"
bitcoin = "0.30.2"
bitcoin = "0.32"
bitcoin-bech32 = "0.12"
bech32 = "0.8"
libc = "0.2"
Expand Down
2 changes: 1 addition & 1 deletion src/args.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::cli::LdkUserInfo;
use bitcoin::network::constants::Network;
use bitcoin::network::Network;
use lightning::ln::msgs::SocketAddress;
use std::collections::HashMap;
use std::env;
Expand Down
57 changes: 42 additions & 15 deletions src/bitcoind_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ use crate::convert::{
use crate::disk::FilesystemLogger;
use crate::hex_utils;
use base64;
use bitcoin::address::{Address, Payload, WitnessVersion};
use bitcoin::address::Address;
use bitcoin::blockdata::constants::WITNESS_SCALE_FACTOR;
use bitcoin::blockdata::script::ScriptBuf;
use bitcoin::blockdata::transaction::Transaction;
use bitcoin::consensus::{encode, Decodable, Encodable};
use bitcoin::hash_types::{BlockHash, Txid};
use bitcoin::hashes::Hash;
use bitcoin::key::XOnlyPublicKey;
use bitcoin::psbt::PartiallySignedTransaction;
use bitcoin::psbt::Psbt;
use bitcoin::{Network, OutPoint, TxOut, WPubkeyHash};
use lightning::chain::chaininterface::{BroadcasterInterface, ConfirmationTarget, FeeEstimator};
use lightning::events::bump_transaction::{Utxo, WalletSource};
Expand Down Expand Up @@ -80,7 +80,8 @@ impl BitcoindClient {
"Failed to make initial call to bitcoind - please check your RPC user/password and access settings")
})?;
let mut fees: HashMap<ConfirmationTarget, AtomicU32> = HashMap::new();
fees.insert(ConfirmationTarget::OnChainSweep, AtomicU32::new(5000));
fees.insert(ConfirmationTarget::MaximumFeeEstimate, AtomicU32::new(50000));
fees.insert(ConfirmationTarget::UrgentOnChainSweep, AtomicU32::new(5000));
fees.insert(
ConfirmationTarget::MinAllowedAnchorChannelRemoteFee,
AtomicU32::new(MIN_FEERATE),
Expand All @@ -92,6 +93,7 @@ impl BitcoindClient {
fees.insert(ConfirmationTarget::AnchorChannelFee, AtomicU32::new(MIN_FEERATE));
fees.insert(ConfirmationTarget::NonAnchorChannelFee, AtomicU32::new(2000));
fees.insert(ConfirmationTarget::ChannelCloseMinimum, AtomicU32::new(MIN_FEERATE));
fees.insert(ConfirmationTarget::OutputSpendingFee, AtomicU32::new(MIN_FEERATE));

let client = Self {
bitcoind_rpc_client: Arc::new(bitcoind_rpc_client),
Expand Down Expand Up @@ -177,7 +179,27 @@ impl BitcoindClient {
}
};

fees.get(&ConfirmationTarget::OnChainSweep)
let very_high_prio_estimate = {
let high_prio_conf_target = serde_json::json!(2);
let high_prio_estimate_mode = serde_json::json!("CONSERVATIVE");
let resp = rpc_client
.call_method::<FeeResponse>(
"estimatesmartfee",
&vec![high_prio_conf_target, high_prio_estimate_mode],
)
.await
.unwrap();

match resp.feerate_sat_per_kw {
Some(feerate) => std::cmp::max(feerate, MIN_FEERATE),
None => 50000,
}
};

fees.get(&ConfirmationTarget::MaximumFeeEstimate)
.unwrap()
.store(very_high_prio_estimate, Ordering::Release);
fees.get(&ConfirmationTarget::UrgentOnChainSweep)
.unwrap()
.store(high_prio_estimate, Ordering::Release);
fees.get(&ConfirmationTarget::MinAllowedAnchorChannelRemoteFee)
Expand All @@ -195,6 +217,9 @@ impl BitcoindClient {
fees.get(&ConfirmationTarget::ChannelCloseMinimum)
.unwrap()
.store(background_estimate, Ordering::Release);
fees.get(&ConfirmationTarget::OutputSpendingFee)
.unwrap()
.store(background_estimate, Ordering::Release);

tokio::time::sleep(Duration::from_secs(60)).await;
}
Expand Down Expand Up @@ -335,24 +360,26 @@ impl WalletSource for BitcoindClient {
.into_iter()
.filter_map(|utxo| {
let outpoint = OutPoint { txid: utxo.txid, vout: utxo.vout };
match utxo.address.payload.clone() {
Payload::WitnessProgram(wp) => match wp.version() {
WitnessVersion::V0 => WPubkeyHash::from_slice(wp.program().as_bytes())
.map(|wpkh| Utxo::new_v0_p2wpkh(outpoint, utxo.amount, &wpkh))
.ok(),
let value = bitcoin::Amount::from_sat(utxo.amount);
match utxo.address.witness_program() {
Some(prog) if prog.is_p2wpkh() => {
WPubkeyHash::from_slice(prog.program().as_bytes())
.map(|wpkh| Utxo::new_v0_p2wpkh(outpoint, value, &wpkh))
.ok()
},
Some(prog) if prog.is_p2tr() => {
// TODO: Add `Utxo::new_v1_p2tr` upstream.
WitnessVersion::V1 => XOnlyPublicKey::from_slice(wp.program().as_bytes())
XOnlyPublicKey::from_slice(prog.program().as_bytes())
.map(|_| Utxo {
outpoint,
output: TxOut {
value: utxo.amount,
script_pubkey: ScriptBuf::new_witness_program(&wp),
value,
script_pubkey: utxo.address.script_pubkey(),
},
satisfaction_weight: 1 /* empty script_sig */ * WITNESS_SCALE_FACTOR as u64 +
1 /* witness items */ + 1 /* schnorr sig len */ + 64, /* schnorr sig */
})
.ok(),
_ => None,
.ok()
},
_ => None,
}
Expand All @@ -366,7 +393,7 @@ impl WalletSource for BitcoindClient {
})
}

fn sign_psbt(&self, tx: PartiallySignedTransaction) -> Result<Transaction, ()> {
fn sign_psbt(&self, tx: Psbt) -> Result<Transaction, ()> {
let mut tx_bytes = Vec::new();
let _ = tx.unsigned_tx.consensus_encode(&mut tx_bytes).map_err(|_| ());
let tx_hex = hex_utils::hex_str(&tx_bytes);
Expand Down
49 changes: 26 additions & 23 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,29 @@ use crate::disk::{self, INBOUND_PAYMENTS_FNAME, OUTBOUND_PAYMENTS_FNAME};
use crate::hex_utils;
use crate::{
ChannelManager, HTLCStatus, InboundPaymentInfoStorage, MillisatAmount, NetworkGraph,
OnionMessenger, OutboundPaymentInfoStorage, PaymentInfo, PeerManager,
OutboundPaymentInfoStorage, PaymentInfo, PeerManager,
};
use bitcoin::hashes::sha256::Hash as Sha256;
use bitcoin::hashes::Hash;
use bitcoin::network::constants::Network;
use bitcoin::network::Network;
use bitcoin::secp256k1::PublicKey;
use lightning::ln::bolt11_payment::payment_parameters_from_invoice;
use lightning::ln::bolt11_payment::payment_parameters_from_zero_amount_invoice;
use lightning::ln::channelmanager::{PaymentId, RecipientOnionFields, Retry};
use lightning::ln::invoice_utils as utils;
use lightning::ln::msgs::SocketAddress;
use lightning::ln::{ChannelId, PaymentHash, PaymentPreimage};
use lightning::ln::types::ChannelId;
use lightning::offers::offer::{self, Offer};
use lightning::routing::gossip::NodeId;
use lightning::routing::router::{PaymentParameters, RouteParameters};
use lightning::sign::{EntropySource, KeysManager};
use lightning::types::payment::{PaymentHash, PaymentPreimage};
use lightning::util::config::{ChannelHandshakeConfig, ChannelHandshakeLimits, UserConfig};
use lightning::util::persist::KVStore;
use lightning::util::ser::{Writeable, Writer};
use lightning_invoice::payment::payment_parameters_from_invoice;
use lightning_invoice::payment::payment_parameters_from_zero_amount_invoice;
use lightning_invoice::{utils, Bolt11Invoice, Currency};
use lightning::util::ser::Writeable;
use lightning_invoice::{Bolt11Invoice, Currency};
use lightning_persister::fs_store::FilesystemStore;
use std::env;
use std::io;
use std::io::Write;
use std::net::{SocketAddr, ToSocketAddrs};
use std::path::Path;
Expand All @@ -46,7 +47,7 @@ pub(crate) struct LdkUserInfo {
pub(crate) fn poll_for_user_input(
peer_manager: Arc<PeerManager>, channel_manager: Arc<ChannelManager>,
keys_manager: Arc<KeysManager>, network_graph: Arc<NetworkGraph>,
onion_messenger: Arc<OnionMessenger>, inbound_payments: Arc<Mutex<InboundPaymentInfoStorage>>,
inbound_payments: Arc<Mutex<InboundPaymentInfoStorage>>,
outbound_payments: Arc<Mutex<OutboundPaymentInfoStorage>>, ldk_data_dir: String,
network: Network, logger: Arc<disk::FilesystemLogger>, fs_store: Arc<FilesystemStore>,
) {
Expand All @@ -57,9 +58,9 @@ pub(crate) fn poll_for_user_input(
println!("Local Node ID is {}.", channel_manager.get_our_node_id());
'read_command: loop {
print!("> ");
io::stdout().flush().unwrap(); // Without flushing, the `>` doesn't print
std::io::stdout().flush().unwrap(); // Without flushing, the `>` doesn't print
let mut line = String::new();
if let Err(e) = io::stdin().read_line(&mut line) {
if let Err(e) = std::io::stdin().read_line(&mut line) {
break println!("ERROR: {}", e);
}

Expand Down Expand Up @@ -159,7 +160,7 @@ pub(crate) fn poll_for_user_input(
let payment_id = PaymentId(random_bytes);

let amt_msat = match (offer.amount(), user_provided_amt) {
(Some(offer::Amount::Bitcoin { amount_msats }), _) => *amount_msats,
(Some(offer::Amount::Bitcoin { amount_msats }), _) => amount_msats,
(_, Some(amt)) => amt,
(amt, _) => {
println!("ERROR: Cannot process non-Bitcoin-denominated offer value {:?}", amt);
Expand All @@ -173,9 +174,9 @@ pub(crate) fn poll_for_user_input(

while user_provided_amt.is_none() {
print!("Paying offer for {} msat. Continue (Y/N)? >", amt_msat);
io::stdout().flush().unwrap();
std::io::stdout().flush().unwrap();

if let Err(e) = io::stdin().read_line(&mut line) {
if let Err(e) = std::io::stdin().read_line(&mut line) {
println!("ERROR: {}", e);
break 'read_command;
}
Expand Down Expand Up @@ -266,7 +267,7 @@ pub(crate) fn poll_for_user_input(
);
},
"getoffer" => {
let offer_builder = channel_manager.create_offer_builder();
let offer_builder = channel_manager.create_offer_builder(None);
if let Err(e) = offer_builder {
println!("ERROR: Failed to initiate offer building: {:?}", e);
continue;
Expand Down Expand Up @@ -554,7 +555,7 @@ fn list_channels(channel_manager: &Arc<ChannelManager>, network_graph: &Arc<Netw
.get(&NodeId::from_pubkey(&chan_info.counterparty.node_id))
{
if let Some(announcement) = &node_info.announcement_info {
println!("\t\tpeer_alias: {}", announcement.alias);
println!("\t\tpeer_alias: {}", announcement.alias());
}
}

Expand All @@ -569,7 +570,7 @@ fn list_channels(channel_manager: &Arc<ChannelManager>, network_graph: &Arc<Netw
println!("\t\tavailable_balance_for_recv_msat: {},", chan_info.inbound_capacity_msat);
}
println!("\t\tchannel_can_send_payments: {},", chan_info.is_usable);
println!("\t\tpublic: {},", chan_info.is_public);
println!("\t\tpublic: {},", chan_info.is_announced);
println!("\t}},");
}
println!("]");
Expand Down Expand Up @@ -676,8 +677,8 @@ fn do_disconnect_peer(
}

fn open_channel(
peer_pubkey: PublicKey, channel_amt_sat: u64, announced_channel: bool, with_anchors: bool,
channel_manager: Arc<ChannelManager>,
peer_pubkey: PublicKey, channel_amt_sat: u64, announce_for_forwarding: bool,
with_anchors: bool, channel_manager: Arc<ChannelManager>,
) -> Result<(), ()> {
let config = UserConfig {
channel_handshake_limits: ChannelHandshakeLimits {
Expand All @@ -686,7 +687,7 @@ fn open_channel(
..Default::default()
},
channel_handshake_config: ChannelHandshakeConfig {
announced_channel,
announce_for_forwarding,
negotiate_anchors_zero_fee_htlc_tx: with_anchors,
..Default::default()
},
Expand Down Expand Up @@ -870,9 +871,11 @@ fn close_channel(
fn force_close_channel(
channel_id: [u8; 32], counterparty_node_id: PublicKey, channel_manager: Arc<ChannelManager>,
) {
match channel_manager
.force_close_broadcasting_latest_txn(&ChannelId(channel_id), &counterparty_node_id)
{
match channel_manager.force_close_broadcasting_latest_txn(
&ChannelId(channel_id),
&counterparty_node_id,
"Manually force-closed".to_string(),
) {
Ok(()) => println!("EVENT: initiating channel force-close"),
Err(e) => println!("ERROR: failed to force-close channel: {:?}", e),
}
Expand Down
4 changes: 2 additions & 2 deletions src/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ use bitcoin::Network;
use chrono::Utc;
use lightning::routing::scoring::{ProbabilisticScorer, ProbabilisticScoringDecayParameters};
use lightning::util::logger::{Logger, Record};
use lightning::util::ser::{Readable, ReadableArgs, Writer};
use lightning::util::ser::{Readable, ReadableArgs};
use std::collections::HashMap;
use std::fs;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::io::{BufRead, BufReader, Write};
use std::net::SocketAddr;
use std::path::Path;
use std::sync::Arc;
Expand Down
Loading

0 comments on commit 9e6de1b

Please sign in to comment.