Skip to content

Commit

Permalink
Use LDK-internal Hash{Map,Set} types in lightning-liquidity
Browse files Browse the repository at this point in the history
  • Loading branch information
tnull committed Dec 5, 2024
1 parent d2ab610 commit dca99f4
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 38 deletions.
3 changes: 1 addition & 2 deletions lightning-liquidity/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@ categories = ["cryptography::cryptocurrencies"]

[features]
default = ["std"]
std = []
std = ["lightning/std"]

[dependencies]
lightning = { version = "0.0.124", path = "../lightning", default-features = false }
lightning-types = { version = "0.1", path = "../lightning-types", default-features = false }
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"] }
Expand Down
4 changes: 3 additions & 1 deletion lightning-liquidity/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 7 additions & 5 deletions lightning-liquidity/src/lsps0/msgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,11 @@ impl From<LSPS0Message> for LSPSMessage {

#[cfg(test)]
mod tests {
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() {
Expand All @@ -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());
Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand Down Expand Up @@ -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);

Expand Down
14 changes: 7 additions & 7 deletions lightning-liquidity/src/lsps1/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{new_hash_map, HashMap, HashSet};
use crate::sync::{Arc, Mutex, RwLock};

use lightning::ln::msgs::{ErrorAction, LightningError};
Expand Down Expand Up @@ -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,
}
}
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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();

Expand Down
4 changes: 2 additions & 2 deletions lightning-liquidity/src/lsps1/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{new_hash_map, HashMap, String, ToString};
use crate::sync::{Arc, Mutex, RwLock};
use crate::utils;

Expand Down Expand Up @@ -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,
}
}
Expand Down
11 changes: 5 additions & 6 deletions lightning-liquidity/src/lsps2/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
//
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
// You may not use this file except in accordance with one or both of these
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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`].
Expand All @@ -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::{new_hash_map, new_hash_set, HashMap, HashSet, String};
use crate::sync::{Arc, Mutex, RwLock};

use lightning::ln::msgs::{ErrorAction, LightningError};
Expand Down Expand Up @@ -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 }
}
}
Expand Down Expand Up @@ -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,
}
}
Expand Down
18 changes: 9 additions & 9 deletions lightning-liquidity/src/lsps2/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{new_hash_map, HashMap, String, ToString, Vec};
use crate::sync::{Arc, Mutex, RwLock};

use lightning::events::HTLCDestination;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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) =
Expand Down
6 changes: 3 additions & 3 deletions lightning-liquidity/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{new_hash_map, new_hash_set, HashMap, HashSet, ToString, Vec};
use crate::sync::{Arc, Mutex, RwLock};

use lightning::chain::{self, BestBlock, Confirm, Filter, Listen};
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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,
Expand Down
9 changes: 6 additions & 3 deletions lightning/src/util/hash_tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -67,7 +67,8 @@ mod hashbrown_tables {

/// The HashMap type used in LDK.
pub type HashMap<K, V> = hashbrown::HashMap<K, V, RandomState>;
pub(crate) type HashSet<K> = hashbrown::HashSet<K, RandomState>;
/// The HashSet type used in LDK.
pub type HashSet<K> = hashbrown::HashSet<K, RandomState>;

pub(crate) type OccupiedHashMapEntry<'a, K, V> =
hashbrown::hash_map::OccupiedEntry<'a, K, V, RandomState>;
Expand Down Expand Up @@ -96,9 +97,11 @@ mod hashbrown_tables {
res
}

pub(crate) fn new_hash_set<K>() -> HashSet<K> {
/// Builds a new [`HashSet`].
pub fn new_hash_set<K>() -> HashSet<K> {
HashSet::with_hasher(RandomState::new())
}
/// Builds a new [`HashSet`] with the given capacity.
pub(crate) fn hash_set_with_capacity<K>(cap: usize) -> HashSet<K> {
HashSet::with_capacity_and_hasher(cap, RandomState::new())
}
Expand Down

0 comments on commit dca99f4

Please sign in to comment.