Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ jobs:
- name: Test on Rust ${{ matrix.toolchain }}
if: "matrix.platform != 'windows-latest'"
run: |
RUSTFLAGS="--cfg no_download" cargo test
RUSTFLAGS="--cfg no_download --cfg cycle_tests" cargo test
- name: Test with UniFFI support on Rust ${{ matrix.toolchain }}
if: "matrix.platform != 'windows-latest' && matrix.build-uniffi"
run: |
RUSTFLAGS="--cfg no_download" cargo test --features uniffi
RUSTFLAGS="--cfg no_download --cfg cycle_tests" cargo test --features uniffi
doc:
name: Documentation
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/vss-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ jobs:
cd ldk-node
export TEST_VSS_BASE_URL="http://localhost:8080/vss"
RUSTFLAGS="--cfg vss_test" cargo test io::vss_store
RUSTFLAGS="--cfg vss_test" cargo test --test integration_tests_vss
RUSTFLAGS="--cfg vss_test --cfg cycle_tests" cargo test --test integration_tests_vss
27 changes: 14 additions & 13 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@ default = []
#lightning-liquidity = { version = "0.2.0", features = ["std"] }
#lightning-macros = { version = "0.2.0" }

lightning = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "1c730c8a16e28cc8e0c4817717ee63c97abcf4b0", features = ["std"] }
lightning-types = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "1c730c8a16e28cc8e0c4817717ee63c97abcf4b0" }
lightning-invoice = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "1c730c8a16e28cc8e0c4817717ee63c97abcf4b0", features = ["std"] }
lightning-net-tokio = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "1c730c8a16e28cc8e0c4817717ee63c97abcf4b0" }
lightning-persister = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "1c730c8a16e28cc8e0c4817717ee63c97abcf4b0", features = ["tokio"] }
lightning-background-processor = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "1c730c8a16e28cc8e0c4817717ee63c97abcf4b0" }
lightning-rapid-gossip-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "1c730c8a16e28cc8e0c4817717ee63c97abcf4b0" }
lightning-block-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "1c730c8a16e28cc8e0c4817717ee63c97abcf4b0", features = ["rest-client", "rpc-client", "tokio"] }
lightning-transaction-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "1c730c8a16e28cc8e0c4817717ee63c97abcf4b0", features = ["esplora-async-https", "time", "electrum-rustls-ring"] }
lightning-liquidity = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "1c730c8a16e28cc8e0c4817717ee63c97abcf4b0", features = ["std"] }
lightning-macros = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "1c730c8a16e28cc8e0c4817717ee63c97abcf4b0" }
lightning = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "5236dba053a3f4f01cf0c32ce42b609a93738891", features = ["std"] }
lightning-types = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "5236dba053a3f4f01cf0c32ce42b609a93738891" }
lightning-invoice = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "5236dba053a3f4f01cf0c32ce42b609a93738891", features = ["std"] }
lightning-net-tokio = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "5236dba053a3f4f01cf0c32ce42b609a93738891" }
lightning-persister = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "5236dba053a3f4f01cf0c32ce42b609a93738891", features = ["tokio"] }
lightning-background-processor = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "5236dba053a3f4f01cf0c32ce42b609a93738891" }
lightning-rapid-gossip-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "5236dba053a3f4f01cf0c32ce42b609a93738891" }
lightning-block-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "5236dba053a3f4f01cf0c32ce42b609a93738891", features = ["rest-client", "rpc-client", "tokio"] }
lightning-transaction-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "5236dba053a3f4f01cf0c32ce42b609a93738891", features = ["esplora-async-https", "time", "electrum-rustls-ring"] }
lightning-liquidity = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "5236dba053a3f4f01cf0c32ce42b609a93738891", features = ["std"] }
lightning-macros = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "5236dba053a3f4f01cf0c32ce42b609a93738891" }

bdk_chain = { version = "0.23.0", default-features = false, features = ["std"] }
bdk_esplora = { version = "0.22.0", default-features = false, features = ["async-https-rustls", "tokio"]}
Expand Down Expand Up @@ -78,13 +78,13 @@ log = { version = "0.4.22", default-features = false, features = ["std"]}
vss-client = { package = "vss-client-ng", version = "0.4" }
prost = { version = "0.11.6", default-features = false}
#bitcoin-payment-instructions = { version = "0.6" }
bitcoin-payment-instructions = { git = "https://github.com/tnull/bitcoin-payment-instructions", rev = "a9ad849a0eb7b155a688d713de6d9010cb48f073" }
bitcoin-payment-instructions = { git = "https://github.com/tnull/bitcoin-payment-instructions", rev = "fdca6c62f2fe2c53427d3e51e322a49aa7323ee2" }

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["winbase"] }

[dev-dependencies]
lightning = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "1c730c8a16e28cc8e0c4817717ee63c97abcf4b0", features = ["std", "_test_utils"] }
lightning = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "5236dba053a3f4f01cf0c32ce42b609a93738891", features = ["std", "_test_utils"] }
proptest = "1.0.0"
regex = "1.5.6"
criterion = { version = "0.7.0", features = ["async_tokio"] }
Expand Down Expand Up @@ -124,6 +124,7 @@ check-cfg = [
"cfg(tokio_unstable)",
"cfg(cln_test)",
"cfg(lnd_test)",
"cfg(cycle_tests)",
]

