X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Futil%2Ftest_utils.rs;h=305dc40a0a296e701add66f2066d8182c0eb4d78;hb=039da78ea9d5dfbc844cc1c2933e69f54e325574;hp=5346f8ec306a6e0e31080993fc4473c86570523f;hpb=a0917e50e88d849302ec2651c4255789ad5c940c;p=rust-lightning diff --git a/lightning/src/util/test_utils.rs b/lightning/src/util/test_utils.rs index 5346f8ec..305dc40a 100644 --- a/lightning/src/util/test_utils.rs +++ b/lightning/src/util/test_utils.rs @@ -13,6 +13,7 @@ use crate::chain; use crate::chain::WatchedOutput; use crate::chain::chaininterface; use crate::chain::chaininterface::ConfirmationTarget; +#[cfg(test)] use crate::chain::chaininterface::FEERATE_FLOOR_SATS_PER_KW; use crate::chain::chainmonitor; use crate::chain::chainmonitor::{MonitorUpdateId, UpdateOrigin}; @@ -25,6 +26,7 @@ use crate::events; use crate::events::bump_transaction::{WalletSource, Utxo}; use crate::ln::ChannelId; use crate::ln::channelmanager::{ChannelDetails, self}; +#[cfg(test)] use crate::ln::chan_utils::CommitmentTransaction; use crate::ln::features::{ChannelFeatures, InitFeatures, NodeFeatures}; use crate::ln::{msgs, wire}; @@ -59,9 +61,6 @@ use bitcoin::secp256k1::ecdh::SharedSecret; use bitcoin::secp256k1::ecdsa::{RecoverableSignature, Signature}; use bitcoin::secp256k1::schnorr; -#[cfg(any(test, feature = "_test_utils"))] -use regex; - use crate::io; use crate::prelude::*; use core::cell::RefCell; @@ -143,7 +142,9 @@ impl<'a> Router for TestRouter<'a> { &self, payer: &PublicKey, params: &RouteParameters, first_hops: Option<&[&ChannelDetails]>, inflight_htlcs: InFlightHtlcs ) -> Result { - if let Some((find_route_query, find_route_res)) = self.next_routes.lock().unwrap().pop_front() { + let route_res; + let next_route_opt = self.next_routes.lock().unwrap().pop_front(); + if let Some((find_route_query, find_route_res)) = next_route_opt { assert_eq!(find_route_query, *params); if let Ok(ref route) = find_route_res { assert_eq!(route.route_params, Some(find_route_query)); @@ -201,10 +202,18 @@ impl<'a> Router for TestRouter<'a> { } } } - return find_route_res; - } + route_res = find_route_res; + } else { + route_res = self.router.find_route(payer, params, first_hops, inflight_htlcs); + }; - self.router.find_route(payer, params, first_hops, inflight_htlcs) + if let Ok(route) = &route_res { + // Previously, `Route`s failed to round-trip through serialization due to a write/read + // mismatch. Thus, here we test all test-generated routes round-trip: + let ser = route.encode(); + assert_eq!(Route::read(&mut &ser[..]).unwrap(), *route); + } + route_res } fn create_blinded_payment_paths< @@ -389,12 +398,14 @@ impl<'a> chain::Watch for TestChainMonitor<'a> { } } +#[cfg(test)] struct JusticeTxData { justice_tx: Transaction, value: u64, commitment_number: u64, } +#[cfg(test)] pub(crate) struct WatchtowerPersister { persister: TestPersister, /// Upon a new commitment_signed, we'll get a @@ -408,6 +419,7 @@ pub(crate) struct WatchtowerPersister { destination_script: ScriptBuf, } +#[cfg(test)] impl WatchtowerPersister { #[cfg(test)] pub(crate) fn new(destination_script: ScriptBuf) -> Self { @@ -438,6 +450,7 @@ impl WatchtowerPersister { } } +#[cfg(test)] impl chainmonitor::Persist for WatchtowerPersister { fn persist_new_channel(&self, funding_txo: OutPoint, data: &channelmonitor::ChannelMonitor, id: MonitorUpdateId @@ -632,6 +645,9 @@ impl KVStore for TestStore { } } +unsafe impl Sync for TestStore {} +unsafe impl Send for TestStore {} + pub struct TestBroadcaster { pub txn_broadcasted: Mutex>, pub blocks: Arc>>, @@ -1164,6 +1180,7 @@ pub struct TestKeysInterface { pub disable_revocation_policy_check: bool, enforcement_states: Mutex>>>, expectations: Mutex>>, + pub unavailable_signers: Mutex>, } impl EntropySource for TestKeysInterface { @@ -1222,7 +1239,11 @@ impl SignerProvider for TestKeysInterface { fn derive_channel_signer(&self, channel_value_satoshis: u64, channel_keys_id: [u8; 32]) -> TestChannelSigner { let keys = self.backing.derive_channel_signer(channel_value_satoshis, channel_keys_id); let state = self.make_enforcement_state_cell(keys.commitment_seed); - TestChannelSigner::new_with_revoked(keys, state, self.disable_revocation_policy_check) + let signer = TestChannelSigner::new_with_revoked(keys, state, self.disable_revocation_policy_check); + if self.unavailable_signers.lock().unwrap().contains(&channel_keys_id) { + signer.set_available(false); + } + signer } fn read_chan_signer(&self, buffer: &[u8]) -> Result { @@ -1260,6 +1281,7 @@ impl TestKeysInterface { disable_revocation_policy_check: false, enforcement_states: Mutex::new(new_hash_map()), expectations: Mutex::new(None), + unavailable_signers: Mutex::new(new_hash_set()), } } @@ -1273,9 +1295,7 @@ impl TestKeysInterface { } pub fn derive_channel_keys(&self, channel_value_satoshis: u64, id: &[u8; 32]) -> TestChannelSigner { - let keys = self.backing.derive_channel_keys(channel_value_satoshis, id); - let state = self.make_enforcement_state_cell(keys.commitment_seed); - TestChannelSigner::new_with_revoked(keys, state, self.disable_revocation_policy_check) + self.derive_channel_signer(channel_value_satoshis, *id) } fn make_enforcement_state_cell(&self, commitment_seed: [u8; 32]) -> Arc> {