Skip to content

Commit efbaa41

Browse files
committed
Stop relying on Hash{Set,Map}::from_iter directly
In the next commit we'll bump the `hashbrown` version, which no longer randomizes its hasher by default. Thus, we'll need to call a different constructor in no-std builds from std builds. Here we do a quick prefactor to use wrappers for `FromIterator` constructors instead of calling the tables directly to make the version bump changeset smaller.
1 parent 3e0d55b commit efbaa41

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

lightning/src/chain/chainmonitor.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ use crate::ln::channelmanager::ChannelDetails;
4343

4444
use crate::prelude::*;
4545
use crate::sync::{RwLock, RwLockReadGuard, Mutex, MutexGuard};
46-
use core::iter::FromIterator;
4746
use core::ops::Deref;
4847
use core::sync::atomic::{AtomicUsize, Ordering};
4948
use bitcoin::secp256k1::PublicKey;
@@ -318,7 +317,7 @@ where C::Target: chain::Filter,
318317
FN: Fn(&ChannelMonitor<ChannelSigner>, &TransactionData) -> Vec<TransactionOutputs>
319318
{
320319
let err_str = "ChannelMonitor[Update] persistence failed unrecoverably. This indicates we cannot continue normal operation and must shut down.";
321-
let funding_outpoints: HashSet<OutPoint> = HashSet::from_iter(self.monitors.read().unwrap().keys().cloned());
320+
let funding_outpoints = hash_set_from_iter(self.monitors.read().unwrap().keys().cloned());
322321
for funding_outpoint in funding_outpoints.iter() {
323322
let monitor_lock = self.monitors.read().unwrap();
324323
if let Some(monitor_state) = monitor_lock.get(funding_outpoint) {
@@ -486,9 +485,9 @@ where C::Target: chain::Filter,
486485
#[cfg(not(c_bindings))]
487486
/// Lists the pending updates for each [`ChannelMonitor`] (by `OutPoint` being monitored).
488487
pub fn list_pending_monitor_updates(&self) -> HashMap<OutPoint, Vec<MonitorUpdateId>> {
489-
self.monitors.read().unwrap().iter().map(|(outpoint, holder)| {
488+
hash_map_from_iter(self.monitors.read().unwrap().iter().map(|(outpoint, holder)| {
490489
(*outpoint, holder.pending_monitor_updates.lock().unwrap().clone())
491-
}).collect()
490+
}))
492491
}
493492

494493
#[cfg(c_bindings)]

lightning/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,17 @@ mod prelude {
198198
pub(crate) fn hash_map_with_capacity<K: core::hash::Hash + Eq, V>(cap: usize) -> HashMap<K, V> {
199199
HashMap::with_capacity(cap)
200200
}
201+
pub(crate) fn hash_map_from_iter<K: core::hash::Hash + Eq, V, I: IntoIterator<Item = (K, V)>>(iter: I) -> HashMap<K, V> {
202+
HashMap::from_iter(iter)
203+
}
201204

202205
pub(crate) fn new_hash_set<K: core::hash::Hash + Eq>() -> HashSet<K> { HashSet::new() }
203206
pub(crate) fn hash_set_with_capacity<K: core::hash::Hash + Eq>(cap: usize) -> HashSet<K> {
204207
HashSet::with_capacity(cap)
205208
}
209+
pub(crate) fn hash_set_from_iter<K: core::hash::Hash + Eq, I: IntoIterator<Item = K>>(iter: I) -> HashSet<K> {
210+
HashSet::from_iter(iter)
211+
}
206212

207213
pub use alloc::borrow::ToOwned;
208214
pub use alloc::string::ToString;

lightning/src/ln/channelmanager.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10284,7 +10284,9 @@ where
1028410284
mut channel_monitors: Vec<&'a mut ChannelMonitor<<SP::Target as SignerProvider>::EcdsaSigner>>) -> Self {
1028510285
Self {
1028610286
entropy_source, node_signer, signer_provider, fee_estimator, chain_monitor, tx_broadcaster, router, logger, default_config,
10287-
channel_monitors: channel_monitors.drain(..).map(|monitor| { (monitor.get_funding_txo().0, monitor) }).collect()
10287+
channel_monitors: hash_map_from_iter(
10288+
channel_monitors.drain(..).map(|monitor| { (monitor.get_funding_txo().0, monitor) })
10289+
),
1028810290
}
1028910291
}
1029010292
}
@@ -10557,7 +10559,7 @@ where
1055710559
for _ in 0..pending_outbound_payments_count_compat {
1055810560
let session_priv = Readable::read(reader)?;
1055910561
let payment = PendingOutboundPayment::Legacy {
10560-
session_privs: [session_priv].iter().cloned().collect()
10562+
session_privs: hash_set_from_iter([session_priv]),
1056110563
};
1056210564
if pending_outbound_payments_compat.insert(PaymentId(session_priv), payment).is_some() {
1056310565
return Err(DecodeError::InvalidValue)
@@ -10780,7 +10782,7 @@ where
1078010782
retry_strategy: None,
1078110783
attempts: PaymentAttempts::new(),
1078210784
payment_params: None,
10783-
session_privs: [session_priv_bytes].iter().map(|a| *a).collect(),
10785+
session_privs: hash_set_from_iter([session_priv_bytes]),
1078410786
payment_hash: htlc.payment_hash,
1078510787
payment_secret: None, // only used for retries, and we'll never retry on startup
1078610788
payment_metadata: None, // only used for retries, and we'll never retry on startup

0 commit comments

Comments
 (0)