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

Commit

Permalink
impl ProvideInherent for InclusionInherent (#1318)
Browse files Browse the repository at this point in the history
* impl ProvideInherent for InclusionInherent

* reduce import churn; correct expect message

* move inclusion inherent identifier into primitives

It's not clear precisely why this is desired, but it's a pattern
I've seen in several places, so I'm going this to be on the
safe side. Worst case, we can revert this commit pretty easily.

* bump kusama spec_version to placate CI

* add license header

* empty commit; maybe github will notice the one with changes

* add sanity check to only include valid inherents
  • Loading branch information
coriolinus authored Jun 30, 2020
1 parent 51ce2b3 commit 7aa95b1
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
23 changes: 23 additions & 0 deletions primitives/src/inclusion_inherent.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2017-2020 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.

// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.

//! Inclusion Inherent primitives define types and constants which can be imported
//! without needing to import the entire inherent module.
use inherents::InherentIdentifier;

/// Unique identifier for the Inclusion Inherent
pub const INHERENT_IDENTIFIER: InherentIdentifier = *b"inclusn0";
1 change: 1 addition & 0 deletions primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use runtime_primitives::{generic, MultiSignature};
pub use runtime_primitives::traits::{BlakeTwo256, Hash as HashT, Verify, IdentifyAccount};

pub mod inclusion_inherent;
pub mod parachain;

pub use parity_scale_codec::Compact;
Expand Down
6 changes: 6 additions & 0 deletions primitives/src/parachain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,12 @@ pub type SignedAvailabilityBitfield = Signed<AvailabilityBitfield>;
#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug)]
pub struct SignedAvailabilityBitfields(pub Vec<SignedAvailabilityBitfield>);

impl From<Vec<SignedAvailabilityBitfield>> for SignedAvailabilityBitfields {
fn from(fields: Vec<SignedAvailabilityBitfield>) -> SignedAvailabilityBitfields {
SignedAvailabilityBitfields(fields)
}
}

/// A backed (or backable, depending on context) candidate.
// TODO: yes, this is roughly the same as AttestedCandidate.
// After https://github.com/paritytech/polkadot/issues/1250
Expand Down
31 changes: 28 additions & 3 deletions runtime/parachains/src/inclusion_inherent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,23 @@
use sp_std::prelude::*;
use primitives::{
inclusion_inherent,
parachain::{BackedCandidate, SignedAvailabilityBitfields},
};
use frame_support::{
decl_storage, decl_module, decl_error, ensure,
decl_error, decl_module, decl_storage, ensure,
dispatch::DispatchResult,
weights::{DispatchClass, Weight},
traits::Get,
};
use system::ensure_none;
use crate::{inclusion, scheduler::{self, FreedReason}};
use crate::{
inclusion,
scheduler::{self, FreedReason},
};
use inherents::{InherentIdentifier, InherentData, MakeFatalError, ProvideInherent};

pub trait Trait: inclusion::Trait + scheduler::Trait { }
pub trait Trait: inclusion::Trait + scheduler::Trait {}

decl_storage! {
trait Store for Module<T: Trait> as ParaInclusionInherent {
Expand Down Expand Up @@ -118,3 +123,23 @@ decl_module! {
}
}
}

impl<T: Trait> ProvideInherent for Module<T> {
type Call = Call<T>;
type Error = MakeFatalError<()>;
const INHERENT_IDENTIFIER: InherentIdentifier = inclusion_inherent::INHERENT_IDENTIFIER;

fn create_inherent(data: &InherentData) -> Option<Self::Call> {
data.get_data(&Self::INHERENT_IDENTIFIER)
.expect("inclusion inherent data failed to decode")
.map(|(signed_bitfields, backed_candidates): (SignedAvailabilityBitfields, Vec<BackedCandidate<T::Hash>>)| {
// Sanity check: session changes can invalidate an inherent, and we _really_ don't want that to happen.
// See github.com/paritytech/polkadot/issues/1327
if Self::inclusion(system::RawOrigin::None.into(), signed_bitfields.clone(), backed_candidates.clone()).is_ok() {
Call::inclusion(signed_bitfields, backed_candidates)
} else {
Call::inclusion(Vec::new().into(), Vec::new())
}
})
}
}

0 comments on commit 7aa95b1

Please sign in to comment.