1
// This file is part of Substrate.
2

            
3
// Copyright (C) Parity Technologies (UK) Ltd.
4
// SPDX-License-Identifier: Apache-2.0
5

            
6
// Licensed under the Apache License, Version 2.0 (the "License");
7
// you may not use this file except in compliance with the License.
8
// You may obtain a copy of the License at
9
//
10
// 	http://www.apache.org/licenses/LICENSE-2.0
11
//
12
// Unless required by applicable law or agreed to in writing, software
13
// distributed under the License is distributed on an "AS IS" BASIS,
14
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
// See the License for the specific language governing permissions and
16
// limitations under the License.
17

            
18
//! Substrate runtime version
19
//!
20
//! Each runtime that should be executed by a Substrate based node needs to have a runtime version.
21
//! The runtime version is defined by [`RuntimeVersion`]. The runtime version is used to
22
//! distinguish different runtimes. The most important field is the
23
//! [`spec_version`](RuntimeVersion::spec_version). The `spec_version` should be increased in a
24
//! runtime when a new runtime build includes breaking changes that would make other runtimes unable
25
//! to import blocks built by this runtime or vice-versa, where the new runtime could not import
26
//! blocks built by the old runtime. The runtime version also carries other version information
27
//! about the runtime, see [`RuntimeVersion`] for more information on this.
28
//!
29
//! Substrate will fetch the runtime version from a `wasm` blob by first checking the
30
//! `runtime_version` link section or calling the `Core::version` runtime api. The link section can
31
//! be generated in the runtime using the [`runtime_version`] attribute. The `Core` runtime api also
32
//! needs to be implemented for the runtime using `impl_runtime_apis!`.
33

            
34
#![cfg_attr(not(feature = "std"), no_std)]
35

            
36
extern crate alloc;
37

            
38
#[cfg(feature = "serde")]
39
use serde::{Deserialize, Serialize};
40
#[cfg(feature = "std")]
41
use std::collections::HashSet;
42
#[cfg(feature = "std")]
43
use std::fmt;
44

            
45
#[doc(hidden)]
46
pub use alloc::borrow::Cow;
47
use codec::{Decode, Encode, Input};
48
use scale_info::TypeInfo;
49
use sp_runtime::RuntimeString;
50
pub use sp_runtime::{create_runtime_str, StateVersion};
51
#[doc(hidden)]
52
pub use sp_std;
53

            
54
#[cfg(feature = "std")]
55
use sp_runtime::traits::Block as BlockT;
56

            
57
#[cfg(feature = "std")]
58
pub mod embed;
59

            
60
/// An attribute that accepts a version declaration of a runtime and generates a custom wasm
61
/// section with the equivalent contents.
62
///
63
/// The custom section allows to read the version of the runtime without having to execute any
64
/// code. Instead, the generated custom section can be relatively easily parsed from the wasm
65
/// binary. The identifier of the custom section is "runtime_version".
66
///
67
/// A shortcoming of this macro is that it is unable to embed information regarding supported
68
/// APIs. This is supported by the `construct_runtime!` macro.
69
///
70
/// # Usage
71
///
72
/// This macro accepts a const item like the following:
73
///
74
/// ```rust
75
/// use sp_version::{create_runtime_str, RuntimeVersion};
76
///
77
/// #[sp_version::runtime_version]
78
/// pub const VERSION: RuntimeVersion = RuntimeVersion {
79
/// 	spec_name: create_runtime_str!("test"),
80
/// 	impl_name: create_runtime_str!("test"),
81
/// 	authoring_version: 10,
82
/// 	spec_version: 265,
83
/// 	impl_version: 1,
84
/// 	apis: RUNTIME_API_VERSIONS,
85
/// 	transaction_version: 2,
86
/// 	state_version: 1,
87
/// };
88
///
89
/// # const RUNTIME_API_VERSIONS: sp_version::ApisVec = sp_version::create_apis_vec!([]);
90
/// ```
91
///
92
/// It will pass it through and add code required for emitting a custom section. The
93
/// information that will go into the custom section is parsed from the item declaration. Due
94
/// to that, the macro is somewhat rigid in terms of the code it accepts. There are the
95
/// following considerations:
96
///
97
/// - The `spec_name` and `impl_name` must be set by a macro-like expression. The name of the
98
///   macro doesn't matter though.
99
///
100
/// - `authoring_version`, `spec_version`, `impl_version` and `transaction_version` must be set
101
///   by a literal. Literal must be an integer. No other expressions are allowed there. In
102
///   particular, you can't supply a constant variable.
103
///
104
/// - `apis` doesn't have any specific constraints. This is because this information doesn't
105
///   get into the custom section and is not parsed.
106
///
107
/// # Compilation Target & "std" feature
108
///
109
/// This macro assumes it will be used within a runtime. By convention, a runtime crate defines
110
/// a feature named "std". This feature is enabled when the runtime is compiled to native code
111
/// and disabled when it is compiled to the wasm code.
112
///
113
/// The custom section can only be emitted while compiling to wasm. In order to detect the
114
/// compilation target we use the "std" feature. This macro will emit the custom section only
115
/// if the "std" feature is **not** enabled.
116
///
117
/// Including this macro in the context where there is no "std" feature and the code is not
118
/// compiled to wasm can lead to cryptic linking errors.
119
pub use sp_version_proc_macro::runtime_version;
120

            
121
/// The identity of a particular API interface that the runtime might provide.
122
///
123
/// The id is generated by hashing the name of the runtime api with BLAKE2 using a hash size
124
/// of 8 bytes.
125
///
126
/// The name of the runtime api is the name of the trait when using `decl_runtime_apis!` macro. So,
127
/// in the following runtime api declaration:
128
///
129
/// ```nocompile
130
/// decl_runtime_apis! {
131
///     trait TestApi {
132
///         fn do_test();
133
///     }
134
/// }
135
/// ```
136
///
137
/// The name of the trait would be `TestApi` and would be taken as input to the BLAKE2 hash
138
/// function.
139
///
140
/// As Rust supports renaming of traits, the name of a runtime api given to `impl_runtime_apis!`
141
/// doesn't need to be the same as in `decl_runtime_apis!`, but only the name in
142
/// `decl_runtime_apis!` is the important one!
143
pub type ApiId = [u8; 8];
144

            
145
/// A vector of pairs of `ApiId` and a `u32` for version.
146
pub type ApisVec = alloc::borrow::Cow<'static, [(ApiId, u32)]>;
147

            
148
/// Create a vector of Api declarations.
149
#[macro_export]
150
macro_rules! create_apis_vec {
151
	( $y:expr ) => {
152
		$crate::Cow::Borrowed(&$y)
153
	};
154
}
155

            
156
/// Runtime version.
157
/// This should not be thought of as classic Semver (major/minor/tiny).
158
/// This triplet have different semantics and mis-interpretation could cause problems.
159
/// In particular: bug fixes should result in an increment of `spec_version` and possibly
160
/// `authoring_version`, absolutely not `impl_version` since they change the semantics of the
161
/// runtime.
162
#[derive(Clone, PartialEq, Eq, Encode, Default, sp_runtime::RuntimeDebug, TypeInfo)]
163
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
164
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
165
pub struct RuntimeVersion {
166
	/// Identifies the different Substrate runtimes. There'll be at least polkadot and node.
167
	/// A different on-chain spec_name to that of the native runtime would normally result
168
	/// in node not attempting to sync or author blocks.
169
	pub spec_name: RuntimeString,
170

            
171
	/// Name of the implementation of the spec. This is of little consequence for the node
172
	/// and serves only to differentiate code of different implementation teams. For this
173
	/// codebase, it will be parity-polkadot. If there were a non-Rust implementation of the
174
	/// Polkadot runtime (e.g. C++), then it would identify itself with an accordingly different
175
	/// `impl_name`.
176
	pub impl_name: RuntimeString,
177

            
178
	/// `authoring_version` is the version of the authorship interface. An authoring node
179
	/// will not attempt to author blocks unless this is equal to its native runtime.
180
	pub authoring_version: u32,
181

            
182
	/// Version of the runtime specification.
183
	///
184
	/// A full-node will not attempt to use its native runtime in substitute for the on-chain
185
	/// Wasm runtime unless all of `spec_name`, `spec_version` and `authoring_version` are the same
186
	/// between Wasm and native.
187
	///
188
	/// This number should never decrease.
189
	pub spec_version: u32,
190

            
191
	/// Version of the implementation of the specification.
192
	///
193
	/// Nodes are free to ignore this; it serves only as an indication that the code is different;
194
	/// as long as the other two versions are the same then while the actual code may be different,
195
	/// it is nonetheless required to do the same thing. Non-consensus-breaking optimizations are
196
	/// about the only changes that could be made which would result in only the `impl_version`
197
	/// changing.
198
	///
199
	/// This number can be reverted to `0` after a [`spec_version`](Self::spec_version) bump.
200
	pub impl_version: u32,
201

            
202
	/// List of supported API "features" along with their versions.
203
	#[cfg_attr(
204
		feature = "serde",
205
		serde(
206
			serialize_with = "apis_serialize::serialize",
207
			deserialize_with = "apis_serialize::deserialize",
208
		)
209
	)]
