Skip to content

Commit

Permalink
Make borsh optional in sdk and program crates (#1576)
Browse files Browse the repository at this point in the history
* make borsh optional in sdk and program

* fmt

* don't include borsh in dev-context-only-utils
  • Loading branch information
kevinheavey authored Jun 5, 2024
1 parent ad29f68 commit f807911
Show file tree
Hide file tree
Showing 15 changed files with 144 additions and 141 deletions.
4 changes: 3 additions & 1 deletion sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ edition = { workspace = true }
program = []

default = [
"borsh",
"full" # functionality that is not compatible or needed for on-chain programs
]
full = [
Expand All @@ -33,6 +34,7 @@ full = [
"sha3",
"digest",
]
borsh = ["dep:borsh", "solana-program/borsh"]
dev-context-only-utils = [
"qualifier_attr"
]
Expand All @@ -45,7 +47,7 @@ frozen-abi = [
[dependencies]
bincode = { workspace = true }
bitflags = { workspace = true, features = ["serde"] }
borsh = { workspace = true }
borsh = { workspace = true, optional = true }
bs58 = { workspace = true }
bytemuck = { workspace = true, features = ["derive"] }
byteorder = { workspace = true, optional = true }
Expand Down
7 changes: 4 additions & 3 deletions sdk/program/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ rust-version = "1.75.0" # solana platform-tools rust version
[dependencies]
bincode = { workspace = true }
blake3 = { workspace = true, features = ["digest", "traits-preview"] }
borsh = { workspace = true }
borsh0-10 = { package = "borsh", version = "0.10.3" }
borsh = { workspace = true, optional = true }
borsh0-10 = { package = "borsh", version = "0.10.3", optional = true }
bs58 = { workspace = true }
bv = { workspace = true, features = ["serde"] }
bytemuck = { workspace = true, features = ["derive"] }
Expand Down Expand Up @@ -92,5 +92,6 @@ targets = ["x86_64-unknown-linux-gnu"]
crate-type = ["cdylib", "rlib"]

[features]
default = []
default = ["borsh"]
borsh = ["dep:borsh", "dep:borsh0-10"]
frozen-abi = ["dep:solana-frozen-abi", "dep:solana-frozen-abi-macro"]
23 changes: 7 additions & 16 deletions sdk/program/src/blake3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
//!
//! [blake3]: https://github.com/BLAKE3-team/BLAKE3
#[cfg(feature = "borsh")]
use borsh::{BorshDeserialize, BorshSchema, BorshSerialize};
use {
crate::sanitize::Sanitize,
borsh::{BorshDeserialize, BorshSchema, BorshSerialize},
std::{convert::TryFrom, fmt, mem, str::FromStr},
thiserror::Error,
};
Expand All @@ -16,22 +17,12 @@ const MAX_BASE58_LEN: usize = 44;

/// A blake3 hash.
#[cfg_attr(feature = "frozen-abi", derive(AbiExample))]
#[derive(
Serialize,
Deserialize,
BorshSerialize,
BorshDeserialize,
BorshSchema,
Clone,
Copy,
Default,
Eq,
PartialEq,
Ord,
PartialOrd,
Hash,
#[cfg_attr(
feature = "borsh",
derive(BorshSerialize, BorshDeserialize, BorshSchema),
borsh(crate = "borsh")
)]
#[borsh(crate = "borsh")]
#[derive(Serialize, Deserialize, Clone, Copy, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[repr(transparent)]
pub struct Hash(pub [u8; HASH_BYTES]);

Expand Down
12 changes: 7 additions & 5 deletions sdk/program/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
//! [SHA-256]: https://en.wikipedia.org/wiki/SHA-2
//! [`Hash`]: struct@Hash
#[cfg(feature = "borsh")]
use borsh::{BorshDeserialize, BorshSchema, BorshSerialize};
use {
crate::{sanitize::Sanitize, wasm_bindgen},
borsh::{BorshDeserialize, BorshSchema, BorshSerialize},
bytemuck::{Pod, Zeroable},
sha2::{Digest, Sha256},
std::{convert::TryFrom, fmt, mem, str::FromStr},
Expand All @@ -29,12 +30,14 @@ const MAX_BASE58_LEN: usize = 44;
/// [`Message::hash`]: crate::message::Message::hash
#[wasm_bindgen]
#[cfg_attr(feature = "frozen-abi", derive(AbiExample))]
#[cfg_attr(
feature = "borsh",
derive(BorshSerialize, BorshDeserialize, BorshSchema),
borsh(crate = "borsh")
)]
#[derive(
Serialize,
Deserialize,
BorshSerialize,
BorshDeserialize,
BorshSchema,
Clone,
Copy,
Default,
Expand All @@ -46,7 +49,6 @@ const MAX_BASE58_LEN: usize = 44;
Pod,
Zeroable,
)]
#[borsh(crate = "borsh")]
#[repr(transparent)]
pub struct Hash(pub(crate) [u8; HASH_BYTES]);

Expand Down
4 changes: 3 additions & 1 deletion sdk/program/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
#![allow(clippy::arithmetic_side_effects)]

