Skip to content

Commit ebd12af

Browse files
committed
demo two exposed functional tests
1 parent 778306d commit ebd12af

File tree

21 files changed

+115
-90
lines changed

21 files changed

+115
-90
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ members = [
1313
"lightning-transaction-sync",
1414
"possiblyrandom",
1515
"ext-test-macro",
16-
]
16+
]
1717

1818
exclude = [
1919
"ext-functional-test-demo",

ext-functional-test-demo/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "ext-functional-tester"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
lightning = { path = "../lightning", features = ["_test_utils"] }

ext-functional-test-demo/src/main.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
fn main() {
2+
println!("Hello, world!");
3+
}
4+
5+
#[cfg(test)]
6+
mod tests {
7+
use lightning::util::dyn_signer::{DynKeysInterfaceTrait, DynSigner};
8+
use lightning::util::test_utils::{TestSignerFactory, SIGNER_FACTORY};
9+
use std::panic::catch_unwind;
10+
use std::sync::Arc;
11+
use std::time::Duration;
12+
13+
struct BrokenSignerFactory();
14+
15+
impl TestSignerFactory for BrokenSignerFactory {
16+
fn make_signer(
17+
&self, _seed: &[u8; 32], _now: Duration,
18+
) -> Box<dyn DynKeysInterfaceTrait<EcdsaSigner = DynSigner>> {
19+
panic!()
20+
}
21+
}
22+
23+
#[test]
24+
fn test_functional() {
25+
lightning::ln::functional_tests::test_insane_channel_opens();
26+
lightning::ln::functional_tests::fake_network_test();
27+
28+
SIGNER_FACTORY.set(Arc::new(BrokenSignerFactory()));
29+
catch_unwind(|| lightning::ln::functional_tests::fake_network_test()).unwrap_err();
30+
}
31+
}

lightning-background-processor/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1918,6 +1918,8 @@ mod tests {
19181918
failure: PathFailure::OnPath { network_update: None },
19191919
path: path.clone(),
19201920
short_channel_id: Some(scored_scid),
1921+
error_code: None,
1922+
error_data: None,
19211923
});
19221924
let event = $receive.expect("PaymentPathFailed not handled within deadline");
19231925
match event {
@@ -1935,6 +1937,8 @@ mod tests {
19351937
failure: PathFailure::OnPath { network_update: None },
19361938
path: path.clone(),
19371939
short_channel_id: None,
1940+
error_code: None,
1941+
error_data: None,
19381942
});
19391943
let event = $receive.expect("PaymentPathFailed not handled within deadline");
19401944
match event {

lightning/src/chain/channelmonitor.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1628,7 +1628,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
16281628
/// Unsafe test-only version of `broadcast_latest_holder_commitment_txn` used by our test framework
16291629
/// to bypass HolderCommitmentTransaction state update lockdown after signature and generate
16301630
/// revoked commitment transaction.
1631-
#[cfg(any(test, feature = "unsafe_revoked_tx_signing"))]
1631+
#[cfg(any(test, feature = "_test_utils", feature = "unsafe_revoked_tx_signing"))]
16321632
pub fn unsafe_get_latest_holder_commitment_txn<L: Deref>(&self, logger: &L) -> Vec<Transaction>
16331633
where L::Target: Logger {
16341634
let mut inner = self.inner.lock().unwrap();
@@ -1917,7 +1917,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
19171917
self.inner.lock().unwrap().counterparty_payment_script = script;
19181918
}
19191919

1920-
#[cfg(test)]
1920+
#[cfg(any(test, feature = "_test_utils"))]
19211921
pub fn do_signer_call<F: FnMut(&Signer) -> ()>(&self, mut f: F) {
19221922
let inner = self.inner.lock().unwrap();
19231923
f(&inner.onchain_tx_handler.signer);
@@ -3621,7 +3621,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
36213621
}
36223622
}
36233623

3624-
#[cfg(any(test,feature = "unsafe_revoked_tx_signing"))]
3624+
#[cfg(any(test, feature = "_test_utils", feature = "unsafe_revoked_tx_signing"))]
36253625
/// Note that this includes possibly-locktimed-in-the-future transactions!
36263626
fn unsafe_get_latest_holder_commitment_txn<L: Deref>(
36273627
&mut self, logger: &WithChannelMonitor<L>

lightning/src/chain/onchaintx.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1164,7 +1164,7 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
11641164
MaybeSignedTransaction(tx)
11651165
}
11661166

