Skip to content

Commit

Permalink
extract packet crate
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinheavey committed Sep 29, 2024
1 parent 9c20984 commit cb2ac4f
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 13 deletions.
28 changes: 28 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ members = [
"sdk/macro",
"sdk/msg",
"sdk/package-metadata-macro",
"sdk/packet",
"sdk/program",
"sdk/program-error",
"sdk/program-memory",
Expand Down Expand Up @@ -221,6 +222,7 @@ bytes = "1.7"
bzip2 = "0.4.4"
caps = "0.5.5"
cargo_metadata = "0.15.4"
cfg_eval = "0.1.2"
chrono = { version = "0.4.38", default-features = false }
chrono-humanize = "0.2.3"
clap = "2.33.1"
Expand Down Expand Up @@ -421,6 +423,7 @@ solana-net-utils = { path = "net-utils", version = "=2.1.0" }
solana-nohash-hasher = "0.2.1"
solana-notifier = { path = "notifier", version = "=2.1.0" }
solana-package-metadata-macro = { path = "sdk/package-metadata-macro", version = "=2.1.0" }
solana-packet = { path = "sdk/packet", version = "=2.1.0" }
solana-perf = { path = "perf", version = "=2.1.0" }
solana-poh = { path = "poh", version = "=2.1.0" }
solana-poseidon = { path = "poseidon", version = "=2.1.0" }
Expand Down
24 changes: 24 additions & 0 deletions programs/sbf/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ solana-frozen-abi = { workspace = true, optional = true, features = [
solana-frozen-abi-macro = { workspace = true, optional = true, features = [
"frozen-abi",
] }
solana-packet = { workspace = true, features = ["bincode", "serde"] }
solana-program = { workspace = true }
solana-program-memory = { workspace = true }
solana-sanitize = { workspace = true }
Expand Down
42 changes: 42 additions & 0 deletions sdk/packet/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[package]
name = "solana-packet"
description = "The definition of a Solana network packet."
documentation = "https://docs.rs/solana-packet"
version = { workspace = true }
authors = { workspace = true }
repository = { workspace = true }
homepage = { workspace = true }
license = { workspace = true }
edition = { workspace = true }

[dependencies]
bincode = { workspace = true, optional = true }
bitflags = { workspace = true }
cfg_eval = { workspace = true, optional = true }
solana-frozen-abi = { workspace = true, optional = true }
solana-frozen-abi-macro = { workspace = true, optional = true }
serde = { workspace = true, optional = true }
serde_derive = { workspace = true, optional = true }
serde_with = { workspace = true, optional = true, features = ["macros"] }

[dev-dependencies]
solana-packet = { path = ".", features = ["dev-context-only-utils"] }
static_assertions = { workspace = true }

[features]
bincode = ["dep:bincode", "serde"]
dev-context-only-utils = ["bincode"]
serde = [
"bitflags/serde",
"dep:cfg_eval",
"dep:serde",
"dep:serde_derive",
"dep:serde_with",
]
frozen-abi = ["dep:solana-frozen-abi", "dep:solana-frozen-abi-macro"]

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[lints]
workspace = true
40 changes: 28 additions & 12 deletions sdk/src/packet.rs → sdk/packet/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
//! The definition of a Solana network packet.
#![cfg_attr(feature = "frozen-abi", feature(min_specialization))]

#[cfg(feature = "bincode")]
use bincode::{Options, Result};
#[cfg(feature = "frozen-abi")]
use solana_frozen_abi_macro::AbiEXample;
use {
bincode::{Options, Result},
bitflags::bitflags,
serde::{Deserialize, Serialize},
serde_with::{serde_as, Bytes},
std::{
fmt, io,
fmt,
net::{IpAddr, Ipv4Addr, SocketAddr},
slice::SliceIndex,
},
};
#[cfg(feature = "serde")]
use {
serde_derive::{Deserialize, Serialize},
serde_with::{serde_as, Bytes},
};

#[cfg(test)]
static_assertions::const_assert_eq!(PACKET_DATA_SIZE, 1232);
Expand All @@ -22,7 +29,8 @@ pub const PACKET_DATA_SIZE: usize = 1280 - 40 - 8;

bitflags! {
#[repr(C)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct PacketFlags: u8 {
const DISCARD = 0b0000_0001;
const FORWARDED = 0b0000_0010;
Expand All @@ -41,7 +49,8 @@ bitflags! {
}

#[cfg_attr(feature = "frozen-abi", derive(AbiExample))]
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[derive(Clone, Debug, PartialEq, Eq)]
#[repr(C)]
pub struct Meta {
pub size: usize,
Expand Down Expand Up @@ -90,14 +99,18 @@ impl ::solana_frozen_abi::abi_example::EvenAsOpaque for PacketFlags {
// https://github.com/serde-rs/serde/pull/1860
// ryoqun's dirty experiments:
// https://github.com/ryoqun/serde-array-comparisons
#[serde_as]
//
// We use the cfg_eval crate as advised by the serde_with guide:
// https://docs.rs/serde_with/latest/serde_with/guide/serde_as/index.html#gating-serde_as-on-features
#[cfg_attr(feature = "serde", cfg_eval::cfg_eval, serde_as)]
#[cfg_attr(feature = "frozen-abi", derive(AbiExample))]
#[derive(Clone, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[derive(Clone, Eq)]
#[repr(C)]
pub struct Packet {
// Bytes past Packet.meta.size are not valid to read from.
// Use Packet.data(index) to read from the buffer.
#[serde_as(as = "Bytes")]
#[cfg_attr(feature = "serde", serde_as(as = "Bytes"))]
buffer: [u8; PACKET_DATA_SIZE],
meta: Meta,
}
Expand Down Expand Up @@ -146,19 +159,21 @@ impl Packet {
&mut self.meta
}

pub fn from_data<T: Serialize>(dest: Option<&SocketAddr>, data: T) -> Result<Self> {
#[cfg(feature = "bincode")]
pub fn from_data<T: serde::Serialize>(dest: Option<&SocketAddr>, data: T) -> Result<Self> {
let mut packet = Self::default();
Self::populate_packet(&mut packet, dest, &data)?;
Ok(packet)
}

pub fn populate_packet<T: Serialize>(
#[cfg(feature = "bincode")]
pub fn populate_packet<T: serde::Serialize>(
&mut self,
dest: Option<&SocketAddr>,
data: &T,
) -> Result<()> {
debug_assert!(!self.meta.discard());
let mut wr = io::Cursor::new(self.buffer_mut());
let mut wr = std::io::Cursor::new(self.buffer_mut());
bincode::serialize_into(&mut wr, data)?;
self.meta.size = wr.position() as usize;
if let Some(dest) = dest {
Expand All @@ -167,6 +182,7 @@ impl Packet {
Ok(())
}

#[cfg(feature = "bincode")]
pub fn deserialize_slice<T, I>(&self, index: I) -> Result<T>
where
T: serde::de::DeserializeOwned,
Expand Down
3 changes: 2 additions & 1 deletion sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ pub mod native_loader;
pub mod net;
pub mod nonce_account;
pub mod offchain_message;
pub mod packet;
pub mod poh_config;
pub mod precompiles;
pub mod program_utils;
Expand Down Expand Up @@ -115,6 +114,8 @@ pub use solana_decode_error as decode_error;
pub use solana_derivation_path as derivation_path;
#[deprecated(since = "2.1.0", note = "Use `solana-feature-set` crate instead")]
pub use solana_feature_set as feature_set;
#[deprecated(since = "2.1.0", note = "Use `solana-packet` crate instead")]
pub use solana_packet as packet;
#[deprecated(since = "2.1.0", note = "Use `solana-program-memory` crate instead")]
pub use solana_program_memory as program_memory;
#[deprecated(since = "2.1.0", note = "Use `solana-sanitize` crate instead")]
Expand Down

0 comments on commit cb2ac4f

Please sign in to comment.