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 13 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
59 changes: 54 additions & 5 deletions lightning/src/chain/chainmonitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,17 @@ use bitcoin::hash_types::{BlockHash, Txid};
use crate::chain;
use crate::chain::chaininterface::{BroadcasterInterface, FeeEstimator};
use crate::chain::channelmonitor::{
Balance, ChannelMonitor, ChannelMonitorUpdate, MonitorEvent, TransactionOutputs,
write_util_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, PeerStorageMonitorHolderList,
};
use crate::ln::types::ChannelId;
use crate::prelude::*;
use crate::sign::ecdsa::EcdsaChannelSigner;
Expand All @@ -47,6 +49,7 @@ use crate::types::features::{InitFeatures, NodeFeatures};
use crate::util::errors::APIError;
use crate::util::logger::{Logger, WithContext};
use crate::util::persist::MonitorName;
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,56 @@ where
}

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

const MAX_PEER_STORAGE_SIZE: usize = 65531;
let random_bytes = self.entropy_source.get_secure_random_bytes();
let serialised_channels = Vec::new();
let random_usize = usize::from_le_bytes(random_bytes[0..core::mem::size_of::<usize>()].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 keep the length in a separate const, avoiding to make this line overly long/noisy.

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, thanks!


let monitors = self.monitors.read().unwrap();
let mut monitors_list = PeerStorageMonitorHolderList { monitors: Vec::new() };
let mut curr_size = 0;

// Randomising Keys in the HashMap to fetch monitors without repetition.
let mut keys: Vec<&ChannelId> = monitors.keys().collect();
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 make this a bit cleaner by using the proposed iterator skiping approach in the loop below, maybe while simply keeping track of which monitors we already wrote?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will address this in the next fixup.

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();

match write_util_internal(&chan_mon, true, &mut ser_chan) {
Ok(_) => {
// Adding size of peer_storage_monitor.
curr_size += ser_chan.0.serialized_length()
Copy link
Contributor

Choose a reason for hiding this comment

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

If we move this check below constructing PeerStorageMonitorHolder, can we just use it's serialized_lenght implementation instead of tallying up the individual fields here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My bad, thanks for pointing this out. Fixed.

+ min_seen_secret.serialized_length()
+ chan_id.serialized_length()
+ counterparty_node_id.serialized_length();

if curr_size > MAX_PEER_STORAGE_SIZE {
break;
}

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

monitors_list.monitors.push(peer_storage_monitor);
},
Err(_) => {
panic!("Can not write monitor for {}", mon.monitor.channel_id())
Copy link
Contributor

Choose a reason for hiding this comment

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

Really, please avoid these explicit panics in any of this code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This would panic only if there is some issue with the write_util which suggests that we are unable to serialise the channelmonitor, should we just log the error here instead?

Copy link
Contributor

Choose a reason for hiding this comment

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

Mhh, while I'd still prefer to never panic in any of the peer storage code, indeed this would likely only get hit if we run out of memory or similar. If we keep it, let's just expect on the write_internal call, which avoids all this matching.

},
}
}

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