210
	pub apis: ApisVec,
211

            
212
	/// All existing calls (dispatchables) are fully compatible when this number doesn't change. If
213
	/// this number changes, then [`spec_version`](Self::spec_version) must change, also.
214
	///
215
	/// This number must change when an existing call (pallet index, call index) is changed,
216
	/// either through an alteration in its user-level semantics, a parameter
217
	/// added/removed, a parameter type changed, or a call/pallet changing its index. An alteration
218
	/// of the user level semantics is for example when the call was before `transfer` and now is
219
	/// `transfer_all`, the semantics of the call changed completely.
220
	///
221
	/// Removing a pallet or a call doesn't require a *bump* as long as no pallet or call is put at
222
	/// the same index. Removing doesn't require a bump as the chain will reject a transaction
223
	/// referencing this removed call/pallet while decoding and thus, the user isn't at risk to
224
	/// execute any unknown call. FRAME runtime devs have control over the index of a call/pallet
225
	/// to prevent that an index gets reused.
226
	///
227
	/// Adding a new pallet or call also doesn't require a *bump* as long as they also don't reuse
228
	/// any previously used index.
229
	///
230
	/// This number should never decrease.
231
	pub transaction_version: u32,
232

            
233
	/// Version of the state implementation used by this runtime.
234
	/// Use of an incorrect version is consensus breaking.
