Skip to content

Commit

Permalink
Extract instruction crate (#2405)
Browse files Browse the repository at this point in the history
* extract instruction crate

* remove thiserror

* make bincode optional in solana-instruction

* make serde optional in solana-instruction

* fix doc tests

* fmt

* harmonize features

* fmt Cargo.toml

* clippy

* fix syscalls.rs imports

* update digests

* update digest

* unused imports

* post-rebase fmt

* fix doc links

* use workspace lints

* fmt

* make rustc_version dep optional

* update digest

* update digest

* update import

* fmt

* fix dup import

* remove dev-context-only-utils (no longer needed)

* fmt

* remove unnecessary allow()

* fix overwriting instruction syscall stubs

* fmt

* move get_processed_sibling_instruction and get_stack_height back to solana-program to avoid breaking change

* fix path

* fix typo

* move the checked_add utility function back to solana-program

* move AccountMeta to its own file

* fix bad import

* move ProcessedSiblingInstruction to syscalls.rs

* move CompiledInstruction back to solana-program

* move ProcessedSiblingInstruction back into lib.rs

* make std optional in solana-instruction

* fix required features for frozen-abi

* update digest

* update digest

* missing import

* update digest

* don't assume std is imported in frozen-abi macros

* fix import warnings

* simplify cfg usage
  • Loading branch information
kevinheavey authored Sep 13, 2024
1 parent 875f8b4 commit b4ed7a4
Show file tree
Hide file tree
Showing 19 changed files with 996 additions and 748 deletions.
22 changes: 22 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 @@ -110,6 +110,7 @@ members = [
"sdk/feature-set",
"sdk/gen-headers",
"sdk/hash",
"sdk/instruction",
"sdk/macro",
"sdk/msg",
"sdk/package-metadata-macro",
Expand Down Expand Up @@ -403,6 +404,7 @@ solana-geyser-plugin-manager = { path = "geyser-plugin-manager", version = "=2.1
solana-gossip = { path = "gossip", version = "=2.1.0" }
solana-hash = { path = "sdk/hash", version = "=2.1.0" }
solana-inline-spl = { path = "inline-spl", version = "=2.1.0" }
solana-instruction = { path = "sdk/instruction", version = "=2.1.0", default-features = false }
solana-lattice-hash = { path = "lattice-hash", version = "=2.1.0" }
solana-ledger = { path = "ledger", version = "=2.1.0" }
solana-loader-v4-program = { path = "programs/loader-v4", version = "=2.1.0" }
Expand Down
4 changes: 2 additions & 2 deletions frozen-abi/macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ fn do_derive_abi_enum_visitor(input: ItemEnum) -> TokenStream {
let enum_name = #type_str;
use ::serde::ser::Serialize;
use ::solana_frozen_abi::abi_example::AbiExample;
digester.update_with_string(format!("enum {} (variants = {})", enum_name, #variant_count));
digester.update_with_string(::std::format!("enum {} (variants = {})", enum_name, #variant_count));
#serialized_variants
digester.create_child()
}
Expand Down Expand Up @@ -303,7 +303,7 @@ fn quote_for_test(
::solana_frozen_abi::__private::log::error!("digest error: {:#?}", result);
}
result.unwrap();
let actual_digest = format!("{}", hash);
let actual_digest = ::std::format!("{}", hash);
if ::std::env::var("SOLANA_ABI_BULK_UPDATE").is_ok() {
if #expected_digest != actual_digest {
#p!("sed -i -e 's/{}/{}/g' $(git grep --files-with-matches frozen_abi)", #expected_digest, hash);
Expand Down
1 change: 1 addition & 0 deletions program-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ solana-bpf-loader-program = { workspace = true }
solana-compute-budget = { workspace = true }
solana-feature-set = { workspace = true }
solana-inline-spl = { workspace = true }
solana-instruction = { workspace = true }
solana-log-collector = { workspace = true }
solana-logger = { workspace = true }
solana-program-runtime = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion program-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use {
solana_bpf_loader_program::serialization::serialize_parameters,
solana_compute_budget::compute_budget::ComputeBudget,
solana_feature_set::FEATURE_NAMES,
solana_instruction::{error::InstructionError, Instruction},
solana_log_collector::ic_msg,
solana_program_runtime::{
invoke_context::BuiltinFunctionWithContext, loaded_programs::ProgramCacheEntry, stable_log,
Expand All @@ -37,7 +38,6 @@ use {
fee_calculator::{FeeRateGovernor, DEFAULT_TARGET_LAMPORTS_PER_SIGNATURE},
genesis_config::{ClusterType, GenesisConfig},
hash::Hash,
instruction::{Instruction, InstructionError},
native_token::sol_to_lamports,
poh_config::PohConfig,
program_error::{ProgramError, UNSUPPORTED_SYSVAR},
Expand Down
18 changes: 18 additions & 0 deletions programs/sbf/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 runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ struct RentMetrics {
pub type BankStatusCache = StatusCache<Result<()>>;
#[cfg_attr(
feature = "frozen-abi",
frozen_abi(digest = "EQwW6Ym6ECKaAREnAgkhXYisBQovuraBKSALdJ8koZzq")
frozen_abi(digest = "BswQL6n7kKwgHFKcwMCQcrWjt8h59Vh6KkNb75iaqG2B")
)]
pub type BankSlotDelta = SlotDelta<Result<()>>;

Expand Down
58 changes: 58 additions & 0 deletions sdk/instruction/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
[package]
name = "solana-instruction"
description = "Types for directing the execution of Solana programs."
documentation = "https://docs.rs/solana-instruction"
version = { workspace = true }
authors = { workspace = true }
repository = { workspace = true }
homepage = { workspace = true }
license = { workspace = true }
edition = { workspace = true }

[dependencies]
bincode = { workspace = true, optional = true }
borsh = { workspace = true, optional = true }
num-traits = { workspace = true }
serde = { workspace = true, optional = true }
serde_derive = { workspace = true, optional = true }
solana-frozen-abi = { workspace = true, optional = true }
solana-frozen-abi-macro = { workspace = true, optional = true }
solana-pubkey = { workspace = true, default-features = false }

[target.'cfg(target_os = "solana")'.dependencies]
solana-define-syscall = { workspace = true }

[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { workspace = true, features = ["js", "wasm-bindgen"] }
js-sys = { workspace = true }
wasm-bindgen = { workspace = true }

[dev-dependencies]
solana-instruction = { path = ".", features = ["borsh"] }

[build-dependencies]
rustc_version = { workspace = true, optional = true }

[features]
bincode = ["dep:bincode", "dep:serde"]
borsh = ["dep:borsh"]
default = ["std"]
frozen-abi = [
"dep:rustc_version",
"dep:solana-frozen-abi",
"dep:solana-frozen-abi-macro",
"serde",
"std",
]
serde = [
"dep:serde",
"dep:serde_derive",
"solana-pubkey/serde",
]
std = []

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[lints]
workspace = true
1 change: 1 addition & 0 deletions sdk/instruction/build.rs
102 changes: 102 additions & 0 deletions sdk/instruction/src/account_meta.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
use solana_pubkey::Pubkey;

/// Describes a single account read or written by a program during instruction
/// execution.
///
/// When constructing an [`Instruction`], a list of all accounts that may be
/// read or written during the execution of that instruction must be supplied.
/// Any account that may be mutated by the program during execution, either its
/// data or metadata such as held lamports, must be writable.
///
/// Note that because the Solana runtime schedules parallel transaction
/// execution around which accounts are writable, care should be taken that only
/// accounts which actually may be mutated are specified as writable. As the
/// default [`AccountMeta::new`] constructor creates writable accounts, this is
/// a minor hazard: use [`AccountMeta::new_readonly`] to specify that an account
/// is not writable.
#[repr(C)]
#[cfg_attr(
feature = "serde",
derive(serde_derive::Serialize, serde_derive::Deserialize)
)]
#[derive(Debug, Default, PartialEq, Eq, Clone)]
pub struct AccountMeta {
/// An account's public key.
pub pubkey: Pubkey,
/// True if an `Instruction` requires a `Transaction` signature matching `pubkey`.
pub is_signer: bool,
/// True if the account data or metadata may be mutated during program execution.
pub is_writable: bool,
}

impl AccountMeta {
/// Construct metadata for a writable account.
///
/// # Examples
///
/// ```
/// # use solana_pubkey::Pubkey;
/// # use solana_instruction::{AccountMeta, Instruction};
/// # use borsh::{BorshSerialize, BorshDeserialize};
/// #
/// # #[derive(BorshSerialize, BorshDeserialize)]
/// # #[borsh(crate = "borsh")]
/// # pub struct MyInstruction;
/// #
/// # let instruction = MyInstruction;
/// # let from = Pubkey::new_unique();
/// # let to = Pubkey::new_unique();
/// # let program_id = Pubkey::new_unique();
/// let instr = Instruction::new_with_borsh(
/// program_id,
/// &instruction,
/// vec![
/// AccountMeta::new(from, true),
/// AccountMeta::new(to, false),
/// ],
/// );
/// ```
pub fn new(pubkey: Pubkey, is_signer: bool) -> Self {
Self {
pubkey,
is_signer,
is_writable: true,
}
}

/// Construct metadata for a read-only account.
///
/// # Examples
///
/// ```
/// # use solana_pubkey::Pubkey;
/// # use solana_instruction::{AccountMeta, Instruction};
/// # use borsh::{BorshSerialize, BorshDeserialize};
/// #
/// # #[derive(BorshSerialize, BorshDeserialize)]
/// # #[borsh(crate = "borsh")]
/// # pub struct MyInstruction;
/// #
/// # let instruction = MyInstruction;
/// # let from = Pubkey::new_unique();
/// # let to = Pubkey::new_unique();
/// # let from_account_storage = Pubkey::new_unique();
/// # let program_id = Pubkey::new_unique();
/// let instr = Instruction::new_with_borsh(
/// program_id,
/// &instruction,
/// vec![
/// AccountMeta::new(from, true),
/// AccountMeta::new(to, false),
/// AccountMeta::new_readonly(from_account_storage, false),
/// ],
/// );
/// ```
pub fn new_readonly(pubkey: Pubkey, is_signer: bool) -> Self {
Self {
pubkey,
is_signer,
is_writable: false,
}
}
}
Loading

0 comments on commit b4ed7a4

Please sign in to comment.