Skip to content

Commit ee684f2

Browse files
Aditya SharmaAditya Sharma
Aditya Sharma
authored and
Aditya Sharma
committed
Handle PeerStorage Message and its Persistence
This commit introduces the handling and persistence of PeerStorage messages on a per-peer basis. The peer storage is stored within the PeerState to simplify management, ensuring we do not need to remove it when there are no active channels with the peer. Key changes include: - Add PeerStorage to PeerState for persistent storage. - Implement internal_peer_storage to manage PeerStorage and its updates. - Add resend logic in peer_connected() to resend PeerStorage before sending the channel reestablish message upon reconnection. - Update PeerState's write() and read() methods to support PeerStorage persistence.
1 parent ba66847 commit ee684f2

File tree

1 file changed

+62
-1
lines changed

1 file changed

+62
-1
lines changed

lightning/src/ln/channelmanager.rs

+62-1
Original file line numberDiff line numberDiff line change
@@ -1380,6 +1380,8 @@ pub(super) struct PeerState<SP: Deref> where SP::Target: SignerProvider {
13801380
/// [`ChannelMessageHandler::peer_connected`] and no corresponding
13811381
/// [`ChannelMessageHandler::peer_disconnected`].
13821382
pub is_connected: bool,
1383+
/// Holds the peer storage data for the channel partner on a per-peer basis.
1384+
peer_storage: Vec<u8>,
13831385
}
13841386

13851387
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/
81708172
}
81718173
}
81728174

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+
}
81748210

81758211
fn internal_funding_signed(&self, counterparty_node_id: &PublicKey, msg: &msgs::FundingSigned) -> Result<(), MsgHandleErrInternal> {
81768212
let best_block = *self.best_block.read().unwrap();
@@ -11662,6 +11698,7 @@ where
1166211698
actions_blocking_raa_monitor_updates: BTreeMap::new(),
1166311699
closed_channel_monitor_update_ids: BTreeMap::new(),
1166411700
is_connected: true,
11701+
peer_storage: Vec::new(),
1166511702
}));
1166611703
},
1166711704
hash_map::Entry::Occupied(e) => {
@@ -11691,6 +11728,15 @@ where
1169111728
let peer_state = &mut *peer_state_lock;
1169211729
let pending_msg_events = &mut peer_state.pending_msg_events;
1169311730

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+
1169411740
for (_, chan) in peer_state.channel_by_id.iter_mut() {
1169511741
let logger = WithChannelContext::from(&self.logger, &chan.context(), None);
1169611742
match chan.peer_connected_get_handshake(self.chain_hash, &&logger) {
@@ -12848,6 +12894,8 @@ where
1284812894
peer_states.push(peer_state_mutex.unsafe_well_ordered_double_lock_self());
1284912895
}
1285012896

12897+
let mut peer_storage_dir: Vec<(&PublicKey, &Vec<u8>)> = Vec::new();
12898+
1285112899
(serializable_peer_count).write(writer)?;
1285212900
for ((peer_pubkey, _), peer_state) in per_peer_state.iter().zip(peer_states.iter()) {
1285312901
// Peers which we have no channels to should be dropped once disconnected. As we
@@ -12857,6 +12905,8 @@ where
1285712905
if !peer_state.ok_to_remove(false) {
1285812906
peer_pubkey.write(writer)?;
1285912907
peer_state.latest_features.write(writer)?;
12908+
peer_storage_dir.push((peer_pubkey, &peer_state.peer_storage));
12909+
1286012910
if !peer_state.monitor_update_blocked_actions.is_empty() {
1286112911
monitor_update_blocked_actions_per_peer
1286212912
.get_or_insert_with(Vec::new)
@@ -12978,6 +13028,7 @@ where
1297813028
(14, decode_update_add_htlcs_opt, option),
1297913029
(15, self.inbound_payment_id_secret, required),
1298013030
(17, in_flight_monitor_updates, required),
13031+
(19, peer_storage_dir, optional_vec),
1298113032
});
1298213033

1298313034
Ok(())
@@ -13210,6 +13261,7 @@ where
1321013261
monitor_update_blocked_actions: BTreeMap::new(),
1321113262
actions_blocking_raa_monitor_updates: BTreeMap::new(),
1321213263
closed_channel_monitor_update_ids: BTreeMap::new(),
13264+
peer_storage: Vec::new(),
1321313265
is_connected: false,
1321413266
}
1321513267
};
@@ -13505,6 +13557,7 @@ where
1350513557
let mut in_flight_monitor_updates: Option<HashMap<(PublicKey, ChannelId), Vec<ChannelMonitorUpdate>>> = None;
1350613558
let mut decode_update_add_htlcs: Option<HashMap<u64, Vec<msgs::UpdateAddHTLC>>> = None;
1350713559
let mut inbound_payment_id_secret = None;
13560+
let mut peer_storage_dir: Option<Vec<(PublicKey, Vec<u8>)>> = None;
1350813561
read_tlv_fields!(reader, {
1350913562
(1, pending_outbound_payments_no_retry, option),
1351013563
(2, pending_intercepted_htlcs, option),
@@ -13521,8 +13574,10 @@ where
1352113574
(14, decode_update_add_htlcs, option),
1352213575
(15, inbound_payment_id_secret, option),
1352313576
(17, in_flight_monitor_updates, required),
13577+
(19, peer_storage_dir, optional_vec),
1352413578
});
1352513579
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);
1352613581
if fake_scid_rand_bytes.is_none() {
1352713582
fake_scid_rand_bytes = Some(args.entropy_source.get_secure_random_bytes());
1352813583
}
@@ -13554,6 +13609,12 @@ where
1355413609
}
1355513610
let pending_outbounds = OutboundPayments::new(pending_outbound_payments.unwrap());
1355613611

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+
1355713618
// Handle transitioning from the legacy TLV to the new one on upgrades.
1355813619
if let Some(legacy_in_flight_upds) = legacy_in_flight_monitor_updates {
1355913620
// We should never serialize an empty map.

0 commit comments

Comments
 (0)