Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sdk: Upgrade to borsh 1.2.1 #34355

Merged
merged 10 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 59 additions & 3 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ bincode = "1.3.3"
bitflags = { version = "2.3.3", features = ["serde"] }
blake3 = "1.5.0"
block-buffer = "0.10.4"
borsh = { version = "1.2.0", features = ["derive", "unstable__schema"], default-features = false }
borsh = { version = "1.2.0", features = ["derive", "unstable__schema"] }
bs58 = "0.4.0"
bv = "0.11.1"
byte-unit = "4.0.19"
Expand Down
2 changes: 1 addition & 1 deletion cost-model/src/cost_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use {
},
},
solana_sdk::{
borsh0_10::try_from_slice_unchecked,
borsh1::try_from_slice_unchecked,
compute_budget::{self, ComputeBudgetInstruction},
feature_set::{include_loaded_accounts_data_size_in_fee_calculation, FeatureSet},
fee::FeeStructure,
Expand Down
2 changes: 1 addition & 1 deletion program-runtime/src/compute_budget_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use {
prioritization_fee::{PrioritizationFeeDetails, PrioritizationFeeType},
},
solana_sdk::{
borsh0_10::try_from_slice_unchecked,
borsh1::try_from_slice_unchecked,
compute_budget::{self, ComputeBudgetInstruction},
entrypoint::HEAP_LENGTH as MIN_HEAP_FRAME_BYTES,
feature_set::{
Expand Down
4 changes: 2 additions & 2 deletions sdk/program/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ rust-version = "1.72.0" # solana platform-tools rust version
bincode = { workspace = true }
blake3 = { workspace = true, features = ["digest", "traits-preview"] }
borsh = { workspace = true }
#borsh0-10 = { package = "borsh", version = "0.10.3" }
#borsh0-9 = { package = "borsh", version = "0.9.3" }
borsh0-10 = { package = "borsh", version = "0.10.3" }
borsh0-9 = { package = "borsh", version = "0.9.3" }
bs58 = { workspace = true }
bv = { workspace = true, features = ["serde"] }
bytemuck = { workspace = true, features = ["derive"] }
Expand Down
1 change: 1 addition & 0 deletions sdk/program/src/blake3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const MAX_BASE58_LEN: usize = 44;
Hash,
AbiExample,
)]
#[borsh(crate = "borsh")]
#[repr(transparent)]
pub struct Hash(pub [u8; HASH_BYTES]);

Expand Down
68 changes: 64 additions & 4 deletions sdk/program/src/borsh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
//! be removed in a future release
//!
//! [borsh]: https://borsh.io/
/*
use borsh0_10::{maybestd::io::Error, BorshDeserialize, BorshSchema, BorshSerialize};

/// Get the worst-case packed length for the given BorshSchema
Expand Down Expand Up @@ -56,9 +55,70 @@ pub fn get_instance_packed_len<T: BorshSerialize>(instance: &T) -> Result<usize,
#[allow(deprecated)]
crate::borsh0_10::get_instance_packed_len(instance)
}
*/

macro_rules! impl_get_packed_len {
macro_rules! impl_get_packed_len_v0 {
($borsh:ident $(,#[$meta:meta])?) => {
/// Get the worst-case packed length for the given BorshSchema
///
/// Note: due to the serializer currently used by Borsh, this function cannot
/// be used on-chain in the Solana SBF execution environment.
$(#[$meta])?
pub fn get_packed_len<S: $borsh::BorshSchema>() -> usize {
let $borsh::schema::BorshSchemaContainer { declaration, definitions } =
&S::schema_container();
get_declaration_packed_len(declaration, definitions)
}

/// Get packed length for the given BorshSchema Declaration
fn get_declaration_packed_len(
declaration: &str,
definitions: &std::collections::HashMap<$borsh::schema::Declaration, $borsh::schema::Definition>,
) -> usize {
match definitions.get(declaration) {
Some($borsh::schema::Definition::Array { length, elements }) => {
*length as usize * get_declaration_packed_len(elements, definitions)
}
Some($borsh::schema::Definition::Enum { variants }) => {
1 + variants
.iter()
.map(|(_, declaration)| get_declaration_packed_len(declaration, definitions))
.max()
.unwrap_or(0)
}
Some($borsh::schema::Definition::Struct { fields }) => match fields {
$borsh::schema::Fields::NamedFields(named_fields) => named_fields
.iter()
.map(|(_, declaration)| get_declaration_packed_len(declaration, definitions))
.sum(),
$borsh::schema::Fields::UnnamedFields(declarations) => declarations
.iter()
.map(|declaration| get_declaration_packed_len(declaration, definitions))
.sum(),
$borsh::schema::Fields::Empty => 0,
},
Some($borsh::schema::Definition::Sequence {
elements: _elements,
}) => panic!("Missing support for Definition::Sequence"),
Some($borsh::schema::Definition::Tuple { elements }) => elements
.iter()
.map(|element| get_declaration_packed_len(element, definitions))
.sum(),
None => match declaration {
"bool" | "u8" | "i8" => 1,
"u16" | "i16" => 2,
"u32" | "i32" => 4,
"u64" | "i64" => 8,
"u128" | "i128" => 16,
"nil" => 0,
_ => panic!("Missing primitive type: {declaration}"),
},
}
}
}
}
pub(crate) use impl_get_packed_len_v0;

macro_rules! impl_get_packed_len_v1 {
($borsh:ident $(,#[$meta:meta])?) => {
/// Get the worst-case packed length for the given BorshSchema
///
Expand Down Expand Up @@ -118,7 +178,7 @@ macro_rules! impl_get_packed_len {
}
}
}
pub(crate) use impl_get_packed_len;
pub(crate) use impl_get_packed_len_v1;

macro_rules! impl_try_from_slice_unchecked {
($borsh:ident, $borsh_io:ident $(,#[$meta:meta])?) => {
Expand Down
9 changes: 3 additions & 6 deletions sdk/program/src/borsh0_10.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
//! [borsh]: https://borsh.io/
use {
crate::borsh::{
impl_get_instance_packed_len, impl_get_packed_len, impl_try_from_slice_unchecked,
impl_get_instance_packed_len, impl_get_packed_len_v0, impl_try_from_slice_unchecked,
},
borsh0_10::maybestd::io,
};

impl_get_packed_len!(
impl_get_packed_len_v0!(
borsh0_10,
#[deprecated(
since = "1.18.0",
Expand All @@ -36,9 +36,6 @@ impl_get_instance_packed_len!(
#[cfg(test)]
#[allow(deprecated)]
mod tests {
use {
crate::borsh::impl_tests,
borsh0_10::maybestd::io,
};
use {crate::borsh::impl_tests, borsh0_10::maybestd::io};
impl_tests!(borsh0_10, io);
}
9 changes: 3 additions & 6 deletions sdk/program/src/borsh0_9.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
//! [borsh]: https://borsh.io/
use {
crate::borsh::{
impl_get_instance_packed_len, impl_get_packed_len, impl_try_from_slice_unchecked,
impl_get_instance_packed_len, impl_get_packed_len_v0, impl_try_from_slice_unchecked,
},
borsh0_9::maybestd::io,
};

impl_get_packed_len!(
impl_get_packed_len_v0!(
borsh0_9,
#[deprecated(
since = "1.17.0",
Expand All @@ -39,9 +39,6 @@ impl_get_instance_packed_len!(
#[cfg(test)]
#[allow(deprecated)]
mod tests {
use {
crate::borsh::impl_tests,
borsh0_9::maybestd::io,
};
use {crate::borsh::impl_tests, borsh0_9::maybestd::io};
impl_tests!(borsh0_9, io);
}
1 change: 1 addition & 0 deletions sdk/program/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const MAX_BASE58_LEN: usize = 44;
Pod,
Zeroable,
)]
#[borsh(crate = "borsh")]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incidentally, is there a good place to read documentation of borsh tags? I looked on their repo and rust docs, but must have missed it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docs.rs was the best source at https://docs.rs/borsh/latest/borsh/derive.BorshDeserialize.html but I did also have to read the source directly too. This one tripped me up for awhile

#[repr(transparent)]
pub struct Hash(pub(crate) [u8; HASH_BYTES]);

Expand Down
4 changes: 4 additions & 0 deletions sdk/program/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ impl Instruction {
/// # use borsh::{BorshSerialize, BorshDeserialize};
/// #
/// #[derive(BorshSerialize, BorshDeserialize)]
/// # #[borsh(crate = "borsh")]
/// pub struct MyInstruction {
/// pub lamports: u64,
/// }
Expand Down Expand Up @@ -469,6 +470,7 @@ impl Instruction {
/// # use borsh::{io::Error, BorshSerialize, BorshDeserialize};
/// #
/// #[derive(BorshSerialize, BorshDeserialize)]
/// # #[borsh(crate = "borsh")]
/// pub struct MyInstruction {
/// pub lamports: u64,
/// }
Expand Down Expand Up @@ -557,6 +559,7 @@ impl AccountMeta {
/// # use borsh::{BorshSerialize, BorshDeserialize};
/// #
/// # #[derive(BorshSerialize, BorshDeserialize)]
/// # #[borsh(crate = "borsh")]
/// # pub struct MyInstruction;
/// #
/// # let instruction = MyInstruction;
Expand Down Expand Up @@ -592,6 +595,7 @@ impl AccountMeta {
/// # use borsh::{BorshSerialize, BorshDeserialize};
/// #
/// # #[derive(BorshSerialize, BorshDeserialize)]
/// # #[borsh(crate = "borsh")]
/// # pub struct MyInstruction;
/// #
/// # let instruction = MyInstruction;
Expand Down
1 change: 1 addition & 0 deletions sdk/program/src/keccak.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const MAX_BASE58_LEN: usize = 44;
Hash,
AbiExample,
)]
#[borsh(crate = "borsh")]
#[repr(transparent)]
pub struct Hash(pub [u8; HASH_BYTES]);

Expand Down
4 changes: 2 additions & 2 deletions sdk/program/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,9 +477,9 @@ pub(crate) mod atomic_u64;
pub mod big_mod_exp;
pub mod blake3;
pub mod borsh;
pub mod borsh0_10;
pub mod borsh0_9;
pub mod borsh1;
//pub mod borsh0_10;
//pub mod borsh0_9;
pub mod bpf_loader;
pub mod bpf_loader_deprecated;
pub mod bpf_loader_upgradeable;
Expand Down
3 changes: 3 additions & 0 deletions sdk/program/src/message/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ impl Message {
/// // another crate so it can be shared between the on-chain program and
/// // the client.
/// #[derive(BorshSerialize, BorshDeserialize)]
/// # #[borsh(crate = "borsh")]
/// enum BankInstruction {
/// Initialize,
/// Deposit { lamports: u64 },
Expand Down Expand Up @@ -264,6 +265,7 @@ impl Message {
/// // another crate so it can be shared between the on-chain program and
/// // the client.
/// #[derive(BorshSerialize, BorshDeserialize)]
/// # #[borsh(crate = "borsh")]
/// enum BankInstruction {
/// Initialize,
/// Deposit { lamports: u64 },
Expand Down Expand Up @@ -363,6 +365,7 @@ impl Message {
/// // another crate so it can be shared between the on-chain program and
/// // the client.
/// #[derive(BorshSerialize, BorshDeserialize)]
/// # #[borsh(crate = "borsh")]
/// enum BankInstruction {
/// Initialize,
/// Deposit { lamports: u64 },
Expand Down
Loading