Skip to content

Commit

Permalink
Add another helper to init packet from [&u8]
Browse files Browse the repository at this point in the history
  • Loading branch information
steviez committed Nov 7, 2024
1 parent 9780e08 commit 8186cbb
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 10 deletions.
2 changes: 1 addition & 1 deletion entry/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ fn start_verify_transactions_gpu(
uninitialized_packets
.zip(transaction_iter)
.all(|(uninit_packet, tx)| {
Packet::init_packet(uninit_packet, &tx, None).is_ok()
Packet::init_packet_from_data(uninit_packet, &tx, None).is_ok()
});

if all_packets_initialized {
Expand Down
53 changes: 44 additions & 9 deletions sdk/packet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ use solana_frozen_abi_macro::AbiExample;
use {
bitflags::bitflags,
std::{
fmt, io, mem,
fmt,
io::{self, Write},
mem,
net::{IpAddr, Ipv4Addr, SocketAddr},
ptr,
slice::SliceIndex,
Expand Down Expand Up @@ -162,7 +164,7 @@ impl Packet {
#[cfg(feature = "bincode")]
/// Initializes a std::mem::MaybeUninit<Packet> such that the Packet can
/// be safely extracted via methods such as MaybeUninit::assume_init()
pub fn init_packet<T: serde::Serialize>(
pub fn init_packet_from_data<T: serde::Serialize>(
packet: &mut mem::MaybeUninit<Packet>,
data: &T,
addr: Option<&SocketAddr>,
Expand All @@ -176,24 +178,57 @@ impl Packet {
} else {
(IpAddr::V4(Ipv4Addr::UNSPECIFIED), 0)
};
// SAFETY: Access the field by pointer as creating a reference to
// and/or within the uninitialized Packet is undefined behavior
unsafe {
ptr::addr_of_mut!((*packet.as_mut_ptr()).meta).write(Meta {
Self::init_packet_meta(
packet,
Meta {
size: serialized_size,
addr: ip,
port,
flags: PacketFlags::empty(),
});
}
},
);

Ok(())
}

pub fn init_packet_from_bytes(
packet: &mut mem::MaybeUninit<Packet>,
bytes: &[u8],
addr: Option<&SocketAddr>,
) -> io::Result<()> {
let mut writer = PacketWriter::new_from_uninit_packet(packet);
let num_bytes_written = writer.write(bytes)?;
debug_assert_eq!(bytes.len(), num_bytes_written);

let size = writer.position();
let (ip, port) = if let Some(addr) = addr {
(addr.ip(), addr.port())
} else {
(IpAddr::V4(Ipv4Addr::UNSPECIFIED), 0)
};
Self::init_packet_meta(
packet,
Meta {
size,
addr: ip,
port,
flags: PacketFlags::empty(),
},
);

Ok(())
}

fn init_packet_meta(packet: &mut mem::MaybeUninit<Packet>, meta: Meta) {
// SAFETY: Access the field by pointer as creating a reference to
// and/or within the uninitialized Packet is undefined behavior
unsafe { ptr::addr_of_mut!((*packet.as_mut_ptr()).meta).write(meta) };
}

#[cfg(feature = "bincode")]
pub fn from_data<T: serde::Serialize>(dest: Option<&SocketAddr>, data: T) -> Result<Self> {
let mut packet = mem::MaybeUninit::uninit();
Self::init_packet(&mut packet, &data, dest)?;
Self::init_packet_from_data(&mut packet, &data, dest)?;
// SAFETY: init_packet_from_data() just initialized the packet
unsafe { Ok(packet.assume_init()) }
}
Expand Down

0 comments on commit 8186cbb

Please sign in to comment.