Skip to content

Commit 7bc1187

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`.
1 parent 1fc2726 commit 7bc1187

File tree

3 files changed

+33
-18
lines changed

3 files changed

+33
-18
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, PathFailure, PaymentPurpose, PaymentFailureReason};
@@ -398,7 +398,7 @@ pub struct NodeCfg<'a> {
398398
pub override_init_features: Rc<RefCell<Option<InitFeatures>>>,
399399
}
400400

401-
type TestChannelManager<'node_cfg, 'chan_mon_cfg> = ChannelManager<
401+
pub(crate) type TestChannelManager<'node_cfg, 'chan_mon_cfg> = ChannelManager<
402402
&'node_cfg TestChainMonitor<'chan_mon_cfg>,
403403
&'chan_mon_cfg test_utils::TestBroadcaster,
404404
&'node_cfg test_utils::TestKeysInterface,
@@ -3286,7 +3286,7 @@ pub fn create_node_cfgs<'a>(node_count: usize, chanmon_cfgs: &'a Vec<TestChanMon
32863286
create_node_cfgs_with_persisters(node_count, chanmon_cfgs, chanmon_cfgs.iter().map(|c| &c.persister).collect())
32873287
}
32883288

3289-
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>> {
3289+
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>> {
32903290
let mut nodes = Vec::new();
32913291

32923292
for i in 0..node_count {

lightning/src/util/test_utils.rs

+29-14
Original file line numberDiff line numberDiff line change
@@ -378,17 +378,35 @@ impl SignerProvider for OnlyReadsKeysInterface {
378378
}
379379
}
380380

381+
#[cfg(feature = "std")]
382+
pub trait SyncBroadcaster: chaininterface::BroadcasterInterface + Sync {}
383+
#[cfg(feature = "std")]
384+
pub trait SyncPersist: Persist<TestChannelSigner> + Sync {}
385+
#[cfg(feature = "std")]
386+
impl<T: chaininterface::BroadcasterInterface + Sync> SyncBroadcaster for T {}
387+
#[cfg(feature = "std")]
388+
impl<T: Persist<TestChannelSigner> + Sync> SyncPersist for T {}
389+
390+
#[cfg(not(feature = "std"))]
391+
pub trait SyncBroadcaster: chaininterface::BroadcasterInterface {}
392+
#[cfg(not(feature = "std"))]
393+
pub trait SyncPersist: Persist<TestChannelSigner> {}
394+
#[cfg(not(feature = "std"))]
395+
impl<T: chaininterface::BroadcasterInterface> SyncBroadcaster for T {}
396+
#[cfg(not(feature = "std"))]
397+
impl<T: Persist<TestChannelSigner>> SyncPersist for T {}
398+
381399
pub struct TestChainMonitor<'a> {
382400
pub added_monitors: Mutex<Vec<(ChannelId, ChannelMonitor<TestChannelSigner>)>>,
383401
pub monitor_updates: Mutex<HashMap<ChannelId, Vec<ChannelMonitorUpdate>>>,
384402
pub latest_monitor_update_id: Mutex<HashMap<ChannelId, (u64, u64)>>,
385403
pub chain_monitor: ChainMonitor<
386404
TestChannelSigner,
387405
&'a TestChainSource,
388-
&'a dyn chaininterface::BroadcasterInterface,
406+
&'a dyn SyncBroadcaster,
389407
&'a TestFeeEstimator,
390408
&'a TestLogger,
391-
&'a dyn Persist<TestChannelSigner>,
409+
&'a dyn SyncPersist,
392410
>,
393411
pub keys_manager: &'a TestKeysInterface,
394412
/// If this is set to Some(), the next update_channel call (not watch_channel) must be a
@@ -401,10 +419,9 @@ pub struct TestChainMonitor<'a> {
401419
}
402420
impl<'a> TestChainMonitor<'a> {
403421
pub fn new(
404-
chain_source: Option<&'a TestChainSource>,
405-
broadcaster: &'a dyn chaininterface::BroadcasterInterface, logger: &'a TestLogger,
406-
fee_estimator: &'a TestFeeEstimator, persister: &'a dyn Persist<TestChannelSigner>,
407-
keys_manager: &'a TestKeysInterface,
422+
chain_source: Option<&'a TestChainSource>, broadcaster: &'a dyn SyncBroadcaster,
423+
logger: &'a TestLogger, fee_estimator: &'a TestFeeEstimator,
424+
persister: &'a dyn SyncPersist, keys_manager: &'a TestKeysInterface,
408425
) -> Self {
409426
Self {
410427
added_monitors: Mutex::new(Vec::new()),
@@ -1739,19 +1756,17 @@ impl Drop for TestChainSource {
17391756

17401757
pub struct TestScorer {
17411758
/// Stores a tuple of (scid, ChannelUsage)
1742-
scorer_expectations: RefCell<Option<VecDeque<(u64, ChannelUsage)>>>,
1759+
scorer_expectations: Mutex<Option<VecDeque<(u64, ChannelUsage)>>>,
17431760
}
17441761

17451762
impl TestScorer {
17461763
pub fn new() -> Self {
1747-
Self { scorer_expectations: RefCell::new(None) }
1764+
Self { scorer_expectations: Mutex::new(None) }
17481765
}
17491766

17501767
pub fn expect_usage(&self, scid: u64, expectation: ChannelUsage) {
1751-
self.scorer_expectations
1752-
.borrow_mut()
1753-
.get_or_insert_with(|| VecDeque::new())
1754-
.push_back((scid, expectation));
1768+
let mut expectations = self.scorer_expectations.lock().unwrap();
1769+
expectations.get_or_insert_with(|| VecDeque::new()).push_back((scid, expectation));
17551770
}
17561771
}
17571772

@@ -1772,7 +1787,7 @@ impl ScoreLookUp for TestScorer {
17721787
Some(scid) => scid,
17731788
None => return 0,
17741789
};
1775-
if let Some(scorer_expectations) = self.scorer_expectations.borrow_mut().as_mut() {
1790+
if let Some(scorer_expectations) = self.scorer_expectations.lock().unwrap().as_mut() {
17761791
match scorer_expectations.pop_front() {
17771792
Some((scid, expectation)) => {
17781793
assert_eq!(expectation, usage);
@@ -1810,7 +1825,7 @@ impl Drop for TestScorer {
18101825
return;
18111826
}
18121827

1813-
if let Some(scorer_expectations) = self.scorer_expectations.borrow().as_ref() {
1828+
if let Some(scorer_expectations) = self.scorer_expectations.lock().unwrap().as_ref() {
18141829
if !scorer_expectations.is_empty() {
18151830
panic!("Unsatisfied scorer expectations: {:?}", scorer_expectations)
18161831
}

0 commit comments

Comments
 (0)