Skip to content

Commit 1608102

Browse files
committed
Use LDK-internal Hash{Map,Set} types in lightning-liquidity
1 parent d2ab610 commit 1608102

File tree

9 files changed

+44
-39
lines changed

9 files changed

+44
-39
lines changed

lightning-liquidity/Cargo.toml

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,14 @@ categories = ["cryptography::cryptocurrencies"]
1515

1616
[features]
1717
default = ["std"]
18-
std = []
18+
std = ["lightning/std"]
1919

2020
[dependencies]
2121
lightning = { version = "0.0.124", path = "../lightning", default-features = false }
2222
lightning-types = { version = "0.1", path = "../lightning-types", default-features = false }
2323
lightning-invoice = { version = "0.32.0", path = "../lightning-invoice", default-features = false, features = ["serde"] }
2424

2525
bitcoin = { version = "0.32.2", default-features = false, features = ["serde"] }
26-
hashbrown = { version = "0.8" }
2726

2827
chrono = { version = "0.4", default-features = false, features = ["serde", "alloc"] }
2928
serde = { version = "1.0", default-features = false, features = ["derive", "alloc"] }

lightning-liquidity/src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ extern crate alloc;
2525
mod prelude {
2626
#![allow(unused_imports)]
2727
pub use alloc::{boxed::Box, collections::VecDeque, string::String, vec, vec::Vec};
28-
pub use hashbrown::{hash_map, HashMap, HashSet};
28+
//pub use hashbrown::{hash_map, HashMap, HashSet};
2929

3030
pub use alloc::borrow::ToOwned;
3131
pub use alloc::string::ToString;
32+
33+
pub(crate) use lightning::util::hash_tables::*;
3234
}
3335

3436
pub mod events;

lightning-liquidity/src/lsps0/msgs.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,11 @@ impl From<LSPS0Message> for LSPSMessage {
9090

9191
#[cfg(test)]
9292
mod tests {
93-
use super::*;
93+
use lightning::util::hash_tables::new_hash_map;
94+
95+
use super::*;
9496
use crate::lsps0::ser::LSPSMethod;
95-
use crate::prelude::{HashMap, ToString};
97+
use crate::prelude::ToString;
9698

9799
#[test]
98100
fn deserializes_request() {
@@ -102,7 +104,7 @@ mod tests {
102104
"method": "lsps0.list_protocols"
103105
}"#;
104106

105-
let mut request_id_method_map = HashMap::new();
107+
let mut request_id_method_map = new_hash_map();
106108

107109
let msg = LSPSMessage::from_str_with_id_map(json, &mut request_id_method_map);
108110
assert!(msg.is_ok());
@@ -138,7 +140,7 @@ mod tests {
138140
"protocols": [1,2,3]
139141
}
140142
}"#;
141-
let mut request_id_to_method_map = HashMap::new();
143+
let mut request_id_to_method_map = new_hash_map();
142144
request_id_to_method_map
143145
.insert(RequestId("request:id:xyz123".to_string()), LSPSMethod::LSPS0ListProtocols);
144146

@@ -164,7 +166,7 @@ mod tests {
164166
"message": "Unknown Error"
165167
}
166168
}"#;
167-
let mut request_id_to_method_map = HashMap::new();
169+
let mut request_id_to_method_map = new_hash_map();
168170
request_id_to_method_map
169171
.insert(RequestId("request:id:xyz123".to_string()), LSPSMethod::LSPS0ListProtocols);
170172

@@ -193,7 +195,7 @@ mod tests {
193195
"protocols": [1,2,3]
194196
}
195197
}"#;
196-
let mut request_id_to_method_map = HashMap::new();
198+
let mut request_id_to_method_map = new_hash_map();
197199
request_id_to_method_map
198200
.insert(RequestId("request:id:xyz123".to_string()), LSPSMethod::LSPS0ListProtocols);
199201

lightning-liquidity/src/lsps1/client.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::message_queue::MessageQueue;
1818

1919
use crate::events::{Event, EventQueue};
2020
use crate::lsps0::ser::{ProtocolMessageHandler, RequestId, ResponseError};
21-
use crate::prelude::{HashMap, HashSet};
21+
use crate::prelude::{HashMap, HashSet, new_hash_map};
2222
use crate::sync::{Arc, Mutex, RwLock};
2323

