Skip to content

Commit 0f5580a

Browse files
committed
Use Writeable for ChannelMonitor instead of a specific function.
There's no reason to have ChannelMonitor::write_for_disk instead of just using the Writeable trait anymore. Previously, it was used to differentiate with `write_for_watchtower`, but support for watchtower-mode ChannelMonitors was never completed and the partial bits were removed long ago. This has the nice benefit of hitting the custom Writeable codepaths in C bindings instead of trying to hit trait-generics paths.
1 parent 4345aa8 commit 0f5580a

File tree

8 files changed

+27
-28
lines changed

8 files changed

+27
-28
lines changed

fuzz/src/chanmon_consistency.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ impl chain::Watch for TestChainMonitor {
112112

113113
fn watch_channel(&self, funding_txo: OutPoint, monitor: channelmonitor::ChannelMonitor<EnforcingChannelKeys>) -> Result<(), channelmonitor::ChannelMonitorUpdateErr> {
114114
let mut ser = VecWriter(Vec::new());
115-
monitor.serialize_for_disk(&mut ser).unwrap();
115+
monitor.write(&mut ser).unwrap();
116116
if let Some(_) = self.latest_monitors.lock().unwrap().insert(funding_txo, (monitor.get_latest_update_id(), ser.0)) {
117117
panic!("Already had monitor pre-watch_channel");
118118
}
@@ -131,7 +131,7 @@ impl chain::Watch for TestChainMonitor {
131131
read(&mut Cursor::new(&map_entry.get().1)).unwrap().1;
132132
deserialized_monitor.update_monitor(&update, &&TestBroadcaster{}, &&FuzzEstimator{}, &self.logger).unwrap();
133133
let mut ser = VecWriter(Vec::new());
134-
deserialized_monitor.serialize_for_disk(&mut ser).unwrap();
134+
deserialized_monitor.write(&mut ser).unwrap();
135135
map_entry.insert((update.update_id, ser.0));
136136
self.should_update_manager.store(true, atomic::Ordering::Relaxed);
137137
self.update_ret.lock().unwrap().clone()

fuzz/src/chanmon_deser.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use bitcoin::hash_types::BlockHash;
55

66
use lightning::chain::channelmonitor;
77
use lightning::util::enforcing_trait_impls::EnforcingChannelKeys;
8-
use lightning::util::ser::{Readable, Writer};
8+
use lightning::util::ser::{Readable, Writer, Writeable};
99

1010
use utils::test_logger;
1111

@@ -26,7 +26,7 @@ impl Writer for VecWriter {
2626
pub fn do_test<Out: test_logger::Output>(data: &[u8], _out: Out) {
2727
if let Ok((latest_block_hash, monitor)) = <(BlockHash, channelmonitor::ChannelMonitor<EnforcingChannelKeys>)>::read(&mut Cursor::new(data)) {
2828
let mut w = VecWriter(Vec::new());
29-
monitor.serialize_for_disk(&mut w).unwrap();
29+
monitor.write(&mut w).unwrap();
3030
let deserialized_copy = <(BlockHash, channelmonitor::ChannelMonitor<EnforcingChannelKeys>)>::read(&mut Cursor::new(&w.0)).unwrap();
3131
assert!(latest_block_hash == deserialized_copy.0);
3232
assert!(monitor == deserialized_copy.1);

lightning-persister/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ trait DiskWriteable {
4545

4646
impl<ChanSigner: ChannelKeys> DiskWriteable for ChannelMonitor<ChanSigner> {
4747
fn write(&self, writer: &mut fs::File) -> Result<(), Error> {
48-
self.serialize_for_disk(writer)
48+
Writeable::write(self, writer)
4949
}
5050
}
5151

lightning/src/chain/channelmonitor.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,12 @@ impl Readable for ChannelMonitorUpdateStep {
617617
/// get_and_clear_pending_monitor_events or get_and_clear_pending_events are serialized to disk and
618618
/// reloaded at deserialize-time. Thus, you must ensure that, when handling events, all events
619619
/// gotten are fully handled before re-serializing the new state.
620+
///
621+
/// Note that the deserializer is only implemented for (Sha256dHash, ChannelMonitor), which
622+
/// tells you the last block hash which was block_connect()ed. You MUST rescan any blocks along
623+
/// the "reorg path" (ie disconnecting blocks until you find a common ancestor from both the
624+
/// returned block hash and the the current chain and then reconnecting blocks to get to the
625+
/// best chain) upon deserializing the object!
620626
pub struct ChannelMonitor<ChanSigner: ChannelKeys> {
621627
latest_update_id: u64,
622628
commitment_transaction_number_obscure_factor: u64,
@@ -755,15 +761,8 @@ impl<ChanSigner: ChannelKeys> PartialEq for ChannelMonitor<ChanSigner> {
755761
}
756762
}
757763

758-
impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
759-
/// Writes this monitor into the given writer, suitable for writing to disk.
760-
///
761-
/// Note that the deserializer is only implemented for (Sha256dHash, ChannelMonitor), which
762-
/// tells you the last block hash which was block_connect()ed. You MUST rescan any blocks along
763-
/// the "reorg path" (ie disconnecting blocks until you find a common ancestor from both the
764-
/// returned block hash and the the current chain and then reconnecting blocks to get to the
765-
/// best chain) upon deserializing the object!
766-
pub fn serialize_for_disk<W: Writer>(&self, writer: &mut W) -> Result<(), Error> {
764+
impl<ChanSigner: ChannelKeys> Writeable for ChannelMonitor<ChanSigner> {
765+
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), Error> {
767766
//TODO: We still write out all the serialization here manually instead of using the fancy
768767
//serialization framework we have, we should migrate things over to it.
769768
writer.write_all(&[SERIALIZATION_VERSION; 1])?;

lightning/src/ln/chanmon_update_fail_tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use routing::router::get_route;
2626
use util::enforcing_trait_impls::EnforcingChannelKeys;
2727
use util::events::{Event, EventsProvider, MessageSendEvent, MessageSendEventsProvider};
2828
use util::errors::APIError;
29-
use util::ser::Readable;
29+
use util::ser::{Readable, Writeable};
3030

3131
use bitcoin::hashes::sha256::Hash as Sha256;
3232
use bitcoin::hashes::Hash;
@@ -105,7 +105,7 @@ fn test_monitor_and_persister_update_fail() {
105105
let monitors = nodes[0].chain_monitor.chain_monitor.monitors.lock().unwrap();
106106
let monitor = monitors.get(&outpoint).unwrap();
107107
let mut w = test_utils::TestVecWriter(Vec::new());
108-
monitor.serialize_for_disk(&mut w).unwrap();
108+
monitor.write(&mut w).unwrap();
109109
let new_monitor = <(BlockHash, ChannelMonitor<EnforcingChannelKeys>)>::read(
110110
&mut ::std::io::Cursor::new(&w.0)).unwrap().1;
111111
assert!(new_monitor == *monitor);

lightning/src/ln/functional_test_utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ impl<'a, 'b, 'c> Drop for Node<'a, 'b, 'c> {
170170
let old_monitors = self.chain_monitor.chain_monitor.monitors.lock().unwrap();
171171
for (_, old_monitor) in old_monitors.iter() {
172172
let mut w = test_utils::TestVecWriter(Vec::new());
173-
old_monitor.serialize_for_disk(&mut w).unwrap();
173+
old_monitor.write(&mut w).unwrap();
174174
let (_, deserialized_monitor) = <(BlockHash, ChannelMonitor<EnforcingChannelKeys>)>::read(
175175
&mut ::std::io::Cursor::new(&w.0)).unwrap();
176176
deserialized_monitors.push(deserialized_monitor);

lightning/src/ln/functional_tests.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4279,7 +4279,7 @@ fn test_no_txn_manager_serialize_deserialize() {
42794279

42804280
let nodes_0_serialized = nodes[0].node.encode();
42814281
let mut chan_0_monitor_serialized = test_utils::TestVecWriter(Vec::new());
4282-
nodes[0].chain_monitor.chain_monitor.monitors.lock().unwrap().iter().next().unwrap().1.serialize_for_disk(&mut chan_0_monitor_serialized).unwrap();
4282+
nodes[0].chain_monitor.chain_monitor.monitors.lock().unwrap().iter().next().unwrap().1.write(&mut chan_0_monitor_serialized).unwrap();
42834283

42844284
logger = test_utils::TestLogger::new();
42854285
fee_estimator = test_utils::TestFeeEstimator { sat_per_kw: 253 };
@@ -4388,7 +4388,7 @@ fn test_manager_serialize_deserialize_events() {
43884388
// Start the de/seriailization process mid-channel creation to check that the channel manager will hold onto events that are serialized
43894389
let nodes_0_serialized = nodes[0].node.encode();
43904390
let mut chan_0_monitor_serialized = test_utils::TestVecWriter(Vec::new());
4391-
nodes[0].chain_monitor.chain_monitor.monitors.lock().unwrap().iter().next().unwrap().1.serialize_for_disk(&mut chan_0_monitor_serialized).unwrap();
4391+
nodes[0].chain_monitor.chain_monitor.monitors.lock().unwrap().iter().next().unwrap().1.write(&mut chan_0_monitor_serialized).unwrap();
43924392

43934393
fee_estimator = test_utils::TestFeeEstimator { sat_per_kw: 253 };
43944394
logger = test_utils::TestLogger::new();
@@ -4480,7 +4480,7 @@ fn test_simple_manager_serialize_deserialize() {
44804480

44814481
let nodes_0_serialized = nodes[0].node.encode();
44824482
let mut chan_0_monitor_serialized = test_utils::TestVecWriter(Vec::new());
4483-
nodes[0].chain_monitor.chain_monitor.monitors.lock().unwrap().iter().next().unwrap().1.serialize_for_disk(&mut chan_0_monitor_serialized).unwrap();
4483+
nodes[0].chain_monitor.chain_monitor.monitors.lock().unwrap().iter().next().unwrap().1.write(&mut chan_0_monitor_serialized).unwrap();
44844484

44854485
logger = test_utils::TestLogger::new();
44864486
fee_estimator = test_utils::TestFeeEstimator { sat_per_kw: 253 };
@@ -4539,7 +4539,7 @@ fn test_manager_serialize_deserialize_inconsistent_monitor() {
45394539
let mut node_0_stale_monitors_serialized = Vec::new();
45404540
for monitor in nodes[0].chain_monitor.chain_monitor.monitors.lock().unwrap().iter() {
45414541
let mut writer = test_utils::TestVecWriter(Vec::new());
4542-
monitor.1.serialize_for_disk(&mut writer).unwrap();
4542+
monitor.1.write(&mut writer).unwrap();
45434543
node_0_stale_monitors_serialized.push(writer.0);
45444544
}
45454545

@@ -4558,7 +4558,7 @@ fn test_manager_serialize_deserialize_inconsistent_monitor() {
45584558
let mut node_0_monitors_serialized = Vec::new();
45594559
for monitor in nodes[0].chain_monitor.chain_monitor.monitors.lock().unwrap().iter() {
45604560
let mut writer = test_utils::TestVecWriter(Vec::new());
4561-
monitor.1.serialize_for_disk(&mut writer).unwrap();
4561+
monitor.1.write(&mut writer).unwrap();
45624562
node_0_monitors_serialized.push(writer.0);
45634563
}
45644564

@@ -7392,7 +7392,7 @@ fn test_data_loss_protect() {
73927392
// Cache node A state before any channel update
73937393
let previous_node_state = nodes[0].node.encode();
73947394
let mut previous_chain_monitor_state = test_utils::TestVecWriter(Vec::new());
7395-
nodes[0].chain_monitor.chain_monitor.monitors.lock().unwrap().iter().next().unwrap().1.serialize_for_disk(&mut previous_chain_monitor_state).unwrap();
7395+
nodes[0].chain_monitor.chain_monitor.monitors.lock().unwrap().iter().next().unwrap().1.write(&mut previous_chain_monitor_state).unwrap();
73967396

73977397
send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000, 8_000_000);
73987398
send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000, 8_000_000);
@@ -8274,7 +8274,7 @@ fn test_update_err_monitor_lockdown() {
82748274
let monitors = nodes[0].chain_monitor.chain_monitor.monitors.lock().unwrap();
82758275
let monitor = monitors.get(&outpoint).unwrap();
82768276
let mut w = test_utils::TestVecWriter(Vec::new());
8277-
monitor.serialize_for_disk(&mut w).unwrap();
8277+
monitor.write(&mut w).unwrap();
82788278
let new_monitor = <(BlockHash, channelmonitor::ChannelMonitor<EnforcingChannelKeys>)>::read(
82798279
&mut ::std::io::Cursor::new(&w.0)).unwrap().1;
82808280
assert!(new_monitor == *monitor);
@@ -8333,7 +8333,7 @@ fn test_concurrent_monitor_claim() {
83338333
let monitors = nodes[0].chain_monitor.chain_monitor.monitors.lock().unwrap();
83348334
let monitor = monitors.get(&outpoint).unwrap();
83358335
let mut w = test_utils::TestVecWriter(Vec::new());
8336-
monitor.serialize_for_disk(&mut w).unwrap();
8336+
monitor.write(&mut w).unwrap();
83378337
let new_monitor = <(BlockHash, channelmonitor::ChannelMonitor<EnforcingChannelKeys>)>::read(
83388338
&mut ::std::io::Cursor::new(&w.0)).unwrap().1;
83398339
assert!(new_monitor == *monitor);
@@ -8359,7 +8359,7 @@ fn test_concurrent_monitor_claim() {
83598359
let monitors = nodes[0].chain_monitor.chain_monitor.monitors.lock().unwrap();
83608360
let monitor = monitors.get(&outpoint).unwrap();
83618361
let mut w = test_utils::TestVecWriter(Vec::new());
8362-
monitor.serialize_for_disk(&mut w).unwrap();
8362+
monitor.write(&mut w).unwrap();
83638363
let new_monitor = <(BlockHash, channelmonitor::ChannelMonitor<EnforcingChannelKeys>)>::read(
83648364
&mut ::std::io::Cursor::new(&w.0)).unwrap().1;
83658365
assert!(new_monitor == *monitor);

lightning/src/util/test_utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl<'a> chain::Watch for TestChainMonitor<'a> {
8787
// At every point where we get a monitor update, we should be able to send a useful monitor
8888
// to a watchtower and disk...
8989
let mut w = TestVecWriter(Vec::new());
90-
monitor.serialize_for_disk(&mut w).unwrap();
90+
monitor.write(&mut w).unwrap();
9191
let new_monitor = <(BlockHash, channelmonitor::ChannelMonitor<EnforcingChannelKeys>)>::read(
9292
&mut ::std::io::Cursor::new(&w.0)).unwrap().1;
9393
assert!(new_monitor == monitor);
@@ -120,7 +120,7 @@ impl<'a> chain::Watch for TestChainMonitor<'a> {
120120
let monitors = self.chain_monitor.monitors.lock().unwrap();
121121
let monitor = monitors.get(&funding_txo).unwrap();
122122
w.0.clear();
123-
monitor.serialize_for_disk(&mut w).unwrap();
123+
monitor.write(&mut w).unwrap();
124124
let new_monitor = <(BlockHash, channelmonitor::ChannelMonitor<EnforcingChannelKeys>)>::read(
125125
&mut ::std::io::Cursor::new(&w.0)).unwrap().1;
126126
assert!(new_monitor == *monitor);

0 commit comments

Comments
 (0)