From 2e7beca9baecdf7865e429c2e684f1c781ef8a48 Mon Sep 17 00:00:00 2001 From: Greg Fitzgerald Date: Wed, 16 May 2018 16:01:15 -0600 Subject: [PATCH 1/2] Generalize to_request_packets --- src/request.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/request.rs b/src/request.rs index 1be8ee01dc79f7..699cba2bfb46b6 100644 --- a/src/request.rs +++ b/src/request.rs @@ -4,6 +4,7 @@ use bincode::serialize; use hash::Hash; use packet; use packet::SharedPackets; +use serde::Serialize; use signature::PublicKey; #[cfg_attr(feature = "cargo-clippy", allow(large_enum_variant))] @@ -28,7 +29,10 @@ pub enum Response { TransactionCount { transaction_count: u64 }, } -pub fn to_request_packets(r: &packet::PacketRecycler, reqs: Vec) -> Vec { +pub fn to_request_packets( + r: &packet::PacketRecycler, + reqs: Vec, +) -> Vec { let mut out = vec![]; for rrs in reqs.chunks(packet::NUM_PACKETS) { let p = r.allocate(); From 4955c6f13ab0a20009753fa433926ae4c64bfd43 Mon Sep 17 00:00:00 2001 From: Greg Fitzgerald Date: Wed, 16 May 2018 16:11:53 -0600 Subject: [PATCH 2/2] request::to_request_packets -> packet::to_packets --- src/packet.rs | 42 +++++++++++++++++++++++++++++++++++++++++- src/request.rs | 50 -------------------------------------------------- 2 files changed, 41 insertions(+), 51 deletions(-) diff --git a/src/packet.rs b/src/packet.rs index a9a84bc88e1995..33d347606777db 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -2,6 +2,7 @@ use bincode::{deserialize, serialize}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use result::{Error, Result}; +use serde::Serialize; use signature::PublicKey; use std::collections::VecDeque; use std::fmt; @@ -216,6 +217,25 @@ impl Packets { } } +pub fn to_packets(r: &PacketRecycler, xs: Vec) -> Vec { + let mut out = vec![]; + for x in xs.chunks(NUM_PACKETS) { + let p = r.allocate(); + p.write() + .unwrap() + .packets + .resize(x.len(), Default::default()); + for (i, o) in x.iter().zip(p.write().unwrap().packets.iter_mut()) { + let v = serialize(&i).expect("serialize request"); + let len = v.len(); + o.data[..len].copy_from_slice(&v); + o.meta.size = len; + } + out.push(p); + } + return out; +} + const BLOB_INDEX_END: usize = size_of::(); const BLOB_ID_END: usize = BLOB_INDEX_END + size_of::() + size_of::(); @@ -308,11 +328,13 @@ impl Blob { #[cfg(test)] mod test { - use packet::{Blob, BlobRecycler, Packet, PacketRecycler, Packets}; + use packet::{to_packets, Blob, BlobRecycler, Packet, PacketRecycler, Packets, NUM_PACKETS}; + use request::Request; use std::collections::VecDeque; use std::io; use std::io::Write; use std::net::UdpSocket; + #[test] pub fn packet_recycler_test() { let r = PacketRecycler::default(); @@ -354,6 +376,24 @@ mod test { r.recycle(p); } + #[test] + fn test_to_packets() { + let tr = Request::GetTransactionCount; + let re = PacketRecycler::default(); + let rv = to_packets(&re, vec![tr.clone(); 1]); + assert_eq!(rv.len(), 1); + assert_eq!(rv[0].read().unwrap().packets.len(), 1); + + let rv = to_packets(&re, vec![tr.clone(); NUM_PACKETS]); + assert_eq!(rv.len(), 1); + assert_eq!(rv[0].read().unwrap().packets.len(), NUM_PACKETS); + + let rv = to_packets(&re, vec![tr.clone(); NUM_PACKETS + 1]); + assert_eq!(rv.len(), 2); + assert_eq!(rv[0].read().unwrap().packets.len(), NUM_PACKETS); + assert_eq!(rv[1].read().unwrap().packets.len(), 1); + } + #[test] pub fn blob_send_recv() { trace!("start"); diff --git a/src/request.rs b/src/request.rs index 699cba2bfb46b6..7c5d23b52baf7d 100644 --- a/src/request.rs +++ b/src/request.rs @@ -1,10 +1,6 @@ //! The `request` module defines the messages for the thin client. -use bincode::serialize; use hash::Hash; -use packet; -use packet::SharedPackets; -use serde::Serialize; use signature::PublicKey; #[cfg_attr(feature = "cargo-clippy", allow(large_enum_variant))] @@ -28,49 +24,3 @@ pub enum Response { LastId { id: Hash }, TransactionCount { transaction_count: u64 }, } - -pub fn to_request_packets( - r: &packet::PacketRecycler, - reqs: Vec, -) -> Vec { - let mut out = vec![]; - for rrs in reqs.chunks(packet::NUM_PACKETS) { - let p = r.allocate(); - p.write() - .unwrap() - .packets - .resize(rrs.len(), Default::default()); - for (i, o) in rrs.iter().zip(p.write().unwrap().packets.iter_mut()) { - let v = serialize(&i).expect("serialize request"); - let len = v.len(); - o.data[..len].copy_from_slice(&v); - o.meta.size = len; - } - out.push(p); - } - return out; -} - -#[cfg(test)] -mod tests { - use packet::{PacketRecycler, NUM_PACKETS}; - use request::{to_request_packets, Request}; - - #[test] - fn test_to_packets() { - let tr = Request::GetTransactionCount; - let re = PacketRecycler::default(); - let rv = to_request_packets(&re, vec![tr.clone(); 1]); - assert_eq!(rv.len(), 1); - assert_eq!(rv[0].read().unwrap().packets.len(), 1); - - let rv = to_request_packets(&re, vec![tr.clone(); NUM_PACKETS]); - assert_eq!(rv.len(), 1); - assert_eq!(rv[0].read().unwrap().packets.len(), NUM_PACKETS); - - let rv = to_request_packets(&re, vec![tr.clone(); NUM_PACKETS + 1]); - assert_eq!(rv.len(), 2); - assert_eq!(rv[0].read().unwrap().packets.len(), NUM_PACKETS); - assert_eq!(rv[1].read().unwrap().packets.len(), 1); - } -}