Skip to content

Commit 8efba6f

Browse files
committed
Give ChannelManagerReadArgs HashMap-of-monitors ownership
Its somewhat awkward that ChannelManagerReadArgs requires a mutable reference to a HashMap of ChannelMonitors, forcing the callsite to define a scope for the HashMap which they almost certainly won't use after deserializing the ChannelManager. Worse, to map the current version to C bindings, we'd need to also create a HashMap binding, which is overkill for just this one use. Instead, we just give the ReadArgs struct ownership of the HashMap and add a constructor which fills the HashMap for you.
1 parent 830ceae commit 8efba6f

File tree

4 files changed

+30
-10
lines changed

4 files changed

+30
-10
lines changed

fuzz/src/chanmon_consistency.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
242242
tx_broadcaster: broadcast.clone(),
243243
logger,
244244
default_config: config,
245-
channel_monitors: &mut monitor_refs,
245+
channel_monitors: monitor_refs,
246246
};
247247

248248
(<(BlockHash, ChannelManager<EnforcingChannelKeys, Arc<TestChannelMonitor>, Arc<TestBroadcaster>, Arc<KeyProvider>, Arc<FuzzEstimator>, Arc<dyn Logger>>)>::read(&mut Cursor::new(&$ser.0), read_args).expect("Failed to read manager").1, monitor)

lightning/src/ln/channelmanager.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3772,7 +3772,27 @@ pub struct ChannelManagerReadArgs<'a, ChanSigner: 'a + ChannelKeys, M: Deref, T:
37723772
///
37733773
/// In such cases the latest local transactions will be sent to the tx_broadcaster included in
37743774
/// this struct.
3775-
pub channel_monitors: &'a mut HashMap<OutPoint, &'a mut ChannelMonitor<ChanSigner>>,
3775+
pub channel_monitors: HashMap<OutPoint, &'a mut ChannelMonitor<ChanSigner>>,
3776+
}
3777+
3778+
impl<'a, ChanSigner: 'a + ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
3779+
ChannelManagerReadArgs<'a, ChanSigner, M, T, K, F, L>
3780+
where M::Target: ManyChannelMonitor<Keys=ChanSigner>,
3781+
T::Target: BroadcasterInterface,
3782+
K::Target: KeysInterface<ChanKeySigner = ChanSigner>,
3783+
F::Target: FeeEstimator,
3784+
L::Target: Logger,
3785+
{
3786+
/// Simple utility function to create a ChannelManagerReadArgs which creates the monitor
3787+
/// HashMap for you. This is primarily useful for C bindings where it is not practical to
3788+
/// populate a HashMap directly from C.
3789+
pub fn construct(keys_manager: K, fee_estimator: F, monitor: M, tx_broadcaster: T, logger: L, default_config: UserConfig,
3790+
mut channel_monitors: Vec<&'a mut ChannelMonitor<ChanSigner>>) -> Self {
3791+
Self {
3792+
keys_manager, fee_estimator, monitor, tx_broadcaster, logger, default_config,
3793+
channel_monitors: channel_monitors.drain(..).map(|monitor| { (monitor.get_funding_txo().0, monitor) }).collect()
3794+
}
3795+
}
37763796
}
37773797

