@@ -33,22 +33,133 @@ use routing::network_graph::NetGraphMsgHandler;
3333use std:: collections:: { HashMap , hash_map, HashSet , LinkedList } ;
3434use std:: sync:: { Arc , Mutex } ;
3535use std:: sync:: atomic:: { AtomicUsize , Ordering } ;
36- use std:: { cmp, error, hash, fmt} ;
36+ use std:: { cmp, error, hash, fmt, mem } ;
3737use std:: ops:: Deref ;
3838
3939use bitcoin:: hashes:: sha256:: Hash as Sha256 ;
4040use bitcoin:: hashes:: sha256:: HashEngine as Sha256Engine ;
4141use 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+ struct IgnoringMessageHandler { }
46+ impl MessageSendEventsProvider for IgnoringMessageHandler {
47+ fn get_and_clear_pending_msg_events ( & self ) -> Vec < MessageSendEvent > { Vec :: new ( ) }
48+ }
49+ impl RoutingMessageHandler for IgnoringMessageHandler {
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 IgnoringMessageHandler {
64+ type Target = IgnoringMessageHandler ;
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+ struct ErroringMessageHandler {
71+ message_queue : Mutex < Vec < MessageSendEvent > >
72+ }
73+ impl ErroringMessageHandler {
74+ /// Constructs a new ErroringMessageHandler
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 ErroringMessageHandler {
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 ErroringMessageHandler {
95+ // Any messages which are related to a specific channel generate an error message to let the
96+ // peer know we don't care about channels.
97+ fn handle_open_channel ( & self , their_node_id : & PublicKey , _their_features : InitFeatures , msg : & msgs:: OpenChannel ) {
98+ ErroringMessageHandler :: push_error ( self , their_node_id, msg. temporary_channel_id ) ;
99+ }
100+ fn handle_accept_channel ( & self , their_node_id : & PublicKey , _their_features : InitFeatures , msg : & msgs:: AcceptChannel ) {
101+ ErroringMessageHandler :: push_error ( self , their_node_id, msg. temporary_channel_id ) ;
102+ }
103+ fn handle_funding_created ( & self , their_node_id : & PublicKey , msg : & msgs:: FundingCreated ) {
104+ ErroringMessageHandler :: push_error ( self , their_node_id, msg. temporary_channel_id ) ;
105+ }
106+ fn handle_funding_signed ( & self , their_node_id : & PublicKey , msg : & msgs:: FundingSigned ) {
107+ ErroringMessageHandler :: push_error ( self , their_node_id, msg. channel_id ) ;
108+ }
109+ fn handle_funding_locked ( & self , their_node_id : & PublicKey , msg : & msgs:: FundingLocked ) {
110+ ErroringMessageHandler :: push_error ( self , their_node_id, msg. channel_id ) ;
111+ }
112+ fn handle_shutdown ( & self , their_node_id : & PublicKey , _their_features : & InitFeatures , msg : & msgs:: Shutdown ) {
113+ ErroringMessageHandler :: push_error ( self , their_node_id, msg. channel_id ) ;
114+ }
115+ fn handle_closing_signed ( & self , their_node_id : & PublicKey , msg : & msgs:: ClosingSigned ) {
116+ ErroringMessageHandler :: push_error ( self , their_node_id, msg. channel_id ) ;
117+ }
118+ fn handle_update_add_htlc ( & self , their_node_id : & PublicKey , msg : & msgs:: UpdateAddHTLC ) {
119+ ErroringMessageHandler :: push_error ( self , their_node_id, msg. channel_id ) ;
120+ }
121+ fn handle_update_fulfill_htlc ( & self , their_node_id : & PublicKey , msg : & msgs:: UpdateFulfillHTLC ) {
122+ ErroringMessageHandler :: push_error ( self , their_node_id, msg. channel_id ) ;
123+ }
124+ fn handle_update_fail_htlc ( & self , their_node_id : & PublicKey , msg : & msgs:: UpdateFailHTLC ) {
125+ ErroringMessageHandler :: push_error ( self , their_node_id, msg. channel_id ) ;
126+ }
127+ fn handle_update_fail_malformed_htlc ( & self , their_node_id : & PublicKey , msg : & msgs:: UpdateFailMalformedHTLC ) {
128+ ErroringMessageHandler :: push_error ( self , their_node_id, msg. channel_id ) ;
129+ }
130+ fn handle_commitment_signed ( & self , their_node_id : & PublicKey , msg : & msgs:: CommitmentSigned ) {
131+ ErroringMessageHandler :: push_error ( self , their_node_id, msg. channel_id ) ;
132+ }
133+ fn handle_revoke_and_ack ( & self , their_node_id : & PublicKey , msg : & msgs:: RevokeAndACK ) {
134+ ErroringMessageHandler :: push_error ( self , their_node_id, msg. channel_id ) ;
135+ }
136+ fn handle_update_fee ( & self , their_node_id : & PublicKey , msg : & msgs:: UpdateFee ) {
137+ ErroringMessageHandler :: push_error ( self , their_node_id, msg. channel_id ) ;
138+ }
139+ fn handle_announcement_signatures ( & self , their_node_id : & PublicKey , msg : & msgs:: AnnouncementSignatures ) {
140+ ErroringMessageHandler :: push_error ( self , their_node_id, msg. channel_id ) ;
141+ }
142+ fn handle_channel_reestablish ( & self , their_node_id : & PublicKey , msg : & msgs:: ChannelReestablish ) {
143+ ErroringMessageHandler :: push_error ( self , their_node_id, msg. channel_id ) ;
144+ }
145+ fn peer_disconnected ( & self , _their_node_id : & PublicKey , _no_connection_possible : bool ) { }
146+ fn peer_connected ( & self , _their_node_id : & PublicKey , _msg : & msgs:: Init ) { }
147+ fn handle_error ( & self , _their_node_id : & PublicKey , _msg : & msgs:: ErrorMessage ) { }
148+ }
149+ impl Deref for ErroringMessageHandler {
150+ type Target = ErroringMessageHandler ;
151+ fn deref ( & self ) -> & Self { self }
152+ }
153+
43154/// Provides references to trait impls which handle different types of messages.
44155pub struct MessageHandler < CM : Deref , RM : Deref > where
45156 CM :: Target : ChannelMessageHandler ,
46157 RM :: Target : RoutingMessageHandler {
47158 /// A message handler which handles messages specific to channels. Usually this is just a
48- /// ChannelManager object.
159+ /// ChannelManager object or a ErroringMessageHandler .
49160 pub chan_handler : CM ,
50161 /// A message handler which handles messages updating our knowledge of the network channel
51- /// graph. Usually this is just a NetGraphMsgHandlerMonitor object.
162+ /// graph. Usually this is just a NetGraphMsgHandlerMonitor object or an IgnoringMessageHandler .
52163 pub route_handler : RM ,
53164}
54165
0 commit comments