Skip to content

Peer Storage (Part 3): Identifying Lost Channel States #3897

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,5 @@ check-cfg = [
"cfg(splicing)",
"cfg(async_payments)",
"cfg(simple_close)",
"cfg(peer_storage)",
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Indentation is off.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, need to fix my indentation configuration. Fixed.

]
2 changes: 2 additions & 0 deletions ci/ci-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,5 @@ RUSTFLAGS="--cfg=async_payments" cargo test --verbose --color always -p lightnin
RUSTFLAGS="--cfg=simple_close" cargo test --verbose --color always -p lightning
[ "$CI_MINIMIZE_DISK_USAGE" != "" ] && cargo clean
RUSTFLAGS="--cfg=lsps1_service" cargo test --verbose --color always -p lightning-liquidity
[ "$CI_MINIMIZE_DISK_USAGE" != "" ] && cargo clean
RUSTFLAGS="--cfg=peer_storage" cargo test --verbose --color always -p lightning
57 changes: 51 additions & 6 deletions lightning/src/chain/chainmonitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,17 @@ use bitcoin::hash_types::{BlockHash, Txid};

use crate::chain;
use crate::chain::chaininterface::{BroadcasterInterface, FeeEstimator};
#[allow(unused_imports)]
use crate::chain::channelmonitor::{
Balance, ChannelMonitor, ChannelMonitorUpdate, MonitorEvent, TransactionOutputs,
WithChannelMonitor,
write_chanmon_internal, Balance, ChannelMonitor, ChannelMonitorUpdate, MonitorEvent,
TransactionOutputs, WithChannelMonitor,
};
use crate::chain::transaction::{OutPoint, TransactionData};
use crate::chain::{ChannelMonitorUpdateStatus, Filter, WatchedOutput};
use crate::events::{self, Event, EventHandler, ReplayEvent};
use crate::ln::channel_state::ChannelDetails;
use crate::ln::msgs::{self, BaseMessageHandler, Init, MessageSendEvent, SendOnlyMessageHandler};
use crate::ln::our_peer_storage::DecryptedOurPeerStorage;
use crate::ln::our_peer_storage::{DecryptedOurPeerStorage, PeerStorageMonitorHolder};
use crate::ln::types::ChannelId;
use crate::prelude::*;
use crate::sign::ecdsa::EcdsaChannelSigner;
Expand All @@ -47,6 +48,8 @@ use crate::types::features::{InitFeatures, NodeFeatures};
use crate::util::errors::APIError;
use crate::util::logger::{Logger, WithContext};
use crate::util::persist::MonitorName;
#[allow(unused_imports)]
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Please rather cfg-gate these imports, too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

use crate::util::ser::{VecWriter, Writeable};
use crate::util::wakers::{Future, Notifier};
use bitcoin::secp256k1::PublicKey;
use core::ops::Deref;
Expand Down Expand Up @@ -810,10 +813,52 @@ where
}

fn send_peer_storage(&self, their_node_id: PublicKey) {
// TODO: Serialize `ChannelMonitor`s inside `our_peer_storage`.

#[allow(unused_mut)]
let mut monitors_list: Vec<PeerStorageMonitorHolder> = Vec::new();
let random_bytes = self.entropy_source.get_secure_random_bytes();
let serialised_channels = Vec::new();

#[cfg(peer_storage)]
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not cfg-gate the whole message then? This way it might get really confusing what is part and what isn't part of the cfg flag?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, for now, cfg-tagging fn send_peer_storage and all the call sites of this function.

{
const MAX_PEER_STORAGE_SIZE: usize = 65531;
const USIZE_LEN: usize = core::mem::size_of::<usize>();
let random_usize = usize::from_le_bytes(random_bytes[0..USIZE_LEN].try_into().unwrap());
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Let's just avoid the unnecessary unwrap here:

diff --git a/lightning/src/chain/chainmonitor.rs b/lightning/src/chain/chainmonitor.rs
index 1681d0187..ba7613f8e 100644
--- a/lightning/src/chain/chainmonitor.rs
+++ b/lightning/src/chain/chainmonitor.rs
@@ -816,12 +816,13 @@ where
                #[allow(unused_mut)]
                let mut monitors_list: Vec<PeerStorageMonitorHolder> = Vec::new();
                let random_bytes = self.entropy_source.get_secure_random_bytes();
-
                #[cfg(peer_storage)]
                {
                        const MAX_PEER_STORAGE_SIZE: usize = 65531;
                        const USIZE_LEN: usize = core::mem::size_of::<usize>();
-                       let random_usize = usize::from_le_bytes(random_bytes[0..USIZE_LEN].try_into().unwrap());
+                       let mut usize_bytes = [0u8; USIZE_LEN];
+                       usize_bytes.copy_from_slice(&random_bytes);
+                       let random_usize = usize::from_le_bytes(usize_bytes);
                        let mut curr_size = 0;
                        let monitors = self.monitors.read().unwrap();
                        // Randomising Keys in the HashMap to fetch monitors without repetition.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for this cleanup. Fixed.

let mut curr_size = 0;
let monitors = self.monitors.read().unwrap();
// Randomising Keys in the HashMap to fetch monitors without repetition.
let mut keys: Vec<&ChannelId> = monitors.keys().collect();
for i in (1..keys.len()).rev() {
let j = random_usize % (i + 1);
keys.swap(i, j);
}

for chan_id in keys {
let mon = &monitors[chan_id];
let mut ser_chan = VecWriter(Vec::new());
let min_seen_secret = mon.monitor.get_min_seen_secret();
let counterparty_node_id = mon.monitor.get_counterparty_node_id();
let chan_mon = mon.monitor.inner.lock().unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we put this lock and the write call into a separate scope so we drop the lock ASAP again?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, thanks for pointing this out. Fixed.


write_chanmon_internal(&chan_mon, true, &mut ser_chan)
.expect("can not write Channel Monitor for peer storage message");

let peer_storage_monitor = PeerStorageMonitorHolder {
channel_id: *chan_id,
min_seen_secret,
counterparty_node_id,
monitor_bytes: ser_chan.0,
};

// Adding size of peer_storage_monitor.
curr_size += peer_storage_monitor.serialized_length();

if curr_size > MAX_PEER_STORAGE_SIZE {
break;
}
monitors_list.push(peer_storage_monitor);
}
}

let serialised_channels = monitors_list.encode();
let our_peer_storage = DecryptedOurPeerStorage::new(serialised_channels);
let cipher = our_peer_storage.encrypt(&self.our_peerstorage_encryption_key, &random_bytes);

Expand Down
Loading
Loading