ethportal_api/
state.rs

1use discv5::enr::NodeId;
2use jsonrpsee::{core::RpcResult, proc_macros::rpc};
3use serde_json::Value;
4
5use crate::{
6    types::{
7        content_key::state::StateContentKey,
8        enr::Enr,
9        ping_extensions::extension_types::PingExtensionType,
10        portal::{
11            AcceptInfo, DataRadius, FindContentInfo, FindNodesInfo, GetContentInfo,
12            PaginateLocalContentInfo, PongInfo, PutContentInfo, TraceContentInfo,
13            TracePutContentInfo,
14        },
15        portal_wire::OfferTrace,
16    },
17    RawContentValue, RoutingTableInfo,
18};
19
20/// Portal State JSON-RPC endpoints
21#[rpc(client, server, namespace = "portal")]
22pub trait StateNetworkApi {
23    /// Returns meta information about overlay routing table.
24    #[method(name = "stateRoutingTableInfo")]
25    async fn routing_table_info(&self) -> RpcResult<RoutingTableInfo>;
26
27    /// Returns the node data radios
28    #[method(name = "stateRadius")]
29    async fn radius(&self) -> RpcResult<DataRadius>;
30
31    /// Write an Ethereum Node Record to the overlay routing table.
32    #[method(name = "stateAddEnr")]
33    async fn add_enr(&self, enr: Enr) -> RpcResult<bool>;
34
35    /// Fetch the latest ENR associated with the given node ID.
36    #[method(name = "stateGetEnr")]
37    async fn get_enr(&self, node_id: NodeId) -> RpcResult<Enr>;
38
39    /// Delete Node ID from the overlay routing table.
40    #[method(name = "stateDeleteEnr")]
41    async fn delete_enr(&self, node_id: NodeId) -> RpcResult<bool>;
42
43    /// Fetch the ENR representation associated with the given Node ID.
44    #[method(name = "stateLookupEnr")]
45    async fn lookup_enr(&self, node_id: NodeId) -> RpcResult<Enr>;
46
47    /// Send a PING message to the designated node and wait for a PONG response
48    #[method(name = "statePing")]
49    async fn ping(
50        &self,
51        enr: Enr,
52        payload_type: Option<PingExtensionType>,
53        payload: Option<Value>,
54    ) -> RpcResult<PongInfo>;
55
56    /// Send a FINDNODES request for nodes that fall within the given set of distances, to the
57    /// designated peer and wait for a response
58    #[method(name = "stateFindNodes")]
59    async fn find_nodes(&self, enr: Enr, distances: Vec<u16>) -> RpcResult<FindNodesInfo>;
60
61    /// Lookup a target node within in the network
62    #[method(name = "stateRecursiveFindNodes")]
63    async fn recursive_find_nodes(&self, node_id: NodeId) -> RpcResult<Vec<Enr>>;
64
65    /// Send FINDCONTENT message to get the content with a content key.
66    #[method(name = "stateFindContent")]
67    async fn find_content(
68        &self,
69        enr: Enr,
70        content_key: StateContentKey,
71    ) -> RpcResult<FindContentInfo>;
72
73    /// First checks local storage if content is not found lookup a target content key in the
74    /// network
75    #[method(name = "stateGetContent")]
76    async fn get_content(&self, content_key: StateContentKey) -> RpcResult<GetContentInfo>;
77
78    /// First checks local storage if content is not found lookup a target content key in the
79    /// network. Return tracing info.
80    #[method(name = "stateTraceGetContent")]
81    async fn trace_get_content(&self, content_key: StateContentKey) -> RpcResult<TraceContentInfo>;
82
83    /// Pagination of local content keys
84    #[method(name = "statePaginateLocalContentKeys")]
85    async fn paginate_local_content_keys(
86        &self,
87        offset: u64,
88        limit: u64,
89    ) -> RpcResult<PaginateLocalContentInfo<StateContentKey>>;
90
91    /// Send the provided content value to interested peers. Clients may choose to send to some or
92    /// all peers. Return the number of peers that the content was gossiped to.
93    #[method(name = "statePutContent")]
94    async fn put_content(
95        &self,
96        content_key: StateContentKey,
97        content_value: RawContentValue,
98    ) -> RpcResult<PutContentInfo>;
99
100    /// Send the provided content value to interested peers. Clients may choose to send to some or
101    /// all peers. Return tracing info detailing the gossip propagation.
102    #[method(name = "stateTracePutContent")]
103    async fn trace_put_content(
104        &self,
105        content_key: StateContentKey,
106        content_value: RawContentValue,
107    ) -> RpcResult<TracePutContentInfo>;
108
109    /// Send an OFFER request with given ContentItems, to the designated peer and wait for a
110    /// response. Does not store the content locally.
111    /// Returns the content keys bitlist upon successful content transmission or empty bitlist
112    /// receive.
113    #[method(name = "stateOffer")]
114    async fn offer(
115        &self,
116        enr: Enr,
117        content_items: Vec<(StateContentKey, RawContentValue)>,
118    ) -> RpcResult<AcceptInfo>;
119
120    /// Send an OFFER request with given ContentItems, to the designated peer.
121    /// Does not store the content locally.
122    /// Returns trace info for offer.
123    #[method(name = "stateTraceOffer")]
124    async fn trace_offer(
125        &self,
126        enr: Enr,
127        content_key: StateContentKey,
128        content_value: RawContentValue,
129    ) -> RpcResult<OfferTrace>;
130
131    /// Store content key with a content data to the local database.
132    #[method(name = "stateStore")]
133    async fn store(
134        &self,
135        content_key: StateContentKey,
136        content_value: RawContentValue,
137    ) -> RpcResult<bool>;
138
139    /// Get a content from the local database
140    #[method(name = "stateLocalContent")]
141    async fn local_content(&self, content_key: StateContentKey) -> RpcResult<RawContentValue>;
142}