Skip to content

Commit 7f09475

Browse files
committed
might Rust 1.22 finally work? Rearranged a lot of borrowing locations and macro behavior and definition locations
1 parent 32bd555 commit 7f09475

File tree

5 files changed

+54
-43
lines changed

5 files changed

+54
-43
lines changed

lightning/src/ln/peer_handler.rs

Lines changed: 46 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -465,44 +465,6 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> where
465465
peer.pending_read_buffer.extend_from_slice(&data);
466466
while peer.pending_read_buffer.len() > 0 {
467467

468-
macro_rules! encode_and_send_msg {
469-
($msg: expr) => {
470-
{
471-
log_trace!(self, "Encoding and sending message of type {} to {}", $msg.type_id(), log_pubkey!(peer.their_node_id.unwrap()));
472-
if let PeerState::Connected(ref mut conduit) = peer.encryptor {
473-
peer.pending_outbound_buffer.push_back(conduit.encrypt(&encode_msg!(&$msg)[..]));
474-
}
475-
peers.peers_needing_send.insert(peer_descriptor.clone());
476-
}
477-
}
478-
}
479-
480-
macro_rules! try_potential_handleerror {
481-
($thing: expr) => {
482-
match $thing {
483-
Ok(x) => x,
484-
Err(e) => {
485-
match e.action {
486-
msgs::ErrorAction::DisconnectPeer { msg: _ } => {
487-
//TODO: Try to push msg
488-
log_trace!(self, "Got Err handling message, disconnecting peer because {}", e.err);
489-
return Err(PeerHandleError{ no_connection_possible: false });
490-
},
491-
msgs::ErrorAction::IgnoreError => {
492-
log_trace!(self, "Got Err handling message, ignoring because {}", e.err);
493-
continue;
494-
},
495-
msgs::ErrorAction::SendErrorMessage { msg } => {
496-
log_trace!(self, "Got Err handling message, sending Error message because {}", e.err);
497-
encode_and_send_msg!(msg);
498-
continue;
499-
},
500-
}
501-
}
502-
};
503-
}
504-
}
505-
506468
macro_rules! insert_node_id {
507469
() => {
508470
match peers.node_id_to_descriptor.entry(peer.their_node_id.unwrap()) {
@@ -519,17 +481,62 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> where
519481
}
520482
}
521483

484+
let mut conduit_option = None;
485+
522486
if let &mut PeerState::Handshake(ref mut handshake) = &mut peer.encryptor {
523487
let (response, conduit, remote_pubkey) = handshake.process_act(&peer.pending_read_buffer, None).unwrap();
524488
peer.pending_read_buffer.drain(..); // we read it all
525489

526490
peer.pending_outbound_buffer.push_back(response);
527491
if let Some(conduit) = conduit {
528-
peer.encryptor = PeerState::Connected(conduit);
492+
conduit_option = Some(conduit);
529493
}
530494
}
531495

496+
if let Some(conduit) = conduit_option {
497+
// Rust 1.22 does not allow assignment in a borrowed context, even if mutable
498+
peer.encryptor = PeerState::Connected(conduit);
499+
}
500+
532501
if let &mut PeerState::Connected(ref mut conduit) = &mut peer.encryptor {
502+
503+
macro_rules! encode_and_send_msg {
504+
($msg: expr) => {
505+
{
506+
log_trace!(self, "Encoding and sending message of type {} to {}", $msg.type_id(), log_pubkey!(peer.their_node_id.unwrap()));
507+
// we are in a context where conduit is known
508+
peer.pending_outbound_buffer.push_back(conduit.encrypt(&encode_msg!(&$msg)[..]));
509+
peers.peers_needing_send.insert(peer_descriptor.clone());
510+
}
511+
}
512+
}
513+
514+
macro_rules! try_potential_handleerror {
515+
($thing: expr) => {
516+
match $thing {
517+
Ok(x) => x,
518+
Err(e) => {
519+
match e.action {
520+
msgs::ErrorAction::DisconnectPeer { msg: _ } => {
521+
//TODO: Try to push msg
522+
log_trace!(self, "Got Err handling message, disconnecting peer because {}", e.err);
523+
return Err(PeerHandleError{ no_connection_possible: false });
524+
},
525+
msgs::ErrorAction::IgnoreError => {
526+
log_trace!(self, "Got Err handling message, ignoring because {}", e.err);
527+
continue;
528+
},
529+
msgs::ErrorAction::SendErrorMessage { msg } => {
530+
log_trace!(self, "Got Err handling message, sending Error message because {}", e.err);
531+
encode_and_send_msg!(msg);
532+
continue;
533+
},
534+
}
535+
}
536+
};
537+
}
538+
}
539+
533540
let mut messages = conduit.decrypt_message_stream(Some(&peer.pending_read_buffer));
534541

535542
if messages.len() != 1 {

lightning/src/ln/peers/conduit.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ impl Conduit {
127127

128128
#[cfg(test)]
129129
mod tests {
130+
use hex;
130131
use ln::peers::conduit::Conduit;
131132

132133
#[test]

lightning/src/ln/peers/handshake/acts.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ pub enum Act {
2323
impl Act {
2424
/// Convert any act into a byte vector
2525
pub fn serialize(&self) -> Vec<u8> {
26-
match &self {
27-
Act::One(ref act) => {
26+
match self {
27+
&Act::One(ref act) => {
2828
act.0.to_vec()
2929
}
30-
Act::Two(ref act) => {
30+
&Act::Two(ref act) => {
3131
act.0.to_vec()
3232
}
33-
Act::Three(ref act) => {
33+
&Act::Three(ref act) => {
3434
act.0.to_vec()
3535
}
3636
}

lightning/src/ln/peers/handshake/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
//! Handshake states can be advanced automatically, or by manually calling the appropriate step.
33
//! Once complete, returns an instance of Conduit.
44
5+
use secp256k1;
6+
57
use bitcoin_hashes::{Hash, HashEngine};
68
use bitcoin_hashes::sha256::Hash as Sha256;
79
use secp256k1::{PublicKey, SecretKey};

lightning/src/ln/peers/handshake/tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![cfg(test)]
22

3+
use secp256k1;
34
use secp256k1::key::{PublicKey, SecretKey};
45

56
use ln::peers::handshake::PeerHandshake;

0 commit comments

Comments
 (0)