From ed9469b5b64f27ff56453bca870e8b96acde1567 Mon Sep 17 00:00:00 2001 From: kevinheavey Date: Fri, 2 Aug 2024 22:13:54 +0400 Subject: [PATCH 1/8] extract account-info crate --- Cargo.lock | 14 +++++++++ Cargo.toml | 2 ++ sdk/account-info/Cargo.toml | 24 +++++++++++++++ .../src/debug_account_data.rs | 0 .../src/lib.rs} | 29 ++++++++++++------- sdk/program/Cargo.toml | 1 + sdk/program/src/lib.rs | 3 +- 7 files changed, 61 insertions(+), 12 deletions(-) create mode 100644 sdk/account-info/Cargo.toml rename sdk/{program => account-info}/src/debug_account_data.rs (100%) rename sdk/{program/src/account_info.rs => account-info/src/lib.rs} (96%) diff --git a/Cargo.lock b/Cargo.lock index cd4377a2bdb21b..8ecc8ccd7904a8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5634,6 +5634,19 @@ dependencies = [ "zstd", ] +[[package]] +name = "solana-account-info" +version = "2.1.0" +dependencies = [ + "bincode", + "serde", + "solana-program", + "solana-program-error", + "solana-program-memory", + "solana-pubkey", + "static_assertions", +] + [[package]] name = "solana-accounts-bench" version = "2.1.0" @@ -7211,6 +7224,7 @@ dependencies = [ "serial_test", "sha2 0.10.8", "sha3", + "solana-account-info", "solana-atomic-u64", "solana-clock", "solana-decode-error", diff --git a/Cargo.toml b/Cargo.toml index 3f22cf63d50ca6..f31a5b6f8c08d3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -101,6 +101,7 @@ members = [ "runtime-transaction", "sanitize", "sdk", + "sdk/account-info", "sdk/atomic-u64", "sdk/cargo-build-sbf", "sdk/cargo-test-sbf", @@ -357,6 +358,7 @@ smpl_jwt = "0.7.1" socket2 = "0.5.7" soketto = "0.7" solana-account-decoder = { path = "account-decoder", version = "=2.1.0" } +solana-account-info = { path = "sdk/account-info", version = "=2.1.0" } solana-accounts-db = { path = "accounts-db", version = "=2.1.0" } solana-address-lookup-table-program = { path = "programs/address-lookup-table", version = "=2.1.0" } solana-atomic-u64 = { path = "sdk/atomic-u64", version = "=2.1.0" } diff --git a/sdk/account-info/Cargo.toml b/sdk/account-info/Cargo.toml new file mode 100644 index 00000000000000..0cbf953251c3d0 --- /dev/null +++ b/sdk/account-info/Cargo.toml @@ -0,0 +1,24 @@ +[package] +name = "solana-account-info" +description = "Solana AccountInfo and related definitions." +documentation = "https://docs.rs/solana-account-info" +version = { workspace = true } +authors = { workspace = true } +repository = { workspace = true } +homepage = { workspace = true } +license = { workspace = true } +edition = { workspace = true } + +[dependencies] +bincode = { workspace = true } +serde = { workspace = true } +solana-program-error = { workspace = true } +solana-program-memory = { workspace = true } +solana-pubkey = { workspace = true, default-features = false } + +[dev-dependencies] +solana-program = { workspace = true } +static_assertions = { workspace = true } + +[package.metadata.docs.rs] +targets = ["x86_64-unknown-linux-gnu"] diff --git a/sdk/program/src/debug_account_data.rs b/sdk/account-info/src/debug_account_data.rs similarity index 100% rename from sdk/program/src/debug_account_data.rs rename to sdk/account-info/src/debug_account_data.rs diff --git a/sdk/program/src/account_info.rs b/sdk/account-info/src/lib.rs similarity index 96% rename from sdk/program/src/account_info.rs rename to sdk/account-info/src/lib.rs index fb7614903673b8..c916654bbcbd71 100644 --- a/sdk/program/src/account_info.rs +++ b/sdk/account-info/src/lib.rs @@ -1,12 +1,9 @@ //! Account information. use { - crate::{ - debug_account_data::*, entrypoint::MAX_PERMITTED_DATA_INCREASE, - program_error::ProgramError, pubkey::Pubkey, - }, - solana_clock::Epoch, + solana_program_error::ProgramError, solana_program_memory::sol_memset, + solana_pubkey::Pubkey, std::{ cell::{Ref, RefCell, RefMut}, fmt, @@ -14,6 +11,15 @@ use { slice::from_raw_parts_mut, }, }; +pub mod debug_account_data; + +// inline from solana_program::entrypoint +const MAX_PERMITTED_DATA_INCREASE: usize = 1_024 * 10; +#[cfg(test)] +static_assertions::const_assert_eq!( + MAX_PERMITTED_DATA_INCREASE, + solana_program::entrypoint::MAX_PERMITTED_DATA_INCREASE +); /// Account information #[derive(Clone)] @@ -28,7 +34,7 @@ pub struct AccountInfo<'a> { /// Program that owns this account pub owner: &'a Pubkey, /// The epoch at which this account will next owe rent - pub rent_epoch: Epoch, + pub rent_epoch: u64, /// Was the transaction signed by this account's public key? pub is_signer: bool, /// Is the account writable? @@ -49,7 +55,7 @@ impl<'a> fmt::Debug for AccountInfo<'a> { .field("rent_epoch", &self.rent_epoch) .field("lamports", &self.lamports()) .field("data.len", &self.data_len()); - debug_account_data(&self.data.borrow(), &mut f); + debug_account_data::debug_account_data(&self.data.borrow(), &mut f); f.finish_non_exhaustive() } @@ -203,7 +209,7 @@ impl<'a> AccountInfo<'a> { data: &'a mut [u8], owner: &'a Pubkey, executable: bool, - rent_epoch: Epoch, + rent_epoch: u64, ) -> Self { Self { key, @@ -242,7 +248,7 @@ impl<'a, T: IntoAccountInfo<'a>> From for AccountInfo<'a> { /// Provides information required to construct an `AccountInfo`, used in /// conversion implementations. pub trait Account { - fn get(&mut self) -> (&mut u64, &mut [u8], &Pubkey, bool, Epoch); + fn get(&mut self) -> (&mut u64, &mut [u8], &Pubkey, bool, u64); } /// Convert (&'a Pubkey, &'a mut T) where T: Account into an `AccountInfo` @@ -398,7 +404,10 @@ impl<'a> AsRef> for AccountInfo<'a> { #[cfg(test)] mod tests { - use super::*; + use { + super::*, + crate::debug_account_data::{Hex, MAX_DEBUG_ACCOUNT_DATA}, + }; #[test] fn test_next_account_infos() { diff --git a/sdk/program/Cargo.toml b/sdk/program/Cargo.toml index dd8b6aaa9ae227..c32c8b19120a02 100644 --- a/sdk/program/Cargo.toml +++ b/sdk/program/Cargo.toml @@ -31,6 +31,7 @@ serde_bytes = { workspace = true } serde_derive = { workspace = true } sha2 = { workspace = true } sha3 = { workspace = true } +solana-account-info = { workspace = true } solana-atomic-u64 = { workspace = true } solana-clock = { workspace = true, features = ["serde"] } solana-decode-error = { workspace = true } diff --git a/sdk/program/src/lib.rs b/sdk/program/src/lib.rs index 8488c00daf937f..781b5860cbe498 100644 --- a/sdk/program/src/lib.rs +++ b/sdk/program/src/lib.rs @@ -469,7 +469,6 @@ // Allows macro expansion of `use ::solana_program::*` to work within this crate extern crate self as solana_program; -pub mod account_info; pub mod address_lookup_table; pub mod big_mod_exp; pub mod blake3; @@ -483,7 +482,6 @@ pub mod bpf_loader; pub mod bpf_loader_deprecated; pub mod bpf_loader_upgradeable; pub mod compute_units; -pub mod debug_account_data; pub mod ed25519_program; pub mod entrypoint; pub mod entrypoint_deprecated; @@ -539,6 +537,7 @@ pub use solana_short_vec as short_vec; #[cfg(target_arch = "wasm32")] pub use wasm_bindgen::prelude::wasm_bindgen; pub use { + solana_account_info::{self as account_info, debug_account_data}, solana_clock as clock, solana_msg::msg, solana_program_option as program_option, solana_pubkey as pubkey, }; From 69cef6a0270d46bfc41912b4396962d186bfdfc0 Mon Sep 17 00:00:00 2001 From: kevinheavey Date: Fri, 2 Aug 2024 22:20:00 +0400 Subject: [PATCH 2/8] make bincode optional in account-info crate --- sdk/account-info/Cargo.toml | 7 +++++-- sdk/account-info/src/lib.rs | 2 ++ sdk/program/Cargo.toml | 2 +- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/sdk/account-info/Cargo.toml b/sdk/account-info/Cargo.toml index 0cbf953251c3d0..a1e9cabd437030 100644 --- a/sdk/account-info/Cargo.toml +++ b/sdk/account-info/Cargo.toml @@ -10,8 +10,8 @@ license = { workspace = true } edition = { workspace = true } [dependencies] -bincode = { workspace = true } -serde = { workspace = true } +bincode = { workspace = true, optional = true } +serde = { workspace = true, optional = true } solana-program-error = { workspace = true } solana-program-memory = { workspace = true } solana-pubkey = { workspace = true, default-features = false } @@ -20,5 +20,8 @@ solana-pubkey = { workspace = true, default-features = false } solana-program = { workspace = true } static_assertions = { workspace = true } +[features] +bincode = ["dep:bincode", "dep:serde"] + [package.metadata.docs.rs] targets = ["x86_64-unknown-linux-gnu"] diff --git a/sdk/account-info/src/lib.rs b/sdk/account-info/src/lib.rs index c916654bbcbd71..a7bf4da547066f 100644 --- a/sdk/account-info/src/lib.rs +++ b/sdk/account-info/src/lib.rs @@ -223,10 +223,12 @@ impl<'a> AccountInfo<'a> { } } + #[cfg(feature = "bincode")] pub fn deserialize_data(&self) -> Result { bincode::deserialize(&self.data.borrow()) } + #[cfg(feature = "bincode")] pub fn serialize_data(&self, state: &T) -> Result<(), bincode::Error> { if bincode::serialized_size(state)? > self.data_len() as u64 { return Err(Box::new(bincode::ErrorKind::SizeLimit)); diff --git a/sdk/program/Cargo.toml b/sdk/program/Cargo.toml index c32c8b19120a02..6500de05b6fde5 100644 --- a/sdk/program/Cargo.toml +++ b/sdk/program/Cargo.toml @@ -31,7 +31,7 @@ serde_bytes = { workspace = true } serde_derive = { workspace = true } sha2 = { workspace = true } sha3 = { workspace = true } -solana-account-info = { workspace = true } +solana-account-info = { workspace = true, features = ["bincode"] } solana-atomic-u64 = { workspace = true } solana-clock = { workspace = true, features = ["serde"] } solana-decode-error = { workspace = true } From a6b0cb53bdfcf050e55569b10ad1083edd40508e Mon Sep 17 00:00:00 2001 From: kevinheavey Date: Fri, 2 Aug 2024 22:20:09 +0400 Subject: [PATCH 3/8] update lock file --- programs/sbf/Cargo.lock | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/programs/sbf/Cargo.lock b/programs/sbf/Cargo.lock index 05e8a5b9403ad9..5675041e0bbcb2 100644 --- a/programs/sbf/Cargo.lock +++ b/programs/sbf/Cargo.lock @@ -4704,6 +4704,17 @@ dependencies = [ "zstd", ] +[[package]] +name = "solana-account-info" +version = "2.1.0" +dependencies = [ + "bincode", + "serde", + "solana-program-error", + "solana-program-memory", + "solana-pubkey", +] + [[package]] name = "solana-accounts-db" version = "2.1.0" @@ -5625,6 +5636,7 @@ dependencies = [ "serde_derive", "sha2 0.10.8", "sha3", + "solana-account-info", "solana-atomic-u64", "solana-clock", "solana-decode-error", From 39658387f5be5362a53e765e1b3774ab05fcd234 Mon Sep 17 00:00:00 2001 From: kevinheavey Date: Sat, 3 Aug 2024 13:52:38 +0400 Subject: [PATCH 4/8] update doc examples --- sdk/account-info/src/lib.rs | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/sdk/account-info/src/lib.rs b/sdk/account-info/src/lib.rs index a7bf4da547066f..a6a29a86eb845e 100644 --- a/sdk/account-info/src/lib.rs +++ b/sdk/account-info/src/lib.rs @@ -301,11 +301,9 @@ impl<'a, T: Account> IntoAccountInfo<'a> for &'a mut (Pubkey, T) { /// # Examples /// /// ``` -/// use solana_program::{ -/// account_info::{AccountInfo, next_account_info}, -/// entrypoint::ProgramResult, -/// pubkey::Pubkey, -/// }; +/// use solana_program::entrypoint::ProgramResult; +/// use solana_account_info::{AccountInfo, next_account_info}; +/// use solana_pubkey::Pubkey; /// # use solana_program::program_error::ProgramError; /// /// pub fn process_instruction( @@ -352,11 +350,9 @@ pub fn next_account_info<'a, 'b, I: Iterator>>( /// # Examples /// /// ``` -/// use solana_program::{ -/// account_info::{AccountInfo, next_account_info, next_account_infos}, -/// entrypoint::ProgramResult, -/// pubkey::Pubkey, -/// }; +/// use solana_program::entrypoint::ProgramResult; +/// use solana_account_info::{AccountInfo, next_account_info, next_account_infos}; +/// use solana_pubkey::Pubkey; /// # use solana_program::program_error::ProgramError; /// /// pub fn process_instruction( From 52f4a413bfa78c20eaa95484b02e864e0e00fc0a Mon Sep 17 00:00:00 2001 From: kevinheavey Date: Sat, 3 Aug 2024 18:11:31 +0400 Subject: [PATCH 5/8] remove solana-program from doc examples --- sdk/account-info/src/lib.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sdk/account-info/src/lib.rs b/sdk/account-info/src/lib.rs index a6a29a86eb845e..267497a42af64d 100644 --- a/sdk/account-info/src/lib.rs +++ b/sdk/account-info/src/lib.rs @@ -301,10 +301,10 @@ impl<'a, T: Account> IntoAccountInfo<'a> for &'a mut (Pubkey, T) { /// # Examples /// /// ``` -/// use solana_program::entrypoint::ProgramResult; +/// use solana_program_error::ProgramResult; /// use solana_account_info::{AccountInfo, next_account_info}; /// use solana_pubkey::Pubkey; -/// # use solana_program::program_error::ProgramError; +/// # use solana_program_error::ProgramError; /// /// pub fn process_instruction( /// program_id: &Pubkey, @@ -350,10 +350,10 @@ pub fn next_account_info<'a, 'b, I: Iterator>>( /// # Examples /// /// ``` -/// use solana_program::entrypoint::ProgramResult; +/// use solana_program_error::ProgramResult; /// use solana_account_info::{AccountInfo, next_account_info, next_account_infos}; /// use solana_pubkey::Pubkey; -/// # use solana_program::program_error::ProgramError; +/// # use solana_program_error::ProgramError; /// /// pub fn process_instruction( /// program_id: &Pubkey, From e73816bd26ea12ae55d7fc183d5dd042dab09eec Mon Sep 17 00:00:00 2001 From: kevinheavey Date: Sat, 3 Aug 2024 18:14:59 +0400 Subject: [PATCH 6/8] remove solana-program from dev deps --- Cargo.lock | 2 -- sdk/account-info/Cargo.toml | 4 ---- sdk/account-info/src/lib.rs | 5 ----- sdk/program/src/entrypoint.rs | 1 + 4 files changed, 1 insertion(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8ecc8ccd7904a8..050794a00067f6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5640,11 +5640,9 @@ version = "2.1.0" dependencies = [ "bincode", "serde", - "solana-program", "solana-program-error", "solana-program-memory", "solana-pubkey", - "static_assertions", ] [[package]] diff --git a/sdk/account-info/Cargo.toml b/sdk/account-info/Cargo.toml index a1e9cabd437030..55dc3d8a30ef44 100644 --- a/sdk/account-info/Cargo.toml +++ b/sdk/account-info/Cargo.toml @@ -16,10 +16,6 @@ solana-program-error = { workspace = true } solana-program-memory = { workspace = true } solana-pubkey = { workspace = true, default-features = false } -[dev-dependencies] -solana-program = { workspace = true } -static_assertions = { workspace = true } - [features] bincode = ["dep:bincode", "dep:serde"] diff --git a/sdk/account-info/src/lib.rs b/sdk/account-info/src/lib.rs index 267497a42af64d..70c9ef60133d8f 100644 --- a/sdk/account-info/src/lib.rs +++ b/sdk/account-info/src/lib.rs @@ -15,11 +15,6 @@ pub mod debug_account_data; // inline from solana_program::entrypoint const MAX_PERMITTED_DATA_INCREASE: usize = 1_024 * 10; -#[cfg(test)] -static_assertions::const_assert_eq!( - MAX_PERMITTED_DATA_INCREASE, - solana_program::entrypoint::MAX_PERMITTED_DATA_INCREASE -); /// Account information #[derive(Clone)] diff --git a/sdk/program/src/entrypoint.rs b/sdk/program/src/entrypoint.rs index c0353a8dc94b05..4d715ed54dd6fb 100644 --- a/sdk/program/src/entrypoint.rs +++ b/sdk/program/src/entrypoint.rs @@ -310,6 +310,7 @@ unsafe impl std::alloc::GlobalAlloc for BumpAllocator { } } +// WARNING: if this is changed, the duplicate definition in solana_account_info must also be changed /// Maximum number of bytes a program may add to an account during a single realloc pub const MAX_PERMITTED_DATA_INCREASE: usize = 1_024 * 10; From 15e28066ab2bcf7e1cf65cf9a7ba62a83ef4aeb7 Mon Sep 17 00:00:00 2001 From: kevinheavey Date: Sat, 14 Sep 2024 13:59:06 +0400 Subject: [PATCH 7/8] fmt --- sdk/program/src/lib.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sdk/program/src/lib.rs b/sdk/program/src/lib.rs index 781b5860cbe498..d1c00d39272420 100644 --- a/sdk/program/src/lib.rs +++ b/sdk/program/src/lib.rs @@ -538,8 +538,9 @@ pub use solana_short_vec as short_vec; pub use wasm_bindgen::prelude::wasm_bindgen; pub use { solana_account_info::{self as account_info, debug_account_data}, - solana_clock as clock, solana_msg::msg, solana_program_option as program_option, - solana_pubkey as pubkey, + solana_clock as clock, + solana_msg::msg, + solana_program_option as program_option, solana_pubkey as pubkey, }; /// The [config native program][np]. From 1f9b33bf915ac0b5e0d4b9fbd3e240436fa37318 Mon Sep 17 00:00:00 2001 From: kevinheavey Date: Wed, 25 Sep 2024 01:18:04 +0400 Subject: [PATCH 8/8] move MAX_PERMITTED_DATA_INCREASE to account-info crate --- sdk/account-info/src/lib.rs | 4 ++-- sdk/program/src/entrypoint.rs | 6 +----- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/sdk/account-info/src/lib.rs b/sdk/account-info/src/lib.rs index 70c9ef60133d8f..aed2b36669ad86 100644 --- a/sdk/account-info/src/lib.rs +++ b/sdk/account-info/src/lib.rs @@ -13,8 +13,8 @@ use { }; pub mod debug_account_data; -// inline from solana_program::entrypoint -const MAX_PERMITTED_DATA_INCREASE: usize = 1_024 * 10; +/// Maximum number of bytes a program may add to an account during a single realloc +pub const MAX_PERMITTED_DATA_INCREASE: usize = 1_024 * 10; /// Account information #[derive(Clone)] diff --git a/sdk/program/src/entrypoint.rs b/sdk/program/src/entrypoint.rs index 4d715ed54dd6fb..f360d5ef2b2ece 100644 --- a/sdk/program/src/entrypoint.rs +++ b/sdk/program/src/entrypoint.rs @@ -5,7 +5,6 @@ //! [`bpf_loader`]: crate::bpf_loader extern crate alloc; -pub use solana_program_error::ProgramResult; use { crate::{account_info::AccountInfo, pubkey::Pubkey}, alloc::vec::Vec, @@ -18,6 +17,7 @@ use { slice::{from_raw_parts, from_raw_parts_mut}, }, }; +pub use {solana_account_info::MAX_PERMITTED_DATA_INCREASE, solana_program_error::ProgramResult}; /// User implemented function to process an instruction /// @@ -310,10 +310,6 @@ unsafe impl std::alloc::GlobalAlloc for BumpAllocator { } } -// WARNING: if this is changed, the duplicate definition in solana_account_info must also be changed -/// Maximum number of bytes a program may add to an account during a single realloc -pub const MAX_PERMITTED_DATA_INCREASE: usize = 1_024 * 10; - /// `assert_eq(std::mem::align_of::(), 8)` is true for BPF but not for some host machines pub const BPF_ALIGN_OF_U128: usize = 8;