Instructions and Messages
Instructions and messages are fundamental components of Arch's transaction processing system that enable communication between clients and programs. They form the basis for all state changes and interactions within the Arch network.
Instructions
An instruction is the basic unit of program execution in Arch. It contains all the information needed for a program to execute a specific operation. Instructions are processed atomically, meaning they either complete entirely or have no effect.
Structure
pub struct Instruction {
/// Program ID that executes this instruction
pub program_id: Pubkey,
/// Accounts required for this instruction
pub accounts: Vec<AccountMeta>,
/// Instruction data
pub data: Vec<u8>,
}
Components:
- Program ID: The pubkey of the program that will process the instruction
- Accounts: List of accounts required for the instruction, with their metadata
- Instruction Data: Custom data specific to the instruction, typically serialized using Borsh or another format
Account Metadata
pub struct AccountMeta {
pub pubkey: Pubkey,
pub is_signer: bool,
pub is_writable: bool,
}
pubkey
: The account's public keyis_signer
: Whether the account must sign the transactionis_writable
: Whether the account's data can be modified
Messages
A message is a collection of instructions that form a transaction. Messages ensure atomic execution of multiple instructions, meaning either all instructions succeed or none take effect.
Structure
pub struct Message {
/// List of account keys referenced by the instructions
pub account_keys: Vec<Pubkey>,
/// Recent blockhash
pub recent_blockhash: Hash,
/// List of instructions to execute
pub instructions: Vec<CompiledInstruction>,
}
Components:
- Account Keys: All unique accounts referenced across instructions
- Recent Blockhash: Used for transaction uniqueness and timeout
- Instructions: List of instructions to execute in sequence
Instruction Processing Flow:
-
Client creates an instruction with:
- Program ID to execute the instruction
- Required accounts with appropriate permissions
- Instruction-specific data (serialized parameters)
-
Instruction(s) are bundled into a message:
- Multiple instructions can be atomic
- Account permissions are consolidated
- Blockhash is included for uniqueness
-
Message is signed to create a transaction:
- All required signers must sign
- Transaction size limits apply
- Fees are calculated
-
Transaction is sent to the network:
- Validated by validators
- Processed in parallel when possible
- Results are confirmed
-
Program processes the instruction:
- Deserializes instruction data
- Validates accounts and permissions
- Executes operation
- Updates account state
Best Practices:
-
Account Validation
- Always verify account ownership
- Check account permissions
- Validate account relationships
-
Data Serialization
- Use consistent serialization format (preferably Borsh)
- Include version information
- Handle errors gracefully
- Validate data lengths
-
Error Handling
- Return specific error types
- Provide clear error messages
- Handle all edge cases
- Implement proper cleanup
Cross-Program Invocation (CPI)
Instructions can invoke other programs through CPI, enabling composability:
-
Create new instruction for target program:
- Specify program ID
- Include required accounts
- Prepare instruction data
-
Pass required accounts:
- Include all necessary accounts
- Set proper permissions
- Handle PDA derivation
-
Invoke using
invoke
orinvoke_signed
:- For regular accounts:
invoke
- For PDAs:
invoke_signed
- Handle return values
- For regular accounts:
-
Handle results:
- Check return status
- Process any returned data
- Handle errors appropriately
Security Considerations:
-
Account Verification
- Verify all account permissions
- Check ownership and signatures
- Validate account relationships
- Prevent privilege escalation
-
Data Validation
- Sanitize all input data
- Check buffer lengths
- Validate numerical ranges
- Prevent integer overflow
-
State Management
- Maintain atomic operations
- Handle partial failures
- Prevent race conditions
- Ensure consistent state
Common Patterns:
-
Initialization
- Create necessary accounts
- Set initial state
- Assign proper ownership
-
State Updates
- Validate permissions
- Update account data
- Maintain invariants
-
Account Management
- Close accounts when done
- Manage PDAs properly