1167-
#[cfg(any(test, feature="unsafe_revoked_tx_signing"))]
1167+
#[cfg(any(test, feature="_test_utils", feature="unsafe_revoked_tx_signing"))]
11681168
pub(crate) fn get_fully_signed_copy_holder_tx(&mut self, funding_redeemscript: &Script) -> Transaction {
11691169
let sig = self.signer.unsafe_sign_holder_commitment(&self.holder_commitment, &self.secp_ctx).expect("sign holder commitment");
11701170
self.holder_commitment.add_holder_sig(funding_redeemscript, sig)

lightning/src/events/mod.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -803,9 +803,9 @@ pub enum Event {
803803
/// If this is `Some`, then the corresponding channel should be avoided when the payment is
804804
/// retried. May be `None` for older [`Event`] serializations.
805805
short_channel_id: Option<u64>,
806-
#[cfg(test)]
806+
#[cfg(any(test, feature = "_test_utils"))]
807807
error_code: Option<u16>,
808-
#[cfg(test)]
808+
#[cfg(any(test, feature = "_test_utils"))]
809809
error_data: Option<Vec<u8>>,
810810
},
811811
/// Indicates that a probe payment we sent returned successful, i.e., only failed at the destination.
@@ -1213,15 +1213,15 @@ impl Writeable for Event {
12131213
&Event::PaymentPathFailed {
12141214
ref payment_id, ref payment_hash, ref payment_failed_permanently, ref failure,
12151215
ref path, ref short_channel_id,
1216-
#[cfg(test)]
1216+
#[cfg(any(test, feature = "_test_utils"))]
12171217
ref error_code,
1218-
#[cfg(test)]
1218+
#[cfg(any(test, feature = "_test_utils"))]
12191219
ref error_data,
12201220
} => {
12211221
3u8.write(writer)?;
1222-
#[cfg(test)]
1222+
#[cfg(any(test, feature = "_test_utils"))]
12231223
error_code.write(writer)?;
1224-
#[cfg(test)]
1224+
#[cfg(any(test, feature = "_test_utils"))]
12251225
error_data.write(writer)?;
12261226
write_tlv_fields!(writer, {
12271227
(0, payment_hash, required),
@@ -1489,9 +1489,9 @@ impl MaybeReadable for Event {
14891489
},
14901490
3u8 => {
14911491
let mut f = || {
1492-
#[cfg(test)]
1492+
#[cfg(any(test, feature = "_test_utils"))]
14931493
let error_code = Readable::read(reader)?;
1494-
#[cfg(test)]
1494+
#[cfg(any(test, feature = "_test_utils"))]
14951495
let error_data = Readable::read(reader)?;
14961496
let mut payment_hash = PaymentHash([0; 32]);
14971497
let mut payment_failed_permanently = false;
@@ -1521,9 +1521,9 @@ impl MaybeReadable for Event {
15211521
failure,
15221522
path: Path { hops: path.unwrap(), blinded_tail },
15231523
short_channel_id,
1524-
#[cfg(test)]
1524+
#[cfg(any(test, feature = "_test_utils"))]
15251525
error_code,
1526-
#[cfg(test)]
1526+
#[cfg(any(test, feature = "_test_utils"))]
15271527
error_data,
15281528
}))
15291529
};

lightning/src/ln/channel.rs

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -847,9 +847,6 @@ pub(crate) fn commitment_tx_base_weight(channel_type_features: &ChannelTypeFeatu
847847
if channel_type_features.supports_anchors_zero_fee_htlc_tx() { COMMITMENT_TX_BASE_ANCHOR_WEIGHT } else { COMMITMENT_TX_BASE_WEIGHT }
848848
}
849849

850-
#[cfg(not(test))]
851-
const COMMITMENT_TX_WEIGHT_PER_HTLC: u64 = 172;
852-
#[cfg(test)]
853850
pub const COMMITMENT_TX_WEIGHT_PER_HTLC: u64 = 172;
854851

855852
pub const ANCHOR_OUTPUT_VALUE_SATOSHI: u64 = 330;
@@ -1125,9 +1122,9 @@ pub(crate) struct ShutdownResult {
11251122
/// the channel. Sadly, there isn't really a good number for this - if we expect to have no new
11261123
/// HTLCs for days we may need this to suffice for feerate increases across days, but that may
11271124
/// leave the channel less usable as we hold a bigger reserve.
1128-
#[cfg(any(fuzzing, test))]
1125+
#[cfg(any(fuzzing, test, feature = "_test_utils"))]
11291126
pub const FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE: u64 = 2;
1130-
#[cfg(not(any(fuzzing, test)))]
1127+
#[cfg(not(any(fuzzing, test, feature = "_test_utils")))]
11311128
const FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE: u64 = 2;
11321129

11331130
/// If we fail to see a funding transaction confirmed on-chain within this many blocks after the
@@ -1373,9 +1370,9 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
13731370

13741371
/// The minimum and maximum absolute fee, in satoshis, we are willing to place on the closing
13751372
/// transaction. These are set once we reach `closing_negotiation_ready`.
1376-
#[cfg(test)]
1373+
#[cfg(any(test, feature = "_test_utils"))]
13771374
pub(crate) closing_fee_limits: Option<(u64, u64)>,
1378-
#[cfg(not(test))]
1375+
#[cfg(not(any(test, feature = "_test_utils")))]
13791376
closing_fee_limits: Option<(u64, u64)>,
13801377

13811378
/// If we remove an HTLC (or fee update), commit, and receive our counterparty's
@@ -1402,34 +1399,22 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
14021399

14031400
counterparty_dust_limit_satoshis: u64,
14041401

1405-
#[cfg(test)]
14061402
pub(super) holder_dust_limit_satoshis: u64,
1407-
#[cfg(not(test))]
1408-
holder_dust_limit_satoshis: u64,
14091403

1410-
#[cfg(test)]
14111404
pub(super) counterparty_max_htlc_value_in_flight_msat: u64,
1412-
#[cfg(not(test))]
1413-
counterparty_max_htlc_value_in_flight_msat: u64,
14141405

1415-
#[cfg(test)]
14161406
pub(super) holder_max_htlc_value_in_flight_msat: u64,
1417-
#[cfg(not(test))]
1418-
holder_max_htlc_value_in_flight_msat: u64,
14191407

14201408
/// minimum channel reserve for self to maintain - set by them.
14211409
counterparty_selected_channel_reserve_satoshis: Option<u64>,
14221410

1423-
#[cfg(test)]
14241411
pub(super) holder_selected_channel_reserve_satoshis: u64,
1425-
#[cfg(not(test))]
1426-
holder_selected_channel_reserve_satoshis: u64,
14271412

14281413
counterparty_htlc_minimum_msat: u64,
14291414
holder_htlc_minimum_msat: u64,
1430-
#[cfg(test)]
1415+
#[cfg(any(test, feature="_test_utils"))]
14311416
pub counterparty_max_accepted_htlcs: u16,
1432-
#[cfg(not(test))]
1417+
#[cfg(not(any(test, feature="_test_utils")))]
14331418
counterparty_max_accepted_htlcs: u16,
14341419
holder_max_accepted_htlcs: u16,
14351420
minimum_depth: Option<u32>,
@@ -1533,9 +1518,9 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
15331518

15341519
/// The unique identifier used to re-derive the private key material for the channel through
15351520
/// [`SignerProvider::derive_channel_signer`].
1536-
#[cfg(not(test))]
1521+
#[cfg(not(any(test, feature = "_test_utils")))]
15371522
channel_keys_id: [u8; 32],
1538-
#[cfg(test)]
1523+
#[cfg(any(test, feature = "_test_utils"))]
15391524
pub channel_keys_id: [u8; 32],
15401525

15411526
/// If we can't release a [`ChannelMonitorUpdate`] until some external action completes, we
@@ -2217,7 +2202,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
22172202
}
22182203

22192204
/// Returns the holder signer for this channel.
2220-
#[cfg(test)]
2205+
#[cfg(any(test, feature = "_test_utils"))]
22212206
pub fn get_signer(&self) -> &ChannelSignerType<SP> {
22222207
return &self.holder_signer
22232208
}

lightning/src/ln/channelmanager.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4082,7 +4082,7 @@ where
40824082
})
40834083
}
40844084

4085-
#[cfg(test)]
4085+
#[cfg(any(test, feature = "_test_utils"))]
40864086
pub(crate) fn test_send_payment_along_path(&self, path: &Path, payment_hash: &PaymentHash, recipient_onion: RecipientOnionFields, total_value: u64, cur_height: u32, payment_id: PaymentId, keysend_preimage: &Option<PaymentPreimage>, session_priv_bytes: [u8; 32]) -> Result<(), APIError> {
40874087
let _lck = self.total_consistency_lock.read().unwrap();
40884088
self.send_payment_along_path(SendAlongPathArgs {
@@ -4254,7 +4254,7 @@ where
42544254
&self.pending_events, |args| self.send_payment_along_path(args))
42554255
}
42564256

4257-
#[cfg(test)]
4257+
#[cfg(any(test, feature = "_test_utils"))]
42584258
pub(super) fn test_send_payment_internal(&self, route: &Route, payment_hash: PaymentHash, recipient_onion: RecipientOnionFields, keysend_preimage: Option<PaymentPreimage>, payment_id: PaymentId, recv_value_msat: Option<u64>, onion_session_privs: Vec<[u8; 32]>) -> Result<(), PaymentSendFailure> {
42594259
let best_block_height = self.best_block.read().unwrap().height;
42604260
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
@@ -4263,7 +4263,7 @@ where
42634263
best_block_height, |args| self.send_payment_along_path(args))
42644264
}
42654265

4266-
#[cfg(test)]
4266+
#[cfg(any(test, feature = "_test_utils"))]
42674267
pub(crate) fn test_add_new_pending_payment(&self, payment_hash: PaymentHash, recipient_onion: RecipientOnionFields, payment_id: PaymentId, route: &Route) -> Result<Vec<[u8; 32]>, PaymentSendFailure> {
42684268
let best_block_height = self.best_block.read().unwrap().height;
42694269
self.pending_outbound_payments.test_add_new_pending_payment(payment_hash, recipient_onion, payment_id, route, None, &self.entropy_source, best_block_height)

lightning/src/ln/functional_test_utils.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use crate::routing::router::{self, PaymentParameters, Route, RouteParameters};
2727
use crate::sign::{EntropySource, RandomBytes};
2828
use crate::util::config::{UserConfig, MaxDustHTLCExposure};
2929
use crate::util::errors::APIError;
30-
#[cfg(test)]
30+
#[cfg(any(test, feature = "_test_utils"))]
3131
use crate::util::logger::Logger;
3232
use crate::util::scid_utils;
3333
use crate::util::test_channel_signer::TestChannelSigner;
@@ -486,7 +486,7 @@ impl<'a, 'b, 'c> Node<'a, 'b, 'c> {
486486
/// `false`, the channel signer will act like an off-line remote signer and will return `Err` for
487487
/// several of the signing methods. Currently, only `get_per_commitment_point` and
488488
/// `release_commitment_secret` are affected by this setting.
489-
#[cfg(test)]
489+
#[cfg(any(test, feature = "_test_utils"))]
490490
pub fn set_channel_signer_available(&self, peer_id: &PublicKey, chan_id: &ChannelId, available: bool) {
491491
use crate::sign::ChannelSigner;
492492
log_debug!(self.logger, "Setting channel signer for {} as available={}", chan_id, available);
@@ -926,7 +926,7 @@ pub fn remove_first_msg_event_to_node(msg_node_id: &PublicKey, msg_events: &mut
926926
}
927927
}
928928

929-
#[cfg(test)]
929+
#[cfg(any(test, feature = "_test_utils"))]
930930
macro_rules! get_channel_ref {
931931
($node: expr, $counterparty_node: expr, $per_peer_state_lock: ident, $peer_state_lock: ident, $channel_id: expr) => {
932932
{
@@ -1101,7 +1101,7 @@ pub fn _reload_node<'a, 'b, 'c>(node: &'a Node<'a, 'b, 'c>, default_config: User
11011101
node_deserialized
11021102
}
11031103

1104-
#[cfg(test)]
1104+
#[cfg(any(test, feature = "_test_utils"))]
11051105
macro_rules! reload_node {
11061106
($node: expr, $new_config: expr, $chanman_encoded: expr, $monitors_encoded: expr, $persister: ident, $new_chain_monitor: ident, $new_channelmanager: ident) => {
11071107
let chanman_encoded = $chanman_encoded;
@@ -1899,7 +1899,7 @@ macro_rules! expect_pending_htlcs_forwardable_and_htlc_handling_failed {
18991899
}}
19001900
}
19011901

1902-
#[cfg(test)]
1902+
#[cfg(any(test, feature = "_test_utils"))]
19031903
macro_rules! expect_pending_htlcs_forwardable_from_events {
19041904
($node: expr, $events: expr, $ignore: expr) => {{
19051905
assert_eq!($events.len(), 1);
@@ -2097,7 +2097,7 @@ macro_rules! get_route {
20972097
}}
20982098
}
20992099

2100-
#[cfg(test)]
2100+
#[cfg(any(test, feature = "_test_utils"))]
21012101
#[macro_export]
21022102
macro_rules! get_route_and_payment_hash {
21032103
($send_node: expr, $recv_node: expr, $recv_value: expr) => {{
@@ -2232,7 +2232,7 @@ macro_rules! expect_payment_sent {
22322232
}
22332233
}
22342234

2235-
#[cfg(test)]
2235+
#[cfg(any(test, feature = "_test_utils"))]
22362236
#[macro_export]
22372237
macro_rules! expect_payment_path_successful {
22382238
($node: expr) => {
@@ -2316,7 +2316,7 @@ macro_rules! expect_payment_forwarded {
23162316
}
23172317
}
23182318

2319-
#[cfg(test)]
2319+
#[cfg(any(test, feature = "_test_utils"))]
23202320
#[macro_export]
23212321
macro_rules! expect_channel_shutdown_state {
23222322
($node: expr, $chan_id: expr, $state: path) => {
@@ -2405,7 +2405,7 @@ impl<'a> PaymentFailedConditions<'a> {
24052405
}
24062406
}
24072407

2408-
#[cfg(test)]
2408+
#[cfg(any(test, feature = "_test_utils"))]
24092409
macro_rules! expect_payment_failed_with_update {
24102410
($node: expr, $expected_payment_hash: expr, $payment_failed_permanently: expr, $scid: expr, $chan_closed: expr) => {
24112411
$crate::ln::functional_test_utils::expect_payment_failed_conditions(
@@ -2415,7 +2415,7 @@ macro_rules! expect_payment_failed_with_update {
24152415
}
24162416
}
24172417

2418-
#[cfg(test)]
2418+
#[cfg(any(test, feature = "_test_utils"))]
24192419
macro_rules! expect_payment_failed {
24202420
($node: expr, $expected_payment_hash: expr, $payment_failed_permanently: expr $(, $expected_error_code: expr, $expected_error_data: expr)*) => {
24212421
#[allow(unused_mut)]
@@ -2434,13 +2434,13 @@ pub fn expect_payment_failed_conditions_event<'a, 'b, 'c, 'd, 'e>(
24342434
if conditions.expected_mpp_parts_remain { assert_eq!(payment_failed_events.len(), 1); } else { assert_eq!(payment_failed_events.len(), 2); }
24352435
let expected_payment_id = match &payment_failed_events[0] {
24362436
Event::PaymentPathFailed { payment_hash, payment_failed_permanently, payment_id, failure,
2437-
#[cfg(test)]
2437+
#[cfg(any(test, feature = "_test_utils"))]
24382438
error_code,
2439-
#[cfg(test)]
2439+
#[cfg(any(test, feature = "_test_utils"))]
24402440
error_data, .. } => {
24412441
assert_eq!(*payment_hash, expected_payment_hash, "unexpected payment_hash");
24422442
assert_eq!(*payment_failed_permanently, expected_payment_failed_permanently, "unexpected payment_failed_permanently value");
2443-
#[cfg(test)]
2443+
#[cfg(any(test, feature = "_test_utils"))]
24442444
{
24452445
assert!(error_code.is_some(), "expected error_code.is_some() = true");
24462446
assert!(error_data.is_some(), "expected error_data.is_some() = true");

lightning/src/ln/functional_tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -987,8 +987,8 @@ fn test_update_fee() {
987987
check_closed_event!(nodes[1], 1, ClosureReason::LocallyInitiatedCooperativeClosure, [nodes[0].node.get_our_node_id()], 100000);
988988
}
989989

990-
#[test]
991-
fn fake_network_test() {
990+
#[xtest(feature = "_test_utils")]
991+
pub fn fake_network_test() {
992992
// Simple test which builds a network of ChannelManagers, connects them to each other, and
993993
// tests that payments get routed and transactions broadcast in semi-reasonable ways.
994994
let chanmon_cfgs = create_chanmon_cfgs(4);

0 commit comments

Comments
 (0)