-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathmessage_handler.rs
More file actions
81 lines (68 loc) · 2.55 KB
/
Copy pathmessage_handler.rs
File metadata and controls
81 lines (68 loc) · 2.55 KB
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
// 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 std::ops::Deref;
use std::sync::Arc;
use bitcoin::secp256k1::PublicKey;
use lightning::ln::peer_handler::CustomMessageHandler;
use lightning::ln::wire::CustomMessageReader;
use lightning::util::logger::Logger;
use lightning::util::ser::LengthLimitedRead;
use lightning_liquidity::lsps0::ser::RawLSPSMessage;
use lightning_types::features::{InitFeatures, NodeFeatures};
use crate::liquidity::LiquiditySource;
pub(crate) struct NodeCustomMessageHandler<L: Deref>
where
L::Target: Logger,
{
liquidity_source: Arc<LiquiditySource<L>>,
}
impl<L: Deref> NodeCustomMessageHandler<L>
where
L::Target: Logger,
{
pub(crate) fn new(liquidity_source: Arc<LiquiditySource<L>>) -> Self {
Self { liquidity_source }
}
}
impl<L: Deref> CustomMessageReader for NodeCustomMessageHandler<L>
where
L::Target: Logger,
{
type CustomMessage = RawLSPSMessage;
fn read<RD: LengthLimitedRead>(
&self, message_type: u16, buffer: &mut RD,
) -> Result<Option<Self::CustomMessage>, lightning::ln::msgs::DecodeError> {
self.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> {
self.liquidity_source.liquidity_manager().handle_custom_message(msg, sender_node_id)
}
fn get_and_clear_pending_msg(&self) -> Vec<(PublicKey, Self::CustomMessage)> {
self.liquidity_source.liquidity_manager().get_and_clear_pending_msg()
}
fn provided_node_features(&self) -> NodeFeatures {
self.liquidity_source.liquidity_manager().provided_node_features()
}
fn provided_init_features(&self, their_node_id: PublicKey) -> InitFeatures {
self.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<(), ()> {
self.liquidity_source.liquidity_manager().peer_connected(their_node_id, msg, inbound)
}
fn peer_disconnected(&self, their_node_id: PublicKey) {
self.liquidity_source.liquidity_manager().peer_disconnected(their_node_id)
}
}