Skip to content

Commit 3e89106

Browse files
yuntaiTheBlueMatt
authored andcommitted
migrate peer_handler to use Writeable
added inplace byte_utils
1 parent 63cb37d commit 3e89106

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed

src/ln/peer_handler.rs

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use secp256k1::key::{SecretKey,PublicKey};
22

33
use ln::msgs;
4-
use ln::msgs::{MsgEncodable,MsgDecodable};
4+
use util::ser::{Writer, Reader, Writeable, Readable};
55
use ln::peer_channel_encryptor::{PeerChannelEncryptor,NextNoiseStep};
66
use util::byte_utils;
77
use util::events::{EventsProvider,Event};
@@ -113,15 +113,15 @@ pub struct PeerManager<Descriptor: SocketDescriptor> {
113113
}
114114

115115
macro_rules! encode_msg {
116-
($msg: expr, $msg_code: expr) => {
117-
{
118-
let just_msg = $msg.encode();
119-
let mut encoded_msg = Vec::with_capacity(just_msg.len() + 2);
120-
encoded_msg.extend_from_slice(&byte_utils::be16_to_array($msg_code));
121-
encoded_msg.extend_from_slice(&just_msg[..]);
122-
encoded_msg
123-
}
124-
}
116+
($msg: expr, $msg_code: expr) => {{
117+
let mut w = Writer::new(::std::io::Cursor::new(vec![]));
118+
0u16.write(&mut w).unwrap();
119+
$msg.write(&mut w).unwrap();
120+
let mut msg = w.into_inner().into_inner();
121+
let len = msg.len();
122+
msg[..2].copy_from_slice(&byte_utils::be16_to_array(len as u16 - 2));
123+
msg
124+
}}
125125
}
126126

