Docs.rs
  • solana-program-1.16.23
    • solana-program 1.16.23
    • Docs.rs crate page
    • Apache-2.0
    • Links
    • Homepage
    • Repository
    • crates.io
    • Source
    • Owners
    • anza-team
    • Dependencies
      • bincode ^1.3.3 normal
      • blake3 ^1.3.3 normal
      • borsh ^0.10.3 normal
      • borsh ^0.9.3 normal
      • bs58 ^0.4.0 normal
      • bv ^0.11.1 normal
      • bytemuck ^1.13.1 normal
      • itertools ^0.10.5 normal
      • lazy_static ^1.4.0 normal
      • log ^0.4.17 normal
      • memoffset ^0.9 normal
      • num-derive ^0.3 normal
      • num-traits ^0.2 normal
      • rustversion ^1.0.12 normal
      • serde ^1.0.163 normal
      • serde_bytes ^0.11.9 normal
      • serde_derive ^1.0.103 normal
      • serde_json ^1.0.96 normal
      • sha2 ^0.10.6 normal
      • sha3 ^0.10.4 normal
      • solana-frozen-abi =1.16.23 normal
      • solana-frozen-abi-macro =1.16.23 normal
      • solana-sdk-macro =1.16.23 normal
      • thiserror ^1.0.40 normal
      • anyhow ^1.0.71 dev
      • assert_matches ^1.5.0 dev
      • serde_json ^1.0.96 dev
      • static_assertions ^1.1.0 dev
      • cc ^1.0.79 build
      • rustc_version ^0.4 build
      • ark-bn254 ^0.4.0 normal
      • ark-ec ^0.4.0 normal
      • ark-ff ^0.4.0 normal
      • ark-serialize ^0.4.0 normal
      • array-bytes =1.4.1 normal
      • base64 ^0.21.2 normal
      • bitflags ^1.3.1 normal
      • curve25519-dalek ^3.2.1 normal
      • itertools ^0.10.5 normal
      • libc ^0.2.144 normal
      • libsecp256k1 ^0.6.0 normal
      • num-bigint ^0.4.3 normal
      • rand ^0.7.0 normal
      • rand_chacha ^0.2.2 normal
      • tiny-bip39 ^0.8.2 normal
      • wasm-bindgen ^0.2 normal
      • zeroize ^1.3 normal
      • solana-logger =1.16.23 dev
      • parking_lot ^0.12 normal
      • console_error_panic_hook ^0.1.7 normal
      • console_log ^0.2.2 normal
      • getrandom ^0.2 normal
      • js-sys ^0.3.63 normal
      • getrandom ^0.2 normal
    • Versions
    • 53.14% of the crate is documented
  • Go to latest version
  • Platform
    • x86_64-unknown-linux-gnu
  • Feature flags
  • docs.rs
    • About docs.rs
    • Privacy policy
  • Rust
    • Rust website
    • The Book
    • Standard Library API Reference
    • Rust by Example
    • The Cargo Guide
    • Clippy Documentation

Crate solana_program

solana_program1.16.23

  • All Items
  • Modules
  • Macros
  • Attribute Macros

Crates

  • solana_program
?
Change settings

Crate solana_program

source ·
Expand description

The base library for all Solana on-chain Rust programs.

All Solana Rust programs that run on-chain will link to this crate, which acts as a standard library for Solana programs. Solana programs also link to the Rust standard library, though it is modified for the Solana runtime environment. While off-chain programs that interact with the Solana network can link to this crate, they typically instead use the solana-sdk crate, which reexports all modules from solana-program.

This library defines

  • macros for declaring the program entrypoint,
  • core data types,
  • logging macros,
  • serialization methods,
  • methods for cross-program instruction execution,
  • program IDs and instruction constructors for the system program and other native programs,
  • sysvar accessors.

Idiomatic examples of solana-program usage can be found in the Solana Program Library.

Defining a solana program

Solana program crates have some unique properties compared to typical Rust programs:

  • They are often compiled for both on-chain use and off-chain use. This is primarily because off-chain clients may need access to data types defined by the on-chain program.
  • They do not define a main function, but instead define their entrypoint with the entrypoint! macro.
  • They are compiled as the “cdylib” crate type for dynamic loading by the Solana runtime.
  • They run in a constrained VM environment, and while they do have access to the Rust standard library, many features of the standard library, particularly related to OS services, will fail at runtime, will silently do nothing, or are not defined. See the restrictions to the Rust standard library in the Solana documentation for more.