235
	pub state_version: u8,
236
}
237

            
238
impl RuntimeVersion {
239
	/// `Decode` while giving a "version hint"
240
	///
241
	/// There exists multiple versions of [`RuntimeVersion`] and they are versioned using the `Core`
242
	/// runtime api:
243
	/// - `Core` version < 3 is a runtime version without a transaction version and state version.
244
	/// - `Core` version 3 is a runtime version without a state version.
245
	/// - `Core` version 4 is the latest runtime version.
246
	pub fn decode_with_version_hint<I: Input>(
247
		input: &mut I,
248
		core_version: Option<u32>,
249
	) -> Result<RuntimeVersion, codec::Error> {
250
		let spec_name = Decode::decode(input)?;
251
		let impl_name = Decode::decode(input)?;
252
		let authoring_version = Decode::decode(input)?;
253
		let spec_version = Decode::decode(input)?;
254
		let impl_version = Decode::decode(input)?;
255
		let apis = Decode::decode(input)?;
256
		let core_version =
257
			if core_version.is_some() { core_version } else { core_version_from_apis(&apis) };
258
		let transaction_version =
259
			if core_version.map(|v| v >= 3).unwrap_or(false) { Decode::decode(input)? } else { 1 };
260
		let state_version =
261
			if core_version.map(|v| v >= 4).unwrap_or(false) { Decode::decode(input)? } else { 0 };
262
		Ok(RuntimeVersion {
263
			spec_name,
264
			impl_name,
265
			authoring_version,
266
			spec_version,
267
			impl_version,
268
			apis,
269
			transaction_version,
270
			state_version,
271
		})
272
	}
273
}
274

            
275
impl Decode for RuntimeVersion {
276
	fn decode<I: Input>(input: &mut I) -> Result<Self, codec::Error> {
277
		Self::decode_with_version_hint(input, None)
278
	}
279
}
280

            
281
#[cfg(feature = "std")]
282
impl fmt::Display for RuntimeVersion {
283
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
284
		write!(
285
			f,
286
			"{}-{} ({}-{}.tx{}.au{})",
287
			self.spec_name,
288
			self.spec_version,
289
			self.impl_name,
290
			self.impl_version,
291
			self.transaction_version,
292
			self.authoring_version,
293
		)
294
	}
