From 3f23e3c2884c2d752cd35acc1b479c29a39f8836 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Mon, 26 Aug 2024 21:31:27 +0000 Subject: [PATCH] Add a constructor and per-target override to `TestFeeEstimator` 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 | 7 +++---- lightning/src/chain/channelmonitor.rs | 4 ++-- lightning/src/ln/channelmanager.rs | 2 +- lightning/src/ln/functional_test_utils.rs | 6 +++--- lightning/src/ln/functional_tests.rs | 6 +++--- lightning/src/ln/reload_tests.rs | 3 +-- lightning/src/util/test_utils.rs | 13 +++++++++++-- 7 files changed, 24 insertions(+), 17 deletions(-) diff --git a/lightning-background-processor/src/lib.rs b/lightning-background-processor/src/lib.rs index 654774252..69511d5ee 100644 --- a/lightning-background-processor/src/lib.rs +++ b/lightning-background-processor/src/lib.rs @@ -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 = lightning::routing::scoring::MultiThreadedLockableScore; #[cfg(not(c_bindings))] - type LockingWrapper = Mutex; + type LockingWrapper = std::sync::Mutex; type ChannelManager = channelmanager::ChannelManager< Arc, @@ -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())); diff --git a/lightning/src/chain/channelmonitor.rs b/lightning/src/chain/channelmonitor.rs index deb9cc868..86f0d3de5 100644 --- a/lightning/src/chain/channelmonitor.rs +++ b/lightning/src/chain/channelmonitor.rs @@ -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()); diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index 8a07e18a4..ce16b6746 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -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); diff --git a/lightning/src/ln/functional_test_utils.rs b/lightning/src/ln/functional_test_utils.rs index 37a8f9b67..8f1872422 100644 --- a/lightning/src/ln/functional_test_utils.rs +++ b/lightning/src/ln/functional_test_utils.rs @@ -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 { 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(); diff --git a/lightning/src/ln/functional_tests.rs b/lightning/src/ln/functional_tests.rs index 12f1466e0..2030bdb08 100644 --- a/lightning/src/ln/functional_tests.rs +++ b/lightning/src/ln/functional_tests.rs @@ -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) { diff --git a/lightning/src/ln/reload_tests.rs b/lightning/src/ln/reload_tests.rs index 21bc1a0b5..a17f8495a 100644 --- a/lightning/src/ln/reload_tests.rs +++ b/lightning/src/ln/reload_tests.rs @@ -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); diff --git a/lightning/src/util/test_utils.rs b/lightning/src/util/test_utils.rs index dad004a35..13d7b138f 100644 --- a/lightning/src/util/test_utils.rs +++ b/lightning/src/util/test_utils.rs @@ -99,10 +99,19 @@ impl Writer for TestVecWriter { pub struct TestFeeEstimator { pub sat_per_kw: Mutex, + pub target_override: Mutex>, +} +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()) } } -- 2.39.5