#[cfg(feature = "borsh")]
use borsh::BorshSerialize;
use {
crate::{pubkey::Pubkey, sanitize::Sanitize, short_vec, wasm_bindgen},
bincode::serialize,
borsh::BorshSerialize,
serde::Serialize,
thiserror::Error,
};
Expand Down Expand Up @@ -339,6 +340,7 @@ pub struct Instruction {
}

impl Instruction {
#[cfg(feature = "borsh")]
/// Create a new instruction from a value, encoded with [`borsh`].
///
/// [`borsh`]: https://docs.rs/borsh/latest/borsh/
Expand Down
23 changes: 7 additions & 16 deletions sdk/program/src/keccak.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
//!
//! [keccak]: https://keccak.team/keccak.html
#[cfg(feature = "borsh")]
use borsh::{BorshDeserialize, BorshSchema, BorshSerialize};
use {
crate::sanitize::Sanitize,
borsh::{BorshDeserialize, BorshSchema, BorshSerialize},
sha3::{Digest, Keccak256},
std::{convert::TryFrom, fmt, mem, str::FromStr},
thiserror::Error,
Expand All @@ -14,22 +15,12 @@ pub const HASH_BYTES: usize = 32;
/// Maximum string length of a base58 encoded hash
const MAX_BASE58_LEN: usize = 44;
#[cfg_attr(feature = "frozen-abi", derive(AbiExample))]
#[derive(
Serialize,
Deserialize,
BorshSerialize,
BorshDeserialize,
BorshSchema,
Clone,
Copy,
Default,
Eq,
PartialEq,
Ord,
PartialOrd,
Hash,
#[cfg_attr(
feature = "borsh",
derive(BorshSerialize, BorshDeserialize, BorshSchema),
borsh(crate = "borsh")
)]
#[borsh(crate = "borsh")]
#[derive(Serialize, Deserialize, Clone, Copy, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[repr(transparent)]
pub struct Hash(pub [u8; HASH_BYTES]);

Expand Down
3 changes: 3 additions & 0 deletions sdk/program/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,8 +475,11 @@ pub mod alt_bn128;
pub(crate) mod atomic_u64;
pub mod big_mod_exp;
pub mod blake3;
#[cfg(feature = "borsh")]
pub mod borsh;
#[cfg(feature = "borsh")]
pub mod borsh0_10;
#[cfg(feature = "borsh")]
pub mod borsh1;
pub mod bpf_loader;
pub mod bpf_loader_deprecated;
Expand Down
4 changes: 3 additions & 1 deletion sdk/program/src/program_error.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
//! The [`ProgramError`] type and related definitions.
#![allow(clippy::arithmetic_side_effects)]
#[cfg(feature = "borsh")]
use borsh::io::Error as BorshIoError;
use {
crate::{decode_error::DecodeError, instruction::InstructionError, msg, pubkey::PubkeyError},
borsh::io::Error as BorshIoError,
num_traits::{FromPrimitive, ToPrimitive},
std::convert::TryFrom,
thiserror::Error,
Expand Down Expand Up @@ -343,6 +344,7 @@ impl From<PubkeyError> for ProgramError {
}
}

