Remove Blobs and switch to Packets (#6937)

* Remove Blobs and switch to Packets

* Fix some gossip messages not respecting MTU size

* Failure to serialize is not fatal

* Add log macros

* Remove unused extern

* Apparently macro use is required

* Explicitly scope macro

* Fix test compile
This commit is contained in:
Sagar Dhawan
2019-11-14 10:24:53 -08:00
committed by GitHub
parent d6cbb02c92
commit f108f483b7
14 changed files with 279 additions and 712 deletions

View File

@ -51,3 +51,5 @@ pub mod transport;
#[macro_use]
extern crate serde_derive;
extern crate log as logger;

View File

@ -1,6 +1,8 @@
use crate::clock::Slot;
use std::fmt;
use bincode::Result;
use serde::Serialize;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
use std::{fmt, io};
/// Maximum over-the-wire size of a Transaction
/// 1280 is IPv6 minimum MTU
@ -33,6 +35,29 @@ impl Packet {
pub fn new(data: [u8; PACKET_DATA_SIZE], meta: Meta) -> Self {
Self { data, meta }
}
pub fn from_data<T: Serialize>(dest: &SocketAddr, data: T) -> Self {
let mut me = Packet::default();
if let Err(e) = Self::populate_packet(&mut me, Some(dest), &data) {
logger::error!("Couldn't write to packet {:?}. Data skipped.", e);
}
me
}
pub fn populate_packet<T: Serialize>(
packet: &mut Packet,
dest: Option<&SocketAddr>,
data: &T,
) -> Result<()> {
let mut wr = io::Cursor::new(&mut packet.data[..]);
bincode::serialize_into(&mut wr, data)?;
let len = wr.position() as usize;
packet.meta.size = len;
if let Some(dest) = dest {
packet.meta.set_addr(dest);
}
Ok(())
}
}
impl fmt::Debug for Packet {