295
}
296

            
297
#[cfg(feature = "std")]
298
fn has_api_with<P: Fn(u32) -> bool>(apis: &ApisVec, id: &ApiId, predicate: P) -> bool {
299
	apis.iter().any(|(s, v)| s == id && predicate(*v))
300
}
301

            
302
/// Returns the version of the `Core` runtime api.
303
pub fn core_version_from_apis(apis: &ApisVec) -> Option<u32> {
304
	let id = sp_crypto_hashing_proc_macro::blake2b_64!(b"Core");
305
	apis.iter().find(|(s, _v)| s == &id).map(|(_s, v)| *v)
306
}
307

            
308
#[cfg(feature = "std")]
309
impl RuntimeVersion {
310
	/// Check if this version matches other version for calling into runtime.
311
	pub fn can_call_with(&self, other: &RuntimeVersion) -> bool {
312
		self.spec_version == other.spec_version &&
313
			self.spec_name == other.spec_name &&
314
			self.authoring_version == other.authoring_version
315
	}
316

            
317
	/// Check if the given api with `api_id` is implemented and the version passes the given
318
	/// `predicate`.
319
	pub fn has_api_with<P: Fn(u32) -> bool>(&self, id: &ApiId, predicate: P) -> bool {
320
		has_api_with(&self.apis, id, predicate)
321
	}
322

            
323
	/// Returns the api version found for api with `id`.
324
	pub fn api_version(&self, id: &ApiId) -> Option<u32> {
325
		self.apis.iter().find_map(|a| (a.0 == *id).then(|| a.1))
326
	}
327
}
328

            
329
impl RuntimeVersion {
330
	/// Returns state version to use for update.
331
	///
332
	/// For runtime with core api version less than 4,
333
	/// V0 trie version will be applied to state.
334
	/// Otherwise, V1 trie version will be use.
335
389526
	pub fn state_version(&self) -> StateVersion {
336
389526
		// If version > than 1, keep using latest version.
337
389526
		self.state_version.try_into().unwrap_or(StateVersion::V1)
338
389526
	}
339
}
340

            
341
/// The version of the native runtime.
342
///
343
/// In contrast to the bare [`RuntimeVersion`] this also carries a list of `spec_version`s of
344
/// runtimes this native runtime can be used to author blocks for.
345
#[derive(Debug)]
346
#[cfg(feature = "std")]
347
pub struct NativeVersion {
348
	/// Basic runtime version info.
349
	pub runtime_version: RuntimeVersion,
350
	/// Authoring runtimes (`spec_version`s) that this native runtime supports.
351
	pub can_author_with: HashSet<u32>,
352
}
353

            
354
#[cfg(feature = "std")]
355
impl NativeVersion {
356
	/// Check if this version matches other version for authoring blocks.
357
	///
358
	/// # Return
359
	///
360
	/// - Returns `Ok(())` when authoring is supported.
361
	/// - Returns `Err(_)` with a detailed error when authoring is not supported.
362
	pub fn can_author_with(&self, other: &RuntimeVersion) -> Result<(), String> {
363
		if self.runtime_version.spec_name != other.spec_name {
364
			Err(format!(
365
				"`spec_name` does not match `{}` vs `{}`",
366
				self.runtime_version.spec_name, other.spec_name,
367
			))
368
		} else if self.runtime_version.authoring_version != other.authoring_version &&
369
			!self.can_author_with.contains(&other.authoring_version)
370
		{
371
			Err(format!(
372
				"`authoring_version` does not match `{version}` vs `{other_version}` and \
373
				`can_author_with` not contains `{other_version}`",
374
				version = self.runtime_version.authoring_version,
375
				other_version = other.authoring_version,
376
			))
377
		} else {
378
			Ok(())
379
		}
380
	}
381
}
382

            
383
#[cfg(feature = "std")]
384
/// Returns the version of the native runtime.
385
pub trait GetNativeVersion {
386
	/// Returns the version of the native runtime.
387
	fn native_version(&self) -> &NativeVersion;
388
}
389

            
390
/// Something that can provide the runtime version at a given block.
391
#[cfg(feature = "std")]
392
pub trait GetRuntimeVersionAt<Block: BlockT> {
393
	/// Returns the version of runtime at the given block.
394
	fn runtime_version(&self, at: <Block as BlockT>::Hash) -> Result<RuntimeVersion, String>;
395
}
396

            
397
#[cfg(feature = "std")]
398
impl<T: GetRuntimeVersionAt<Block>, Block: BlockT> GetRuntimeVersionAt<Block>
399
	for std::sync::Arc<T>
