1#![warn(missing_docs)]
21
22#![cfg_attr(not(feature = "std"), no_std)]
23
24#[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
93pub enum ExecutionContext {
95 Importing,
97 Syncing,
106 BlockConstruction,
108 OffchainCall(Option<(Box<dyn offchain::Externalities>, offchain::Capabilities)>),
112}
113
114impl ExecutionContext {
115 pub fn capabilities(&self) -> offchain::Capabilities {
117 use ExecutionContext::*;
118
119 match self {
120 Importing | Syncing | BlockConstruction =>
121 offchain::Capabilities::none(),
122 OffchainCall(None) => [
124 offchain::Capability::Keystore,
125 offchain::Capability::TransactionPool,
126 ][..].into(),
127 OffchainCall(Some((_, capabilities))) => *capabilities,
128 }
129 }
130}
131
132#[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#[derive(Encode, Decode, PartialEq)]
161pub struct OpaqueMetadata(Vec<u8>);
162
163impl OpaqueMetadata {
164 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#[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 pub fn new(vec: Vec<u8>) -> Self {
186 OpaquePeerId(vec)
187 }
188}
189
190#[cfg(feature = "std")]
192pub enum NativeOrEncoded<R> {
193 Native(R),
195 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 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 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#[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 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
269pub trait TypeId {
271 const TYPE_ID: [u8; 4];
273}
274
275#[derive(Encode, Decode, PassByEnum, Copy, Clone)]
279pub enum LogLevel {
280 Error = 1,
282 Warn = 2,
284 Info = 3,
286 Debug = 4,
288 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#[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 tetcore_std::mem::forget(encoded);
348
349 res
350}
351
352#[derive(Clone, Decode, Encode, Eq, PartialEq, RuntimeDebug)]
355pub enum Void {}
356
357#[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}