Skip to content

Commit

Permalink
extract packet crate (#3029)
Browse files Browse the repository at this point in the history
* extract packet crate

* typo

* sort deps

* update digest

* add doc_auto_cfg like in #3121

* fix frozen-abi
  • Loading branch information
kevinheavey authored Oct 28, 2024
1 parent 10bebfa commit 8045d36
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 14 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 @@ -122,6 +122,7 @@ members = [
"sdk/native-token",
"sdk/package-metadata",
"sdk/package-metadata-macro",
"sdk/packet",
"sdk/precompile-error",
"sdk/program",
"sdk/program-entrypoint",
Expand Down Expand Up @@ -237,6 +238,7 @@ bytes = "1.8"
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 @@ -447,6 +449,7 @@ solana-nohash-hasher = "0.2.1"
solana-notifier = { path = "notifier", version = "=2.1.0" }
solana-package-metadata = { path = "sdk/package-metadata", 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
2 changes: 1 addition & 1 deletion core/src/banking_trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub struct BankingTracer {
#[cfg_attr(
feature = "frozen-abi",
derive(AbiExample),
frozen_abi(digest = "F5GH1poHbPqipU4DB3MczhSxHZw4o27f3C7QnMVirFci")
frozen_abi(digest = "6PCDw6YSEivfbwhbPmE4NAsXb88ZX6hkFnruP8B38nma")
)]
#[derive(Serialize, Deserialize, Debug)]
pub struct TimedTracedEvent(pub std::time::SystemTime, pub TracedEvent);
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 @@ -101,6 +101,7 @@ solana-frozen-abi-macro = { workspace = true, optional = true, features = [
] }
solana-instruction = { workspace = true }
solana-native-token = { workspace = true }
solana-packet = { workspace = true, features = ["bincode", "serde"] }
solana-precompile-error = { workspace = true, optional = true }
solana-program = { workspace = true }
solana-program-memory = { workspace = true }
Expand Down
44 changes: 44 additions & 0 deletions sdk/packet/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
[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 }
serde = { workspace = true, optional = true }
serde_derive = { workspace = true, optional = true }
serde_with = { workspace = true, optional = true, features = ["macros"] }
solana-frozen-abi = { workspace = true, optional = true, features = ["frozen-abi"] }
solana-frozen-abi-macro = { workspace = true, optional = true, features = ["frozen-abi"] }

[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"]
all-features = true
rustdoc-args = ["--cfg=docsrs"]

[lints]
workspace = true
41 changes: 29 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,24 @@
//! The definition of a Solana network packet.
#![cfg_attr(feature = "frozen-abi", feature(min_specialization))]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]

#[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 +30,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 @@ -39,7 +48,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 @@ -88,14 +98,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 @@ -144,19 +158,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 @@ -165,6 +181,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 @@ -81,7 +81,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 @@ -120,6 +119,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_pubkey::pubkey` instead")]
Expand Down

0 comments on commit 8045d36

Please sign in to comment.