]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Add a constructor and per-target override to `TestFeeEstimator`
authorMatt Corallo <git@bluematt.me>
Mon, 26 Aug 2024 21:31:27 +0000 (21:31 +0000)
committerMatt Corallo <git@bluematt.me>
Tue, 27 Aug 2024 19:57:34 +0000 (19:57 +0000)
This will allow us to test `ConfirmationTarget`s used in functional
tests by setting an override on just the target we expect to be
used.

lightning-background-processor/src/lib.rs
lightning/src/chain/channelmonitor.rs
lightning/src/ln/channelmanager.rs
lightning/src/ln/functional_test_utils.rs
lightning/src/ln/functional_tests.rs
lightning/src/ln/reload_tests.rs
lightning/src/util/test_utils.rs

index 6547742524840290f5b001230b9dadc2b190d849..69511d5ee0c4bb90c28234c18d9420597bdfe1f7 100644 (file)
@@ -1107,7 +1107,7 @@ mod tests {
        use std::collections::VecDeque;
        use std::path::PathBuf;
        use std::sync::mpsc::SyncSender;
-       use std::sync::{Arc, Mutex};
+       use std::sync::Arc;
        use std::time::Duration;
        use std::{env, fs};
 
@@ -1126,7 +1126,7 @@ mod tests {
        #[cfg(c_bindings)]
        type LockingWrapper<T> = lightning::routing::scoring::MultiThreadedLockableScore<T>;
        #[cfg(not(c_bindings))]
-       type LockingWrapper<T> = Mutex<T>;
+       type LockingWrapper<T> = std::sync::Mutex<T>;
 
        type ChannelManager = channelmanager::ChannelManager<
                Arc<ChainMonitor>,
@@ -1532,8 +1532,7 @@ mod tests {
                let mut nodes = Vec::new();
                for i in 0..num_nodes {
                        let tx_broadcaster = Arc::new(test_utils::TestBroadcaster::new(network));
-                       let fee_estimator =
-                               Arc::new(test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) });
+                       let fee_estimator = Arc::new(test_utils::TestFeeEstimator::new(253));
                        let logger = Arc::new(test_utils::TestLogger::with_id(format!("node {}", i)));
                        let genesis_block = genesis_block(network);
                        let network_graph = Arc::new(NetworkGraph::new(network, logger.clone()));
index deb9cc8688b1d0ad64454a5d88790292d7561d31..86f0d3de5ed226666db6d7185bc11e72675a7663 100644 (file)
@@ -5016,7 +5016,7 @@ mod tests {
        use crate::util::test_utils::{TestLogger, TestBroadcaster, TestFeeEstimator};
        use crate::util::ser::{ReadableArgs, Writeable};
        use crate::util::logger::Logger;
-       use crate::sync::{Arc, Mutex};
+       use crate::sync::Arc;
        use crate::io;
        use crate::ln::features::ChannelTypeFeatures;
 
@@ -5116,7 +5116,7 @@ mod tests {
                let secp_ctx = Secp256k1::new();
                let logger = Arc::new(TestLogger::new());
                let broadcaster = Arc::new(TestBroadcaster::new(Network::Testnet));
-               let fee_estimator = TestFeeEstimator { sat_per_kw: Mutex::new(253) };
+               let fee_estimator = TestFeeEstimator::new(253);
 
                let dummy_key = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
 
index 8a07e18a43330e2b13c9b5df477a6e4c6bb7a620..ce16b67464cdb6360f38ca50ab3d6d9077311737 100644 (file)
@@ -14198,7 +14198,7 @@ pub mod bench {
                let genesis_block = bitcoin::constants::genesis_block(network);
 
                let tx_broadcaster = test_utils::TestBroadcaster::new(network);
-               let fee_estimator = test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) };
+               let fee_estimator = test_utils::TestFeeEstimator::new(253);
                let logger_a = test_utils::TestLogger::with_id("node a".to_owned());
                let scorer = RwLock::new(test_utils::TestScorer::new());
                let router = test_utils::TestRouter::new(Arc::new(NetworkGraph::new(network, &logger_a)), &logger_a, &scorer);
index 37a8f9b679fd210ce8463302e2f3f4ac71ef313c..8f18724229aea4559780373db1893b44511929d5 100644 (file)
@@ -659,7 +659,7 @@ impl<'a, 'b, 'c> Drop for Node<'a, 'b, 'c> {
                        // Check that if we serialize and then deserialize all our channel monitors we get the
                        // same set of outputs to watch for on chain as we have now. Note that if we write
                        // tests that fully close channels and remove the monitors at some point this may break.
-                       let feeest = test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) };
+                       let feeest = test_utils::TestFeeEstimator::new(253);
                        let mut deserialized_monitors = Vec::new();
                        {
                                for (outpoint, _channel_id) in self.chain_monitor.chain_monitor.list_monitors() {
@@ -692,7 +692,7 @@ impl<'a, 'b, 'c> Drop for Node<'a, 'b, 'c> {
                                        entropy_source: self.keys_manager,
                                        node_signer: self.keys_manager,
                                        signer_provider: self.keys_manager,
-                                       fee_estimator: &test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) },
+                                       fee_estimator: &test_utils::TestFeeEstimator::new(253),
                                        router: &test_utils::TestRouter::new(Arc::new(network_graph), &self.logger, &scorer),
                                        chain_monitor: self.chain_monitor,
                                        tx_broadcaster: &broadcaster,
@@ -3176,7 +3176,7 @@ pub fn create_chanmon_cfgs(node_count: usize) -> Vec<TestChanMonCfg> {
        let mut chan_mon_cfgs = Vec::new();
        for i in 0..node_count {
                let tx_broadcaster = test_utils::TestBroadcaster::new(Network::Testnet);
-               let fee_estimator = test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) };
+               let fee_estimator = test_utils::TestFeeEstimator::new(253);
                let chain_source = test_utils::TestChainSource::new(Network::Testnet);
                let logger = test_utils::TestLogger::with_id(format!("node {}", i));
                let persister = test_utils::TestPersister::new();
index 12f1466e02568903781d0143d80c1c4959661d18..2030bdb08f0d612b77a0afc1c5daa75b7def4f31 100644 (file)
@@ -7247,7 +7247,7 @@ fn test_user_configurable_csv_delay() {
        let logger = TestLogger::new();
 
        // We test config.our_to_self > BREAKDOWN_TIMEOUT is enforced in OutboundV1Channel::new()
-       if let Err(error) = OutboundV1Channel::new(&LowerBoundedFeeEstimator::new(&test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) }),
+       if let Err(error) = OutboundV1Channel::new(&LowerBoundedFeeEstimator::new(&test_utils::TestFeeEstimator::new(253)),
                &nodes[0].keys_manager, &nodes[0].keys_manager, nodes[1].node.get_our_node_id(), &nodes[1].node.init_features(), 1000000, 1000000, 0,
                &low_our_to_self_config, 0, 42, None, &logger)
        {
@@ -7261,7 +7261,7 @@ fn test_user_configurable_csv_delay() {
        nodes[1].node.create_channel(nodes[0].node.get_our_node_id(), 1000000, 1000000, 42, None, None).unwrap();
        let mut open_channel = get_event_msg!(nodes[1], MessageSendEvent::SendOpenChannel, nodes[0].node.get_our_node_id());
        open_channel.common_fields.to_self_delay = 200;
-       if let Err(error) = InboundV1Channel::new(&LowerBoundedFeeEstimator::new(&test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) }),
+       if let Err(error) = InboundV1Channel::new(&LowerBoundedFeeEstimator::new(&test_utils::TestFeeEstimator::new(253)),
                &nodes[0].keys_manager, &nodes[0].keys_manager, nodes[1].node.get_our_node_id(), &nodes[0].node.channel_type_features(), &nodes[1].node.init_features(), &open_channel, 0,
                &low_our_to_self_config, 0, &nodes[0].logger, /*is_0conf=*/false)
        {
@@ -7296,7 +7296,7 @@ fn test_user_configurable_csv_delay() {
        nodes[1].node.create_channel(nodes[0].node.get_our_node_id(), 1000000, 1000000, 42, None, None).unwrap();
        let mut open_channel = get_event_msg!(nodes[1], MessageSendEvent::SendOpenChannel, nodes[0].node.get_our_node_id());
        open_channel.common_fields.to_self_delay = 200;
-       if let Err(error) = InboundV1Channel::new(&LowerBoundedFeeEstimator::new(&test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) }),
+       if let Err(error) = InboundV1Channel::new(&LowerBoundedFeeEstimator::new(&test_utils::TestFeeEstimator::new(253)),
                &nodes[0].keys_manager, &nodes[0].keys_manager, nodes[1].node.get_our_node_id(), &nodes[0].node.channel_type_features(), &nodes[1].node.init_features(), &open_channel, 0,
                &high_their_to_self_config, 0, &nodes[0].logger, /*is_0conf=*/false)
        {
index 21bc1a0b5e1710154ff23c0d25372703a1756278..a17f8495a9a87eddb4ead69a396a99abc17d658a 100644 (file)
@@ -28,7 +28,6 @@ use crate::util::config::UserConfig;
 use bitcoin::hash_types::BlockHash;
 
 use crate::prelude::*;
-use crate::sync::Mutex;
 
 use crate::ln::functional_test_utils::*;
 
@@ -388,7 +387,7 @@ fn test_manager_serialize_deserialize_inconsistent_monitor() {
        }
 
        logger = test_utils::TestLogger::new();
-       fee_estimator = test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) };
+       fee_estimator = test_utils::TestFeeEstimator::new(253);
        persister = test_utils::TestPersister::new();
        let keys_manager = &chanmon_cfgs[0].keys_manager;
        new_chain_monitor = test_utils::TestChainMonitor::new(Some(nodes[0].chain_source), nodes[0].tx_broadcaster, &logger, &fee_estimator, &persister, keys_manager);
index dad004a3540409eae036a051b6e91f6d0dc46a39..13d7b138f4c1f5e0c0f064825c3313c2bb681b27 100644 (file)
@@ -99,10 +99,19 @@ impl Writer for TestVecWriter {
 
 pub struct TestFeeEstimator {
        pub sat_per_kw: Mutex<u32>,
+       pub target_override: Mutex<HashMap<ConfirmationTarget, u32>>,
+}
+impl TestFeeEstimator {
+       pub fn new(sat_per_kw: u32) -> Self {
+               Self {
+                       sat_per_kw: Mutex::new(sat_per_kw),
+                       target_override: Mutex::new(new_hash_map()),
+               }
+       }
 }
 impl chaininterface::FeeEstimator for TestFeeEstimator {
-       fn get_est_sat_per_1000_weight(&self, _confirmation_target: ConfirmationTarget) -> u32 {
-               *self.sat_per_kw.lock().unwrap()
+       fn get_est_sat_per_1000_weight(&self, conf_target: ConfirmationTarget) -> u32 {
+               *self.target_override.lock().unwrap().get(&conf_target).unwrap_or(&*self.sat_per_kw.lock().unwrap())
        }
 }