Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit ed0d05e

Browse files
committed
Upgrade check_validation_outputs
1 parent cf8fd9b commit ed0d05e

File tree

9 files changed

+149
-17
lines changed

9 files changed

+149
-17
lines changed

primitives/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#![warn(missing_docs)]
2020
#![cfg_attr(not(feature = "std"), no_std)]
2121

22+
pub mod staging;
2223
pub mod v0;
2324
pub mod v1;
2425
pub mod v2;

primitives/src/staging/mod.rs

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
// Copyright 2022 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+
//! Staging primitives used to accumulate changes before going live.
18+
19+
use crate::{v1, v2};
20+
21+
use parity_scale_codec::{Decode, Encode};
22+
use sp_std::{collections::btree_map::BTreeMap, prelude::*};
23+
24+
sp_api::decl_runtime_apis! {
25+
/// The API for querying the state of parachains on-chain.
26+
#[api_version(4294967295)] // Macro doesn't allow specifying `u32::MAX`.
27+
pub trait ParachainHost<H: Encode + Decode = v1::Hash, N: Encode + Decode = v1::BlockNumber> {
28+
/// Get the current validators.
29+
fn validators() -> Vec<v1::ValidatorId>;
30+
31+
/// Returns the validator groups and rotation info localized based on the hypothetical child
32+
/// of a block whose state this is invoked on. Note that `now` in the `GroupRotationInfo`
33+
/// should be the successor of the number of the block.
34+
fn validator_groups() -> (Vec<Vec<v1::ValidatorIndex>>, v1::GroupRotationInfo<N>);
35+
36+
/// Yields information on all availability cores as relevant to the child block.
37+
/// Cores are either free or occupied. Free cores can have paras assigned to them.
38+
fn availability_cores() -> Vec<v1::CoreState<H, N>>;
39+
40+
/// Yields the persisted validation data for the given `ParaId` along with an assumption that
41+
/// should be used if the para currently occupies a core.
42+
///
43+
/// Returns `None` if either the para is not registered or the assumption is `Freed`
44+
/// and the para already occupies a core.
45+
fn persisted_validation_data(para_id: v1::Id, assumption: v1::OccupiedCoreAssumption)
46+
-> Option<v1::PersistedValidationData<H, N>>;
47+
48+
/// Returns the persisted validation data for the given `ParaId` along with the corresponding
49+
/// validation code hash. Instead of accepting assumption about the para, matches the validation
50+
/// data hash against an expected one and yields `None` if they're not equal.
51+
fn assumed_validation_data(
52+
para_id: v1::Id,
53+
expected_persisted_validation_data_hash: v1::Hash,
54+
) -> Option<(v1::PersistedValidationData<H, N>, v1::ValidationCodeHash)>;
55+
56+
/// Checks if the given validation outputs pass the acceptance criteria.
57+
#[changed_in(4294967295)]
58+
fn check_validation_outputs(para_id: v1::Id, outputs: v1::CandidateCommitments) -> bool;
59+
60+
/// Returns the session index expected at a child of the block.
61+
///
62+
/// This can be used to instantiate a `SigningContext`.
63+
fn session_index_for_child() -> v1::SessionIndex;
64+
65+
/// Fetch the validation code used by a para, making the given `OccupiedCoreAssumption`.
66+
///
67+
/// Returns `None` if either the para is not registered or the assumption is `Freed`
68+
/// and the para already occupies a core.
69+
fn validation_code(para_id: v1::Id, assumption: v1::OccupiedCoreAssumption)
70+
-> Option<v1::ValidationCode>;
71+
72+
/// Get the receipt of a candidate pending availability. This returns `Some` for any paras
73+
/// assigned to occupied cores in `availability_cores` and `None` otherwise.
74+
fn candidate_pending_availability(para_id: v1::Id) -> Option<v1::CommittedCandidateReceipt<H>>;
75+
76+
/// Get a vector of events concerning candidates that occurred within a block.
77+
fn candidate_events() -> Vec<v1::CandidateEvent<H>>;
78+
79+
/// Get all the pending inbound messages in the downward message queue for a para.
80+
fn dmq_contents(
81+
recipient: v1::Id,
82+
) -> Vec<v1::InboundDownwardMessage<N>>;
83+
84+
/// Get the contents of all channels addressed to the given recipient. Channels that have no
85+
/// messages in them are also included.
86+
fn inbound_hrmp_channels_contents(recipient: v1::Id) -> BTreeMap<v1::Id, Vec<v1::InboundHrmpMessage<N>>>;
87+
88+
/// Get the validation code from its hash.
89+
fn validation_code_by_hash(hash: v1::ValidationCodeHash) -> Option<v1::ValidationCode>;
90+
91+
/// Scrape dispute relevant from on-chain, backing votes and resolved disputes.
92+
fn on_chain_votes() -> Option<v1::ScrapedOnChainVotes<H>>;
93+
94+
/***** Added in v2 *****/
95+
96+
/// Get the session info for the given session, if stored.
97+
///
98+
/// NOTE: This function is only available since parachain host version 2.
99+
fn session_info(index: v1::SessionIndex) -> Option<v2::SessionInfo>;
100+
101+
/// Submits a PVF pre-checking statement into the transaction pool.
102+
///
103+
/// NOTE: This function is only available since parachain host version 2.
104+
fn submit_pvf_check_statement(stmt: v2::PvfCheckStatement, signature: v1::ValidatorSignature);
105+
106+
/// Returns code hashes of PVFs that require pre-checking by validators in the active set.
107+
///
108+
/// NOTE: This function is only available since parachain host version 2.
109+
fn pvfs_require_precheck() -> Vec<v1::ValidationCodeHash>;
110+
111+
/// Fetch the hash of the validation code used by a para, making the given `OccupiedCoreAssumption`.
112+
///
113+
/// NOTE: This function is only available since parachain host version 2.
114+
fn validation_code_hash(para_id: v1::Id, assumption: v1::OccupiedCoreAssumption)
115+
-> Option<v1::ValidationCodeHash>;
116+
117+
/***** Staging *****/
118+
119+
/// Checks if the given validation outputs pass the acceptance criteria.
120+
fn check_validation_outputs(
121+
para_id: v1::Id,
122+
relay_parent_number: v1::BlockNumber,
123+
outputs: v1::CandidateCommitments,
124+
) -> bool;
125+
}
126+
}

