Forward transactions as packets instead of blobs (#5334)
* Forward transactions as packets instead of blobs * clippy
This commit is contained in:
@@ -12,13 +12,11 @@ pub use solana_sdk::packet::PACKET_DATA_SIZE;
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
use solana_sdk::signature::Signable;
|
||||
use solana_sdk::signature::Signature;
|
||||
use std::borrow::Borrow;
|
||||
use std::borrow::Cow;
|
||||
use std::cmp;
|
||||
use std::fmt;
|
||||
use std::io;
|
||||
use std::io::Cursor;
|
||||
use std::io::Write;
|
||||
use std::mem;
|
||||
use std::mem::size_of;
|
||||
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, UdpSocket};
|
||||
@@ -365,18 +363,6 @@ pub fn to_shared_blobs<T: Serialize>(rsps: Vec<(T, SocketAddr)>) -> Result<Share
|
||||
Ok(blobs)
|
||||
}
|
||||
|
||||
pub fn packets_to_blobs<T: Borrow<Packet>>(packets: &[T]) -> Vec<Blob> {
|
||||
let mut current_index = 0;
|
||||
let mut blobs = vec![];
|
||||
while current_index < packets.len() {
|
||||
let mut blob = Blob::default();
|
||||
current_index += blob.store_packets(&packets[current_index..]) as usize;
|
||||
blobs.push(blob);
|
||||
}
|
||||
|
||||
blobs
|
||||
}
|
||||
|
||||
macro_rules! range {
|
||||
($prev:expr, $type:ident) => {
|
||||
$prev..$prev + size_of::<$type>()
|
||||
@@ -558,52 +544,6 @@ impl Blob {
|
||||
&self.data[SIGNATURE_RANGE]
|
||||
}
|
||||
|
||||
pub fn store_packets<T: Borrow<Packet>>(&mut self, packets: &[T]) -> u64 {
|
||||
let size = self.size();
|
||||
let mut cursor = Cursor::new(&mut self.data_mut()[size..]);
|
||||
let mut written = 0;
|
||||
let mut last_index = 0;
|
||||
for packet in packets {
|
||||
if bincode::serialize_into(&mut cursor, &packet.borrow().meta.size).is_err() {
|
||||
break;
|
||||
}
|
||||
let packet = packet.borrow();
|
||||
if cursor.write_all(&packet.data[..packet.meta.size]).is_err() {
|
||||
break;
|
||||
}
|
||||
|
||||
written = cursor.position() as usize;
|
||||
last_index += 1;
|
||||
}
|
||||
|
||||
self.set_size(size + written);
|
||||
last_index
|
||||
}
|
||||
|
||||
// other side of store_packets
|
||||
pub fn load_packets(&self, packets: &mut PinnedVec<Packet>) {
|
||||
// rough estimate
|
||||
let mut pos = 0;
|
||||
let size_len = bincode::serialized_size(&0usize).unwrap() as usize;
|
||||
|
||||
while pos + size_len < self.size() {
|
||||
let size: usize = bincode::deserialize_from(&self.data()[pos..]).unwrap();
|
||||
|
||||
pos += size_len;
|
||||
|
||||
if size > PACKET_DATA_SIZE || pos + size > self.size() {
|
||||
break;
|
||||
}
|
||||
|
||||
let mut packet = Packet::default();
|
||||
packet.meta.size = size;
|
||||
packet.data[..size].copy_from_slice(&self.data()[pos..pos + size]);
|
||||
|
||||
pos += size;
|
||||
packets.push(packet);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn recv_blob(socket: &UdpSocket, r: &SharedBlob) -> io::Result<()> {
|
||||
let mut p = r.write().unwrap();
|
||||
trace!("receiving on {}", socket.local_addr().unwrap());
|
||||
@@ -701,8 +641,6 @@ pub fn index_blobs(blobs: &[SharedBlob], id: &Pubkey, mut blob_index: u64, slot:
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use bincode;
|
||||
use rand::Rng;
|
||||
use solana_sdk::hash::Hash;
|
||||
use solana_sdk::signature::{Keypair, KeypairUtil};
|
||||
use solana_sdk::system_transaction;
|
||||
@@ -834,62 +772,6 @@ mod tests {
|
||||
assert_eq!(config, b.erasure_config());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_store_blobs_max() {
|
||||
let serialized_size_size = bincode::serialized_size(&0usize).unwrap() as usize;
|
||||
let serialized_packet_size = serialized_size_size + PACKET_DATA_SIZE;
|
||||
let num_packets = (BLOB_SIZE - BLOB_HEADER_SIZE) / serialized_packet_size + 1;
|
||||
let mut blob = Blob::default();
|
||||
let packets: Vec<_> = (0..num_packets)
|
||||
.map(|_| {
|
||||
let mut packet = Packet::default();
|
||||
packet.meta.size = PACKET_DATA_SIZE;
|
||||
packet
|
||||
})
|
||||
.collect();
|
||||
|
||||
// Everything except the last packet should have been written
|
||||
assert_eq!(blob.store_packets(&packets[..]), (num_packets - 1) as u64);
|
||||
|
||||
blob = Blob::default();
|
||||
// Store packets such that blob only has room for one more
|
||||
assert_eq!(
|
||||
blob.store_packets(&packets[..num_packets - 2]),
|
||||
(num_packets - 2) as u64
|
||||
);
|
||||
|
||||
// Fill the last packet in the blob
|
||||
assert_eq!(blob.store_packets(&packets[..num_packets - 2]), 1);
|
||||
|
||||
// Blob is now full
|
||||
assert_eq!(blob.store_packets(&packets), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_packets_to_blobs() {
|
||||
let mut rng = rand::thread_rng();
|
||||
|
||||
let packets: Vec<_> = (0..2)
|
||||
.map(|_| {
|
||||
let mut packet = Packet::default();
|
||||
packet.meta.size = rng.gen_range(1, PACKET_DATA_SIZE);
|
||||
for i in 0..packet.meta.size {
|
||||
packet.data[i] = rng.gen_range(1, std::u8::MAX);
|
||||
}
|
||||
packet
|
||||
})
|
||||
.collect();
|
||||
|
||||
let blobs = packets_to_blobs(&packets[..]);
|
||||
|
||||
let mut reconstructed_packets = PinnedVec::default();
|
||||
blobs
|
||||
.iter()
|
||||
.for_each(|b| b.load_packets(&mut reconstructed_packets));
|
||||
|
||||
assert_eq!(reconstructed_packets[..], packets[..]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_blob_data_align() {
|
||||
assert_eq!(std::mem::align_of::<BlobData>(), BLOB_DATA_ALIGN);
|
||||
|
Reference in New Issue
Block a user