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 25, 2024
1 parent 15e7493 commit 87aadef
Show file tree
Hide file tree
Showing 21 changed files with 131 additions and 96 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ members = [
"lightning-transaction-sync",
"possiblyrandom",
"ext-test-macro",
"ext-functional-test-demo",
]

exclude = [
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
24 changes: 14 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,9 +1213,9 @@ 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)?;
Expand Down Expand Up @@ -1489,10 +1489,12 @@ impl MaybeReadable for Event {
},
3u8 => {
let mut f = || {
#[cfg(test)]
let error_code = Readable::read(reader)?;
#[cfg(test)]
let error_data = Readable::read(reader)?;
#[cfg(any(test, feature = "_test_utils"))]

let error_code = Readable::read(reader)?;
#[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;
let mut network_update = None;
Expand Down Expand Up @@ -1521,9 +1523,11 @@ 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
28 changes: 2 additions & 26 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,10 +1122,7 @@ 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))]
pub const FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE: u64 = 2;
#[cfg(not(any(fuzzing, test)))]
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
/// channel creation on an inbound channel, we simply force-close and move on.
Expand Down Expand Up @@ -1373,10 +1367,7 @@ 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)]
pub(crate) closing_fee_limits: Option<(u64, u64)>,
#[cfg(not(test))]
closing_fee_limits: Option<(u64, u64)>,

/// If we remove an HTLC (or fee update), commit, and receive our counterparty's
/// `revoke_and_ack`, we remove all knowledge of said HTLC (or fee update). However, the latest
Expand All @@ -1402,35 +1393,20 @@ 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)]
pub counterparty_max_accepted_htlcs: u16,
#[cfg(not(test))]
counterparty_max_accepted_htlcs: u16,
holder_max_accepted_htlcs: u16,
minimum_depth: Option<u32>,

Expand Down Expand Up @@ -1534,7 +1510,7 @@ 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))]
channel_keys_id: [u8; 32],
pub(crate) channel_keys_id: [u8; 32],
#[cfg(test)]
pub channel_keys_id: [u8; 32],

Expand Down Expand Up @@ -2217,7 +2193,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
9 changes: 6 additions & 3 deletions lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4082,7 +4082,8 @@ 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 +4255,8 @@ 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 +4265,8 @@ 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
Loading

0 comments on commit 87aadef

Please sign in to comment.