2424
use lightning::ln::msgs::{ErrorAction, LightningError};
@@ -69,7 +69,7 @@ where
6969
entropy_source,
7070
pending_messages,
7171
pending_events,
72-
per_peer_state: RwLock::new(HashMap::new()),
72+
per_peer_state: RwLock::new(new_hash_map()),
7373
_config: config,
7474
}
7575
}
@@ -142,7 +142,7 @@ where
142142
&self, request_id: RequestId, counterparty_node_id: &PublicKey, error: ResponseError,
143143
) -> Result<(), LightningError> {
144144
let outer_state_lock = self.per_peer_state.read().unwrap();
145-
match outer_state_lock.get(&counterparty_node_id) {
145+
match outer_state_lock.get(counterparty_node_id) {
146146
Some(inner_state_lock) => {
147147
let mut peer_state_lock = inner_state_lock.lock().unwrap();
148148

@@ -219,7 +219,7 @@ where
219219
response: CreateOrderResponse,
220220
) -> Result<(), LightningError> {
221221
let outer_state_lock = self.per_peer_state.read().unwrap();
222-
match outer_state_lock.get(&counterparty_node_id) {
222+
match outer_state_lock.get(counterparty_node_id) {
223223
Some(inner_state_lock) => {
224224
let mut peer_state_lock = inner_state_lock.lock().unwrap();
225225

@@ -260,7 +260,7 @@ where
260260
&self, request_id: RequestId, counterparty_node_id: &PublicKey, error: ResponseError,
261261
) -> Result<(), LightningError> {
262262
let outer_state_lock = self.per_peer_state.read().unwrap();
263-
match outer_state_lock.get(&counterparty_node_id) {
263+
match outer_state_lock.get(counterparty_node_id) {
264264
Some(inner_state_lock) => {
265265
let mut peer_state_lock = inner_state_lock.lock().unwrap();
266266

@@ -338,7 +338,7 @@ where
338338
response: CreateOrderResponse,
339339
) -> Result<(), LightningError> {
340340
let outer_state_lock = self.per_peer_state.read().unwrap();
341-
match outer_state_lock.get(&counterparty_node_id) {
341+
match outer_state_lock.get(counterparty_node_id) {
342342
Some(inner_state_lock) => {
343343
let mut peer_state_lock = inner_state_lock.lock().unwrap();
344344

@@ -379,7 +379,7 @@ where
379379
&self, request_id: RequestId, counterparty_node_id: &PublicKey, error: ResponseError,
380380
) -> Result<(), LightningError> {
381381
let outer_state_lock = self.per_peer_state.read().unwrap();
382-
match outer_state_lock.get(&counterparty_node_id) {
382+
match outer_state_lock.get(counterparty_node_id) {
383383
Some(inner_state_lock) => {
384384
let mut peer_state_lock = inner_state_lock.lock().unwrap();
385385

lightning-liquidity/src/lsps1/service.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::message_queue::MessageQueue;
2020

2121
use crate::events::{Event, EventQueue};
2222
use crate::lsps0::ser::{ProtocolMessageHandler, RequestId, ResponseError};
23-
use crate::prelude::{HashMap, String, ToString};
23+
use crate::prelude::{HashMap, String, ToString, new_hash_map};
2424
use crate::sync::{Arc, Mutex, RwLock};
2525
use crate::utils;
2626

@@ -159,7 +159,7 @@ where
159159
chain_source,
160160
pending_messages,
161161
pending_events,
162-
per_peer_state: RwLock::new(HashMap::new()),
162+
per_peer_state: RwLock::new(new_hash_map()),
163163
config,
164164
}
165165
}

lightning-liquidity/src/lsps2/client.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
//
44
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
55
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6-
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7-
// You may not use this file except in accordance with one or both of these
6+
// <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
87
// licenses.
98

109
//! Contains the main LSPS2 client object, [`LSPS2ClientHandler`].
@@ -13,7 +12,7 @@ use crate::events::{Event, EventQueue};
1312
use crate::lsps0::ser::{ProtocolMessageHandler, RequestId, ResponseError};
1413
use crate::lsps2::event::LSPS2ClientEvent;
1514
use crate::message_queue::MessageQueue;
16-
use crate::prelude::{HashMap, HashSet, String};
15+
use crate::prelude::{HashMap, HashSet, String, new_hash_map, new_hash_set};
1716
use crate::sync::{Arc, Mutex, RwLock};
1817

1918
use lightning::ln::msgs::{ErrorAction, LightningError};
@@ -58,8 +57,8 @@ struct PeerState {
5857

5958
impl PeerState {
6059
fn new() -> Self {
61-
let pending_get_info_requests = HashSet::new();
62-
let pending_buy_requests = HashMap::new();
60+
let pending_get_info_requests = new_hash_set();
61+
let pending_buy_requests = new_hash_map();
6362
Self { pending_get_info_requests, pending_buy_requests }
6463
}
6564
}
@@ -95,7 +94,7 @@ where
9594
entropy_source,
9695
pending_messages,
9796
pending_events,
98-
per_peer_state: RwLock::new(HashMap::new()),
97+
per_peer_state: RwLock::new(new_hash_map()),
9998
_config,
10099
}
101100
}

lightning-liquidity/src/lsps2/service.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::lsps2::event::LSPS2ServiceEvent;
1515
use crate::lsps2::payment_queue::{InterceptedHTLC, PaymentQueue};
1616
use crate::lsps2::utils::{compute_opening_fee, is_valid_opening_fee_params};
1717
use crate::message_queue::MessageQueue;
18-
use crate::prelude::{HashMap, String, ToString, Vec};
18+
use crate::prelude::{HashMap, String, ToString, Vec, new_hash_map};
1919
use crate::sync::{Arc, Mutex, RwLock};
2020

2121
use lightning::events::HTLCDestination;
@@ -438,10 +438,10 @@ struct PeerState {
438438

439439
impl PeerState {
440440
fn new() -> Self {
441-
let outbound_channels_by_intercept_scid = HashMap::new();
442-
let pending_requests = HashMap::new();
443-
let intercept_scid_by_user_channel_id = HashMap::new();
444-
let intercept_scid_by_channel_id = HashMap::new();
441+
let outbound_channels_by_intercept_scid = new_hash_map();
442+
let pending_requests = new_hash_map();
443+
let intercept_scid_by_user_channel_id = new_hash_map();
444+
let intercept_scid_by_channel_id = new_hash_map();
445445
Self {
446446
outbound_channels_by_intercept_scid,
447447
pending_requests,
@@ -481,9 +481,9 @@ where
481481
Self {
482482
pending_messages,
483483
pending_events,
484-
per_peer_state: RwLock::new(HashMap::new()),
485-
peer_by_intercept_scid: RwLock::new(HashMap::new()),
486-
peer_by_channel_id: RwLock::new(HashMap::new()),
484+
per_peer_state: RwLock::new(new_hash_map()),
485+
peer_by_intercept_scid: RwLock::new(new_hash_map()),
486+
peer_by_channel_id: RwLock::new(new_hash_map()),
487487
channel_manager,
488488
config,
489489
}
@@ -852,7 +852,7 @@ where
852852
self.peer_by_channel_id.read().unwrap().get(&next_channel_id)
853853
{
854854
let outer_state_lock = self.per_peer_state.read().unwrap();
855-
match outer_state_lock.get(&counterparty_node_id) {
855+
match outer_state_lock.get(counterparty_node_id) {
856856
Some(inner_state_lock) => {
857857
let mut peer_state = inner_state_lock.lock().unwrap();
858858
if let Some(intercept_scid) =

lightning-liquidity/src/manager.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::lsps1::service::{LSPS1ServiceConfig, LSPS1ServiceHandler};
1717
use crate::lsps2::client::{LSPS2ClientConfig, LSPS2ClientHandler};
1818
use crate::lsps2::msgs::LSPS2Message;
1919
use crate::lsps2::service::{LSPS2ServiceConfig, LSPS2ServiceHandler};
20-
use crate::prelude::{HashMap, HashSet, ToString, Vec};
20+
use crate::prelude::{HashMap, HashSet, ToString, Vec, new_hash_map, new_hash_set};
2121
use crate::sync::{Arc, Mutex, RwLock};
2222

2323
use lightning::chain::{self, BestBlock, Confirm, Filter, Listen};
@@ -124,7 +124,7 @@ where
124124
where {
125125
let pending_messages = Arc::new(MessageQueue::new());
126126
let pending_events = Arc::new(EventQueue::new());
127-
let ignored_peers = RwLock::new(HashSet::new());
127+
let ignored_peers = RwLock::new(new_hash_set());
128128

129129
let mut supported_protocols = Vec::new();
130130

@@ -199,7 +199,7 @@ where {
199199
Self {
200200
pending_messages,
201201
pending_events,
202-
request_id_to_method_map: Mutex::new(HashMap::new()),
202+
request_id_to_method_map: Mutex::new(new_hash_map()),
203203
ignored_peers,
204204
lsps0_client_handler,
205205
lsps0_service_handler,

lightning/src/util/hash_tables.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//!
44
//! This module simply re-exports the `HashMap` used in LDK for public consumption.
55
6-
pub(crate) use hashbrown::hash_map;
6+
pub use hashbrown::hash_map;
77

88
mod hashbrown_tables {
99
#[cfg(feature = "std")]
@@ -67,7 +67,8 @@ mod hashbrown_tables {
6767

6868
/// The HashMap type used in LDK.
6969
pub type HashMap<K, V> = hashbrown::HashMap<K, V, RandomState>;
70-
pub(crate) type HashSet<K> = hashbrown::HashSet<K, RandomState>;
70+
/// The HashSet type used in LDK.
71+
pub type HashSet<K> = hashbrown::HashSet<K, RandomState>;
7172

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

99-
pub(crate) fn new_hash_set<K>() -> HashSet<K> {
100+
/// Builds a new [`HashSet`].
101+
pub fn new_hash_set<K>() -> HashSet<K> {
100102
HashSet::with_hasher(RandomState::new())
101103
}
104+
/// Builds a new [`HashSet`] with the given capacity.
102105
pub(crate) fn hash_set_with_capacity<K>(cap: usize) -> HashSet<K> {
103106
HashSet::with_capacity_and_hasher(cap, RandomState::new())
104107
}

0 commit comments

Comments
 (0)