mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 20:27:58 +00:00
Adds check_inherents function to the BlockerBuilder API (#912)
* Adds new `check_inherents` function to the `BlockBuilder` API * Switch to `check_inherents` in `node-consensus` * Remove `CheckedBlock`, because it is not required anymore * Fixes after rebase * Fixes compilation on stable
This commit is contained in:
@@ -1,94 +0,0 @@
|
||||
// Copyright 2018 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate 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.
|
||||
|
||||
// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Typesafe block interaction.
|
||||
|
||||
use super::{Call, Block, TIMESTAMP_SET_POSITION, NOTE_OFFLINE_POSITION};
|
||||
use timestamp::Call as TimestampCall;
|
||||
use consensus::Call as ConsensusCall;
|
||||
|
||||
/// Provides a type-safe wrapper around a structurally valid block.
|
||||
pub struct CheckedBlock {
|
||||
inner: Block,
|
||||
file_line: Option<(&'static str, u32)>,
|
||||
}
|
||||
|
||||
impl CheckedBlock {
|
||||
/// Create a new checked block. Fails if the block is not structurally valid.
|
||||
pub fn new(block: Block) -> Result<Self, Block> {
|
||||
let has_timestamp = block.extrinsics.get(TIMESTAMP_SET_POSITION as usize).map_or(false, |xt| {
|
||||
!xt.is_signed() && match xt.function {
|
||||
Call::Timestamp(TimestampCall::set(_)) => true,
|
||||
_ => false,
|
||||
}
|
||||
});
|
||||
|
||||
if !has_timestamp { return Err(block) }
|
||||
|
||||
Ok(CheckedBlock {
|
||||
inner: block,
|
||||
file_line: None,
|
||||
})
|
||||
}
|
||||
|
||||
// Creates a new checked block, asserting that it is valid.
|
||||
#[doc(hidden)]
|
||||
pub fn new_unchecked(block: Block, file: &'static str, line: u32) -> Self {
|
||||
CheckedBlock {
|
||||
inner: block,
|
||||
file_line: Some((file, line)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Extract the timestamp from the block.
|
||||
pub fn timestamp(&self) -> ::node_primitives::Timestamp {
|
||||
let x = self.inner.extrinsics.get(TIMESTAMP_SET_POSITION as usize).and_then(|xt| match xt.function {
|
||||
Call::Timestamp(TimestampCall::set(x)) => Some(x),
|
||||
_ => None
|
||||
});
|
||||
|
||||
match x {
|
||||
Some(x) => x,
|
||||
None => panic!("Invalid block asserted at {:?}", self.file_line),
|
||||
}
|
||||
}
|
||||
|
||||
/// Extract the noted missed proposal validator indices (if any) from the block.
|
||||
pub fn noted_offline(&self) -> &[u32] {
|
||||
self.inner.extrinsics.get(NOTE_OFFLINE_POSITION as usize).and_then(|xt| match xt.function {
|
||||
Call::Consensus(ConsensusCall::note_offline(ref x)) => Some(&x[..]),
|
||||
_ => None,
|
||||
}).unwrap_or(&[])
|
||||
}
|
||||
|
||||
/// Convert into inner block.
|
||||
pub fn into_inner(self) -> Block { self.inner }
|
||||
}
|
||||
|
||||
impl ::std::ops::Deref for CheckedBlock {
|
||||
type Target = Block;
|
||||
|
||||
fn deref(&self) -> &Block { &self.inner }
|
||||
}
|
||||
|
||||
/// Assert that a block is structurally valid. May lead to panic in the future
|
||||
/// in case it isn't.
|
||||
#[macro_export]
|
||||
macro_rules! assert_node_block {
|
||||
($block: expr) => {
|
||||
$crate::CheckedBlock::new_unchecked($block, file!(), line!())
|
||||
}
|
||||
}
|
||||
@@ -57,13 +57,13 @@ extern crate srml_treasury as treasury;
|
||||
extern crate sr_version as version;
|
||||
extern crate node_primitives;
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
mod checked_block;
|
||||
|
||||
use rstd::prelude::*;
|
||||
use substrate_primitives::u32_trait::{_2, _4};
|
||||
use node_primitives::{AccountId, AccountIndex, Balance, BlockNumber, Hash, Index, SessionKey, Signature, InherentData};
|
||||
use runtime_api::runtime::*;
|
||||
use node_primitives::{
|
||||
AccountId, AccountIndex, Balance, BlockNumber, Hash, Index,
|
||||
SessionKey, Signature, InherentData, Timestamp as TimestampType
|
||||
};
|
||||
use runtime_api::{BlockBuilderError, runtime::*};
|
||||
use runtime_primitives::ApplyResult;
|
||||
use runtime_primitives::transaction_validity::TransactionValidity;
|
||||
use runtime_primitives::generic;
|
||||
@@ -83,8 +83,6 @@ pub use balances::Call as BalancesCall;
|
||||
pub use runtime_primitives::{Permill, Perbill};
|
||||
pub use timestamp::BlockPeriod;
|
||||
pub use srml_support::StorageValue;
|
||||
#[cfg(any(feature = "std", test))]
|
||||
pub use checked_block::CheckedBlock;
|
||||
|
||||
const TIMESTAMP_SET_POSITION: u32 = 0;
|
||||
const NOTE_OFFLINE_POSITION: u32 = 1;
|
||||
@@ -271,7 +269,7 @@ impl_apis! {
|
||||
}
|
||||
}
|
||||
|
||||
impl BlockBuilder<Block, InherentData, UncheckedExtrinsic> for Runtime {
|
||||
impl BlockBuilder<Block, InherentData, UncheckedExtrinsic, InherentData> for Runtime {
|
||||
fn initialise_block(header: <Block as BlockT>::Header) {
|
||||
Executive::initialise_block(&header)
|
||||
}
|
||||
@@ -298,6 +296,38 @@ impl_apis! {
|
||||
inherent
|
||||
}
|
||||
|
||||
fn check_inherents(block: Block, data: InherentData) -> Result<(), BlockBuilderError> {
|
||||
// TODO: v1: should be automatically gathered
|
||||
|
||||
// Timestamp module...
|
||||
const MAX_TIMESTAMP_DRIFT: TimestampType = 60;
|
||||
let xt = block.extrinsics.get(TIMESTAMP_SET_POSITION as usize)
|
||||
.ok_or_else(|| BlockBuilderError::Generic("No valid timestamp inherent in block".into()))?;
|
||||
let t = match (xt.is_signed(), &xt.function) {
|
||||
(false, Call::Timestamp(TimestampCall::set(t))) => t,
|
||||
_ => return Err(BlockBuilderError::Generic("No valid timestamp inherent in block".into())),
|
||||
};
|
||||
|
||||
if *t > data.timestamp + MAX_TIMESTAMP_DRIFT {
|
||||
return Err(BlockBuilderError::TimestampInFuture(*t))
|
||||
}
|
||||
|
||||
// Offline indices
|
||||
let noted_offline =
|
||||
block.extrinsics.get(NOTE_OFFLINE_POSITION as usize).and_then(|xt| match xt.function {
|
||||
Call::Consensus(ConsensusCall::note_offline(ref x)) => Some(&x[..]),
|
||||
_ => None,
|
||||
}).unwrap_or(&[]);
|
||||
|
||||
noted_offline.iter().try_for_each(|n|
|
||||
if !data.offline_indices.contains(n) {
|
||||
Err(BlockBuilderError::Generic("Online node marked offline".into()))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
fn random_seed() -> <Block as BlockT>::Hash {
|
||||
System::random_seed()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user