#[cfg(feature = "borsh")]
impl From<BorshIoError> for ProgramError {
fn from(error: BorshIoError) -> Self {
Self::BorshIoError(format!("{error}"))
Expand Down
19 changes: 14 additions & 5 deletions sdk/program/src/pubkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

#[cfg(test)]
use arbitrary::Arbitrary;
#[cfg(feature = "borsh")]
use borsh::{BorshDeserialize, BorshSchema, BorshSerialize};
use {
crate::{decode_error::DecodeError, hash::hashv, wasm_bindgen},
borsh::{BorshDeserialize, BorshSchema, BorshSerialize},
bytemuck::{Pod, Zeroable},
num_derive::{FromPrimitive, ToPrimitive},
std::{
Expand Down Expand Up @@ -70,10 +71,12 @@ impl From<u64> for PubkeyError {
#[wasm_bindgen]
#[repr(transparent)]
#[cfg_attr(feature = "frozen-abi", derive(AbiExample))]
#[cfg_attr(
feature = "borsh",
derive(BorshSerialize, BorshDeserialize, BorshSchema),
borsh(crate = "borsh")
)]
#[derive(
BorshDeserialize,
BorshSchema,
BorshSerialize,
Clone,
Copy,
Default,
Expand All @@ -87,7 +90,6 @@ impl From<u64> for PubkeyError {
Serialize,
Zeroable,
)]
#[borsh(crate = "borsh")]
#[cfg_attr(test, derive(Arbitrary))]
pub struct Pubkey(pub(crate) [u8; 32]);

Expand Down Expand Up @@ -675,6 +677,7 @@ impl fmt::Display for Pubkey {
}
}

#[cfg(feature = "borsh")]
impl borsh0_10::de::BorshDeserialize for Pubkey {
fn deserialize_reader<R: borsh0_10::maybestd::io::Read>(
reader: &mut R,
Expand All @@ -685,6 +688,7 @@ impl borsh0_10::de::BorshDeserialize for Pubkey {
}
}

#[cfg(feature = "borsh")]
macro_rules! impl_borsh_schema {
($borsh:ident) => {
impl $borsh::BorshSchema for Pubkey
Expand Down Expand Up @@ -716,8 +720,10 @@ macro_rules! impl_borsh_schema {
}
};
}
#[cfg(feature = "borsh")]
impl_borsh_schema!(borsh0_10);

#[cfg(feature = "borsh")]
macro_rules! impl_borsh_serialize {
($borsh:ident) => {
impl $borsh::ser::BorshSerialize for Pubkey {
Expand All @@ -731,6 +737,7 @@ macro_rules! impl_borsh_serialize {
}
};
}
#[cfg(feature = "borsh")]
impl_borsh_serialize!(borsh0_10);

#[cfg(test)]
Expand Down Expand Up @@ -964,6 +971,7 @@ mod tests {
}
}

#[cfg(feature = "borsh")]
fn pubkey_from_seed_by_marker(marker: &[u8]) -> Result<Pubkey, PubkeyError> {
let key = Pubkey::new_unique();
let owner = Pubkey::default();
Expand All @@ -977,6 +985,7 @@ mod tests {
Pubkey::create_with_seed(&key, seed, base)
}

#[cfg(feature = "borsh")]
#[test]
fn test_create_with_seed_rejects_illegal_owner() {
assert_eq!(
Expand Down
16 changes: 8 additions & 8 deletions sdk/program/src/secp256k1_recover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@
//! [sp]: crate::secp256k1_program
//! [`ecrecover`]: https://docs.soliditylang.org/en/v0.8.14/units-and-global-variables.html?highlight=ecrecover#mathematical-and-cryptographic-functions
use {
borsh::{BorshDeserialize, BorshSchema, BorshSerialize},
core::convert::TryFrom,
thiserror::Error,
};
#[cfg(feature = "borsh")]
use borsh::{BorshDeserialize, BorshSchema, BorshSerialize};
use {core::convert::TryFrom, thiserror::Error};

#[derive(Debug, Clone, PartialEq, Eq, Error)]
pub enum Secp256k1RecoverError {
Expand Down Expand Up @@ -66,10 +64,12 @@ pub const SECP256K1_PUBLIC_KEY_LENGTH: usize = 64;

#[repr(transparent)]
#[cfg_attr(feature = "frozen-abi", derive(AbiExample))]
#[derive(
BorshSerialize, BorshDeserialize, BorshSchema, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash,
#[cfg_attr(
feature = "borsh",
derive(BorshSerialize, BorshDeserialize, BorshSchema),
borsh(crate = "borsh")
)]
#[borsh(crate = "borsh")]
#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct Secp256k1Pubkey(pub [u8; SECP256K1_PUBLIC_KEY_LENGTH]);

impl Secp256k1Pubkey {
Expand Down
1 change: 1 addition & 0 deletions sdk/program/src/serialize_utils/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ pub(crate) fn read_bool<T: AsRef<[u8]>>(cursor: &mut Cursor<T>) -> Result<bool,
}
}

#[cfg(feature = "borsh")]
#[cfg(test)]
mod test {
use {super::*, rand::Rng, std::fmt::Debug};
Expand Down
27 changes: 12 additions & 15 deletions sdk/program/src/stake/stake_flags.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
#[cfg(feature = "borsh")]
use borsh::{BorshDeserialize, BorshSchema, BorshSerialize};

/// Additional flags for stake state.
#[cfg_attr(feature = "frozen-abi", derive(AbiExample))]
#[derive(
Serialize,
Deserialize,
BorshDeserialize,
BorshSchema,
BorshSerialize,
Copy,
PartialEq,
Eq,
Clone,
PartialOrd,
Ord,
Hash,
Debug,
#[cfg_attr(
feature = "borsh",
derive(BorshSerialize, BorshDeserialize, BorshSchema),
borsh(crate = "borsh")
)]
#[borsh(crate = "borsh")]
#[derive(Serialize, Deserialize, Copy, PartialEq, Eq, Clone, PartialOrd, Ord, Hash, Debug)]
pub struct StakeFlags {
bits: u8,
}

#[cfg(feature = "borsh")]
impl borsh0_10::de::BorshDeserialize for StakeFlags {
fn deserialize_reader<R: borsh0_10::maybestd::io::Read>(
reader: &mut R,
Expand All @@ -30,6 +23,8 @@ impl borsh0_10::de::BorshDeserialize for StakeFlags {
})
}
}

#[cfg(feature = "borsh")]
impl borsh0_10::BorshSchema for StakeFlags {
fn declaration() -> borsh0_10::schema::Declaration {
"StakeFlags".to_string()
Expand All @@ -55,6 +50,8 @@ impl borsh0_10::BorshSchema for StakeFlags {
<u8 as borsh0_10::BorshSchema>::add_definitions_recursively(definitions);
}
}

#[cfg(feature = "borsh")]
impl borsh0_10::ser::BorshSerialize for StakeFlags {
fn serialize<W: borsh0_10::maybestd::io::Write>(
&self,
Expand Down
Loading

0 comments on commit f807911

Please sign in to comment.