Expand description
§RPC Service Layer
This module provides the service layer that connects HTTP endpoints to the node’s state machine. It handles the communication between external RPC requests and the internal event-driven architecture.
§Architecture
The RPC system follows a request-response pattern through channels:
HTTP Server -> RpcSender -> RpcService -> Event -> State Machine
|
HTTP Response <- oneshot channel <- respond_* methods <-+- HTTP server receives a request and sends it via
RpcSender RpcServicewraps the request with a unique ID and responder channel- Request becomes an [
Event::Rpc] dispatched to the state machine - State machine processes the request and calls a
respond_*method - Response is sent back through the oneshot channel to the HTTP server
§Adding a new RPC endpoint
To add a new RPC endpoint, follow these steps:
-
Define the request type in [
node::rpc]: Add a variant toRpcRequestenum innode/src/rpc/mod.rs -
Define the response type in [
node::rpc]: Create a type alias likepub type RpcMyNewResponse = ...; -
Add the respond method to
RpcServicetrait innode/src/rpc_effectful/mod.rs:ⓘfn respond_my_new_endpoint( &mut self, rpc_id: RpcId, response: RpcMyNewResponse, ) -> Result<(), RespondError>; -
Implement the respond method in this file using the macro:
ⓘrpc_service_impl!(respond_my_new_endpoint, RpcMyNewResponse); -
Handle the request in
node/src/rpc/rpc_reducer.rs: Add a match arm for your newRpcRequestvariant -
Add the HTTP route in
node/native/src/http_server.rs: Define the route and callrpc_sender.my_new_endpoint() -
Add the sender method in
sender.rs: Implement the method onRpcSenderthat sends the request through the channel -
Document the endpoint in the website documentation: Add the new endpoint to
website/docs/developers/api-and-data/rpc-api.mdso node operators can discover and use it
Modules§
Structs§
Enums§
- Action
Stats Response - Respond
Error - RpcP2p
Connection Incoming Response - RpcSnarker
JobCommit Response - RpcSnarker
JobSpec Response