adds bitflags to Packet.Meta

Instead of a separate bool type for each flag, all the flags can be
encoded in a type-safe bitflags encoded in a single u8:
https://github.com/solana-labs/solana/blob/d6ec103be/sdk/src/packet.rs#L19-L31
This commit is contained in:
behzad nouri
2022-01-02 12:10:32 -05:00
parent 73a7741c49
commit 01a096adc8
18 changed files with 130 additions and 92 deletions

View File

@ -1,5 +1,6 @@
use {
bincode::Result,
bitflags::bitflags,
serde::Serialize,
std::{
fmt, io,
@ -13,17 +14,24 @@ use {
/// 8 bytes is the size of the fragment header
pub const PACKET_DATA_SIZE: usize = 1280 - 40 - 8;
bitflags! {
#[repr(C)]
pub struct PacketFlags: u8 {
const DISCARD = 0b00000001;
const FORWARDED = 0b00000010;
const REPAIR = 0b00000100;
const SIMPLE_VOTE_TX = 0b00001000;
const TRACER_TX = 0b00010000;
}
}
#[derive(Clone, Debug, PartialEq)]
#[repr(C)]
pub struct Meta {
pub size: usize,
pub forwarded: bool,
pub repair: bool,
pub discard: bool,
pub addr: IpAddr,
pub port: u16,
pub is_tracer_tx: bool,
pub is_simple_vote_tx: bool,
pub flags: PacketFlags,
}
#[derive(Clone)]
@ -98,19 +106,45 @@ impl Meta {
self.addr = socket_addr.ip();
self.port = socket_addr.port();
}
#[inline]
pub fn discard(&self) -> bool {
self.flags.contains(PacketFlags::DISCARD)
}
#[inline]
pub fn set_discard(&mut self, discard: bool) {
self.flags.set(PacketFlags::DISCARD, discard);
}
#[inline]
pub fn forwarded(&self) -> bool {
self.flags.contains(PacketFlags::FORWARDED)
}
#[inline]
pub fn repair(&self) -> bool {
self.flags.contains(PacketFlags::REPAIR)
}
#[inline]
pub fn is_simple_vote_tx(&self) -> bool {
self.flags.contains(PacketFlags::SIMPLE_VOTE_TX)
}
#[inline]
pub fn is_tracer_tx(&self) -> bool {
self.flags.contains(PacketFlags::TRACER_TX)
}
}
impl Default for Meta {
fn default() -> Self {
Self {
size: 0,
forwarded: false,
repair: false,
discard: false,
addr: IpAddr::V4(Ipv4Addr::UNSPECIFIED),
port: 0,
is_tracer_tx: false,
is_simple_vote_tx: false,
flags: PacketFlags::empty(),
}
}
}