Appendix: Handler Examples

Reference Guide to Production RPC Handlers

← Back to Building and Integrating Custom RPC Handlers


Introduction

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.

Source: src/xrpld/rpc/handlers/Submit.cpp

Implementation Highlights

Key Patterns Demonstrated

1. Permission Checking

2. Complex Validation

  • Use specialized parsing functions when available

  • 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

4. Accessing Application State

  • Use context object to access application components

  • Through context.app, access the main Application singleton

  • Cache results when making multiple queries

No Error Handling?

Notably, ServerInfo returns early without extensive error checking because:

  1. Server state is always available

  2. Missing data is handled by omitting fields

  3. No user input to validate

  4. No permission that would cause rejection

This demonstrates that not all handlers need error-heavy validation. Only validate what the client provides.


Common Implementation Patterns

Pattern 1: Parameter Extraction and Validation

Pattern 2: Ledger Access

Pattern 3: Permission Checking

Pattern 4: Complex Result Building


Performance Considerations

1. Minimize Ledger Queries

2. Avoid Synchronous Blocking

3. Check Permissions Early


Testing These Handlers

See Testing RPC Handlers for comprehensive testing strategies.

Quick test template:


← Back to Appendices | Helper Functions →


Related Module Sections:

Last updated