Skip to content

Commit 006a5d0

Browse files
committed
fixup: Serialise ChannelMonitors and send them over inside Peer Storage
1 parent 4c9f3c3 commit 006a5d0

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

lightning/src/chain/chainmonitor.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -813,25 +813,42 @@ where
813813
}
814814

815815
fn send_peer_storage(&self, their_node_id: PublicKey) {
816+
static MAX_PEER_STORAGE_SIZE: usize = 65000;
816817
let random_bytes = self.entropy_source.get_secure_random_bytes();
818+
let random_usize = usize::from_le_bytes(random_bytes[0..8].try_into().unwrap());
817819

818-
// TODO(aditya): Choose n random channels so that peer storage does not exceed 64k.
819820
let monitors = self.monitors.read().unwrap();
820821
let mut monitors_list = PeerStorageMonitorHolderList { monitors: Vec::new() };
822+
let mut curr_size = 0;
821823

822-
for (chan_id, mon) in monitors.iter() {
824+
// Randomising Keys in the HashMap to fetch monitors without repetition.
825+
let mut keys: Vec<&ChannelId> = monitors.keys().collect();
826+
for i in (1..keys.len()).rev() {
827+
let j = random_usize % (i + 1);
828+
keys.swap(i, j);
829+
}
830+
831+
for chan_id in keys {
832+
let mon = &monitors[chan_id];
823833
let mut ser_chan = VecWriter(Vec::new());
824834
let min_seen_secret = mon.monitor.get_min_seen_secret();
825835
let counterparty_node_id = mon.monitor.get_counterparty_node_id();
826836

827837
match write_util(&mon.monitor.inner.lock().unwrap(), true, &mut ser_chan) {
828838
Ok(_) => {
839+
let mut ser_channel = Vec::new();
829840
let peer_storage_monitor = PeerStorageMonitorHolder {
830841
channel_id: *chan_id,
831842
min_seen_secret,
832843
counterparty_node_id,
833844
monitor_bytes: ser_chan.0,
834845
};
846+
peer_storage_monitor.write(&mut ser_channel).unwrap();
847+
848+
curr_size += ser_channel.len();
849+
if curr_size > MAX_PEER_STORAGE_SIZE {
850+
break;
851+
}
835852

836853
monitors_list.monitors.push(peer_storage_monitor);
837854
},

0 commit comments

Comments
 (0)