2018-04-17 19:46:50 -07:00
|
|
|
//! The `packet` module defines data structures and methods to pull data from the network.
|
2019-06-27 00:32:32 -07:00
|
|
|
use crate::cuda_runtime::PinnedVec;
|
2019-07-11 13:58:33 -07:00
|
|
|
use crate::erasure::ErasureConfig;
|
2019-06-20 20:15:33 -07:00
|
|
|
use crate::recvmmsg::{recv_mmsg, NUM_RCVMMSGS};
|
2019-06-27 00:32:32 -07:00
|
|
|
use crate::recycler::{Recycler, Reset};
|
2019-06-20 20:15:33 -07:00
|
|
|
use crate::result::{Error, Result};
|
2019-03-11 03:06:22 -07:00
|
|
|
use bincode;
|
2019-01-30 20:18:28 -08:00
|
|
|
use byteorder::{ByteOrder, LittleEndian};
|
2018-05-16 15:11:53 -07:00
|
|
|
use serde::Serialize;
|
2019-05-17 07:00:06 -07:00
|
|
|
use solana_metrics::inc_new_counter_debug;
|
2018-11-16 08:04:46 -08:00
|
|
|
pub use solana_sdk::packet::PACKET_DATA_SIZE;
|
2019-02-27 13:37:08 -08:00
|
|
|
use solana_sdk::pubkey::Pubkey;
|
2019-06-12 16:43:05 -07:00
|
|
|
use solana_sdk::signature::Signable;
|
|
|
|
use solana_sdk::signature::Signature;
|
|
|
|
use std::borrow::Cow;
|
2018-12-05 12:47:19 -08:00
|
|
|
use std::cmp;
|
2018-04-02 19:32:58 -07:00
|
|
|
use std::fmt;
|
|
|
|
use std::io;
|
2019-03-08 19:10:18 -08:00
|
|
|
use std::io::Cursor;
|
2019-06-27 00:32:32 -07:00
|
|
|
use std::mem;
|
2018-05-02 09:24:25 -07:00
|
|
|
use std::mem::size_of;
|
2018-04-02 19:32:58 -07:00
|
|
|
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, UdpSocket};
|
2019-04-14 17:30:08 -07:00
|
|
|
use std::ops::{Deref, DerefMut};
|
2018-09-26 09:50:12 -07:00
|
|
|
use std::sync::{Arc, RwLock};
|
2019-05-29 12:17:50 -07:00
|
|
|
use std::time::Instant;
|
2018-04-02 19:32:58 -07:00
|
|
|
|
2018-09-26 09:50:12 -07:00
|
|
|
pub type SharedBlob = Arc<RwLock<Blob>>;
|
2018-09-03 03:22:47 -07:00
|
|
|
pub type SharedBlobs = Vec<SharedBlob>;
|
2018-04-02 19:32:58 -07:00
|
|
|
|
2018-04-11 17:30:53 -07:00
|
|
|
pub const NUM_PACKETS: usize = 1024 * 8;
|
2019-07-30 22:15:07 -07:00
|
|
|
pub const BLOB_SIZE: usize = (2 * 1024 - 128); // wikipedia says there should be 20b for ipv4 headers
|
2018-08-17 11:56:32 -07:00
|
|
|
pub const BLOB_DATA_SIZE: usize = BLOB_SIZE - (BLOB_HEADER_SIZE * 2);
|
2019-04-14 21:10:09 -07:00
|
|
|
pub const BLOB_DATA_ALIGN: usize = 16; // safe for erasure input pointers, gf.c needs 16byte-aligned buffers
|
2018-03-26 21:07:11 -07:00
|
|
|
pub const NUM_BLOBS: usize = (NUM_PACKETS * PACKET_DATA_SIZE) / BLOB_SIZE;
|
2019-07-30 22:15:07 -07:00
|
|
|
pub const PACKETS_PER_BLOB: usize = 8; // reasonable estimate for payment packets per blob based on ~200b transaction size
|
2018-04-02 19:32:58 -07:00
|
|
|
|
2019-03-08 19:10:18 -08:00
|
|
|
#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
|
2018-03-26 21:07:11 -07:00
|
|
|
#[repr(C)]
|
2018-04-02 19:32:58 -07:00
|
|
|
pub struct Meta {
|
|
|
|
pub size: usize,
|
2019-04-20 16:44:06 -07:00
|
|
|
pub forward: bool,
|
2018-04-02 19:32:58 -07:00
|
|
|
pub addr: [u16; 8],
|
|
|
|
pub port: u16,
|
|
|
|
pub v6: bool,
|
2019-08-20 17:16:06 -07:00
|
|
|
pub seed: [u8; 32],
|
|
|
|
pub slot: u64,
|
2018-04-02 19:32:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
2018-03-26 21:07:11 -07:00
|
|
|
#[repr(C)]
|
2018-04-02 19:32:58 -07:00
|
|
|
pub struct Packet {
|
2018-03-26 21:07:11 -07:00
|
|
|
pub data: [u8; PACKET_DATA_SIZE],
|
2018-04-02 19:32:58 -07:00
|
|
|
pub meta: Meta,
|
|
|
|
}
|
|
|
|
|
2019-03-08 19:10:18 -08:00
|
|
|
impl Packet {
|
|
|
|
pub fn new(data: [u8; PACKET_DATA_SIZE], meta: Meta) -> Self {
|
2019-03-10 17:33:01 -07:00
|
|
|
Self { data, meta }
|
2019-03-08 19:10:18 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-02 19:32:58 -07:00
|
|
|
impl fmt::Debug for Packet {
|
2018-12-08 21:44:20 -08:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-04-02 19:32:58 -07:00
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"Packet {{ size: {:?}, addr: {:?} }}",
|
|
|
|
self.meta.size,
|
|
|
|
self.meta.addr()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Packet {
|
|
|
|
fn default() -> Packet {
|
|
|
|
Packet {
|
2019-04-17 14:14:57 -07:00
|
|
|
data: unsafe { std::mem::uninitialized() },
|
2018-04-02 19:32:58 -07:00
|
|
|
meta: Meta::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-08 19:10:18 -08:00
|
|
|
impl PartialEq for Packet {
|
|
|
|
fn eq(&self, other: &Packet) -> bool {
|
2019-05-06 09:11:50 -07:00
|
|
|
let self_data: &[u8] = self.data.as_ref();
|
|
|
|
let other_data: &[u8] = other.data.as_ref();
|
2019-05-29 10:08:35 -07:00
|
|
|
self.meta == other.meta && self_data[..self.meta.size] == other_data[..other.meta.size]
|
2019-03-08 19:10:18 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-02 19:32:58 -07:00
|
|
|
impl Meta {
|
|
|
|
pub fn addr(&self) -> SocketAddr {
|
|
|
|
if !self.v6 {
|
|
|
|
let addr = [
|
|
|
|
self.addr[0] as u8,
|
|
|
|
self.addr[1] as u8,
|
|
|
|
self.addr[2] as u8,
|
|
|
|
self.addr[3] as u8,
|
|
|
|
];
|
|
|
|
let ipv4: Ipv4Addr = From::<[u8; 4]>::from(addr);
|
|
|
|
SocketAddr::new(IpAddr::V4(ipv4), self.port)
|
|
|
|
} else {
|
|
|
|
let ipv6: Ipv6Addr = From::<[u16; 8]>::from(self.addr);
|
|
|
|
SocketAddr::new(IpAddr::V6(ipv6), self.port)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_addr(&mut self, a: &SocketAddr) {
|
|
|
|
match *a {
|
|
|
|
SocketAddr::V4(v4) => {
|
|
|
|
let ip = v4.ip().octets();
|
|
|
|
self.addr[0] = u16::from(ip[0]);
|
|
|
|
self.addr[1] = u16::from(ip[1]);
|
|
|
|
self.addr[2] = u16::from(ip[2]);
|
|
|
|
self.addr[3] = u16::from(ip[3]);
|
2018-08-30 12:07:54 -07:00
|
|
|
self.addr[4] = 0;
|
|
|
|
self.addr[5] = 0;
|
|
|
|
self.addr[6] = 0;
|
|
|
|
self.addr[7] = 0;
|
|
|
|
self.v6 = false;
|
2018-04-02 19:32:58 -07:00
|
|
|
}
|
|
|
|
SocketAddr::V6(v6) => {
|
|
|
|
self.addr = v6.ip().segments();
|
|
|
|
self.v6 = true;
|
|
|
|
}
|
|
|
|
}
|
2018-08-30 12:07:54 -07:00
|
|
|
self.port = a.port();
|
2018-04-02 19:32:58 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-17 18:15:50 -07:00
|
|
|
#[derive(Debug, Clone)]
|
2018-04-02 19:32:58 -07:00
|
|
|
pub struct Packets {
|
2019-06-27 00:32:32 -07:00
|
|
|
pub packets: PinnedVec<Packet>,
|
|
|
|
|
|
|
|
recycler: Option<PacketsRecycler>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Packets {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
if let Some(ref recycler) = self.recycler {
|
|
|
|
let old = mem::replace(&mut self.packets, PinnedVec::default());
|
|
|
|
recycler.recycle(old)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Reset for Packets {
|
|
|
|
fn reset(&mut self) {
|
|
|
|
self.packets.resize(0, Packet::default());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Reset for PinnedVec<Packet> {
|
|
|
|
fn reset(&mut self) {
|
|
|
|
self.resize(0, Packet::default());
|
|
|
|
}
|
2018-04-02 19:32:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//auto derive doesn't support large arrays
|
|
|
|
impl Default for Packets {
|
|
|
|
fn default() -> Packets {
|
2019-06-27 00:32:32 -07:00
|
|
|
let packets = PinnedVec::with_capacity(NUM_RCVMMSGS);
|
2018-04-02 19:32:58 -07:00
|
|
|
Packets {
|
2019-06-27 00:32:32 -07:00
|
|
|
packets,
|
|
|
|
recycler: None,
|
2018-04-02 19:32:58 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-27 00:32:32 -07:00
|
|
|
pub type PacketsRecycler = Recycler<PinnedVec<Packet>>;
|
|
|
|
|
2019-01-10 12:34:48 -08:00
|
|
|
impl Packets {
|
2019-03-08 15:47:53 -08:00
|
|
|
pub fn new(packets: Vec<Packet>) -> Self {
|
2019-06-27 00:32:32 -07:00
|
|
|
let packets = PinnedVec::from_vec(packets);
|
|
|
|
Self {
|
|
|
|
packets,
|
|
|
|
recycler: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_with_recycler(recycler: PacketsRecycler, size: usize, name: &'static str) -> Self {
|
|
|
|
let mut packets = recycler.allocate(name);
|
|
|
|
packets.reserve_and_pin(size);
|
|
|
|
Packets {
|
|
|
|
packets,
|
|
|
|
recycler: Some(recycler),
|
|
|
|
}
|
2019-03-08 15:47:53 -08:00
|
|
|
}
|
|
|
|
|
2019-01-10 12:34:48 -08:00
|
|
|
pub fn set_addr(&mut self, addr: &SocketAddr) {
|
|
|
|
for m in self.packets.iter_mut() {
|
|
|
|
m.meta.set_addr(&addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-14 21:10:09 -07:00
|
|
|
#[repr(align(16))] // 16 === BLOB_DATA_ALIGN
|
2019-04-14 17:30:08 -07:00
|
|
|
pub struct BlobData {
|
2018-04-02 19:32:58 -07:00
|
|
|
pub data: [u8; BLOB_SIZE],
|
|
|
|
}
|
|
|
|
|
2019-04-14 17:30:08 -07:00
|
|
|
impl Clone for BlobData {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
BlobData { data: self.data }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for BlobData {
|
|
|
|
fn default() -> Self {
|
|
|
|
BlobData {
|
|
|
|
data: [0u8; BLOB_SIZE],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialEq for BlobData {
|
|
|
|
fn eq(&self, other: &BlobData) -> bool {
|
2019-05-06 09:11:50 -07:00
|
|
|
let self_data: &[u8] = self.data.as_ref();
|
|
|
|
let other_data: &[u8] = other.data.as_ref();
|
|
|
|
self_data == other_data
|
2019-04-14 17:30:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// this code hides _data, maps it to _data.data
|
|
|
|
impl Deref for Blob {
|
|
|
|
type Target = BlobData;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self._data
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl DerefMut for Blob {
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
&mut self._data
|
2018-11-19 23:20:18 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-14 17:30:08 -07:00
|
|
|
#[derive(Clone, Default, PartialEq)]
|
|
|
|
pub struct Blob {
|
|
|
|
_data: BlobData, // hidden member, passed through by Deref
|
|
|
|
pub meta: Meta,
|
|
|
|
}
|
|
|
|
|
2018-04-02 19:32:58 -07:00
|
|
|
impl fmt::Debug for Blob {
|
2018-12-08 21:44:20 -08:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-04-02 19:32:58 -07:00
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"Blob {{ size: {:?}, addr: {:?} }}",
|
|
|
|
self.meta.size,
|
|
|
|
self.meta.addr()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-25 12:15:26 -07:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum BlobError {
|
|
|
|
/// the Blob's meta and data are not self-consistent
|
|
|
|
BadState,
|
2018-11-12 12:41:19 -08:00
|
|
|
/// Blob verification failed
|
|
|
|
VerificationFailed,
|
2018-07-25 12:15:26 -07:00
|
|
|
}
|
2018-07-25 03:35:36 -07:00
|
|
|
|
2018-04-02 19:32:58 -07:00
|
|
|
impl Packets {
|
2019-04-17 14:14:57 -07:00
|
|
|
pub fn recv_from(&mut self, socket: &UdpSocket) -> Result<usize> {
|
2018-04-02 19:32:58 -07:00
|
|
|
let mut i = 0;
|
|
|
|
//DOCUMENTED SIDE-EFFECT
|
|
|
|
//Performance out of the IO without poll
|
2018-05-25 22:00:47 -07:00
|
|
|
// * block on the socket until it's readable
|
2018-04-02 19:32:58 -07:00
|
|
|
// * set the socket to non blocking
|
|
|
|
// * read until it fails
|
|
|
|
// * set it back to blocking before returning
|
|
|
|
socket.set_nonblocking(false)?;
|
2018-09-13 14:41:28 -07:00
|
|
|
trace!("receiving on {}", socket.local_addr().unwrap());
|
2019-05-29 12:17:50 -07:00
|
|
|
let start = Instant::now();
|
|
|
|
let mut total_size = 0;
|
2018-09-13 14:41:28 -07:00
|
|
|
loop {
|
2019-04-17 14:14:57 -07:00
|
|
|
self.packets.resize(i + NUM_RCVMMSGS, Packet::default());
|
2018-09-13 14:41:28 -07:00
|
|
|
match recv_mmsg(socket, &mut self.packets[i..]) {
|
2018-04-02 19:32:58 -07:00
|
|
|
Err(_) if i > 0 => {
|
2019-05-29 12:17:50 -07:00
|
|
|
if start.elapsed().as_millis() > 1 {
|
|
|
|
break;
|
|
|
|
}
|
2018-04-02 19:32:58 -07:00
|
|
|
}
|
|
|
|
Err(e) => {
|
2018-05-04 11:11:39 -07:00
|
|
|
trace!("recv_from err {:?}", e);
|
2018-04-02 19:32:58 -07:00
|
|
|
return Err(Error::IO(e));
|
|
|
|
}
|
2019-05-29 12:17:50 -07:00
|
|
|
Ok((size, npkts)) => {
|
2019-01-24 12:46:40 -08:00
|
|
|
if i == 0 {
|
|
|
|
socket.set_nonblocking(true)?;
|
|
|
|
}
|
2018-09-13 14:41:28 -07:00
|
|
|
trace!("got {} packets", npkts);
|
|
|
|
i += npkts;
|
2019-05-29 12:17:50 -07:00
|
|
|
total_size += size;
|
|
|
|
// Try to batch into blob-sized buffers
|
|
|
|
// will cause less re-shuffling later on.
|
2019-07-30 22:15:07 -07:00
|
|
|
if start.elapsed().as_millis() > 1 || total_size >= (BLOB_DATA_SIZE - 512) {
|
2019-04-17 14:14:57 -07:00
|
|
|
break;
|
2018-04-02 19:32:58 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-17 14:14:57 -07:00
|
|
|
self.packets.truncate(i);
|
2019-05-17 07:00:06 -07:00
|
|
|
inc_new_counter_debug!("packets-recv_count", i);
|
2019-04-17 14:14:57 -07:00
|
|
|
Ok(i)
|
2018-04-02 19:32:58 -07:00
|
|
|
}
|
2019-04-17 14:14:57 -07:00
|
|
|
|
2018-04-02 19:32:58 -07:00
|
|
|
pub fn send_to(&self, socket: &UdpSocket) -> Result<()> {
|
|
|
|
for p in &self.packets {
|
|
|
|
let a = p.meta.addr();
|
|
|
|
socket.send_to(&p.data[..p.meta.size], &a)?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-17 18:15:50 -07:00
|
|
|
pub fn to_packets_chunked<T: Serialize>(xs: &[T], chunks: usize) -> Vec<Packets> {
|
2018-05-16 15:11:53 -07:00
|
|
|
let mut out = vec![];
|
2018-06-08 10:09:45 -07:00
|
|
|
for x in xs.chunks(chunks) {
|
2019-04-17 18:15:50 -07:00
|
|
|
let mut p = Packets::default();
|
|
|
|
p.packets.resize(x.len(), Packet::default());
|
|
|
|
for (i, o) in x.iter().zip(p.packets.iter_mut()) {
|
2019-01-24 21:14:15 -08:00
|
|
|
let mut wr = io::Cursor::new(&mut o.data[..]);
|
2019-03-11 03:06:22 -07:00
|
|
|
bincode::serialize_into(&mut wr, &i).expect("serialize request");
|
2019-01-24 21:14:15 -08:00
|
|
|
let len = wr.position() as usize;
|
2018-05-16 15:11:53 -07:00
|
|
|
o.meta.size = len;
|
|
|
|
}
|
|
|
|
out.push(p);
|
|
|
|
}
|
2018-07-11 13:40:46 -07:00
|
|
|
out
|
2018-05-16 15:11:53 -07:00
|
|
|
}
|
|
|
|
|
2019-04-17 18:15:50 -07:00
|
|
|
pub fn to_packets<T: Serialize>(xs: &[T]) -> Vec<Packets> {
|
2018-09-26 09:50:12 -07:00
|
|
|
to_packets_chunked(xs, NUM_PACKETS)
|
2018-06-08 10:09:45 -07:00
|
|
|
}
|
|
|
|
|
2018-12-22 19:30:30 -08:00
|
|
|
pub fn to_blob<T: Serialize>(resp: T, rsp_addr: SocketAddr) -> Result<Blob> {
|
|
|
|
let mut b = Blob::default();
|
2019-03-11 03:06:22 -07:00
|
|
|
let v = bincode::serialize(&resp)?;
|
2018-12-22 19:30:30 -08:00
|
|
|
let len = v.len();
|
2019-08-13 18:04:14 -07:00
|
|
|
if len > BLOB_SIZE {
|
|
|
|
return Err(Error::ToBlobError);
|
|
|
|
}
|
2018-12-22 19:30:30 -08:00
|
|
|
b.data[..len].copy_from_slice(&v);
|
|
|
|
b.meta.size = len;
|
|
|
|
b.meta.set_addr(&rsp_addr);
|
|
|
|
Ok(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_blobs<T: Serialize>(rsps: Vec<(T, SocketAddr)>) -> Result<Vec<Blob>> {
|
|
|
|
let mut blobs = Vec::new();
|
|
|
|
for (resp, rsp_addr) in rsps {
|
|
|
|
blobs.push(to_blob(resp, rsp_addr)?);
|
2018-05-25 16:10:14 -07:00
|
|
|
}
|
2018-12-22 19:30:30 -08:00
|
|
|
Ok(blobs)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_shared_blob<T: Serialize>(resp: T, rsp_addr: SocketAddr) -> Result<SharedBlob> {
|
|
|
|
let blob = Arc::new(RwLock::new(to_blob(resp, rsp_addr)?));
|
2018-05-25 16:10:14 -07:00
|
|
|
Ok(blob)
|
|
|
|
}
|
|
|
|
|
2018-12-22 19:30:30 -08:00
|
|
|
pub fn to_shared_blobs<T: Serialize>(rsps: Vec<(T, SocketAddr)>) -> Result<SharedBlobs> {
|
2018-09-03 03:22:47 -07:00
|
|
|
let mut blobs = Vec::new();
|
2018-05-25 16:10:14 -07:00
|
|
|
for (resp, rsp_addr) in rsps {
|
2018-12-22 19:30:30 -08:00
|
|
|
blobs.push(to_shared_blob(resp, rsp_addr)?);
|
2018-05-25 16:10:14 -07:00
|
|
|
}
|
|
|
|
Ok(blobs)
|
|
|
|
}
|
|
|
|
|
2019-01-30 20:18:28 -08:00
|
|
|
macro_rules! range {
|
2019-06-20 20:15:33 -07:00
|
|
|
($prev:expr, $type:ident) => {
|
2019-01-30 20:18:28 -08:00
|
|
|
$prev..$prev + size_of::<$type>()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-06-12 16:43:05 -07:00
|
|
|
const SIGNATURE_RANGE: std::ops::Range<usize> = range!(0, Signature);
|
|
|
|
const FORWARDED_RANGE: std::ops::Range<usize> = range!(SIGNATURE_RANGE.end, bool);
|
|
|
|
const PARENT_RANGE: std::ops::Range<usize> = range!(FORWARDED_RANGE.end, u64);
|
2019-07-12 13:43:19 -07:00
|
|
|
const VERSION_RANGE: std::ops::Range<usize> = range!(PARENT_RANGE.end, u64);
|
|
|
|
const SLOT_RANGE: std::ops::Range<usize> = range!(VERSION_RANGE.end, u64);
|
2019-01-30 20:18:28 -08:00
|
|
|
const INDEX_RANGE: std::ops::Range<usize> = range!(SLOT_RANGE.end, u64);
|
2019-06-20 20:15:33 -07:00
|
|
|
const ID_RANGE: std::ops::Range<usize> = range!(INDEX_RANGE.end, Pubkey);
|
2019-06-12 16:43:05 -07:00
|
|
|
const FLAGS_RANGE: std::ops::Range<usize> = range!(ID_RANGE.end, u32);
|
2019-07-11 13:58:33 -07:00
|
|
|
const ERASURE_CONFIG_RANGE: std::ops::Range<usize> = range!(FLAGS_RANGE.end, ErasureConfig);
|
|
|
|
const SIZE_RANGE: std::ops::Range<usize> = range!(ERASURE_CONFIG_RANGE.end, u64);
|
2018-06-04 11:43:23 -07:00
|
|
|
|
2019-06-20 20:15:33 -07:00
|
|
|
macro_rules! align {
|
|
|
|
($x:expr, $align:expr) => {
|
|
|
|
$x + ($align - 1) & !($align - 1)
|
|
|
|
};
|
|
|
|
}
|
2018-05-31 11:21:07 -07:00
|
|
|
|
2019-06-20 20:15:33 -07:00
|
|
|
pub const BLOB_HEADER_SIZE: usize = align!(SIZE_RANGE.end, BLOB_DATA_ALIGN); // make sure data() is safe for erasure
|
2019-06-12 16:43:05 -07:00
|
|
|
pub const SIGNABLE_START: usize = PARENT_RANGE.start;
|
2019-01-30 20:18:28 -08:00
|
|
|
|
2019-02-12 14:45:16 -08:00
|
|
|
pub const BLOB_FLAG_IS_LAST_IN_SLOT: u32 = 0x2;
|
2019-02-11 17:30:46 -08:00
|
|
|
|
2018-05-31 11:21:07 -07:00
|
|
|
pub const BLOB_FLAG_IS_CODING: u32 = 0x1;
|
2018-04-26 15:01:51 -07:00
|
|
|
|
2018-04-02 19:32:58 -07:00
|
|
|
impl Blob {
|
2018-12-05 12:47:19 -08:00
|
|
|
pub fn new(data: &[u8]) -> Self {
|
|
|
|
let mut blob = Self::default();
|
2019-04-18 21:56:43 -07:00
|
|
|
|
|
|
|
assert!(data.len() <= blob.data.len());
|
|
|
|
|
2018-12-05 12:47:19 -08:00
|
|
|
let data_len = cmp::min(data.len(), blob.data.len());
|
2019-04-18 21:56:43 -07:00
|
|
|
|
2018-12-05 12:47:19 -08:00
|
|
|
let bytes = &data[..data_len];
|
|
|
|
blob.data[..data_len].copy_from_slice(bytes);
|
2019-07-08 18:21:52 -07:00
|
|
|
blob.meta.size = data_len;
|
2018-12-05 12:47:19 -08:00
|
|
|
blob
|
|
|
|
}
|
|
|
|
|
2019-03-17 18:48:23 -07:00
|
|
|
pub fn from_serializable<T: Serialize + ?Sized>(data: &T) -> Self {
|
|
|
|
let mut blob = Self::default();
|
|
|
|
let pos = {
|
|
|
|
let mut out = Cursor::new(blob.data_mut());
|
|
|
|
bincode::serialize_into(&mut out, data).expect("failed to serialize output");
|
|
|
|
out.position() as usize
|
|
|
|
};
|
|
|
|
blob.set_size(pos);
|
2019-07-11 13:58:33 -07:00
|
|
|
blob.set_erasure_config(&ErasureConfig::default());
|
2019-03-17 18:48:23 -07:00
|
|
|
blob
|
|
|
|
}
|
|
|
|
|
2019-01-30 20:18:28 -08:00
|
|
|
pub fn parent(&self) -> u64 {
|
|
|
|
LittleEndian::read_u64(&self.data[PARENT_RANGE])
|
2018-11-07 13:18:14 -08:00
|
|
|
}
|
2019-01-30 20:18:28 -08:00
|
|
|
pub fn set_parent(&mut self, ix: u64) {
|
|
|
|
LittleEndian::write_u64(&mut self.data[PARENT_RANGE], ix);
|
2018-11-07 13:18:14 -08:00
|
|
|
}
|
2019-07-12 13:43:19 -07:00
|
|
|
pub fn version(&self) -> u64 {
|
|
|
|
LittleEndian::read_u64(&self.data[VERSION_RANGE])
|
|
|
|
}
|
|
|
|
pub fn set_version(&mut self, version: u64) {
|
|
|
|
LittleEndian::write_u64(&mut self.data[VERSION_RANGE], version);
|
|
|
|
}
|
2019-01-30 20:18:28 -08:00
|
|
|
pub fn slot(&self) -> u64 {
|
|
|
|
LittleEndian::read_u64(&self.data[SLOT_RANGE])
|
2018-04-02 19:32:58 -07:00
|
|
|
}
|
2019-01-30 20:18:28 -08:00
|
|
|
pub fn set_slot(&mut self, ix: u64) {
|
|
|
|
LittleEndian::write_u64(&mut self.data[SLOT_RANGE], ix);
|
|
|
|
}
|
|
|
|
pub fn index(&self) -> u64 {
|
|
|
|
LittleEndian::read_u64(&self.data[INDEX_RANGE])
|
|
|
|
}
|
|
|
|
pub fn set_index(&mut self, ix: u64) {
|
|
|
|
LittleEndian::write_u64(&mut self.data[INDEX_RANGE], ix);
|
2018-04-02 19:32:58 -07:00
|
|
|
}
|
2019-01-30 20:18:28 -08:00
|
|
|
|
2019-07-11 13:58:33 -07:00
|
|
|
pub fn set_erasure_config(&mut self, config: &ErasureConfig) {
|
|
|
|
self.data[ERASURE_CONFIG_RANGE].copy_from_slice(&bincode::serialize(config).unwrap())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn erasure_config(&self) -> ErasureConfig {
|
|
|
|
bincode::deserialize(&self.data[ERASURE_CONFIG_RANGE]).unwrap_or_default()
|
|
|
|
}
|
|
|
|
|
2019-06-03 20:38:05 -07:00
|
|
|
pub fn seed(&self) -> [u8; 32] {
|
|
|
|
let mut seed = [0; 32];
|
2019-06-19 15:36:06 -07:00
|
|
|
let seed_len = seed.len();
|
|
|
|
let signature_bytes = self.get_signature_bytes();
|
|
|
|
seed[0..seed_len].copy_from_slice(&signature_bytes[(signature_bytes.len() - seed_len)..]);
|
2019-06-03 20:38:05 -07:00
|
|
|
seed
|
|
|
|
}
|
|
|
|
|
2019-02-27 13:37:08 -08:00
|
|
|
/// sender id, we use this for identifying if its a blob from the leader that we should
|
|
|
|
/// retransmit. eventually blobs should have a signature that we can use for spam filtering
|
|
|
|
pub fn id(&self) -> Pubkey {
|
|
|
|
Pubkey::new(&self.data[ID_RANGE])
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_id(&mut self, id: &Pubkey) {
|
|
|
|
self.data[ID_RANGE].copy_from_slice(id.as_ref())
|
|
|
|
}
|
|
|
|
|
2019-02-12 10:56:48 -08:00
|
|
|
/// Used to determine whether or not this blob should be forwarded in retransmit
|
|
|
|
/// A bool is used here instead of a flag because this item is not intended to be signed when
|
|
|
|
/// blob signatures are introduced
|
|
|
|
pub fn should_forward(&self) -> bool {
|
2019-04-20 16:44:06 -07:00
|
|
|
self.data[FORWARDED_RANGE][0] & 0x1 == 0
|
2018-04-28 00:31:20 -07:00
|
|
|
}
|
2018-05-04 11:11:39 -07:00
|
|
|
|
2019-04-20 16:44:06 -07:00
|
|
|
/// Mark this blob's forwarded status
|
|
|
|
pub fn set_forwarded(&mut self, forward: bool) {
|
|
|
|
self.data[FORWARDED_RANGE][0] = u8::from(forward)
|
2018-04-28 00:31:20 -07:00
|
|
|
}
|
|
|
|
|
2019-01-30 20:18:28 -08:00
|
|
|
pub fn flags(&self) -> u32 {
|
|
|
|
LittleEndian::read_u32(&self.data[FLAGS_RANGE])
|
2018-05-31 11:21:07 -07:00
|
|
|
}
|
2019-01-30 20:18:28 -08:00
|
|
|
pub fn set_flags(&mut self, ix: u32) {
|
|
|
|
LittleEndian::write_u32(&mut self.data[FLAGS_RANGE], ix);
|
2018-05-31 11:21:07 -07:00
|
|
|
}
|
|
|
|
|
2018-06-01 12:49:47 -07:00
|
|
|
pub fn is_coding(&self) -> bool {
|
2019-01-30 20:18:28 -08:00
|
|
|
(self.flags() & BLOB_FLAG_IS_CODING) != 0
|
2018-06-01 12:49:47 -07:00
|
|
|
}
|
|
|
|
|
2019-01-30 20:18:28 -08:00
|
|
|
pub fn set_coding(&mut self) {
|
|
|
|
let flags = self.flags();
|
|
|
|
self.set_flags(flags | BLOB_FLAG_IS_CODING);
|
2018-06-01 12:49:47 -07:00
|
|
|
}
|
|
|
|
|
2019-02-12 14:45:16 -08:00
|
|
|
pub fn set_is_last_in_slot(&mut self) {
|
2019-02-11 17:30:46 -08:00
|
|
|
let flags = self.flags();
|
2019-02-12 14:45:16 -08:00
|
|
|
self.set_flags(flags | BLOB_FLAG_IS_LAST_IN_SLOT);
|
2019-02-11 17:30:46 -08:00
|
|
|
}
|
|
|
|
|
2019-02-12 14:45:16 -08:00
|
|
|
pub fn is_last_in_slot(&self) -> bool {
|
|
|
|
(self.flags() & BLOB_FLAG_IS_LAST_IN_SLOT) != 0
|
2019-02-11 17:30:46 -08:00
|
|
|
}
|
|
|
|
|
2019-01-30 20:18:28 -08:00
|
|
|
pub fn data_size(&self) -> u64 {
|
2019-07-08 18:21:52 -07:00
|
|
|
cmp::min(
|
|
|
|
LittleEndian::read_u64(&self.data[SIZE_RANGE]),
|
|
|
|
BLOB_SIZE as u64,
|
|
|
|
)
|
2018-06-04 11:43:23 -07:00
|
|
|
}
|
|
|
|
|
2019-04-18 21:56:43 -07:00
|
|
|
pub fn set_data_size(&mut self, size: u64) {
|
|
|
|
LittleEndian::write_u64(&mut self.data[SIZE_RANGE], size);
|
2018-06-04 11:43:23 -07:00
|
|
|
}
|
|
|
|
|
2018-04-02 19:32:58 -07:00
|
|
|
pub fn data(&self) -> &[u8] {
|
2019-07-10 11:08:17 -07:00
|
|
|
&self.data[BLOB_HEADER_SIZE..BLOB_HEADER_SIZE + BLOB_DATA_SIZE]
|
2018-04-02 19:32:58 -07:00
|
|
|
}
|
|
|
|
pub fn data_mut(&mut self) -> &mut [u8] {
|
2019-07-10 11:08:17 -07:00
|
|
|
&mut self.data[BLOB_HEADER_SIZE..BLOB_HEADER_SIZE + BLOB_DATA_SIZE]
|
2018-04-26 15:01:51 -07:00
|
|
|
}
|
2019-01-30 20:18:28 -08:00
|
|
|
pub fn size(&self) -> usize {
|
|
|
|
let size = self.data_size() as usize;
|
|
|
|
|
|
|
|
if size > BLOB_HEADER_SIZE && size == self.meta.size {
|
|
|
|
size - BLOB_HEADER_SIZE
|
2018-07-25 03:35:36 -07:00
|
|
|
} else {
|
2019-01-30 20:18:28 -08:00
|
|
|
0
|
2018-07-25 03:35:36 -07:00
|
|
|
}
|
2018-07-25 02:41:26 -07:00
|
|
|
}
|
2019-01-30 20:18:28 -08:00
|
|
|
|
2018-04-26 15:01:51 -07:00
|
|
|
pub fn set_size(&mut self, size: usize) {
|
2018-06-04 11:43:23 -07:00
|
|
|
let new_size = size + BLOB_HEADER_SIZE;
|
|
|
|
self.meta.size = new_size;
|
2019-01-30 20:18:28 -08:00
|
|
|
self.set_data_size(new_size as u64);
|
2018-04-02 19:32:58 -07:00
|
|
|
}
|
2018-09-12 14:45:43 -07:00
|
|
|
|
2019-06-19 15:36:06 -07:00
|
|
|
pub fn get_signature_bytes(&self) -> &[u8] {
|
|
|
|
&self.data[SIGNATURE_RANGE]
|
|
|
|
}
|
|
|
|
|
2018-09-12 14:45:43 -07:00
|
|
|
pub fn recv_blob(socket: &UdpSocket, r: &SharedBlob) -> io::Result<()> {
|
2018-09-26 09:50:12 -07:00
|
|
|
let mut p = r.write().unwrap();
|
2018-09-12 14:45:43 -07:00
|
|
|
trace!("receiving on {}", socket.local_addr().unwrap());
|
|
|
|
|
|
|
|
let (nrecv, from) = socket.recv_from(&mut p.data)?;
|
|
|
|
p.meta.size = nrecv;
|
|
|
|
p.meta.set_addr(&from);
|
|
|
|
trace!("got {} bytes from {}", nrecv, from);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-09-26 09:50:12 -07:00
|
|
|
pub fn recv_from(socket: &UdpSocket) -> Result<SharedBlobs> {
|
2018-09-03 03:22:47 -07:00
|
|
|
let mut v = Vec::new();
|
2018-04-02 19:32:58 -07:00
|
|
|
//DOCUMENTED SIDE-EFFECT
|
|
|
|
//Performance out of the IO without poll
|
2018-05-25 22:00:47 -07:00
|
|
|
// * block on the socket until it's readable
|
2018-04-02 19:32:58 -07:00
|
|
|
// * set the socket to non blocking
|
|
|
|
// * read until it fails
|
|
|
|
// * set it back to blocking before returning
|
|
|
|
socket.set_nonblocking(false)?;
|
|
|
|
for i in 0..NUM_BLOBS {
|
2018-09-26 09:50:12 -07:00
|
|
|
let r = SharedBlob::default();
|
2018-09-12 14:45:43 -07:00
|
|
|
|
|
|
|
match Blob::recv_blob(socket, &r) {
|
|
|
|
Err(_) if i > 0 => {
|
|
|
|
trace!("got {:?} messages on {}", i, socket.local_addr().unwrap());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Err(e) => {
|
2019-06-21 11:28:52 -07:00
|
|
|
if e.kind() != io::ErrorKind::WouldBlock && e.kind() != io::ErrorKind::TimedOut
|
|
|
|
{
|
2018-09-12 14:45:43 -07:00
|
|
|
info!("recv_from err {:?}", e);
|
2018-04-02 19:32:58 -07:00
|
|
|
}
|
2018-09-12 14:45:43 -07:00
|
|
|
return Err(Error::IO(e));
|
2018-04-02 19:32:58 -07:00
|
|
|
}
|
2018-11-02 14:32:05 -07:00
|
|
|
Ok(()) => {
|
|
|
|
if i == 0 {
|
|
|
|
socket.set_nonblocking(true)?;
|
|
|
|
}
|
|
|
|
}
|
2018-04-02 19:32:58 -07:00
|
|
|
}
|
2018-09-03 03:22:47 -07:00
|
|
|
v.push(r);
|
2018-04-02 19:32:58 -07:00
|
|
|
}
|
|
|
|
Ok(v)
|
|
|
|
}
|
2018-09-18 08:02:57 -07:00
|
|
|
pub fn send_to(socket: &UdpSocket, v: SharedBlobs) -> Result<()> {
|
2018-09-03 03:22:47 -07:00
|
|
|
for r in v {
|
2018-04-02 19:32:58 -07:00
|
|
|
{
|
2018-09-26 09:50:12 -07:00
|
|
|
let p = r.read().unwrap();
|
2018-04-02 19:32:58 -07:00
|
|
|
let a = p.meta.addr();
|
2018-07-11 07:38:57 -07:00
|
|
|
if let Err(e) = socket.send_to(&p.data[..p.meta.size], &a) {
|
2018-07-16 19:31:52 -07:00
|
|
|
warn!(
|
2018-07-11 07:38:57 -07:00
|
|
|
"error sending {} byte packet to {:?}: {:?}",
|
|
|
|
p.meta.size, a, e
|
|
|
|
);
|
|
|
|
Err(e)?;
|
|
|
|
}
|
2018-04-02 19:32:58 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-12 16:43:05 -07:00
|
|
|
impl Signable for Blob {
|
|
|
|
fn pubkey(&self) -> Pubkey {
|
|
|
|
self.id()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signable_data(&self) -> Cow<[u8]> {
|
|
|
|
let end = cmp::max(SIGNABLE_START, self.data_size() as usize);
|
|
|
|
Cow::Borrowed(&self.data[SIGNABLE_START..end])
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_signature(&self) -> Signature {
|
2019-06-19 15:36:06 -07:00
|
|
|
Signature::new(self.get_signature_bytes())
|
2019-06-12 16:43:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn set_signature(&mut self, signature: Signature) {
|
|
|
|
self.data[SIGNATURE_RANGE].copy_from_slice(signature.as_ref())
|
|
|
|
}
|
2019-04-23 16:24:44 -07:00
|
|
|
}
|
|
|
|
|
2019-06-12 16:43:05 -07:00
|
|
|
pub fn index_blobs(blobs: &[SharedBlob], id: &Pubkey, mut blob_index: u64, slot: u64, parent: u64) {
|
2018-11-07 13:18:14 -08:00
|
|
|
// enumerate all the blobs, those are the indices
|
2019-02-25 16:15:53 -08:00
|
|
|
for blob in blobs.iter() {
|
2019-01-15 10:51:53 -08:00
|
|
|
let mut blob = blob.write().unwrap();
|
2019-03-07 09:43:42 -08:00
|
|
|
blob.set_index(blob_index);
|
2019-02-25 16:15:53 -08:00
|
|
|
blob.set_slot(slot);
|
2019-03-07 09:43:42 -08:00
|
|
|
blob.set_parent(parent);
|
2019-02-27 13:37:08 -08:00
|
|
|
blob.set_id(id);
|
2019-03-07 09:43:42 -08:00
|
|
|
blob_index += 1;
|
2018-11-07 13:18:14 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-02 19:32:58 -07:00
|
|
|
#[cfg(test)]
|
2018-06-28 12:10:36 -07:00
|
|
|
mod tests {
|
2019-03-11 03:06:22 -07:00
|
|
|
use super::*;
|
2018-11-16 08:04:46 -08:00
|
|
|
use solana_sdk::hash::Hash;
|
2018-12-03 10:26:28 -08:00
|
|
|
use solana_sdk::signature::{Keypair, KeypairUtil};
|
2019-04-03 08:45:57 -07:00
|
|
|
use solana_sdk::system_transaction;
|
2018-03-26 21:07:11 -07:00
|
|
|
use std::io;
|
|
|
|
use std::io::Write;
|
2019-05-03 11:01:35 -07:00
|
|
|
use std::net::{SocketAddr, UdpSocket};
|
2019-01-10 12:34:48 -08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_packets_set_addr() {
|
|
|
|
// test that the address is actually being updated
|
|
|
|
let send_addr = socketaddr!([127, 0, 0, 1], 123);
|
|
|
|
let packets = vec![Packet::default()];
|
2019-06-27 00:32:32 -07:00
|
|
|
let mut msgs = Packets::new(packets);
|
2019-01-10 12:34:48 -08:00
|
|
|
msgs.set_addr(&send_addr);
|
|
|
|
assert_eq!(SocketAddr::from(msgs.packets[0].meta.addr()), send_addr);
|
|
|
|
}
|
2018-06-28 12:10:36 -07:00
|
|
|
|
2018-04-02 19:32:58 -07:00
|
|
|
#[test]
|
|
|
|
pub fn packet_send_recv() {
|
2019-04-17 14:14:57 -07:00
|
|
|
solana_logger::setup();
|
|
|
|
let recv_socket = UdpSocket::bind("127.0.0.1:0").expect("bind");
|
|
|
|
let addr = recv_socket.local_addr().unwrap();
|
|
|
|
let send_socket = UdpSocket::bind("127.0.0.1:0").expect("bind");
|
|
|
|
let saddr = send_socket.local_addr().unwrap();
|
|
|
|
let mut p = Packets::default();
|
|
|
|
|
|
|
|
p.packets.resize(10, Packet::default());
|
|
|
|
|
|
|
|
for m in p.packets.iter_mut() {
|
2018-04-02 19:32:58 -07:00
|
|
|
m.meta.set_addr(&addr);
|
2018-09-07 20:18:36 -07:00
|
|
|
m.meta.size = PACKET_DATA_SIZE;
|
2018-04-02 19:32:58 -07:00
|
|
|
}
|
2019-04-17 14:14:57 -07:00
|
|
|
p.send_to(&send_socket).unwrap();
|
|
|
|
|
|
|
|
let recvd = p.recv_from(&recv_socket).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(recvd, p.packets.len());
|
|
|
|
|
2019-06-27 00:32:32 -07:00
|
|
|
for m in &p.packets {
|
2018-09-07 20:18:36 -07:00
|
|
|
assert_eq!(m.meta.size, PACKET_DATA_SIZE);
|
2018-04-02 19:32:58 -07:00
|
|
|
assert_eq!(m.meta.addr(), saddr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-16 15:11:53 -07:00
|
|
|
#[test]
|
|
|
|
fn test_to_packets() {
|
2018-11-05 09:50:58 -08:00
|
|
|
let keypair = Keypair::new();
|
|
|
|
let hash = Hash::new(&[1; 32]);
|
2019-05-20 10:03:19 -07:00
|
|
|
let tx = system_transaction::create_user_account(&keypair, &keypair.pubkey(), 1, hash);
|
2018-09-26 09:50:12 -07:00
|
|
|
let rv = to_packets(&vec![tx.clone(); 1]);
|
2018-05-16 15:11:53 -07:00
|
|
|
assert_eq!(rv.len(), 1);
|
2019-04-17 18:15:50 -07:00
|
|
|
assert_eq!(rv[0].packets.len(), 1);
|
2018-05-16 15:11:53 -07:00
|
|
|
|
2018-09-26 09:50:12 -07:00
|
|
|
let rv = to_packets(&vec![tx.clone(); NUM_PACKETS]);
|
2018-05-16 15:11:53 -07:00
|
|
|
assert_eq!(rv.len(), 1);
|
2019-04-17 18:15:50 -07:00
|
|
|
assert_eq!(rv[0].packets.len(), NUM_PACKETS);
|
2018-05-16 15:11:53 -07:00
|
|
|
|
2018-09-26 09:50:12 -07:00
|
|
|
let rv = to_packets(&vec![tx.clone(); NUM_PACKETS + 1]);
|
2018-05-16 15:11:53 -07:00
|
|
|
assert_eq!(rv.len(), 2);
|
2019-04-17 18:15:50 -07:00
|
|
|
assert_eq!(rv[0].packets.len(), NUM_PACKETS);
|
|
|
|
assert_eq!(rv[1].packets.len(), 1);
|
2018-05-16 15:11:53 -07:00
|
|
|
}
|
|
|
|
|
2018-04-02 19:32:58 -07:00
|
|
|
#[test]
|
|
|
|
pub fn blob_send_recv() {
|
|
|
|
trace!("start");
|
|
|
|
let reader = UdpSocket::bind("127.0.0.1:0").expect("bind");
|
|
|
|
let addr = reader.local_addr().unwrap();
|
|
|
|
let sender = UdpSocket::bind("127.0.0.1:0").expect("bind");
|
2018-09-26 09:50:12 -07:00
|
|
|
let p = SharedBlob::default();
|
|
|
|
p.write().unwrap().meta.set_addr(&addr);
|
|
|
|
p.write().unwrap().meta.size = 1024;
|
2018-09-03 03:22:47 -07:00
|
|
|
let v = vec![p];
|
2018-09-18 08:02:57 -07:00
|
|
|
Blob::send_to(&sender, v).unwrap();
|
2018-04-02 19:32:58 -07:00
|
|
|
trace!("send_to");
|
2018-09-26 09:50:12 -07:00
|
|
|
let rv = Blob::recv_from(&reader).unwrap();
|
2018-04-02 19:32:58 -07:00
|
|
|
trace!("recv_from");
|
|
|
|
assert_eq!(rv.len(), 1);
|
2018-09-26 09:50:12 -07:00
|
|
|
assert_eq!(rv[0].read().unwrap().meta.size, 1024);
|
2018-04-02 19:32:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(all(feature = "ipv6", test))]
|
|
|
|
#[test]
|
|
|
|
pub fn blob_ipv6_send_recv() {
|
|
|
|
let reader = UdpSocket::bind("[::1]:0").expect("bind");
|
|
|
|
let addr = reader.local_addr().unwrap();
|
|
|
|
let sender = UdpSocket::bind("[::1]:0").expect("bind");
|
2018-09-26 09:50:12 -07:00
|
|
|
let p = SharedBlob::default();
|
|
|
|
p.as_mut().unwrap().meta.set_addr(&addr);
|
|
|
|
p.as_mut().unwrap().meta.size = 1024;
|
2018-04-02 19:32:58 -07:00
|
|
|
let mut v = VecDeque::default();
|
|
|
|
v.push_back(p);
|
|
|
|
Blob::send_to(&r, &sender, &mut v).unwrap();
|
2018-09-26 09:50:12 -07:00
|
|
|
let mut rv = Blob::recv_from(&reader).unwrap();
|
2018-04-02 19:32:58 -07:00
|
|
|
let rp = rv.pop_front().unwrap();
|
2018-09-18 08:02:57 -07:00
|
|
|
assert_eq!(rp.as_mut().meta.size, 1024);
|
2018-04-02 19:32:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn debug_trait() {
|
|
|
|
write!(io::sink(), "{:?}", Packet::default()).unwrap();
|
|
|
|
write!(io::sink(), "{:?}", Packets::default()).unwrap();
|
|
|
|
write!(io::sink(), "{:?}", Blob::default()).unwrap();
|
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
pub fn blob_test() {
|
|
|
|
let mut b = Blob::default();
|
2019-01-30 20:18:28 -08:00
|
|
|
b.set_index(<u64>::max_value());
|
|
|
|
assert_eq!(b.index(), <u64>::max_value());
|
2018-04-02 19:32:58 -07:00
|
|
|
b.data_mut()[0] = 1;
|
|
|
|
assert_eq!(b.data()[0], 1);
|
2019-01-30 20:18:28 -08:00
|
|
|
assert_eq!(b.index(), <u64>::max_value());
|
2018-08-13 19:45:25 -07:00
|
|
|
assert_eq!(b.meta, Meta::default());
|
2018-04-02 19:32:58 -07:00
|
|
|
}
|
2019-02-12 10:56:48 -08:00
|
|
|
#[test]
|
|
|
|
fn test_blob_forward() {
|
|
|
|
let mut b = Blob::default();
|
|
|
|
assert!(b.should_forward());
|
2019-04-20 16:44:06 -07:00
|
|
|
b.set_forwarded(true);
|
|
|
|
assert!(!b.should_forward());
|
2019-02-12 10:56:48 -08:00
|
|
|
}
|
2018-04-02 19:32:58 -07:00
|
|
|
|
2019-07-11 13:58:33 -07:00
|
|
|
#[test]
|
|
|
|
fn test_blob_erasure_config() {
|
|
|
|
let mut b = Blob::default();
|
|
|
|
let config = ErasureConfig::new(32, 16);
|
|
|
|
b.set_erasure_config(&config);
|
|
|
|
|
|
|
|
assert_eq!(config, b.erasure_config());
|
|
|
|
}
|
|
|
|
|
2019-04-14 17:30:08 -07:00
|
|
|
#[test]
|
|
|
|
fn test_blob_data_align() {
|
|
|
|
assert_eq!(std::mem::align_of::<BlobData>(), BLOB_DATA_ALIGN);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_packet_partial_eq() {
|
2019-05-29 10:08:35 -07:00
|
|
|
let mut p1 = Packet::default();
|
2019-04-14 17:30:08 -07:00
|
|
|
let mut p2 = Packet::default();
|
|
|
|
|
2019-05-29 10:08:35 -07:00
|
|
|
p1.meta.size = 1;
|
|
|
|
p1.data[0] = 0;
|
|
|
|
|
|
|
|
p2.meta.size = 1;
|
|
|
|
p2.data[0] = 0;
|
|
|
|
|
2019-04-14 17:30:08 -07:00
|
|
|
assert!(p1 == p2);
|
2019-05-29 10:08:35 -07:00
|
|
|
|
|
|
|
p2.data[0] = 4;
|
2019-04-14 17:30:08 -07:00
|
|
|
assert!(p1 != p2);
|
|
|
|
}
|
2019-05-29 10:08:35 -07:00
|
|
|
|
2019-04-14 17:30:08 -07:00
|
|
|
#[test]
|
|
|
|
fn test_blob_partial_eq() {
|
|
|
|
let p1 = Blob::default();
|
|
|
|
let mut p2 = Blob::default();
|
|
|
|
|
|
|
|
assert!(p1 == p2);
|
|
|
|
p2.data[1] = 4;
|
|
|
|
assert!(p1 != p2);
|
|
|
|
}
|
2019-06-12 16:43:05 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_sign_blob() {
|
|
|
|
let mut b = Blob::default();
|
|
|
|
let k = Keypair::new();
|
|
|
|
let p = k.pubkey();
|
|
|
|
b.set_id(&p);
|
|
|
|
b.sign(&k);
|
|
|
|
assert!(b.verify());
|
|
|
|
|
|
|
|
// Set a bigger chunk of data to sign
|
|
|
|
b.set_size(80);
|
|
|
|
b.sign(&k);
|
|
|
|
assert!(b.verify());
|
|
|
|
}
|
2019-06-27 00:32:32 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_packets_reset() {
|
|
|
|
let mut packets = Packets::default();
|
|
|
|
packets.packets.resize(10, Packet::default());
|
|
|
|
assert_eq!(packets.packets.len(), 10);
|
|
|
|
packets.reset();
|
|
|
|
assert_eq!(packets.packets.len(), 0);
|
|
|
|
}
|
2019-07-12 13:43:19 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_version() {
|
|
|
|
let mut b = Blob::default();
|
|
|
|
assert_eq!(b.version(), 0);
|
|
|
|
b.set_version(1);
|
|
|
|
assert_eq!(b.version(), 1);
|
|
|
|
}
|
2018-04-02 19:32:58 -07:00
|
|
|
}
|