From 1608102d46201295b41ff3d4e161ff51f11add5d Mon Sep 17 00:00:00 2001 From: Elias Rohrer Date: Thu, 5 Dec 2024 13:49:14 +0100 Subject: [PATCH] Use LDK-internal Hash{Map,Set} types in `lightning-liquidity` --- lightning-liquidity/Cargo.toml | 3 +-- lightning-liquidity/src/lib.rs | 4 +++- lightning-liquidity/src/lsps0/msgs.rs | 14 ++++++++------ lightning-liquidity/src/lsps1/client.rs | 14 +++++++------- lightning-liquidity/src/lsps1/service.rs | 4 ++-- lightning-liquidity/src/lsps2/client.rs | 11 +++++------ lightning-liquidity/src/lsps2/service.rs | 18 +++++++++--------- lightning-liquidity/src/manager.rs | 6 +++--- lightning/src/util/hash_tables.rs | 9 ++++++--- 9 files changed, 44 insertions(+), 39 deletions(-) diff --git a/lightning-liquidity/Cargo.toml b/lightning-liquidity/Cargo.toml index 17eae8762b6..3ee224b3e8b 100644 --- a/lightning-liquidity/Cargo.toml +++ b/lightning-liquidity/Cargo.toml @@ -15,7 +15,7 @@ categories = ["cryptography::cryptocurrencies"] [features] default = ["std"] -std = [] +std = ["lightning/std"] [dependencies] lightning = { version = "0.0.124", path = "../lightning", default-features = false } @@ -23,7 +23,6 @@ lightning-types = { version = "0.1", path = "../lightning-types", default-featur lightning-invoice = { version = "0.32.0", path = "../lightning-invoice", default-features = false, features = ["serde"] } bitcoin = { version = "0.32.2", default-features = false, features = ["serde"] } -hashbrown = { version = "0.8" } chrono = { version = "0.4", default-features = false, features = ["serde", "alloc"] } serde = { version = "1.0", default-features = false, features = ["derive", "alloc"] } diff --git a/lightning-liquidity/src/lib.rs b/lightning-liquidity/src/lib.rs index 6f23f3b4ad8..884cc56daa5 100644 --- a/lightning-liquidity/src/lib.rs +++ b/lightning-liquidity/src/lib.rs @@ -25,10 +25,12 @@ extern crate alloc; mod prelude { #![allow(unused_imports)] pub use alloc::{boxed::Box, collections::VecDeque, string::String, vec, vec::Vec}; - pub use hashbrown::{hash_map, HashMap, HashSet}; + //pub use hashbrown::{hash_map, HashMap, HashSet}; pub use alloc::borrow::ToOwned; pub use alloc::string::ToString; + + pub(crate) use lightning::util::hash_tables::*; } pub mod events; diff --git a/lightning-liquidity/src/lsps0/msgs.rs b/lightning-liquidity/src/lsps0/msgs.rs index 2366aa67cf4..adac5f0fae6 100644 --- a/lightning-liquidity/src/lsps0/msgs.rs +++ b/lightning-liquidity/src/lsps0/msgs.rs @@ -90,9 +90,11 @@ impl From for LSPSMessage { #[cfg(test)] mod tests { - use super::*; + use lightning::util::hash_tables::new_hash_map; + +use super::*; use crate::lsps0::ser::LSPSMethod; - use crate::prelude::{HashMap, ToString}; + use crate::prelude::ToString; #[test] fn deserializes_request() { @@ -102,7 +104,7 @@ mod tests { "method": "lsps0.list_protocols" }"#; - let mut request_id_method_map = HashMap::new(); + let mut request_id_method_map = new_hash_map(); let msg = LSPSMessage::from_str_with_id_map(json, &mut request_id_method_map); assert!(msg.is_ok()); @@ -138,7 +140,7 @@ mod tests { "protocols": [1,2,3] } }"#; - let mut request_id_to_method_map = HashMap::new(); + let mut request_id_to_method_map = new_hash_map(); request_id_to_method_map .insert(RequestId("request:id:xyz123".to_string()), LSPSMethod::LSPS0ListProtocols); @@ -164,7 +166,7 @@ mod tests { "message": "Unknown Error" } }"#; - let mut request_id_to_method_map = HashMap::new(); + let mut request_id_to_method_map = new_hash_map(); request_id_to_method_map .insert(RequestId("request:id:xyz123".to_string()), LSPSMethod::LSPS0ListProtocols); @@ -193,7 +195,7 @@ mod tests { "protocols": [1,2,3] } }"#; - let mut request_id_to_method_map = HashMap::new(); + let mut request_id_to_method_map = new_hash_map(); request_id_to_method_map .insert(RequestId("request:id:xyz123".to_string()), LSPSMethod::LSPS0ListProtocols); diff --git a/lightning-liquidity/src/lsps1/client.rs b/lightning-liquidity/src/lsps1/client.rs index a83c2ae87b0..caa95a7e008 100644 --- a/lightning-liquidity/src/lsps1/client.rs +++ b/lightning-liquidity/src/lsps1/client.rs @@ -18,7 +18,7 @@ use crate::message_queue::MessageQueue; use crate::events::{Event, EventQueue}; use crate::lsps0::ser::{ProtocolMessageHandler, RequestId, ResponseError}; -use crate::prelude::{HashMap, HashSet}; +use crate::prelude::{HashMap, HashSet, new_hash_map}; use crate::sync::{Arc, Mutex, RwLock}; use lightning::ln::msgs::{ErrorAction, LightningError}; @@ -69,7 +69,7 @@ where entropy_source, pending_messages, pending_events, - per_peer_state: RwLock::new(HashMap::new()), + per_peer_state: RwLock::new(new_hash_map()), _config: config, } } @@ -142,7 +142,7 @@ where &self, request_id: RequestId, counterparty_node_id: &PublicKey, error: ResponseError, ) -> Result<(), LightningError> { let outer_state_lock = self.per_peer_state.read().unwrap(); - match outer_state_lock.get(&counterparty_node_id) { + match outer_state_lock.get(counterparty_node_id) { Some(inner_state_lock) => { let mut peer_state_lock = inner_state_lock.lock().unwrap(); @@ -219,7 +219,7 @@ where response: CreateOrderResponse, ) -> Result<(), LightningError> { let outer_state_lock = self.per_peer_state.read().unwrap(); - match outer_state_lock.get(&counterparty_node_id) { + match outer_state_lock.get(counterparty_node_id) { Some(inner_state_lock) => { let mut peer_state_lock = inner_state_lock.lock().unwrap(); @@ -260,7 +260,7 @@ where &self, request_id: RequestId, counterparty_node_id: &PublicKey, error: ResponseError, ) -> Result<(), LightningError> { let outer_state_lock = self.per_peer_state.read().unwrap(); - match outer_state_lock.get(&counterparty_node_id) { + match outer_state_lock.get(counterparty_node_id) { Some(inner_state_lock) => { let mut peer_state_lock = inner_state_lock.lock().unwrap(); @@ -338,7 +338,7 @@ where response: CreateOrderResponse, ) -> Result<(), LightningError> { let outer_state_lock = self.per_peer_state.read().unwrap(); - match outer_state_lock.get(&counterparty_node_id) { + match outer_state_lock.get(counterparty_node_id) { Some(inner_state_lock) => { let mut peer_state_lock = inner_state_lock.lock().unwrap(); @@ -379,7 +379,7 @@ where &self, request_id: RequestId, counterparty_node_id: &PublicKey, error: ResponseError, ) -> Result<(), LightningError> { let outer_state_lock = self.per_peer_state.read().unwrap(); - match outer_state_lock.get(&counterparty_node_id) { + match outer_state_lock.get(counterparty_node_id) { Some(inner_state_lock) => { let mut peer_state_lock = inner_state_lock.lock().unwrap(); diff --git a/lightning-liquidity/src/lsps1/service.rs b/lightning-liquidity/src/lsps1/service.rs index e4e6508a281..73d70254a46 100644 --- a/lightning-liquidity/src/lsps1/service.rs +++ b/lightning-liquidity/src/lsps1/service.rs @@ -20,7 +20,7 @@ use crate::message_queue::MessageQueue; use crate::events::{Event, EventQueue}; use crate::lsps0::ser::{ProtocolMessageHandler, RequestId, ResponseError}; -use crate::prelude::{HashMap, String, ToString}; +use crate::prelude::{HashMap, String, ToString, new_hash_map}; use crate::sync::{Arc, Mutex, RwLock}; use crate::utils; @@ -159,7 +159,7 @@ where chain_source, pending_messages, pending_events, - per_peer_state: RwLock::new(HashMap::new()), + per_peer_state: RwLock::new(new_hash_map()), config, } } diff --git a/lightning-liquidity/src/lsps2/client.rs b/lightning-liquidity/src/lsps2/client.rs index e00ad44465f..04da1915bf9 100644 --- a/lightning-liquidity/src/lsps2/client.rs +++ b/lightning-liquidity/src/lsps2/client.rs @@ -3,8 +3,7 @@ // // This file is licensed under the Apache License, Version 2.0 or the MIT license -// , at your option. -// You may not use this file except in accordance with one or both of these +// , at your option. You may not use this file except in accordance with one or both of these // licenses. //! Contains the main LSPS2 client object, [`LSPS2ClientHandler`]. @@ -13,7 +12,7 @@ use crate::events::{Event, EventQueue}; use crate::lsps0::ser::{ProtocolMessageHandler, RequestId, ResponseError}; use crate::lsps2::event::LSPS2ClientEvent; use crate::message_queue::MessageQueue; -use crate::prelude::{HashMap, HashSet, String}; +use crate::prelude::{HashMap, HashSet, String, new_hash_map, new_hash_set}; use crate::sync::{Arc, Mutex, RwLock}; use lightning::ln::msgs::{ErrorAction, LightningError}; @@ -58,8 +57,8 @@ struct PeerState { impl PeerState { fn new() -> Self { - let pending_get_info_requests = HashSet::new(); - let pending_buy_requests = HashMap::new(); + let pending_get_info_requests = new_hash_set(); + let pending_buy_requests = new_hash_map(); Self { pending_get_info_requests, pending_buy_requests } } } @@ -95,7 +94,7 @@ where entropy_source, pending_messages, pending_events, - per_peer_state: RwLock::new(HashMap::new()), + per_peer_state: RwLock::new(new_hash_map()), _config, } } diff --git a/lightning-liquidity/src/lsps2/service.rs b/lightning-liquidity/src/lsps2/service.rs index 76836310a38..de2e37cb1ff 100644 --- a/lightning-liquidity/src/lsps2/service.rs +++ b/lightning-liquidity/src/lsps2/service.rs @@ -15,7 +15,7 @@ use crate::lsps2::event::LSPS2ServiceEvent; use crate::lsps2::payment_queue::{InterceptedHTLC, PaymentQueue}; use crate::lsps2::utils::{compute_opening_fee, is_valid_opening_fee_params}; use crate::message_queue::MessageQueue; -use crate::prelude::{HashMap, String, ToString, Vec}; +use crate::prelude::{HashMap, String, ToString, Vec, new_hash_map}; use crate::sync::{Arc, Mutex, RwLock}; use lightning::events::HTLCDestination; @@ -438,10 +438,10 @@ struct PeerState { impl PeerState { fn new() -> Self { - let outbound_channels_by_intercept_scid = HashMap::new(); - let pending_requests = HashMap::new(); - let intercept_scid_by_user_channel_id = HashMap::new(); - let intercept_scid_by_channel_id = HashMap::new(); + let outbound_channels_by_intercept_scid = new_hash_map(); + let pending_requests = new_hash_map(); + let intercept_scid_by_user_channel_id = new_hash_map(); + let intercept_scid_by_channel_id = new_hash_map(); Self { outbound_channels_by_intercept_scid, pending_requests, @@ -481,9 +481,9 @@ where Self { pending_messages, pending_events, - per_peer_state: RwLock::new(HashMap::new()), - peer_by_intercept_scid: RwLock::new(HashMap::new()), - peer_by_channel_id: RwLock::new(HashMap::new()), + per_peer_state: RwLock::new(new_hash_map()), + peer_by_intercept_scid: RwLock::new(new_hash_map()), + peer_by_channel_id: RwLock::new(new_hash_map()), channel_manager, config, } @@ -852,7 +852,7 @@ where self.peer_by_channel_id.read().unwrap().get(&next_channel_id) { let outer_state_lock = self.per_peer_state.read().unwrap(); - match outer_state_lock.get(&counterparty_node_id) { + match outer_state_lock.get(counterparty_node_id) { Some(inner_state_lock) => { let mut peer_state = inner_state_lock.lock().unwrap(); if let Some(intercept_scid) = diff --git a/lightning-liquidity/src/manager.rs b/lightning-liquidity/src/manager.rs index c9416177681..f03b451c261 100644 --- a/lightning-liquidity/src/manager.rs +++ b/lightning-liquidity/src/manager.rs @@ -17,7 +17,7 @@ use crate::lsps1::service::{LSPS1ServiceConfig, LSPS1ServiceHandler}; use crate::lsps2::client::{LSPS2ClientConfig, LSPS2ClientHandler}; use crate::lsps2::msgs::LSPS2Message; use crate::lsps2::service::{LSPS2ServiceConfig, LSPS2ServiceHandler}; -use crate::prelude::{HashMap, HashSet, ToString, Vec}; +use crate::prelude::{HashMap, HashSet, ToString, Vec, new_hash_map, new_hash_set}; use crate::sync::{Arc, Mutex, RwLock}; use lightning::chain::{self, BestBlock, Confirm, Filter, Listen}; @@ -124,7 +124,7 @@ where where { let pending_messages = Arc::new(MessageQueue::new()); let pending_events = Arc::new(EventQueue::new()); - let ignored_peers = RwLock::new(HashSet::new()); + let ignored_peers = RwLock::new(new_hash_set()); let mut supported_protocols = Vec::new(); @@ -199,7 +199,7 @@ where { Self { pending_messages, pending_events, - request_id_to_method_map: Mutex::new(HashMap::new()), + request_id_to_method_map: Mutex::new(new_hash_map()), ignored_peers, lsps0_client_handler, lsps0_service_handler, diff --git a/lightning/src/util/hash_tables.rs b/lightning/src/util/hash_tables.rs index 3debb42c8f0..00341d57b45 100644 --- a/lightning/src/util/hash_tables.rs +++ b/lightning/src/util/hash_tables.rs @@ -3,7 +3,7 @@ //! //! This module simply re-exports the `HashMap` used in LDK for public consumption. -pub(crate) use hashbrown::hash_map; +pub use hashbrown::hash_map; mod hashbrown_tables { #[cfg(feature = "std")] @@ -67,7 +67,8 @@ mod hashbrown_tables { /// The HashMap type used in LDK. pub type HashMap = hashbrown::HashMap; - pub(crate) type HashSet = hashbrown::HashSet; + /// The HashSet type used in LDK. + pub type HashSet = hashbrown::HashSet; pub(crate) type OccupiedHashMapEntry<'a, K, V> = hashbrown::hash_map::OccupiedEntry<'a, K, V, RandomState>; @@ -96,9 +97,11 @@ mod hashbrown_tables { res } - pub(crate) fn new_hash_set() -> HashSet { + /// Builds a new [`HashSet`]. + pub fn new_hash_set() -> HashSet { HashSet::with_hasher(RandomState::new()) } + /// Builds a new [`HashSet`] with the given capacity. pub(crate) fn hash_set_with_capacity(cap: usize) -> HashSet { HashSet::with_capacity_and_hasher(cap, RandomState::new()) }