Because multiple crates that are linked together cannot all define program entrypoints (see the entrypoint! documentation) a common convention is to use a Cargo feature called no-entrypoint to allow the program entrypoint to be disabled.

The skeleton of a Solana program typically looks like:

#[cfg(not(feature = "no-entrypoint"))]
pub mod entrypoint {
    use solana_program::{
        account_info::AccountInfo,
        entrypoint,
        entrypoint::ProgramResult,
        pubkey::Pubkey,
    };

    entrypoint!(process_instruction);

    pub fn process_instruction(
        program_id: &Pubkey,
        accounts: &[AccountInfo],
        instruction_data: &[u8],
    ) -> ProgramResult {
        // Decode and dispatch instructions here.
        todo!()
    }
}

// Additional code goes here.

With a Cargo.toml file that contains

[lib]
crate-type = ["cdylib", "rlib"]

[features]
no-entrypoint = []

Note that a Solana program must specify its crate-type as “cdylib”, and “cdylib” crates will automatically be discovered and built by the cargo build-bpf command. Solana programs also often have crate-type “rlib” so they can be linked to other Rust crates.

On-chain vs. off-chain compilation targets

Solana programs run on the rbpf VM, which implements a variant of the eBPF instruction set. Because this crate can be compiled for both on-chain and off-chain execution, the environments of which are significantly different, it extensively uses conditional compilation to tailor its implementation to the environment. The cfg predicate used for identifying compilation for on-chain programs is target_os = "solana", as in this example from the solana-program codebase that logs a message via a syscall when run on-chain, and via a library call when offchain:

pub fn sol_log(message: &str) {
    #[cfg(target_os = "solana")]
    unsafe {
        sol_log_(message.as_ptr(), message.len() as u64);
    }

    #[cfg(not(target_os = "solana"))]
    program_stubs::sol_log(message);
}

This cfg pattern is suitable as well for user code that needs to work both on-chain and off-chain.

solana-program and solana-sdk were previously a single crate. Because of this history, and because of the dual-usage of solana-program for two different environments, it contains some features that are not available to on-chain programs at compile-time. It also contains some on-chain features that will fail in off-chain scenarios at runtime. This distinction is not well-reflected in the documentation.

For a more complete description of Solana’s implementation of eBPF and its limitations, see the main Solana documentation for on-chain programs.

Core data types

  • Pubkey — The address of a Solana account. Some account addresses are ed25519 public keys, with corresponding secret keys that are managed off-chain. Often, though, account addresses do not have corresponding secret keys — as with program derived addresses — or the secret key is not relevant to the operation of a program, and may have even been disposed of. As running Solana programs can not safely create or manage secret keys, the full Keypair is not defined in solana-program but in solana-sdk.
  • Hash — A cryptographic hash. Used to uniquely identify blocks, and also for general purpose hashing.
  • AccountInfo — A description of a single Solana account. All accounts that might be accessed by a program invocation are provided to the program entrypoint as AccountInfo.
  • Instruction — A directive telling the runtime to execute a program, passing it a set of accounts and program-specific data.
  • ProgramError and ProgramResult — The error type that all programs must return, reported to the runtime as a u64.
  • Sol — The Solana native token type, with conversions to and from lamports, the smallest fractional unit of SOL, in the native_token module.

Serialization

Within the Solana runtime, programs, and network, at least three different serialization formats are used, and solana-program provides access to those needed by programs.

In user-written Solana program code, serialization is primarily used for accessing AccountInfo data and Instruction data, both of which are program-specific binary data. Every program is free to decide their own serialization format, but data received from other sources — sysvars for example — must be deserialized using the methods indicated by the documentation for that data or data type.

The three serialization formats in use in Solana are:

  • Borsh, a compact and well-specified format developed by the NEAR project, suitable for use in protocol definitions and for archival storage. It has a Rust implementation and a JavaScript implementation and is recommended for all purposes.

    Users need to import the borsh crate themselves — it is not re-exported by solana-program, though this crate provides several useful utilities in its borsh module that are not available in the borsh library.

    The Instruction::new_with_borsh function creates an Instruction by serializing a value with borsh.

  • Bincode, a compact serialization format that implements the Serde Rust APIs. As it does not have a specification nor a JavaScript implementation, and uses more CPU than borsh, it is not recommend for new code.

    Many system program and native program instructions are serialized with bincode, and it is used for other purposes in the runtime. In these cases Rust programmers are generally not directly exposed to the encoding format as it is hidden behind APIs.

    The Instruction::new_with_bincode function creates an Instruction by serializing a value with bincode.

  • Pack, a Solana-specific serialization API that is used by many older programs in the Solana Program Library to define their account format. It is difficult to implement and does not define a language-independent serialization format. It is not generally recommended for new code.

