This appendix walks through three actual RPC handlers from the rippled codebase. These handlers are battle-tested, production-ready implementations that demonstrate best practices and patterns you should follow in your own custom handlers.
Each example includes:
Complete code walkthrough
Key implementation patterns
How it integrates with the broader system
Performance and security considerations
1. AccountInfo Handler
Overview
The AccountInfo handler queries account state and returns detailed information about an account on the ledger. It's one of the most commonly used RPC commands and demonstrates fundamental patterns.
Source: src/xrpld/rpc/handlers/AccountInfo.cpp
Complete Implementation
Key Patterns Demonstrated
1. Parameter Validation
Always check for required parameters before accessing
Use predefined JSON string constants (jss::*) for consistency
Return early with error if validation fails
2. Account Parsing
Use template function parseBase58<>() for Base58Check decoding
Check optional return value before using
Return specific error with descriptive message
3. Ledger Lookup
Use RPC::lookupLedger() to get appropriate ledger
Helper function returns error if ledger unavailable
Forward error response to client
4. Data Query
Use keylet functions to locate ledger objects
Check for null before dereferencing
Return appropriate "not found" error
5. Response Construction
Build response as Json::Value object
Include both requested data and contextual information
Always return ledger context for validation
Error Handling
Error Condition
Error Code
Message
Missing account parameter
rpcINVALID_PARAMS
"Missing 'account' field"
Malformed account address
rpcACT_MALFORMED
"Account is malformed"
No ledger available
rpcNO_CURRENT
"No current ledger"
Account doesn't exist
rpcACT_NOT_FOUND
"Account not found"
2. Submit Handler
Overview
The Submit handler processes transaction submission. It demonstrates more complex patterns including transaction validation, fee calculation, and async processing.
Provide specific error messages for validation failures
Validate all inputs before state-changing operations
3. Context-Dependent Logic
Factor validation against current ledger state
Provide helpful error messages with actual values
Calculate derived values only when needed
4. Async Operation Handling
Use job queue for async operations
Map internal result codes to RPC errors
Return human-readable error explanations
Error Scenarios
Scenario
Code
Response
Unauthorized client
rpcFORBIDDEN
403 Forbidden
Missing tx_json
rpcINVALID_PARAMS
400 Bad Request
Unparseable transaction
rpcINVALID_PARAMS
400 Bad Request
Fee below minimum
rpcINVALID_PARAMS
400 Bad Request with fee info
Submission failed
rpcTXN_FAILED
500 with result code
3. ServerInfo Handler
Overview
The ServerInfo handler returns comprehensive information about the running node. It demonstrates accessing application state and formatting complex hierarchical data.
Source: src/xrpld/rpc/handlers/ServerInfo.cpp
Implementation Highlights
Key Patterns Demonstrated
1. Hierarchical JSON Construction
Build nested JSON structures by chaining subscript operators
Use jss:: constants for all key names
JSON library auto-creates intermediate objects
2. Optional Data Handling
Check for optional data before including
Leave out fields if data isn't available
Client code should handle missing optional fields
3. Role-Based Response Modification
Tailor response content based on caller's role
Include sensitive information only for authorized clients
TER const result = context.app.getJobQueue()
.postTransaction(txn);
if (result != tesSUCCESS) {
return rpcError(rpcTXN_FAILED, transResultString(result));
}