@@ -1380,6 +1380,8 @@ pub(super) struct PeerState<SP: Deref> where SP::Target: SignerProvider {
1380
1380
/// [`ChannelMessageHandler::peer_connected`] and no corresponding
1381
1381
/// [`ChannelMessageHandler::peer_disconnected`].
1382
1382
pub is_connected: bool,
1383
+ /// Holds the peer storage data for the channel partner on a per-peer basis.
1384
+ peer_storage: Vec<u8>,
1383
1385
}
1384
1386
1385
1387
impl <SP: Deref> PeerState<SP> where SP::Target: SignerProvider {
@@ -8170,7 +8172,41 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
8170
8172
}
8171
8173
}
8172
8174
8173
- fn internal_peer_storage(&self, _counterparty_node_id: &PublicKey, _msg: &msgs::PeerStorageMessage) {}
8175
+ fn internal_peer_storage(&self, counterparty_node_id: &PublicKey, msg: &msgs::PeerStorageMessage) {
8176
+ let per_peer_state = self.per_peer_state.read().unwrap();
8177
+ let peer_state_mutex = match per_peer_state.get(counterparty_node_id) {
8178
+ Some(peer_state_mutex) => peer_state_mutex,
8179
+ None => return,
8180
+ };
8181
+ let mut peer_state_lock = peer_state_mutex.lock().unwrap();
8182
+ let peer_state = &mut *peer_state_lock;
8183
+ let logger = WithContext::from(&self.logger, Some(*counterparty_node_id), None, None);
8184
+
8185
+ // Check if we have any channels with the peer (Currently we only provide the servie to peers we have a channel with).
8186
+ if !peer_state.channel_by_id.values().any(|phase| phase.is_funded()) {
8187
+ log_debug!(logger, "Ignoring peer storage request from {} as we don't have any funded channels with them.", log_pubkey!(counterparty_node_id));
8188
+ return;
8189
+ }
8190
+
8191
+ #[cfg(not(test))]
8192
+ if msg.data.len() > 1024 {
8193
+ log_debug!(logger, "Sending warning to peer and ignoring peer storage request from {} as its over 1KiB", log_pubkey!(counterparty_node_id));
8194
+ peer_state.pending_msg_events.push(events::MessageSendEvent::HandleError {
8195
+ node_id: counterparty_node_id.clone(),
8196
+ action: msgs::ErrorAction::SendWarningMessage {
8197
+ msg: msgs::WarningMessage {
8198
+ channel_id: ChannelId([0; 32]),
8199
+ data: "Supports only data up to 1 KiB in peer storage.".to_owned()
8200
+ },
8201
+ log_level: Level::Trace,
8202
+ }
8203
+ });
8204
+ return;
8205
+ }
8206
+
8207
+ log_trace!(logger, "Received Peer Storage from {}", log_pubkey!(counterparty_node_id));
8208
+ peer_state.peer_storage = msg.data.clone();
8209
+ }
8174
8210
8175
8211
fn internal_funding_signed(&self, counterparty_node_id: &PublicKey, msg: &msgs::FundingSigned) -> Result<(), MsgHandleErrInternal> {
8176
8212
let best_block = *self.best_block.read().unwrap();
@@ -11662,6 +11698,7 @@ where
11662
11698
actions_blocking_raa_monitor_updates: BTreeMap::new(),
11663
11699
closed_channel_monitor_update_ids: BTreeMap::new(),
11664
11700
is_connected: true,
11701
+ peer_storage: Vec::new(),
11665
11702
}));
11666
11703
},
11667
11704
hash_map::Entry::Occupied(e) => {
@@ -11691,6 +11728,15 @@ where
11691
11728
let peer_state = &mut *peer_state_lock;
11692
11729
let pending_msg_events = &mut peer_state.pending_msg_events;
11693
11730
11731
+ if !peer_state.peer_storage.is_empty() {
11732
+ pending_msg_events.push(events::MessageSendEvent::SendPeerStorageRetrievalMessage {
11733
+ node_id: counterparty_node_id.clone(),
11734
+ msg: msgs::PeerStorageRetrievalMessage {
11735
+ data: peer_state.peer_storage.clone()
11736
+ },
11737
+ });
11738
+ }
11739
+
11694
11740
for (_, chan) in peer_state.channel_by_id.iter_mut() {
11695
11741
let logger = WithChannelContext::from(&self.logger, &chan.context(), None);
11696
11742
match chan.peer_connected_get_handshake(self.chain_hash, &&logger) {
@@ -12848,6 +12894,8 @@ where
12848
12894
peer_states.push(peer_state_mutex.unsafe_well_ordered_double_lock_self());
12849
12895
}
12850
12896
12897
+ let mut peer_storage_dir: Vec<(&PublicKey, &Vec<u8>)> = Vec::new();
12898
+
12851
12899
(serializable_peer_count).write(writer)?;
12852
12900
for ((peer_pubkey, _), peer_state) in per_peer_state.iter().zip(peer_states.iter()) {
12853
12901
// Peers which we have no channels to should be dropped once disconnected. As we
@@ -12857,6 +12905,8 @@ where
12857
12905
if !peer_state.ok_to_remove(false) {
12858
12906
peer_pubkey.write(writer)?;
12859
12907
peer_state.latest_features.write(writer)?;
12908
+ peer_storage_dir.push((peer_pubkey, &peer_state.peer_storage));
12909
+
12860
12910
if !peer_state.monitor_update_blocked_actions.is_empty() {
12861
12911
monitor_update_blocked_actions_per_peer
12862
12912
.get_or_insert_with(Vec::new)
@@ -12978,6 +13028,7 @@ where
12978
13028
(14, decode_update_add_htlcs_opt, option),
12979
13029
(15, self.inbound_payment_id_secret, required),
12980
13030
(17, in_flight_monitor_updates, required),
13031
+ (19, peer_storage_dir, optional_vec),
12981
13032
});
12982
13033
12983
13034
Ok(())
@@ -13210,6 +13261,7 @@ where
13210
13261
monitor_update_blocked_actions: BTreeMap::new(),
13211
13262
actions_blocking_raa_monitor_updates: BTreeMap::new(),
13212
13263
closed_channel_monitor_update_ids: BTreeMap::new(),
13264
+ peer_storage: Vec::new(),
13213
13265
is_connected: false,
13214
13266
}
13215
13267
};
@@ -13505,6 +13557,7 @@ where
13505
13557
let mut in_flight_monitor_updates: Option<HashMap<(PublicKey, ChannelId), Vec<ChannelMonitorUpdate>>> = None;
13506
13558
let mut decode_update_add_htlcs: Option<HashMap<u64, Vec<msgs::UpdateAddHTLC>>> = None;
13507
13559
let mut inbound_payment_id_secret = None;
13560
+ let mut peer_storage_dir: Option<Vec<(PublicKey, Vec<u8>)>> = None;
13508
13561
read_tlv_fields!(reader, {
13509
13562
(1, pending_outbound_payments_no_retry, option),
13510
13563
(2, pending_intercepted_htlcs, option),
@@ -13521,8 +13574,10 @@ where
13521
13574
(14, decode_update_add_htlcs, option),
13522
13575
(15, inbound_payment_id_secret, option),
13523
13576
(17, in_flight_monitor_updates, required),
13577
+ (19, peer_storage_dir, optional_vec),
13524
13578
});
13525
13579
let mut decode_update_add_htlcs = decode_update_add_htlcs.unwrap_or_else(|| new_hash_map());
13580
+ let peer_storage_dir: Vec<(PublicKey, Vec<u8>)> = peer_storage_dir.unwrap_or_else(Vec::new);
13526
13581
if fake_scid_rand_bytes.is_none() {
13527
13582
fake_scid_rand_bytes = Some(args.entropy_source.get_secure_random_bytes());
13528
13583
}
@@ -13554,6 +13609,12 @@ where
13554
13609
}
13555
13610
let pending_outbounds = OutboundPayments::new(pending_outbound_payments.unwrap());
13556
13611
13612
+ for (peer_pubkey, peer_storage) in peer_storage_dir {
13613
+ if let Some(peer_state) = per_peer_state.get_mut(&peer_pubkey) {
13614
+ peer_state.get_mut().unwrap().peer_storage = peer_storage;
13615
+ }
13616
+ }
13617
+
13557
13618
// Handle transitioning from the legacy TLV to the new one on upgrades.
13558
13619
if let Some(legacy_in_flight_upds) = legacy_in_flight_monitor_updates {
13559
13620
// We should never serialize an empty map.
0 commit comments