Skip to content

Commit e6f5b25

Browse files
committed
Collapse NodeCustomMessageHandler enum into a struct
Remove the `Ignoring` variant now that the liquidity source is always built, so the enum and its match arms are now pure overhead. Replace it with a struct that holds the `LiquiditySource` directly and have each trait method delegate straight to `liquidity_manager()`.
1 parent a55f8b3 commit e6f5b25

2 files changed

Lines changed: 12 additions & 52 deletions

File tree

src/builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1861,7 +1861,7 @@ fn build_with_store_internal(
18611861
let liquidity_source = runtime
18621862
.block_on(async move { liquidity_source_builder.build().await.map(Arc::new) })?;
18631863
let custom_message_handler =
1864-
Arc::new(NodeCustomMessageHandler::new_liquidity(Arc::clone(&liquidity_source)));
1864+
Arc::new(NodeCustomMessageHandler::new(Arc::clone(&liquidity_source)));
18651865

18661866
(liquidity_source, custom_message_handler)
18671867
};

src/message_handler.rs

Lines changed: 11 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,19 @@ use lightning_types::features::{InitFeatures, NodeFeatures};
1818

1919
use crate::liquidity::LiquiditySource;
2020

21-
pub(crate) enum NodeCustomMessageHandler<L: Deref>
21+
pub(crate) struct NodeCustomMessageHandler<L: Deref>
2222
where
2323
L::Target: Logger,
2424
{
25-
Ignoring,
26-
Liquidity { liquidity_source: Arc<LiquiditySource<L>> },
25+
liquidity_source: Arc<LiquiditySource<L>>,
2726
}
2827

2928
impl<L: Deref> NodeCustomMessageHandler<L>
3029
where
3130
L::Target: Logger,
3231
{
33-
pub(crate) fn new_liquidity(liquidity_source: Arc<LiquiditySource<L>>) -> Self {
34-
Self::Liquidity { liquidity_source }
35-
}
36-
37-
pub(crate) fn new_ignoring() -> Self {
38-
Self::Ignoring
32+
pub(crate) fn new(liquidity_source: Arc<LiquiditySource<L>>) -> Self {
33+
Self { liquidity_source }
3934
}
4035
}
4136

@@ -48,12 +43,7 @@ where
4843
fn read<RD: LengthLimitedRead>(
4944
&self, message_type: u16, buffer: &mut RD,
5045
) -> Result<Option<Self::CustomMessage>, lightning::ln::msgs::DecodeError> {
51-
match self {
52-
Self::Ignoring => Ok(None),
53-
Self::Liquidity { liquidity_source, .. } => {
54-
liquidity_source.liquidity_manager().read(message_type, buffer)
55-
},
56-
}
46+
self.liquidity_source.liquidity_manager().read(message_type, buffer)
5747
}
5848
}
5949

@@ -64,58 +54,28 @@ where
6454
fn handle_custom_message(
6555
&self, msg: Self::CustomMessage, sender_node_id: PublicKey,
6656
) -> Result<(), lightning::ln::msgs::LightningError> {
67-
match self {
68-
Self::Ignoring => Ok(()), // Should be unreachable!() as the reader will return `None`
69-
Self::Liquidity { liquidity_source, .. } => {
70-
liquidity_source.liquidity_manager().handle_custom_message(msg, sender_node_id)
71-
},
72-
}
57+
self.liquidity_source.liquidity_manager().handle_custom_message(msg, sender_node_id)
7358
}
7459

7560
fn get_and_clear_pending_msg(&self) -> Vec<(PublicKey, Self::CustomMessage)> {
76-
match self {
77-
Self::Ignoring => Vec::new(),
78-
Self::Liquidity { liquidity_source, .. } => {
79-
liquidity_source.liquidity_manager().get_and_clear_pending_msg()
80-
},
81-
}
61+
self.liquidity_source.liquidity_manager().get_and_clear_pending_msg()
8262
}
8363

8464
fn provided_node_features(&self) -> NodeFeatures {
85-
match self {
86-
Self::Ignoring => NodeFeatures::empty(),
87-
Self::Liquidity { liquidity_source, .. } => {
88-
liquidity_source.liquidity_manager().provided_node_features()
89-
},
90-
}
65+
self.liquidity_source.liquidity_manager().provided_node_features()
9166
}
9267

9368
fn provided_init_features(&self, their_node_id: PublicKey) -> InitFeatures {
94-
match self {
95-
Self::Ignoring => InitFeatures::empty(),
96-
Self::Liquidity { liquidity_source, .. } => {
97-
liquidity_source.liquidity_manager().provided_init_features(their_node_id)
98-
},
99-
}
69+
self.liquidity_source.liquidity_manager().provided_init_features(their_node_id)
10070
}
10171

10272
fn peer_connected(
10373
&self, their_node_id: PublicKey, msg: &lightning::ln::msgs::Init, inbound: bool,
10474
) -> Result<(), ()> {
105-
match self {
106-
Self::Ignoring => Ok(()),
107-
Self::Liquidity { liquidity_source, .. } => {
108-
liquidity_source.liquidity_manager().peer_connected(their_node_id, msg, inbound)
109-
},
110-
}
75+
self.liquidity_source.liquidity_manager().peer_connected(their_node_id, msg, inbound)
11176
}
11277

11378
fn peer_disconnected(&self, their_node_id: PublicKey) {
114-
match self {
115-
Self::Ignoring => {},
116-
Self::Liquidity { liquidity_source, .. } => {
117-
liquidity_source.liquidity_manager().peer_disconnected(their_node_id)
118-
},
119-
}
79+
self.liquidity_source.liquidity_manager().peer_disconnected(their_node_id)
12080
}
12181
}

0 commit comments

Comments
 (0)