Solana program fundamentals
Solana programs are on-chain services that receive instructions, validate the accounts supplied with each instruction, and change account state or ask another program to act. These examples build that model from simple messages to stored data, derived addresses, and composed program behavior.
Program model
Program entry point
A Solana program starts when the runtime calls its entry point with a program address, an ordered account list, and instruction bytes. The small basics/…/lib.rs example shows the smallest useful program: it receives a call and reports the program address without changing state.
The program itself does not hold mutable state between calls. Persistent state lives in accounts. An instruction therefore describes both the requested action and the accounts that the action may inspect or change.
References: basics/…/lib.rs• L6-19 basics/…/lib.rs• L1-9
Account boundaries
An account is the durable data and balance resource that a program uses to remember facts. The account-data example creates an account owned by the example program, sizes it for serialized AddressInfo data, and stores that value after the system program creates the account.
Account checks define the safety boundary for each instruction. The checking-accounts example teaches the core checks: use the expected program address, require the expected number and order of accounts, distinguish new accounts from initialized accounts, require a writable account for changes, and confirm that the system program is the real system program.
References: basics/…/create.rs• L12-33 basics/…/processor.rs• L11-17 basics/…/lib.rs• L11-61
Instruction processing
Instruction data
Instruction data carries the values that make one call different from another. The processing-instructions example decodes a name and height, then applies a simple rule to decide whether the visitor may ride. This shows the separation between the request data and the accounts used by a program.
Native programs must agree with their clients on a byte format. The examples use Borsh-compatible encoders for structured values. A program rejects bytes that do not match the instruction it expects instead of guessing at their meaning.
References: basics/…/lib.rs• L7-29 basics/…/README.md• L1-9 basics/…/processor.rs• L11-17
Instruction selection
A program can expose more than one action through the same entry point. The favorites example decodes a typed instruction and routes it to either PDA creation or PDA reading. The counter example uses a leading instruction value to select an increment action. It lets any key create a counter and increment any counter. The repository-layout example routes one decoded request to a ride, game, or food action.
This gives each on-chain action a clear contract: the action defines its input data, its required accounts, and the state change that it may perform. The account list remains part of that contract even when the instruction data is valid.
References: basics/…/processor.rs• L7-21 basics/…/lib.rs• L20-45 basics/…/README.md• L1-5 basics/…/processor.rs• L16-48
Instruction boundaries
An instruction may also select a behavior that reads fixed program data instead of changing account state. The repository-layout example keeps the shared request shape at the entry point, then passes only the fields needed by the selected attraction. An unknown attraction or attraction name returns invalid instruction data.
This pattern separates request decoding from action rules. It lets one program present several actions while keeping each action's input checks and result clear.
References: basics/…/processor.rs• L25-48 basics/…/eat_food.rs• L16-34 basics/…/get_on_ride.rs• L17-45 basics/…/play_game.rs• L16-34
Program-derived addresses
Deterministic account identity
A program-derived address (PDA) is a repeatable account address calculated from a program address and a set of seeds. It gives a program a known place for state without asking a user to create or remember a random key. The favorites example derives one record from the favorite seed and the user address.
The same seeds must be used when an account is created and when it is later read or changed. The program checks the supplied account against the derived address before it trusts the account as the intended record.
References: basics/…/create_pda.rs• L12-24 basics/…/get_pda.rs• L10-23
Program authority
A PDA has no private key. A program can act for it only when the runtime verifies the seed set and bump supplied by that program. The create example uses this signed invocation to let the program create an account at its derived address, then store the initial state in that account.
The PDA can also hold lamports that fund a system-program account creation. The pda-rent-payer example shows this authority flow, but its current example instruction does not check who may spend the rent-vault PDA. A real program must add an authority rule before it exposes this pattern.
After creation, a later instruction finds the same PDA and reads its stored value. This joins deterministic identity with durable account state.
References: basics/…/create_pda.rs• L26-48 basics/…/get_pda.rs• L15-23 basics/…/processor.rs• L13-19 basics/…/create_new_account.rs• L9-45
On-chain behavior
State transition
An on-chain behavior is a state transition: an instruction identifies the action, account checks establish which state may change, and the program writes the new value. Account creation adds a new owned state record. A later instruction reads that record and applies a small change such as incrementing a counter.
The foundational examples compose these parts into a predictable lifecycle:
- A client builds instruction data and supplies the required accounts.
- The program decodes the request and selects the action.
- The program checks account identity, ownership, signer status, and write access.
- The program creates or reads the account and applies the requested state change.
- The updated account can be read by a later instruction or by a client.
This diagram shows how a request becomes persistent program state.
flowchart LR
Client[Client request] --> Instruction[Instruction data and accounts]
Instruction --> Entry[Program entry point]
Entry --> Decode[Decode and select action]
Decode --> Check[Check account boundaries]
Check --> Create[Create owned account]
Check --> Update[Read and update account]
Create --> State[Persistent account state]
Update --> State
State --> Read[Later reads or instructions]
References: basics/…/create.rs• L12-33 basics/…/processor.rs• L11-17 basics/…/lib.rs• L34-45 basics/…/create_pda.rs• L26-48
Program composition
One program can call another program to complete part of a larger action. A cross-program invocation (CPI) is this call between on-chain programs. In the lever and hand examples, hand asks lever to switch shared power state, so the caller coordinates the action while the owning program keeps control of its state rules.
Account state and PDAs make this composition addressable. The caller passes the state account and the target program, while the target program validates and updates the account under its own rules. For the wider CPI flow, see Instruction and CPI patterns.
References: basics/…/test_cpi.rs• L25-31 basics/…/test_cpi.rs• L43-63 basics/…/Cargo.toml• L12-14 basics/…/Cargo.toml• L1-14
Learning paths
The examples are best read as three connected topics rather than as separate program styles:
- Account lifecycles explains how accounts are created, initialized, changed, and closed.
- Instruction and CPI patterns explains instruction contracts, dispatch, and calls between programs.
- PDA-based program design explains how deterministic addresses support program-owned state and authority.
References: basics/…/lib.rs• L10-35 basics/…/test_program_derived_addresses.rs• L38-69 basics/…/test_cpi.rs• L33-63
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the client instruction encoding aligned with the program's decoding and action selection.
- Keep account order, signer status, writable status, ownership, and expected program addresses aligned across every instruction contract.
- Keep PDA seed bytes and bump handling identical in client address derivation, account creation, and later account access.
- Keep the account data size and serialized state shape aligned when a program creates or updates an account.
- Keep the selected attraction names and their action routing aligned across the shared request and action modules.
- When a CPI target or its account contract changes, update the caller's passed accounts and target-program contract together.
- When a PDA funds account creation, keep its caller-authorization rule aligned with the accounts that can spend the PDA's lamports.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Instruction bytes do not match the expected format | The program cannot select or run the requested action | Decode a defined instruction type and return an invalid-data error for unknown bytes |
| An attraction value or name is not supported | The repository-layout action returns invalid instruction data | Route only known attraction types and match the requested name before running the action |
| An account is missing, in the wrong order, or has the wrong owner | The program may reject the request or risk changing the wrong state | Check account count and identity before reading or writing account data |
| A client uses different PDA seeds from the program | The supplied account is not the intended state record | Derive the address from one shared seed contract and verify it on chain |
| A PDA creation call uses invalid signing seeds | Account creation fails and no durable state is made | Use the exact seeds and bump used to derive the PDA |
| A rent-vault instruction has no caller authority check | Any caller can spend the PDA's lamports to fund an arbitrary account | Require an authorized signer or stored authority before the PDA signs the system-program call |
| A state account is read with a different data shape | State decoding fails or produces invalid behavior | Keep serialized state definitions and account space in sync |
| A CPI passes the wrong target program or state account | The composed action fails or reaches the wrong authority boundary | Require the expected target program and let the owning program validate its accounts |
Key files
| File | Why to read it |
|---|---|
| basics/…/lib.rs | Smallest entry-point example and the basic runtime call shape |
| basics/…/lib.rs | Core account and program identity checks |
| basics/…/create.rs | Account creation, funding, ownership, and stored data in one flow |
| basics/…/processor.rs | Instruction decoding for the account-data example |
| basics/…/lib.rs | Structured instruction data and a data-driven action |
| basics/…/README.md | Client and program agreement on custom instruction encoding |
| basics/…/processor.rs | Shared request decoding and routing to several actions |
| basics/…/get_on_ride.rs | Action-level checks based on decoded request data |
| basics/…/processor.rs | PDA instruction selection and dispatch |
| basics/…/create_pda.rs | PDA validation, creation, and initial state storage |
| basics/…/get_pda.rs | Deterministic lookup and state reading |
| basics/…/create_new_account.rs | PDA-funded account creation and its required authority boundary |
| basics/…/lib.rs | Small instruction selector and persistent counter update |
| basics/…/README.md | Counter behavior and its in-process test and deploy flow |
| basics/…/test_cpi.rs | Caller side and observed result of a cross-program action |
| basics/…/Cargo.toml | Native caller dependency on the target program's CPI interface |
| basics/…/Cargo.toml | Target program package and CPI feature contract |