From 8232ff25d89dfa7a6944d6624db304377c9b5ac2 Mon Sep 17 00:00:00 2001 From: dylan_DPC Date: Mon, 13 Aug 2018 20:58:53 +0530 Subject: [PATCH 1/4] add dependence on base58 encoding for Address --- Cargo.lock | 1 + substrate/runtime/primitives/src/traits.rs | 2 +- substrate/runtime/staking/Cargo.toml | 1 + substrate/runtime/staking/src/address.rs | 16 +++++++++++++++- substrate/runtime/staking/src/lib.rs | 2 ++ 5 files changed, 20 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c49662d9772d4..25ef539b11d4d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2828,6 +2828,7 @@ dependencies = [ name = "substrate-runtime-staking" version = "0.1.0" dependencies = [ + "base58 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "hex-literal 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "safe-mix 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.70 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/substrate/runtime/primitives/src/traits.rs b/substrate/runtime/primitives/src/traits.rs index 3c89087d70dc3..45d2b5fdfafaf 100644 --- a/substrate/runtime/primitives/src/traits.rs +++ b/substrate/runtime/primitives/src/traits.rs @@ -426,4 +426,4 @@ pub trait Applyable: Sized + Send + Sync { fn index(&self) -> &Self::Index; fn sender(&self) -> &Self::AccountId; fn apply(self) -> Result<(), &'static str>; -} +} \ No newline at end of file diff --git a/substrate/runtime/staking/Cargo.toml b/substrate/runtime/staking/Cargo.toml index 4ca513a1f0c9d..ccdb21053a6a9 100644 --- a/substrate/runtime/staking/Cargo.toml +++ b/substrate/runtime/staking/Cargo.toml @@ -5,6 +5,7 @@ authors = ["Parity Technologies "] [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} diff --git a/substrate/runtime/staking/src/address.rs b/substrate/runtime/staking/src/address.rs index 249e0b20db225..36dfdbe1c2e70 100644 --- a/substrate/runtime/staking/src/address.rs +++ b/substrate/runtime/staking/src/address.rs @@ -19,6 +19,7 @@ #[cfg(feature = "std")] use std::fmt; use super::{Member, Decode, Encode, As, Input, Output}; +use base58::ToBase58; /// A vetted and verified extrinsic from the external world. #[derive(PartialEq, Eq, Clone)] @@ -41,7 +42,7 @@ impl fmt::Display for Address AccountIndex: Member, { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { - write!(f, "{:?}", self) + write!(f, "{:?}", self.to_base58()) } } @@ -109,3 +110,16 @@ impl Default for Address where Address::Id(Default::default()) } } + +impl ToBase58 for Address where + AccountId: Member + ToBase58, + AccountIndex: Member + ToBase58, +{ + fn to_base58(&self) -> String { + match self { + Address::Id(ref id) => id.to_base58(), + Address::Index(ref id) => id.to_base58(), + } + + } +} \ No newline at end of file diff --git a/substrate/runtime/staking/src/lib.rs b/substrate/runtime/staking/src/lib.rs index b62b585dbbaa2..7fd43e42c123e 100644 --- a/substrate/runtime/staking/src/lib.rs +++ b/substrate/runtime/staking/src/lib.rs @@ -28,6 +28,8 @@ extern crate serde_derive; #[cfg(test)] extern crate wabt; +extern crate base58; + #[macro_use] extern crate substrate_runtime_support as runtime_support; From cce30a760b6e63190227387bc6501abe815c2a51 Mon Sep 17 00:00:00 2001 From: dylan_DPC Date: Fri, 17 Aug 2018 00:13:10 +0530 Subject: [PATCH 2/4] add various from checks and trait bounds move base58 to a separate file add byte-num and base58 dependencies --- Cargo.lock | 8 ++ substrate/primitives/src/hash.rs | 2 +- substrate/runtime/primitives/Cargo.toml | 2 + .../runtime/primitives/src/address_format.rs | 74 +++++++++++++++++++ substrate/runtime/primitives/src/generic.rs | 2 +- substrate/runtime/primitives/src/lib.rs | 3 + substrate/runtime/primitives/src/testing.rs | 1 + substrate/runtime/primitives/src/traits.rs | 1 + substrate/runtime/staking/src/address.rs | 63 ++++++++++------ substrate/runtime/staking/src/lib.rs | 7 +- substrate/runtime/system/src/lib.rs | 5 +- 11 files changed, 140 insertions(+), 28 deletions(-) create mode 100644 substrate/runtime/primitives/src/address_format.rs diff --git a/Cargo.lock b/Cargo.lock index 25ef539b11d4d..8a9b155fb2c6c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -193,6 +193,11 @@ name = "bs58" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "byte-num" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "byte-tools" version = "0.2.0" @@ -2777,6 +2782,8 @@ dependencies = [ name = "substrate-runtime-primitives" version = "0.1.0" dependencies = [ + "base58 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "byte-num 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "integer-sqrt 0.1.0 (git+https://github.com/paritytech/integer-sqrt-rs.git)", "log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", "num-traits 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", @@ -3721,6 +3728,7 @@ dependencies = [ "checksum blake2-rfc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)" = "5d6d530bdd2d52966a6d03b7a964add7ae1a288d25214066fd4b600f0f796400" "checksum block-buffer 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a076c298b9ecdb530ed9d967e74a6027d6a7478924520acddcddc24c1c8ab3ab" "checksum bs58 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2e6ea4851598d7433fbdba71fa2509d9b0df68124b9c0effe7588f5149692d9f" +"checksum byte-num 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4ac0bdba75808434f04270f3b7976ea6a662dc367d264feb54c5953258d59c66" "checksum byte-tools 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "560c32574a12a89ecd91f5e742165893f86e3ab98d21f8ea548658eb9eef5f40" "checksum byteorder 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "96c8b41881888cc08af32d47ac4edd52bc7fa27fef774be47a92443756451304" "checksum byteorder 1.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "74c0b906e9446b0a2e4f760cdb3fa4b2c48cdc6db8766a845c54b6ff063fd2e9" diff --git a/substrate/primitives/src/hash.rs b/substrate/primitives/src/hash.rs index 89f2961b95283..ab3221e63c2db 100644 --- a/substrate/primitives/src/hash.rs +++ b/substrate/primitives/src/hash.rs @@ -130,7 +130,7 @@ mod tests { } #[test] - fn test_h256() { + fn test_h256() { let tests = vec![ (Default::default(), "0x0000000000000000000000000000000000000000000000000000000000000000"), (H256::from(2), "0x0000000000000000000000000000000000000000000000000000000000000002"), diff --git a/substrate/runtime/primitives/Cargo.toml b/substrate/runtime/primitives/Cargo.toml index e7adc094d6789..f848c2fd374b3 100644 --- a/substrate/runtime/primitives/Cargo.toml +++ b/substrate/runtime/primitives/Cargo.toml @@ -15,6 +15,8 @@ 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 } log = {version = "0.3", optional = true } +base58 = "0.1" +byte-num = "0.1" [dev-dependencies] serde_json = "1.0" diff --git a/substrate/runtime/primitives/src/address_format.rs b/substrate/runtime/primitives/src/address_format.rs new file mode 100644 index 0000000000000..fd36cc0d7b474 --- /dev/null +++ b/substrate/runtime/primitives/src/address_format.rs @@ -0,0 +1,74 @@ +// 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 . + +use base58::ToBase58; +use byte_num::convert::IntoAscii; +use testing::Digest; +use substrate_primitives::H256; +use generic::Digest as GenericDigest; + + +#[derive(Clone, Debug)] +pub struct Base58Compatible ( pub Vec); + +pub trait Base58 : Into { + fn encode(self) -> String { + self.into().0.to_base58() + } +} + +impl From for Base58Compatible { + fn from(x: u64) -> Self { + Base58Compatible(x.itoa()) + } +} + +impl Base58 for u64 {} + +impl From for Base58Compatible { + fn from(x: Digest) -> Self { + Base58Compatible(x.logs.iter().map(|digest| digest.itoa()).flatten().collect()) + } +} +impl Base58 for Digest {} + +impl From for Base58Compatible { + fn from(x: H256) -> Self { + Base58Compatible(x[..].to_vec()) + } +} + +impl Base58 for H256 {} + +impl> From> for Base58Compatible where + Vec : From> +{ + fn from(x: GenericDigest) -> Self { + Base58Compatible(x.into()) + } +} + +impl> Base58 for GenericDigest where + Vec: From> +{} + +impl> From> for Vec +{ + fn from(x: GenericDigest) -> Self { + x.logs.into_iter().map(|element| Into::::into(element)).collect() + } + +} diff --git a/substrate/runtime/primitives/src/generic.rs b/substrate/runtime/primitives/src/generic.rs index f1a9448c8fafc..5316b9908c5ba 100644 --- a/substrate/runtime/primitives/src/generic.rs +++ b/substrate/runtime/primitives/src/generic.rs @@ -317,7 +317,7 @@ impl Encode for Header where impl traits::Header for Header where Number: Member + ::rstd::hash::Hash + Copy + Codec + MaybeDisplay + SimpleArithmetic + Codec, Hash: HashT, - DigestItem: Member + Default + Codec, + DigestItem: Member + Default + Codec + Into>, Hash::Output: Default + ::rstd::hash::Hash + Copy + Member + MaybeDisplay + SimpleBitOps + Codec, { type Number = Number; diff --git a/substrate/runtime/primitives/src/lib.rs b/substrate/runtime/primitives/src/lib.rs index a1ff760f7608f..555fc71761a50 100644 --- a/substrate/runtime/primitives/src/lib.rs +++ b/substrate/runtime/primitives/src/lib.rs @@ -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 base58; +extern crate byte_num; #[cfg(test)] extern crate serde_json; @@ -59,6 +61,7 @@ pub mod testing; pub mod traits; pub mod generic; pub mod bft; +pub mod address_format; use traits::{Verify, Lazy}; diff --git a/substrate/runtime/primitives/src/testing.rs b/substrate/runtime/primitives/src/testing.rs index eab978d3b8951..47f820dc9a7fd 100644 --- a/substrate/runtime/primitives/src/testing.rs +++ b/substrate/runtime/primitives/src/testing.rs @@ -21,6 +21,7 @@ use std::fmt::Debug; use codec::Codec; use runtime_support::AuxDispatchable; use traits::{self, Checkable, Applyable, BlakeTwo256}; +use address_format::Base58; pub use substrate_primitives::H256; diff --git a/substrate/runtime/primitives/src/traits.rs b/substrate/runtime/primitives/src/traits.rs index 45d2b5fdfafaf..169c5ded71da6 100644 --- a/substrate/runtime/primitives/src/traits.rs +++ b/substrate/runtime/primitives/src/traits.rs @@ -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::Base58; /// A lazy value. pub trait Lazy { diff --git a/substrate/runtime/staking/src/address.rs b/substrate/runtime/staking/src/address.rs index 36dfdbe1c2e70..b9656369af588 100644 --- a/substrate/runtime/staking/src/address.rs +++ b/substrate/runtime/staking/src/address.rs @@ -19,14 +19,15 @@ #[cfg(feature = "std")] use std::fmt; use super::{Member, Decode, Encode, As, Input, Output}; -use base58::ToBase58; +use primitives::address_format::{Base58, Base58Compatible}; + /// 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 where - AccountId: Member, - AccountIndex: Member, + AccountId: Member + Base58, + AccountIndex: Member + Base58, { /// It's an account ID (pubkey). #[cfg_attr(feature = "std", serde(deserialize_with="AccountId::deserialize"))] @@ -38,17 +39,18 @@ pub enum Address where #[cfg(feature = "std")] impl fmt::Display for Address where - AccountId: Member, - AccountIndex: Member, + AccountId: Member + Sized + Base58 + Into> + Default + fmt::Debug + Sync, + AccountIndex: Member + Sized + Base58 + Into> + fmt::Debug + Sync, + Vec: From { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { - write!(f, "{:?}", self.to_base58()) + write!(f, "{:?}", &self.clone().encode()) } } impl From for Address where - AccountId: Member, - AccountIndex: Member, + AccountId: Member + Base58, + AccountIndex: Member + Base58, { fn from(a: AccountId) -> Self { Address::Id(a) @@ -60,8 +62,8 @@ fn need_more_than(a: T, b: T) -> Option { } impl Decode for Address where - AccountId: Member + Decode, - AccountIndex: Member + Decode + PartialOrd + Ord + As + As + As + Copy, + AccountId: Member + Base58 + Decode, + AccountIndex: Member + Base58 + Decode + PartialOrd + Ord + As + As + As + Copy, { fn decode(input: &mut I) -> Option { Some(match input.read_byte()? { @@ -76,8 +78,8 @@ impl Decode for Address where } impl Encode for Address where - AccountId: Member + Encode, - AccountIndex: Member + Encode + PartialOrd + Ord + As + As + As + Copy, + AccountId: Member + Base58 + Encode, + AccountIndex: Member + Base58 + Encode + PartialOrd + Ord + As + As + As + Copy, { fn encode_to(&self, dest: &mut T) { match *self { @@ -103,23 +105,40 @@ impl Encode for Address where } impl Default for Address where - AccountId: Member + Default, - AccountIndex: Member, + AccountId: Member + Base58 + Default, + AccountIndex: Member + Base58, { fn default() -> Self { Address::Id(Default::default()) } } -impl ToBase58 for Address where - AccountId: Member + ToBase58, - AccountIndex: Member + ToBase58, +impl Into> for Address where + AccountId: Member + Base58 + Into> + Default + fmt::Debug + Sync, + AccountIndex: Member + Base58 + Into> + fmt::Debug + Sync, + Vec: From + { - fn to_base58(&self) -> String { + fn into(self) -> Vec { match self { - Address::Id(ref id) => id.to_base58(), - Address::Index(ref id) => id.to_base58(), - } + Address::Id(id) => Into::>::into(id), + Address::Index(id) => Into::>::into(id) + } + } +} + +impl Base58 for Address where + AccountId: Member + Base58 + Into> + Default + fmt::Debug + Sync, + AccountIndex: Member + Base58 + Into> + fmt::Debug + Sync, + Vec: From +{} - } +impl Into for Address where + AccountId: Member + Base58 + Into> + Default + fmt::Debug + Sync, + AccountIndex: Member + Base58 + Into> + fmt::Debug + Sync, + Vec: From +{ + fn into(self) -> Base58Compatible { + Base58Compatible(self.into()) + } } \ No newline at end of file diff --git a/substrate/runtime/staking/src/lib.rs b/substrate/runtime/staking/src/lib.rs index 7fd43e42c123e..b06657bdfd71d 100644 --- a/substrate/runtime/staking/src/lib.rs +++ b/substrate/runtime/staking/src/lib.rs @@ -55,6 +55,7 @@ use runtime_support::dispatch::Result; use session::OnSessionChange; use primitives::traits::{Zero, One, Bounded, RefInto, SimpleArithmetic, Executable, MakePayment, As, AuxLookup, Member, CheckedAdd, CheckedSub}; +use primitives::address_format::Base58; use address::Address as RawAddress; mod mock; @@ -105,7 +106,7 @@ pub trait Trait: system::Trait + session::Trait { type Balance: Parameter + SimpleArithmetic + Codec + Default + Copy + As + As + As; /// Type used for storing an account's index; implies the maximum number of accounts the system /// can hold. - type AccountIndex: Parameter + Member + Codec + SimpleArithmetic + As + As + As + As + As + Copy; + type AccountIndex: Parameter + Member + Codec + SimpleArithmetic + As + As + As + As + As + Copy + Base58; /// A function which is invoked when the given account is dead. /// /// Gives a chance to clean up resources associated with the given account. @@ -847,7 +848,9 @@ impl OnSessionChange for Module { } } -impl AuxLookup for Module { +impl AuxLookup for Module +where ::AccountId: primitives::address_format::Base58 +{ type Source = address::Address; type Target = T::AccountId; fn lookup(a: Self::Source) -> result::Result { diff --git a/substrate/runtime/system/src/lib.rs b/substrate/runtime/system/src/lib.rs index 60dd6059b4070..097b64b36f7dd 100644 --- a/substrate/runtime/system/src/lib.rs +++ b/substrate/runtime/system/src/lib.rs @@ -40,6 +40,7 @@ extern crate safe_mix; use rstd::prelude::*; use primitives::traits::{self, CheckEqual, SimpleArithmetic, SimpleBitOps, Zero, One, Bounded, Hash, Member, MaybeDisplay}; +use primitives::address_format::Base58; use runtime_support::{StorageValue, StorageMap, Parameter}; use safe_mix::TripletMix; @@ -68,7 +69,7 @@ pub trait Trait: Eq + Clone { type Hash: Parameter + Member + MaybeDisplay + SimpleBitOps + Default + Copy + CheckEqual + rstd::hash::Hash + AsRef<[u8]>; type Hashing: Hash; type Digest: Parameter + Member + Default + traits::Digest; - type AccountId: Parameter + Member + MaybeDisplay + Ord + Default; + type AccountId: Parameter + Member + MaybeDisplay + Ord + Default + Base58; type Header: Parameter + traits::Header< Number = Self::BlockNumber, Hash = Self::Hash, @@ -212,7 +213,7 @@ impl primitives::BuildStorage for GenesisConfig Ok(map![ Self::hash(&>::key_for(T::BlockNumber::zero())).to_vec() => [69u8; 32].encode(), - Self::hash(>::key()).to_vec() => 1u64.encode(), + Self::hash(>::key()).to_vec() => Encode::encode(&1u64), Self::hash(>::key()).to_vec() => [69u8; 32].encode(), Self::hash(>::key()).to_vec() => [0u8; 32].encode(), Self::hash(>::key()).to_vec() => [0u8; 4].encode() From 6ac29034947fce2ce833861013aaef7db60aea39 Mon Sep 17 00:00:00 2001 From: dylan_DPC Date: Wed, 22 Aug 2018 19:26:18 +0530 Subject: [PATCH 3/4] remove unwanted space in fn call --- substrate/primitives/src/hash.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/substrate/primitives/src/hash.rs b/substrate/primitives/src/hash.rs index ab3221e63c2db..89f2961b95283 100644 --- a/substrate/primitives/src/hash.rs +++ b/substrate/primitives/src/hash.rs @@ -130,7 +130,7 @@ mod tests { } #[test] - fn test_h256() { + fn test_h256() { let tests = vec![ (Default::default(), "0x0000000000000000000000000000000000000000000000000000000000000000"), (H256::from(2), "0x0000000000000000000000000000000000000000000000000000000000000002"), From 1691fc9d246f9aa398e8fcdd28d7221f2d2ec7da Mon Sep 17 00:00:00 2001 From: dylan_DPC Date: Mon, 3 Sep 2018 16:32:02 +0530 Subject: [PATCH 4/4] changes to ss58 and adds trait bounds --- Cargo.lock | 5 +- substrate/runtime-support/src/dispatch.rs | 31 ++++++++++ substrate/runtime/primitives/Cargo.toml | 2 +- .../runtime/primitives/src/address_format.rs | 39 ++++++------ substrate/runtime/primitives/src/lib.rs | 2 +- substrate/runtime/primitives/src/testing.rs | 2 +- substrate/runtime/primitives/src/traits.rs | 2 +- substrate/runtime/staking/Cargo.toml | 1 + substrate/runtime/staking/src/address.rs | 60 +++++++++---------- substrate/runtime/staking/src/lib.rs | 18 ++++-- substrate/runtime/system/src/lib.rs | 4 +- 11 files changed, 106 insertions(+), 60 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8a9b155fb2c6c..ce26093f6c1ad 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1923,7 +1923,7 @@ dependencies = [ "lazy_static 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.41 (registry+https://github.com/rust-lang/crates.io-index)", "num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)", - "rand 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -2782,8 +2782,8 @@ dependencies = [ name = "substrate-runtime-primitives" version = "0.1.0" dependencies = [ - "base58 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "byte-num 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "ed25519 0.1.0", "integer-sqrt 0.1.0 (git+https://github.com/paritytech/integer-sqrt-rs.git)", "log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", "num-traits 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2836,6 +2836,7 @@ name = "substrate-runtime-staking" version = "0.1.0" dependencies = [ "base58 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "ed25519 0.1.0", "hex-literal 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "safe-mix 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.70 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/substrate/runtime-support/src/dispatch.rs b/substrate/runtime-support/src/dispatch.rs index 9b26493839bff..aab0e3b551e2c 100644 --- a/substrate/runtime-support/src/dispatch.rs +++ b/substrate/runtime-support/src/dispatch.rs @@ -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>; diff --git a/substrate/runtime/primitives/Cargo.toml b/substrate/runtime/primitives/Cargo.toml index f848c2fd374b3..d622094cdb9db 100644 --- a/substrate/runtime/primitives/Cargo.toml +++ b/substrate/runtime/primitives/Cargo.toml @@ -14,8 +14,8 @@ 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 } -base58 = "0.1" byte-num = "0.1" [dev-dependencies] diff --git a/substrate/runtime/primitives/src/address_format.rs b/substrate/runtime/primitives/src/address_format.rs index fd36cc0d7b474..7c62f868522f7 100644 --- a/substrate/runtime/primitives/src/address_format.rs +++ b/substrate/runtime/primitives/src/address_format.rs @@ -14,54 +14,59 @@ // You should have received a copy of the GNU General Public License // along with Substrate Demo. If not, see . -use base58::ToBase58; 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 Base58Compatible ( pub Vec); +pub struct SS58Compatible ( pub Public); -pub trait Base58 : Into { +pub trait SS58: Into { fn encode(self) -> String { - self.into().0.to_base58() + self.into().0.to_ss58check() } } -impl From for Base58Compatible { +impl From for SS58Compatible { fn from(x: u64) -> Self { - Base58Compatible(x.itoa()) + SS58Compatible (Public::from_slice(&x.itoa())) } } -impl Base58 for u64 {} +impl SS58 for u64 {} -impl From for Base58Compatible { +impl From for SS58Compatible { fn from(x: Digest) -> Self { - Base58Compatible(x.logs.iter().map(|digest| digest.itoa()).flatten().collect()) + SS58Compatible (Public::from_slice(&x + .logs + .iter() + .map(|digest| digest.itoa()) + .flatten() + .collect::>() + )) } } -impl Base58 for Digest {} +impl SS58 for Digest {} -impl From for Base58Compatible { +impl From for SS58Compatible { fn from(x: H256) -> Self { - Base58Compatible(x[..].to_vec()) + SS58Compatible (Public::from_slice(&x[..])) } } -impl Base58 for H256 {} +impl SS58 for H256 {} -impl> From> for Base58Compatible where +impl> From> for SS58Compatible where Vec : From> { fn from(x: GenericDigest) -> Self { - Base58Compatible(x.into()) + SS58Compatible (Public::from_slice(Vec::from(x.logs).as_slice())) } } -impl> Base58 for GenericDigest where +impl> SS58 for GenericDigest where Vec: From> {} diff --git a/substrate/runtime/primitives/src/lib.rs b/substrate/runtime/primitives/src/lib.rs index 555fc71761a50..08d6d0a2b4d56 100644 --- a/substrate/runtime/primitives/src/lib.rs +++ b/substrate/runtime/primitives/src/lib.rs @@ -40,7 +40,7 @@ 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 base58; +extern crate ed25519; extern crate byte_num; #[cfg(test)] diff --git a/substrate/runtime/primitives/src/testing.rs b/substrate/runtime/primitives/src/testing.rs index 47f820dc9a7fd..2415d819aacfb 100644 --- a/substrate/runtime/primitives/src/testing.rs +++ b/substrate/runtime/primitives/src/testing.rs @@ -21,7 +21,7 @@ use std::fmt::Debug; use codec::Codec; use runtime_support::AuxDispatchable; use traits::{self, Checkable, Applyable, BlakeTwo256}; -use address_format::Base58; +use address_format::SS58; pub use substrate_primitives::H256; diff --git a/substrate/runtime/primitives/src/traits.rs b/substrate/runtime/primitives/src/traits.rs index 169c5ded71da6..4fa6fbd5399f9 100644 --- a/substrate/runtime/primitives/src/traits.rs +++ b/substrate/runtime/primitives/src/traits.rs @@ -27,7 +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::Base58; +use address_format::SS58; /// A lazy value. pub trait Lazy { diff --git a/substrate/runtime/staking/Cargo.toml b/substrate/runtime/staking/Cargo.toml index ccdb21053a6a9..fd896ca0eb389 100644 --- a/substrate/runtime/staking/Cargo.toml +++ b/substrate/runtime/staking/Cargo.toml @@ -21,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" diff --git a/substrate/runtime/staking/src/address.rs b/substrate/runtime/staking/src/address.rs index b9656369af588..1576aa6af5a43 100644 --- a/substrate/runtime/staking/src/address.rs +++ b/substrate/runtime/staking/src/address.rs @@ -19,15 +19,15 @@ #[cfg(feature = "std")] use std::fmt; use super::{Member, Decode, Encode, As, Input, Output}; -use primitives::address_format::{Base58, Base58Compatible}; - +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 where - AccountId: Member + Base58, - AccountIndex: Member + Base58, + AccountId: Member + fmt::Display + Into, + AccountIndex: Member + fmt::Display + Into, { /// It's an account ID (pubkey). #[cfg_attr(feature = "std", serde(deserialize_with="AccountId::deserialize"))] @@ -39,9 +39,9 @@ pub enum Address where #[cfg(feature = "std")] impl fmt::Display for Address where - AccountId: Member + Sized + Base58 + Into> + Default + fmt::Debug + Sync, - AccountIndex: Member + Sized + Base58 + Into> + fmt::Debug + Sync, - Vec: From + AccountId: Member + Sized + fmt::Display + Into + Default + fmt::Debug + Sync, + AccountIndex: Member + Sized + fmt::Display + Into + fmt::Debug + Sync, + Public: From { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { write!(f, "{:?}", &self.clone().encode()) @@ -49,8 +49,8 @@ impl fmt::Display for Address } impl From for Address where - AccountId: Member + Base58, - AccountIndex: Member + Base58, + AccountId: Member + fmt::Display + Into, + AccountIndex: Member + fmt::Display + Into, { fn from(a: AccountId) -> Self { Address::Id(a) @@ -62,8 +62,8 @@ fn need_more_than(a: T, b: T) -> Option { } impl Decode for Address where - AccountId: Member + Base58 + Decode, - AccountIndex: Member + Base58 + Decode + PartialOrd + Ord + As + As + As + Copy, + AccountId: Member + fmt::Display + Decode, + AccountIndex: Member + fmt::Display + Decode + PartialOrd + Ord + As + As + As + Copy, { fn decode(input: &mut I) -> Option { Some(match input.read_byte()? { @@ -78,8 +78,8 @@ impl Decode for Address where } impl Encode for Address where - AccountId: Member + Base58 + Encode, - AccountIndex: Member + Base58 + Encode + PartialOrd + Ord + As + As + As + Copy, + AccountId: Member + fmt::Display + Encode, + AccountIndex: Member + fmt::Display + Encode + PartialOrd + Ord + As + As + As + Copy, { fn encode_to(&self, dest: &mut T) { match *self { @@ -105,40 +105,40 @@ impl Encode for Address where } impl Default for Address where - AccountId: Member + Base58 + Default, - AccountIndex: Member + Base58, + AccountId: Member + fmt::Display + Into + Default, + AccountIndex: Member + fmt::Display + Into, { fn default() -> Self { Address::Id(Default::default()) } } -impl Into> for Address where - AccountId: Member + Base58 + Into> + Default + fmt::Debug + Sync, - AccountIndex: Member + Base58 + Into> + fmt::Debug + Sync, - Vec: From +impl Into for Address where + AccountId: Member + fmt::Display + Into + Default + fmt::Debug + Sync, + AccountIndex: Member + fmt::Display + Into + fmt::Debug + Sync, + { - fn into(self) -> Vec { + fn into(self) -> Public { match self { - Address::Id(id) => Into::>::into(id), - Address::Index(id) => Into::>::into(id) + Address::Id(id) => Into::::into(id), + Address::Index(id) => Into::::into(id) } } } -impl Base58 for Address where - AccountId: Member + Base58 + Into> + Default + fmt::Debug + Sync, - AccountIndex: Member + Base58 + Into> + fmt::Debug + Sync, +impl SS58 for Address where + AccountId: Member + fmt::Display + Into + Default + fmt::Debug + Sync, + AccountIndex: Member + fmt::Display + Into + fmt::Debug + Sync, Vec: From {} -impl Into for Address where - AccountId: Member + Base58 + Into> + Default + fmt::Debug + Sync, - AccountIndex: Member + Base58 + Into> + fmt::Debug + Sync, +impl Into for Address where + AccountId: Member + fmt::Display + Into + Default + fmt::Debug + Sync, + AccountIndex: Member + fmt::Display + Into + fmt::Debug + Sync, Vec: From { - fn into(self) -> Base58Compatible { - Base58Compatible(self.into()) + fn into(self) -> SS58Compatible { + SS58Compatible(self.into()) } } \ No newline at end of file diff --git a/substrate/runtime/staking/src/lib.rs b/substrate/runtime/staking/src/lib.rs index b06657bdfd71d..01f0d64b27164 100644 --- a/substrate/runtime/staking/src/lib.rs +++ b/substrate/runtime/staking/src/lib.rs @@ -45,6 +45,7 @@ extern crate substrate_runtime_sandbox as sandbox; extern crate substrate_runtime_session as session; extern crate substrate_runtime_system as system; extern crate substrate_runtime_timestamp as timestamp; +extern crate ed25519; #[cfg(test)] use std::fmt::Debug; use rstd::prelude::*; @@ -55,8 +56,9 @@ use runtime_support::dispatch::Result; use session::OnSessionChange; use primitives::traits::{Zero, One, Bounded, RefInto, SimpleArithmetic, Executable, MakePayment, As, AuxLookup, Member, CheckedAdd, CheckedSub}; -use primitives::address_format::Base58; +use primitives::address_format::{SS58, SS58Compatible}; use address::Address as RawAddress; +use std::fmt; mod mock; @@ -106,7 +108,7 @@ pub trait Trait: system::Trait + session::Trait { type Balance: Parameter + SimpleArithmetic + Codec + Default + Copy + As + As + As; /// Type used for storing an account's index; implies the maximum number of accounts the system /// can hold. - type AccountIndex: Parameter + Member + Codec + SimpleArithmetic + As + As + As + As + As + Copy + Base58; + type AccountIndex: Parameter + Member + Codec + SimpleArithmetic + As + As + As + As + As + Copy + fmt::Display; /// A function which is invoked when the given account is dead. /// /// Gives a chance to clean up resources associated with the given account. @@ -117,7 +119,11 @@ decl_module! { pub struct Module; #[cfg_attr(feature = "std", derive(Serialize, Deserialize))] - pub enum Call where aux: T::PublicAux { + pub enum Call + where + aux: T::PublicAux, + SS58Compatible: From<::AccountId> + From<::AccountIndex> + { fn transfer(aux, dest: RawAddress, value: T::Balance) -> Result = 0; fn stake(aux) -> Result = 1; fn unstake(aux, index: u32) -> Result = 2; @@ -236,7 +242,9 @@ pub enum UpdateBalanceOutcome { AccountKilled, } -impl Module { +impl Module +where SS58Compatible: From +{ // PUBLIC IMMUTABLES @@ -849,7 +857,7 @@ impl OnSessionChange for Module { } impl AuxLookup for Module -where ::AccountId: primitives::address_format::Base58 +where SS58Compatible: From<::AccountId> + From<::AccountIndex> { type Source = address::Address; type Target = T::AccountId; diff --git a/substrate/runtime/system/src/lib.rs b/substrate/runtime/system/src/lib.rs index 097b64b36f7dd..1e83b4130dfc0 100644 --- a/substrate/runtime/system/src/lib.rs +++ b/substrate/runtime/system/src/lib.rs @@ -40,7 +40,7 @@ extern crate safe_mix; use rstd::prelude::*; use primitives::traits::{self, CheckEqual, SimpleArithmetic, SimpleBitOps, Zero, One, Bounded, Hash, Member, MaybeDisplay}; -use primitives::address_format::Base58; +use primitives::address_format::SS58Compatible; use runtime_support::{StorageValue, StorageMap, Parameter}; use safe_mix::TripletMix; @@ -69,7 +69,7 @@ pub trait Trait: Eq + Clone { type Hash: Parameter + Member + MaybeDisplay + SimpleBitOps + Default + Copy + CheckEqual + rstd::hash::Hash + AsRef<[u8]>; type Hashing: Hash; type Digest: Parameter + Member + Default + traits::Digest; - type AccountId: Parameter + Member + MaybeDisplay + Ord + Default + Base58; + type AccountId: Parameter + Member + MaybeDisplay + Ord + Default + Into; type Header: Parameter + traits::Header< Number = Self::BlockNumber, Hash = Self::Hash,