@@ -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) {
@@ -12857,6 +12903,9 @@ where
12857
12903
if !peer_state.ok_to_remove(false) {
12858
12904
peer_pubkey.write(writer)?;
12859
12905
peer_state.latest_features.write(writer)?;
12906
+
12907
+ peer_state.peer_storage.write(writer)?;
12908
+
12860
12909
if !peer_state.monitor_update_blocked_actions.is_empty() {
12861
12910
monitor_update_blocked_actions_per_peer
12862
12911
.get_or_insert_with(Vec::new)
@@ -13210,6 +13259,7 @@ where
13210
13259
monitor_update_blocked_actions: BTreeMap::new(),
13211
13260
actions_blocking_raa_monitor_updates: BTreeMap::new(),
13212
13261
closed_channel_monitor_update_ids: BTreeMap::new(),
13262
+ peer_storage: Vec::new(),
13213
13263
is_connected: false,
13214
13264
}
13215
13265
};
@@ -13433,8 +13483,10 @@ where
13433
13483
for _ in 0..peer_count {
13434
13484
let peer_pubkey: PublicKey = Readable::read(reader)?;
13435
13485
let latest_features = Readable::read(reader)?;
13486
+ let peer_storage = Readable::read(reader)?;
13436
13487
if let Some(peer_state) = per_peer_state.get_mut(&peer_pubkey) {
13437
13488
peer_state.get_mut().unwrap().latest_features = latest_features;
13489
+ peer_state.get_mut().unwrap().peer_storage = peer_storage;
13438
13490
}
13439
13491
}
13440
13492
0 commit comments