From 7d8ae2df5c07a1241da58e03f0f0b51b10515db1 Mon Sep 17 00:00:00 2001 From: Sergei Pepyakin Date: Wed, 6 Feb 2019 14:51:14 +0100 Subject: [PATCH] Indices maintenance (#1705) * Migrate the indicies crate to 2018 edition. * Use .cloned() instead of .map(|x| x.clone()) * Update documentation. --- substrate/srml/indices/Cargo.toml | 19 +++++++------- substrate/srml/indices/src/address.rs | 5 ++-- substrate/srml/indices/src/lib.rs | 36 ++++++++------------------- substrate/srml/indices/src/mock.rs | 7 +++--- substrate/srml/indices/src/tests.rs | 2 +- 5 files changed, 28 insertions(+), 41 deletions(-) diff --git a/substrate/srml/indices/Cargo.toml b/substrate/srml/indices/Cargo.toml index 32de3ae1c6..2e7e2b5e33 100644 --- a/substrate/srml/indices/Cargo.toml +++ b/substrate/srml/indices/Cargo.toml @@ -2,6 +2,7 @@ name = "srml-indices" version = "0.1.0" authors = ["Parity Technologies "] +edition = "2018" [dependencies] hex-literal = "0.1.0" @@ -10,12 +11,12 @@ safe-mix = { version = "1.0", default-features = false} parity-codec = { version = "3.0", default-features = false } parity-codec-derive = { version = "3.0", default-features = false } substrate-keyring = { path = "../../core/keyring", optional = true } -substrate-primitives = { path = "../../core/primitives", default-features = false } -sr-std = { path = "../../core/sr-std", default-features = false } -sr-io = { path = "../../core/sr-io", default-features = false } -sr-primitives = { path = "../../core/sr-primitives", default-features = false } +rstd = { package = "sr-std", path = "../../core/sr-std", default-features = false } +runtime-io = { package = "sr-io", path = "../../core/sr-io", default-features = false } +primitives = { package = "sr-primitives", path = "../../core/sr-primitives", default-features = false } srml-support = { path = "../support", default-features = false } -srml-system = { path = "../system", default-features = false } +system = { package = "srml-system", path = "../system", default-features = false } +substrate-primitives = { path = "../../core/primitives", default-features = false } [dev-dependencies] ref_thread_local = "0.0" @@ -29,9 +30,9 @@ std = [ "parity-codec/std", "parity-codec-derive/std", "substrate-primitives/std", - "sr-std/std", - "sr-io/std", + "rstd/std", + "runtime-io/std", "srml-support/std", - "sr-primitives/std", - "srml-system/std", + "primitives/std", + "system/std", ] diff --git a/substrate/srml/indices/src/address.rs b/substrate/srml/indices/src/address.rs index ccaa6b46a5..5e313883e0 100644 --- a/substrate/srml/indices/src/address.rs +++ b/substrate/srml/indices/src/address.rs @@ -18,9 +18,10 @@ #[cfg(feature = "std")] use std::fmt; -use super::{Member, Decode, Encode, As, Input, Output}; +use crate::{Member, Decode, Encode, As, Input, Output}; -/// A vetted and verified extrinsic from the external world. +/// An indices-aware address, which can be either a direct `AccountId` or +/// an index. #[derive(PartialEq, Eq, Clone)] #[cfg_attr(feature = "std", derive(Debug, Hash))] pub enum Address where diff --git a/substrate/srml/indices/src/lib.rs b/substrate/srml/indices/src/lib.rs index 3aff9a555f..3cbc87b6dc 100644 --- a/substrate/srml/indices/src/lib.rs +++ b/substrate/srml/indices/src/lib.rs @@ -14,43 +14,26 @@ // You should have received a copy of the GNU General Public License // along with Substrate. If not, see . -//! Balances: Handles setting and retrieval of free balance, -//! retrieving total balance, reserve and unreserve balance, -//! repatriating a reserved balance to a beneficiary account that exists, -//! transfering a balance between accounts (when not reserved), -//! slashing an account balance, account removal, rewards, -//! lookup of an index to reclaim an account (when not balance not reserved), -//! increasing total stake. +//! An index is a short form of an address. This module handles allocation +//! of indices for a newly created accounts. #![cfg_attr(not(feature = "std"), no_std)] +// We need these `extern crate` to be placed here otherwise there will be errors. +// TODO: https://github.com/paritytech/substrate/issues/1509 #[macro_use] extern crate srml_support as runtime_support; - -extern crate sr_std as rstd; - -#[macro_use] extern crate parity_codec_derive; - extern crate parity_codec as codec; -extern crate sr_primitives as primitives; -extern crate srml_system as system; - -#[cfg(test)] -#[macro_use] -extern crate ref_thread_local; -#[cfg(test)] -extern crate sr_io as runtime_io; -#[cfg(test)] -extern crate substrate_primitives; use rstd::{prelude::*, result, marker::PhantomData}; use codec::{Encode, Decode, Codec, Input, Output}; use runtime_support::{StorageValue, StorageMap, Parameter}; use primitives::traits::{One, SimpleArithmetic, As, StaticLookup, Member}; -use address::Address as RawAddress; use system::{IsDeadAccount, OnNewAccount}; +use self::address::Address as RawAddress; + mod mock; pub mod address; @@ -104,7 +87,10 @@ decl_event!( ::AccountId, ::AccountIndex { - /// A new account was created. + /// A new account index was assigned. + /// + /// This event is not triggered when an existing index is reassigned + /// to another `AccountId`. NewAccountIndex(AccountId, AccountIndex), } ); @@ -138,7 +124,7 @@ impl Module { let enum_set_size = Self::enum_set_size(); let set = Self::enum_set(index / enum_set_size); let i: usize = (index % enum_set_size).as_(); - set.get(i).map(|x| x.clone()) + set.get(i).cloned() } /// `true` if the account `index` is ready for reclaim. diff --git a/substrate/srml/indices/src/mock.rs b/substrate/srml/indices/src/mock.rs index 8c8305fef8..c8fe65151b 100644 --- a/substrate/srml/indices/src/mock.rs +++ b/substrate/srml/indices/src/mock.rs @@ -19,13 +19,12 @@ #![cfg(test)] use std::collections::HashSet; -use ref_thread_local::RefThreadLocal; +use ref_thread_local::{ref_thread_local, RefThreadLocal}; use primitives::BuildStorage; use primitives::testing::{Digest, DigestItem, Header}; use substrate_primitives::{H256, Blake2Hasher}; -use runtime_io; -use {GenesisConfig, Module, Trait, system}; -use super::{IsDeadAccount, OnNewAccount, ResolveHint}; +use {runtime_io, system}; +use crate::{GenesisConfig, Module, Trait, IsDeadAccount, OnNewAccount, ResolveHint}; impl_outer_origin!{ pub enum Origin for Runtime {} diff --git a/substrate/srml/indices/src/tests.rs b/substrate/srml/indices/src/tests.rs index f16a93470d..965f155f29 100644 --- a/substrate/srml/indices/src/tests.rs +++ b/substrate/srml/indices/src/tests.rs @@ -19,7 +19,7 @@ #![cfg(test)] use super::*; -use mock::{Indices, new_test_ext, make_account, kill_account, TestIsDeadAccount}; +use crate::mock::{Indices, new_test_ext, make_account, kill_account, TestIsDeadAccount}; use runtime_io::with_externalities; #[test]