Skip to content

Commit

Permalink
Extract transaction-context crate (#3132)
Browse files Browse the repository at this point in the history
* extract transaction-context crate

* inline MAX_PERMITTED_DATA_INCREASE

* add doc_auto_cfg

* sort features

* update lock file

* fix feature activation

* move serde dep to correct table

* lint

* add missing #[cfg(not(target_os = "solana"))]

* rename feature to debug_signature

* only activate "debug_signature" when "full" is activated, as it was before

* replace underscore with hyphen
  • Loading branch information
kevinheavey authored Nov 19, 2024
1 parent 31e897b commit 45bdc18
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 24 deletions.
19 changes: 19 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ members = [
"sdk/stable-layout",
"sdk/sysvar-id",
"sdk/time-utils",
"sdk/transaction-context",
"sdk/transaction-error",
"send-transaction-service",
"stake-accounts",
Expand Down Expand Up @@ -548,6 +549,7 @@ solana-thin-client = { path = "thin-client", version = "=2.2.0" }
solana-transaction-error = { path = "sdk/transaction-error", version = "=2.2.0" }
solana-tpu-client = { path = "tpu-client", version = "=2.2.0", default-features = false }
solana-tpu-client-next = { path = "tpu-client-next", version = "=2.2.0" }
solana-transaction-context = { path = "sdk/transaction-context", version = "=2.2.0" }
solana-transaction-status = { path = "transaction-status", version = "=2.2.0" }
solana-transaction-status-client-types = { path = "transaction-status-client-types", version = "=2.2.0" }
solana-transaction-metrics-tracker = { path = "transaction-metrics-tracker", version = "=2.2.0" }
Expand Down
15 changes: 15 additions & 0 deletions programs/sbf/Cargo.lock

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

10 changes: 9 additions & 1 deletion sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ full = [
"rand0-7",
"serde_json",
"solana-signature",
"solana-transaction-context/debug-signature",
"ed25519-dalek",
"libsecp256k1",
"sha3",
Expand All @@ -46,7 +47,11 @@ full = [
"dep:solana-transaction-error"
]
borsh = ["dep:borsh", "solana-program/borsh", "solana-secp256k1-recover/borsh"]
dev-context-only-utils = ["qualifier_attr", "solana-account/dev-context-only-utils"]
dev-context-only-utils = [
"qualifier_attr",
"solana-account/dev-context-only-utils",
"solana-transaction-context/dev-context-only-utils",
]
frozen-abi = [
"dep:solana-frozen-abi",
"dep:solana-frozen-abi-macro",
Expand Down Expand Up @@ -147,6 +152,9 @@ solana-signature = { workspace = true, features = [
], optional = true }
solana-signer = { workspace = true, optional = true }
solana-time-utils = { workspace = true }
solana-transaction-context = { workspace = true, features = [
"bincode",
] }
solana-transaction-error = { workspace = true, features = ["serde"], optional = true }
thiserror = { workspace = true }

Expand Down
6 changes: 5 additions & 1 deletion sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ pub mod signer;
pub mod simple_vote_transaction_checker;
pub mod system_transaction;
pub mod transaction;
pub mod transaction_context;
pub mod transport;
pub mod wasm;

Expand Down Expand Up @@ -195,6 +194,11 @@ pub use solana_serde_varint as serde_varint;
pub use solana_short_vec as short_vec;
#[deprecated(since = "2.2.0", note = "Use `solana-time-utils` crate instead")]
pub use solana_time_utils as timing;
#[deprecated(
since = "2.2.0",
note = "Use `solana-transaction-context` crate instead"
)]
pub use solana_transaction_context as transaction_context;

/// Convenience macro for `AddAssign` with saturating arithmetic.
/// Replace by `std::num::Saturating` once stable
Expand Down
48 changes: 48 additions & 0 deletions sdk/transaction-context/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
[package]
name = "solana-transaction-context"
description = "Solana data shared between program runtime and built-in programs as well as SBF programs."
documentation = "https://docs.rs/solana-transaction-context"
version = { workspace = true }
authors = { workspace = true }
repository = { workspace = true }
homepage = { workspace = true }
license = { workspace = true }
edition = { workspace = true }

[dependencies]
serde = { workspace = true, optional = true }
serde_derive = { workspace = true, optional = true }
solana-account = { workspace = true }
solana-instruction = { workspace = true, features = ["std"] }
solana-pubkey = { workspace = true }

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
all-features = true
rustdoc-args = ["--cfg=docsrs"]

[target.'cfg(not(target_os = "solana"))'.dependencies]
bincode = { workspace = true, optional = true }
solana-rent = { workspace = true }
solana-signature = { workspace = true, optional = true }

[dev-dependencies]
solana-account-info = { workspace = true }
solana-program = { workspace = true }
solana-transaction-context = { path = ".", features = [
"dev-context-only-utils",
] }
static_assertions = { workspace = true }

[features]
bincode = ["dep:bincode", "serde", "solana-account/bincode"]
debug-signature = ["dep:solana-signature"]
dev-context-only-utils = [
"bincode",
"debug-signature",
"solana-account/dev-context-only-utils"
]
serde = ["dep:serde", "dep:serde_derive"]

[lints]
workspace = true
Original file line number Diff line number Diff line change
@@ -1,30 +1,54 @@
//! Data shared between program runtime and built-in programs as well as SBF programs.
#![deny(clippy::indexing_slicing)]

#[cfg(all(not(target_os = "solana"), feature = "full", debug_assertions))]
use crate::signature::Signature;
#![cfg_attr(docsrs, feature(doc_auto_cfg))]

#[cfg(all(
not(target_os = "solana"),
feature = "debug-signature",
debug_assertions
))]
use solana_signature::Signature;
#[cfg(not(target_os = "solana"))]
use {solana_account::WritableAccount, solana_rent::Rent, std::mem::MaybeUninit};
use {
crate::{instruction::InstructionError, pubkey::Pubkey},
solana_account::{AccountSharedData, ReadableAccount},
solana_instruction::error::InstructionError,
solana_pubkey::Pubkey,
std::{
cell::{Ref, RefCell, RefMut},
collections::HashSet,
pin::Pin,
rc::Rc,
},
};