Developers should carefully consider the CPU cost of serialization, balanced against the need for correctness and ease of use: off-the-shelf serialization formats tend to be more expensive than carefully hand-written application-specific formats; but application-specific formats are more difficult to ensure the correctness of, and to provide multi-language implementations for. It is not uncommon for programs to pack and unpack their data with hand-written code.

Cross-program instruction execution

Solana programs may call other programs, termed cross-program invocation (CPI), with the invoke and invoke_signed functions. When calling another program the caller must provide the Instruction to be invoked, as well as the AccountInfo for every account required by the instruction. Because the only way for a program to acquire AccountInfo values is by receiving them from the runtime at the program entrypoint, any account required by the callee program must transitively be required by the caller program, and provided by its caller.

A simple example of transferring lamports via CPI:

use solana_program::{
    account_info::{next_account_info, AccountInfo},
    entrypoint,
    entrypoint::ProgramResult,
    program::invoke,
    pubkey::Pubkey,
    system_instruction,
    system_program,
};

entrypoint!(process_instruction);

fn process_instruction(
    program_id: &Pubkey,
    accounts: &[AccountInfo],
    instruction_data: &[u8],
) -> ProgramResult {
    let account_info_iter = &mut accounts.iter();

    let payer = next_account_info(account_info_iter)?;
    let recipient = next_account_info(account_info_iter)?;

    assert!(payer.is_writable);
    assert!(payer.is_signer);
    assert!(recipient.is_writable);

    let lamports = 1000000;

    invoke(
        &system_instruction::transfer(payer.key, recipient.key, lamports),
        &[payer.clone(), recipient.clone()],
    )
}

Solana also includes a mechanism to let programs control and sign for accounts without needing to protect a corresponding secret key, called program derived addresses. PDAs are derived with the Pubkey::find_program_address function. With a PDA, a program can call invoke_signed to call another program while virtually “signing” for the PDA.

A simple example of creating an account for a PDA:

use solana_program::{
    account_info::{next_account_info, AccountInfo},
    entrypoint,
    entrypoint::ProgramResult,
    program::invoke_signed,
    pubkey::Pubkey,
    system_instruction,
    system_program,
};

entrypoint!(process_instruction);

fn process_instruction(
    program_id: &Pubkey,
    accounts: &[AccountInfo],
    instruction_data: &[u8],
) -> ProgramResult {
    let account_info_iter = &mut accounts.iter();
    let payer = next_account_info(account_info_iter)?;
    let vault_pda = next_account_info(account_info_iter)?;
    let system_program = next_account_info(account_info_iter)?;

    assert!(payer.is_writable);
    assert!(payer.is_signer);
    assert!(vault_pda.is_writable);
    assert_eq!(vault_pda.owner, &system_program::ID);
    assert!(system_program::check_id(system_program.key));

    let vault_bump_seed = instruction_data[0];
    let vault_seeds = &[b"vault", payer.key.as_ref(), &[vault_bump_seed]];
    let expected_vault_pda = Pubkey::create_program_address(vault_seeds, program_id)?;

    assert_eq!(vault_pda.key, &expected_vault_pda);

    let lamports = 10000000;
    let vault_size = 16;

    invoke_signed(
        &system_instruction::create_account(
            &payer.key,
            &vault_pda.key,
            lamports,
            vault_size,
            &program_id,
        ),
        &[
            payer.clone(),
            vault_pda.clone(),
        ],
        &[
            &[
                b"vault",
                payer.key.as_ref(),
                &[vault_bump_seed],
            ],
        ]
    )?;
    Ok(())
}

Native programs

Some solana programs are native programs, running native machine code that is distributed with the runtime, with well-known program IDs.

Some native programs can be invoked by other programs, but some can only be executed as “top-level” instructions included by off-chain clients in a Transaction.

This crate defines the program IDs for most native programs. Even though some native programs cannot be invoked by other programs, a Solana program may need access to their program IDs. For example, a program may need to verify that an ed25519 signature verification instruction was included in the same transaction as its own instruction. For many native programs, this crate also defines enums that represent the instructions they process, and constructors for building the instructions.

Locations of program IDs and instruction constructors are noted in the list below, as well as whether they are invokable by other programs.

