Skip to content

Commit 2ff4ae7

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 6df9129 commit 2ff4ae7

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
@@ -3775,7 +3775,27 @@ pub struct ChannelManagerReadArgs<'a, ChanSigner: 'a + ChannelKeys, M: Deref, T:
37753775
///
37763776
/// In such cases the latest local transactions will be sent to the tx_broadcaster included in
37773777
/// this struct.
3778-
pub channel_monitors: &'a mut HashMap<OutPoint, &'a mut ChannelMonitor<ChanSigner>>,
3778+
pub channel_monitors: HashMap<OutPoint, &'a mut ChannelMonitor<ChanSigner>>,
3779+
}
3780+
3781+
impl<'a, ChanSigner: 'a + ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
3782+
ChannelManagerReadArgs<'a, ChanSigner, M, T, K, F, L>
3783+
where M::Target: ManyChannelMonitor<Keys=ChanSigner>,
3784+
T::Target: BroadcasterInterface,
3785+
K::Target: KeysInterface<ChanKeySigner = ChanSigner>,
3786+
F::Target: FeeEstimator,
3787+
L::Target: Logger,
3788+
{
3789+
/// Simple utility function to create a ChannelManagerReadArgs which creates the monitor
3790+
/// HashMap for you. This is primarily useful for C bindings where it is not practical to
3791+
/// populate a HashMap directly from C.
3792+
pub fn new(keys_manager: K, fee_estimator: F, monitor: M, tx_broadcaster: T, logger: L, default_config: UserConfig,
3793+
mut channel_monitors: Vec<&'a mut ChannelMonitor<ChanSigner>>) -> Self {
3794+
Self {
3795+
keys_manager, fee_estimator, monitor, tx_broadcaster, logger, default_config,
3796+
channel_monitors: channel_monitors.drain(..).map(|monitor| { (monitor.get_funding_txo().0, monitor) }).collect()
3797+
}
3798+
}
37793799
}
37803800

37813801
// Implement ReadableArgs for an Arc'd ChannelManager to make it a bit easier to work with the
@@ -3802,7 +3822,7 @@ impl<'a, ChanSigner: ChannelKeys + Readable, M: Deref, T: Deref, K: Deref, F: De
38023822
F::Target: FeeEstimator,
38033823
L::Target: Logger,
38043824
{
3805-
fn read<R: ::std::io::Read>(reader: &mut R, args: ChannelManagerReadArgs<'a, ChanSigner, M, T, K, F, L>) -> Result<Self, DecodeError> {
3825+
fn read<R: ::std::io::Read>(reader: &mut R, mut args: ChannelManagerReadArgs<'a, ChanSigner, M, T, K, F, L>) -> Result<Self, DecodeError> {
38063826
let _ver: u8 = Readable::read(reader)?;
38073827
let min_ver: u8 = Readable::read(reader)?;
38083828
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
@@ -4318,7 +4318,7 @@ fn test_no_txn_manager_serialize_deserialize() {
43184318
monitor: nodes[0].chan_monitor,
43194319
tx_broadcaster: nodes[0].tx_broadcaster.clone(),
43204320
logger: &logger,
4321-
channel_monitors: &mut channel_monitors,
4321+
channel_monitors,
43224322
}).unwrap()
43234323
};
43244324
nodes_0_deserialized = nodes_0_deserialized_tmp;
@@ -4426,7 +4426,7 @@ fn test_manager_serialize_deserialize_events() {
44264426
monitor: nodes[0].chan_monitor,
44274427
tx_broadcaster: nodes[0].tx_broadcaster.clone(),
44284428
logger: &logger,
4429-
channel_monitors: &mut channel_monitors,
4429+
channel_monitors,
44304430
}).unwrap()
44314431
};
44324432
nodes_0_deserialized = nodes_0_deserialized_tmp;
@@ -4516,7 +4516,7 @@ fn test_simple_manager_serialize_deserialize() {
45164516
monitor: nodes[0].chan_monitor,
45174517
tx_broadcaster: nodes[0].tx_broadcaster.clone(),
45184518
logger: &logger,
4519-
channel_monitors: &mut channel_monitors,
4519+
channel_monitors,
45204520
}).unwrap()
45214521
};
45224522
nodes_0_deserialized = nodes_0_deserialized_tmp;
@@ -4606,7 +4606,7 @@ fn test_manager_serialize_deserialize_inconsistent_monitor() {
46064606
monitor: nodes[0].chan_monitor,
46074607
tx_broadcaster: nodes[0].tx_broadcaster.clone(),
46084608
logger: &logger,
4609-
channel_monitors: &mut node_0_stale_monitors.iter_mut().map(|monitor| { (monitor.get_funding_txo().0, monitor) }).collect(),
4609+
channel_monitors: node_0_stale_monitors.iter_mut().map(|monitor| { (monitor.get_funding_txo().0, monitor) }).collect(),
46104610
}) { } else {
46114611
panic!("If the monitor(s) are stale, this indicates a bug and we should get an Err return");
46124612
};
@@ -4620,7 +4620,7 @@ fn test_manager_serialize_deserialize_inconsistent_monitor() {
46204620
monitor: nodes[0].chan_monitor,
46214621
tx_broadcaster: nodes[0].tx_broadcaster.clone(),
46224622
logger: &logger,
4623-
channel_monitors: &mut node_0_monitors.iter_mut().map(|monitor| { (monitor.get_funding_txo().0, monitor) }).collect(),
4623+
channel_monitors: node_0_monitors.iter_mut().map(|monitor| { (monitor.get_funding_txo().0, monitor) }).collect(),
46244624
}).unwrap();
46254625
nodes_0_deserialized = nodes_0_deserialized_tmp;
46264626
assert!(nodes_0_read.is_empty());
@@ -7891,7 +7891,7 @@ fn test_data_loss_protect() {
78917891
logger: &logger,
78927892
tx_broadcaster: &tx_broadcaster,
78937893
default_config: UserConfig::default(),
7894-
channel_monitors: &mut channel_monitors,
7894+
channel_monitors,
78957895
}).unwrap().1
78967896
};
78977897
nodes[0].node = &node_state_0;

0 commit comments

Comments
 (0)