Skip to content

Commit 74f9098

Browse files
committed
Make TestChannelManager Sync
In a coming commit we'll need to hold references to `TestChannelManager` in threads, requiring that it be `Sync`. Fairly minor merge conflicts addressed in: * `lightning/src/util/test_utils.rs`
1 parent ff9f627 commit 74f9098

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

lightning-persister/src/test_utils.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ pub(crate) fn do_test_data_migration<S: MigratableKVStore, T: MigratableKVStore>
113113

114114
// Integration-test the given KVStore implementation. Test relaying a few payments and check that
115115
// the persisted data is updated the appropriate number of times.
116-
pub(crate) fn do_test_store<K: KVStore>(store_0: &K, store_1: &K) {
116+
pub(crate) fn do_test_store<K: KVStore + Sync>(store_0: &K, store_1: &K) {
117117
let chanmon_cfgs = create_chanmon_cfgs(2);
118118
let mut node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
119119
let chain_mon_0 = test_utils::TestChainMonitor::new(

lightning/src/ln/functional_test_utils.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
//! A bunch of useful utilities for building networks of nodes and exchanging messages between
1111
//! nodes for functional tests.
1212
13-
use crate::chain::{BestBlock, ChannelMonitorUpdateStatus, Confirm, Listen, Watch, chainmonitor::Persist};
13+
use crate::chain::{BestBlock, ChannelMonitorUpdateStatus, Confirm, Listen, Watch};
1414
use crate::chain::channelmonitor::ChannelMonitor;
1515
use crate::chain::transaction::OutPoint;
1616
use crate::events::{ClaimedHTLC, ClosureReason, Event, HTLCDestination, MessageSendEvent, MessageSendEventsProvider, PathFailure, PaymentPurpose, PaymentFailureReason};
@@ -399,7 +399,7 @@ pub struct NodeCfg<'a> {
399399
pub override_init_features: Rc<RefCell<Option<InitFeatures>>>,
400400
}
401401

402-
type TestChannelManager<'node_cfg, 'chan_mon_cfg> = ChannelManager<
402+
pub(crate) type TestChannelManager<'node_cfg, 'chan_mon_cfg> = ChannelManager<
403403
&'node_cfg TestChainMonitor<'chan_mon_cfg>,
404404
&'chan_mon_cfg test_utils::TestBroadcaster,
405405
&'node_cfg test_utils::TestKeysInterface,
@@ -3259,7 +3259,7 @@ pub fn create_node_cfgs<'a>(node_count: usize, chanmon_cfgs: &'a Vec<TestChanMon
32593259
create_node_cfgs_with_persisters(node_count, chanmon_cfgs, chanmon_cfgs.iter().map(|c| &c.persister).collect())
32603260
}
32613261

3262-
pub fn create_node_cfgs_with_persisters<'a>(node_count: usize, chanmon_cfgs: &'a Vec<TestChanMonCfg>, persisters: Vec<&'a impl Persist<TestChannelSigner>>) -> Vec<NodeCfg<'a>> {
3262+
pub fn create_node_cfgs_with_persisters<'a>(node_count: usize, chanmon_cfgs: &'a Vec<TestChanMonCfg>, persisters: Vec<&'a impl test_utils::SyncPersist>) -> Vec<NodeCfg<'a>> {
32633263
let mut nodes = Vec::new();
32643264

32653265
for i in 0..node_count {

lightning/src/util/test_utils.rs

+26-7
Original file line numberDiff line numberDiff line change
@@ -335,11 +335,29 @@ impl SignerProvider for OnlyReadsKeysInterface {
335335
fn get_shutdown_scriptpubkey(&self) -> Result<ShutdownScript, ()> { Err(()) }
336336
}
337337

338+
#[cfg(feature = "std")]
339+
pub trait SyncBroadcaster: chaininterface::BroadcasterInterface + Sync {}
340+
#[cfg(feature = "std")]
341+
pub trait SyncPersist: chainmonitor::Persist<TestChannelSigner> + Sync {}
342+
#[cfg(feature = "std")]
343+
impl<T: chaininterface::BroadcasterInterface + Sync> SyncBroadcaster for T {}
344+
#[cfg(feature = "std")]
345+
impl<T: chainmonitor::Persist<TestChannelSigner> + Sync> SyncPersist for T {}
346+
347+
#[cfg(not(feature = "std"))]
348+
pub trait SyncBroadcaster: chaininterface::BroadcasterInterface {}
349+
#[cfg(not(feature = "std"))]
350+
pub trait SyncPersist: chainmonitor::Persist<TestChannelSigner> {}
351+
#[cfg(not(feature = "std"))]
352+
impl<T: chaininterface::BroadcasterInterface> SyncBroadcaster for T {}
353+
#[cfg(not(feature = "std"))]
354+
impl<T: chainmonitor::Persist<TestChannelSigner>> SyncPersist for T {}
355+
338356
pub struct TestChainMonitor<'a> {
339357
pub added_monitors: Mutex<Vec<(OutPoint, channelmonitor::ChannelMonitor<TestChannelSigner>)>>,
340358
pub monitor_updates: Mutex<HashMap<ChannelId, Vec<channelmonitor::ChannelMonitorUpdate>>>,
341359
pub latest_monitor_update_id: Mutex<HashMap<ChannelId, (OutPoint, u64, u64)>>,
342-
pub chain_monitor: chainmonitor::ChainMonitor<TestChannelSigner, &'a TestChainSource, &'a dyn chaininterface::BroadcasterInterface, &'a TestFeeEstimator, &'a TestLogger, &'a dyn chainmonitor::Persist<TestChannelSigner>>,
360+
pub chain_monitor: chainmonitor::ChainMonitor<TestChannelSigner, &'a TestChainSource, &'a dyn SyncBroadcaster, &'a TestFeeEstimator, &'a TestLogger, &'a dyn SyncPersist>,
343361
pub keys_manager: &'a TestKeysInterface,
344362
/// If this is set to Some(), the next update_channel call (not watch_channel) must be a
345363
/// ChannelForceClosed event for the given channel_id with should_broadcast set to the given
@@ -350,7 +368,7 @@ pub struct TestChainMonitor<'a> {
350368
pub expect_monitor_round_trip_fail: Mutex<Option<ChannelId>>,
351369
}
352370
impl<'a> TestChainMonitor<'a> {
353-
pub fn new(chain_source: Option<&'a TestChainSource>, broadcaster: &'a dyn chaininterface::BroadcasterInterface, logger: &'a TestLogger, fee_estimator: &'a TestFeeEstimator, persister: &'a dyn chainmonitor::Persist<TestChannelSigner>, keys_manager: &'a TestKeysInterface) -> Self {
371+
pub fn new(chain_source: Option<&'a TestChainSource>, broadcaster: &'a dyn SyncBroadcaster, logger: &'a TestLogger, fee_estimator: &'a TestFeeEstimator, persister: &'a dyn SyncPersist, keys_manager: &'a TestKeysInterface) -> Self {
354372
Self {
355373
added_monitors: Mutex::new(Vec::new()),
356374
monitor_updates: Mutex::new(new_hash_map()),
@@ -1448,18 +1466,19 @@ impl Drop for TestChainSource {
14481466

14491467
pub struct TestScorer {
14501468
/// Stores a tuple of (scid, ChannelUsage)
1451-
scorer_expectations: RefCell<Option<VecDeque<(u64, ChannelUsage)>>>,
1469+
scorer_expectations: Mutex<Option<VecDeque<(u64, ChannelUsage)>>>,
14521470
}
14531471

14541472
impl TestScorer {
14551473
pub fn new() -> Self {
14561474
Self {
1457-
scorer_expectations: RefCell::new(None),
1475+
scorer_expectations: Mutex::new(None),
14581476
}
14591477
}
14601478

14611479
pub fn expect_usage(&self, scid: u64, expectation: ChannelUsage) {
1462-
self.scorer_expectations.borrow_mut().get_or_insert_with(|| VecDeque::new()).push_back((scid, expectation));
1480+
let mut expectations = self.scorer_expectations.lock().unwrap();
1481+
expectations.get_or_insert_with(|| VecDeque::new()).push_back((scid, expectation));
14631482
}
14641483
}
14651484

@@ -1477,7 +1496,7 @@ impl ScoreLookUp for TestScorer {
14771496
Some(scid) => scid,
14781497
None => return 0,
14791498
};
1480-
if let Some(scorer_expectations) = self.scorer_expectations.borrow_mut().as_mut() {
1499+
if let Some(scorer_expectations) = self.scorer_expectations.lock().unwrap().as_mut() {
14811500
match scorer_expectations.pop_front() {
14821501
Some((scid, expectation)) => {
14831502
assert_eq!(expectation, usage);
@@ -1511,7 +1530,7 @@ impl Drop for TestScorer {
15111530
return;
15121531
}
15131532

1514-
if let Some(scorer_expectations) = self.scorer_expectations.borrow().as_ref() {
1533+
if let Some(scorer_expectations) = self.scorer_expectations.lock().unwrap().as_ref() {
15151534
if !scorer_expectations.is_empty() {
15161535
panic!("Unsatisfied scorer expectations: {:?}", scorer_expectations)
15171536
}

0 commit comments

Comments
 (0)