entrypoint, which defines where the execution of a program
begins.
Program Structure
While there are no strict rules for file structure, Solana programs typically follow a common pattern:entrypoint.rs: Defines the entrypoint that routes incoming instructions.state.rs: Defines program state (account data).instructions.rs: Defines the instructions that the program can execute.processor.rs: Defines the instruction handlers (functions) that implement the business logic for each instruction.error.rs: Defines custom errors that the program can return.
Example Program
To demonstrate how to build a native Rust program with multiple instructions, we’ll walk through a simple counter program that implements two instructions:InitializeCounter: Creates and initializes a new account with an initial value.IncrementCounter: Increments the value stored in an existing account.
lib.rs file,
though in practice you may want to split larger programs into multiple files.
Full Program Code
Full Program Code
Part 1: Writing the Program
Let’s start by building the counter program. We’ll create a program that can initialize a counter with a starting value and increment it.Create a new program
src/lib.rs and Cargo.toml files.edition field in Cargo.toml to 2021. Otherwise, you might
encounter an error when building the program.Add dependencies
solana-program for the core SDK and borsh for serialization.Configure crate-type
[lib] section
to configure how Cargo builds the program.Setup program entrypoint
lib.rs:input data into the parameters of the
process_instruction function.A Solana program entrypoint has the following function signature. Developers
are free to create their own implementation of the entrypoint function.Define program state
data field of the account.Add the following code to lib.rs:Define instruction enum
lib.rs:Implement instruction deserialization
instruction_data (raw bytes) into one of our
CounterInstruction enum variants. The Borsh try_from_slice method handles
this conversion automatically.Update the process_instruction function to use Borsh deserialization:Route instructions to handlers
process_instruction function to route instructions
to their appropriate handler functions.This routing pattern is common in Solana programs. The instruction_data is
deserialized into a variant of an enum representing the instruction, then the
appropriate handler function is called. Each handler function includes the
implementation for that instruction.Add the following code to lib.rs updating the process_instruction function
and adding the handlers for the InitializeCounter and IncrementCounter
instructions:Implement initialize handler
create_account
instruction. The new account is created with our program as the owner, giving
our program the ability to write to the account and initialize the data.Add the following code to lib.rs updating the process_initialize_counter
function:Implement increment handler
- Reads the account
datafield for thecounter_account - Deserializes it into a
CounterAccountstruct - Increments the
countfield by 1 - Serializes the
CounterAccountstruct back into the account’sdatafield
lib.rs updating the process_increment_counter
function:Completed Program
- Entrypoint: Defines where program execution begins and routes all incoming requests to appropriate instruction handlers
- Instruction Handling: Defines instructions and their associated handlers functions
- State Management: Defines account data structures and manages their state in program owned accounts
- Cross-program invocation (CPI): Calls the System Program to create new program owned accounts
Part 2: Testing the Program
Now let’s test our counter program. We’ll use LiteSVM, a testing framework that lets us test programs without deploying to a cluster.Add test dependencies
litesvm for
testing and solana-sdk.Create test module
lib.rs, directly below the program code:#[cfg(test)] attribute ensures this code is only compiled when running
tests.Initialize test environment
lib.rs updating the test_counter_program function:Load the program
cargo build-sbf command to build the program. This will generate the
counter_program.so file in the target/deploy directory.--arch v0. Solana CLI 4.3 and
later build SBPF v3 programs by default, and LiteSVM 0.6.1 can’t load
them: without the flag, add_program_from_file fails with
Instruction(InvalidAccountData). Older Solana CLI releases already
build v0 by default and accept the flag.edition in Cargo.toml is set to 2021.test_counter_program function to load the program into the test
environment.cargo build-sbf --arch v0 before running tests to generate the .so
file. The test loads the compiled program.Test initialization instruction
lib.rs updating the test_counter_program function:Verify initialization
lib.rs updating the test_counter_program function:Test increment instruction
lib.rs updating the test_counter_program function:Verify final results
lib.rs updating the test_counter_program function:--nocapture flag prints the
output of the test.Part 3: Invoking the Program
Now let’s add a client script to invoke the program.Create client example
Cargo.toml:Implement client code
examples/client.rs and replace the program_id with
the output of the previous command:Part 4: Deploying the Program
Now that we have our program and client ready, let’s build, deploy, and invoke the program.Build the program
--arch v0 for LiteSVM, but solana-test-validator 4.1 and later
rejects SBPF v0 programs, so rebuild before you deploy:cargo build-sbf --arch v3 instead.target/deploy/:Start local validator
Deploy the program
solana program show command with your
program ID:Run the client
http://localhost:8899 that the
solana-test-validator is running on.