referrerpolicy=no-referrer-when-downgrade

polkadot_primitives/
runtime_api.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Polkadot.
3
4// Polkadot is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Polkadot is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Polkadot.  If not, see <http://www.gnu.org/licenses/>.
16
17//! Runtime API module declares the `trait ParachainHost` which is part
18//! of the Runtime API exposed from the Runtime to the Host.
19//!
20//! The functions in trait ParachainHost` can be part of the stable API
21//! (which is versioned) or they can be staging (aka unstable/testing
22//! functions).
23//!
24//! The separation outlined above is achieved with the versioned API feature
25//! of `decl_runtime_apis!` and `impl_runtime_apis!`. Before moving on let's
26//! see a quick example about how API versioning works.
27//!
28//! # Runtime API versioning crash course
29//!
30//! The versioning is achieved with the `api_version` attribute. It can be
31//! placed on:
32//! * trait declaration - represents the base version of the API.
33//! * method declaration (inside a trait declaration) - represents a versioned method, which is not
34//!   available in the base version.
35//! * trait implementation - represents which version of the API is being implemented.
36//!
37//! Let's see a quick example:
38//!
39//! ```nocompile
40//! sp_api::decl_runtime_apis! {
41//! 	#[api_version(2)]
42//! 	pub trait MyApi {
43//! 		fn fn1();
44//! 		fn fn2();
45//! 		#[api_version(3)]
46//! 		fn fn3();
47//! 		#[api_version(4)]
48//! 		fn fn4();
49//! 	}
50//! }
51//!
52//! struct Runtime {}
53//!
54//! sp_api::impl_runtime_apis! {
55//!     #[api_version(3)]
56//!     impl self::MyApi<Block> for Runtime {
57//!         fn fn1() {}
58//!         fn fn2() {}
59//!         fn fn3() {}
60//!     }
61//! }
62//! ```
63//! A new API named `MyApi` is declared with `decl_runtime_apis!`. The trait declaration
64//! has got an `api_version` attribute which represents its base version - 2 in this case.
65//!
66//! The API has got three methods - `fn1`, `fn2`, `fn3` and `fn4`. `fn3` and `fn4` has got
67//! an `api_version` attribute which makes them versioned methods. These methods do not exist
68//! in the base version of the API. Behind the scenes the declaration above creates three
69//! runtime APIs:
70//! * `MyApiV2` with `fn1` and `fn2`
71//! * `MyApiV3` with `fn1`, `fn2` and `fn3`.
72//! * `MyApiV4` with `fn1`, `fn2`, `fn3` and `fn4`.
73//!
74//! Please note that `v4` contains all methods from `v3`, `v3` all methods from `v2` and so on.
75//!
76//! Back to our example. At the end runtime API is implemented for `struct Runtime` with
77//! `impl_runtime_apis` macro. `api_version` attribute is attached to the `impl` block which
78//! means that a version different from the base one is being implemented - in our case this
79//! is `v3`.
80//!
81//! This version of the API contains three methods so the `impl` block has got definitions
82//! for them. Note that `fn4` is not implemented as it is not part of this version of the API.
83//! `impl_runtime_apis` generates a default implementation for it calling `unimplemented!()`.
84//!
85//! Hopefully this should be all you need to know in order to use versioned methods in the node.
86//! For more details about how the API versioning works refer to `spi_api`
87//! documentation [here](https://docs.substrate.io/rustdocs/latest/sp_api/macro.decl_runtime_apis.html).
88//!
89//! # How versioned methods are used for `ParachainHost`
90//!
91//! Let's introduce two types of `ParachainHost` API implementation:
92//! * stable - used on stable production networks like Polkadot and Kusama. There is only one stable
93//!   API at a single point in time.
94//! * staging - methods that are ready for production, but will be released on Rococo first. We can
95//!   batch together multiple changes and then release all of them to production, by making staging
96//!   production (bump base version). We can not change or remove any method in staging after a
97//!   release, as this would break Rococo. It should be ok to keep adding methods to staging across
98//!   several releases. For experimental methods, you have to keep them on a separate branch until
99//!   ready.
100//!
101//! The stable version of `ParachainHost` is indicated by the base version of the API. Any staging
102//! method must use `api_version` attribute so that it is assigned to a specific version of a
103//! staging API. This way in a single declaration one can see what's the stable version of
104//! `ParachainHost` and what staging versions/functions are available.
105//!
106//! All stable API functions should use primitives from the latest version.
107//! In the time of writing of this document - this is `v2`. So for example:
108//! ```ignore
109//! fn validators() -> Vec<v2::ValidatorId>;
110//! ```
111//! indicates a function from the stable `v2` API.
112//!
113//! All staging API functions should use primitives from `vstaging`. They should be clearly
114//! separated from the stable primitives.
115
116use crate::{
117	async_backing::{BackingState, Constraints},
118	slashing, ApprovalVotingParams, AsyncBackingParams, BlockNumber, CandidateCommitments,
119	CandidateEvent, CandidateHash, CommittedCandidateReceiptV2 as CommittedCandidateReceipt,
120	CoreIndex, CoreState, DisputeState, ExecutorParams, GroupRotationInfo, Hash, NodeFeatures,
121	OccupiedCoreAssumption, PersistedValidationData, PvfCheckStatement, ScrapedOnChainVotes,
122	SessionIndex, SessionInfo, ValidatorId, ValidatorIndex, ValidatorSignature,
123};
124
125use alloc::{
126	collections::{btree_map::BTreeMap, vec_deque::VecDeque},
127	vec::Vec,
128};
129use polkadot_core_primitives as pcp;
130use polkadot_parachain_primitives::primitives as ppp;
131
132sp_api::decl_runtime_apis! {
133	/// The API for querying the state of parachains on-chain.
134	#[api_version(5)]
135	pub trait ParachainHost {
136		/// Get the current validators.
137		fn validators() -> Vec<ValidatorId>;
138
139		/// Returns the validator groups and rotation info localized based on the hypothetical child
140		///  of a block whose state  this is invoked on. Note that `now` in the `GroupRotationInfo`
141		/// should be the successor of the number of the block.
142		fn validator_groups() -> (Vec<Vec<ValidatorIndex>>, GroupRotationInfo<BlockNumber>);
143
144		/// Yields information on all availability cores as relevant to the child block.
145		/// Cores are either free or occupied. Free cores can have paras assigned to them.
146		fn availability_cores() -> Vec<CoreState<Hash, BlockNumber>>;
147
148		/// Yields the persisted validation data for the given `ParaId` along with an assumption that
149		/// should be used if the para currently occupies a core.
150		///
151		/// Returns `None` if either the para is not registered or the assumption is `Freed`
152		/// and the para already occupies a core.
153		fn persisted_validation_data(para_id: ppp::Id, assumption: OccupiedCoreAssumption)
154			-> Option<PersistedValidationData<Hash, BlockNumber>>;
155
156		/// Returns the persisted validation data for the given `ParaId` along with the corresponding
157		/// validation code hash. Instead of accepting assumption about the para, matches the validation
158		/// data hash against an expected one and yields `None` if they're not equal.
159		fn assumed_validation_data(
160			para_id: ppp::Id,
161			expected_persisted_validation_data_hash: Hash,
162		) -> Option<(PersistedValidationData<Hash, BlockNumber>, ppp::ValidationCodeHash)>;
163
164		/// Checks if the given validation outputs pass the acceptance criteria.
165		fn check_validation_outputs(para_id: ppp::Id, outputs: CandidateCommitments) -> bool;
166
167		/// Returns the session index expected at a child of the block.
168		///
169		/// This can be used to instantiate a `SigningContext`.
170		fn session_index_for_child() -> SessionIndex;
171
172		/// Fetch the validation code used by a para, making the given `OccupiedCoreAssumption`.
173		///
174		/// Returns `None` if either the para is not registered or the assumption is `Freed`
175		/// and the para already occupies a core.
176		fn validation_code(
177			para_id: ppp::Id,
178			assumption: OccupiedCoreAssumption,
179		) -> Option<ppp::ValidationCode>;
180
181		/// Get the receipt of a candidate pending availability. This returns `Some` for any paras
182		/// assigned to occupied cores in `availability_cores` and `None` otherwise.
183		fn candidate_pending_availability(para_id: ppp::Id) -> Option<CommittedCandidateReceipt<Hash>>;
184
185		/// Get a vector of events concerning candidates that occurred within a block.
186		fn candidate_events() -> Vec<CandidateEvent<Hash>>;
187
188		/// Get all the pending inbound messages in the downward message queue for a para.
189		fn dmq_contents(
190			recipient: ppp::Id,
191		) -> Vec<pcp::v2::InboundDownwardMessage<BlockNumber>>;
192
193		/// Get the contents of all channels addressed to the given recipient. Channels that have no
194		/// messages in them are also included.
195		fn inbound_hrmp_channels_contents(
196			recipient: ppp::Id,
197		) -> BTreeMap<ppp::Id, Vec<pcp::v2::InboundHrmpMessage<BlockNumber>>>;
198
199		/// Get the validation code from its hash.
200		fn validation_code_by_hash(hash: ppp::ValidationCodeHash) -> Option<ppp::ValidationCode>;
201
202		/// Scrape dispute relevant from on-chain, backing votes and resolved disputes.
203		fn on_chain_votes() -> Option<ScrapedOnChainVotes<Hash>>;
204
205		/***** Added in v2 *****/
206
207		/// Get the session info for the given session, if stored.
208		///
209		/// NOTE: This function is only available since parachain host version 2.
210		fn session_info(index: SessionIndex) -> Option<SessionInfo>;
211
212		/// Submits a PVF pre-checking statement into the transaction pool.
213		///
214		/// NOTE: This function is only available since parachain host version 2.
215		fn submit_pvf_check_statement(stmt: PvfCheckStatement, signature: ValidatorSignature);
216
217		/// Returns code hashes of PVFs that require pre-checking by validators in the active set.
218		///
219		/// NOTE: This function is only available since parachain host version 2.
220		fn pvfs_require_precheck() -> Vec<ppp::ValidationCodeHash>;
221
222		/// Fetch the hash of the validation code used by a para, making the given `OccupiedCoreAssumption`.
223		///
224		/// NOTE: This function is only available since parachain host version 2.
225		fn validation_code_hash(para_id: ppp::Id, assumption: OccupiedCoreAssumption)
226			-> Option<ppp::ValidationCodeHash>;
227
228		/// Returns all onchain disputes.
229		fn disputes() -> Vec<(SessionIndex, CandidateHash, DisputeState<BlockNumber>)>;
230
231		/// Returns execution parameters for the session.
232		fn session_executor_params(session_index: SessionIndex) -> Option<ExecutorParams>;
233
234		/// Returns a list of validators that lost a past session dispute and need to be slashed.
235		/// NOTE: This function is only available since parachain host version 5.
236		fn unapplied_slashes() -> Vec<(SessionIndex, CandidateHash, slashing::PendingSlashes)>;
237
238		/// Returns a merkle proof of a validator session key.
239		/// NOTE: This function is only available since parachain host version 5.
240		fn key_ownership_proof(
241			validator_id: ValidatorId,
242		) -> Option<slashing::OpaqueKeyOwnershipProof>;
243
244		/// Submit an unsigned extrinsic to slash validators who lost a dispute about
245		/// a candidate of a past session.
246		/// NOTE: This function is only available since parachain host version 5.
247		fn submit_report_dispute_lost(
248			dispute_proof: slashing::DisputeProof,
249			key_ownership_proof: slashing::OpaqueKeyOwnershipProof,
250		) -> Option<()>;
251
252		/***** Added in v6 *****/
253
254		/// Get the minimum number of backing votes for a parachain candidate.
255		/// This is a staging method! Do not use on production runtimes!
256		#[api_version(6)]
257		fn minimum_backing_votes() -> u32;
258
259
260		/***** Added in v7: Asynchronous backing *****/
261
262		/// Returns the state of parachain backing for a given para.
263		#[api_version(7)]
264		fn para_backing_state(_: ppp::Id) -> Option<BackingState<Hash, BlockNumber>>;
265
266		/// Returns candidate's acceptance limitations for asynchronous backing for a relay parent.
267		#[api_version(7)]
268		fn async_backing_params() -> AsyncBackingParams;
269
270		/***** Added in v8 *****/
271
272		/// Returns a list of all disabled validators at the given block.
273		#[api_version(8)]
274		fn disabled_validators() -> Vec<ValidatorIndex>;
275
276		/***** Added in v9 *****/
277
278		/// Get node features.
279		/// This is a staging method! Do not use on production runtimes!
280		#[api_version(9)]
281		fn node_features() -> NodeFeatures;
282
283		/***** Added in v10 *****/
284		/// Approval voting configuration parameters
285		#[api_version(10)]
286		fn approval_voting_params() -> ApprovalVotingParams;
287
288		/***** Added in v11 *****/
289		/// Claim queue
290		#[api_version(11)]
291		fn claim_queue() -> BTreeMap<CoreIndex, VecDeque<ppp::Id>>;
292
293		/***** Added in v11 *****/
294		/// Elastic scaling support
295		#[api_version(11)]
296		fn candidates_pending_availability(para_id: ppp::Id) -> Vec<CommittedCandidateReceipt<Hash>>;
297
298		/***** Added in v12 *****/
299		/// Retrieve the maximum uncompressed code size.
300		#[api_version(12)]
301		fn validation_code_bomb_limit() -> u32;
302
303		/***** Added in v13 *****/
304		/// Returns the constraints on the actions that can be taken by a new parachain
305		/// block.
306		#[api_version(13)]
307		fn backing_constraints(para_id: ppp::Id) -> Option<Constraints>;
308
309		/***** Added in v13 *****/
310		/// Retrieve the scheduling lookahead
311		#[api_version(13)]
312		fn scheduling_lookahead() -> u32;
313
314		/***** Added in v14 *****/
315		/// Retrieve paraids at relay parent
316		#[api_version(14)]
317		fn para_ids() -> Vec<ppp::Id>;
318
319	}
320}