Skip to content

Commit fbcb350

Browse files
committed
respond to Jeff's comments and add node pubkeys to map
1 parent e7fff85 commit fbcb350

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

lightning/src/ln/peer_handler.rs

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,16 @@ enum InitSyncTracker{
108108
}
109109

110110
enum PeerState {
111-
Authenticating(PeerHandshake),
111+
Handshake(PeerHandshake),
112112
Connected(Conduit),
113113
}
114114

115115
impl PeerState {
116116
fn is_ready_for_encryption(&self) -> bool {
117-
match self {
118-
&PeerState::Connected(_) => true,
119-
_ => false
117+
if let &PeerState::Connected(_) = self {
118+
true
119+
} else {
120+
false
120121
}
121122
}
122123
}
@@ -282,7 +283,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> where
282283

283284
let mut peers = self.peers.lock().unwrap();
284285
if peers.peers.insert(descriptor, Peer {
285-
encryptor: PeerState::Authenticating(handshake),
286+
encryptor: PeerState::Handshake(handshake),
286287
outbound: true,
287288
their_node_id: Some(their_node_id.clone()),
288289
their_features: None,
@@ -317,7 +318,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> where
317318

318319
let mut peers = self.peers.lock().unwrap();
319320
if peers.peers.insert(descriptor, Peer {
320-
encryptor: PeerState::Authenticating(handshake),
321+
encryptor: PeerState::Handshake(handshake),
321322
outbound: false,
322323
their_node_id: None,
323324
their_features: None,
@@ -468,9 +469,9 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> where
468469
let mut conduit_option = None;
469470
let mut remote_pubkey_option = None;
470471

471-
if let &mut PeerState::Authenticating(ref mut handshake) = &mut peer.encryptor {
472+
if let &mut PeerState::Handshake(ref mut handshake) = &mut peer.encryptor {
472473
let (response, conduit, remote_pubkey) = handshake.process_act(&peer.pending_read_buffer, None).unwrap();
473-
peer.pending_read_buffer = Vec::new(); // empty the pending read buffer
474+
peer.pending_read_buffer.drain(..); // we read it all
474475

475476
if let Some(key) = remote_pubkey {
476477
remote_pubkey_option = Some(key);
@@ -489,6 +490,19 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> where
489490
if let Some(conduit) = conduit_option {
490491
// Rust 1.22 does not allow assignment in a borrowed context, even if mutable
491492
peer.encryptor = PeerState::Connected(conduit);
493+
494+
// the handshake has finished, so one way or another, we now have their node id
495+
match peers.node_id_to_descriptor.entry(peer.their_node_id.unwrap()) {
496+
hash_map::Entry::Occupied(_) => {
497+
log_trace!(self, "Got second connection with {}, closing", log_pubkey!(peer.their_node_id.unwrap()));
498+
peer.their_node_id = None; // Unset so that we don't generate a peer_disconnected event
499+
return Err(PeerHandleError{ no_connection_possible: false })
500+
},
501+
hash_map::Entry::Vacant(entry) => {
502+
log_trace!(self, "Finished noise handshake for connection with {}", log_pubkey!(peer.their_node_id.unwrap()));
503+
entry.insert(peer_descriptor.clone())
504+
},
505+
};
492506
}
493507

494508
if let &mut PeerState::Connected(ref mut conduit) = &mut peer.encryptor {
@@ -530,12 +544,18 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> where
530544
}
531545
}
532546

533-
let message_option = conduit.decrypt_single_message(Some(&peer.pending_read_buffer));
534-
let msg_data = if let Some(message) = message_option {
535-
message
536-
} else {
547+
let next_message_result = conduit.decrypt(&peer.pending_read_buffer);
548+
549+
let offset = next_message_result.1;
550+
if offset == 0 {
551+
// nothing got read
537552
break;
538-
};
553+
}else{
554+
peer.pending_read_buffer.drain(0..offset);
555+
}
556+
557+
// something got read, so we definitely have a message
558+
let msg_data = next_message_result.0.unwrap();
539559

540560
let mut reader = ::std::io::Cursor::new(&msg_data[..]);
541561
let message_result = wire::read(&mut reader);

lightning/src/ln/peers/conduit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl Conduit {
4242
}
4343

4444
pub(super) fn read(&mut self, data: &[u8]) {
45-
let mut read_buffer = self.read_buffer.get_or_insert(Vec::new());
45+
let read_buffer = self.read_buffer.get_or_insert(Vec::new());
4646
read_buffer.extend_from_slice(data);
4747
}
4848

0 commit comments

Comments
 (0)