diff --git a/Cargo.lock b/Cargo.lock index 8ed03ef0b0f2e0..52fc29e76191d9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1312,6 +1312,17 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" +[[package]] +name = "cfg_eval" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "45565fc9416b9896014f5732ac776f810ee53a66730c17e4020c3ec064a8f88f" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.79", +] + [[package]] name = "chrono" version = "0.4.38" @@ -7238,6 +7249,22 @@ dependencies = [ "toml 0.8.12", ] +[[package]] +name = "solana-packet" +version = "2.1.0" +dependencies = [ + "bincode", + "bitflags 2.6.0", + "cfg_eval", + "serde", + "serde_derive", + "serde_with", + "solana-frozen-abi", + "solana-frozen-abi-macro", + "solana-packet", + "static_assertions", +] + [[package]] name = "solana-perf" version = "2.1.0" @@ -7978,6 +8005,7 @@ dependencies = [ "solana-instruction", "solana-logger", "solana-native-token", + "solana-packet", "solana-precompile-error", "solana-program", "solana-program-memory", diff --git a/Cargo.toml b/Cargo.toml index 7a1f5b02c8f719..e1709009279de5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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", @@ -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" @@ -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" } diff --git a/core/src/banking_trace.rs b/core/src/banking_trace.rs index 150e9a33e1940f..6e0797c8c3842f 100644 --- a/core/src/banking_trace.rs +++ b/core/src/banking_trace.rs @@ -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); diff --git a/programs/sbf/Cargo.lock b/programs/sbf/Cargo.lock index 8dc6238a8e8504..1db2d7f98f724f 100644 --- a/programs/sbf/Cargo.lock +++ b/programs/sbf/Cargo.lock @@ -961,6 +961,17 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" +[[package]] +name = "cfg_eval" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "45565fc9416b9896014f5732ac776f810ee53a66730c17e4020c3ec064a8f88f" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.58", +] + [[package]] name = "chrono" version = "0.4.38" @@ -5651,6 +5662,18 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b8a731ed60e89177c8a7ab05fe0f1511cedd3e70e773f288f9de33a9cfdc21e" +[[package]] +name = "solana-packet" +version = "2.1.0" +dependencies = [ + "bincode", + "bitflags 2.6.0", + "cfg_eval", + "serde", + "serde_derive", + "serde_with", +] + [[package]] name = "solana-perf" version = "2.1.0" @@ -6738,6 +6761,7 @@ dependencies = [ "solana-feature-set", "solana-instruction", "solana-native-token", + "solana-packet", "solana-precompile-error", "solana-program", "solana-program-memory", diff --git a/sdk/Cargo.toml b/sdk/Cargo.toml index 0c8352fc02661a..98000e4ee968af 100644 --- a/sdk/Cargo.toml +++ b/sdk/Cargo.toml @@ -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 } diff --git a/sdk/packet/Cargo.toml b/sdk/packet/Cargo.toml new file mode 100644 index 00000000000000..b410a6df56f9af --- /dev/null +++ b/sdk/packet/Cargo.toml @@ -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 diff --git a/sdk/src/packet.rs b/sdk/packet/src/lib.rs similarity index 88% rename from sdk/src/packet.rs rename to sdk/packet/src/lib.rs index f0258710dcf423..0f29fe7d327011 100644 --- a/sdk/src/packet.rs +++ b/sdk/packet/src/lib.rs @@ -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); @@ -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; @@ -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, @@ -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, } @@ -144,19 +158,21 @@ impl Packet { &mut self.meta } - pub fn from_data(dest: Option<&SocketAddr>, data: T) -> Result { + #[cfg(feature = "bincode")] + pub fn from_data(dest: Option<&SocketAddr>, data: T) -> Result { let mut packet = Self::default(); Self::populate_packet(&mut packet, dest, &data)?; Ok(packet) } - pub fn populate_packet( + #[cfg(feature = "bincode")] + pub fn populate_packet( &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 { @@ -165,6 +181,7 @@ impl Packet { Ok(()) } + #[cfg(feature = "bincode")] pub fn deserialize_slice(&self, index: I) -> Result where T: serde::de::DeserializeOwned, diff --git a/sdk/src/lib.rs b/sdk/src/lib.rs index e1d9503a75de20..3d192fc02a0c3e 100644 --- a/sdk/src/lib.rs +++ b/sdk/src/lib.rs @@ -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; @@ -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")]