Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions substrate/runtime-support/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,37 @@ macro_rules! decl_dispatch {
$($rest)*
}
};
// WITH MANY TRAIT BOUNDS
(
impl for $mod_type:ident<$trait_instance:ident: $trait_name:ident>;
$(#[$attr:meta])*
pub enum $call_type:ident where aux: $aux_type:ty,
$($tr:ident : $bound:ty)*
{
$(
fn $fn_name:ident(aux,
$(
, $param_name:ident : $param:ty
)*
) -> $result:ty
= $id:expr ;
)*
}
$($rest:tt)*
) => {
__decl_dispatch_module_with_aux! {
impl for $mod_type<$trait_instance: $trait_name>;
$(#[$attr])*
pub enum $call_type where $($tr : $bound),* ;
$(
fn $fn_name(aux $(, $param_name: $param )*) -> $result = $id;
)*
}
decl_dispatch! {
impl for $mod_type<$trait_instance: $trait_name>;
$($rest)*
}
};
// BASE CASE
(
impl for $mod_type:ident<$trait_instance:ident: $trait_name:ident>;
Expand Down
2 changes: 2 additions & 0 deletions substrate/runtime/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ substrate-primitives = { path = "../../primitives", default_features = false }
substrate-runtime-std = { path = "../../runtime-std", default_features = false }
substrate-runtime-io = { path = "../../runtime-io", default_features = false }
substrate-runtime-support = { path = "../../runtime-support", default_features = false }
ed25519 = { path = "../../ed25519", default_features = false }
log = {version = "0.3", optional = true }
byte-num = "0.1"

[dev-dependencies]
serde_json = "1.0"
Expand Down
79 changes: 79 additions & 0 deletions substrate/runtime/primitives/src/address_format.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright 2017 Parity Technologies (UK) Ltd.
// This file is part of Substrate Demo.

// Substrate Demo 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 Demo 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 Demo. If not, see <http://www.gnu.org/licenses/>.

use byte_num::convert::IntoAscii;
use testing::Digest;
use substrate_primitives::H256;
use generic::Digest as GenericDigest;
use ed25519::Public;

#[derive(Clone, Debug)]
pub struct SS58Compatible ( pub Public);

pub trait SS58: Into<SS58Compatible > {
fn encode(self) -> String {
self.into().0.to_ss58check()
}
}

impl From<u64> for SS58Compatible {
fn from(x: u64) -> Self {
SS58Compatible (Public::from_slice(&x.itoa()))
}
}

impl SS58 for u64 {}

impl From<Digest> for SS58Compatible {
fn from(x: Digest) -> Self {
SS58Compatible (Public::from_slice(&x
.logs
.iter()
.map(|digest| digest.itoa())
.flatten()
.collect::<Vec<u8>>()
))
}
}
impl SS58 for Digest {}

impl From<H256> for SS58Compatible {
fn from(x: H256) -> Self {
SS58Compatible (Public::from_slice(&x[..]))
}
}

impl SS58 for H256 {}

impl<Item: Into<u8>> From<GenericDigest<Item>> for SS58Compatible where
Vec<u8> : From<Vec<Item>>
{
fn from(x: GenericDigest<Item>) -> Self {
SS58Compatible (Public::from_slice(Vec::from(x.logs).as_slice()))
}
}

impl<Item: Into<u8>> SS58 for GenericDigest<Item> where
Vec<u8>: From<Vec<Item>>
{}

impl<Item: Into<u8>> From<GenericDigest<Item>> for Vec<u8>
{
fn from(x: GenericDigest<Item>) -> Self {
x.logs.into_iter().map(|element| Into::<u8>::into(element)).collect()
}

}
2 changes: 1 addition & 1 deletion substrate/runtime/primitives/src/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ impl<Number, Hash, DigestItem> Encode for Header<Number, Hash, DigestItem> where
impl<Number, Hash, DigestItem> traits::Header for Header<Number, Hash, DigestItem> where
Number: Member + ::rstd::hash::Hash + Copy + Codec + MaybeDisplay + SimpleArithmetic + Codec,
Hash: HashT,
DigestItem: Member + Default + Codec,
DigestItem: Member + Default + Codec + Into<Vec<u8>>,
Hash::Output: Default + ::rstd::hash::Hash + Copy + Member + MaybeDisplay + SimpleBitOps + Codec,
{
type Number = Number;
Expand Down
3 changes: 3 additions & 0 deletions substrate/runtime/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ extern crate substrate_runtime_io as runtime_io;
extern crate substrate_runtime_support as runtime_support;
extern crate substrate_codec as codec;
extern crate substrate_primitives;
extern crate ed25519;
extern crate byte_num;

#[cfg(test)]
extern crate serde_json;
Expand All @@ -59,6 +61,7 @@ pub mod testing;
pub mod traits;
pub mod generic;
pub mod bft;
pub mod address_format;

use traits::{Verify, Lazy};

Expand Down
1 change: 1 addition & 0 deletions substrate/runtime/primitives/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use std::fmt::Debug;
use codec::Codec;
use runtime_support::AuxDispatchable;
use traits::{self, Checkable, Applyable, BlakeTwo256};
use address_format::SS58;

pub use substrate_primitives::H256;

Expand Down
3 changes: 2 additions & 1 deletion substrate/runtime/primitives/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub use integer_sqrt::IntegerSquareRoot;
pub use num_traits::{Zero, One, Bounded};
pub use num_traits::ops::checked::{CheckedAdd, CheckedSub, CheckedMul, CheckedDiv};
use rstd::ops::{Add, Sub, Mul, Div, Rem, AddAssign, SubAssign, MulAssign, DivAssign, RemAssign};
use address_format::SS58;

/// A lazy value.
pub trait Lazy<T: ?Sized> {
Expand Down Expand Up @@ -426,4 +427,4 @@ pub trait Applyable: Sized + Send + Sync {
fn index(&self) -> &Self::Index;
fn sender(&self) -> &Self::AccountId;
fn apply(self) -> Result<(), &'static str>;
}
}
2 changes: 2 additions & 0 deletions substrate/runtime/staking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ authors = ["Parity Technologies <[email protected]>"]

[dependencies]
hex-literal = "0.1.0"
base58 = "0.1"
serde = { version = "1.0", default_features = false }
serde_derive = { version = "1.0", optional = true }
safe-mix = { version = "1.0", default_features = false}
Expand All @@ -20,6 +21,7 @@ substrate-runtime-consensus = { path = "../consensus", default_features = false
substrate-runtime-system = { path = "../system", default_features = false }
substrate-runtime-session = { path = "../session", default_features = false }
substrate-runtime-timestamp = { path = "../timestamp", default_features = false }
ed25519 = { path = "../../ed25519", default_features = false }

[dev-dependencies]
wabt = "0.4"
Expand Down
59 changes: 46 additions & 13 deletions substrate/runtime/staking/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@
#[cfg(feature = "std")]
use std::fmt;
use super::{Member, Decode, Encode, As, Input, Output};
use primitives::address_format::{SS58, SS58Compatible};
use ed25519::Public;

/// A vetted and verified extrinsic from the external world.
#[derive(PartialEq, Eq, Clone)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug, Hash))]
pub enum Address<AccountId, AccountIndex> where
AccountId: Member,
AccountIndex: Member,
AccountId: Member + fmt::Display + Into<SS58Compatible>,
AccountIndex: Member + fmt::Display + Into<SS58Compatible>,
{
/// It's an account ID (pubkey).
#[cfg_attr(feature = "std", serde(deserialize_with="AccountId::deserialize"))]
Expand All @@ -37,17 +39,18 @@ pub enum Address<AccountId, AccountIndex> where

#[cfg(feature = "std")]
impl<AccountId, AccountIndex> fmt::Display for Address<AccountId, AccountIndex> where
AccountId: Member,
AccountIndex: Member,
AccountId: Member + Sized + fmt::Display + Into<SS58Compatible> + Default + fmt::Debug + Sync,
AccountIndex: Member + Sized + fmt::Display + Into<SS58Compatible> + fmt::Debug + Sync,
Public: From<AccountIndex>
{
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, "{:?}", self)
write!(f, "{:?}", &self.clone().encode())
}
}

impl<AccountId, AccountIndex> From<AccountId> for Address<AccountId, AccountIndex> where
AccountId: Member,
AccountIndex: Member,
AccountId: Member + fmt::Display + Into<SS58Compatible>,
AccountIndex: Member + fmt::Display + Into<SS58Compatible>,
{
fn from(a: AccountId) -> Self {
Address::Id(a)
Expand All @@ -59,8 +62,8 @@ fn need_more_than<T: PartialOrd>(a: T, b: T) -> Option<T> {
}

impl<AccountId, AccountIndex> Decode for Address<AccountId, AccountIndex> where
AccountId: Member + Decode,
AccountIndex: Member + Decode + PartialOrd<AccountIndex> + Ord + As<u32> + As<u16> + As<u8> + Copy,
AccountId: Member + fmt::Display + Decode,
AccountIndex: Member + fmt::Display + Decode + PartialOrd<AccountIndex> + Ord + As<u32> + As<u16> + As<u8> + Copy,
{
fn decode<I: Input>(input: &mut I) -> Option<Self> {
Some(match input.read_byte()? {
Expand All @@ -75,8 +78,8 @@ impl<AccountId, AccountIndex> Decode for Address<AccountId, AccountIndex> where
}

impl<AccountId, AccountIndex> Encode for Address<AccountId, AccountIndex> where
AccountId: Member + Encode,
AccountIndex: Member + Encode + PartialOrd<AccountIndex> + Ord + As<u32> + As<u16> + As<u8> + Copy,
AccountId: Member + fmt::Display + Encode,
AccountIndex: Member + fmt::Display + Encode + PartialOrd<AccountIndex> + Ord + As<u32> + As<u16> + As<u8> + Copy,
{
fn encode_to<T: Output>(&self, dest: &mut T) {
match *self {
Expand All @@ -102,10 +105,40 @@ impl<AccountId, AccountIndex> Encode for Address<AccountId, AccountIndex> where
}

impl<AccountId, AccountIndex> Default for Address<AccountId, AccountIndex> where
AccountId: Member + Default,
AccountIndex: Member,
AccountId: Member + fmt::Display + Into<SS58Compatible> + Default,
AccountIndex: Member + fmt::Display + Into<SS58Compatible>,
{
fn default() -> Self {
Address::Id(Default::default())
}
}

impl<AccountId, AccountIndex> Into<Public> for Address<AccountId, AccountIndex> where
AccountId: Member + fmt::Display + Into<SS58Compatible> + Default + fmt::Debug + Sync,
AccountIndex: Member + fmt::Display + Into<SS58Compatible> + fmt::Debug + Sync,


{
fn into(self) -> Public {
match self {
Address::Id(id) => Into::<Public>::into(id),
Address::Index(id) => Into::<Public>::into(id)
}
}
}

impl<AccountId, AccountIndex> SS58 for Address<AccountId, AccountIndex> where
AccountId: Member + fmt::Display + Into<SS58Compatible> + Default + fmt::Debug + Sync,
AccountIndex: Member + fmt::Display + Into<SS58Compatible> + fmt::Debug + Sync,
Vec<u8>: From<AccountIndex>
{}

impl<AccountId, AccountIndex> Into<SS58Compatible> for Address<AccountId, AccountIndex> where
AccountId: Member + fmt::Display + Into<SS58Compatible> + Default + fmt::Debug + Sync,
AccountIndex: Member + fmt::Display + Into<SS58Compatible> + fmt::Debug + Sync,
Vec<u8>: From<AccountIndex>
{
fn into(self) -> SS58Compatible {
SS58Compatible(self.into())
}
}
Loading