RPC subsystem
This page is for developers. It follows a request from the wire to a handler and back, and shows how to add a method. The methods themselves are listed in API. How the data behind them is stored is in Storage.
All of it lives in crates/ledger-engine/src. The main files:
| File | Role |
|---|---|
server.rs | reads requests, picks local or forwarded, builds the response |
api.rs | Engine, Engine::handle (the method dispatch), RpcError and shared parsing helpers |
owned.rs, nft.rs, proof.rs | more handlers, as further impl Engine blocks |
selector.rs | object key from ledger_entry and proof_entry params |
forward.rs | which requests go to xrpld, and the client that sends them |
subs.rs | subscribe and unsubscribe |
limits.rs | the time limit of one request |
guard.rs | per-client limits and request weights |
xrpl_json.rs | binary objects and transactions as JSON, as xrpld writes them |
ter.rs | transaction result codes and messages for stream messages |
§From the wire to a handler
There are two entry points in server.rs, one per transport.
- HTTP:
http_rpctakesPOST /with{"method": ..., "params": [{...}]}. A body that is not a JSON object gets status 400 and a plain text message.params_oftakes the first element ofparams; a missing, null or emptyparamsgives an empty object, and anything that is not an object givesbadSyntax. An emptymethodgivesunknownCmd. - WebSocket:
ws_answerhandles one text message. The method iscommand, ormethodif there is nocommand. The params are the whole message withoutcommand,methodandid. A message that is not a JSON object getsbadSyntax.
Both then ask Guard::request whether the client is within its budget, with a weight from guard::weight. A client over its budget gets slowDown. On WebSocket, subscribe and unsubscribe go to subscription, which uses subs::parse and the Hub. Over HTTP there is no such branch, so they end in unknownCmd.
Everything else goes to call. It decides, in this order:
- On a public listener,
submit,simulateandripple_path_findgetnotSupported. submitandsimulatego to a node only if the server was started to forward them and has a forwarder. Otherwise they getunknownCmd. Their params pass throughforward::submission_paramsfirst.- If
forward::forwardedsays so, the request goes to a node. Without a forwarder it getsunknownCmd. - The rest is answered locally by
call_own.
call_own takes a request slot, runs the handler on a blocking thread with tokio::task::spawn_blocking, and starts the time limit on that thread with limits::start just before it calls Engine::handle. For server_info, call then adds load_factor and rippled_version from the node’s last server_info (forward::node_info), without waiting for the node.
§The method dispatch
Engine::handle(method, params) in api.rs is the one place where a method name meets its handler. It is a match on the name. Each arm calls a method on Engine that takes the params as &Map<String, Value> and returns Res<Map<String, Value>>, which is Result<Map<String, Value>, RpcError>. A name with no arm gets unknownCmd.
Before the match, handle reads the API version with api::api_version. A missing or null api_version is 2. Values 1 and 2 are accepted, as a number or a string. Anything else gets invalid_API_version (code 6000), for every method. The handlers whose answer differs between versions take the version as a second argument, for example ledger_rpc(p, api) and account_tx(p, api). The others ignore it.
Handlers read params straight from the map. api.rs has helpers for the common cases, and they return the errors Clio returns:
account_id: a base58 address, oractMalformed;hash256: a 64-character hex hash, orinvalidParamswith the message<what>Malformed;as_u32andas_bool: numbers and flags, accepting the forms clients send;Engine::resolve: the ledger fromledger_hashorledger_index, or the latest validated one. A ledger outside the served range getslgrNotFound;Engine::resolve_stateandEngine::resolve_old_state: the same for methods that read state, which exists only from the state snapshot on (or where the old-state index covers it).
The handler returns only its fields. server.rs adds status, the warnings entry with id 2001, and on WebSocket id, api_version and type.
§Local and forwarded methods
Astraea serves only validated ledgers. Requests it can’t answer from validated data go to xrpld, by the rule Clio uses. forward::forwarded returns true for:
- the node methods in
FORWARDED:server_state,fee,ledger_current,server_definitions; - a method in
CURRENT_ON_NODEasked withledger_indexof"current"or"closed"; account_infoorledgerwith"queue": true.
CURRENT_ON_NODE is an allowlist on purpose. Without it, ledger_index: "current" would pass any method to the node, admin ones included. Methods in CLIO_ONLY (for example nft_history and mpt_holders) are never forwarded, because Clio always serves them itself. The user-facing list is in API.
Forwarder::forward sends the method and params to the nodes as JSON-RPC and returns the node’s result as it is, including a node’s own error. Nodes are tried in turn, a node that just failed last, within TOTAL_LIMIT (8 seconds) over all of them. Identical requests within CACHE_FOR (250 ms) get the cached answer. When all nodes fail, the answer is failedToForward (code 90), and for the next DOWN_FOR (5 seconds) it is given at once. Forwarder::send_transaction handles submit: never cached, and sent to another node only when the first can’t have received it.
Forwarded answers carry "forwarded": true. They never pass through Engine::handle, so a forwarded method needs no handler.
§Errors
An error is an RpcError: a fixed error name (error), a number (code) and a message. Handlers build one with api::rpc_err or a shortcut: invalid_params, lgr_not_found, internal. Lower modules that return String errors are mapped with internal, which gives internal (code 73), except for the time-limit error described below.
The names and codes follow Clio. The common ones are below; handlers build more where a method needs them, such as actNotFound, txnNotFound, entryNotFound, invalidLgrRange and the malformed* codes of ledger_entry. Search for rpc_err to see them all.
| Error | Code | Where |
|---|---|---|
badSyntax | 1 | request is not a JSON object, or params is not an object |
tooBusy | 9 | no slot in time, or past the time limit |
slowDown | 10 | client over its guard budget |
lgrNotFound | 21 | ledger outside the served range |
invalidParams | 31 | bad or missing parameter; the message names it, as in Clio |
unknownCmd | 32 | no such method, or not available on this server |
actMalformed | 35 | bad account address |
internal | 73 | store or decoding error |
notSupported | 75 | method refused on a public listener; unsupported stream; ledger_data with out_of_order |
failedToForward | 90 | no node answered |
invalid_API_version | 6000 | api_version other than 1 or 2 |
server.rs turns an RpcError into the client format with error_fields: error, error_code, error_message, status: "error", type: "response" and the original request. Over HTTP these sit inside result, as in Clio. Over WebSocket they are at the top level. A node error in a forwarded answer stays an error over WebSocket too, with its fields at the top level. A client over its budget on HTTP gets status 503, as from Clio.
§Limits a handler runs under
A handler runs on a blocking thread, which can’t be stopped from outside, so it stops itself: limits::start sets a deadline on the thread: REQUEST_LIMIT (10 seconds), or PUBLIC_REQUEST_LIMIT (5 seconds) on a public listener. A loop whose length the client controls calls limits::check_rpc() (or limits::check() where errors are strings). Past the deadline the request ends with tooBusy, and api::internal turns the string error limits::EXCEEDED back into tooBusy. Threads that read for a request take its deadline with limits::current and limits::start_at.
The request also needs a slot. Local requests share MAX_INFLIGHT (32) slots, forwarded ones MAX_FORWARDS (16). proof_* methods have their own MAX_PROOFS (4) and don’t queue: when those are taken, the answer is tooBusy at once. Proofs for a transaction or a fixed ledger are cached. Each request also counts against the client’s budget in guard by guard::weight; the weights are listed on Web server.
Listeners, public access and these numbers in more detail are on Web server.
§Adding a method
For a new method, open an issue first (CONTRIBUTING.md). Astraea answers like Clio 2.8, field for field, so start from Clio’s answer for the same request.
- Write the handler as a method on
Engine:fn my_method(&self, p: &Map<String, Value>) -> Res<Map<String, Value>>, withapi: u32as a second argument if the answer depends on the version. Put it in the module of its kind (api.rs,owned.rs,nft.rs,proof.rs) or in a new module declared inlib.rswith its ownimpl Engineblock. - Read params with the helpers in
api.rs, and resolve the ledger withEngine::resolveorEngine::resolve_state. Return only fields from validated ledgers in the served range. - Build object and transaction JSON with
xrpl_json(xrpl_json::ledger_entry,xrpl_json::transaction,xrpl_json::metadata), not by hand. - Return errors with the names, codes and messages Clio uses for the same case.
- Call
limits::check_rpc()inside every loop whose length depends on the request. - Add an arm to the
matchinEngine::handle. - In
server.rs, check whether the method belongs inFIXED_BY_LEDGER(its answer for a ledger named by sequence or hash never changes, so HTTP may sendimmutable). Inguard.rs, give it a weight inweightif it costs more than an ordinary read. - If Clio passes the method to xrpld instead, don’t write a handler: add it to
FORWARDEDorCURRENT_ON_NODEinforward.rs, and extend the testforwarding_follows_clio_rule.
Tests come in three kinds, by what they exercise:
- Pure parsing and formatting: a unit test in the same file, in
#[cfg(test)] mod tests. For examplekeys_match_xrpldinselector.rschecks keys against what xrpld 3.4.1 returns for the same params. - A handler over real stores: an integration test in
crates/ledger-engine/tests/.tests/old_ledger_state.rsbuildsEnginevalues over temporary directories (tempfile), writes a snapshot and history from a fixture of public ledger data, and callsEngine::handledirectly;four_methods_match_direct_statecompares the answers of two servers that hold the same data differently. Integration test files start with#![allow(clippy::unwrap_used, clippy::expect_used)]. - The request path:
server.rshas tests that build the router withapps, listen on a local port and send JSON-RPC, for examplesubmissions_stay_off_the_public_door_and_off_by_default.
Answers that must match Clio are also compared with a live Clio. That belongs in scripts/, not in cargo test; scripts/astraea-clio-check.py sends the same requests to both servers and compares the whole answers; if you add a method or a parameter, add requests for it there. Say in the pull request what you ran and what came out.
Before the pull request, run the checks CI runs: cargo fmt --all --check, cargo clippy --workspace --all-targets --locked -- -D warnings, cargo test --workspace --locked and cargo deny check. No unwrap or expect outside tests. Add the method to docs/api.md, and a line under ## Unreleased in CHANGELOG.md (add the heading if it is missing).