Skip to content

Commit 53c0350

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 6cd3b2b commit 53c0350

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
@@ -4319,7 +4319,7 @@ fn test_no_txn_manager_serialize_deserialize() {
43194319

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

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

44334433
fee_estimator = test_utils::TestFeeEstimator { sat_per_kw: 253 };
44344434
logger = test_utils::TestLogger::new();
@@ -4520,7 +4520,7 @@ fn test_simple_manager_serialize_deserialize() {
45204520

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

45254525
logger = test_utils::TestLogger::new();
45264526
fee_estimator = test_utils::TestFeeEstimator { sat_per_kw: 253 };
@@ -4579,7 +4579,7 @@ fn test_manager_serialize_deserialize_inconsistent_monitor() {
45794579
let mut node_0_stale_monitors_serialized = Vec::new();
45804580
for monitor in nodes[0].chain_monitor.chain_monitor.monitors.lock().unwrap().iter() {
45814581
let mut writer = test_utils::TestVecWriter(Vec::new());
4582-
monitor.1.serialize_for_disk(&mut writer).unwrap();
4582+
monitor.1.write(&mut writer).unwrap();
45834583
node_0_stale_monitors_serialized.push(writer.0);
45844584
}
45854585

@@ -4598,7 +4598,7 @@ fn test_manager_serialize_deserialize_inconsistent_monitor() {
45984598
let mut node_0_monitors_serialized = Vec::new();
45994599
for monitor in nodes[0].chain_monitor.chain_monitor.monitors.lock().unwrap().iter() {
46004600
let mut writer = test_utils::TestVecWriter(Vec::new());
4601-
monitor.1.serialize_for_disk(&mut writer).unwrap();
4601+
monitor.1.write(&mut writer).unwrap();
46024602
node_0_monitors_serialized.push(writer.0);
46034603
}
46044604

@@ -7432,7 +7432,7 @@ fn test_data_loss_protect() {
74327432
// Cache node A state before any channel update
74337433
let previous_node_state = nodes[0].node.encode();
74347434
let mut previous_chain_monitor_state = test_utils::TestVecWriter(Vec::new());
7435-
nodes[0].chain_monitor.chain_monitor.monitors.lock().unwrap().iter().next().unwrap().1.serialize_for_disk(&mut previous_chain_monitor_state).unwrap();
7435+
nodes[0].chain_monitor.chain_monitor.monitors.lock().unwrap().iter().next().unwrap().1.write(&mut previous_chain_monitor_state).unwrap();
74367436

74377437
send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000, 8_000_000);
74387438
send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000, 8_000_000);
@@ -8314,7 +8314,7 @@ fn test_update_err_monitor_lockdown() {
83148314
let monitors = nodes[0].chain_monitor.chain_monitor.monitors.lock().unwrap();
83158315
let monitor = monitors.get(&outpoint).unwrap();
83168316
let mut w = test_utils::TestVecWriter(Vec::new());
8317-
monitor.serialize_for_disk(&mut w).unwrap();
8317+
monitor.write(&mut w).unwrap();
83188318
let new_monitor = <(BlockHash, channelmonitor::ChannelMonitor<EnforcingChannelKeys>)>::read(
83198319
&mut ::std::io::Cursor::new(&w.0)).unwrap().1;
83208320
assert!(new_monitor == *monitor);
@@ -8373,7 +8373,7 @@ fn test_concurrent_monitor_claim() {
83738373
let monitors = nodes[0].chain_monitor.chain_monitor.monitors.lock().unwrap();
83748374
let monitor = monitors.get(&outpoint).unwrap();
83758375
let mut w = test_utils::TestVecWriter(Vec::new());
8376-
monitor.serialize_for_disk(&mut w).unwrap();
8376+
monitor.write(&mut w).unwrap();
83778377
let new_monitor = <(BlockHash, channelmonitor::ChannelMonitor<EnforcingChannelKeys>)>::read(
83788378
&mut ::std::io::Cursor::new(&w.0)).unwrap().1;
83798379
assert!(new_monitor == *monitor);
@@ -8399,7 +8399,7 @@ fn test_concurrent_monitor_claim() {
83998399
let monitors = nodes[0].chain_monitor.chain_monitor.monitors.lock().unwrap();
84008400
let monitor = monitors.get(&outpoint).unwrap();
84018401
let mut w = test_utils::TestVecWriter(Vec::new());
8402-
monitor.serialize_for_disk(&mut w).unwrap();
8402+
monitor.write(&mut w).unwrap();
84038403
let new_monitor = <(BlockHash, channelmonitor::ChannelMonitor<EnforcingChannelKeys>)>::read(
84048404
&mut ::std::io::Cursor::new(&w.0)).unwrap().1;
84058405
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)