Skip to content

Commit

Permalink
Use utility methods to construct HashMaps and HashSets
Browse files Browse the repository at this point in the history
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 constructors
instead of calling the tables directly to make the version bump
changeset smaller.
  • Loading branch information
TheBlueMatt committed Jan 2, 2024
1 parent b23f728 commit b35ab51
Show file tree
Hide file tree
Showing 22 changed files with 155 additions and 131 deletions.
2 changes: 1 addition & 1 deletion lightning/src/chain/chainmonitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ where C::Target: chain::Filter,
/// transactions relevant to the watched channels.
pub fn new(chain_source: Option<C>, broadcaster: T, logger: L, feeest: F, persister: P) -> Self {
Self {
monitors: RwLock::new(HashMap::new()),
monitors: RwLock::new(new_hash_map()),
sync_persistence_id: AtomicCounter::new(),
chain_source,
broadcaster,
Expand Down
30 changes: 15 additions & 15 deletions lightning/src/chain/channelmonitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1221,7 +1221,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
channel_parameters.clone(), initial_holder_commitment_tx, secp_ctx
);

let mut outputs_to_watch = HashMap::new();
let mut outputs_to_watch = new_hash_map();
outputs_to_watch.insert(funding_info.0.txid, vec![(funding_info.0.index as u32, funding_info.1.clone())]);

Self::from_impl(ChannelMonitorImpl {
Expand All @@ -1247,17 +1247,17 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
on_holder_tx_csv: counterparty_channel_parameters.selected_contest_delay,

commitment_secrets: CounterpartyCommitmentSecrets::new(),
counterparty_claimable_outpoints: HashMap::new(),
counterparty_commitment_txn_on_chain: HashMap::new(),
counterparty_hash_commitment_number: HashMap::new(),
counterparty_fulfilled_htlcs: HashMap::new(),
counterparty_claimable_outpoints: new_hash_map(),
counterparty_commitment_txn_on_chain: new_hash_map(),
counterparty_hash_commitment_number: new_hash_map(),
counterparty_fulfilled_htlcs: new_hash_map(),

prev_holder_signed_commitment_tx: None,
current_holder_commitment_tx: holder_commitment_tx,
current_counterparty_commitment_number: 1 << 48,
current_holder_commitment_number,

payment_preimages: HashMap::new(),
payment_preimages: new_hash_map(),
pending_monitor_events: Vec::new(),
pending_events: Vec::new(),
is_processing_pending_events: false,
Expand Down Expand Up @@ -2154,7 +2154,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
/// HTLCs which were resolved on-chain (i.e. where the final HTLC resolution was done by an
/// event from this `ChannelMonitor`).
pub(crate) fn get_all_current_outbound_htlcs(&self) -> HashMap<HTLCSource, (HTLCOutputInCommitment, Option<PaymentPreimage>)> {
let mut res = HashMap::new();
let mut res = new_hash_map();
// Just examine the available counterparty commitment transactions. See docs on
// `fail_unbroadcast_htlcs`, below, for justification.
let us = self.inner.lock().unwrap();
Expand Down Expand Up @@ -2206,7 +2206,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
return self.get_all_current_outbound_htlcs();
}

let mut res = HashMap::new();
let mut res = new_hash_map();
macro_rules! walk_htlcs {
($holder_commitment: expr, $htlc_iter: expr) => {
for (htlc, source) in $htlc_iter {
Expand Down Expand Up @@ -3916,7 +3916,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
/// Filters a block's `txdata` for transactions spending watched outputs or for any child
/// transactions thereof.
fn filter_block<'a>(&self, txdata: &TransactionData<'a>) -> Vec<&'a Transaction> {
let mut matched_txn = HashSet::new();
let mut matched_txn = new_hash_set();
txdata.iter().filter(|&&(_, tx)| {
let mut matches = self.spends_watched_output(tx);
for input in tx.input.iter() {
Expand Down Expand Up @@ -4431,7 +4431,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
}

let counterparty_claimable_outpoints_len: u64 = Readable::read(reader)?;
let mut counterparty_claimable_outpoints = HashMap::with_capacity(cmp::min(counterparty_claimable_outpoints_len as usize, MAX_ALLOC_SIZE / 64));
let mut counterparty_claimable_outpoints = hash_map_with_capacity(cmp::min(counterparty_claimable_outpoints_len as usize, MAX_ALLOC_SIZE / 64));
for _ in 0..counterparty_claimable_outpoints_len {
let txid: Txid = Readable::read(reader)?;
let htlcs_count: u64 = Readable::read(reader)?;
Expand All @@ -4445,7 +4445,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
}

let counterparty_commitment_txn_on_chain_len: u64 = Readable::read(reader)?;
let mut counterparty_commitment_txn_on_chain = HashMap::with_capacity(cmp::min(counterparty_commitment_txn_on_chain_len as usize, MAX_ALLOC_SIZE / 32));
let mut counterparty_commitment_txn_on_chain = hash_map_with_capacity(cmp::min(counterparty_commitment_txn_on_chain_len as usize, MAX_ALLOC_SIZE / 32));
for _ in 0..counterparty_commitment_txn_on_chain_len {
let txid: Txid = Readable::read(reader)?;
let commitment_number = <U48 as Readable>::read(reader)?.0;
Expand All @@ -4455,7 +4455,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
}

let counterparty_hash_commitment_number_len: u64 = Readable::read(reader)?;
let mut counterparty_hash_commitment_number = HashMap::with_capacity(cmp::min(counterparty_hash_commitment_number_len as usize, MAX_ALLOC_SIZE / 32));
let mut counterparty_hash_commitment_number = hash_map_with_capacity(cmp::min(counterparty_hash_commitment_number_len as usize, MAX_ALLOC_SIZE / 32));
for _ in 0..counterparty_hash_commitment_number_len {
let payment_hash: PaymentHash = Readable::read(reader)?;
let commitment_number = <U48 as Readable>::read(reader)?.0;
Expand All @@ -4478,7 +4478,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
let current_holder_commitment_number = <U48 as Readable>::read(reader)?.0;

let payment_preimages_len: u64 = Readable::read(reader)?;
let mut payment_preimages = HashMap::with_capacity(cmp::min(payment_preimages_len as usize, MAX_ALLOC_SIZE / 32));
let mut payment_preimages = hash_map_with_capacity(cmp::min(payment_preimages_len as usize, MAX_ALLOC_SIZE / 32));
for _ in 0..payment_preimages_len {
let preimage: PaymentPreimage = Readable::read(reader)?;
let hash = PaymentHash(Sha256::hash(&preimage.0[..]).to_byte_array());
Expand Down Expand Up @@ -4518,7 +4518,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
}

let outputs_to_watch_len: u64 = Readable::read(reader)?;
let mut outputs_to_watch = HashMap::with_capacity(cmp::min(outputs_to_watch_len as usize, MAX_ALLOC_SIZE / (mem::size_of::<Txid>() + mem::size_of::<u32>() + mem::size_of::<Vec<ScriptBuf>>())));
let mut outputs_to_watch = hash_map_with_capacity(cmp::min(outputs_to_watch_len as usize, MAX_ALLOC_SIZE / (mem::size_of::<Txid>() + mem::size_of::<u32>() + mem::size_of::<Vec<ScriptBuf>>())));
for _ in 0..outputs_to_watch_len {
let txid = Readable::read(reader)?;
let outputs_len: u64 = Readable::read(reader)?;
Expand Down Expand Up @@ -4560,7 +4560,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
let mut counterparty_node_id = None;
let mut confirmed_commitment_tx_counterparty_output = None;
let mut spendable_txids_confirmed = Some(Vec::new());
let mut counterparty_fulfilled_htlcs = Some(HashMap::new());
let mut counterparty_fulfilled_htlcs = Some(new_hash_map());
let mut initial_counterparty_commitment_info = None;
read_tlv_fields!(reader, {
(1, funding_spend_confirmed, option),
Expand Down
12 changes: 6 additions & 6 deletions lightning/src/chain/onchaintx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,13 +374,13 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
signer.provide_channel_parameters(&channel_parameters);

let pending_claim_requests_len: u64 = Readable::read(reader)?;
let mut pending_claim_requests = HashMap::with_capacity(cmp::min(pending_claim_requests_len as usize, MAX_ALLOC_SIZE / 128));
let mut pending_claim_requests = hash_map_with_capacity(cmp::min(pending_claim_requests_len as usize, MAX_ALLOC_SIZE / 128));
for _ in 0..pending_claim_requests_len {
pending_claim_requests.insert(Readable::read(reader)?, Readable::read(reader)?);
}

let claimable_outpoints_len: u64 = Readable::read(reader)?;
let mut claimable_outpoints = HashMap::with_capacity(cmp::min(pending_claim_requests_len as usize, MAX_ALLOC_SIZE / 128));
let mut claimable_outpoints = hash_map_with_capacity(cmp::min(pending_claim_requests_len as usize, MAX_ALLOC_SIZE / 128));
for _ in 0..claimable_outpoints_len {
let outpoint = Readable::read(reader)?;
let ancestor_claim_txid = Readable::read(reader)?;
Expand Down Expand Up @@ -445,8 +445,8 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
prev_holder_commitment: None,
signer,
channel_transaction_parameters: channel_parameters,
pending_claim_requests: HashMap::new(),
claimable_outpoints: HashMap::new(),
pending_claim_requests: new_hash_map(),
claimable_outpoints: new_hash_map(),
locktimed_packages: BTreeMap::new(),
onchain_events_awaiting_threshold_conf: Vec::new(),
pending_claim_events: Vec::new(),
Expand Down Expand Up @@ -834,7 +834,7 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
F::Target: FeeEstimator,
{
log_debug!(logger, "Updating claims view at height {} with {} matched transactions in block {}", cur_height, txn_matched.len(), conf_height);
let mut bump_candidates = HashMap::new();
let mut bump_candidates = new_hash_map();
for tx in txn_matched {
// Scan all input to verify is one of the outpoint spent is of interest for us
let mut claimed_outputs_material = Vec::new();
Expand Down Expand Up @@ -1020,7 +1020,7 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
where B::Target: BroadcasterInterface,
F::Target: FeeEstimator,
{
let mut bump_candidates = HashMap::new();
let mut bump_candidates = new_hash_map();
let onchain_events_awaiting_threshold_conf =
self.onchain_events_awaiting_threshold_conf.drain(..).collect::<Vec<_>>();
for entry in onchain_events_awaiting_threshold_conf {
Expand Down
2 changes: 1 addition & 1 deletion lightning/src/events/bump_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ where
/// Returns a new instance backed by the given [`WalletSource`] that serves as an implementation
/// of [`CoinSelectionSource`].
pub fn new(source: W, logger: L) -> Self {
Self { source, logger, locked_utxos: Mutex::new(HashMap::new()) }
Self { source, logger, locked_utxos: Mutex::new(new_hash_map()) }
}

/// Performs coin selection on the set of UTXOs obtained from
Expand Down
34 changes: 32 additions & 2 deletions lightning/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,40 @@ mod prelude {
extern crate hashbrown;

pub use alloc::{vec, vec::Vec, string::String, collections::VecDeque, boxed::Box};

#[cfg(not(feature = "hashbrown"))]
mod std_hashtables {
pub(crate) use std::collections::{HashMap, HashSet, hash_map};

pub(crate) type OccupiedHashMapEntry<'a, K, V> =
std::collections::hash_map::OccupiedEntry<'a, K, V>;
pub(crate) type VacantHashMapEntry<'a, K, V> =
std::collections::hash_map::VacantEntry<'a, K, V>;
}
#[cfg(not(feature = "hashbrown"))]
pub use std::collections::{HashMap, HashSet, hash_map};
pub(crate) use std_hashtables::*;

#[cfg(feature = "hashbrown")]
mod hashbrown_tables {
pub(crate) use hashbrown::{HashMap, HashSet, hash_map};

pub(crate) type OccupiedHashMapEntry<'a, K, V> =
hashbrown::hash_map::OccupiedEntry<'a, K, V, hash_map::DefaultHashBuilder>;
pub(crate) type VacantHashMapEntry<'a, K, V> =
hashbrown::hash_map::VacantEntry<'a, K, V, hash_map::DefaultHashBuilder>;
}
#[cfg(feature = "hashbrown")]
pub use self::hashbrown::{HashMap, HashSet, hash_map};
pub(crate) use hashbrown_tables::*;

pub(crate) fn new_hash_map<K: core::hash::Hash + Eq, V>() -> HashMap<K, V> { HashMap::new() }
pub(crate) fn hash_map_with_capacity<K: core::hash::Hash + Eq, V>(cap: usize) -> HashMap<K, V> {
HashMap::with_capacity(cap)
}

pub(crate) fn new_hash_set<K: core::hash::Hash + Eq>() -> HashSet<K> { HashSet::new() }
pub(crate) fn hash_set_with_capacity<K: core::hash::Hash + Eq>(cap: usize) -> HashSet<K> {
HashSet::with_capacity(cap)
}

pub use alloc::borrow::ToOwned;
pub use alloc::string::ToString;
Expand Down
6 changes: 3 additions & 3 deletions lightning/src/ln/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6408,7 +6408,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
channel_ready_event_emitted: false,

#[cfg(any(test, fuzzing))]
historical_inbound_htlc_fulfills: HashSet::new(),
historical_inbound_htlc_fulfills: new_hash_set(),

channel_type,
channel_keys_id,
Expand Down Expand Up @@ -7209,7 +7209,7 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
channel_ready_event_emitted: false,

#[cfg(any(test, fuzzing))]
historical_inbound_htlc_fulfills: HashSet::new(),
historical_inbound_htlc_fulfills: new_hash_set(),

channel_type,
channel_keys_id,
Expand Down Expand Up @@ -8023,7 +8023,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
let channel_update_status = Readable::read(reader)?;

#[cfg(any(test, fuzzing))]
let mut historical_inbound_htlc_fulfills = HashSet::new();
let mut historical_inbound_htlc_fulfills = new_hash_set();
#[cfg(any(test, fuzzing))]
{
let htlc_fulfills_len: u64 = Readable::read(reader)?;
Expand Down
Loading

0 comments on commit b35ab51

Please sign in to comment.