Implement support for renaming runtime api functions (#2160)

* Implement support for renaming runtime api functions

* Redelete the wasm files

* FIxes test

* Fix test correctly...

* Bring back old `authorities`

* Tag as deprecated

* Fixes compilation on WASM

* Add missing method implementations

* Fixes tests

* Increase `spec_version`
This commit is contained in:
Bastian Köcher
2019-04-02 10:05:35 +02:00
committed by Gav Wood
parent e89ce263bd
commit 345145326b
13 changed files with 288 additions and 81 deletions
@@ -23,11 +23,12 @@ pub use inherents::{InherentData, CheckInherentsResult};
decl_runtime_apis! {
/// The `BlockBuilder` api trait that provides required functions for building a block for a runtime.
#[api_version(2)]
#[api_version(3)]
pub trait BlockBuilder {
/// Apply the given extrinsics.
fn apply_extrinsic(extrinsic: <Block as BlockT>::Extrinsic) -> ApplyResult;
/// Finish the current block.
#[renamed("finalise_block", 3)]
fn finalize_block() -> <Block as BlockT>::Header;
/// Generate inherent extrinsics. The inherent data will vary from chain to chain.
fn inherent_extrinsics(inherent: InherentData) -> Vec<<Block as BlockT>::Extrinsic>;
+14 -6
View File
@@ -17,7 +17,7 @@
use std::{sync::Arc, cmp::Ord, panic::UnwindSafe, result};
use parity_codec::{Encode, Decode};
use runtime_primitives::generic::BlockId;
use runtime_primitives::traits::Block as BlockT;
use runtime_primitives::traits::{Block as BlockT, RuntimeApiInfo};
use state_machine::{
self, OverlayedChanges, Ext, CodeExecutor, ExecutionManager, ExecutionStrategy, NeverOffchainExt,
};
@@ -28,6 +28,7 @@ use primitives::{H256, Blake2Hasher, NativeOrEncoded, NeverNativeValue, Offchain
use crate::backend;
use crate::error;
use crate::runtime_api::Core as CoreApi;
/// Method call executor.
pub trait CallExecutor<B, H>
@@ -220,7 +221,15 @@ where
mut side_effects_handler: Option<&mut O>,
) -> Result<NativeOrEncoded<R>, error::Error> where ExecutionManager<EM>: Clone {
let state = self.backend.state_at(*at)?;
if method != "Core_initialize_block" && initialized_block.map(|id| id != *at).unwrap_or(true) {
let core_version = self.runtime_version(at)?.apis.iter().find(|a| a.0 == CoreApi::<Block>::ID).map(|a| a.1);
let init_block_function = if core_version < Some(2) {
"Core_initialise_block"
} else {
"Core_initialize_block"
};
if method != init_block_function && initialized_block.map(|id| id != *at).unwrap_or(true) {
let header = prepare_environment_block()?;
state_machine::new(
&state,
@@ -228,7 +237,7 @@ where
side_effects_handler.as_mut().map(|x| &mut **x),
changes,
&self.executor,
"Core_initialize_block",
init_block_function,
&header.encode(),
).execute_using_consensus_failure_handler::<_, R, fn() -> _>(
execution_manager.clone(),
@@ -253,7 +262,7 @@ where
).map(|(result, _, _)| result)?;
// If the method is `initialize_block` we need to set the `initialized_block`
if method == "Core_initialize_block" {
if method == init_block_function {
*initialized_block = Some(*at);
}
@@ -265,8 +274,7 @@ where
let mut overlay = OverlayedChanges::default();
let state = self.backend.state_at(*id)?;
let mut ext = Ext::new(&mut overlay, &state, self.backend.changes_trie_storage(), NeverOffchainExt::new());
self.executor.runtime_version(&mut ext)
.ok_or(error::ErrorKind::VersionInvalid.into())
self.executor.runtime_version(&mut ext).ok_or(error::ErrorKind::VersionInvalid.into())
}
fn call_at_state<
+7
View File
@@ -35,6 +35,7 @@ pub use runtime_version::{ApiId, RuntimeVersion, ApisVec, create_apis_vec};
pub use rstd::{slice, mem};
#[cfg(feature = "std")]
use rstd::result;
#[doc(hidden)]
pub use parity_codec::{Encode, Decode};
#[cfg(feature = "std")]
use crate::error;
@@ -42,6 +43,7 @@ use sr_api_macros::decl_runtime_apis;
use primitives::OpaqueMetadata;
#[cfg(feature = "std")]
use std::panic::UnwindSafe;
use rstd::vec::Vec;
/// Something that can be constructed to a runtime api.
#[cfg(feature = "std")]
@@ -113,13 +115,18 @@ pub trait CallRuntimeAt<Block: BlockT> {
decl_runtime_apis! {
/// The `Core` api trait that is mandatory for each runtime.
#[core_trait]
#[api_version(2)]
pub trait Core {
/// Returns the version of the runtime.
fn version() -> RuntimeVersion;
/// Execute the given block.
fn execute_block(block: Block);
/// Initialize a block with the given header.
#[renamed("initialise_block", 2)]
fn initialize_block(header: &<Block as BlockT>::Header);
/// Returns the authorities.
#[deprecated(since = "1.0", note = "Please switch to `AuthoritiesApi`.")]
fn authorities() -> Vec<AuthorityIdFor<Block>>;
}
/// The `Metadata` api trait that returns metadata for the runtime.