Overview
Thelight-account-checks crate provides unified account validation for Light Protocol programs. It offers a consistent API across both solana-program and pinocchio SDKs through trait abstraction.
Crate: light-account-checksLocation:
program-libs/account-checks/Error Codes: 12006-12021
Key Features
SDK Abstraction
Single codebase works with both Solana and Pinocchio account types
Type Safety
8-byte discriminators ensure correct account types
Rich Errors
Detailed error messages with file:line:column locations
Zero Cost
No runtime overhead from abstraction
Core Types
AccountInfoTrait
Trait abstraction over account info types:solana_program::account_info::AccountInfo(feature:solana)pinocchio::account_info::AccountInfo(feature:pinocchio)TestAccountInfo(feature:test-only)
AccountIterator
Enhanced iterator with error location tracking:Validation Functions
Ownership Checks
check_owner
check_owner
Verifies account is owned by expected program.Use case: Verify account belongs to your program before reading/writing.
check_program
check_program
Checks if account is a program account (executable).Use case: Validate CPI targets are executable programs.
Permission Checks
check_signer
check_signer
Verifies account signed the transaction.Use case: Ensure authority accounts have valid signatures.
check_mut / check_non_mut
check_mut / check_non_mut
Verifies mutability matches expectation.Use case: Prevent accidental writes to readonly accounts.
Discriminator Validation
check_discriminator
check_discriminator
Validates account type via 8-byte prefix.Example:
set_discriminator
set_discriminator
Initializes account with type discriminator.Use case: Called once during account initialization.
PDA Validation
check_pda_seeds
check_pda_seeds
Verifies PDA derivation and finds bump seed.Returns: Canonical bump seedExample:
check_pda_seeds_with_bump
check_pda_seeds_with_bump
Verifies PDA with known bump seed.Use case: When bump is stored in account data.
Rent Exemption
check_account_balance_is_rent_exempt
check_account_balance_is_rent_exempt
Validates account has sufficient lamports for rent exemption.Returns: Rent-exempt balance for the size
Combined Validators
check_account_info_mut
check_account_info_mut
Combined validation: writable + owned + discriminator.
check_account_info_non_mut
check_account_info_non_mut
Combined validation: readonly + owned + discriminator.
Error Codes
| Code | Error | Description |
|---|---|---|
| 12006 | InvalidDiscriminator | Account type mismatch |
| 12007 | OwnerMismatch | Account owned by wrong program |
| 12008 | AccountNotSigner | Required signer missing |
| 12009 | AccountMutable | Account is writable when readonly expected |
| 12010 | InvalidAccountSize | Account data too small |
| 12011 | AccountImmutable | Account is readonly when writable expected |
| 12012 | AlreadyInitialized | Account already has discriminator |
| 12013 | SignerCheckFailed | Authority signature validation failed |
| 12014 | InvalidPda | PDA derivation doesn’t match |
| 12015 | InvalidBump | Invalid bump seed for PDA |
| 12016 | BorrowAccountDataFailed | Failed to borrow account data |
| 12017 | BorrowAccountDataMutFailed | Failed to mutably borrow account data |
| 12018 | NotRentExempt | Account balance below rent exemption |
| 12019 | AccountNotInitialized | Account discriminator is zero |
| 12020 | InvalidProgramExecutable | Account is not executable |
| 12021 | InvalidAccountIndex | Index out of bounds |
Usage Patterns
Basic Validation
With AccountIterator
PDA Validation
Custom Account Types
Feature Flags
Enables
solana-program::account_info::AccountInfo implementationEnables
pinocchio::account_info::AccountInfo implementationEnables
TestAccountInfo mock implementation for unit testsEnables detailed error messages with
solana_msg::msg!()Testing
Best Practices
Always Validate Early
Always Validate Early
Perform all account validation at the start of instruction processing, before any state changes.
Use Combined Validators
Use Combined Validators
Prefer
check_account_info_mut and check_account_info_non_mut for common validation patterns.Leverage AccountIterator
Leverage AccountIterator
Use
AccountIterator for cleaner code with better error messages including source locations.Define Discriminators as Constants
Define Discriminators as Constants
Always use constant discriminators to catch type mismatches at compile time.
Resources
Source Code
View on GitHub
API Docs
Rust documentation