Skip to content

Commit ab82102

Browse files
Drop Deref indirection for KVStore
Reduces generics and verbosity across the codebase, should provide equivalent behavior. Unfortunately the same improvement can't be made for KVStoreSync and Persist, due to the blanket implementation where all KVStoreSync traits implement Persist resulting in conflicting implementations.
1 parent 238b799 commit ab82102

14 files changed

Lines changed: 99 additions & 189 deletions

File tree

lightning-background-processor/src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,6 @@ pub const NO_LIQUIDITY_MANAGER: Option<
474474
CM = &DynChannelManager,
475475
Filter = dyn chain::Filter + Send + Sync,
476476
C = &(dyn chain::Filter + Send + Sync),
477-
KVStore = DummyKVStore,
478477
K = &DummyKVStore,
479478
TimeProvider = dyn lightning_liquidity::utils::time::TimeProvider + Send + Sync,
480479
TP = &(dyn lightning_liquidity::utils::time::TimeProvider + Send + Sync),
@@ -955,7 +954,7 @@ pub async fn process_events_async<
955954
LM: Deref,
956955
D: Deref,
957956
O: Deref,
958-
K: Deref,
957+
K: KVStore,
959958
OS: Deref<Target = OutputSweeper<T, D, F, CF, K, L, O>>,
960959
S: Deref<Target = SC>,
961960
SC: for<'b> WriteableScore<'b>,
@@ -978,7 +977,6 @@ where
978977
LM::Target: ALiquidityManager,
979978
O::Target: OutputSpender,
980979
D::Target: ChangeDestinationSource,
981-
K::Target: KVStore,
982980
{
983981
let async_event_handler = |event| {
984982
let network_graph = gossip_sync.network_graph();

lightning-liquidity/src/events/event_queue.rs

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use alloc::collections::VecDeque;
1212
use alloc::vec::Vec;
1313

1414
use core::future::Future;
15-
use core::ops::Deref;
1615
use core::task::{Poll, Waker};
1716

1817
use lightning::ln::msgs::DecodeError;
@@ -25,10 +24,7 @@ use lightning::util::wakers::Notifier;
2524
/// The maximum queue size we allow before starting to drop events.
2625
pub const MAX_EVENT_QUEUE_SIZE: usize = 1000;
2726

28-
pub(crate) struct EventQueue<K: Deref + Clone>
29-
where
30-
K::Target: KVStore,
31-
{
27+
pub(crate) struct EventQueue<K: KVStore + Clone> {
3228
state: Mutex<QueueState>,
3329
waker: Mutex<Option<Waker>>,
3430
#[cfg(feature = "std")]
@@ -37,10 +33,7 @@ where
3733
persist_notifier: Arc<Notifier>,
3834
}
3935

40-
impl<K: Deref + Clone> EventQueue<K>
41-
where
42-
K::Target: KVStore,
43-
{
36+
impl<K: KVStore + Clone> EventQueue<K> {
4437
pub fn new(
4538
queue: VecDeque<LiquidityEvent>, kv_store: K, persist_notifier: Arc<Notifier>,
4639
) -> Self {
@@ -164,14 +157,9 @@ struct QueueState {
164157

165158
// A guard type that will notify about new events when dropped.
166159
#[must_use]
167-
pub(crate) struct EventQueueNotifierGuard<'a, K: Deref + Clone>(&'a EventQueue<K>)
168-
where
169-
K::Target: KVStore;
170-
171-
impl<'a, K: Deref + Clone> EventQueueNotifierGuard<'a, K>
172-
where
173-
K::Target: KVStore,
174-
{
160+
pub(crate) struct EventQueueNotifierGuard<'a, K: KVStore + Clone>(&'a EventQueue<K>);
161+
162+
impl<'a, K: KVStore + Clone> EventQueueNotifierGuard<'a, K> {
175163
pub fn enqueue<E: Into<LiquidityEvent>>(&self, event: E) {
176164
let mut state_lock = self.0.state.lock().unwrap();
177165
if state_lock.queue.len() < MAX_EVENT_QUEUE_SIZE {
@@ -183,10 +171,7 @@ where
183171
}
184172
}
185173

186-
impl<'a, K: Deref + Clone> Drop for EventQueueNotifierGuard<'a, K>
187-
where
188-
K::Target: KVStore,
189-
{
174+
impl<'a, K: KVStore + Clone> Drop for EventQueueNotifierGuard<'a, K> {
190175
fn drop(&mut self) {
191176
let (should_notify, should_persist_notify) = {
192177
let state_lock = self.0.state.lock().unwrap();
@@ -208,14 +193,9 @@ where
208193
}
209194
}
210195

211-
struct EventFuture<'a, K: Deref + Clone>(&'a EventQueue<K>)
212-
where
213-
K::Target: KVStore;
196+
struct EventFuture<'a, K: KVStore + Clone>(&'a EventQueue<K>);
214197

215-
impl<K: Deref + Clone> Future for EventFuture<'_, K>
216-
where
217-
K::Target: KVStore,
218-
{
198+
impl<K: KVStore + Clone> Future for EventFuture<'_, K> {
219199
type Output = LiquidityEvent;
220200

221201
fn poll(

lightning-liquidity/src/lsps0/client.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,14 @@ use lightning::util::persist::KVStore;
2222

2323
use bitcoin::secp256k1::PublicKey;
2424

25-
use core::ops::Deref;
26-
2725
/// A message handler capable of sending and handling bLIP-50 / LSPS0 messages.
28-
pub struct LSPS0ClientHandler<ES: EntropySource, K: Deref + Clone>
29-
where
30-
K::Target: KVStore,
31-
{
26+
pub struct LSPS0ClientHandler<ES: EntropySource, K: KVStore + Clone> {
3227
entropy_source: ES,
3328
pending_messages: Arc<MessageQueue>,
3429
pending_events: Arc<EventQueue<K>>,
3530
}
3631

37-
impl<ES: EntropySource, K: Deref + Clone> LSPS0ClientHandler<ES, K>
38-
where
39-
K::Target: KVStore,
40-
{
32+
impl<ES: EntropySource, K: KVStore + Clone> LSPS0ClientHandler<ES, K> {
4133
/// Returns a new instance of [`LSPS0ClientHandler`].
4234
pub(crate) fn new(
4335
entropy_source: ES, pending_messages: Arc<MessageQueue>, pending_events: Arc<EventQueue<K>>,
@@ -87,9 +79,8 @@ where
8779
}
8880
}
8981

90-
impl<ES: EntropySource, K: Deref + Clone> LSPSProtocolMessageHandler for LSPS0ClientHandler<ES, K>
91-
where
92-
K::Target: KVStore,
82+
impl<ES: EntropySource, K: KVStore + Clone> LSPSProtocolMessageHandler
83+
for LSPS0ClientHandler<ES, K>
9384
{
9485
type ProtocolMessage = LSPS0Message;
9586
const PROTOCOL_NUMBER: Option<u16> = None;

lightning-liquidity/src/lsps1/client.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ use lightning::util::persist::KVStore;
3030
use bitcoin::secp256k1::PublicKey;
3131
use bitcoin::Address;
3232

33-
use core::ops::Deref;
34-
3533
/// Client-side configuration options for bLIP-51 / LSPS1 channel requests.
3634
#[derive(Clone, Debug)]
3735
pub struct LSPS1ClientConfig {
@@ -47,21 +45,15 @@ struct PeerState {
4745
}
4846

4947
/// The main object allowing to send and receive bLIP-51 / LSPS1 messages.
50-
pub struct LSPS1ClientHandler<ES: EntropySource, K: Deref + Clone>
51-
where
52-
K::Target: KVStore,
53-
{
48+
pub struct LSPS1ClientHandler<ES: EntropySource, K: KVStore + Clone> {
5449
entropy_source: ES,
5550
pending_messages: Arc<MessageQueue>,
5651
pending_events: Arc<EventQueue<K>>,
5752
per_peer_state: RwLock<HashMap<PublicKey, Mutex<PeerState>>>,
5853
config: LSPS1ClientConfig,
5954
}
6055

61-
impl<ES: EntropySource, K: Deref + Clone> LSPS1ClientHandler<ES, K>
62-
where
63-
K::Target: KVStore,
64-
{
56+
impl<ES: EntropySource, K: KVStore + Clone> LSPS1ClientHandler<ES, K> {
6557
/// Constructs an `LSPS1ClientHandler`.
6658
pub(crate) fn new(
6759
entropy_source: ES, pending_messages: Arc<MessageQueue>,
@@ -430,9 +422,8 @@ where
430422
}
431423
}
432424

433-
impl<ES: EntropySource, K: Deref + Clone> LSPSProtocolMessageHandler for LSPS1ClientHandler<ES, K>
434-
where
435-
K::Target: KVStore,
425+
impl<ES: EntropySource, K: KVStore + Clone> LSPSProtocolMessageHandler
426+
for LSPS1ClientHandler<ES, K>
436427
{
437428
type ProtocolMessage = LSPS1Message;
438429
const PROTOCOL_NUMBER: Option<u16> = Some(1);

lightning-liquidity/src/lsps1/service.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,10 @@ impl PeerState {
132132
}
133133

134134
/// The main object allowing to send and receive bLIP-51 / LSPS1 messages.
135-
pub struct LSPS1ServiceHandler<ES: EntropySource, CM: Deref + Clone, C: Deref, K: Deref + Clone>
135+
pub struct LSPS1ServiceHandler<ES: EntropySource, CM: Deref + Clone, C: Deref, K: KVStore + Clone>
136136
where
137137
CM::Target: AChannelManager,
138138
C::Target: Filter,
139-
K::Target: KVStore,
140139
{
141140
entropy_source: ES,
142141
channel_manager: CM,
@@ -147,12 +146,11 @@ where
147146
config: LSPS1ServiceConfig,
148147
}
149148

150-
impl<ES: EntropySource, CM: Deref + Clone, C: Deref, K: Deref + Clone>
149+
impl<ES: EntropySource, CM: Deref + Clone, C: Deref, K: KVStore + Clone>
151150
LSPS1ServiceHandler<ES, CM, C, K>
152151
where
153152
CM::Target: AChannelManager,
154153
C::Target: Filter,
155-
K::Target: KVStore,
156154
{
157155
/// Constructs a `LSPS1ServiceHandler`.
158156
pub(crate) fn new(
@@ -419,12 +417,11 @@ where
419417
}
420418
}
421419

422-
impl<ES: EntropySource, CM: Deref + Clone, C: Deref, K: Deref + Clone> LSPSProtocolMessageHandler
420+
impl<ES: EntropySource, CM: Deref + Clone, C: Deref, K: KVStore + Clone> LSPSProtocolMessageHandler
423421
for LSPS1ServiceHandler<ES, CM, C, K>
424422
where
425423
CM::Target: AChannelManager,
426424
C::Target: Filter,
427-
K::Target: KVStore,
428425
{
429426
type ProtocolMessage = LSPS1Message;
430427
const PROTOCOL_NUMBER: Option<u16> = Some(1);

lightning-liquidity/src/lsps2/client.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use alloc::string::{String, ToString};
1313
use lightning::util::persist::KVStore;
1414

1515
use core::default::Default;
16-
use core::ops::Deref;
1716

1817
use crate::events::EventQueue;
1918
use crate::lsps0::ser::{LSPSProtocolMessageHandler, LSPSRequestId, LSPSResponseError};
@@ -68,21 +67,15 @@ impl PeerState {
6867
/// opened. Please refer to the [`bLIP-52 / LSPS2 specification`] for more information.
6968
///
7069
/// [`bLIP-52 / LSPS2 specification`]: https://github.com/lightning/blips/blob/master/blip-0052.md#trust-models
71-
pub struct LSPS2ClientHandler<ES: EntropySource, K: Deref + Clone>
72-
where
73-
K::Target: KVStore,
74-
{
70+
pub struct LSPS2ClientHandler<ES: EntropySource, K: KVStore + Clone> {
7571
entropy_source: ES,
7672
pending_messages: Arc<MessageQueue>,
7773
pending_events: Arc<EventQueue<K>>,
7874
per_peer_state: RwLock<HashMap<PublicKey, Mutex<PeerState>>>,
7975
config: LSPS2ClientConfig,
8076
}
8177

82-
impl<ES: EntropySource, K: Deref + Clone> LSPS2ClientHandler<ES, K>
83-
where
84-
K::Target: KVStore,
85-
{
78+
impl<ES: EntropySource, K: KVStore + Clone> LSPS2ClientHandler<ES, K> {
8679
/// Constructs an `LSPS2ClientHandler`.
8780
pub(crate) fn new(
8881
entropy_source: ES, pending_messages: Arc<MessageQueue>,
@@ -373,9 +366,8 @@ where
373366
}
374367
}
375368

376-
impl<ES: EntropySource, K: Deref + Clone> LSPSProtocolMessageHandler for LSPS2ClientHandler<ES, K>
377-
where
378-
K::Target: KVStore,
369+
impl<ES: EntropySource, K: KVStore + Clone> LSPSProtocolMessageHandler
370+
for LSPS2ClientHandler<ES, K>
379371
{
380372
type ProtocolMessage = LSPS2Message;
381373
const PROTOCOL_NUMBER: Option<u16> = Some(2);

lightning-liquidity/src/lsps2/service.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -702,10 +702,9 @@ macro_rules! get_or_insert_peer_state_entry {
702702
}
703703

704704
/// The main object allowing to send and receive bLIP-52 / LSPS2 messages.
705-
pub struct LSPS2ServiceHandler<CM: Deref, K: Deref + Clone, T: BroadcasterInterface>
705+
pub struct LSPS2ServiceHandler<CM: Deref, K: KVStore + Clone, T: BroadcasterInterface>
706706
where
707707
CM::Target: AChannelManager,
708-
K::Target: KVStore,
709708
{
710709
channel_manager: CM,
711710
kv_store: K,
@@ -720,10 +719,9 @@ where
720719
persistence_in_flight: AtomicUsize,
721720
}
722721

723-
impl<CM: Deref, K: Deref + Clone, T: BroadcasterInterface + Clone> LSPS2ServiceHandler<CM, K, T>
722+
impl<CM: Deref, K: KVStore + Clone, T: BroadcasterInterface + Clone> LSPS2ServiceHandler<CM, K, T>
724723
where
725724
CM::Target: AChannelManager,
726-
K::Target: KVStore,
727725
{
728726
/// Constructs a `LSPS2ServiceHandler`.
729727
pub(crate) fn new(
@@ -2042,11 +2040,10 @@ where
20422040
}
20432041
}
20442042

2045-
impl<CM: Deref, K: Deref + Clone, T: BroadcasterInterface + Clone> LSPSProtocolMessageHandler
2043+
impl<CM: Deref, K: KVStore + Clone, T: BroadcasterInterface + Clone> LSPSProtocolMessageHandler
20462044
for LSPS2ServiceHandler<CM, K, T>
20472045
where
20482046
CM::Target: AChannelManager,
2049-
K::Target: KVStore,
20502047
{
20512048
type ProtocolMessage = LSPS2Message;
20522049
const PROTOCOL_NUMBER: Option<u16> = Some(2);
@@ -2116,19 +2113,21 @@ fn calculate_amount_to_forward_per_htlc(
21162113

21172114
/// A synchroneous wrapper around [`LSPS2ServiceHandler`] to be used in contexts where async is not
21182115
/// available.
2119-
pub struct LSPS2ServiceHandlerSync<'a, CM: Deref, K: Deref + Clone, T: BroadcasterInterface + Clone>
2120-
where
2116+
pub struct LSPS2ServiceHandlerSync<
2117+
'a,
2118+
CM: Deref,
2119+
K: KVStore + Clone,
2120+
T: BroadcasterInterface + Clone,
2121+
> where
21212122
CM::Target: AChannelManager,
2122-
K::Target: KVStore,
21232123
{
21242124
inner: &'a LSPS2ServiceHandler<CM, K, T>,
21252125
}
21262126

2127-
impl<'a, CM: Deref, K: Deref + Clone, T: BroadcasterInterface + Clone>
2127+
impl<'a, CM: Deref, K: KVStore + Clone, T: BroadcasterInterface + Clone>
21282128
LSPS2ServiceHandlerSync<'a, CM, K, T>
21292129
where
21302130
CM::Target: AChannelManager,
2131-
K::Target: KVStore,
21322131
{
21332132
pub(crate) fn from_inner(inner: &'a LSPS2ServiceHandler<CM, K, T>) -> Self {
21342133
Self { inner }

lightning-liquidity/src/lsps5/client.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ use alloc::collections::VecDeque;
3535
use alloc::string::String;
3636
use lightning::util::persist::KVStore;
3737

38-
use core::ops::Deref;
39-
4038
impl PartialEq<LSPSRequestId> for (LSPSRequestId, (LSPS5AppName, LSPS5WebhookUrl)) {
4139
fn eq(&self, other: &LSPSRequestId) -> bool {
4240
&self.0 == other
@@ -125,21 +123,15 @@ impl PeerState {
125123
/// [`lsps5.list_webhooks`]: super::msgs::LSPS5Request::ListWebhooks
126124
/// [`lsps5.remove_webhook`]: super::msgs::LSPS5Request::RemoveWebhook
127125
/// [`LSPS5Validator`]: super::validator::LSPS5Validator
128-
pub struct LSPS5ClientHandler<ES: EntropySource, K: Deref + Clone>
129-
where
130-
K::Target: KVStore,
131-
{
126+
pub struct LSPS5ClientHandler<ES: EntropySource, K: KVStore + Clone> {
132127
pending_messages: Arc<MessageQueue>,
133128
pending_events: Arc<EventQueue<K>>,
134129
entropy_source: ES,
135130
per_peer_state: RwLock<HashMap<PublicKey, Mutex<PeerState>>>,
136131
_config: LSPS5ClientConfig,
137132
}
138133

139-
impl<ES: EntropySource, K: Deref + Clone> LSPS5ClientHandler<ES, K>
140-
where
141-
K::Target: KVStore,
142-
{
134+
impl<ES: EntropySource, K: KVStore + Clone> LSPS5ClientHandler<ES, K> {
143135
/// Constructs an `LSPS5ClientHandler`.
144136
pub(crate) fn new(
145137
entropy_source: ES, pending_messages: Arc<MessageQueue>,
@@ -424,9 +416,8 @@ where
424416
}
425417
}
426418

427-
impl<ES: EntropySource, K: Deref + Clone> LSPSProtocolMessageHandler for LSPS5ClientHandler<ES, K>
428-
where
429-
K::Target: KVStore,
419+
impl<ES: EntropySource, K: KVStore + Clone> LSPSProtocolMessageHandler
420+
for LSPS5ClientHandler<ES, K>
430421
{
431422
type ProtocolMessage = LSPS5Message;
432423
const PROTOCOL_NUMBER: Option<u16> = Some(5);

0 commit comments

Comments
 (0)