Use Writeable for ChannelMonitor instead of a specific function.
authorMatt Corallo <git@bluematt.me>
Wed, 25 Nov 2020 20:03:19 +0000 (15:03 -0500)
committerMatt Corallo <git@bluematt.me>
Mon, 4 Jan 2021 17:40:40 +0000 (12:40 -0500)
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.

fuzz/src/chanmon_consistency.rs
fuzz/src/chanmon_deser.rs
lightning-persister/src/lib.rs
lightning/src/chain/channelmonitor.rs
lightning/src/ln/chanmon_update_fail_tests.rs
lightning/src/ln/functional_test_utils.rs
lightning/src/ln/functional_tests.rs
lightning/src/util/test_utils.rs

index 377ac8d7b5702c439e84dcc7b1db898645483428..732f0f5b1832028608ecab5b022ea22d446bc07c 100644 (file)
@@ -112,7 +112,7 @@ impl chain::Watch for TestChainMonitor {
 
        fn watch_channel(&self, funding_txo: OutPoint, monitor: channelmonitor::ChannelMonitor<EnforcingChannelKeys>) -> Result<(), channelmonitor::ChannelMonitorUpdateErr> {
                let mut ser = VecWriter(Vec::new());
-               monitor.serialize_for_disk(&mut ser).unwrap();
+               monitor.write(&mut ser).unwrap();
                if let Some(_) = self.latest_monitors.lock().unwrap().insert(funding_txo, (monitor.get_latest_update_id(), ser.0)) {
                        panic!("Already had monitor pre-watch_channel");
                }
@@ -131,7 +131,7 @@ impl chain::Watch for TestChainMonitor {
                        read(&mut Cursor::new(&map_entry.get().1)).unwrap().1;
                deserialized_monitor.update_monitor(&update, &&TestBroadcaster{}, &&FuzzEstimator{}, &self.logger).unwrap();
                let mut ser = VecWriter(Vec::new());
-               deserialized_monitor.serialize_for_disk(&mut ser).unwrap();
+               deserialized_monitor.write(&mut ser).unwrap();
                map_entry.insert((update.update_id, ser.0));
                self.should_update_manager.store(true, atomic::Ordering::Relaxed);
                self.update_ret.lock().unwrap().clone()
index fd326cc2ec85bdc72670da4cae4f7388366c05af..75a4044b5718568b3173a2622779845b9e204b17 100644 (file)
@@ -5,7 +5,7 @@ use bitcoin::hash_types::BlockHash;
 
 use lightning::chain::channelmonitor;
 use lightning::util::enforcing_trait_impls::EnforcingChannelKeys;
-use lightning::util::ser::{Readable, Writer};
+use lightning::util::ser::{Readable, Writer, Writeable};
 
 use utils::test_logger;
 
@@ -26,7 +26,7 @@ impl Writer for VecWriter {
 pub fn do_test<Out: test_logger::Output>(data: &[u8], _out: Out) {
        if let Ok((latest_block_hash, monitor)) = <(BlockHash, channelmonitor::ChannelMonitor<EnforcingChannelKeys>)>::read(&mut Cursor::new(data)) {
                let mut w = VecWriter(Vec::new());
-               monitor.serialize_for_disk(&mut w).unwrap();
+               monitor.write(&mut w).unwrap();
                let deserialized_copy = <(BlockHash, channelmonitor::ChannelMonitor<EnforcingChannelKeys>)>::read(&mut Cursor::new(&w.0)).unwrap();
                assert!(latest_block_hash == deserialized_copy.0);
                assert!(monitor == deserialized_copy.1);
index ec08e948f60411e41aa00c0bc60d4704b8e71fce..ff8eeeb1f860427fb76e91f2692c7197d54e2525 100644 (file)
@@ -45,7 +45,7 @@ trait DiskWriteable {
 
 impl<ChanSigner: ChannelKeys> DiskWriteable for ChannelMonitor<ChanSigner> {
        fn write(&self, writer: &mut fs::File) -> Result<(), Error> {
-               self.serialize_for_disk(writer)
+               Writeable::write(self, writer)
        }
 }
 
index 548c8f06c2051225391a6c3a7563520a43b809af..5278376126228bcc467a80278c61128e44bc1013 100644 (file)
@@ -617,6 +617,12 @@ impl Readable for ChannelMonitorUpdateStep {
 /// get_and_clear_pending_monitor_events or get_and_clear_pending_events are serialized to disk and
 /// reloaded at deserialize-time. Thus, you must ensure that, when handling events, all events
 /// gotten are fully handled before re-serializing the new state.
+///
+/// Note that the deserializer is only implemented for (Sha256dHash, ChannelMonitor), which
+/// tells you the last block hash which was block_connect()ed. You MUST rescan any blocks along
+/// the "reorg path" (ie disconnecting blocks until you find a common ancestor from both the
+/// returned block hash and the the current chain and then reconnecting blocks to get to the
+/// best chain) upon deserializing the object!
 pub struct ChannelMonitor<ChanSigner: ChannelKeys> {
        latest_update_id: u64,
        commitment_transaction_number_obscure_factor: u64,
@@ -755,15 +761,8 @@ impl<ChanSigner: ChannelKeys> PartialEq for ChannelMonitor<ChanSigner> {
        }
 }
 
-impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
-       /// Writes this monitor into the given writer, suitable for writing to disk.
-       ///
-       /// Note that the deserializer is only implemented for (Sha256dHash, ChannelMonitor), which
-       /// tells you the last block hash which was block_connect()ed. You MUST rescan any blocks along
-       /// the "reorg path" (ie disconnecting blocks until you find a common ancestor from both the
-       /// returned block hash and the the current chain and then reconnecting blocks to get to the
-       /// best chain) upon deserializing the object!
-       pub fn serialize_for_disk<W: Writer>(&self, writer: &mut W) -> Result<(), Error> {
+impl<ChanSigner: ChannelKeys> Writeable for ChannelMonitor<ChanSigner> {
+       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), Error> {
                //TODO: We still write out all the serialization here manually instead of using the fancy
                //serialization framework we have, we should migrate things over to it.
                writer.write_all(&[SERIALIZATION_VERSION; 1])?;
index 689c3496de16ebfd83fdc782d7ddec931d36b0f9..1d4655bbdd0983dd4f799a548df70503d78abc6f 100644 (file)
@@ -26,7 +26,7 @@ use routing::router::get_route;
 use util::enforcing_trait_impls::EnforcingChannelKeys;
 use util::events::{Event, EventsProvider, MessageSendEvent, MessageSendEventsProvider};
 use util::errors::APIError;
-use util::ser::Readable;
+use util::ser::{Readable, Writeable};
 
 use bitcoin::hashes::sha256::Hash as Sha256;
 use bitcoin::hashes::Hash;
@@ -105,7 +105,7 @@ fn test_monitor_and_persister_update_fail() {
                let monitors = nodes[0].chain_monitor.chain_monitor.monitors.lock().unwrap();
                let monitor = monitors.get(&outpoint).unwrap();
                let mut w = test_utils::TestVecWriter(Vec::new());
-               monitor.serialize_for_disk(&mut w).unwrap();
+               monitor.write(&mut w).unwrap();
                let new_monitor = <(BlockHash, ChannelMonitor<EnforcingChannelKeys>)>::read(
                        &mut ::std::io::Cursor::new(&w.0)).unwrap().1;
                assert!(new_monitor == *monitor);
index d54045f8e619d7d3075482d7e893279ca04f7686..94c4474f8bd20de07263a9f0ef7ba2d923e63000 100644 (file)
@@ -170,7 +170,7 @@ impl<'a, 'b, 'c> Drop for Node<'a, 'b, 'c> {
                                let old_monitors = self.chain_monitor.chain_monitor.monitors.lock().unwrap();
                                for (_, old_monitor) in old_monitors.iter() {
                                        let mut w = test_utils::TestVecWriter(Vec::new());
-                                       old_monitor.serialize_for_disk(&mut w).unwrap();
+                                       old_monitor.write(&mut w).unwrap();
                                        let (_, deserialized_monitor) = <(BlockHash, ChannelMonitor<EnforcingChannelKeys>)>::read(
                                                &mut ::std::io::Cursor::new(&w.0)).unwrap();
                                        deserialized_monitors.push(deserialized_monitor);
index 36709062a6d3b8e4d06a7542dee627e920b621cd..98b166285a3991ee13b1d131eee3fdda9916c297 100644 (file)
@@ -4279,7 +4279,7 @@ fn test_no_txn_manager_serialize_deserialize() {
 
        let nodes_0_serialized = nodes[0].node.encode();
        let mut chan_0_monitor_serialized = test_utils::TestVecWriter(Vec::new());
-       nodes[0].chain_monitor.chain_monitor.monitors.lock().unwrap().iter().next().unwrap().1.serialize_for_disk(&mut chan_0_monitor_serialized).unwrap();
+       nodes[0].chain_monitor.chain_monitor.monitors.lock().unwrap().iter().next().unwrap().1.write(&mut chan_0_monitor_serialized).unwrap();
 
        logger = test_utils::TestLogger::new();
        fee_estimator = test_utils::TestFeeEstimator { sat_per_kw: 253 };
@@ -4388,7 +4388,7 @@ fn test_manager_serialize_deserialize_events() {
        // Start the de/seriailization process mid-channel creation to check that the channel manager will hold onto events that are serialized
        let nodes_0_serialized = nodes[0].node.encode();
        let mut chan_0_monitor_serialized = test_utils::TestVecWriter(Vec::new());
-       nodes[0].chain_monitor.chain_monitor.monitors.lock().unwrap().iter().next().unwrap().1.serialize_for_disk(&mut chan_0_monitor_serialized).unwrap();
+       nodes[0].chain_monitor.chain_monitor.monitors.lock().unwrap().iter().next().unwrap().1.write(&mut chan_0_monitor_serialized).unwrap();
 
        fee_estimator = test_utils::TestFeeEstimator { sat_per_kw: 253 };
        logger = test_utils::TestLogger::new();
@@ -4480,7 +4480,7 @@ fn test_simple_manager_serialize_deserialize() {
 
        let nodes_0_serialized = nodes[0].node.encode();
        let mut chan_0_monitor_serialized = test_utils::TestVecWriter(Vec::new());
-       nodes[0].chain_monitor.chain_monitor.monitors.lock().unwrap().iter().next().unwrap().1.serialize_for_disk(&mut chan_0_monitor_serialized).unwrap();
+       nodes[0].chain_monitor.chain_monitor.monitors.lock().unwrap().iter().next().unwrap().1.write(&mut chan_0_monitor_serialized).unwrap();
 
        logger = test_utils::TestLogger::new();
        fee_estimator = test_utils::TestFeeEstimator { sat_per_kw: 253 };
@@ -4539,7 +4539,7 @@ fn test_manager_serialize_deserialize_inconsistent_monitor() {
        let mut node_0_stale_monitors_serialized = Vec::new();
        for monitor in nodes[0].chain_monitor.chain_monitor.monitors.lock().unwrap().iter() {
                let mut writer = test_utils::TestVecWriter(Vec::new());
-               monitor.1.serialize_for_disk(&mut writer).unwrap();
+               monitor.1.write(&mut writer).unwrap();
                node_0_stale_monitors_serialized.push(writer.0);
        }
 
@@ -4558,7 +4558,7 @@ fn test_manager_serialize_deserialize_inconsistent_monitor() {
        let mut node_0_monitors_serialized = Vec::new();
        for monitor in nodes[0].chain_monitor.chain_monitor.monitors.lock().unwrap().iter() {
                let mut writer = test_utils::TestVecWriter(Vec::new());
-               monitor.1.serialize_for_disk(&mut writer).unwrap();
+               monitor.1.write(&mut writer).unwrap();
                node_0_monitors_serialized.push(writer.0);
        }
 
@@ -7392,7 +7392,7 @@ fn test_data_loss_protect() {
        // Cache node A state before any channel update
        let previous_node_state = nodes[0].node.encode();
        let mut previous_chain_monitor_state = test_utils::TestVecWriter(Vec::new());
-       nodes[0].chain_monitor.chain_monitor.monitors.lock().unwrap().iter().next().unwrap().1.serialize_for_disk(&mut previous_chain_monitor_state).unwrap();
+       nodes[0].chain_monitor.chain_monitor.monitors.lock().unwrap().iter().next().unwrap().1.write(&mut previous_chain_monitor_state).unwrap();
 
        send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000, 8_000_000);
        send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000, 8_000_000);
@@ -8274,7 +8274,7 @@ fn test_update_err_monitor_lockdown() {
                let monitors = nodes[0].chain_monitor.chain_monitor.monitors.lock().unwrap();
                let monitor = monitors.get(&outpoint).unwrap();
                let mut w = test_utils::TestVecWriter(Vec::new());
-               monitor.serialize_for_disk(&mut w).unwrap();
+               monitor.write(&mut w).unwrap();
                let new_monitor = <(BlockHash, channelmonitor::ChannelMonitor<EnforcingChannelKeys>)>::read(
                                &mut ::std::io::Cursor::new(&w.0)).unwrap().1;
                assert!(new_monitor == *monitor);
@@ -8333,7 +8333,7 @@ fn test_concurrent_monitor_claim() {
                let monitors = nodes[0].chain_monitor.chain_monitor.monitors.lock().unwrap();
                let monitor = monitors.get(&outpoint).unwrap();
                let mut w = test_utils::TestVecWriter(Vec::new());
-               monitor.serialize_for_disk(&mut w).unwrap();
+               monitor.write(&mut w).unwrap();
                let new_monitor = <(BlockHash, channelmonitor::ChannelMonitor<EnforcingChannelKeys>)>::read(
                                &mut ::std::io::Cursor::new(&w.0)).unwrap().1;
                assert!(new_monitor == *monitor);
@@ -8359,7 +8359,7 @@ fn test_concurrent_monitor_claim() {
                let monitors = nodes[0].chain_monitor.chain_monitor.monitors.lock().unwrap();
                let monitor = monitors.get(&outpoint).unwrap();
                let mut w = test_utils::TestVecWriter(Vec::new());
-               monitor.serialize_for_disk(&mut w).unwrap();
+               monitor.write(&mut w).unwrap();
                let new_monitor = <(BlockHash, channelmonitor::ChannelMonitor<EnforcingChannelKeys>)>::read(
                                &mut ::std::io::Cursor::new(&w.0)).unwrap().1;
                assert!(new_monitor == *monitor);
index c944e572cd4569deb44ee8d06180f64d2440ef41..6e27e0431dfe335ae5651021bcebd4ba83860c71 100644 (file)
@@ -87,7 +87,7 @@ impl<'a> chain::Watch for TestChainMonitor<'a> {
                // At every point where we get a monitor update, we should be able to send a useful monitor
                // to a watchtower and disk...
                let mut w = TestVecWriter(Vec::new());
-               monitor.serialize_for_disk(&mut w).unwrap();
+               monitor.write(&mut w).unwrap();
                let new_monitor = <(BlockHash, channelmonitor::ChannelMonitor<EnforcingChannelKeys>)>::read(
                        &mut ::std::io::Cursor::new(&w.0)).unwrap().1;
                assert!(new_monitor == monitor);
@@ -120,7 +120,7 @@ impl<'a> chain::Watch for TestChainMonitor<'a> {
                let monitors = self.chain_monitor.monitors.lock().unwrap();
                let monitor = monitors.get(&funding_txo).unwrap();
                w.0.clear();
-               monitor.serialize_for_disk(&mut w).unwrap();
+               monitor.write(&mut w).unwrap();
                let new_monitor = <(BlockHash, channelmonitor::ChannelMonitor<EnforcingChannelKeys>)>::read(
                        &mut ::std::io::Cursor::new(&w.0)).unwrap().1;
                assert!(new_monitor == *monitor);