Check in block authoring that we can author with current authoring version (#4201)

* Check in block authoring that we can author with current authoring version

* Update client/consensus/pow/src/lib.rs

Co-Authored-By: André Silva <andre.beat@gmail.com>

* Fix compilation
This commit is contained in:
Bastian Köcher
2019-11-29 11:01:11 +01:00
committed by GitHub
parent 0b52f194f5
commit accc678640
18 changed files with 187 additions and 62 deletions
+3 -10
View File
@@ -28,7 +28,6 @@ use parity_scale_codec::Error as CodecError;
/// Client Result type alias
pub type Result<T> = result::Result<T, Error>;
/// Error when the runtime failed to apply an extrinsic.
#[derive(Debug, Display)]
pub enum ApplyExtrinsicFailed {
@@ -72,8 +71,9 @@ pub enum Error {
#[display(fmt = "Current state of blockchain has invalid authorities set")]
InvalidAuthoritiesSet,
/// Could not get runtime version.
#[display(fmt = "On-chain runtime does not specify version")]
VersionInvalid,
#[display(fmt = "Failed to get runtime version: {}", _0)]
#[from(ignore)]
VersionInvalid(String),
/// Genesis config is invalid.
#[display(fmt = "Genesis config provided is invalid")]
GenesisInvalid,
@@ -125,7 +125,6 @@ pub enum Error {
InvalidStateRoot,
/// A convenience variant for String
#[display(fmt = "{}", _0)]
#[from(ignore)]
Msg(String),
}
@@ -139,12 +138,6 @@ impl error::Error for Error {
}
}
impl From<String> for Error {
fn from(s: String) -> Self {
Error::Msg(s)
}
}
impl<'a> From<&'a str> for Error {
fn from(s: &'a str) -> Self {
Error::Msg(s.into())
@@ -31,7 +31,7 @@
use std::sync::Arc;
use std::time::Duration;
use sr_primitives::traits::{Block as BlockT, DigestFor};
use sr_primitives::{traits::{Block as BlockT, DigestFor}, generic::BlockId};
use futures::prelude::*;
pub use inherents::InherentData;
@@ -123,13 +123,59 @@ impl SyncOracle for NoNetwork {
fn is_offline(&mut self) -> bool { false }
}
impl<T> SyncOracle for Arc<T>
where T: ?Sized, for<'r> &'r T: SyncOracle
{
impl<T> SyncOracle for Arc<T> where T: ?Sized, for<'r> &'r T: SyncOracle {
fn is_major_syncing(&mut self) -> bool {
<&T>::is_major_syncing(&mut &**self)
}
fn is_offline(&mut self) -> bool {
<&T>::is_offline(&mut &**self)
}
}
/// Checks if the current active native block authoring implementation can author with the runtime
/// at the given block.
pub trait CanAuthorWith<Block: BlockT> {
/// See trait docs for more information.
fn can_author_with(&self, at: &BlockId<Block>) -> bool;
}
/// Checks if the node can author blocks by using
/// [`NativeVersion::can_author_with`](runtime_version::NativeVersion::can_author_with).
pub struct CanAuthorWithNativeVersion<T>(T);
impl<T> CanAuthorWithNativeVersion<T> {
/// Creates a new instance of `Self`.
pub fn new(inner: T) -> Self {
Self(inner)
}
}
impl<T: runtime_version::GetRuntimeVersion<Block>, Block: BlockT> CanAuthorWith<Block>
for CanAuthorWithNativeVersion<T>
{
fn can_author_with(&self, at: &BlockId<Block>) -> bool {
match self.0.runtime_version(at) {
Ok(version) => self.0.native_version().can_author_with(&version),
Err(e) => {
error!(
target: "CanAuthorWithNativeVersion",
"Failed to get runtime version at `{}` and will disable authoring. Error: {}",
at,
e,
);
false
}
}
}
}
/// Returns always `true` for `can_author_with`. This is useful for tests.
pub struct AlwaysCanAuthor;
impl<Block: BlockT> CanAuthorWith<Block> for AlwaysCanAuthor {
fn can_author_with(&self, _: &BlockId<Block>) -> bool {
true
}
}
@@ -33,6 +33,9 @@ use codec::Decode;
use sr_primitives::RuntimeString;
pub use sr_primitives::create_runtime_str;
#[cfg(feature = "std")]
use sr_primitives::{traits::Block as BlockT, generic::BlockId};
/// The identity of a particular API interface that the runtime might provide.
pub type ApiId = [u8; 8];
@@ -165,6 +168,27 @@ impl NativeVersion {
}
}
/// Something that can provide the runtime version at a given block and the native runtime version.
#[cfg(feature = "std")]
pub trait GetRuntimeVersion<Block: BlockT> {
/// Returns the version of the native runtime.
fn native_version(&self) -> &NativeVersion;
/// Returns the version of runtime at the given block.
fn runtime_version(&self, at: &BlockId<Block>) -> Result<RuntimeVersion, String>;
}
#[cfg(feature = "std")]
impl<T: GetRuntimeVersion<Block>, Block: BlockT> GetRuntimeVersion<Block> for std::sync::Arc<T> {
fn native_version(&self) -> &NativeVersion {
(&**self).native_version()
}
fn runtime_version(&self, at: &BlockId<Block>) -> Result<RuntimeVersion, String> {
(&**self).runtime_version(at)
}
}
#[cfg(feature = "std")]
mod apis_serialize {
use super::*;