-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathmessage_handler.rs
123 lines (106 loc) · 3.39 KB
/
message_handler.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// This file is Copyright its original authors, visible in version control history.
//
// 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 licenses.
use crate::liquidity::LiquiditySource;
use lightning::ln::peer_handler::CustomMessageHandler;
use lightning::ln::wire::CustomMessageReader;
use lightning::util::logger::Logger;
use lightning_types::features::{InitFeatures, NodeFeatures};
use lightning_liquidity::lsps0::ser::RawLSPSMessage;
use bitcoin::secp256k1::PublicKey;
use std::ops::Deref;
use std::sync::Arc;
pub(crate) enum NodeCustomMessageHandler<L: Deref>
where
L::Target: Logger,
{
Ignoring,
Liquidity { liquidity_source: Arc<LiquiditySource<L>> },
}
impl<L: Deref> NodeCustomMessageHandler<L>
where
L::Target: Logger,
{
pub(crate) fn new_liquidity(liquidity_source: Arc<LiquiditySource<L>>) -> Self {
Self::Liquidity { liquidity_source }
}
pub(crate) fn new_ignoring() -> Self {
Self::Ignoring
}
}
impl<L: Deref> CustomMessageReader for NodeCustomMessageHandler<L>
where
L::Target: Logger,
{
type CustomMessage = RawLSPSMessage;
fn read<RD: lightning::io::Read>(
&self, message_type: u16, buffer: &mut RD,
) -> Result<Option<Self::CustomMessage>, lightning::ln::msgs::DecodeError> {
match self {
Self::Ignoring => Ok(None),
Self::Liquidity { liquidity_source, .. } => {
liquidity_source.liquidity_manager().read(message_type, buffer)
},
}
}
}
impl<L: Deref> CustomMessageHandler for NodeCustomMessageHandler<L>
where
L::Target: Logger,
{
fn handle_custom_message(
&self, msg: Self::CustomMessage, sender_node_id: PublicKey,
) -> Result<(), lightning::ln::msgs::LightningError> {
match self {
Self::Ignoring => Ok(()), // Should be unreachable!() as the reader will return `None`
Self::Liquidity { liquidity_source, .. } => {
liquidity_source.liquidity_manager().handle_custom_message(msg, sender_node_id)
},
}
}
fn get_and_clear_pending_msg(&self) -> Vec<(PublicKey, Self::CustomMessage)> {
match self {
Self::Ignoring => Vec::new(),
Self::Liquidity { liquidity_source, .. } => {
liquidity_source.liquidity_manager().get_and_clear_pending_msg()
},
}
}
fn provided_node_features(&self) -> NodeFeatures {
match self {
Self::Ignoring => NodeFeatures::empty(),
Self::Liquidity { liquidity_source, .. } => {
liquidity_source.liquidity_manager().provided_node_features()
},
}
}
fn provided_init_features(&self, their_node_id: PublicKey) -> InitFeatures {
match self {
Self::Ignoring => InitFeatures::empty(),
Self::Liquidity { liquidity_source, .. } => {
liquidity_source.liquidity_manager().provided_init_features(their_node_id)
},
}
}
fn peer_connected(
&self, their_node_id: PublicKey, msg: &lightning::ln::msgs::Init, inbound: bool,
) -> Result<(), ()> {
match self {
Self::Ignoring => Ok(()),
Self::Liquidity { liquidity_source, .. } => {
liquidity_source.liquidity_manager().peer_connected(their_node_id, msg, inbound)
},
}
}
fn peer_disconnected(&self, their_node_id: PublicKey) {
match self {
Self::Ignoring => {},
Self::Liquidity { liquidity_source, .. } => {
liquidity_source.liquidity_manager().peer_disconnected(their_node_id)
},
}
}
}