Skip to content

Commit 3d2a345

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 addbb78 commit 3d2a345

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

lightning/src/chain/chainmonitor.rs

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

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

490489
#[cfg(c_bindings)]

lightning/src/lib.rs

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

200203
pub(crate) fn new_hash_set<K: core::hash::Hash + Eq>() -> HashSet<K> { HashSet::new() }
201204
pub(crate) fn hash_set_with_capacity<K: core::hash::Hash + Eq>(cap: usize) -> HashSet<K> {
202205
HashSet::with_capacity(cap)
203206
}
207+
pub(crate) fn hash_set_from_iter<K: core::hash::Hash + Eq, I: IntoIterator<Item = K>>(iter: I) -> HashSet<K> {
208+
HashSet::from_iter(iter)
209+
}
204210

205211
pub use alloc::borrow::ToOwned;
206212
pub use alloc::string::ToString;

lightning/src/ln/channelmanager.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10237,7 +10237,9 @@ where
1023710237
mut channel_monitors: Vec<&'a mut ChannelMonitor<<SP::Target as SignerProvider>::EcdsaSigner>>) -> Self {
1023810238
Self {
1023910239
entropy_source, node_signer, signer_provider, fee_estimator, chain_monitor, tx_broadcaster, router, logger, default_config,
10240-
channel_monitors: channel_monitors.drain(..).map(|monitor| { (monitor.get_funding_txo().0, monitor) }).collect()
10240+
channel_monitors: hash_map_from_iter(
10241+
channel_monitors.drain(..).map(|monitor| { (monitor.get_funding_txo().0, monitor) })
10242+
),
1024110243
}
1024210244
}
1024310245
}
@@ -10727,7 +10729,7 @@ where
1072710729
retry_strategy: None,
1072810730
attempts: PaymentAttempts::new(),
1072910731
payment_params: None,
10730-
session_privs: [session_priv_bytes].iter().map(|a| *a).collect(),
10732+
session_privs: hash_set_from_iter([session_priv_bytes]),
1073110733
payment_hash: htlc.payment_hash,
1073210734
payment_secret: None, // only used for retries, and we'll never retry on startup
1073310735
payment_metadata: None, // only used for retries, and we'll never retry on startup

0 commit comments

Comments
 (0)