// Copyright 2017-2018 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see .
//! System manager: Handles all of the top-level stuff; executing block/transaction, setting code
//! and depositing logs.
#![cfg_attr(not(feature = "std"), no_std)]
#[cfg(feature = "std")]
extern crate serde;
#[cfg(feature = "std")]
#[macro_use]
extern crate serde_derive;
#[cfg(feature = "std")]
#[macro_use]
extern crate log;
#[macro_use]
extern crate parity_codec_derive;
extern crate num_traits;
extern crate integer_sqrt;
extern crate sr_std as rstd;
extern crate sr_io as runtime_io;
#[doc(hidden)]
pub extern crate parity_codec as codec;
extern crate substrate_primitives;
#[cfg(test)]
extern crate serde_json;
#[cfg(feature = "std")]
use std::collections::HashMap;
use rstd::prelude::*;
use substrate_primitives::hash::{H256, H512};
#[cfg(feature = "std")]
use substrate_primitives::hexdisplay::ascii_format;
#[cfg(feature = "std")]
pub mod testing;
pub mod traits;
pub mod generic;
pub mod transaction_validity;
pub type Justification = Vec;
use traits::{Verify, Lazy};
/// A String that is a `&'static str` on `no_std` and a `Cow<'static, str>` on `std`.
#[cfg(feature = "std")]
pub type RuntimeString = ::std::borrow::Cow<'static, str>;
#[cfg(not(feature = "std"))]
pub type RuntimeString = &'static str;
/// Create a const [RuntimeString].
#[cfg(feature = "std")]
#[macro_export]
macro_rules! create_runtime_str {
( $y:expr ) => {{ ::std::borrow::Cow::Borrowed($y) }}
}
#[cfg(not(feature = "std"))]
#[macro_export]
macro_rules! create_runtime_str {
( $y:expr ) => {{ $y }}
}
#[cfg(feature = "std")]
pub use serde::{Serialize, de::DeserializeOwned};
/// A set of key value pairs for storage.
#[cfg(feature = "std")]
pub type StorageMap = HashMap, Vec>;
/// A set of key value pairs for children storage;
#[cfg(feature = "std")]
pub type ChildrenStorageMap = HashMap, StorageMap>;
/// Complex storage builder stuff.
#[cfg(feature = "std")]
pub trait BuildStorage {
fn hash(data: &[u8]) -> [u8; 16] {
let r = runtime_io::twox_128(data);
trace!(target: "build_storage", "{} <= {}", substrate_primitives::hexdisplay::HexDisplay::from(&r), ascii_format(data));
r
}
fn build_storage(self) -> Result<(StorageMap, ChildrenStorageMap), String>;
}
#[cfg(feature = "std")]
impl BuildStorage for StorageMap {
fn build_storage(self) -> Result<(StorageMap, ChildrenStorageMap), String> {
Ok((self, Default::default()))
}
}
/// Permill is parts-per-million (i.e. after multiplying by this, divide by 1000000).
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
#[derive(Encode, Decode, Default, Copy, Clone, PartialEq, Eq)]
pub struct Permill(u32);
// TODO: impl Mul for N where N: As
impl Permill {
pub fn times + ::rstd::ops::Mul + ::rstd::ops::Div>(self, b: N) -> N {
// TODO: handle overflows
b * >::sa(self.0 as u64) / >::sa(1000000)
}
pub fn from_millionths(x: u32) -> Permill { Permill(x) }
pub fn from_percent(x: u32) -> Permill { Permill(x * 10_000) }
#[cfg(feature = "std")]
pub fn from_fraction(x: f64) -> Permill { Permill((x * 1_000_000.0) as u32) }
}
#[cfg(feature = "std")]
impl From for Permill {
fn from(x: f64) -> Permill {
Permill::from_fraction(x)
}
}
#[cfg(feature = "std")]
impl From for Permill {
fn from(x: f32) -> Permill {
Permill::from_fraction(x as f64)
}
}
impl codec::CompactAs for Permill {
type As = u32;
fn encode_as(&self) -> &u32 {
&self.0
}
fn decode_from(x: u32) -> Permill {
Permill(x)
}
}
impl From> for Permill {
fn from(x: codec::Compact) -> Permill {
x.0
}
}
/// Perbill is parts-per-billion. It stores a value between 0 and 1 in fixed point and
/// provides a means to multiply some other value by that.
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
#[derive(Encode, Decode, Default, Copy, Clone, PartialEq, Eq)]
pub struct Perbill(u32);
// TODO: impl Mul for N where N: As
impl Perbill {
/// Attenuate `b` by self.
pub fn times + ::rstd::ops::Mul + ::rstd::ops::Div>(self, b: N) -> N {
// TODO: handle overflows
b * >::sa(self.0 as u64) / >::sa(1_000_000_000)
}
/// Nothing.
pub fn zero() -> Perbill { Perbill(0) }
/// `true` if this is nothing.
pub fn is_zero(&self) -> bool { self.0 == 0 }
/// Everything.
pub fn one() -> Perbill { Perbill(1_000_000_000) }
/// Construct new instance where `x` is in billionths. Value equivalent to `x / 1,000,000,000`.
pub fn from_billionths(x: u32) -> Perbill { Perbill(x.min(1_000_000_000)) }
/// Construct new instance where `x` is in millionths. Value equivalent to `x / 1,000,000`.
pub fn from_millionths(x: u32) -> Perbill { Perbill(x.min(1_000_000) * 1000) }
/// Construct new instance where `x` is a percent. Value equivalent to `x%`.
pub fn from_percent(x: u32) -> Perbill { Perbill(x.min(100) * 10_000_000) }
#[cfg(feature = "std")]
/// Construct new instance whose value is equal to `x` (between 0 and 1).
pub fn from_fraction(x: f64) -> Perbill { Perbill((x.max(0.0).min(1.0) * 1_000_000_000.0) as u32) }
#[cfg(feature = "std")]
/// Construct new instance whose value is equal to `n / d` (between 0 and 1).
pub fn from_rational(n: f64, d: f64) -> Perbill { Perbill(((n / d).max(0.0).min(1.0) * 1_000_000_000.0) as u32) }
}
#[cfg(feature = "std")]
impl From for Perbill {
fn from(x: f64) -> Perbill {
Perbill::from_fraction(x)
}
}
#[cfg(feature = "std")]
impl From for Perbill {
fn from(x: f32) -> Perbill {
Perbill::from_fraction(x as f64)
}
}
impl codec::CompactAs for Perbill {
type As = u32;
fn encode_as(&self) -> &u32 {
&self.0
}
fn decode_from(x: u32) -> Perbill {
Perbill(x)
}
}
impl From> for Perbill {
fn from(x: codec::Compact) -> Perbill {
x.0
}
}
/// Ed25519 signature verify.
#[derive(Eq, PartialEq, Clone, Default, Encode, Decode)]
#[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))]
pub struct Ed25519Signature(pub H512);
impl Verify for Ed25519Signature {
type Signer = H256;
fn verify>(&self, mut msg: L, signer: &Self::Signer) -> bool {
runtime_io::ed25519_verify((self.0).as_fixed_bytes(), msg.get(), &signer.as_bytes())
}
}
impl From for Ed25519Signature {
fn from(h: H512) -> Ed25519Signature {
Ed25519Signature(h)
}
}
#[derive(Eq, PartialEq, Clone, Copy, Decode)]
#[cfg_attr(feature = "std", derive(Debug, Serialize))]
#[repr(u8)]
/// Outcome of a valid extrinsic application. Capable of being sliced.
pub enum ApplyOutcome {
/// Successful application (extrinsic reported no issue).
Success = 0,
/// Failed application (extrinsic was probably a no-op other than fees).
Fail = 1,
}
impl codec::Encode for ApplyOutcome {
fn using_encoded R>(&self, f: F) -> R {
f(&[*self as u8])
}
}
#[derive(Eq, PartialEq, Clone, Copy, Decode)]
#[cfg_attr(feature = "std", derive(Debug, Serialize))]
#[repr(u8)]
/// Reason why an extrinsic couldn't be applied (i.e. invalid extrinsic).
pub enum ApplyError {
/// Bad signature.
BadSignature = 0,
/// Nonce too low.
Stale = 1,
/// Nonce too high.
Future = 2,
/// Sending account had too low a balance.
CantPay = 3,
}
impl codec::Encode for ApplyError {
fn using_encoded R>(&self, f: F) -> R {
f(&[*self as u8])
}
}
/// Result from attempt to apply an extrinsic.
pub type ApplyResult = Result;
/// Verify a signature on an encoded value in a lazy manner. This can be
/// an optimization if the signature scheme has an "unsigned" escape hash.
pub fn verify_encoded_lazy(sig: &V, item: &T, signer: &V::Signer) -> bool {
// The `Lazy` trait expresses something like `X: FnMut