400
{
401
	fn runtime_version(&self, at: <Block as BlockT>::Hash) -> Result<RuntimeVersion, String> {
402
		(&**self).runtime_version(at)
403
	}
404
}
405

            
406
#[cfg(feature = "std")]
407
impl<T: GetNativeVersion> GetNativeVersion for std::sync::Arc<T> {
408
	fn native_version(&self) -> &NativeVersion {
409
		(&**self).native_version()
410
	}
411
}
412

            
413
#[cfg(feature = "serde")]
414
mod apis_serialize {
415
	use super::*;
416
	use alloc::vec::Vec;
417
	use impl_serde::serialize as bytes;
418
	use serde::{de, ser::SerializeTuple, Serializer};
419

            
420
	#[derive(Serialize)]
421
	struct ApiId<'a>(#[serde(serialize_with = "serialize_bytesref")] &'a super::ApiId, &'a u32);
422

            
423
	pub fn serialize<S>(apis: &ApisVec, ser: S) -> Result<S::Ok, S::Error>
424
	where
425
		S: Serializer,
426
	{
427
		let len = apis.len();
428
		let mut seq = ser.serialize_tuple(len)?;
429
		for (api, ver) in &**apis {
430
			seq.serialize_element(&ApiId(api, ver))?;
431
		}
432
		seq.end()
433
	}
434

            
435
	pub fn serialize_bytesref<S>(&apis: &&super::ApiId, ser: S) -> Result<S::Ok, S::Error>
436
	where
437
		S: Serializer,
438
	{
439
		bytes::serialize(apis, ser)
440
	}
441

            
442
	#[derive(Deserialize)]
443
	struct ApiIdOwned(#[serde(deserialize_with = "deserialize_bytes")] super::ApiId, u32);
444

            
445
	pub fn deserialize<'de, D>(deserializer: D) -> Result<ApisVec, D::Error>
446
	where
447
		D: de::Deserializer<'de>,
448
	{
449
		struct Visitor;
450
		impl<'de> de::Visitor<'de> for Visitor {
451
			type Value = ApisVec;
452

            
453
			fn expecting(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
454
				formatter.write_str("a sequence of api id and version tuples")
455
			}
456

            
457
			fn visit_seq<V>(self, mut visitor: V) -> Result<Self::Value, V::Error>
458
			where
459
				V: de::SeqAccess<'de>,
460
			{
461
				let mut apis = Vec::new();
462
				while let Some(value) = visitor.next_element::<ApiIdOwned>()? {
463
					apis.push((value.0, value.1));
464
				}
465
				Ok(apis.into())
466
			}
467
		}
468
		deserializer.deserialize_seq(Visitor)
469
	}
470

            
471
	pub fn deserialize_bytes<'de, D>(d: D) -> Result<super::ApiId, D::Error>
472
	where
473
		D: de::Deserializer<'de>,
474
	{
475
		let mut arr = [0; 8];
476
		bytes::deserialize_check_len(d, bytes::ExpectedLen::Exact(&mut arr[..]))?;
477
		Ok(arr)
478
	}
479
}