While some native programs have been active since the genesis block, others are activated dynamically after a specific slot, and some are not yet active. This documentation does not distinguish which native programs are active on any particular network. The solana feature status CLI command can help in determining active features.

Native programs important to Solana program authors include:

  • System Program: Creates new accounts, allocates account data, assigns accounts to owning programs, transfers lamports from System Program owned accounts and pays transaction fees.

    • ID: solana_program::system_program
    • Instruction: solana_program::system_instruction
    • Invokable by programs? yes
  • Compute Budget Program: Requests additional CPU or memory resources for a transaction. This program does nothing when called from another program.

    • ID: solana_sdk::compute_budget
    • Instruction: solana_sdk::compute_budget
    • Invokable by programs? no
  • ed25519 Program: Verifies an ed25519 signature.

    • ID: solana_program::ed25519_program
    • Instruction: solana_sdk::ed25519_instruction
    • Invokable by programs? no
  • secp256k1 Program: Verifies secp256k1 public key recovery operations.

    • ID: solana_program::secp256k1_program
    • Instruction: solana_sdk::secp256k1_instruction
    • Invokable by programs? no
  • BPF Loader: Deploys, and executes immutable programs on the chain.

    • ID: solana_program::bpf_loader
    • Instruction: solana_program::loader_instruction
    • Invokable by programs? yes
  • Upgradable BPF Loader: Deploys, upgrades, and executes upgradable programs on the chain.

    • ID: solana_program::bpf_loader_upgradeable
    • Instruction: solana_program::loader_upgradeable_instruction
    • Invokable by programs? yes
  • Deprecated BPF Loader: Deploys, and executes immutable programs on the chain.

    • ID: solana_program::bpf_loader_deprecated
    • Instruction: solana_program::loader_instruction
    • Invokable by programs? yes

Modules

  • account_info
    Account information.
  • address_lookup_table_account
    The definition of address lookup table accounts.
  • alt_bn128
  • big_mod_exp
  • blake3
    Hashing with the blake3 hash function.
  • borsh
    Utilities for the borsh serialization format.
  • borsh0_9
    Utilities for the borsh serialization format, version 0.9.
  • borsh0_10
    Utilities for the borsh serialization format, version 0.10.
  • bpf_loader
    The latest BPF loader native program.
  • bpf_loader_deprecated
    The original and now deprecated Solana BPF loader.
  • bpf_loader_upgradeable
    An upgradeable BPF loader native program.
  • clock
    Information about the network’s clock, ticks, slots, etc.
  • config
    The config native program.
  • debug_account_data
    Debug-formatting of account data.
  • decode_error
    Converting custom error codes to enums.
  • ed25519_program
    The ed25519 native program.
  • entrypoint
    The Rust-based BPF program entrypoint supported by the latest BPF loader.
  • entrypoint_deprecated
    The Rust-based BPF program entrypoint supported by the original BPF loader.
  • epoch_schedule
    Configuration for epochs and slots.
  • feature
    Runtime features.
  • fee_calculator
    Calculation of transaction fees.
  • hash
    Hashing with the SHA-256 hash function, and a general Hash type.
  • incinerator
    A designated address for burning lamports.
  • instruction
    Types for directing the execution of Solana programs.
  • keccak
    Hashing with the keccak (SHA-3) hash function.
  • lamports
    Defines the LamportsError type.
  • loader_instruction
    Instructions for the non-upgradable BPF loader.
  • loader_upgradeable_instruction
    Instructions for the upgradable BPF loader.
  • loader_v4
    The v3 built-in loader program.
  • loader_v4_instruction
    Instructions for the SBF loader.
  • log
    Logging utilities for Rust-based Solana programs.
  • message
    Sequences of Instructions executed within a single transaction.
  • native_token
    Definitions for the native SOL token and its fractional lamports.
  • nonce
    Durable transaction nonces.
  • program
    Cross-program invocation.
  • program_error
    The ProgramError type and related definitions.
  • program_memory
    Basic low-level memory operations.
  • program_option
    A C representation of Rust’s Option, used across the FFI boundary for Solana program interfaces.
  • program_pack
    The Pack serialization trait.
  • program_stubs
    Implementations of syscalls used when solana-program is built for non-SBF targets.
  • program_utils
    Contains a single utility function for deserializing from bincode.
  • pubkey
    Solana account addresses.
  • rent
    Configuration for network rent.
  • sanitize
    A trait for sanitizing values and members of over the wire messages.
  • sdk_ids
    A vector of Solana SDK IDs.
  • secp256k1_program
    The secp256k1 native program.
  • secp256k1_recover
    Public key recovery from secp256k1 ECDSA signatures.
  • serde_varint
    Integers that serialize to variable size.
  • serialize_utils
    Helpers for reading and writing bytes.
  • short_vec
    Compact serde-encoding of vectors with small length.
  • slot_hashes
    A type to hold data for the SlotHashes sysvar.
  • slot_history
    A type to hold data for the SlotHistory sysvar.
  • stake
    The stake native program.
  • stake_history
    A type to hold data for the StakeHistory sysvar.
  • syscalls
    Declarations of Solana program syscalls.
  • system_instruction
    Instructions and constructors for the system program.
  • system_program
    The system native program.
  • sysvar
    Access to special accounts with dynamically-updated data.
  • vote
    The vote native program.

