@@ -39,15 +39,15 @@ use bitcoin::hashes::sha256::Hash as Sha256;
39
39
use bitcoin:: hashes:: sha256:: HashEngine as Sha256Engine ;
40
40
use bitcoin:: hashes:: { HashEngine , Hash } ;
41
41
use ln:: peers:: outbound_queue:: OutboundQueue ;
42
- use ln:: peers:: transport:: { PayloadQueuer , Transport } ;
42
+ use ln:: peers:: transport:: Transport ;
43
43
use std:: collections:: hash_map:: IterMut ;
44
44
use std:: iter:: Filter ;
45
45
46
46
// Number of items that can exist in the OutboundQueue before Sync message flow control is triggered
47
47
const OUTBOUND_QUEUE_SIZE : usize = 10 ;
48
48
49
49
/// Interface PeerManager uses to interact with the Transport object
50
- pub ( super ) trait ITransport : MessageQueuer {
50
+ pub ( super ) trait ITransport {
51
51
/// Instantiate the new outbound Transport
52
52
fn new_outbound ( initiator_static_private_key : & SecretKey , responder_static_public_key : & PublicKey , initiator_ephemeral_private_key : & SecretKey ) -> Self ;
53
53
@@ -59,29 +59,51 @@ pub(super) trait ITransport: MessageQueuer {
59
59
60
60
/// Process input data similar to reading it off a descriptor directly. Returns true on the first call
61
61
/// that results in the transport being newly connected.
62
- fn process_input ( & mut self , input : & [ u8 ] , output_buffer : & mut impl PayloadQueuer ) -> Result < bool , String > ;
62
+ fn process_input ( & mut self , input : & [ u8 ] , outbound_queue : & mut impl IOutboundQueue ) -> Result < bool , String > ;
63
63
64
64
/// Returns true if the connection is established and encrypted messages can be sent.
65
65
fn is_connected ( & self ) -> bool ;
66
66
67
67
/// Returns the node_id of the remote node. Panics if not connected.
68
68
fn get_their_node_id ( & self ) -> PublicKey ;
69
69
70
+ /// Encodes, encrypts, and enqueues a message to the outbound queue. Panics if the connection is
71
+ /// not established yet.
72
+ fn enqueue_message < M : Encode + Writeable , L : Deref > ( & mut self , message : & M , outbound_queue : & mut impl IOutboundQueue , logger : L ) where L :: Target : Logger ;
73
+
70
74
/// Returns all Messages that have been received and can be successfully parsed by the Transport
71
75
fn drain_messages < L : Deref > ( & mut self , logger : L ) -> Result < Vec < Message > , PeerHandleError > where L :: Target : Logger ;
72
76
}
73
77
74
- /// Interface PeerManager uses to queue message to send. Implemented by Transport to handle
75
- /// encryption/decryption post-NOISE.
76
- pub ( super ) trait MessageQueuer {
77
- /// Encodes, encrypts, and enqueues a message to the outbound queue. Panics if the connection is
78
- /// not established yet.
79
- fn enqueue_message < M : Encode + Writeable , Q : PayloadQueuer , L : Deref > ( & mut self , message : & M , output_buffer : & mut Q , logger : L ) where L :: Target : Logger ;
80
- }
78
+ /// The OutboundQueue is a container for unencrypted payloads during the NOISE handshake and
79
+ /// encrypted Messages post-NOISE. This trait abstracts the behavior to push items to a queue, flush
80
+ /// them through a SocketDescriptor, and handle flow control. Each Peer owns a separate OutboundQueue.
81
+ ///
82
+ /// A trait is used to enable tests to use test doubles that implement a subset of the api with
83
+ /// cleaner test validation.
84
+ pub ( super ) trait IOutboundQueue {
85
+
86
+ // ____ _ __ __ _ _ _
87
+ // | _ \ _ _ ___| |__ | \/ | ___| |_| |__ ___ __| |___
88
+ // | |_) | | | / __| '_ \ | |\/| |/ _ \ __| '_ \ / _ \ / _` / __|
89
+ // | __/| |_| \__ \ | | | | | | | __/ |_| | | | (_) | (_| \__ \
90
+ // |_| \__,_|___/_| |_| |_| |_|\___|\__|_| |_|\___/ \__,_|___/
91
+
92
+ /// Unconditionally queue item. May increase queue above soft limit.
93
+ fn push_back ( & mut self , item : Vec < u8 > ) ;
94
+
95
+ /// Returns true if the queue is empty
96
+ fn is_empty ( & self ) -> bool ;
97
+
98
+ /// Returns the amount of free space in the queue before the soft limit
99
+ fn queue_space ( & self ) -> usize ;
100
+
101
+ // _____ _ _ __ __ _ _ _
102
+ // | ___| |_ _ ___| |__ | \/ | ___| |_| |__ ___ __| |___
103
+ // | |_ | | | | / __| '_ \ | |\/| |/ _ \ __| '_ \ / _ \ / _` / __|
104
+ // | _| | | |_| \__ \ | | | | | | | __/ |_| | | | (_) | (_| \__ \
105
+ // |_| |_|\__,_|___/_| |_| |_| |_|\___|\__|_| |_|\___/ \__,_|___/
81
106
82
- /// Trait representing a container that can try to flush data through a SocketDescriptor. Used by the
83
- /// PeerManager to handle flushing the outbound queue and flow control.
84
- pub ( super ) trait SocketDescriptorFlusher {
85
107
/// Write previously enqueued data to the SocketDescriptor. A return of false indicates the
86
108
/// underlying SocketDescriptor could not fulfill the send_data() call and the blocked state
87
109
/// has been set. Use unblock() when the SocketDescriptor may have more room.
@@ -583,11 +605,11 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref, TransportImpl
583
605
584
606
// Fill remaining slots in output queue with sync messages, updating the sync state when
585
607
// appropriate
586
- fn fill_outbound_queue_with_sync < Q : PayloadQueuer + SocketDescriptorFlusher > (
608
+ fn fill_outbound_queue_with_sync (
587
609
& self ,
588
610
sync_status : & mut InitSyncTracker ,
589
- message_queuer : & mut impl MessageQueuer ,
590
- outbound_queue : & mut Q ) {
611
+ transport : & mut TransportImpl ,
612
+ outbound_queue : & mut OutboundQueue ) {
591
613
592
614
let queue_space = outbound_queue. queue_space ( ) ;
593
615
if queue_space > 0 {
@@ -597,12 +619,12 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref, TransportImpl
597
619
let steps = ( ( queue_space + 2 ) / 3 ) as u8 ;
598
620
let all_messages = self . message_handler . route_handler . get_next_channel_announcements ( c, steps) ;
599
621
for & ( ref announce, ref update_a_option, ref update_b_option) in all_messages. iter ( ) {
600
- message_queuer . enqueue_message ( announce, outbound_queue, & * self . logger ) ;
622
+ transport . enqueue_message ( announce, outbound_queue, & * self . logger ) ;
601
623
if let & Some ( ref update_a) = update_a_option {
602
- message_queuer . enqueue_message ( update_a, outbound_queue, & * self . logger ) ;
624
+ transport . enqueue_message ( update_a, outbound_queue, & * self . logger ) ;
603
625
}
604
626
if let & Some ( ref update_b) = update_b_option {
605
- message_queuer . enqueue_message ( update_b, outbound_queue, & * self . logger ) ;
627
+ transport . enqueue_message ( update_b, outbound_queue, & * self . logger ) ;
606
628
}
607
629
* sync_status = InitSyncTracker :: ChannelsSyncing ( announce. contents . short_channel_id + 1 ) ;
608
630
}
@@ -614,7 +636,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref, TransportImpl
614
636
let steps = queue_space as u8 ;
615
637
let all_messages = self . message_handler . route_handler . get_next_node_announcements ( None , steps) ;
616
638
for msg in all_messages. iter ( ) {
617
- message_queuer . enqueue_message ( msg, outbound_queue, & * self . logger ) ;
639
+ transport . enqueue_message ( msg, outbound_queue, & * self . logger ) ;
618
640
* sync_status = InitSyncTracker :: NodesSyncing ( msg. contents . node_id ) ;
619
641
}
620
642
if all_messages. is_empty ( ) || all_messages. len ( ) != steps as usize {
@@ -626,7 +648,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref, TransportImpl
626
648
let steps = queue_space as u8 ;
627
649
let all_messages = self . message_handler . route_handler . get_next_node_announcements ( Some ( & key) , steps) ;
628
650
for msg in all_messages. iter ( ) {
629
- message_queuer . enqueue_message ( msg, outbound_queue, & * self . logger ) ;
651
+ transport . enqueue_message ( msg, outbound_queue, & * self . logger ) ;
630
652
* sync_status = InitSyncTracker :: NodesSyncing ( msg. contents . node_id ) ;
631
653
}
632
654
if all_messages. is_empty ( ) || all_messages. len ( ) != steps as usize {
@@ -637,18 +659,18 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref, TransportImpl
637
659
}
638
660
}
639
661
640
- fn do_attempt_write_data < Q : PayloadQueuer + SocketDescriptorFlusher > (
662
+ fn do_attempt_write_data (
641
663
& self ,
642
664
descriptor : & mut Descriptor ,
643
665
post_init_state : & mut Option < PostInitState > ,
644
- message_queuer : & mut impl MessageQueuer ,
645
- outbound_queue : & mut Q ) {
666
+ transport : & mut TransportImpl ,
667
+ outbound_queue : & mut OutboundQueue ) {
646
668
647
669
while !outbound_queue. is_blocked ( ) {
648
670
// If connected, fill output queue with sync messages
649
671
match post_init_state {
650
672
None => { } ,
651
- & mut Some ( ref mut state) => self . fill_outbound_queue_with_sync ( & mut state. sync_status , message_queuer , outbound_queue)
673
+ & mut Some ( ref mut state) => self . fill_outbound_queue_with_sync ( & mut state. sync_status , transport , outbound_queue)
652
674
}
653
675
654
676
// No messages to send
0 commit comments