Homework 1: Building a Custom RPC Handler
← Back to Building and Integrating Custom RPC Handlers
Overview
In this comprehensive workshop, you will implement a complete RPC handler from scratch. This exercise will consolidate everything you've learned by having you build a production-quality GetAccountBalance command for Rippled.
Estimated Time: 4-6 hours
Difficulty Level: Intermediate to Advanced
Learning Objectives
By completing this workshop, you will:
Understand how handlers are registered in the central handler table
Implement proper input validation and error handling
Access ledger data through the JsonContext API
Calculate XRP reserves and available balances
Build well-structured JSON responses
Write comprehensive unit tests for your handler
Follow Rippled coding conventions and best practices
Prerequisites
Before starting this workshop, ensure you have:
The Challenge
You will implement a new RPC command called get_account_balance that provides detailed balance information for an XRP Ledger account, including:
Current XRP balance
Base reserve requirement
Owner reserve requirement (based on owned objects)
Available balance (balance minus reserves)
Owner count (number of owned ledger objects)
Optional: Trust line balances (if requested)
Part 1: Understanding the RPC System
Before writing code, answer these architectural questions to demonstrate your understanding:
Questions
Handler Discovery
How does Rippled locate and invoke the correct handler when a request arrives?
What data structure stores the mapping between command names and handler functions?
Where in the codebase is this mapping defined?
Request Lifecycle
Trace the complete journey of an RPC request from reception to response
What stages does a request pass through?
At what point is permission checking performed?
Role-Based Access Control
What are the five role levels in Rippled's RBAC system?
How is a client's role determined?
Can you explain the difference between GUEST, USER, and ADMIN roles?
Write your answers in answers.md
Part 2: Handler Implementation
Task 2.1: Create the Handler File
Create a new file:
File: src/xrpld/rpc/handlers/GetAccountBalance.cpp\
Follow the standard file header format used in other handlers.
Task 2.2: Implement the Handler Function
Your handler must:
Validate the
accountparameterCheck if the field exists
Validate the account address format
Return appropriate errors for invalid input
Handle optional
ledger_indexparameterSupport
"current","validated","closed"Support specific ledger numbers
Default to current ledger if not specified
Query the ledger for account data
Use
RPC::lookupLedger()to get the ledgerRead the account object using
keylet::account()Handle account not found scenario
Calculate balance information
Get current XRP balance
Calculate base reserve (from fees)
Calculate owner reserve (based on owner count)
Compute available balance (balance - total reserve)
Build the response
Include all required fields
Use proper JSON structure
Mark response as validated when appropriate
Task 2.3: Register the Handler
Add your handler to the central handler table:
File: src/xrpld/rpc/handlers/Handlers.cpp
Choose appropriate values for:
Minimum required role
Execution conditions
API version support
Task 2.4: Expected Request Format
Task 2.5: Expected Response Format
Part 3: Error Handling
Your handler must properly handle these error scenarios:
Required Error Cases
Missing
accountfieldError:
rpcINVALID_PARAMSMessage: "Missing 'account' field"
Invalid account format
Error:
rpcACT_MALFORMEDMessage: "Invalid account address"
Account not found
Error:
rpcACT_NOT_FOUNDMessage: "Account not found"
Ledger not available
Error:
rpcLGR_NOT_FOUNDMessage: "Ledger not found"
Invalid ledger index
Error:
rpcINVALID_PARAMSMessage: "Invalid ledger_index"
Task 3.1: Implement Error Handling
Ensure each error case returns the proper error code and a descriptive message.
Part 4: Input Validation
Implement comprehensive validation for:
Task 4.1: Account Parameter
Task 4.2: Ledger Parameter
Task 4.3: Edge Cases
Handle these special scenarios:
Empty account string
Account with no owner count field (shouldn't happen, but be defensive)
Ledger with missing fee information
Balance exceeding maximum XRP supply
Part 5: Testing
Create comprehensive unit tests for your handler.
Task 5.1: Create Test File
File: src/test/rpc/GetAccountBalance_test.cpp
Task 5.2: Implement Test Cases
Test 5.2.1: Valid Request
Test 5.2.2: Invalid Account Address
Test 5.2.3: Account Not Found
Test 5.2.4: Ledger Selection
Test 5.2.5: Permission Levels
Test 5.2.6: Missing Parameters
Test 5.2.7: Reserve Calculations
Task 5.3: Optional Advanced Tests
Concurrent access: Test handler with multiple simultaneous requests
Performance: Measure handler execution time
Ledger transitions: Test behavior during ledger close
Large owner counts: Test with accounts owning many objects
Part 6: Optional Enhancements
After completing the core implementation, consider these enhancements:
Enhancement 6.1: Trust Line Balances
Add optional include_lines parameter:
Response includes trust lines:
Enhancement 6.2: Currency Filtering
Add currency filter for trust lines:
Enhancement 6.3: Pagination
For accounts with many trust lines, add pagination:
Reference Implementation Files
Study these existing handlers as reference:
AccountInfo.cpp
Location:
src/xrpld/rpc/handlers/AccountInfo.cppSimilar structure to your handler
Shows account querying and response building
AccountLines.cpp
Location:
src/xrpld/rpc/handlers/AccountLines.cppTrust line iteration and filtering
Pagination implementation
LedgerData.cpp
Location:
src/xrpld/rpc/handlers/LedgerData.cppLedger object iteration
Large result set handling
RPCHelpers.h
Location:
src/xrpld/rpc/detail/RPCHelpers.hUtility functions for common tasks
Account parsing, ledger lookup, etc.
Submission Checklist
Before considering your implementation complete:
Evaluation Criteria
Your implementation will be evaluated on:
Correctness
40%
Handler produces correct results
Error Handling
20%
All error cases properly handled
Code Quality
20%
Follows best practices and conventions
Testing
15%
Comprehensive test coverage
Documentation
5%
Clear comments and documentation
Getting Help
If you encounter difficulties:
Review the module topics
Re-read relevant sections
Study code examples carefully
Examine existing handlers
AccountInfo.cpp is the best reference
Look for similar patterns
Check Rippled documentation
Ask for help
Share specific error messages or issues
Solution Resources
After attempting the workshop independently:
Hints: For guidance see hints →
Code snippets: For common patterns see snippets →
Full solution: Available upon request after submission
What You'll Build
By the end of this workshop, you'll have:
A fully functional RPC handler integrated into Rippled
Comprehensive error handling and input validation
Production-quality unit tests
Understanding of the complete RPC development lifecycle
Skills to build any custom handler you need
Good luck! Enjoy building your first RPC handler!
Last updated