// Inlined to avoid solana_program dep
#[cfg(not(target_os = "solana"))]
use {
crate::{
rent::Rent,
system_instruction::{
MAX_PERMITTED_ACCOUNTS_DATA_ALLOCATIONS_PER_TRANSACTION, MAX_PERMITTED_DATA_LENGTH,
},
},
solana_account::WritableAccount,
solana_program::entrypoint::MAX_PERMITTED_DATA_INCREASE,
std::mem::MaybeUninit,
};
const MAX_PERMITTED_DATA_LENGTH: u64 = 10 * 1024 * 1024;
#[cfg(test)]
static_assertions::const_assert_eq!(
MAX_PERMITTED_DATA_LENGTH,
solana_program::system_instruction::MAX_PERMITTED_DATA_LENGTH
);

// Inlined to avoid solana_program dep
#[cfg(not(target_os = "solana"))]
const MAX_PERMITTED_ACCOUNTS_DATA_ALLOCATIONS_PER_TRANSACTION: i64 =
MAX_PERMITTED_DATA_LENGTH as i64 * 2;
#[cfg(test)]
static_assertions::const_assert_eq!(
MAX_PERMITTED_ACCOUNTS_DATA_ALLOCATIONS_PER_TRANSACTION,
solana_program::system_instruction::MAX_PERMITTED_ACCOUNTS_DATA_ALLOCATIONS_PER_TRANSACTION
);

// Inlined to avoid solana_account_info dep
#[cfg(not(target_os = "solana"))]
const MAX_PERMITTED_DATA_INCREASE: usize = 1_024 * 10;
#[cfg(test)]
static_assertions::const_assert_eq!(
MAX_PERMITTED_DATA_INCREASE,
solana_account_info::MAX_PERMITTED_DATA_INCREASE
);

/// Index of an account inside of the TransactionContext or an InstructionContext.
pub type IndexOfAccount = u16;
Expand Down Expand Up @@ -144,7 +168,11 @@ pub struct TransactionContext {
#[cfg(not(target_os = "solana"))]
rent: Rent,
/// Useful for debugging to filter by or to look it up on the explorer
#[cfg(all(not(target_os = "solana"), feature = "full", debug_assertions))]
#[cfg(all(
not(target_os = "solana"),
feature = "debug-signature",
debug_assertions
))]
signature: Signature,
}

Expand Down Expand Up @@ -172,7 +200,11 @@ impl TransactionContext {
accounts_resize_delta: RefCell::new(0),
remove_accounts_executable_flag_checks: true,
rent,
#[cfg(all(not(target_os = "solana"), feature = "full", debug_assertions))]
#[cfg(all(
not(target_os = "solana"),
feature = "debug-signature",
debug_assertions
))]
signature: Signature::default(),
}
}
Expand Down Expand Up @@ -200,13 +232,21 @@ impl TransactionContext {
}

/// Stores the signature of the current transaction
#[cfg(all(not(target_os = "solana"), feature = "full", debug_assertions))]
#[cfg(all(
not(target_os = "solana"),
feature = "debug-signature",
debug_assertions
))]
pub fn set_signature(&mut self, signature: &Signature) {
self.signature = *signature;
}

/// Returns the signature of the current transaction
#[cfg(all(not(target_os = "solana"), feature = "full", debug_assertions))]
#[cfg(all(
not(target_os = "solana"),
feature = "debug-signature",
debug_assertions
))]
pub fn get_signature(&self) -> &Signature {
&self.signature
}
Expand Down Expand Up @@ -445,7 +485,11 @@ impl TransactionContext {
}

/// Return data at the end of a transaction
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Eq, Serialize)]
#[cfg_attr(
feature = "serde",
derive(serde_derive::Deserialize, serde_derive::Serialize)
)]
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct TransactionReturnData {
pub program_id: Pubkey,
pub data: Vec<u8>,
Expand Down Expand Up @@ -969,15 +1013,15 @@ impl<'a> BorrowedAccount<'a> {
}

/// Deserializes the account data into a state
#[cfg(not(target_os = "solana"))]
#[cfg(all(not(target_os = "solana"), feature = "bincode"))]
pub fn get_state<T: serde::de::DeserializeOwned>(&self) -> Result<T, InstructionError> {
self.account
.deserialize_data()
.map_err(|_| InstructionError::InvalidAccountData)
}

/// Serializes a state into the account data
#[cfg(not(target_os = "solana"))]
#[cfg(all(not(target_os = "solana"), feature = "bincode"))]
pub fn set_state<T: serde::Serialize>(&mut self, state: &T) -> Result<(), InstructionError> {
let data = self.get_data_mut()?;
let serialized_size =
Expand Down

0 comments on commit 45bdc18

Please sign in to comment.