Skip to content

Change ChannelManager deserialization to return an optional blockhash #819

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 3, 2021

Conversation

TheBlueMatt
Copy link
Collaborator

This is just #810 rebased with one additional commit on top to update documentation.

@codecov
Copy link

codecov bot commented Mar 2, 2021

Codecov Report

Merging #819 (8550bd4) into main (81c6bdc) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##             main     #819   +/-   ##
=======================================
  Coverage   90.73%   90.74%           
=======================================
  Files          48       48           
  Lines       25642    25648    +6     
=======================================
+ Hits        23267    23274    +7     
+ Misses       2375     2374    -1     
Impacted Files Coverage Δ
lightning-block-sync/src/init.rs 93.56% <ø> (ø)
lightning-persister/src/lib.rs 92.52% <100.00%> (ø)
lightning/src/chain/channelmonitor.rs 95.53% <100.00%> (+<0.01%) ⬆️
lightning/src/ln/chanmon_update_fail_tests.rs 97.56% <100.00%> (ø)
lightning/src/ln/channelmanager.rs 85.28% <100.00%> (+0.02%) ⬆️
lightning/src/ln/functional_test_utils.rs 94.97% <100.00%> (ø)
lightning/src/ln/functional_tests.rs 96.96% <100.00%> (+0.01%) ⬆️
lightning/src/util/test_utils.rs 83.49% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 81c6bdc...8550bd4. Read the comment docs.

@TheBlueMatt TheBlueMatt force-pushed the 2021-03-810-rebased branch from eaccb04 to 514119a Compare March 2, 2021 19:30
valentinewallace and others added 3 commits March 2, 2021 14:30
If the ChannelManager never receives any blocks, it'll return a default blockhash
on deserialization. It's preferable for this to be an Option instead.
@TheBlueMatt TheBlueMatt force-pushed the 2021-03-810-rebased branch from 514119a to 8550bd4 Compare March 2, 2021 19:31
@TheBlueMatt
Copy link
Collaborator Author

TheBlueMatt commented Mar 2, 2021

Fixed nits and squashed. Gonna merge. Full diff from Val's original work is:

diff --git a/lightning/src/chain/channelmonitor.rs b/lightning/src/chain/channelmonitor.rs
index c989b44f..4d2d1b84 100644
--- a/lightning/src/chain/channelmonitor.rs
+++ b/lightning/src/chain/channelmonitor.rs
@@ -620,7 +620,7 @@ impl Readable for ChannelMonitorUpdateStep {
 /// 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
+/// Note that the deserializer is only implemented for (Option<BlockHash>, 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
@@ -2735,10 +2735,11 @@ impl<'a, Signer: Sign, K: KeysInterface<Signer = Signer>> ReadableArgs<&'a K>
                let mut secp_ctx = Secp256k1::new();
                secp_ctx.seeded_randomize(&keys_manager.get_secure_random_bytes());
 
-               let mut last_seen_block_hash = Some(last_block_hash.clone());
-               if last_block_hash == Default::default() {
-                       last_seen_block_hash = None;
-               }
+               let last_seen_block_hash = if last_block_hash == Default::default() {
+                       None
+               } else {
+                       Some(last_block_hash)
+               };
 
                Ok((last_seen_block_hash, ChannelMonitor {
                        inner: Mutex::new(ChannelMonitorImpl {
diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs
index b64c488a..d3e520f7 100644
--- a/lightning/src/ln/channelmanager.rs
+++ b/lightning/src/ln/channelmanager.rs
@@ -380,7 +380,7 @@ pub type SimpleRefChannelManager<'a, 'b, 'c, 'd, 'e, M, T, F, L> = ChannelManage
 /// ChannelMonitors passed by reference to read(), those channels will be force-closed based on the
 /// ChannelMonitor state and no funds will be lost (mod on-chain transaction fees).
 ///
-/// Note that the deserializer is only implemented for (Sha256dHash, ChannelManager), which
+/// Note that the deserializer is only implemented for (Option<BlockHash>, ChannelManager), which
 /// tells you the last block hash which was block_connect()ed. You MUST rescan any blocks along
 /// the "reorg path" (ie call block_disconnected() until you get to a common block and then call
 /// block_connected() to step towards your best block) upon deserialization before using the
@@ -3925,7 +3925,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> Writeable f
 /// At a high-level, the process for deserializing a ChannelManager and resuming normal operation
 /// is:
 /// 1) Deserialize all stored ChannelMonitors.
-/// 2) Deserialize the ChannelManager by filling in this struct and calling <(Sha256dHash,
+/// 2) Deserialize the ChannelManager by filling in this struct and calling <(Option<BlockHash>,
 ///    ChannelManager)>::read(reader, args).
 ///    This may result in closing some Channels if the ChannelMonitor is newer than the stored
 ///    ChannelManager state to ensure no loss of funds. Thus, transactions may be broadcasted.
@@ -4172,10 +4172,11 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
                //TODO: Broadcast channel update for closed channels, but only after we've made a
                //connection or two.
 
-               let mut last_seen_block_hash = Some(last_block_hash.clone());
-               if last_block_hash == Default::default() {
-                       last_seen_block_hash = None;
-               }
+               let last_seen_block_hash = if last_block_hash == Default::default() {
+                       None
+               } else {
+                       Some(last_block_hash)
+               };
                Ok((last_seen_block_hash, channel_manager))
        }
 }

Copy link

@ariard ariard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review ACK 8550bd4

@TheBlueMatt TheBlueMatt merged commit 793de5f into lightningdevkit:main Mar 3, 2021
jkczyz added a commit to jkczyz/rust-lightning that referenced this pull request Mar 3, 2021
…03-810-rebased"

This reverts commit 793de5f, reversing
changes made to 03a5189.
jkczyz added a commit to jkczyz/rust-lightning that referenced this pull request Mar 5, 2021
…03-810-rebased"

This reverts commit 793de5f, reversing
changes made to 03a5189.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants