jam_std_common/rpc.rs
1use jam_types::{CoreIndex, Hash, ServiceId, Slot};
2use jsonrpsee::{
3 core::{RpcResult, SubscriptionResult},
4 proc_macros::rpc,
5};
6
7/// An update from a state subscription. `header_hash` and `slot` are of the block whose posterior
8/// state contains `value`.
9#[derive(serde::Serialize, serde::Deserialize, Debug)]
10pub struct StateUpdate<T> {
11 pub header_hash: Hash,
12 pub slot: u32,
13 pub value: T,
14}
15
16#[derive(serde::Serialize, serde::Deserialize, Clone, Debug)]
17pub enum VersionedParameters {
18 /// Parameters of the chain, version 1.
19 V1(crate::Parameters),
20}
21
22#[rpc(client, server)]
23pub trait Rpc {
24 /// Parameters of the chain, version 1.
25 #[method(name = "parameters")]
26 fn parameters(&self) -> RpcResult<VersionedParameters>;
27
28 /// Returns the header hash and slot of the head of the "best" chain.
29 #[method(name = "bestBlock")]
30 fn best_block(&self) -> RpcResult<([u8; 32], u32)>;
31
32 /// Subscribe to updates of the head of the "best" chain, as returned by `bestBlock`.
33 #[subscription(name = "subscribeBestBlock", item = ([u8; 32], u32))]
34 async fn subscribe_best_block(&self) -> SubscriptionResult;
35
36 /// Returns the header hash and slot of the latest finalized block.
37 #[method(name = "finalizedBlock")]
38 fn finalized_block(&self) -> RpcResult<([u8; 32], u32)>;
39
40 /// Subscribe to updates of the latest finalized block, as returned by `finalizedBlock`.
41 #[subscription(name = "subscribeFinalizedBlock", item = ([u8; 32], u32))]
42 async fn subscribe_finalized_block(&self) -> SubscriptionResult;
43
44 /// Returns the header hash and slot of the parent of the block with the given header hash, or
45 /// `None` if this is not known.
46 #[method(name = "parent")]
47 fn parent(&self, header_hash: Hash) -> RpcResult<Option<([u8; 32], u32)>>;
48
49 /// Returns the posterior state root of the block with the given header hash, or `None` if this
50 /// is not known.
51 #[method(name = "stateRoot")]
52 fn state_root(&self, header_hash: Hash) -> RpcResult<Option<[u8; 32]>>;
53
54 /// Returns the activity statistics stored in the posterior state of the block with the given
55 /// header hash.
56 ///
57 /// The statistics are encoded as per the GP. `None` is returned if the block's posterior state
58 /// is not known.
59 #[method(name = "statistics")]
60 fn statistics(&self, header_hash: Hash) -> RpcResult<Vec<u8>>;
61
62 /// Subscribe to updates of the activity statistics stored in chain state.
63 ///
64 /// If `finalized` is true, the subscription will track the latest finalized block. If
65 /// `finalized` is false, the subscription will track the head of the "best" chain. Note that
66 /// in the latter case the reported statistics may never be included in the finalized chain.
67 ///
68 /// The statistics are encoded as per the GP.
69 #[subscription(name = "subscribeStatistics", item = StateUpdate<Vec<u8>>)]
70 async fn subscribe_statistics(&self, finalized: bool) -> SubscriptionResult;
71
72 /// Returns the storage data for the service of the given service ID.
73 ///
74 /// The data are encoded as per the GP. `None` is returned if there is no value associated with
75 /// the given service ID.
76 #[method(name = "serviceData")]
77 fn service_data(&self, header_hash: Hash, id: u32) -> RpcResult<Option<Vec<u8>>>;
78
79 /// Subscribe to updates of the storage data for the service of the given service ID.
80 ///
81 /// If `finalized` is true, the subscription will track the latest finalized block. If
82 /// `finalized` is false, the subscription will track the head of the "best" chain. Note that
83 /// in the latter case the reported service info may never be included in the finalized chain.
84 ///
85 /// The data are encoded as per the GP.
86 #[subscription(name = "subscribeServiceData", item = StateUpdate<Vec<u8>>)]
87 async fn subscribe_service_data(&self, id: ServiceId, finalized: bool) -> SubscriptionResult;
88
89 /// Returns the value associated with the given service ID and key in the posterior state of
90 /// the block with the given header hash.
91 ///
92 /// `None` is returned if there is no value associated with the given service ID and key.
93 #[method(name = "serviceValue")]
94 fn service_value(
95 &self,
96 header_hash: Hash,
97 id: ServiceId,
98 key: Vec<u8>,
99 ) -> RpcResult<Option<Vec<u8>>>;
100
101 /// Subscribe to updates of the value associated with the given service ID and key.
102 ///
103 /// If `finalized` is true, the subscription will track the latest finalized block. If
104 /// `finalized` is false, the subscription will track the head of the "best" chain. Note that
105 /// in the latter case reported value changes may never be included in the finalized chain.
106 ///
107 /// The `value` field of subscription messages will be `None` when there is no value associated
108 /// with the given service ID and key.
109 #[subscription(name = "subscribeServiceValue", item = StateUpdate<Option<Vec<u8>>>)]
110 async fn subscribe_service_value(
111 &self,
112 id: ServiceId,
113 key: Vec<u8>,
114 finalized: bool,
115 ) -> SubscriptionResult;
116
117 /// Returns the preimage associated with the given service ID and hash in the posterior state of
118 /// the block with the given header hash.
119 ///
120 /// `None` is returned if there is no preimage associated with the given service ID and
121 /// hash.
122 #[method(name = "servicePreimage")]
123 fn service_preimage(
124 &self,
125 header_hash: Hash,
126 id: ServiceId,
127 hash: Hash,
128 ) -> RpcResult<Option<Vec<u8>>>;
129
130 /// Subscribe to updates of the preimage associated with the given service ID and hash.
131 ///
132 /// If `finalized` is true, the subscription will track the latest finalized block. If
133 /// `finalized` is false, the subscription will track the head of the "best" chain. Note that
134 /// in the latter case reported preimage changes may never be included in the finalized chain.
135 ///
136 /// The `preimage` field of subscription messages will be `None` when there is no preimage
137 /// associated with the given service ID and hash.
138 #[subscription(name = "subscribeServicePreimage", item = StateUpdate<Option<Vec<u8>>>)]
139 async fn subscribe_service_preimage(
140 &self,
141 id: ServiceId,
142 hash: Hash,
143 finalized: bool,
144 ) -> SubscriptionResult;
145
146 /// Returns the preimage request associated with the given service ID and hash/len in the
147 /// posterior state of the block with the given header hash.
148 ///
149 /// `None` is returned if there is no preimage request associated with the given service ID,
150 /// hash and length.
151 #[method(name = "serviceRequest")]
152 fn service_request(
153 &self,
154 header_hash: Hash,
155 id: ServiceId,
156 hash: Hash,
157 len: u32,
158 ) -> RpcResult<Option<Vec<Slot>>>;
159
160 /// Subscribe to updates of the preimage associated with the given service ID and hash.
161 ///
162 /// If `finalized` is true, the subscription will track the latest finalized block. If
163 /// `finalized` is false, the subscription will track the head of the "best" chain. Note that
164 /// in the latter case reported preimage changes may never be included in the finalized chain.
165 ///
166 /// The `request` field of subscription messages will be `None` when there is no preimage
167 /// request associated with the given service ID, hash and length.
168 #[subscription(name = "subscribeServiceRequest", item = StateUpdate<Option<Vec<Slot>>>)]
169 async fn subscribe_service_request(
170 &self,
171 id: ServiceId,
172 hash: Hash,
173 len: u32,
174 finalized: bool,
175 ) -> SubscriptionResult;
176
177 /// Returns the BEEFY root of the block with the given header hash, or `None` if this is not
178 /// known.
179 #[method(name = "beefyRoot")]
180 fn beefy_root(&self, header_hash: Hash) -> RpcResult<Option<[u8; 32]>>;
181
182 /// Submit a work-package to the guarantors currently assigned to the given core.
183 #[method(name = "submitWorkPackage")]
184 async fn submit_work_package(
185 &self,
186 core: CoreIndex,
187 package: Vec<u8>,
188 extrinsics: Vec<Vec<u8>>,
189 ) -> RpcResult<()>;
190
191 /// Submit a preimage which is being requested by a given service.
192 #[method(name = "submitPreimage")]
193 async fn submit_preimage(
194 &self,
195 service: ServiceId,
196 preimage: Vec<u8>,
197 require_block: Hash,
198 ) -> RpcResult<()>;
199
200 /// Returns a list of all services currently known to be on JAM. This is a best-effort list and
201 /// may not reflect the true state. Nodes could e.g. reasonably hide services which are not
202 /// recently active from this list.
203 #[method(name = "listServices")]
204 fn list_services(&self, header_hash: Hash) -> RpcResult<Vec<ServiceId>>;
205}