@@ -12403,11 +12403,23 @@ where
12403
12403
let best_block_height: u32 = Readable::read(reader)?;
12404
12404
let best_block_hash: BlockHash = Readable::read(reader)?;
12405
12405
12406
- let mut failed_htlcs = Vec::new();
12406
+ let empty_peer_state = || {
12407
+ PeerState {
12408
+ channel_by_id: new_hash_map(),
12409
+ inbound_channel_request_by_id: new_hash_map(),
12410
+ latest_features: InitFeatures::empty(),
12411
+ pending_msg_events: Vec::new(),
12412
+ in_flight_monitor_updates: BTreeMap::new(),
12413
+ monitor_update_blocked_actions: BTreeMap::new(),
12414
+ actions_blocking_raa_monitor_updates: BTreeMap::new(),
12415
+ is_connected: false,
12416
+ }
12417
+ };
12407
12418
12419
+ let mut failed_htlcs = Vec::new();
12408
12420
let channel_count: u64 = Readable::read(reader)?;
12409
12421
let mut funding_txo_set = hash_set_with_capacity(cmp::min(channel_count as usize, 128));
12410
- let mut funded_peer_channels: HashMap<PublicKey, HashMap<ChannelId, ChannelPhase<SP>>> = hash_map_with_capacity(cmp::min(channel_count as usize, 128 ));
12422
+ let mut per_peer_state = hash_map_with_capacity(cmp::min(channel_count as usize, MAX_ALLOC_SIZE/mem::size_of::<(PublicKey, Mutex<PeerState<SP>>)>() ));
12411
12423
let mut outpoint_to_peer = hash_map_with_capacity(cmp::min(channel_count as usize, 128));
12412
12424
let mut short_to_chan_info = hash_map_with_capacity(cmp::min(channel_count as usize, 128));
12413
12425
let mut channel_closures = VecDeque::new();
@@ -12495,17 +12507,10 @@ where
12495
12507
if let Some(funding_txo) = channel.context.get_funding_txo() {
12496
12508
outpoint_to_peer.insert(funding_txo, channel.context.get_counterparty_node_id());
12497
12509
}
12498
- match funded_peer_channels.entry(channel.context.get_counterparty_node_id()) {
12499
- hash_map::Entry::Occupied(mut entry) => {
12500
- let by_id_map = entry.get_mut();
12501
- by_id_map.insert(channel.context.channel_id(), ChannelPhase::Funded(channel));
12502
- },
12503
- hash_map::Entry::Vacant(entry) => {
12504
- let mut by_id_map = new_hash_map();
12505
- by_id_map.insert(channel.context.channel_id(), ChannelPhase::Funded(channel));
12506
- entry.insert(by_id_map);
12507
- }
12508
- }
12510
+ per_peer_state.entry(channel.context.get_counterparty_node_id())
12511
+ .or_insert_with(|| Mutex::new(empty_peer_state()))
12512
+ .get_mut().unwrap()
12513
+ .channel_by_id.insert(channel.context.channel_id(), ChannelPhase::Funded(channel));
12509
12514
}
12510
12515
} else if channel.is_awaiting_initial_mon_persist() {
12511
12516
// If we were persisted and shut down while the initial ChannelMonitor persistence
@@ -12572,27 +12577,13 @@ where
12572
12577
claimable_htlcs_list.push((payment_hash, previous_hops));
12573
12578
}
12574
12579
12575
- let peer_state_from_chans = |channel_by_id| {
12576
- PeerState {
12577
- channel_by_id,
12578
- inbound_channel_request_by_id: new_hash_map(),
12579
- latest_features: InitFeatures::empty(),
12580
- pending_msg_events: Vec::new(),
12581
- in_flight_monitor_updates: BTreeMap::new(),
12582
- monitor_update_blocked_actions: BTreeMap::new(),
12583
- actions_blocking_raa_monitor_updates: BTreeMap::new(),
12584
- is_connected: false,
12585
- }
12586
- };
12587
-
12588
12580
let peer_count: u64 = Readable::read(reader)?;
12589
- let mut per_peer_state = hash_map_with_capacity(cmp::min(peer_count as usize, MAX_ALLOC_SIZE/mem::size_of::<(PublicKey, Mutex<PeerState<SP>>)>()));
12590
12581
for _ in 0..peer_count {
12591
- let peer_pubkey = Readable::read(reader)?;
12592
- let peer_chans = funded_peer_channels.remove(&peer_pubkey).unwrap_or(new_hash_map()) ;
12593
- let mut peer_state = peer_state_from_chans(peer_chans);
12594
- peer_state.latest_features = Readable::read(reader)? ;
12595
- per_peer_state.insert(peer_pubkey, Mutex::new(peer_state));
12582
+ let peer_pubkey: PublicKey = Readable::read(reader)?;
12583
+ let latest_features = Readable::read(reader)? ;
12584
+ if let Some( peer_state) = per_peer_state.get_mut(&peer_pubkey) {
12585
+ peer_state.get_mut().unwrap(). latest_features = latest_features ;
12586
+ }
12596
12587
}
12597
12588
12598
12589
let event_count: u64 = Readable::read(reader)?;
@@ -12804,7 +12795,7 @@ where
12804
12795
// still open, we need to replay any monitor updates that are for closed channels,
12805
12796
// creating the neccessary peer_state entries as we go.
12806
12797
let peer_state_mutex = per_peer_state.entry(counterparty_id).or_insert_with(|| {
12807
- Mutex::new(peer_state_from_chans(new_hash_map() ))
12798
+ Mutex::new(empty_peer_state( ))
12808
12799
});
12809
12800
let mut peer_state = peer_state_mutex.lock().unwrap();
12810
12801
handle_in_flight_updates!(counterparty_id, chan_in_flight_updates,
0 commit comments