Macros

  • custom_heap_default
    Define the default global allocator.
  • custom_panic_default
    Define the default global panic handler.
  • declare_deprecated_id
    Same as declare_id except that it reports that this ID has been deprecated.
  • declare_deprecated_sysvar_id
    Same as declare_sysvar_id except that it reports that this ID has been deprecated.
  • declare_id
    Convenience macro to declare a static public key and functions to interact with it.
  • declare_sysvar_id
    Declares an ID that implements SysvarId.
  • entrypoint
    Declare the program entrypoint and set up global handlers.
  • entrypoint_deprecated
    Declare the program entrypoint.
  • impl_sysvar_get
    Implements the Sysvar::get method for both SBF and host targets.
  • infoDeprecated
    Print a message to the log.
  • msg
    Print a message to the log.
  • pubkey
    Convenience macro to define a static public key.
  • unchecked_div_by_const
    Convenience macro for doing integer division where the operation’s safety can be checked at compile-time.

Attribute Macros

  • wasm_bindgen
    Re-export of wasm-bindgen.

Results

enum
solana_program::program_error::ProgramError
Reasons the program may fail
re-export
solana_program::sysvar::slot_history::ProgramError
module
solana_program::program_error
The ProgramError type and related definitions.
trait
solana_program::program_error::PrintProgramError
method
solana_program::program_error::ProgramError::eq
method
solana_program::program_error::ProgramError::fmt
method
solana_program::program_error::ProgramError::clone
method
solana_program::program_error::ProgramError::print
method
solana_program::program_error::ProgramError::serialize
method
solana_program::sysvar::Sysvar::get
Load the sysvar directly from the runtime.
method
solana_program::clock::Clock::get
method
solana_program::epoch_schedule::EpochSchedule::get
method
solana_program::rent::Rent::get
method
solana_program::sysvar::fees::Fees::get
method
solana_program::program_error::ProgramError::from
method
solana_program::program_pack::Pack::pack
Pack into slice
method
solana_program::program_error::ProgramError::clone
method
solana_program::program_pack::Pack::unpack
Unpack from slice and check if initialized
method
solana_program::account_info::AccountInfo::realloc
Realloc the account’s data and optionally …
method
solana_program::program_error::ProgramError::try_from
method
solana_program::program_error::ProgramError::deserialize
method
solana_program::account_info::AccountInfo::try_data_len
method
solana_program::account_info::AccountInfo::try_lamports
method
solana_program::account_info::AccountInfo::try_borrow_data
method
solana_program::program_pack::Pack::unpack_unchecked
Unpack from slice without checking if initialized
method
solana_program::sysvar::Sysvar::from_account_info
Deserializes the sysvar from its AccountInfo.
method
solana_program::feature::Feature::from_account_info
method
solana_program::slot_hashes::SlotHashes::from_account_info
method
solana_program::slot_history::SlotHistory::from_account_info
function
solana_program::account_info::next_account_info
Convenience function for accessing the next item in an …
method
solana_program::account_info::AccountInfo::try_data_is_empty
function
solana_program::account_info::next_account_infos
Convenience function for accessing multiple next items in …
method
solana_program::account_info::AccountInfo::try_borrow_lamports
method
solana_program::account_info::AccountInfo::try_borrow_mut_data
function
solana_program::stake::tools::get_minimum_delegation
Helper function for programs to call GetMinimumDelegation …
method
solana_program::account_info::AccountInfo::try_borrow_mut_lamports
function
solana_program::sysvar::instructions::get_instruction_relative
Returns the Instruction relative to the current Instruction…
function
solana_program::sysvar::instructions::load_current_index_checked
Load the current Instruction’s index in the currently …
function
solana_program::sysvar::instructions::load_instruction_at_checked
Load an Instruction in the currently executing Transaction …