Skip to content

Commit

Permalink
Merge pull request #118 from TheBlueMatt/main
Browse files Browse the repository at this point in the history
Upgrade to LDK 0.0.117
  • Loading branch information
tnull authored Oct 16, 2023
2 parents 828df53 + 88bd0ca commit 878c70a
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 115 deletions.
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.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" }
lightning = { version = "0.0.117", features = ["max_level_trace"] }
lightning-block-sync = { version = "0.0.117", features = [ "rpc-client", "tokio" ] }
lightning-invoice = { version = "0.25.0" }
lightning-net-tokio = { version = "0.0.117" }
lightning-persister = { version = "0.0.117" }
lightning-background-processor = { version = "0.0.117", features = [ "futures" ] }
lightning-rapid-gossip-sync = { version = "0.0.117" }

base64 = "0.13.0"
bitcoin = "0.29.0"
Expand Down
17 changes: 5 additions & 12 deletions src/args.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use crate::cli::LdkUserInfo;
use bitcoin::network::constants::Network;
use lightning::ln::msgs::NetAddress;
use lightning::ln::msgs::SocketAddress;
use std::collections::HashMap;
use std::env;
use std::fs;
use std::net::IpAddr;
use std::path::{Path, PathBuf};
use std::str::FromStr;

Expand Down Expand Up @@ -86,18 +85,12 @@ pub(crate) fn parse_startup_args() -> Result<LdkUserInfo, ()> {
let mut ldk_announced_listen_addr = Vec::new();
loop {
match env::args().skip(arg_idx + 1).next().as_ref() {
Some(s) => match IpAddr::from_str(s) {
Ok(IpAddr::V4(a)) => {
ldk_announced_listen_addr
.push(NetAddress::IPv4 { addr: a.octets(), port: ldk_peer_listening_port });
Some(s) => match SocketAddress::from_str(s) {
Ok(sa) => {
ldk_announced_listen_addr.push(sa);
arg_idx += 1;
}
Ok(IpAddr::V6(a)) => {
ldk_announced_listen_addr
.push(NetAddress::IPv6 { addr: a.octets(), port: ldk_peer_listening_port });
arg_idx += 1;
}
Err(_) => panic!("Failed to parse announced-listen-addr into an IP address"),
Err(_) => panic!("Failed to parse announced-listen-addr into a socket address"),
},
None => break,
}
Expand Down
2 changes: 1 addition & 1 deletion src/bitcoind_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use std::sync::Arc;
use std::time::Duration;

pub struct BitcoindClient {
bitcoind_rpc_client: Arc<RpcClient>,
pub(crate) bitcoind_rpc_client: Arc<RpcClient>,
host: String,
port: u16,
rpc_user: String,
Expand Down
54 changes: 29 additions & 25 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ use bitcoin::hashes::Hash;
use bitcoin::network::constants::Network;
use bitcoin::secp256k1::PublicKey;
use lightning::ln::channelmanager::{PaymentId, RecipientOnionFields, Retry};
use lightning::ln::msgs::NetAddress;
use lightning::ln::{PaymentHash, PaymentPreimage};
use lightning::ln::msgs::SocketAddress;
use lightning::ln::{ChannelId, 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::persist::KVStorePersister;
use lightning::util::persist::KVStore;
use lightning::util::ser::{Writeable, Writer};
use lightning_invoice::payment::pay_invoice;
use lightning_invoice::{utils, Bolt11Invoice, Currency};
use lightning_persister::FilesystemPersister;
use lightning_persister::fs_store::FilesystemStore;
use std::env;
use std::io;
use std::io::Write;
Expand All @@ -38,7 +38,7 @@ pub(crate) struct LdkUserInfo {
pub(crate) bitcoind_rpc_host: String,
pub(crate) ldk_storage_dir_path: String,
pub(crate) ldk_peer_listening_port: u16,
pub(crate) ldk_announced_listen_addr: Vec<NetAddress>,
pub(crate) ldk_announced_listen_addr: Vec<SocketAddress>,
pub(crate) ldk_announced_node_name: [u8; 32],
pub(crate) network: Network,
}
Expand All @@ -65,7 +65,7 @@ pub(crate) fn poll_for_user_input(
keys_manager: Arc<KeysManager>, network_graph: Arc<NetworkGraph>,
onion_messenger: Arc<OnionMessenger>, inbound_payments: Arc<Mutex<PaymentInfoStorage>>,
outbound_payments: Arc<Mutex<PaymentInfoStorage>>, ldk_data_dir: String, network: Network,
logger: Arc<disk::FilesystemLogger>, persister: Arc<FilesystemPersister>,
logger: Arc<disk::FilesystemLogger>, fs_store: Arc<FilesystemStore>,
) {
println!(
"LDK startup successful. Enter \"help\" to view available commands. Press Ctrl-D to quit."
Expand Down Expand Up @@ -172,7 +172,7 @@ pub(crate) fn poll_for_user_input(
&channel_manager,
&invoice,
&mut outbound_payments.lock().unwrap(),
persister.clone(),
Arc::clone(&fs_store),
);
}
"keysend" => {
Expand Down Expand Up @@ -209,7 +209,7 @@ pub(crate) fn poll_for_user_input(
amt_msat,
&*keys_manager,
&mut outbound_payments.lock().unwrap(),
persister.clone(),
Arc::clone(&fs_store),
);
}
"getinvoice" => {
Expand Down Expand Up @@ -247,7 +247,9 @@ pub(crate) fn poll_for_user_input(
expiry_secs.unwrap(),
Arc::clone(&logger),
);
persister.persist(INBOUND_PAYMENTS_FNAME, &*inbound_payments).unwrap();
fs_store
.write("", "", INBOUND_PAYMENTS_FNAME, &inbound_payments.encode())
.unwrap();
}
"connectpeer" => {
let peer_pubkey_and_ip_addr = words.next();
Expand Down Expand Up @@ -515,7 +517,7 @@ fn list_channels(channel_manager: &Arc<ChannelManager>, network_graph: &Arc<Netw
for chan_info in channel_manager.list_channels() {
println!("");
println!("\t{{");
println!("\t\tchannel_id: {},", hex_utils::hex_str(&chan_info.channel_id[..]));
println!("\t\tchannel_id: {},", chan_info.channel_id);
if let Some(funding_txo) = chan_info.funding_txo {
println!("\t\tfunding_txid: {},", funding_txo.txid);
}
Expand All @@ -539,7 +541,7 @@ fn list_channels(channel_manager: &Arc<ChannelManager>, network_graph: &Arc<Netw
}
println!("\t\tis_channel_ready: {},", chan_info.is_channel_ready);
println!("\t\tchannel_value_satoshis: {},", chan_info.channel_value_satoshis);
println!("\t\tlocal_balance_msat: {},", chan_info.balance_msat);
println!("\t\toutbound_capacity_msat: {},", chan_info.outbound_capacity_msat);
if chan_info.is_usable {
println!("\t\tavailable_balance_for_send_msat: {},", chan_info.outbound_capacity_msat);
println!("\t\tavailable_balance_for_recv_msat: {},", chan_info.inbound_capacity_msat);
Expand All @@ -557,7 +559,7 @@ fn list_payments(inbound_payments: &PaymentInfoStorage, outbound_payments: &Paym
println!("");
println!("\t{{");
println!("\t\tamount_millisatoshis: {},", payment_info.amt_msat);
println!("\t\tpayment_hash: {},", hex_utils::hex_str(&payment_hash.0));
println!("\t\tpayment_hash: {},", payment_hash);
println!("\t\thtlc_direction: inbound,");
println!(
"\t\thtlc_status: {},",
Expand All @@ -575,7 +577,7 @@ fn list_payments(inbound_payments: &PaymentInfoStorage, outbound_payments: &Paym
println!("");
println!("\t{{");
println!("\t\tamount_millisatoshis: {},", payment_info.amt_msat);
println!("\t\tpayment_hash: {},", hex_utils::hex_str(&payment_hash.0));
println!("\t\tpayment_hash: {},", payment_hash);
println!("\t\thtlc_direction: outbound,");
println!(
"\t\thtlc_status: {},",
Expand Down Expand Up @@ -682,7 +684,7 @@ fn open_channel(

fn send_payment(
channel_manager: &ChannelManager, invoice: &Bolt11Invoice,
outbound_payments: &mut PaymentInfoStorage, persister: Arc<FilesystemPersister>,
outbound_payments: &mut PaymentInfoStorage, fs_store: Arc<FilesystemStore>,
) {
let payment_hash = PaymentHash((*invoice.payment_hash()).into_inner());
let payment_secret = Some(*invoice.payment_secret());
Expand All @@ -695,7 +697,7 @@ fn send_payment(
amt_msat: MillisatAmount(invoice.amount_milli_satoshis()),
},
);
persister.persist(OUTBOUND_PAYMENTS_FNAME, &*outbound_payments).unwrap();
fs_store.write("", "", OUTBOUND_PAYMENTS_FNAME, &outbound_payments.encode()).unwrap();
match pay_invoice(invoice, Retry::Timeout(Duration::from_secs(10)), channel_manager) {
Ok(_payment_id) => {
let payee_pubkey = invoice.recover_payee_pub_key();
Expand All @@ -707,22 +709,22 @@ fn send_payment(
println!("ERROR: failed to send payment: {:?}", e);
print!("> ");
outbound_payments.payments.get_mut(&payment_hash).unwrap().status = HTLCStatus::Failed;
persister.persist(OUTBOUND_PAYMENTS_FNAME, &*outbound_payments).unwrap();
fs_store.write("", "", OUTBOUND_PAYMENTS_FNAME, &outbound_payments.encode()).unwrap();
}
};
}

fn keysend<E: EntropySource>(
channel_manager: &ChannelManager, payee_pubkey: PublicKey, amt_msat: u64, entropy_source: &E,
outbound_payments: &mut PaymentInfoStorage, persister: Arc<FilesystemPersister>,
outbound_payments: &mut PaymentInfoStorage, fs_store: Arc<FilesystemStore>,
) {
let payment_preimage = PaymentPreimage(entropy_source.get_secure_random_bytes());
let payment_hash = PaymentHash(Sha256::hash(&payment_preimage.0[..]).into_inner());

let route_params = RouteParameters {
payment_params: PaymentParameters::for_keysend(payee_pubkey, 40, false),
final_value_msat: amt_msat,
};
let route_params = RouteParameters::from_payment_params_and_value(
PaymentParameters::for_keysend(payee_pubkey, 40, false),
amt_msat,
);
outbound_payments.payments.insert(
payment_hash,
PaymentInfo {
Expand All @@ -732,7 +734,7 @@ fn keysend<E: EntropySource>(
amt_msat: MillisatAmount(Some(amt_msat)),
},
);
persister.persist(OUTBOUND_PAYMENTS_FNAME, &*outbound_payments).unwrap();
fs_store.write("", "", OUTBOUND_PAYMENTS_FNAME, &outbound_payments.encode()).unwrap();
match channel_manager.send_spontaneous_payment_with_retry(
Some(payment_preimage),
RecipientOnionFields::spontaneous_empty(),
Expand All @@ -748,7 +750,7 @@ fn keysend<E: EntropySource>(
println!("ERROR: failed to send payment: {:?}", e);
print!("> ");
outbound_payments.payments.get_mut(&payment_hash).unwrap().status = HTLCStatus::Failed;
persister.persist(OUTBOUND_PAYMENTS_FNAME, &*outbound_payments).unwrap();
fs_store.write("", "", OUTBOUND_PAYMENTS_FNAME, &outbound_payments.encode()).unwrap();
}
};
}
Expand Down Expand Up @@ -799,7 +801,7 @@ fn get_invoice(
fn close_channel(
channel_id: [u8; 32], counterparty_node_id: PublicKey, channel_manager: Arc<ChannelManager>,
) {
match channel_manager.close_channel(&channel_id, &counterparty_node_id) {
match channel_manager.close_channel(&ChannelId(channel_id), &counterparty_node_id) {
Ok(()) => println!("EVENT: initiating channel close"),
Err(e) => println!("ERROR: failed to close channel: {:?}", e),
}
Expand All @@ -808,7 +810,9 @@ 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(&channel_id, &counterparty_node_id) {
match channel_manager
.force_close_broadcasting_latest_txn(&ChannelId(channel_id), &counterparty_node_id)
{
Ok(()) => println!("EVENT: initiating channel force-close"),
Err(e) => println!("ERROR: failed to force-close channel: {:?}", e),
}
Expand Down
Loading

0 comments on commit 878c70a

Please sign in to comment.