tet_core/
lib.rs

1// This file is part of Tetcore.
2
3// Copyright (C) 2017-2021 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//! Shareable Tetcore types.
19
20#![warn(missing_docs)]
21
22#![cfg_attr(not(feature = "std"), no_std)]
23
24/// Initialize a key-value collection from array.
25///
26/// Creates a vector of given pairs and calls `collect` on the iterator from it.
27/// Can be used to create a `HashMap`.
28#[macro_export]
29macro_rules! map {
30	($( $name:expr => $value:expr ),* $(,)? ) => (
31		vec![ $( ( $name, $value ) ),* ].into_iter().collect()
32	);
33}
34
35use tp_runtime_interface::pass_by::{PassByEnum, PassByInner};
36use tetcore_std::prelude::*;
37use tetcore_std::ops::Deref;
38#[cfg(feature = "std")]
39use std::borrow::Cow;
40#[cfg(feature = "std")]
41use serde::{Serialize, Deserialize};
42#[cfg(feature = "std")]
43pub use serde;
44#[doc(hidden)]
45pub use codec::{Encode, Decode};
46
47pub use debug_derive::RuntimeDebug;
48
49#[cfg(feature = "std")]
50pub use impl_serde::serialize as bytes;
51
52#[cfg(feature = "full_crypto")]
53pub mod hashing;
54
55#[cfg(feature = "full_crypto")]
56pub use hashing::{blake2_128, blake2_256, twox_64, twox_128, twox_256, keccak_256};
57pub mod hexdisplay;
58pub mod crypto;
59
60pub mod u32_trait;
61
62pub mod ed25519;
63pub mod sr25519;
64pub mod ecdsa;
65pub mod hash;
66#[cfg(feature = "std")]
67mod hasher;
68pub mod offchain;
69pub mod sandbox;
70pub mod uint;
71mod changes_trie;
72#[cfg(feature = "std")]
73pub mod traits;
74pub mod testing;
75
76pub use self::hash::{H160, H256, H512, convert_hash};
77pub use self::uint::{U256, U512};
78pub use changes_trie::{ChangesTrieConfiguration, ChangesTrieConfigurationRange};
79#[cfg(feature = "full_crypto")]
80pub use crypto::{DeriveJunction, Pair, Public};
81
82pub use tetsy_hash_db::Hasher;
83#[cfg(feature = "std")]
84pub use self::hasher::blake2::Blake2Hasher;
85#[cfg(feature = "std")]
86pub use self::hasher::keccak::KeccakHasher;
87
88pub use tetcore_storage as storage;
89
90#[doc(hidden)]
91pub use tetcore_std;
92
93/// Context for executing a call into the runtime.
94pub enum ExecutionContext {
95	/// Context used for general block import (including locally authored blocks).
96	Importing,
97	/// Context used for importing blocks as part of an initial sync of the blockchain.
98	///
99	/// We distinguish between major sync and import so that validators who are running
100	/// their initial sync (or catching up after some time offline) can use the faster
101	/// native runtime (since we can reasonably assume the network as a whole has already
102	/// come to a broad conensus on the block and it probably hasn't been crafted
103	/// specifically to attack this node), but when importing blocks at the head of the
104	/// chain in normal operation they can use the safer Wasm version.
105	Syncing,
106	/// Context used for block construction.
107	BlockConstruction,
108	/// Context used for offchain calls.
109	///
110	/// This allows passing offchain extension and customizing available capabilities.
111	OffchainCall(Option<(Box<dyn offchain::Externalities>, offchain::Capabilities)>),
112}
113
114impl ExecutionContext {
115	/// Returns the capabilities of particular context.
116	pub fn capabilities(&self) -> offchain::Capabilities {
117		use ExecutionContext::*;
118
119		match self {
120			Importing | Syncing | BlockConstruction =>
121				offchain::Capabilities::none(),
122			// Enable keystore and transaction pool by default for offchain calls.
123			OffchainCall(None) => [
124				offchain::Capability::Keystore,
125				offchain::Capability::TransactionPool,
126			][..].into(),
127			OffchainCall(Some((_, capabilities))) => *capabilities,
128		}
129	}
130}
131
132/// Hex-serialized shim for `Vec<u8>`.
133#[derive(PartialEq, Eq, Clone, RuntimeDebug)]
134#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Hash, PartialOrd, Ord))]
135pub struct Bytes(#[cfg_attr(feature = "std", serde(with="bytes"))] pub Vec<u8>);
136
137impl From<Vec<u8>> for Bytes {
138	fn from(s: Vec<u8>) -> Self { Bytes(s) }
139}
140
141impl From<OpaqueMetadata> for Bytes {
142	fn from(s: OpaqueMetadata) -> Self { Bytes(s.0) }
143}
144
145impl Deref for Bytes {
146	type Target = [u8];
147	fn deref(&self) -> &[u8] { &self.0[..] }
148}
149
150#[cfg(feature = "std")]
151impl tetcore_std::str::FromStr for Bytes {
152	type Err = bytes::FromHexError;
153
154	fn from_str(s: &str) -> Result<Self, Self::Err> {
155		bytes::from_hex(s).map(Bytes)
156	}
157}
158
159/// Stores the encoded `RuntimeMetadata` for the native side as opaque type.
160#[derive(Encode, Decode, PartialEq)]
161pub struct OpaqueMetadata(Vec<u8>);
162
163impl OpaqueMetadata {
164	/// Creates a new instance with the given metadata blob.
165	pub fn new(metadata: Vec<u8>) -> Self {
166		OpaqueMetadata(metadata)
167	}
168}
169
170impl tetcore_std::ops::Deref for OpaqueMetadata {
171	type Target = Vec<u8>;
172
173	fn deref(&self) -> &Self::Target {
174		&self.0
175	}
176}
177
178/// Simple blob to hold a `PeerId` without committing to its format.
179#[derive(Default, Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, RuntimeDebug, PassByInner)]
180#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
181pub struct OpaquePeerId(pub Vec<u8>);
182
183impl OpaquePeerId {
184	/// Create new `OpaquePeerId`
185	pub fn new(vec: Vec<u8>) -> Self {
186		OpaquePeerId(vec)
187	}
188}
189
190/// Something that is either a native or an encoded value.
191#[cfg(feature = "std")]
192pub enum NativeOrEncoded<R> {
193	/// The native representation.
194	Native(R),
195	/// The encoded representation.
196	Encoded(Vec<u8>)
197}
198
199#[cfg(feature = "std")]
200impl<R> From<R> for NativeOrEncoded<R> {
201	fn from(val: R) -> Self {
202		Self::Native(val)
203	}
204}
205
206#[cfg(feature = "std")]
207impl<R: codec::Encode> tetcore_std::fmt::Debug for NativeOrEncoded<R> {
208	fn fmt(&self, f: &mut tetcore_std::fmt::Formatter) -> tetcore_std::fmt::Result {
209		hexdisplay::HexDisplay::from(&self.as_encoded().as_ref()).fmt(f)
210	}
211}
212
213#[cfg(feature = "std")]
214impl<R: codec::Encode> NativeOrEncoded<R> {
215	/// Return the value as the encoded format.
216	pub fn as_encoded(&self) -> Cow<'_, [u8]> {
217		match self {
218			NativeOrEncoded::Encoded(e) => Cow::Borrowed(e.as_slice()),
219			NativeOrEncoded::Native(n) => Cow::Owned(n.encode()),
220		}
221	}
222
223	/// Return the value as the encoded format.
224	pub fn into_encoded(self) -> Vec<u8> {
225		match self {
226			NativeOrEncoded::Encoded(e) => e,
227			NativeOrEncoded::Native(n) => n.encode(),
228		}
229	}
230}
231
232#[cfg(feature = "std")]
233impl<R: PartialEq + codec::Decode> PartialEq for NativeOrEncoded<R> {
234	fn eq(&self, other: &Self) -> bool {
235		match (self, other) {
236			(NativeOrEncoded::Native(l), NativeOrEncoded::Native(r)) => l == r,
237			(NativeOrEncoded::Native(n), NativeOrEncoded::Encoded(e)) |
238			(NativeOrEncoded::Encoded(e), NativeOrEncoded::Native(n)) =>
239				Some(n) == codec::Decode::decode(&mut &e[..]).ok().as_ref(),
240			(NativeOrEncoded::Encoded(l), NativeOrEncoded::Encoded(r)) => l == r,
241		}
242	}
243}
244
245/// A value that is never in a native representation.
246/// This is type is useful in conjunction with `NativeOrEncoded`.
247#[cfg(feature = "std")]
248#[derive(PartialEq)]
249pub enum NeverNativeValue {}
250
251#[cfg(feature = "std")]
252impl codec::Encode for NeverNativeValue {
253	fn encode(&self) -> Vec<u8> {
254		// The enum is not constructable, so this function should never be callable!
255		unreachable!()
256	}
257}
258
259#[cfg(feature = "std")]
260impl codec::EncodeLike for NeverNativeValue {}
261
262#[cfg(feature = "std")]
263impl codec::Decode for NeverNativeValue {
264	fn decode<I: codec::Input>(_: &mut I) -> Result<Self, codec::Error> {
265		Err("`NeverNativeValue` should never be decoded".into())
266	}
267}
268
269/// Provide a simple 4 byte identifier for a type.
270pub trait TypeId {
271	/// Simple 4 byte identifier.
272	const TYPE_ID: [u8; 4];
273}
274
275/// A log level matching the one from `log` crate.
276///
277/// Used internally by `tet_io::log` method.
278#[derive(Encode, Decode, PassByEnum, Copy, Clone)]
279pub enum LogLevel {
280	/// `Error` log level.
281	Error = 1,
282	/// `Warn` log level.
283	Warn = 2,
284	/// `Info` log level.
285	Info = 3,
286	/// `Debug` log level.
287	Debug = 4,
288	/// `Trace` log level.
289	Trace = 5,
290}
291
292impl From<u32> for LogLevel {
293	fn from(val: u32) -> Self {
294		match val {
295			x if x == LogLevel::Warn as u32 => LogLevel::Warn,
296			x if x == LogLevel::Info as u32 => LogLevel::Info,
297			x if x == LogLevel::Debug as u32 => LogLevel::Debug,
298			x if x == LogLevel::Trace as u32 => LogLevel::Trace,
299			_ => LogLevel::Error,
300		}
301	}
302}
303
304impl From<log::Level> for LogLevel {
305	fn from(l: log::Level) -> Self {
306		use log::Level::*;
307		match l {
308			Error => Self::Error,
309			Warn => Self::Warn,
310			Info => Self::Info,
311			Debug => Self::Debug,
312			Trace => Self::Trace,
313		}
314	}
315}
316
317impl From<LogLevel> for log::Level {
318	fn from(l: LogLevel) -> Self {
319		use self::LogLevel::*;
320		match l {
321			Error => Self::Error,
322			Warn => Self::Warn,
323			Info => Self::Info,
324			Debug => Self::Debug,
325			Trace => Self::Trace,
326		}
327	}
328}
329
330/// Encodes the given value into a buffer and returns the pointer and the length as a single `u64`.
331///
332/// When Tetcore calls into Wasm it expects a fixed signature for functions exported
333/// from the Wasm blob. The return value of this signature is always a `u64`.
334/// This `u64` stores the pointer to the encoded return value and the length of this encoded value.
335/// The low `32bits` are reserved for the pointer, followed by `32bit` for the length.
336#[cfg(not(feature = "std"))]
337pub fn to_tetcore_wasm_fn_return_value(value: &impl Encode) -> u64 {
338	let encoded = value.encode();
339
340	let ptr = encoded.as_ptr() as u64;
341	let length = encoded.len() as u64;
342	let res = ptr | (length << 32);
343
344	// Leak the output vector to avoid it being freed.
345	// This is fine in a WASM context since the heap
346	// will be discarded after the call.
347	tetcore_std::mem::forget(encoded);
348
349	res
350}
351
352/// The void type - it cannot exist.
353// Oh rust, you cnoble me up...
354#[derive(Clone, Decode, Encode, Eq, PartialEq, RuntimeDebug)]
355pub enum Void {}
356
357/// Macro for creating `Maybe*` marker traits.
358///
359/// Such a maybe-marker trait requires the given bound when `feature = std` and doesn't require
360/// the bound on `no_std`. This is useful for situations where you require that a type implements
361/// a certain trait with `feature = std`, but not on `no_std`.
362///
363/// # Example
364///
365/// ```
366/// tet_core::impl_maybe_marker! {
367///     /// A marker for a type that implements `Debug` when `feature = std`.
368///     trait MaybeDebug: std::fmt::Debug;
369///     /// A marker for a type that implements `Debug + Display` when `feature = std`.
370///     trait MaybeDebugDisplay: std::fmt::Debug, std::fmt::Display;
371/// }
372/// ```
373#[macro_export]
374macro_rules! impl_maybe_marker {
375	(
376		$(
377			$(#[$doc:meta] )+
378			trait $trait_name:ident: $( $trait_bound:path ),+;
379		)+
380	) => {
381		$(
382			$(#[$doc])+
383			#[cfg(feature = "std")]
384			pub trait $trait_name: $( $trait_bound + )+ {}
385			#[cfg(feature = "std")]
386			impl<T: $( $trait_bound + )+> $trait_name for T {}
387
388			$(#[$doc])+
389			#[cfg(not(feature = "std"))]
390			pub trait $trait_name {}
391			#[cfg(not(feature = "std"))]
392			impl<T> $trait_name for T {}
393		)+
394	}
395}