Skip to content

Commit c70e7c1

Browse files
committed
Provide Dummy routing and channel message handlers for users
We currently "support" not having a router or channel in memory by forcing users to implement the same, but its trivial to provide our own dummy implementations.
1 parent 9fba7c9 commit c70e7c1

File tree

1 file changed

+112
-3
lines changed

1 file changed

+112
-3
lines changed

lightning/src/ln/peer_handler.rs

Lines changed: 112 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,131 @@ use routing::network_graph::NetGraphMsgHandler;
3333
use std::collections::{HashMap,hash_map,HashSet,LinkedList};
3434
use std::sync::{Arc, Mutex};
3535
use std::sync::atomic::{AtomicUsize, Ordering};
36-
use std::{cmp,error,hash,fmt};
36+
use std::{cmp,error,hash,fmt,mem};
3737
use std::ops::Deref;
3838

3939
use bitcoin::hashes::sha256::Hash as Sha256;
4040
use bitcoin::hashes::sha256::HashEngine as Sha256Engine;
4141
use bitcoin::hashes::{HashEngine, Hash};
4242

43+
/// A dummy struct which implements `RoutingMessageHandler` without storing any routing information
44+
/// or doing any processing. You can provide one of these as the route_handler in a MessageHandler.
45+
pub struct DummyRouteHandler{}
46+
impl MessageSendEventsProvider for DummyRouteHandler {
47+
fn get_and_clear_pending_msg_events(&self) -> Vec<MessageSendEvent> { Vec::new() }
48+
}
49+
impl RoutingMessageHandler for DummyRouteHandler {
50+
fn handle_node_announcement(&self, _msg: &msgs::NodeAnnouncement) -> Result<bool, LightningError> { Ok(false) }
51+
fn handle_channel_announcement(&self, _msg: &msgs::ChannelAnnouncement) -> Result<bool, LightningError> { Ok(false) }
52+
fn handle_channel_update(&self, _msg: &msgs::ChannelUpdate) -> Result<bool, LightningError> { Ok(false) }
53+
fn handle_htlc_fail_channel_update(&self, _update: &msgs::HTLCFailChannelUpdate) {}
54+
fn get_next_channel_announcements(&self, _starting_point: u64, _batch_amount: u8) ->
55+
Vec<(msgs::ChannelAnnouncement, Option<msgs::ChannelUpdate>, Option<msgs::ChannelUpdate>)> { Vec::new() }
56+
fn get_next_node_announcements(&self, _starting_point: Option<&PublicKey>, _batch_amount: u8) -> Vec<msgs::NodeAnnouncement> { Vec::new() }
57+
fn sync_routing_table(&self, _their_node_id: &PublicKey, _init: &msgs::Init) {}
58+
fn handle_reply_channel_range(&self, _their_node_id: &PublicKey, _msg: msgs::ReplyChannelRange) -> Result<(), LightningError> { Ok(()) }
59+
fn handle_reply_short_channel_ids_end(&self, _their_node_id: &PublicKey, _msg: msgs::ReplyShortChannelIdsEnd) -> Result<(), LightningError> { Ok(()) }
60+
fn handle_query_channel_range(&self, _their_node_id: &PublicKey, _msg: msgs::QueryChannelRange) -> Result<(), LightningError> { Ok(()) }
61+
fn handle_query_short_channel_ids(&self, _their_node_id: &PublicKey, _msg: msgs::QueryShortChannelIds) -> Result<(), LightningError> { Ok(()) }
62+
}
63+
impl Deref for DummyRouteHandler {
64+
type Target = DummyRouteHandler;
65+
fn deref(&self) -> &Self { self }
66+
}
67+
68+
/// A dummy struct which implements `ChannelMessageHandler` without having any channels.
69+
/// You can provide one of these as the route_handler in a MessageHandler.
70+
pub struct DummyChannelHandler {
71+
message_queue: Mutex<Vec<MessageSendEvent>>
72+
}
73+
impl DummyChannelHandler {
74+
/// Constructs a new DummyChannelHandler
75+
pub fn new() -> Self {
76+
Self { message_queue: Mutex::new(Vec::new()) }
77+
}
78+
fn push_error(&self, node_id: &PublicKey, channel_id: [u8; 32]) {
79+
self.message_queue.lock().unwrap().push(MessageSendEvent::HandleError {
80+
action: msgs::ErrorAction::SendErrorMessage {
81+
msg: msgs::ErrorMessage { channel_id, data: "We do not support channel messages, sorry.".to_owned() },
82+
},
83+
node_id: node_id.clone(),
84+
});
85+
}
86+
}
87+
impl MessageSendEventsProvider for DummyChannelHandler {
88+
fn get_and_clear_pending_msg_events(&self) -> Vec<MessageSendEvent> {
89+
let mut res = Vec::new();
90+
mem::swap(&mut res, &mut self.message_queue.lock().unwrap());
91+
res
92+
}
93+
}
94+
impl ChannelMessageHandler for DummyChannelHandler {
95+
fn handle_open_channel(&self, their_node_id: &PublicKey, _their_features: InitFeatures, msg: &msgs::OpenChannel) {
96+
DummyChannelHandler::push_error(self, their_node_id, msg.temporary_channel_id);
97+
}
98+
fn handle_accept_channel(&self, their_node_id: &PublicKey, _their_features: InitFeatures, msg: &msgs::AcceptChannel) {
99+
DummyChannelHandler::push_error(self, their_node_id, msg.temporary_channel_id);
100+
}
101+
fn handle_funding_created(&self, their_node_id: &PublicKey, msg: &msgs::FundingCreated) {
102+
DummyChannelHandler::push_error(self, their_node_id, msg.temporary_channel_id);
103+
}
104+
fn handle_funding_signed(&self, their_node_id: &PublicKey, msg: &msgs::FundingSigned) {
105+
DummyChannelHandler::push_error(self, their_node_id, msg.channel_id);
106+
}
107+
fn handle_funding_locked(&self, their_node_id: &PublicKey, msg: &msgs::FundingLocked) {
108+
DummyChannelHandler::push_error(self, their_node_id, msg.channel_id);
109+
}
110+
fn handle_shutdown(&self, their_node_id: &PublicKey, _their_features: &InitFeatures, msg: &msgs::Shutdown) {
111+
DummyChannelHandler::push_error(self, their_node_id, msg.channel_id);
112+
}
113+
fn handle_closing_signed(&self, their_node_id: &PublicKey, msg: &msgs::ClosingSigned) {
114+
DummyChannelHandler::push_error(self, their_node_id, msg.channel_id);
115+
}
116+
fn handle_update_add_htlc(&self, their_node_id: &PublicKey, msg: &msgs::UpdateAddHTLC) {
117+
DummyChannelHandler::push_error(self, their_node_id, msg.channel_id);
118+
}
119+
fn handle_update_fulfill_htlc(&self, their_node_id: &PublicKey, msg: &msgs::UpdateFulfillHTLC) {
120+
DummyChannelHandler::push_error(self, their_node_id, msg.channel_id);
121+
}
122+
fn handle_update_fail_htlc(&self, their_node_id: &PublicKey, msg: &msgs::UpdateFailHTLC) {
123+
DummyChannelHandler::push_error(self, their_node_id, msg.channel_id);
124+
}
125+
fn handle_update_fail_malformed_htlc(&self, their_node_id: &PublicKey, msg: &msgs::UpdateFailMalformedHTLC) {
126+
DummyChannelHandler::push_error(self, their_node_id, msg.channel_id);
127+
}
128+
fn handle_commitment_signed(&self, their_node_id: &PublicKey, msg: &msgs::CommitmentSigned) {
129+
DummyChannelHandler::push_error(self, their_node_id, msg.channel_id);
130+
}
131+
fn handle_revoke_and_ack(&self, their_node_id: &PublicKey, msg: &msgs::RevokeAndACK) {
132+
DummyChannelHandler::push_error(self, their_node_id, msg.channel_id);
133+
}
134+
fn handle_update_fee(&self, their_node_id: &PublicKey, msg: &msgs::UpdateFee) {
135+
DummyChannelHandler::push_error(self, their_node_id, msg.channel_id);
136+
}
137+
fn handle_announcement_signatures(&self, their_node_id: &PublicKey, msg: &msgs::AnnouncementSignatures) {
138+
DummyChannelHandler::push_error(self, their_node_id, msg.channel_id);
139+
}
140+
fn peer_disconnected(&self, _their_node_id: &PublicKey, _no_connection_possible: bool) {}
141+
fn peer_connected(&self, _their_node_id: &PublicKey, _msg: &msgs::Init) {}
142+
fn handle_channel_reestablish(&self, their_node_id: &PublicKey, msg: &msgs::ChannelReestablish) {
143+
DummyChannelHandler::push_error(self, their_node_id, msg.channel_id);
144+
}
145+
fn handle_error(&self, _their_node_id: &PublicKey, _msg: &msgs::ErrorMessage) {}
146+
}
147+
impl Deref for DummyChannelHandler {
148+
type Target = DummyChannelHandler;
149+
fn deref(&self) -> &Self { self }
150+
}
151+
43152
/// Provides references to trait impls which handle different types of messages.
44153
pub struct MessageHandler<CM: Deref, RM: Deref> where
45154
CM::Target: ChannelMessageHandler,
46155
RM::Target: RoutingMessageHandler {
47156
/// A message handler which handles messages specific to channels. Usually this is just a
48-
/// ChannelManager object.
157+
/// ChannelManager object or a DummyChannelHandler.
49158
pub chan_handler: CM,
50159
/// A message handler which handles messages updating our knowledge of the network channel
51-
/// graph. Usually this is just a NetGraphMsgHandlerMonitor object.
160+
/// graph. Usually this is just a NetGraphMsgHandlerMonitor object or a DummyRouteHandler.
52161
pub route_handler: RM,
53162
}
54163

0 commit comments

Comments
 (0)