runtime/kusama/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3058,7 +3058,7 @@ sp_api::impl_runtime_apis! {
30583058
}
30593059
}
30603060

3061-
impl primitives::v2::ParachainHost<Block, Hash, BlockNumber> for Runtime {
3061+
impl primitives::staging::ParachainHost<Block, Hash, BlockNumber> for Runtime {
30623062
fn validators() -> Vec<ValidatorId> {
30633063
parachains_runtime_api_impl::validators::<Runtime>()
30643064
}
@@ -3088,9 +3088,10 @@ sp_api::impl_runtime_apis! {
30883088

30893089
fn check_validation_outputs(
30903090
para_id: ParaId,
3091+
relay_parent_number: BlockNumber,
30913092
outputs: primitives::v1::CandidateCommitments,
30923093
) -> bool {
3093-
parachains_runtime_api_impl::check_validation_outputs::<Runtime>(para_id, outputs)
3094+
parachains_runtime_api_impl::check_validation_outputs::<Runtime>(para_id, relay_parent_number, outputs)
30943095
}
30953096

30963097
fn session_index_for_child() -> SessionIndex {

runtime/parachains/src/inclusion/mod.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -693,14 +693,9 @@ impl<T: Config> Pallet<T> {
693693
/// Run the acceptance criteria checks on the given candidate commitments.
694694
pub(crate) fn check_validation_outputs_for_runtime_api(
695695
para_id: ParaId,
696+
relay_parent_number: T::BlockNumber,
696697
validation_outputs: primitives::v1::CandidateCommitments,
697698
) -> bool {
698-
// This function is meant to be called from the runtime APIs against the relay-parent, hence
699-
// `relay_parent_number` is equal to `now`.
700-
// TODO [now]: unwind this very deep assumption which is now wrong. probably
701-
// by changing runtime API to choose a relay-parent.
702-
let now = <frame_system::Pallet<T>>::block_number();
703-
let relay_parent_number = now;
704699
let prev_context = <paras::Pallet<T>>::para_most_recent_context(para_id);
705700
let check_ctx = CandidateCheckContext::<T>::new(prev_context);
706701

runtime/parachains/src/runtime_api_impl/v1.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,9 +259,14 @@ pub fn assumed_validation_data<T: initializer::Config>(
259259
/// Implementation for the `check_validation_outputs` function of the runtime API.
260260
pub fn check_validation_outputs<T: initializer::Config>(
261261
para_id: ParaId,
262+
relay_parent_number: T::BlockNumber,
262263
outputs: primitives::v1::CandidateCommitments,
263264
) -> bool {
264-
<inclusion::Pallet<T>>::check_validation_outputs_for_runtime_api(para_id, outputs)
265+
<inclusion::Pallet<T>>::check_validation_outputs_for_runtime_api(
266+
para_id,
267+
relay_parent_number,
268+
outputs,
269+
)
265270
}
266271

267272
/// Implementation for the `session_index_for_child` function of the runtime API.

runtime/polkadot/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1808,7 +1808,7 @@ sp_api::impl_runtime_apis! {
18081808
}
18091809
}
18101810

1811-
impl primitives::v2::ParachainHost<Block, Hash, BlockNumber> for Runtime {
1811+
impl primitives::staging::ParachainHost<Block, Hash, BlockNumber> for Runtime {
18121812
fn validators() -> Vec<ValidatorId> {
18131813
parachains_runtime_api_impl::validators::<Runtime>()
18141814
}
@@ -1838,9 +1838,10 @@ sp_api::impl_runtime_apis! {
18381838

18391839
fn check_validation_outputs(
18401840
para_id: ParaId,
1841+
relay_parent_number: BlockNumber,
18411842
outputs: primitives::v1::CandidateCommitments,
18421843
) -> bool {
1843-
parachains_runtime_api_impl::check_validation_outputs::<Runtime>(para_id, outputs)
1844+
parachains_runtime_api_impl::check_validation_outputs::<Runtime>(para_id, relay_parent_number, outputs)
18441845
}
18451846

18461847
fn session_index_for_child() -> SessionIndex {

runtime/rococo/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,7 +1168,7 @@ sp_api::impl_runtime_apis! {
11681168
}
11691169
}
11701170

1171-
impl primitives::v2::ParachainHost<Block, Hash, BlockNumber> for Runtime {
1171+
impl primitives::staging::ParachainHost<Block, Hash, BlockNumber> for Runtime {
11721172
fn validators() -> Vec<ValidatorId> {
11731173
runtime_api_impl::validators::<Runtime>()
11741174
}
@@ -1198,9 +1198,10 @@ sp_api::impl_runtime_apis! {
11981198

11991199
fn check_validation_outputs(
12001200
para_id: Id,
1201+
relay_parent_number: BlockNumber,
12011202
outputs: primitives::v1::CandidateCommitments,
12021203
) -> bool {
1203-
runtime_api_impl::check_validation_outputs::<Runtime>(para_id, outputs)
1204+
runtime_api_impl::check_validation_outputs::<Runtime>(para_id, relay_parent_number, outputs)
12041205
}
12051206

12061207
fn session_index_for_child() -> SessionIndex {

runtime/test-runtime/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,7 @@ sp_api::impl_runtime_apis! {
800800
}
801801
}
802802

803-
impl primitives::v2::ParachainHost<Block, Hash, BlockNumber> for Runtime {
803+
impl primitives::staging::ParachainHost<Block, Hash, BlockNumber> for Runtime {
804804
fn validators() -> Vec<ValidatorId> {
805805
runtime_impl::validators::<Runtime>()
806806
}
@@ -831,9 +831,10 @@ sp_api::impl_runtime_apis! {
831831

832832
fn check_validation_outputs(
833833
para_id: ParaId,
834+
relay_parent_number: BlockNumber,
834835
outputs: primitives::v1::CandidateCommitments,
835836
) -> bool {
836-
runtime_impl::check_validation_outputs::<Runtime>(para_id, outputs)
837+
parachains_runtime_api_impl::check_validation_outputs::<Runtime>(para_id, relay_parent_number, outputs)
837838
}
838839

839840
fn session_index_for_child() -> SessionIndex {

runtime/westend/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,7 +1229,7 @@ sp_api::impl_runtime_apis! {
12291229
}
12301230
}
12311231

1232-
impl primitives::v2::ParachainHost<Block, Hash, BlockNumber> for Runtime {
1232+
impl primitives::staging::ParachainHost<Block, Hash, BlockNumber> for Runtime {
12331233
fn validators() -> Vec<ValidatorId> {
12341234
parachains_runtime_api_impl::validators::<Runtime>()
12351235
}
@@ -1259,9 +1259,10 @@ sp_api::impl_runtime_apis! {
12591259

12601260
fn check_validation_outputs(
12611261
para_id: ParaId,
1262+
relay_parent_number: BlockNumber,
12621263
outputs: primitives::v1::CandidateCommitments,
12631264
) -> bool {
1264-
parachains_runtime_api_impl::check_validation_outputs::<Runtime>(para_id, outputs)
1265+
parachains_runtime_api_impl::check_validation_outputs::<Runtime>(para_id, relay_parent_number, outputs)
12651266
}
12661267

12671268
fn session_index_for_child() -> SessionIndex {

0 commit comments

Comments
 (0)