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
2 changes: 1 addition & 1 deletion .github/workflows/cln-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ jobs:
socat -d -d UNIX-LISTEN:/tmp/lightning-rpc,reuseaddr,fork TCP:127.0.0.1:9937&

- name: Run CLN integration tests
run: RUSTFLAGS="--cfg cln_test" cargo test --test integration_tests_cln
run: RUSTFLAGS="--cfg cln_test --cfg cycle_tests" cargo test --test integration_tests_cln
2 changes: 1 addition & 1 deletion .github/workflows/lnd-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ jobs:

- name: Run LND integration tests
run: LND_CERT_PATH=$LND_DATA_DIR/tls.cert LND_MACAROON_PATH=$LND_DATA_DIR/data/chain/bitcoin/regtest/admin.macaroon
RUSTFLAGS="--cfg lnd_test" cargo test --test integration_tests_lnd -- --exact --show-output
RUSTFLAGS="--cfg lnd_test --cfg cycle_tests" cargo test --test integration_tests_lnd -- --exact --show-output
env:
LND_DATA_DIR: ${{ env.LND_DATA_DIR }}
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
6 changes: 3 additions & 3 deletions benches/payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use bitcoin::hex::DisplayHex;
use bitcoin::Amount;
use common::{
expect_channel_ready_event, generate_blocks_and_wait, premine_and_distribute_funds,
setup_bitcoind_and_electrsd, setup_two_nodes_with_store, TestChainSource,
setup_bitcoind_and_electrsd, setup_two_nodes_with_store, TestChainSource, TestNode,
};
use criterion::{criterion_group, criterion_main, Criterion};
use ldk_node::{Event, Node};
Expand All @@ -18,7 +18,7 @@ use tokio::task::{self};

use crate::common::open_channel_push_amt;

fn spawn_payment(node_a: Arc<Node>, node_b: Arc<Node>, amount_msat: u64) {
fn spawn_payment(node_a: Arc<TestNode>, node_b: Arc<TestNode>, amount_msat: u64) {
let mut preimage_bytes = [0u8; 32];
rand::rng().fill_bytes(&mut preimage_bytes);
let preimage = PaymentPreimage(preimage_bytes);
Expand Down Expand Up @@ -61,7 +61,7 @@ fn spawn_payment(node_a: Arc<Node>, node_b: Arc<Node>, amount_msat: u64) {
});
}

async fn send_payments(node_a: Arc<Node>, node_b: Arc<Node>) -> std::time::Duration {
async fn send_payments(node_a: Arc<TestNode>, node_b: Arc<TestNode>) -> std::time::Duration {
let start = Instant::now();

let total_payments = 1000;
Expand Down
25 changes: 12 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
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
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1761,6 +1761,12 @@ impl Node {
Error::PersistenceFailed
})
}

#[cfg(cycle_tests)]
/// Fetch a reference to the inner NetworkGraph, for Arc cycle detection
pub fn fetch_ref(&self) -> std::sync::Weak<Graph> {
Arc::downgrade(&self.network_graph)
}
}

impl Drop for 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
10 changes: 10 additions & 0 deletions src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,14 @@ impl LogWriter for Writer {
}
}

#[cfg(cycle_tests)]
/// Our log writer
pub struct Logger {
/// Specifies the logger's writer.
writer: Writer,
}

#[cfg(not(cycle_tests))]
pub(crate) struct Logger {
/// Specifies the logger's writer.
writer: Writer,
Expand All @@ -195,10 +203,12 @@ impl Logger {
Ok(Self { writer: Writer::FileWriter { file_path, max_log_level } })
}

/// Creates a new logger writing to the `log` crate
pub fn new_log_facade() -> Self {
Self { writer: Writer::LogFacadeWriter }
}

/// Creates a new logger writing to an underlying [`LogWriter`]
pub fn new_custom_writer(log_writer: Arc<dyn LogWriter>) -> Self {
Self { writer: Writer::CustomWriter(log_writer) }
}
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
48 changes: 45 additions & 3 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub(crate) mod logging;
use std::collections::{HashMap, HashSet};
use std::env;
use std::future::Future;
use std::ops::Deref;
use std::path::PathBuf;
use std::sync::{Arc, RwLock};
use std::time::Duration;
Expand Down Expand Up @@ -261,10 +262,51 @@ pub(crate) fn random_config(anchor_channels: bool) -> TestConfig {
TestConfig { node_config, ..Default::default() }
}

pub struct TestNode {
#[cfg(feature = "uniffi")]
inner: Option<Arc<Node>>,
#[cfg(not(feature = "uniffi"))]
inner: Option<Node>,
}

#[cfg(feature = "uniffi")]
type TestNode = Arc<Node>;
impl From<Arc<Node>> for TestNode {
fn from(inner: Arc<Node>) -> Self {
Self { inner: Some(inner) }
}
}

#[cfg(not(feature = "uniffi"))]
type TestNode = Node;
impl From<Node> for TestNode {
fn from(inner: Node) -> Self {
Self { inner: Some(inner) }
}
}

impl Deref for TestNode {
type Target = Node;
fn deref(&self) -> &Node {
#[cfg(feature = "uniffi")]
{
self.inner.as_ref().unwrap().deref()
}
#[cfg(not(feature = "uniffi"))]
{
&self.inner.as_ref().unwrap()
}
}
}

#[cfg(cycle_tests)]
impl Drop for TestNode {
fn drop(&mut self) {
if !std::thread::panicking() {
let graph_ref = (**self).fetch_ref();
self.inner.take();
assert_eq!(graph_ref.strong_count(), 0);
}
}
}

#[derive(Clone)]
pub(crate) enum TestChainSource<'a> {
Expand Down Expand Up @@ -430,7 +472,7 @@ pub(crate) fn setup_node_for_async_payments(
node.start().unwrap();
assert!(node.status().is_running);
assert!(node.status().latest_fee_rate_cache_update_timestamp.is_some());
node
node.into()
}

pub(crate) async fn generate_blocks_and_wait<E: ElectrumApi>(
Expand Down
Loading
Loading