From 0196c2c4cd10e82c2c67a12c67017936228ecf95 Mon Sep 17 00:00:00 2001 From: Henry de Valence Date: Wed, 25 Sep 2019 09:00:28 -0700 Subject: [PATCH] Place header encoding prior to body encoding. --- zebra-network/src/protocol/codec.rs | 96 ++++++++++++++--------------- 1 file changed, 48 insertions(+), 48 deletions(-) diff --git a/zebra-network/src/protocol/codec.rs b/zebra-network/src/protocol/codec.rs index 8ceb58f8..dbe8d233 100644 --- a/zebra-network/src/protocol/codec.rs +++ b/zebra-network/src/protocol/codec.rs @@ -94,54 +94,6 @@ impl Builder { // ======== Encoding ========= -impl Codec { - /// Write the body of the message into the given writer. This allows writing - /// the message body prior to writing the header, so that the header can - /// contain a checksum of the message body. - fn write_body(&self, msg: &Message, mut writer: W) -> Result<(), Error> { - use Message::*; - match *msg { - Version { - ref version, - ref services, - ref timestamp, - ref address_recv, - ref address_from, - ref nonce, - ref user_agent, - ref start_height, - ref relay, - } => { - writer.write_u32::(version.0)?; - writer.write_u64::(services.0)?; - writer.write_i64::(timestamp.timestamp())?; - - let (recv_services, recv_addr) = address_recv; - writer.write_u64::(recv_services.0)?; - writer.write_socket_addr(*recv_addr)?; - - let (from_services, from_addr) = address_from; - writer.write_u64::(from_services.0)?; - writer.write_socket_addr(*from_addr)?; - - writer.write_u64::(nonce.0)?; - writer.write_string(&user_agent)?; - writer.write_u32::(start_height.0)?; - writer.write_u8(*relay as u8)?; - } - Verack => { /* Empty payload -- no-op */ } - Ping(nonce) => { - writer.write_u64::(nonce.0)?; - } - Pong(nonce) => { - writer.write_u64::(nonce.0)?; - } - _ => bail!("unimplemented message type"), - } - Ok(()) - } -} - impl Encoder for Codec { type Item = Message; type Error = Error; @@ -203,6 +155,54 @@ impl Encoder for Codec { } } +impl Codec { + /// Write the body of the message into the given writer. This allows writing + /// the message body prior to writing the header, so that the header can + /// contain a checksum of the message body. + fn write_body(&self, msg: &Message, mut writer: W) -> Result<(), Error> { + use Message::*; + match *msg { + Version { + ref version, + ref services, + ref timestamp, + ref address_recv, + ref address_from, + ref nonce, + ref user_agent, + ref start_height, + ref relay, + } => { + writer.write_u32::(version.0)?; + writer.write_u64::(services.0)?; + writer.write_i64::(timestamp.timestamp())?; + + let (recv_services, recv_addr) = address_recv; + writer.write_u64::(recv_services.0)?; + writer.write_socket_addr(*recv_addr)?; + + let (from_services, from_addr) = address_from; + writer.write_u64::(from_services.0)?; + writer.write_socket_addr(*from_addr)?; + + writer.write_u64::(nonce.0)?; + writer.write_string(&user_agent)?; + writer.write_u32::(start_height.0)?; + writer.write_u8(*relay as u8)?; + } + Verack => { /* Empty payload -- no-op */ } + Ping(nonce) => { + writer.write_u64::(nonce.0)?; + } + Pong(nonce) => { + writer.write_u64::(nonce.0)?; + } + _ => bail!("unimplemented message type"), + } + Ok(()) + } +} + // ======== Decoding ========= #[derive(Debug)]