diff --git a/Cargo.lock b/Cargo.lock index b60b5f18b34b32..d88d25b668dad6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7229,6 +7229,7 @@ dependencies = [ "solana-sdk-macro", "solana-secp256k1-recover", "solana-serde-varint", + "solana-serialize-utils", "solana-sha256-hasher", "solana-short-vec", "static_assertions", @@ -7839,6 +7840,19 @@ dependencies = [ "solana-short-vec", ] +[[package]] +name = "solana-serialize-utils" +version = "2.1.0" +dependencies = [ + "bincode", + "borsh 1.5.1", + "rand 0.8.5", + "serde", + "solana-instruction", + "solana-pubkey", + "solana-sanitize", +] + [[package]] name = "solana-sha256-hasher" version = "2.1.0" diff --git a/Cargo.toml b/Cargo.toml index 3f22cf63d50ca6..bcad7d79f20d56 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -120,6 +120,7 @@ members = [ "sdk/program-option", "sdk/pubkey", "sdk/serde-varint", + "sdk/serialize-utils", "sdk/sha256-hasher", "sdk/signature", "send-transaction-service", @@ -207,6 +208,7 @@ bincode = "1.3.3" bitflags = { version = "2.6.0" } blake3 = "1.5.4" borsh = { version = "1.5.1", features = ["derive", "unstable__schema"] } +borsh0-10 = { package = "borsh", version = "0.10.3" } bs58 = { version = "0.5.1", default-features = false } bv = "0.11.1" byte-unit = "4.0.19" @@ -433,6 +435,7 @@ solana-rayon-threadlimit = { path = "rayon-threadlimit", version = "=2.1.0" } solana-remote-wallet = { path = "remote-wallet", version = "=2.1.0", default-features = false } solana-sanitize = { path = "sanitize", version = "=2.1.0" } solana-serde-varint = { path = "sdk/serde-varint", version = "=2.1.0" } +solana-serialize-utils = { path = "sdk/serialize-utils", version = "=2.1.0" } solana-sha256-hasher = { path = "sdk/sha256-hasher", version = "=2.1.0" } solana-signature = { path = "sdk/signature", version = "=2.1.0", default-features = false } solana-timings = { path = "timings", version = "=2.1.0" } diff --git a/programs/sbf/Cargo.lock b/programs/sbf/Cargo.lock index 05e8a5b9403ad9..5c20c5febd509c 100644 --- a/programs/sbf/Cargo.lock +++ b/programs/sbf/Cargo.lock @@ -5640,6 +5640,7 @@ dependencies = [ "solana-sdk-macro", "solana-secp256k1-recover", "solana-serde-varint", + "solana-serialize-utils", "solana-sha256-hasher", "solana-short-vec", "thiserror", @@ -6621,6 +6622,15 @@ dependencies = [ "serde", ] +[[package]] +name = "solana-serialize-utils" +version = "2.1.0" +dependencies = [ + "solana-instruction", + "solana-pubkey", + "solana-sanitize", +] + [[package]] name = "solana-sha256-hasher" version = "2.1.0" diff --git a/sdk/program/Cargo.toml b/sdk/program/Cargo.toml index dd8b6aaa9ae227..345ad1348c491b 100644 --- a/sdk/program/Cargo.toml +++ b/sdk/program/Cargo.toml @@ -15,7 +15,7 @@ rust-version = "1.79.0" # solana platform-tools rust ve bincode = { workspace = true } blake3 = { workspace = true, features = ["traits-preview"] } borsh = { workspace = true, optional = true } -borsh0-10 = { package = "borsh", version = "0.10.3", optional = true } +borsh0-10 = { workspace = true, optional = true } bs58 = { workspace = true, features = ["alloc"] } bv = { workspace = true, features = ["serde"] } bytemuck = { workspace = true } @@ -55,6 +55,7 @@ solana-sanitize = { workspace = true } solana-sdk-macro = { workspace = true } solana-secp256k1-recover = { workspace = true } solana-serde-varint = { workspace = true } +solana-serialize-utils = { workspace = true } solana-sha256-hasher = { workspace = true, features = ["sha2"] } solana-short-vec = { workspace = true } thiserror = { workspace = true } diff --git a/sdk/program/src/lib.rs b/sdk/program/src/lib.rs index 8488c00daf937f..f57b034f2ee208 100644 --- a/sdk/program/src/lib.rs +++ b/sdk/program/src/lib.rs @@ -513,7 +513,6 @@ pub mod program_stubs; pub mod program_utils; pub mod rent; pub mod secp256k1_program; -pub mod serialize_utils; pub mod slot_hashes; pub mod slot_history; pub mod stable_layout; @@ -534,6 +533,8 @@ pub use solana_sanitize as sanitize; pub use solana_secp256k1_recover as secp256k1_recover; #[deprecated(since = "2.1.0", note = "Use `solana-serde-varint` crate instead")] pub use solana_serde_varint as serde_varint; +#[deprecated(since = "2.1.0", note = "Use `solana-serialize-utils` crate instead")] +pub use solana_serialize_utils as serialize_utils; #[deprecated(since = "2.1.0", note = "Use `solana-short-vec` crate instead")] pub use solana_short_vec as short_vec; #[cfg(target_arch = "wasm32")] diff --git a/sdk/serialize-utils/Cargo.toml b/sdk/serialize-utils/Cargo.toml new file mode 100644 index 00000000000000..ea37c89b218ab0 --- /dev/null +++ b/sdk/serialize-utils/Cargo.toml @@ -0,0 +1,30 @@ +[package] +name = "solana-serialize-utils" +description = "Solana helpers for reading and writing bytes." +documentation = "https://docs.rs/solana-serialize-utils" +version = { workspace = true } +authors = { workspace = true } +repository = { workspace = true } +homepage = { workspace = true } +license = { workspace = true } +edition = { workspace = true } + +[dependencies] +solana-instruction = { workspace = true, default-features = false, features = [ + "std", +] } +solana-pubkey = { workspace = true, default-features = false } +solana-sanitize = { workspace = true } + +[dev-dependencies] +bincode = { workspace = true } +borsh = { workspace = true } +rand = { workspace = true } +serde = { workspace = true } +solana-pubkey = { workspace = true, default-features = false, features = [ + "borsh", + "serde", +] } + +[package.metadata.docs.rs] +targets = ["x86_64-unknown-linux-gnu"] diff --git a/sdk/program/src/serialize_utils/cursor.rs b/sdk/serialize-utils/src/cursor.rs similarity index 81% rename from sdk/program/src/serialize_utils/cursor.rs rename to sdk/serialize-utils/src/cursor.rs index 3d4dedd092ed3a..5e4639281ad388 100644 --- a/sdk/program/src/serialize_utils/cursor.rs +++ b/sdk/serialize-utils/src/cursor.rs @@ -1,15 +1,13 @@ use { - crate::{ - instruction::InstructionError, - pubkey::{Pubkey, PUBKEY_BYTES}, - }, + solana_instruction::error::InstructionError, + solana_pubkey::{Pubkey, PUBKEY_BYTES}, std::{ io::{BufRead as _, Cursor, Read}, ptr, }, }; -pub(crate) fn read_u8>(cursor: &mut Cursor) -> Result { +pub fn read_u8>(cursor: &mut Cursor) -> Result { let mut buf = [0; 1]; cursor .read_exact(&mut buf) @@ -18,7 +16,7 @@ pub(crate) fn read_u8>(cursor: &mut Cursor) -> Result>(cursor: &mut Cursor) -> Result { +pub fn read_u32>(cursor: &mut Cursor) -> Result { let mut buf = [0; 4]; cursor .read_exact(&mut buf) @@ -27,7 +25,7 @@ pub(crate) fn read_u32>(cursor: &mut Cursor) -> Result>(cursor: &mut Cursor) -> Result { +pub fn read_u64>(cursor: &mut Cursor) -> Result { let mut buf = [0; 8]; cursor .read_exact(&mut buf) @@ -36,7 +34,7 @@ pub(crate) fn read_u64>(cursor: &mut Cursor) -> Result>( +pub fn read_option_u64>( cursor: &mut Cursor, ) -> Result, InstructionError> { let variant = read_u8(cursor)?; @@ -47,7 +45,7 @@ pub(crate) fn read_option_u64>( } } -pub(crate) fn read_i64>(cursor: &mut Cursor) -> Result { +pub fn read_i64>(cursor: &mut Cursor) -> Result { let mut buf = [0; 8]; cursor .read_exact(&mut buf) @@ -56,7 +54,7 @@ pub(crate) fn read_i64>(cursor: &mut Cursor) -> Result, pubkey: *mut Pubkey, ) -> Result<(), InstructionError> { @@ -77,9 +75,7 @@ pub(crate) fn read_pubkey_into( Ok(()) } -pub(crate) fn read_pubkey>( - cursor: &mut Cursor, -) -> Result { +pub fn read_pubkey>(cursor: &mut Cursor) -> Result { let mut buf = [0; 32]; cursor .read_exact(&mut buf) @@ -88,7 +84,7 @@ pub(crate) fn read_pubkey>( Ok(Pubkey::from(buf)) } -pub(crate) fn read_bool>(cursor: &mut Cursor) -> Result { +pub fn read_bool>(cursor: &mut Cursor) -> Result { let byte = read_u8(cursor)?; match byte { 0 => Ok(false), @@ -97,7 +93,6 @@ pub(crate) fn read_bool>(cursor: &mut Cursor) -> Result( + fn test_read( reader: fn(&mut Cursor>) -> Result, test_value: T, ) { @@ -166,7 +161,7 @@ mod test { let mut cursor = Cursor::new(bincode_bytes); let bincode_read = reader(&mut cursor).unwrap(); - let borsh_bytes = borsh0_10::to_vec(&test_value).unwrap(); + let borsh_bytes = borsh::to_vec(&test_value).unwrap(); let mut cursor = Cursor::new(borsh_bytes); let borsh_read = reader(&mut cursor).unwrap(); diff --git a/sdk/program/src/serialize_utils/mod.rs b/sdk/serialize-utils/src/lib.rs similarity index 96% rename from sdk/program/src/serialize_utils/mod.rs rename to sdk/serialize-utils/src/lib.rs index 6b1396f3481d5c..93c70713c29c70 100644 --- a/sdk/program/src/serialize_utils/mod.rs +++ b/sdk/serialize-utils/src/lib.rs @@ -1,7 +1,7 @@ //! Helpers for reading and writing bytes. #![allow(clippy::arithmetic_side_effects)] -use {crate::pubkey::Pubkey, solana_sanitize::SanitizeError}; +use {solana_pubkey::Pubkey, solana_sanitize::SanitizeError}; pub mod cursor;