[[bench]]
Expand Down
39 changes: 26 additions & 13 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use bitcoin::key::Secp256k1;
use bitcoin::secp256k1::PublicKey;
use bitcoin::{BlockHash, Network};
use bitcoin_payment_instructions::onion_message_resolver::LDKOnionMessageDNSSECHrnResolver;
use lightning::chain::{chainmonitor, BestBlock, Watch};
use lightning::chain::{chainmonitor, BestBlock};
use lightning::ln::channelmanager::{self, ChainParameters, ChannelManagerReadArgs};
use lightning::ln::msgs::{RoutingMessageHandler, SocketAddress};
use lightning::ln::peer_handler::{IgnoringMessageHandler, MessageHandler};
Expand Down Expand Up @@ -1481,8 +1481,12 @@ fn build_with_store_internal(

let gossip_source = match gossip_source_config {
GossipSourceConfig::P2PNetwork => {
let p2p_source =
Arc::new(GossipSource::new_p2p(Arc::clone(&network_graph), Arc::clone(&logger)));
let p2p_source = Arc::new(GossipSource::new_p2p(
Arc::clone(&network_graph),
Arc::clone(&chain_source),
Arc::clone(&runtime),
Arc::clone(&logger),
));

// Reset the RGS sync timestamp in case we somehow switch gossip sources
{
Expand Down Expand Up @@ -1597,19 +1601,14 @@ fn build_with_store_internal(
Arc::clone(&keys_manager),
));

let peer_manager_clone = Arc::clone(&peer_manager);

let peer_manager_clone = Arc::downgrade(&peer_manager);
hrn_resolver.register_post_queue_action(Box::new(move || {
peer_manager_clone.process_events();
if let Some(upgraded_pointer) = peer_manager_clone.upgrade() {
upgraded_pointer.process_events();
}
}));

liquidity_source.as_ref().map(|l| l.set_peer_manager(Arc::clone(&peer_manager)));

gossip_source.set_gossip_verifier(
Arc::clone(&chain_source),
Arc::clone(&peer_manager),
Arc::clone(&runtime),
);
liquidity_source.as_ref().map(|l| l.set_peer_manager(Arc::downgrade(&peer_manager)));

let connection_manager =
Arc::new(ConnectionManager::new(Arc::clone(&peer_manager), Arc::clone(&logger)));
Expand Down Expand Up @@ -1685,6 +1684,18 @@ fn build_with_store_internal(

let pathfinding_scores_sync_url = pathfinding_scores_sync_config.map(|c| c.url.clone());

#[cfg(cycle_tests)]
let mut _leak_checker = crate::LeakChecker(Vec::new());
#[cfg(cycle_tests)]
{
use std::any::Any;
use std::sync::Weak;

_leak_checker.0.push(Arc::downgrade(&channel_manager) as Weak<dyn Any + Send + Sync>);
_leak_checker.0.push(Arc::downgrade(&network_graph) as Weak<dyn Any + Send + Sync>);
_leak_checker.0.push(Arc::downgrade(&wallet) as Weak<dyn Any + Send + Sync>);
}

Ok(Node {
runtime,
stop_sender,
Expand Down Expand Up @@ -1717,6 +1728,8 @@ fn build_with_store_internal(
om_mailbox,
async_payments_role,
hrn_resolver,
#[cfg(cycle_tests)]
_leak_checker,
})
}

Expand Down
38 changes: 10 additions & 28 deletions src/gossip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::chain::ChainSource;
use crate::config::RGS_SYNC_TIMEOUT_SECS;
use crate::logger::{log_trace, LdkLogger, Logger};
use crate::runtime::Runtime;
use crate::types::{GossipSync, Graph, P2PGossipSync, PeerManager, RapidGossipSync, UtxoLookup};
use crate::types::{GossipSync, Graph, P2PGossipSync, RapidGossipSync};
use crate::Error;

pub(crate) enum GossipSource {
Expand All @@ -33,12 +33,15 @@ pub(crate) enum GossipSource {
}

impl GossipSource {
pub fn new_p2p(network_graph: Arc<Graph>, logger: Arc<Logger>) -> Self {
let gossip_sync = Arc::new(P2PGossipSync::new(
network_graph,
None::<Arc<UtxoLookup>>,
Arc::clone(&logger),
));
pub fn new_p2p(
network_graph: Arc<Graph>, chain_source: Arc<ChainSource>, runtime: Arc<Runtime>,
logger: Arc<Logger>,
) -> Self {
let verifier = chain_source.as_utxo_source().map(|utxo_source| {
Arc::new(GossipVerifier::new(Arc::new(utxo_source), RuntimeSpawner::new(runtime)))
});

let gossip_sync = Arc::new(P2PGossipSync::new(network_graph, verifier, logger));
Self::P2PNetwork { gossip_sync }
}

Expand All @@ -62,27 +65,6 @@ impl GossipSource {
}
}

pub(crate) fn set_gossip_verifier(
&self, chain_source: Arc<ChainSource>, peer_manager: Arc<PeerManager>,
runtime: Arc<Runtime>,
) {
match self {
Self::P2PNetwork { gossip_sync } => {
if let Some(utxo_source) = chain_source.as_utxo_source() {
let spawner = RuntimeSpawner::new(Arc::clone(&runtime));
let gossip_verifier = Arc::new(GossipVerifier::new(
Arc::new(utxo_source),
spawner,
Arc::clone(gossip_sync),
peer_manager,
));
gossip_sync.add_utxo_lookup(Some(gossip_verifier));
}
},
_ => (),
}
}

pub async fn update_rgs_snapshot(&self) -> Result<u32, Error> {
match self {
Self::P2PNetwork { gossip_sync: _, .. } => Ok(0),
Expand Down
21 changes: 21 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ use std::default::Default;
use std::net::ToSocketAddrs;
use std::sync::{Arc, Mutex, RwLock};
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
#[cfg(cycle_tests)]
use std::{any::Any, sync::Weak};

pub use balance::{BalanceDetails, LightningBalance, PendingSweepBalance};
use bitcoin::secp256k1::PublicKey;
Expand Down Expand Up @@ -173,6 +175,23 @@ use crate::scoring::setup_background_pathfinding_scores_sync;
#[cfg(feature = "uniffi")]
uniffi::include_scaffolding!("ldk_node");

#[cfg(cycle_tests)]
/// A list of [`Weak`]s which can be used to check that a [`Node`]'s inner fields are being
/// properly released after the [`Node`] is dropped.
pub struct LeakChecker(Vec<Weak<dyn Any + Send + Sync>>);

#[cfg(cycle_tests)]
impl LeakChecker {
/// Asserts that all the stored [`Weak`]s point to contents which have been freed.
///
/// This will (obviously) panic if the [`Node`] has not yet been dropped.
pub fn assert_no_leaks(&self) {
for weak in self.0.iter() {
assert_eq!(weak.strong_count(), 0);
}
}
}

/// The main interface object of LDK Node, wrapping the necessary LDK and BDK functionalities.
///
/// Needs to be initialized and instantiated through [`Builder::build`].
Expand Down Expand Up @@ -208,6 +227,8 @@ pub struct Node {
om_mailbox: Option<Arc<OnionMessageMailbox>>,
async_payments_role: Option<AsyncPaymentsRole>,
hrn_resolver: Arc<HRNResolver>,
#[cfg(cycle_tests)]
_leak_checker: LeakChecker,
}

impl Node {
Expand Down
10 changes: 5 additions & 5 deletions src/liquidity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

use std::collections::HashMap;
use std::ops::Deref;
use std::sync::{Arc, Mutex, RwLock};
use std::sync::{Arc, Mutex, RwLock, Weak};
use std::time::Duration;

use bitcoin::hashes::{sha256, Hash};
Expand Down Expand Up @@ -291,7 +291,7 @@ where
lsps2_service: Option<LSPS2Service>,
wallet: Arc<Wallet>,
channel_manager: Arc<ChannelManager>,
peer_manager: RwLock<Option<Arc<PeerManager>>>,
peer_manager: RwLock<Option<Weak<PeerManager>>>,
keys_manager: Arc<KeysManager>,
liquidity_manager: Arc<LiquidityManager>,
config: Arc<Config>,
Expand All @@ -302,7 +302,7 @@ impl<L: Deref> LiquiditySource<L>
where
L::Target: LdkLogger,
{
pub(crate) fn set_peer_manager(&self, peer_manager: Arc<PeerManager>) {
pub(crate) fn set_peer_manager(&self, peer_manager: Weak<PeerManager>) {
*self.peer_manager.write().unwrap() = Some(peer_manager);
}

Expand Down Expand Up @@ -715,8 +715,8 @@ where
return;
};

let init_features = if let Some(peer_manager) =
self.peer_manager.read().unwrap().as_ref()
let init_features = if let Some(Some(peer_manager)) =
self.peer_manager.read().unwrap().as_ref().map(|weak| weak.upgrade())
{
// Fail if we're not connected to the prospective channel partner.
if let Some(peer) = peer_manager.peer_by_node_id(&their_network_key) {
Expand Down
2 changes: 1 addition & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ pub(crate) type Scorer = CombinedScorer<Arc<Graph>, Arc<Logger>>;

pub(crate) type Graph = gossip::NetworkGraph<Arc<Logger>>;

pub(crate) type UtxoLookup = GossipVerifier<RuntimeSpawner, Arc<UtxoSourceClient>, Arc<Logger>>;
pub(crate) type UtxoLookup = GossipVerifier<RuntimeSpawner, Arc<UtxoSourceClient>>;

pub(crate) type P2PGossipSync =
lightning::routing::gossip::P2PGossipSync<Arc<Graph>, Arc<UtxoLookup>, Arc<Logger>>;
Expand Down
Loading