Skip to content

Commit 3f23e3c

Browse files
committed
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.
1 parent 1472019 commit 3f23e3c

File tree

7 files changed

+24
-17
lines changed

7 files changed

+24
-17
lines changed

lightning-background-processor/src/lib.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,7 +1107,7 @@ mod tests {
11071107
use std::collections::VecDeque;
11081108
use std::path::PathBuf;
11091109
use std::sync::mpsc::SyncSender;
1110-
use std::sync::{Arc, Mutex};
1110+
use std::sync::Arc;
11111111
use std::time::Duration;
11121112
use std::{env, fs};
11131113

@@ -1126,7 +1126,7 @@ mod tests {
11261126
#[cfg(c_bindings)]
11271127
type LockingWrapper<T> = lightning::routing::scoring::MultiThreadedLockableScore<T>;
11281128
#[cfg(not(c_bindings))]
1129-
type LockingWrapper<T> = Mutex<T>;
1129+
type LockingWrapper<T> = std::sync::Mutex<T>;
11301130

11311131
type ChannelManager = channelmanager::ChannelManager<
11321132
Arc<ChainMonitor>,
@@ -1532,8 +1532,7 @@ mod tests {
15321532
let mut nodes = Vec::new();
15331533
for i in 0..num_nodes {
15341534
let tx_broadcaster = Arc::new(test_utils::TestBroadcaster::new(network));
1535-
let fee_estimator =
1536-
Arc::new(test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) });
1535+
let fee_estimator = Arc::new(test_utils::TestFeeEstimator::new(253));
15371536
let logger = Arc::new(test_utils::TestLogger::with_id(format!("node {}", i)));
15381537
let genesis_block = genesis_block(network);
15391538
let network_graph = Arc::new(NetworkGraph::new(network, logger.clone()));

lightning/src/chain/channelmonitor.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5016,7 +5016,7 @@ mod tests {
50165016
use crate::util::test_utils::{TestLogger, TestBroadcaster, TestFeeEstimator};
50175017
use crate::util::ser::{ReadableArgs, Writeable};
50185018
use crate::util::logger::Logger;
5019-
use crate::sync::{Arc, Mutex};
5019+
use crate::sync::Arc;
50205020
use crate::io;
50215021
use crate::ln::features::ChannelTypeFeatures;
50225022

@@ -5116,7 +5116,7 @@ mod tests {
51165116
let secp_ctx = Secp256k1::new();
51175117
let logger = Arc::new(TestLogger::new());
51185118
let broadcaster = Arc::new(TestBroadcaster::new(Network::Testnet));
5119-
let fee_estimator = TestFeeEstimator { sat_per_kw: Mutex::new(253) };
5119+
let fee_estimator = TestFeeEstimator::new(253);
51205120

51215121
let dummy_key = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
51225122

lightning/src/ln/channelmanager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14198,7 +14198,7 @@ pub mod bench {
1419814198
let genesis_block = bitcoin::constants::genesis_block(network);
1419914199

1420014200
let tx_broadcaster = test_utils::TestBroadcaster::new(network);
14201-
let fee_estimator = test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) };
14201+
let fee_estimator = test_utils::TestFeeEstimator::new(253);
1420214202
let logger_a = test_utils::TestLogger::with_id("node a".to_owned());
1420314203
let scorer = RwLock::new(test_utils::TestScorer::new());
1420414204
let router = test_utils::TestRouter::new(Arc::new(NetworkGraph::new(network, &logger_a)), &logger_a, &scorer);

lightning/src/ln/functional_test_utils.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ impl<'a, 'b, 'c> Drop for Node<'a, 'b, 'c> {
659659
// Check that if we serialize and then deserialize all our channel monitors we get the
660660
// same set of outputs to watch for on chain as we have now. Note that if we write
661661
// tests that fully close channels and remove the monitors at some point this may break.
662-
let feeest = test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) };
662+
let feeest = test_utils::TestFeeEstimator::new(253);
663663
let mut deserialized_monitors = Vec::new();
664664
{
665665
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> {
692692
entropy_source: self.keys_manager,
693693
node_signer: self.keys_manager,
694694
signer_provider: self.keys_manager,
695-
fee_estimator: &test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) },
695+
fee_estimator: &test_utils::TestFeeEstimator::new(253),
696696
router: &test_utils::TestRouter::new(Arc::new(network_graph), &self.logger, &scorer),
697697
chain_monitor: self.chain_monitor,
698698
tx_broadcaster: &broadcaster,
@@ -3176,7 +3176,7 @@ pub fn create_chanmon_cfgs(node_count: usize) -> Vec<TestChanMonCfg> {
31763176
let mut chan_mon_cfgs = Vec::new();
31773177
for i in 0..node_count {
31783178
let tx_broadcaster = test_utils::TestBroadcaster::new(Network::Testnet);
3179-
let fee_estimator = test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) };
3179+
let fee_estimator = test_utils::TestFeeEstimator::new(253);
31803180
let chain_source = test_utils::TestChainSource::new(Network::Testnet);
31813181
let logger = test_utils::TestLogger::with_id(format!("node {}", i));
31823182
let persister = test_utils::TestPersister::new();

lightning/src/ln/functional_tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7247,7 +7247,7 @@ fn test_user_configurable_csv_delay() {
72477247
let logger = TestLogger::new();
72487248

72497249
// We test config.our_to_self > BREAKDOWN_TIMEOUT is enforced in OutboundV1Channel::new()
7250-
if let Err(error) = OutboundV1Channel::new(&LowerBoundedFeeEstimator::new(&test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) }),
7250+
if let Err(error) = OutboundV1Channel::new(&LowerBoundedFeeEstimator::new(&test_utils::TestFeeEstimator::new(253)),
72517251
&nodes[0].keys_manager, &nodes[0].keys_manager, nodes[1].node.get_our_node_id(), &nodes[1].node.init_features(), 1000000, 1000000, 0,
72527252
&low_our_to_self_config, 0, 42, None, &logger)
72537253
{
@@ -7261,7 +7261,7 @@ fn test_user_configurable_csv_delay() {
72617261
nodes[1].node.create_channel(nodes[0].node.get_our_node_id(), 1000000, 1000000, 42, None, None).unwrap();
72627262
let mut open_channel = get_event_msg!(nodes[1], MessageSendEvent::SendOpenChannel, nodes[0].node.get_our_node_id());
72637263
open_channel.common_fields.to_self_delay = 200;
7264-
if let Err(error) = InboundV1Channel::new(&LowerBoundedFeeEstimator::new(&test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) }),
7264+
if let Err(error) = InboundV1Channel::new(&LowerBoundedFeeEstimator::new(&test_utils::TestFeeEstimator::new(253)),
72657265
&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,
72667266
&low_our_to_self_config, 0, &nodes[0].logger, /*is_0conf=*/false)
72677267
{
@@ -7296,7 +7296,7 @@ fn test_user_configurable_csv_delay() {
72967296
nodes[1].node.create_channel(nodes[0].node.get_our_node_id(), 1000000, 1000000, 42, None, None).unwrap();
72977297
let mut open_channel = get_event_msg!(nodes[1], MessageSendEvent::SendOpenChannel, nodes[0].node.get_our_node_id());
72987298
open_channel.common_fields.to_self_delay = 200;
7299-
if let Err(error) = InboundV1Channel::new(&LowerBoundedFeeEstimator::new(&test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) }),
7299+
if let Err(error) = InboundV1Channel::new(&LowerBoundedFeeEstimator::new(&test_utils::TestFeeEstimator::new(253)),
73007300
&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,
73017301
&high_their_to_self_config, 0, &nodes[0].logger, /*is_0conf=*/false)
73027302
{

lightning/src/ln/reload_tests.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ use crate::util::config::UserConfig;
2828
use bitcoin::hash_types::BlockHash;
2929

3030
use crate::prelude::*;
31-
use crate::sync::Mutex;
3231

3332
use crate::ln::functional_test_utils::*;
3433

@@ -388,7 +387,7 @@ fn test_manager_serialize_deserialize_inconsistent_monitor() {
388387
}
389388

390389
logger = test_utils::TestLogger::new();
391-
fee_estimator = test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) };
390+
fee_estimator = test_utils::TestFeeEstimator::new(253);
392391
persister = test_utils::TestPersister::new();
393392
let keys_manager = &chanmon_cfgs[0].keys_manager;
394393
new_chain_monitor = test_utils::TestChainMonitor::new(Some(nodes[0].chain_source), nodes[0].tx_broadcaster, &logger, &fee_estimator, &persister, keys_manager);

lightning/src/util/test_utils.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,19 @@ impl Writer for TestVecWriter {
9999

100100
pub struct TestFeeEstimator {
101101
pub sat_per_kw: Mutex<u32>,
102+
pub target_override: Mutex<HashMap<ConfirmationTarget, u32>>,
103+
}
104+
impl TestFeeEstimator {
105+
pub fn new(sat_per_kw: u32) -> Self {
106+
Self {
107+
sat_per_kw: Mutex::new(sat_per_kw),
108+
target_override: Mutex::new(new_hash_map()),
109+
}
110+
}
102111
}
103112
impl chaininterface::FeeEstimator for TestFeeEstimator {
104-
fn get_est_sat_per_1000_weight(&self, _confirmation_target: ConfirmationTarget) -> u32 {
105-
*self.sat_per_kw.lock().unwrap()
113+
fn get_est_sat_per_1000_weight(&self, conf_target: ConfirmationTarget) -> u32 {
114+
*self.target_override.lock().unwrap().get(&conf_target).unwrap_or(&*self.sat_per_kw.lock().unwrap())
106115
}
107116
}
108117

0 commit comments

Comments
 (0)