diff --git a/node/src/chain_spec/gdev.rs b/node/src/chain_spec/gdev.rs index a846a23adf99bb7fb9358a95eeb37ae36949e116..29474919483db60f5df72e37620a8cc599c0964d 100644 --- a/node/src/chain_spec/gdev.rs +++ b/node/src/chain_spec/gdev.rs @@ -377,7 +377,7 @@ fn gen_genesis_for_local_chain( owner_key: owner_key.clone(), removable_on: 0, status: IdtyStatus::Validated, - data: (), + data: IdtyData::min(), }, }) .collect(), @@ -507,7 +507,7 @@ fn genesis_data_to_gdev_genesis_conf( owner_key: pubkey, removable_on: 0, status: IdtyStatus::Validated, - data: (), + data: IdtyData::min(), }, }) .collect(), diff --git a/pallets/universal-dividend/Cargo.toml b/pallets/universal-dividend/Cargo.toml index 8b939e5f7a307b0f8886f82668e39dab38fea024..27df84d6e81bf59ad29ba5cc982eddf8fd67510e 100644 --- a/pallets/universal-dividend/Cargo.toml +++ b/pallets/universal-dividend/Cargo.toml @@ -19,6 +19,7 @@ std = [ 'frame-support/std', 'frame-system/std', 'frame-benchmarking/std', + "serde", "sp-arithmetic/std", "sp-io/std", "sp-std/std", @@ -26,20 +27,15 @@ std = [ try-runtime = ['frame-support/try-runtime'] [dependencies] - -# substrate +# crates.io +codec = { package = 'parity-scale-codec', version = '2.3.1', default-features = false, features = ["derive", "max-encoded-len"] } scale-info = { version = "1.0", default-features = false, features = ["derive"] } +serde = { version = "1.0.101", features = ["derive"], optional = true } # substrate bencharks frame-benchmarking = { git = 'https://github.com/librelois/substrate.git', branch = 'duniter-monthly-2022-02', optional = true, default-features = false } pallet-balances = { git = 'https://github.com/librelois/substrate.git', branch = 'duniter-monthly-2022-02', optional = true, default-features = false } -[dependencies.codec] -default-features = false -features = ['derive'] -package = 'parity-scale-codec' -version = '2.3.1' - [dependencies.frame-support] default-features = false git = 'https://github.com/librelois/substrate.git' @@ -78,14 +74,13 @@ targets = ['x86_64-unknown-linux-gnu'] ### DEV ### +[dev-dependencies] +serde = { version = "1.0.101", features = ["derive"] } + [dev-dependencies.pallet-balances] git = 'https://github.com/librelois/substrate.git' branch = 'duniter-monthly-2022-02' -[dev-dependencies.serde] -features = ["derive"] -version = '1.0.119' - [dev-dependencies.sp-core] git = 'https://github.com/librelois/substrate.git' branch = 'duniter-monthly-2022-02' diff --git a/pallets/universal-dividend/src/lib.rs b/pallets/universal-dividend/src/lib.rs index ab6fa536122d0d04d2888e621dbe330fa69b2560..0d19ab291d8f1bfbfac922747998f8418b70cb3b 100644 --- a/pallets/universal-dividend/src/lib.rs +++ b/pallets/universal-dividend/src/lib.rs @@ -21,9 +21,11 @@ mod benchmarking; mod mock; #[cfg(test)] mod tests; +mod types; mod weights; pub use pallet::*; +pub use types::FirstEligibleUd; pub use weights::WeightInfo; use frame_support::traits::{tokens::ExistenceRequirement, Currency}; diff --git a/pallets/universal-dividend/src/types.rs b/pallets/universal-dividend/src/types.rs new file mode 100644 index 0000000000000000000000000000000000000000..2ce0b9a5a735491b967f19ce9061cbc828d0e93f --- /dev/null +++ b/pallets/universal-dividend/src/types.rs @@ -0,0 +1,89 @@ +// Copyright 2021 Axiom-Team +// +// This file is part of Substrate-Libre-Currency. +// +// Substrate-Libre-Currency is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, version 3 of the License. +// +// Substrate-Libre-Currency 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with Substrate-Libre-Currency. If not, see <https://www.gnu.org/licenses/>. + +use codec::{Decode, Encode, Error, Input, MaxEncodedLen, Output}; +use core::num::NonZeroU16; +#[cfg(feature = "std")] +use serde::{Deserialize, Serialize}; +use sp_std::vec::Vec; + +type UdIndex = u16; + +#[cfg_attr(feature = "std", derive(Deserialize, Serialize))] +#[derive(Clone, Copy, Default, Eq, PartialEq)] +pub struct FirstEligibleUd(pub Option<NonZeroU16>); + +#[cfg(feature = "std")] +impl FirstEligibleUd { + pub fn min() -> Self { + Self(Some(NonZeroU16::new(1).expect("unreachable"))) + } +} + +impl From<FirstEligibleUd> for Option<UdIndex> { + fn from(first_eligible_ud: FirstEligibleUd) -> Self { + first_eligible_ud.0.map(|ud_index| ud_index.get()) + } +} + +impl Encode for FirstEligibleUd { + fn size_hint(&self) -> usize { + self.as_u16().size_hint() + } + + fn encode_to<W: Output + ?Sized>(&self, dest: &mut W) { + self.as_u16().encode_to(dest) + } + + fn encode(&self) -> Vec<u8> { + self.as_u16().encode() + } + + fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R { + self.as_u16().using_encoded(f) + } +} + +impl Decode for FirstEligibleUd { + fn decode<I: Input>(input: &mut I) -> Result<Self, Error> { + Ok(match NonZeroU16::new(Decode::decode(input)?) { + Some(non_zero_u16) => Self(Some(non_zero_u16)), + None => Self(None), + }) + } +} + +impl MaxEncodedLen for FirstEligibleUd { + fn max_encoded_len() -> usize { + u16::max_encoded_len() + } +} + +impl scale_info::TypeInfo for FirstEligibleUd { + type Identity = UdIndex; + + fn type_info() -> scale_info::Type { + Self::Identity::type_info() + } +} + +impl FirstEligibleUd { + // private + #[inline(always)] + fn as_u16(&self) -> UdIndex { + self.0.map(|ud_index| ud_index.get()).unwrap_or_default() + } +} diff --git a/runtime/common/src/lib.rs b/runtime/common/src/lib.rs index 2763a8c2ad0afa4e31995ac36a086466bfc296a9..96daf2186c4e7e253b24d34ca0fc5c3ac4e168d3 100644 --- a/runtime/common/src/lib.rs +++ b/runtime/common/src/lib.rs @@ -54,15 +54,18 @@ pub type Hash = sp_core::H256; /// Block header type pub type Header = sp_runtime::generic::Header<BlockNumber, sp_runtime::traits::BlakeTwo256>; -/// Index of an identity -pub type IdtyIndex = u32; - /// Index of a transaction in the chain. pub type Index = u32; /// Alias to 512-bit hash when used in the context of a transaction signature on the chain. pub type Signature = sp_runtime::MultiSignature; +/// Index of an identity +pub type IdtyIndex = u32; + +/// Identity data +pub type IdtyData = pallet_universal_dividend::FirstEligibleUd; + pub struct FullIdentificationOfImpl; impl sp_runtime::traits::Convert<AccountId, Option<entities::ValidatorFullIdentification>> for FullIdentificationOfImpl diff --git a/runtime/common/src/pallets_config.rs b/runtime/common/src/pallets_config.rs index 620f73597eac9c4abc12e271984de78c57e20f1d..fbc1cf7d9301f0261a56a68e150a28fb8420b208 100644 --- a/runtime/common/src/pallets_config.rs +++ b/runtime/common/src/pallets_config.rs @@ -416,7 +416,7 @@ macro_rules! pallets_config { type Event = Event; type EnsureIdtyCallAllowed = Wot; type IdtyCreationPeriod = IdtyCreationPeriod; - type IdtyData = (); + type IdtyData = IdtyData; type IdtyIndex = IdtyIndex; type IdtyNameValidator = IdtyNameValidatorImpl; type IdtyValidationOrigin = EnsureRoot<Self::AccountId>; diff --git a/runtime/g1/src/lib.rs b/runtime/g1/src/lib.rs index c2918966648fe12604e584f4d034dda55b7a7ac1..66c3239ba86c86fb5d03478c12b324009b816c5c 100644 --- a/runtime/g1/src/lib.rs +++ b/runtime/g1/src/lib.rs @@ -27,7 +27,8 @@ pub mod parameters; pub use self::parameters::*; pub use common_runtime::{ constants::*, entities::*, handlers::*, AccountId, Address, Balance, BlockNumber, - FullIdentificationOfImpl, GetCurrentEpochIndex, Hash, Header, IdtyIndex, Index, Signature, + FullIdentificationOfImpl, GetCurrentEpochIndex, Hash, Header, IdtyData, IdtyIndex, Index, + Signature, }; pub use pallet_balances::Call as BalancesCall; pub use pallet_identity::{IdtyStatus, IdtyValue}; diff --git a/runtime/gdev/src/lib.rs b/runtime/gdev/src/lib.rs index 594091a2a995be3a79f67266ca674b125151d39f..2b4c002484eb9bf9703fa2f925e13884bec2ee98 100644 --- a/runtime/gdev/src/lib.rs +++ b/runtime/gdev/src/lib.rs @@ -31,7 +31,8 @@ pub mod parameters; pub use self::parameters::*; pub use common_runtime::{ constants::*, entities::*, handlers::*, AccountId, Address, Balance, BlockNumber, - FullIdentificationOfImpl, GetCurrentEpochIndex, Hash, Header, IdtyIndex, Index, Signature, + FullIdentificationOfImpl, GetCurrentEpochIndex, Hash, Header, IdtyData, IdtyIndex, Index, + Signature, }; pub use pallet_balances::Call as BalancesCall; pub use pallet_duniter_test_parameters::Parameters as GenesisParameters; diff --git a/runtime/gtest/src/lib.rs b/runtime/gtest/src/lib.rs index e134dc461098a27f7930e47c83b68e49940554fd..862a545658a07265f7b4ffe05399a68185fe32ec 100644 --- a/runtime/gtest/src/lib.rs +++ b/runtime/gtest/src/lib.rs @@ -27,7 +27,8 @@ pub mod parameters; pub use self::parameters::*; pub use common_runtime::{ constants::*, entities::*, handlers::*, AccountId, Address, Balance, BlockNumber, - FullIdentificationOfImpl, GetCurrentEpochIndex, Hash, Header, IdtyIndex, Index, Signature, + FullIdentificationOfImpl, GetCurrentEpochIndex, Hash, Header, IdtyData, IdtyIndex, Index, + Signature, }; pub use pallet_balances::Call as BalancesCall; pub use pallet_identity::{IdtyStatus, IdtyValue};