Skip to content

Commit 7bb1652

Browse files
authored
Merge pull request #3897 from adi2011/peer-storage/serialise-deserialise
Peer Storage (Part 3): Identifying Lost Channel States
2 parents 72fd628 + e51028c commit 7bb1652

File tree

8 files changed

+485
-250
lines changed

8 files changed

+485
-250
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,5 @@ check-cfg = [
6767
"cfg(require_route_graph_test)",
6868
"cfg(splicing)",
6969
"cfg(simple_close)",
70+
"cfg(peer_storage)",
7071
]

ci/ci-tests.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,5 @@ RUSTFLAGS="--cfg=async_payments" cargo test --verbose --color always -p lightnin
158158
RUSTFLAGS="--cfg=simple_close" cargo test --verbose --color always -p lightning
159159
[ "$CI_MINIMIZE_DISK_USAGE" != "" ] && cargo clean
160160
RUSTFLAGS="--cfg=lsps1_service" cargo test --verbose --color always -p lightning-liquidity
161+
[ "$CI_MINIMIZE_DISK_USAGE" != "" ] && cargo clean
162+
RUSTFLAGS="--cfg=peer_storage" cargo test --verbose --color always -p lightning

fuzz/src/full_stack.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,7 +1182,7 @@ fn two_peer_forwarding_seed() -> Vec<u8> {
11821182

11831183
// broadcast funding transaction
11841184
ext_from_hex("0b", &mut test);
1185-
// by now client should have sent a channel_ready (CHECK 4: SendChannelReady to 03020000 for chan 2f000000)
1185+
// by now client should have sent a channel_ready (CHECK 4: SendChannelReady to 03020000 for chan 3f000000)
11861186

11871187
// inbound read from peer id 1 of len 18
11881188
ext_from_hex("030112", &mut test);
@@ -1441,7 +1441,7 @@ fn two_peer_forwarding_seed() -> Vec<u8> {
14411441
// inbound read from peer id 0 of len 193
14421442
ext_from_hex("0300c1", &mut test);
14431443
// end of update_add_htlc from 0 to 1 via client and mac
1444-
ext_from_hex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff 5200000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000", &mut test);
1444+
ext_from_hex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff 5300000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000", &mut test);
14451445

14461446
// inbound read from peer id 0 of len 18
14471447
ext_from_hex("030012", &mut test);

lightning/src/chain/chainmonitor.rs

Lines changed: 90 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ use bitcoin::hash_types::{BlockHash, Txid};
2828

2929
use crate::chain;
3030
use crate::chain::chaininterface::{BroadcasterInterface, FeeEstimator};
31+
#[cfg(peer_storage)]
32+
use crate::chain::channelmonitor::write_chanmon_internal;
3133
use crate::chain::channelmonitor::{
3234
Balance, ChannelMonitor, ChannelMonitorUpdate, MonitorEvent, TransactionOutputs,
3335
WithChannelMonitor,
@@ -36,8 +38,11 @@ use crate::chain::transaction::{OutPoint, TransactionData};
3638
use crate::chain::{ChannelMonitorUpdateStatus, Filter, WatchedOutput};
3739
use crate::events::{self, Event, EventHandler, ReplayEvent};
3840
use crate::ln::channel_state::ChannelDetails;
39-
use crate::ln::msgs::{self, BaseMessageHandler, Init, MessageSendEvent, SendOnlyMessageHandler};
40-
use crate::ln::our_peer_storage::DecryptedOurPeerStorage;
41+
#[cfg(peer_storage)]
42+
use crate::ln::msgs::PeerStorage;
43+
use crate::ln::msgs::{BaseMessageHandler, Init, MessageSendEvent, SendOnlyMessageHandler};
44+
#[cfg(peer_storage)]
45+
use crate::ln::our_peer_storage::{DecryptedOurPeerStorage, PeerStorageMonitorHolder};
4146
use crate::ln::types::ChannelId;
4247
use crate::prelude::*;
4348
use crate::sign::ecdsa::EcdsaChannelSigner;
@@ -47,8 +52,12 @@ use crate::types::features::{InitFeatures, NodeFeatures};
4752
use crate::util::errors::APIError;
4853
use crate::util::logger::{Logger, WithContext};
4954
use crate::util::persist::MonitorName;
55+
#[cfg(peer_storage)]
56+
use crate::util::ser::{VecWriter, Writeable};
5057
use crate::util::wakers::{Future, Notifier};
5158
use bitcoin::secp256k1::PublicKey;
59+
#[cfg(peer_storage)]
60+
use core::iter::Cycle;
5261
use core::ops::Deref;
5362
use core::sync::atomic::{AtomicUsize, Ordering};
5463

@@ -264,7 +273,7 @@ pub struct ChainMonitor<
264273
logger: L,
265274
fee_estimator: F,
266275
persister: P,
267-
entropy_source: ES,
276+
_entropy_source: ES,
268277
/// "User-provided" (ie persistence-completion/-failed) [`MonitorEvent`]s. These came directly
269278
/// from the user and not from a [`ChannelMonitor`].
270279
pending_monitor_events: Mutex<Vec<(OutPoint, ChannelId, Vec<MonitorEvent>, PublicKey)>>,
@@ -278,6 +287,7 @@ pub struct ChainMonitor<
278287
/// Messages to send to the peer. This is currently used to distribute PeerStorage to channel partners.
279288
pending_send_only_events: Mutex<Vec<MessageSendEvent>>,
280289

290+
#[cfg(peer_storage)]
281291
our_peerstorage_encryption_key: PeerStorageKey,
282292
}
283293

@@ -477,7 +487,7 @@ where
477487
/// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
478488
pub fn new(
479489
chain_source: Option<C>, broadcaster: T, logger: L, feeest: F, persister: P,
480-
entropy_source: ES, our_peerstorage_encryption_key: PeerStorageKey,
490+
_entropy_source: ES, _our_peerstorage_encryption_key: PeerStorageKey,
481491
) -> Self {
482492
Self {
483493
monitors: RwLock::new(new_hash_map()),
@@ -486,12 +496,13 @@ where
486496
logger,
487497
fee_estimator: feeest,
488498
persister,
489-
entropy_source,
499+
_entropy_source,
490500
pending_monitor_events: Mutex::new(Vec::new()),
491501
highest_chain_height: AtomicUsize::new(0),
492502
event_notifier: Notifier::new(),
493503
pending_send_only_events: Mutex::new(Vec::new()),
494-
our_peerstorage_encryption_key,
504+
#[cfg(peer_storage)]
505+
our_peerstorage_encryption_key: _our_peerstorage_encryption_key,
495506
}
496507
}
497508

@@ -804,23 +815,90 @@ where
804815

805816
/// This function collects the counterparty node IDs from all monitors into a `HashSet`,
806817
/// ensuring unique IDs are returned.
818+
#[cfg(peer_storage)]
807819
fn all_counterparty_node_ids(&self) -> HashSet<PublicKey> {
808820
let mon = self.monitors.read().unwrap();
809821
mon.values().map(|monitor| monitor.monitor.get_counterparty_node_id()).collect()
810822
}
811823

824+
#[cfg(peer_storage)]
812825
fn send_peer_storage(&self, their_node_id: PublicKey) {
813-
// TODO: Serialize `ChannelMonitor`s inside `our_peer_storage`.
826+
let mut monitors_list: Vec<PeerStorageMonitorHolder> = Vec::new();
827+
let random_bytes = self._entropy_source.get_secure_random_bytes();
828+
829+
const MAX_PEER_STORAGE_SIZE: usize = 65531;
830+
const USIZE_LEN: usize = core::mem::size_of::<usize>();
831+
let mut random_bytes_cycle_iter = random_bytes.iter().cycle();
832+
833+
let mut current_size = 0;
834+
let monitors_lock = self.monitors.read().unwrap();
835+
let mut channel_ids = monitors_lock.keys().copied().collect();
836+
837+
fn next_random_id(
838+
channel_ids: &mut Vec<ChannelId>,
839+
random_bytes_cycle_iter: &mut Cycle<core::slice::Iter<u8>>,
840+
) -> Option<ChannelId> {
841+
if channel_ids.is_empty() {
842+
return None;
843+
}
844+
let random_idx = {
845+
let mut usize_bytes = [0u8; USIZE_LEN];
846+
usize_bytes.iter_mut().for_each(|b| {
847+
*b = *random_bytes_cycle_iter.next().expect("A cycle never ends")
848+
});
849+
// Take one more to introduce a slight misalignment.
850+
random_bytes_cycle_iter.next().expect("A cycle never ends");
851+
usize::from_le_bytes(usize_bytes) % channel_ids.len()
852+
};
853+
Some(channel_ids.swap_remove(random_idx))
854+
}
855+
856+
while let Some(channel_id) = next_random_id(&mut channel_ids, &mut random_bytes_cycle_iter)
857+
{
858+
let monitor_holder = if let Some(monitor_holder) = monitors_lock.get(&channel_id) {
859+
monitor_holder
860+
} else {
861+
debug_assert!(
862+
false,
863+
"Tried to access non-existing monitor, this should never happen"
864+
);
865+
break;
866+
};
867+
868+
let mut serialized_channel = VecWriter(Vec::new());
869+
let min_seen_secret = monitor_holder.monitor.get_min_seen_secret();
870+
let counterparty_node_id = monitor_holder.monitor.get_counterparty_node_id();
871+
{
872+
let inner_lock = monitor_holder.monitor.inner.lock().unwrap();
873+
874+
write_chanmon_internal(&inner_lock, true, &mut serialized_channel)
875+
.expect("can not write Channel Monitor for peer storage message");
876+
}
877+
let peer_storage_monitor = PeerStorageMonitorHolder {
878+
channel_id,
879+
min_seen_secret,
880+
counterparty_node_id,
881+
monitor_bytes: serialized_channel.0,
882+
};
883+
884+
let serialized_length = peer_storage_monitor.serialized_length();
885+
886+
if current_size + serialized_length > MAX_PEER_STORAGE_SIZE {
887+
continue;
888+
} else {
889+
current_size += serialized_length;
890+
monitors_list.push(peer_storage_monitor);
891+
}
892+
}
814893

815-
let random_bytes = self.entropy_source.get_secure_random_bytes();
816-
let serialised_channels = Vec::new();
894+
let serialised_channels = monitors_list.encode();
817895
let our_peer_storage = DecryptedOurPeerStorage::new(serialised_channels);
818896
let cipher = our_peer_storage.encrypt(&self.our_peerstorage_encryption_key, &random_bytes);
819897

820898
log_debug!(self.logger, "Sending Peer Storage to {}", log_pubkey!(their_node_id));
821899
let send_peer_storage_event = MessageSendEvent::SendPeerStorage {
822900
node_id: their_node_id,
823-
msg: msgs::PeerStorage { data: cipher.into_vec() },
901+
msg: PeerStorage { data: cipher.into_vec() },
824902
};
825903

826904
self.pending_send_only_events.lock().unwrap().push(send_peer_storage_event)
@@ -920,6 +998,7 @@ where
920998
)
921999
});
9221000

1001+
#[cfg(peer_storage)]
9231002
// Send peer storage everytime a new block arrives.
9241003
for node_id in self.all_counterparty_node_ids() {
9251004
self.send_peer_storage(node_id);
@@ -1021,6 +1100,7 @@ where
10211100
)
10221101
});
10231102

1103+
#[cfg(peer_storage)]
10241104
// Send peer storage everytime a new block arrives.
10251105
for node_id in self.all_counterparty_node_ids() {
10261106
self.send_peer_storage(node_id);

0 commit comments

Comments
 (0)