127127
//TODO: Really should do something smarter for this
@@ -365,7 +365,6 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
365365
msgs::DecodeError::BadLengthDescriptor => return Err(PeerHandleError{ no_connection_possible: false }),
366366
msgs::DecodeError::Io(_) => return Err(PeerHandleError{ no_connection_possible: false }),
367367
msgs::DecodeError::InvalidValue => return Err(PeerHandleError{ no_connection_possible: false }),
368-
msgs::DecodeError::InvalidLength => return Err(PeerHandleError{ no_connection_possible: false }),
369368
}
370369
}
371370
};
@@ -438,10 +437,11 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
438437
// Need an init message as first message
439438
return Err(PeerHandleError{ no_connection_possible: false });
440439
}
440+
let mut reader = Reader::new(::std::io::Cursor::new(&msg_data[2..]));
441441
match msg_type {
442442
// Connection control:
443443
16 => {
444-
let msg = try_potential_decodeerror!(msgs::Init::decode(&msg_data[2..]));
444+
let msg = try_potential_decodeerror!(msgs::Init::read(&mut reader));
445445
if msg.global_features.requires_unknown_bits() {
446446
return Err(PeerHandleError{ no_connection_possible: true });
447447
}
@@ -467,7 +467,7 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
467467
}
468468
},
469469
17 => {
470-
let msg = try_potential_decodeerror!(msgs::ErrorMessage::decode(&msg_data[2..]));
470+
let msg = try_potential_decodeerror!(msgs::ErrorMessage::read(&mut reader));
471471
let mut data_is_printable = true;
472472
for b in msg.data.bytes() {
473473
if b < 32 || b > 126 {
@@ -488,38 +488,38 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
488488
},
489489

490490
18 => {
491-
let msg = try_potential_decodeerror!(msgs::Ping::decode(&msg_data[2..]));
491+
let msg = try_potential_decodeerror!(msgs::Ping::read(&mut reader));
492492
if msg.ponglen < 65532 {
493493
let resp = msgs::Pong { byteslen: msg.ponglen };
494494
encode_and_send_msg!(resp, 19);
495495
}
496496
},
497497
19 => {
498-
try_potential_decodeerror!(msgs::Pong::decode(&msg_data[2..]));
498+
try_potential_decodeerror!(msgs::Pong::read(&mut reader));
499499
},
500500

501501
// Channel control:
502502
32 => {
503-
let msg = try_potential_decodeerror!(msgs::OpenChannel::decode(&msg_data[2..]));
503+
let msg = try_potential_decodeerror!(msgs::OpenChannel::read(&mut reader));
504504
let resp = try_potential_handleerror!(self.message_handler.chan_handler.handle_open_channel(&peer.their_node_id.unwrap(), &msg));
505505
encode_and_send_msg!(resp, 33);
506506
},
507507
33 => {
508-
let msg = try_potential_decodeerror!(msgs::AcceptChannel::decode(&msg_data[2..]));
508+
let msg = try_potential_decodeerror!(msgs::AcceptChannel::read(&mut reader));
509509
try_potential_handleerror!(self.message_handler.chan_handler.handle_accept_channel(&peer.their_node_id.unwrap(), &msg));
510510
},
511511

512512
34 => {
513-
let msg = try_potential_decodeerror!(msgs::FundingCreated::decode(&msg_data[2..]));
513+
let msg = try_potential_decodeerror!(msgs::FundingCreated::read(&mut reader));
514514
let resp = try_potential_handleerror!(self.message_handler.chan_handler.handle_funding_created(&peer.their_node_id.unwrap(), &msg));
515515
encode_and_send_msg!(resp, 35);
516516
},
517517
35 => {
518-
let msg = try_potential_decodeerror!(msgs::FundingSigned::decode(&msg_data[2..]));
518+
let msg = try_potential_decodeerror!(msgs::FundingSigned::read(&mut reader));
519519
try_potential_handleerror!(self.message_handler.chan_handler.handle_funding_signed(&peer.their_node_id.unwrap(), &msg));
520520
},
521521
36 => {
522-
let msg = try_potential_decodeerror!(msgs::FundingLocked::decode(&msg_data[2..]));
522+
let msg = try_potential_decodeerror!(msgs::FundingLocked::read(&mut reader));
523523
let resp_option = try_potential_handleerror!(self.message_handler.chan_handler.handle_funding_locked(&peer.their_node_id.unwrap(), &msg));
524524
match resp_option {
525525
Some(resp) => encode_and_send_msg!(resp, 259),
@@ -528,7 +528,7 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
528528
},
529529

530530
38 => {
531-
let msg = try_potential_decodeerror!(msgs::Shutdown::decode(&msg_data[2..]));
531+
let msg = try_potential_decodeerror!(msgs::Shutdown::read(&mut reader));
532532
let resp_options = try_potential_handleerror!(self.message_handler.chan_handler.handle_shutdown(&peer.their_node_id.unwrap(), &msg));
533533
if let Some(resp) = resp_options.0 {
534534
encode_and_send_msg!(resp, 38);
@@ -538,43 +538,43 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
538538
}
539539
},
540540
39 => {
541-
let msg = try_potential_decodeerror!(msgs::ClosingSigned::decode(&msg_data[2..]));
541+
let msg = try_potential_decodeerror!(msgs::ClosingSigned::read(&mut reader));
542542
let resp_option = try_potential_handleerror!(self.message_handler.chan_handler.handle_closing_signed(&peer.their_node_id.unwrap(), &msg));
543543
if let Some(resp) = resp_option {
544544
encode_and_send_msg!(resp, 39);
545545
}
546546
},
547547

548548
128 => {
549-
let msg = try_potential_decodeerror!(msgs::UpdateAddHTLC::decode(&msg_data[2..]));
549+
let msg = try_potential_decodeerror!(msgs::UpdateAddHTLC::read(&mut reader));
550550
try_potential_handleerror!(self.message_handler.chan_handler.handle_update_add_htlc(&peer.their_node_id.unwrap(), &msg));
551551
},
552552
130 => {
553-
let msg = try_potential_decodeerror!(msgs::UpdateFulfillHTLC::decode(&msg_data[2..]));
553+
let msg = try_potential_decodeerror!(msgs::UpdateFulfillHTLC::read(&mut reader));
554554
try_potential_handleerror!(self.message_handler.chan_handler.handle_update_fulfill_htlc(&peer.their_node_id.unwrap(), &msg));
555555
},
556556
131 => {
557-
let msg = try_potential_decodeerror!(msgs::UpdateFailHTLC::decode(&msg_data[2..]));
557+
let msg = try_potential_decodeerror!(msgs::UpdateFailHTLC::read(&mut reader));
558558
let chan_update = try_potential_handleerror!(self.message_handler.chan_handler.handle_update_fail_htlc(&peer.their_node_id.unwrap(), &msg));
559559
if let Some(update) = chan_update {
560560
self.message_handler.route_handler.handle_htlc_fail_channel_update(&update);
561561
}
562562
},
563563
135 => {
564-
let msg = try_potential_decodeerror!(msgs::UpdateFailMalformedHTLC::decode(&msg_data[2..]));
564+
let msg = try_potential_decodeerror!(msgs::UpdateFailMalformedHTLC::read(&mut reader));
565565
try_potential_handleerror!(self.message_handler.chan_handler.handle_update_fail_malformed_htlc(&peer.their_node_id.unwrap(), &msg));
566566
},
567567

568568
132 => {
569-
let msg = try_potential_decodeerror!(msgs::CommitmentSigned::decode(&msg_data[2..]));
569+
let msg = try_potential_decodeerror!(msgs::CommitmentSigned::read(&mut reader));
570570
let resps = try_potential_handleerror!(self.message_handler.chan_handler.handle_commitment_signed(&peer.their_node_id.unwrap(), &msg));
571571
encode_and_send_msg!(resps.0, 133);
572572
if let Some(resp) = resps.1 {
573573
encode_and_send_msg!(resp, 132);
574574
}
575575
},
576576
133 => {
577-
let msg = try_potential_decodeerror!(msgs::RevokeAndACK::decode(&msg_data[2..]));
577+
let msg = try_potential_decodeerror!(msgs::RevokeAndACK::read(&mut reader));
578578
let resp_option = try_potential_handleerror!(self.message_handler.chan_handler.handle_revoke_and_ack(&peer.their_node_id.unwrap(), &msg));
579579
match resp_option {
580580
Some(resps) => {
@@ -593,34 +593,34 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
593593
}
594594
},
595595
134 => {
596-
let msg = try_potential_decodeerror!(msgs::UpdateFee::decode(&msg_data[2..]));
596+
let msg = try_potential_decodeerror!(msgs::UpdateFee::read(&mut reader));
597597
try_potential_handleerror!(self.message_handler.chan_handler.handle_update_fee(&peer.their_node_id.unwrap(), &msg));
598598
},
599599
136 => { }, // TODO: channel_reestablish
600600

601601
// Routing control:
602602
259 => {
603-
let msg = try_potential_decodeerror!(msgs::AnnouncementSignatures::decode(&msg_data[2..]));
603+
let msg = try_potential_decodeerror!(msgs::AnnouncementSignatures::read(&mut reader));
604604
try_potential_handleerror!(self.message_handler.chan_handler.handle_announcement_signatures(&peer.their_node_id.unwrap(), &msg));
605605
},
606606
256 => {
607-
let msg = try_potential_decodeerror!(msgs::ChannelAnnouncement::decode(&msg_data[2..]));
607+
let msg = try_potential_decodeerror!(msgs::ChannelAnnouncement::read(&mut reader));
608608
let should_forward = try_potential_handleerror!(self.message_handler.route_handler.handle_channel_announcement(&msg));
609609

610610
if should_forward {
611611
// TODO: forward msg along to all our other peers!
612612
}
613613
},
614614
257 => {
615-
let msg = try_potential_decodeerror!(msgs::NodeAnnouncement::decode(&msg_data[2..]));
615+
let msg = try_potential_decodeerror!(msgs::NodeAnnouncement::read(&mut reader));
616616
let should_forward = try_potential_handleerror!(self.message_handler.route_handler.handle_node_announcement(&msg));
617617

618618
if should_forward {
619619
// TODO: forward msg along to all our other peers!
620620
}
621621
},
622622
258 => {
623-
let msg = try_potential_decodeerror!(msgs::ChannelUpdate::decode(&msg_data[2..]));
623+
let msg = try_potential_decodeerror!(msgs::ChannelUpdate::read(&mut reader));
624624
let should_forward = try_potential_handleerror!(self.message_handler.route_handler.handle_channel_update(&msg));
625625

626626
if should_forward {

0 commit comments

Comments
 (0)