37783798
// Implement ReadableArgs for an Arc'd ChannelManager to make it a bit easier to work with the
@@ -3799,7 +3819,7 @@ impl<'a, ChanSigner: ChannelKeys + Readable, M: Deref, T: Deref, K: Deref, F: De
37993819
F::Target: FeeEstimator,
38003820
L::Target: Logger,
38013821
{
3802-
fn read<R: ::std::io::Read>(reader: &mut R, args: ChannelManagerReadArgs<'a, ChanSigner, M, T, K, F, L>) -> Result<Self, DecodeError> {
3822+
fn read<R: ::std::io::Read>(reader: &mut R, mut args: ChannelManagerReadArgs<'a, ChanSigner, M, T, K, F, L>) -> Result<Self, DecodeError> {
38033823
let _ver: u8 = Readable::read(reader)?;
38043824
let min_ver: u8 = Readable::read(reader)?;
38053825
if min_ver > SERIALIZATION_VERSION {

lightning/src/ln/functional_test_utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ impl<'a, 'b, 'c> Drop for Node<'a, 'b, 'c> {
169169
monitor: self.chan_monitor,
170170
tx_broadcaster: self.tx_broadcaster.clone(),
171171
logger: &test_utils::TestLogger::new(),
172-
channel_monitors: &mut channel_monitors,
172+
channel_monitors,
173173
}).unwrap();
174174
}
175175

lightning/src/ln/functional_tests.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4301,7 +4301,7 @@ fn test_no_txn_manager_serialize_deserialize() {
43014301
monitor: nodes[0].chan_monitor,
43024302
tx_broadcaster: nodes[0].tx_broadcaster.clone(),
43034303
logger: &logger,
4304-
channel_monitors: &mut channel_monitors,
4304+
channel_monitors,
43054305
}).unwrap()
43064306
};
43074307
nodes_0_deserialized = nodes_0_deserialized_tmp;
@@ -4409,7 +4409,7 @@ fn test_manager_serialize_deserialize_events() {
44094409
monitor: nodes[0].chan_monitor,
44104410
tx_broadcaster: nodes[0].tx_broadcaster.clone(),
44114411
logger: &logger,
4412-
channel_monitors: &mut channel_monitors,
4412+
channel_monitors,
44134413
}).unwrap()
44144414
};
44154415
nodes_0_deserialized = nodes_0_deserialized_tmp;
@@ -4499,7 +4499,7 @@ fn test_simple_manager_serialize_deserialize() {
44994499
monitor: nodes[0].chan_monitor,
45004500
tx_broadcaster: nodes[0].tx_broadcaster.clone(),
45014501
logger: &logger,
4502-
channel_monitors: &mut channel_monitors,
4502+
channel_monitors,
45034503
}).unwrap()
45044504
};
45054505
nodes_0_deserialized = nodes_0_deserialized_tmp;
@@ -4589,7 +4589,7 @@ fn test_manager_serialize_deserialize_inconsistent_monitor() {
45894589
monitor: nodes[0].chan_monitor,
45904590
tx_broadcaster: nodes[0].tx_broadcaster.clone(),
45914591
logger: &logger,
4592-
channel_monitors: &mut node_0_stale_monitors.iter_mut().map(|monitor| { (monitor.get_funding_txo().0, monitor) }).collect(),
4592+
channel_monitors: node_0_stale_monitors.iter_mut().map(|monitor| { (monitor.get_funding_txo().0, monitor) }).collect(),
45934593
}) { } else {
45944594
panic!("If the monitor(s) are stale, this indicates a bug and we should get an Err return");
45954595
};
@@ -4603,7 +4603,7 @@ fn test_manager_serialize_deserialize_inconsistent_monitor() {
46034603
monitor: nodes[0].chan_monitor,
46044604
tx_broadcaster: nodes[0].tx_broadcaster.clone(),
46054605
logger: &logger,
4606-
channel_monitors: &mut node_0_monitors.iter_mut().map(|monitor| { (monitor.get_funding_txo().0, monitor) }).collect(),
4606+
channel_monitors: node_0_monitors.iter_mut().map(|monitor| { (monitor.get_funding_txo().0, monitor) }).collect(),
46074607
}).unwrap();
46084608
nodes_0_deserialized = nodes_0_deserialized_tmp;
46094609
assert!(nodes_0_read.is_empty());
@@ -7874,7 +7874,7 @@ fn test_data_loss_protect() {
78747874
logger: &logger,
78757875
tx_broadcaster: &tx_broadcaster,
78767876
default_config: UserConfig::default(),
7877-
channel_monitors: &mut channel_monitors,
7877+
channel_monitors,
78787878
}).unwrap().1
78797879
};
78807880
nodes[0].node = &node_state_0;

0 commit comments

Comments
 (0)