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