Transaction versioning in the RuntimeVersion (#5582)

* Add transaction_version

* Semantic versioning for runtimes

* Move new field to bottom

* Versioning

* Runtime versioning stuff.

* Fix test

* Adds tests and fixes bugs

* Bump runtime

Co-authored-by: Bastian Köcher <git@kchr.de>
This commit is contained in:
Gavin Wood
2020-04-17 12:10:31 +02:00
committed by GitHub
parent fea626ca84
commit fd2cb9ca83
12 changed files with 149 additions and 11 deletions
+43 -2
View File
@@ -53,7 +53,7 @@ pub use sp_runtime::{
Block as BlockT, GetNodeBlockType, GetRuntimeBlockType, HashFor, NumberFor,
Header as HeaderT, Hash as HashT,
},
generic::BlockId, transaction_validity::TransactionValidity,
generic::BlockId, transaction_validity::TransactionValidity, RuntimeString,
};
#[doc(hidden)]
pub use sp_core::{offchain, ExecutionContext};
@@ -224,6 +224,7 @@ pub use sp_api_proc_macro::decl_runtime_apis;
/// impl_version: 0,
/// // Here we are exposing the runtime api versions.
/// apis: RUNTIME_API_VERSIONS,
/// transaction_version: 1,
/// };
///
/// # fn main() {}
@@ -520,13 +521,53 @@ pub trait RuntimeApiInfo {
#[cfg(feature = "std")]
pub type ApiErrorFor<T, Block> = <<T as ProvideRuntimeApi<Block>>::Api as ApiErrorExt>::Error;
#[derive(codec::Encode, codec::Decode)]
pub struct OldRuntimeVersion {
pub spec_name: RuntimeString,
pub impl_name: RuntimeString,
pub authoring_version: u32,
pub spec_version: u32,
pub impl_version: u32,
pub apis: ApisVec,
}
impl From<OldRuntimeVersion> for RuntimeVersion {
fn from(x: OldRuntimeVersion) -> Self {
Self {
spec_name: x.spec_name,
impl_name: x.impl_name,
authoring_version: x.authoring_version,
spec_version: x.spec_version,
impl_version: x.impl_version,
apis: x.apis,
transaction_version: 1,
}
}
}
impl From<RuntimeVersion> for OldRuntimeVersion {
fn from(x: RuntimeVersion) -> Self {
Self {
spec_name: x.spec_name,
impl_name: x.impl_name,
authoring_version: x.authoring_version,
spec_version: x.spec_version,
impl_version: x.impl_version,
apis: x.apis,
}
}
}
decl_runtime_apis! {
/// The `Core` runtime api that every Substrate runtime needs to implement.
#[core_trait]
#[api_version(2)]
#[api_version(3)]
pub trait Core {
/// Returns the version of the runtime.
fn version() -> RuntimeVersion;
/// Returns the version of the runtime.
#[changed_in(3)]
fn version() -> OldRuntimeVersion;
/// Execute the given block.
#[skip_initialize_block]
fn execute_block(block: Block);
+12
View File
@@ -57,6 +57,18 @@ pub fn blake2_128(data: &[u8]) -> [u8; 16] {
r
}
/// Do a Blake2 64-bit hash and place result in `dest`.
pub fn blake2_64_into(data: &[u8], dest: &mut [u8; 8]) {
dest.copy_from_slice(blake2_rfc::blake2b::blake2b(8, &[], data).as_bytes());
}
/// Do a Blake2 64-bit hash and return result.
pub fn blake2_64(data: &[u8]) -> [u8; 8] {
let mut r = [0; 8];
blake2_64_into(data, &mut r);
r
}
/// Do a XX 64-bit hash and place result in `dest`.
pub fn twox_64_into(data: &[u8], dest: &mut [u8; 8]) {
use ::core::hash::Hasher;
@@ -103,6 +103,7 @@ mod utils;
/// impl_version: 0,
/// // Here we are exposing the runtime api versions.
/// apis: RUNTIME_API_VERSIONS,
/// transaction_version: 1,
/// };
///
/// # fn main() {}
+15 -3
View File
@@ -93,17 +93,29 @@ pub struct RuntimeVersion {
)
)]
pub apis: ApisVec,
/// All existing dispatches are fully compatible when this number doesn't change. If this
/// number changes, then `spec_version` must change, also.
///
/// This number must change when an existing dispatchable (module ID, dispatch ID) is changed,
/// either through an alteration in its user-level semantics, a parameter added/removed/changed,
/// a dispatchable being removed, a module being removed, or a dispatchable/module changing its
/// index.
///
/// It need *not* change when a new module is added or when a dispatchable is added.
pub transaction_version: u32,
}
#[cfg(feature = "std")]
impl fmt::Display for RuntimeVersion {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}-{}:{}({}-{})",
write!(f, "{}-{} ({}-{}.tx{}.au{})",
self.spec_name,
self.spec_version,
self.authoring_version,
self.impl_name,
self.impl_version
self.impl_version,
self.transaction_version,
self.authoring_version,
)
}
}