Skip to content

Commit

Permalink
demo two exposed functional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
devrandom committed Apr 27, 2024
1 parent 8eb8b4d commit 3805ac1
Show file tree
Hide file tree
Showing 22 changed files with 116 additions and 90 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ lightning/net_graph-*.bin
lightning-rapid-gossip-sync/res/full_graph.lngossip
lightning-custom-message/target
lightning-transaction-sync/target
ext-functional-test-demo/target
no-std-check/target
msrv-no-dev-deps-check/target
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ members = [
"lightning-transaction-sync",
"possiblyrandom",
"ext-test-macro",
]
]

exclude = [
"ext-functional-test-demo",
Expand Down
7 changes: 7 additions & 0 deletions ext-functional-test-demo/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "ext-functional-tester"
version = "0.1.0"
edition = "2021"

[dependencies]
lightning = { path = "../lightning", features = ["_test_utils"] }
31 changes: 31 additions & 0 deletions ext-functional-test-demo/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
fn main() {
println!("Hello, world!");
}

#[cfg(test)]
mod tests {
use lightning::util::dyn_signer::{DynKeysInterfaceTrait, DynSigner};
use lightning::util::test_utils::{TestSignerFactory, SIGNER_FACTORY};
use std::panic::catch_unwind;
use std::sync::Arc;
use std::time::Duration;

struct BrokenSignerFactory();

impl TestSignerFactory for BrokenSignerFactory {
fn make_signer(
&self, _seed: &[u8; 32], _now: Duration,
) -> Box<dyn DynKeysInterfaceTrait<EcdsaSigner = DynSigner>> {
panic!()
}
}

#[test]
fn test_functional() {
lightning::ln::functional_tests::test_insane_channel_opens();
lightning::ln::functional_tests::fake_network_test();

SIGNER_FACTORY.set(Arc::new(BrokenSignerFactory()));
catch_unwind(|| lightning::ln::functional_tests::fake_network_test()).unwrap_err();
}
}
4 changes: 4 additions & 0 deletions lightning-background-processor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1918,6 +1918,8 @@ mod tests {
failure: PathFailure::OnPath { network_update: None },
path: path.clone(),
short_channel_id: Some(scored_scid),
error_code: None,
error_data: None,
});
let event = $receive.expect("PaymentPathFailed not handled within deadline");
match event {
Expand All @@ -1935,6 +1937,8 @@ mod tests {
failure: PathFailure::OnPath { network_update: None },
path: path.clone(),
short_channel_id: None,
error_code: None,
error_data: None,
});
let event = $receive.expect("PaymentPathFailed not handled within deadline");
match event {
Expand Down
6 changes: 3 additions & 3 deletions lightning/src/chain/channelmonitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1628,7 +1628,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
/// Unsafe test-only version of `broadcast_latest_holder_commitment_txn` used by our test framework
/// to bypass HolderCommitmentTransaction state update lockdown after signature and generate
/// revoked commitment transaction.
#[cfg(any(test, feature = "unsafe_revoked_tx_signing"))]
#[cfg(any(test, feature = "_test_utils", feature = "unsafe_revoked_tx_signing"))]
pub fn unsafe_get_latest_holder_commitment_txn<L: Deref>(&self, logger: &L) -> Vec<Transaction>
where L::Target: Logger {
let mut inner = self.inner.lock().unwrap();
Expand Down Expand Up @@ -1917,7 +1917,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
self.inner.lock().unwrap().counterparty_payment_script = script;
}

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

#[cfg(any(test,feature = "unsafe_revoked_tx_signing"))]
#[cfg(any(test, feature = "_test_utils", feature = "unsafe_revoked_tx_signing"))]
/// Note that this includes possibly-locktimed-in-the-future transactions!
fn unsafe_get_latest_holder_commitment_txn<L: Deref>(
&mut self, logger: &WithChannelMonitor<L>
Expand Down
2 changes: 1 addition & 1 deletion lightning/src/chain/onchaintx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1164,7 +1164,7 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
MaybeSignedTransaction(tx)
}

#[cfg(any(test, feature="unsafe_revoked_tx_signing"))]
#[cfg(any(test, feature="_test_utils", feature="unsafe_revoked_tx_signing"))]
pub(crate) fn get_fully_signed_copy_holder_tx(&mut self, funding_redeemscript: &Script) -> Transaction {
let sig = self.signer.unsafe_sign_holder_commitment(&self.holder_commitment, &self.secp_ctx).expect("sign holder commitment");
self.holder_commitment.add_holder_sig(funding_redeemscript, sig)
Expand Down
20 changes: 10 additions & 10 deletions lightning/src/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -803,9 +803,9 @@ pub enum Event {
/// If this is `Some`, then the corresponding channel should be avoided when the payment is
/// retried. May be `None` for older [`Event`] serializations.
short_channel_id: Option<u64>,
#[cfg(test)]
#[cfg(any(test, feature = "_test_utils"))]
error_code: Option<u16>,
#[cfg(test)]
#[cfg(any(test, feature = "_test_utils"))]
error_data: Option<Vec<u8>>,
},
/// Indicates that a probe payment we sent returned successful, i.e., only failed at the destination.
Expand Down Expand Up @@ -1213,15 +1213,15 @@ impl Writeable for Event {
&Event::PaymentPathFailed {
ref payment_id, ref payment_hash, ref payment_failed_permanently, ref failure,
ref path, ref short_channel_id,
#[cfg(test)]
#[cfg(any(test, feature = "_test_utils"))]
ref error_code,
#[cfg(test)]
#[cfg(any(test, feature = "_test_utils"))]
ref error_data,
} => {
3u8.write(writer)?;
#[cfg(test)]
#[cfg(any(test, feature = "_test_utils"))]
error_code.write(writer)?;
#[cfg(test)]
#[cfg(any(test, feature = "_test_utils"))]
error_data.write(writer)?;
write_tlv_fields!(writer, {
(0, payment_hash, required),
Expand Down Expand Up @@ -1489,9 +1489,9 @@ impl MaybeReadable for Event {
},
3u8 => {
let mut f = || {
#[cfg(test)]
#[cfg(any(test, feature = "_test_utils"))]
let error_code = Readable::read(reader)?;
#[cfg(test)]
#[cfg(any(test, feature = "_test_utils"))]
let error_data = Readable::read(reader)?;
let mut payment_hash = PaymentHash([0; 32]);
let mut payment_failed_permanently = false;
Expand Down Expand Up @@ -1521,9 +1521,9 @@ impl MaybeReadable for Event {
failure,
path: Path { hops: path.unwrap(), blinded_tail },
short_channel_id,
#[cfg(test)]
#[cfg(any(test, feature = "_test_utils"))]
error_code,
#[cfg(test)]
#[cfg(any(test, feature = "_test_utils"))]
error_data,
}))
};
Expand Down
33 changes: 9 additions & 24 deletions lightning/src/ln/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -847,9 +847,6 @@ pub(crate) fn commitment_tx_base_weight(channel_type_features: &ChannelTypeFeatu
if channel_type_features.supports_anchors_zero_fee_htlc_tx() { COMMITMENT_TX_BASE_ANCHOR_WEIGHT } else { COMMITMENT_TX_BASE_WEIGHT }
}

#[cfg(not(test))]
const COMMITMENT_TX_WEIGHT_PER_HTLC: u64 = 172;
#[cfg(test)]
pub const COMMITMENT_TX_WEIGHT_PER_HTLC: u64 = 172;

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

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

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

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

counterparty_dust_limit_satoshis: u64,

#[cfg(test)]
pub(super) holder_dust_limit_satoshis: u64,
#[cfg(not(test))]
holder_dust_limit_satoshis: u64,

#[cfg(test)]
pub(super) counterparty_max_htlc_value_in_flight_msat: u64,
#[cfg(not(test))]
counterparty_max_htlc_value_in_flight_msat: u64,

#[cfg(test)]
pub(super) holder_max_htlc_value_in_flight_msat: u64,
#[cfg(not(test))]
holder_max_htlc_value_in_flight_msat: u64,

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

#[cfg(test)]
pub(super) holder_selected_channel_reserve_satoshis: u64,
#[cfg(not(test))]
holder_selected_channel_reserve_satoshis: u64,

counterparty_htlc_minimum_msat: u64,
holder_htlc_minimum_msat: u64,
#[cfg(test)]
#[cfg(any(test, feature="_test_utils"))]
pub counterparty_max_accepted_htlcs: u16,
#[cfg(not(test))]
#[cfg(not(any(test, feature="_test_utils")))]
counterparty_max_accepted_htlcs: u16,
holder_max_accepted_htlcs: u16,
minimum_depth: Option<u32>,
Expand Down Expand Up @@ -1533,9 +1518,9 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {

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

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

/// Returns the holder signer for this channel.
#[cfg(test)]
#[cfg(any(test, feature = "_test_utils"))]
pub fn get_signer(&self) -> &ChannelSignerType<SP> {
return &self.holder_signer
}
Expand Down
6 changes: 3 additions & 3 deletions lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4082,7 +4082,7 @@ where
})
}

#[cfg(test)]
#[cfg(any(test, feature = "_test_utils"))]
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> {
let _lck = self.total_consistency_lock.read().unwrap();
self.send_payment_along_path(SendAlongPathArgs {
Expand Down Expand Up @@ -4254,7 +4254,7 @@ where
&self.pending_events, |args| self.send_payment_along_path(args))
}

#[cfg(test)]
#[cfg(any(test, feature = "_test_utils"))]
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> {
let best_block_height = self.best_block.read().unwrap().height;
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
Expand All @@ -4263,7 +4263,7 @@ where
best_block_height, |args| self.send_payment_along_path(args))
}

#[cfg(test)]
#[cfg(any(test, feature = "_test_utils"))]
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> {
let best_block_height = self.best_block.read().unwrap().height;
self.pending_outbound_payments.test_add_new_pending_payment(payment_hash, recipient_onion, payment_id, route, None, &self.entropy_source, best_block_height)
Expand Down
26 changes: 13 additions & 13 deletions lightning/src/ln/functional_test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::routing::router::{self, PaymentParameters, Route, RouteParameters};
use crate::sign::{EntropySource, RandomBytes};
use crate::util::config::{UserConfig, MaxDustHTLCExposure};
use crate::util::errors::APIError;
#[cfg(test)]
#[cfg(any(test, feature = "_test_utils"))]
use crate::util::logger::Logger;
use crate::util::scid_utils;
use crate::util::test_channel_signer::TestChannelSigner;
Expand Down Expand Up @@ -486,7 +486,7 @@ impl<'a, 'b, 'c> Node<'a, 'b, 'c> {
/// `false`, the channel signer will act like an off-line remote signer and will return `Err` for
/// several of the signing methods. Currently, only `get_per_commitment_point` and
/// `release_commitment_secret` are affected by this setting.
#[cfg(test)]
#[cfg(any(test, feature = "_test_utils"))]
pub fn set_channel_signer_available(&self, peer_id: &PublicKey, chan_id: &ChannelId, available: bool) {
use crate::sign::ChannelSigner;
log_debug!(self.logger, "Setting channel signer for {} as available={}", chan_id, available);
Expand Down Expand Up @@ -926,7 +926,7 @@ pub fn remove_first_msg_event_to_node(msg_node_id: &PublicKey, msg_events: &mut
}
}

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

#[cfg(test)]
#[cfg(any(test, feature = "_test_utils"))]
macro_rules! reload_node {
($node: expr, $new_config: expr, $chanman_encoded: expr, $monitors_encoded: expr, $persister: ident, $new_chain_monitor: ident, $new_channelmanager: ident) => {
let chanman_encoded = $chanman_encoded;
Expand Down Expand Up @@ -1899,7 +1899,7 @@ macro_rules! expect_pending_htlcs_forwardable_and_htlc_handling_failed {
}}
}

#[cfg(test)]
#[cfg(any(test, feature = "_test_utils"))]
macro_rules! expect_pending_htlcs_forwardable_from_events {
($node: expr, $events: expr, $ignore: expr) => {{
assert_eq!($events.len(), 1);
Expand Down Expand Up @@ -2097,7 +2097,7 @@ macro_rules! get_route {
}}
}

#[cfg(test)]
#[cfg(any(test, feature = "_test_utils"))]
#[macro_export]
macro_rules! get_route_and_payment_hash {
($send_node: expr, $recv_node: expr, $recv_value: expr) => {{
Expand Down Expand Up @@ -2232,7 +2232,7 @@ macro_rules! expect_payment_sent {
}
}

#[cfg(test)]
#[cfg(any(test, feature = "_test_utils"))]
#[macro_export]
macro_rules! expect_payment_path_successful {
($node: expr) => {
Expand Down Expand Up @@ -2316,7 +2316,7 @@ macro_rules! expect_payment_forwarded {
}
}

#[cfg(test)]
#[cfg(any(test, feature = "_test_utils"))]
#[macro_export]
macro_rules! expect_channel_shutdown_state {
($node: expr, $chan_id: expr, $state: path) => {
Expand Down Expand Up @@ -2405,7 +2405,7 @@ impl<'a> PaymentFailedConditions<'a> {
}
}

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

#[cfg(test)]
#[cfg(any(test, feature = "_test_utils"))]
macro_rules! expect_payment_failed {
($node: expr, $expected_payment_hash: expr, $payment_failed_permanently: expr $(, $expected_error_code: expr, $expected_error_data: expr)*) => {
#[allow(unused_mut)]
Expand All @@ -2434,13 +2434,13 @@ pub fn expect_payment_failed_conditions_event<'a, 'b, 'c, 'd, 'e>(
if conditions.expected_mpp_parts_remain { assert_eq!(payment_failed_events.len(), 1); } else { assert_eq!(payment_failed_events.len(), 2); }
let expected_payment_id = match &payment_failed_events[0] {
Event::PaymentPathFailed { payment_hash, payment_failed_permanently, payment_id, failure,
#[cfg(test)]
#[cfg(any(test, feature = "_test_utils"))]
error_code,
#[cfg(test)]
#[cfg(any(test, feature = "_test_utils"))]
error_data, .. } => {
assert_eq!(*payment_hash, expected_payment_hash, "unexpected payment_hash");
assert_eq!(*payment_failed_permanently, expected_payment_failed_permanently, "unexpected payment_failed_permanently value");
#[cfg(test)]
#[cfg(any(test, feature = "_test_utils"))]
{
assert!(error_code.is_some(), "expected error_code.is_some() = true");
assert!(error_data.is_some(), "expected error_data.is_some() = true");
Expand Down
Loading

0 comments on commit 3805ac1

Please sign in to comment.