Transaction Structure
A transaction in Cosmos SDK consists of messages, signatures, and metadata:Message Interface
Example Transaction
Transaction Lifecycle
CheckTx: Mempool Validation
CheckTx Validation
Signature Verification
Signature Verification
Verify all required signatures are present and valid
Account Sequence
Account Sequence
Check transaction sequence matches account sequence (prevents replay)
Sufficient Fees
Sufficient Fees
Ensure transaction fee meets minimum gas price
Basic Validation
Basic Validation
Run message ValidateBasic() for stateless checks
Ante Handler
Ante Handler
Execute ante handler for additional validation
Transaction Execution
runTx Method
Core transaction execution logic:Message Execution
Ante Handler
The ante handler performs pre-execution validation and setup:Ante Decorator Chain
Ante handlers are composed of decorators:Custom Ante Decorator
Post Handler
Optional post-execution logic:- Emit additional events
- Refund unused gas
- Execute cleanup logic
Gas Metering
Gas Meter
Gas Config
Gas Consumption
Execution Modes
Mode-Specific Behavior
Priority and Ordering
Transaction Priority
Priority Mempool
Events
Transactions emit events for indexing:Querying Events
Error Handling
Transaction Simulation
Context Management
Context Branching
Best Practices
Validate Early
Validate Early
Perform stateless validation in
ValidateBasic() before execution.Emit Meaningful Events
Emit Meaningful Events
Emit events with relevant attributes for transaction indexing.
Handle Errors Properly
Handle Errors Properly
Use wrapped errors with context for better debugging.
Optimize Gas Usage
Optimize Gas Usage
Minimize state reads/writes and expensive operations.
Use Context Branching
Use Context Branching
Branch context for speculative execution that may need rollback.
Related Topics
BaseApp
Transaction routing and execution
State Management
How transactions modify state
Gas and Fees
Gas metering and fee payment
Ante Handler
Building custom ante handlers