]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Fix peer_handler message serialization 2018-09-ser-rework
authorMatt Corallo <git@bluematt.me>
Sun, 16 Sep 2018 17:16:43 +0000 (13:16 -0400)
committerMatt Corallo <git@bluematt.me>
Sun, 16 Sep 2018 18:33:50 +0000 (14:33 -0400)
Fixes a bug introduced in 3e89106163b941aa4dc4ff92bdb484b7bbcf50c4
where messages were being encoded with their size instead of their
type.
Also utilizes the new size_hinting in peer_handler

src/ln/peer_handler.rs

index 52abecb4bc5f3128827e8cc3a259898c675be56e..01416568b468f404e898cd9b718f17d539835543 100644 (file)
@@ -1,7 +1,7 @@
 use secp256k1::key::{SecretKey,PublicKey};
 
 use ln::msgs;
-use util::ser::{Writeable, Readable};
+use util::ser::{Writeable, Writer, Readable};
 use ln::peer_channel_encryptor::{PeerChannelEncryptor,NextNoiseStep};
 use util::byte_utils;
 use util::events::{EventsProvider,Event};
@@ -112,15 +112,23 @@ pub struct PeerManager<Descriptor: SocketDescriptor> {
        logger: Arc<Logger>,
 }
 
+struct VecWriter(Vec<u8>);
+impl Writer for VecWriter {
+       fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> {
+               self.0.extend_from_slice(buf);
+               Ok(())
+       }
+       fn size_hint(&mut self, size: usize) {
+               self.0.reserve_exact(size);
+       }
+}
+
 macro_rules! encode_msg {
        ($msg: expr, $msg_code: expr) => {{
-               let mut w = ::std::io::Cursor::new(vec![]);
-               0u16.write(&mut w).unwrap();
-               $msg.write(&mut w).unwrap();
-               let mut msg = w.into_inner();
-               let len = msg.len();
-               msg[..2].copy_from_slice(&byte_utils::be16_to_array(len as u16 - 2));
-               msg
+               let mut msg = VecWriter(Vec::new());
+               ($msg_code as u16).write(&mut msg).unwrap();
+               $msg.write(&mut msg).unwrap();
+               msg.0
        }}
 }