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

Extract transaction-context crate #3132

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
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 @@ -130,6 +130,7 @@ members = [
"sdk/sha256-hasher",
"sdk/signature",
"sdk/slot-hashes",
"sdk/transaction-context",
"send-transaction-service",
"short-vec",
"stake-accounts",
Expand Down Expand Up @@ -482,6 +483,7 @@ solana-system-program = { path = "programs/system", version = "=2.1.0" }
solana-test-validator = { path = "test-validator", version = "=2.1.0" }
solana-thin-client = { path = "thin-client", version = "=2.1.0" }
solana-tpu-client = { path = "tpu-client", version = "=2.1.0", default-features = false }
solana-transaction-context = { path = "sdk/transaction-context", version = "=2.1.0" }
solana-transaction-status = { path = "transaction-status", version = "=2.1.0" }
solana-transaction-status-client-types = { path = "transaction-status-client-types", version = "=2.1.0" }
solana-transaction-metrics-tracker = { path = "transaction-metrics-tracker", version = "=2.1.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.

11 changes: 10 additions & 1 deletion sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,19 @@ full = [
"rand0-7",
"serde_json",
"solana-signature",
"solana-transaction-context/debug_assertions",
"ed25519-dalek",
"ed25519-dalek-bip32",
"libsecp256k1",
"sha3",
"digest",
]
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 @@ -108,6 +113,10 @@ solana-signature = { workspace = true, features = [
"std",
"verify",
], optional = true }
solana-transaction-context = { workspace = true, features = [
"bincode",
"debug_assertions",
] }
thiserror = { workspace = true }

[target.'cfg(target_arch = "wasm32")'.dependencies]
Expand Down
6 changes: 5 additions & 1 deletion sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ pub mod simple_vote_transaction_checker;
pub mod system_transaction;
pub mod timing;
pub mod transaction;
pub mod transaction_context;
pub mod transport;
pub mod wasm;

Expand Down Expand Up @@ -168,6 +167,11 @@ pub use solana_secp256k1_recover as secp256k1_recover;
pub use solana_serde_varint as serde_varint;
#[deprecated(since = "2.1.0", note = "Use `solana-short-vec` crate instead")]
pub use solana_short_vec as short_vec;
#[deprecated(
since = "2.1.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_assertions = ["dep:solana-signature"]
dev-context-only-utils = [
"bincode",
"debug_assertions",
"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_assertions",
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 @@ -142,7 +166,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_assertions",
debug_assertions
))]
signature: Signature,
}

Expand All @@ -169,7 +197,11 @@ impl TransactionContext {
return_data: TransactionReturnData::default(),
accounts_resize_delta: RefCell::new(0),
rent,
#[cfg(all(not(target_os = "solana"), feature = "full", debug_assertions))]
#[cfg(all(
not(target_os = "solana"),
feature = "debug_assertions",
debug_assertions
))]
signature: Signature::default(),
}
}
Expand All @@ -192,13 +224,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_assertions",
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_assertions",
debug_assertions
))]
pub fn get_signature(&self) -> &Signature {
&self.signature
}
Expand Down Expand Up @@ -437,7 +477,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 @@ -961,15 +1005,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
Loading