jam_std_common/
rpc.rs

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