From 66ffb479af9852bdcb9cb504979d452d7a676359 Mon Sep 17 00:00:00 2001 From: librelois <c@elo.tf> Date: Fri, 13 May 2022 22:33:49 +0200 Subject: [PATCH] feat: add treasury --- Cargo.lock | 23 +++++ node/src/chain_spec/gdev.rs | 2 + pallets/duniter-account/Cargo.toml | 7 ++ pallets/duniter-account/src/lib.rs | 106 +++++++++++++++--------- runtime/common/Cargo.toml | 7 ++ runtime/common/src/handlers.rs | 15 ++++ runtime/common/src/pallets_config.rs | 31 ++++++- runtime/g1/Cargo.toml | 11 ++- runtime/g1/src/lib.rs | 4 + runtime/g1/src/parameters.rs | 12 +++ runtime/gdev/Cargo.toml | 8 ++ runtime/gdev/src/lib.rs | 9 +- runtime/gdev/src/parameters.rs | 12 +++ runtime/gdev/tests/integration_tests.rs | 90 +++++++++++++++++++- runtime/gtest/Cargo.toml | 11 ++- runtime/gtest/src/lib.rs | 4 + runtime/gtest/src/parameters.rs | 12 +++ 17 files changed, 312 insertions(+), 52 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2bdafcac4..fe2675c91 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -840,6 +840,7 @@ dependencies = [ "pallet-membership", "pallet-provide-randomness", "pallet-session", + "pallet-treasury", "pallet-ud-accounts-storage", "parity-scale-codec", "scale-info", @@ -1976,6 +1977,7 @@ dependencies = [ "pallet-timestamp", "pallet-transaction-payment", "pallet-transaction-payment-rpc-runtime-api", + "pallet-treasury", "pallet-ud-accounts-storage", "pallet-universal-dividend", "pallet-utility", @@ -2037,6 +2039,7 @@ dependencies = [ "pallet-timestamp", "pallet-transaction-payment", "pallet-transaction-payment-rpc-runtime-api", + "pallet-treasury", "pallet-ud-accounts-storage", "pallet-universal-dividend", "pallet-upgrade-origin", @@ -2240,6 +2243,7 @@ dependencies = [ "pallet-timestamp", "pallet-transaction-payment", "pallet-transaction-payment-rpc-runtime-api", + "pallet-treasury", "pallet-ud-accounts-storage", "pallet-universal-dividend", "pallet-utility", @@ -4440,9 +4444,11 @@ dependencies = [ "frame-benchmarking", "frame-support", "frame-system", + "log", "maplit", "pallet-balances", "pallet-provide-randomness", + "pallet-treasury", "parity-scale-codec", "scale-info", "serde", @@ -4747,6 +4753,23 @@ dependencies = [ "sp-runtime", ] +[[package]] +name = "pallet-treasury" +version = "4.0.0-dev" +source = "git+https://github.com/librelois/substrate.git?branch=duniter-monthly-2022-02#8fbc011c06ee051577022c8fd84f2a018123efd3" +dependencies = [ + "frame-benchmarking", + "frame-support", + "frame-system", + "impl-trait-for-tuples", + "pallet-balances", + "parity-scale-codec", + "scale-info", + "serde", + "sp-runtime", + "sp-std", +] + [[package]] name = "pallet-ud-accounts-storage" version = "3.0.0" diff --git a/node/src/chain_spec/gdev.rs b/node/src/chain_spec/gdev.rs index 70ff7bc0a..fefa4d44c 100644 --- a/node/src/chain_spec/gdev.rs +++ b/node/src/chain_spec/gdev.rs @@ -378,6 +378,7 @@ fn gen_genesis_conf( first_ud: 1_000, initial_monetary_mass: 0, }, + treasury: Default::default(), } } @@ -482,5 +483,6 @@ fn genesis_data_to_gdev_genesis_conf( first_ud, initial_monetary_mass, }, + treasury: Default::default(), } } diff --git a/pallets/duniter-account/Cargo.toml b/pallets/duniter-account/Cargo.toml index c4f48360f..0bb55aa80 100644 --- a/pallets/duniter-account/Cargo.toml +++ b/pallets/duniter-account/Cargo.toml @@ -19,6 +19,7 @@ std = [ 'frame-benchmarking/std', 'pallet-balances/std', 'pallet-provide-randomness/std', + 'pallet-treasury/std', 'serde', 'sp-core/std', 'sp-io/std', @@ -33,6 +34,7 @@ pallet-provide-randomness = { path = "../provide-randomness", default-features = # crates.io codec = { package = 'parity-scale-codec', version = "2.3.1", default-features = false, features = ["derive"] } +log = { version = "0.4.14", default-features = false } scale-info = { version = "1.0", default-features = false, features = ["derive"] } # substrate @@ -57,6 +59,11 @@ default-features = false git = 'https://github.com/librelois/substrate.git' branch = 'duniter-monthly-2022-02' +[dependencies.pallet-treasury] +default-features = false +git = 'https://github.com/librelois/substrate.git' +branch = 'duniter-monthly-2022-02' + [dependencies.serde] version = "1.0.101" optional = true diff --git a/pallets/duniter-account/src/lib.rs b/pallets/duniter-account/src/lib.rs index e43e2a91e..4415778cc 100644 --- a/pallets/duniter-account/src/lib.rs +++ b/pallets/duniter-account/src/lib.rs @@ -49,6 +49,7 @@ pub mod pallet { frame_system::Config<AccountData = AccountData<Self::Balance>> + pallet_balances::Config + pallet_provide_randomness::Config<Currency = pallet_balances::Pallet<Self>> + + pallet_treasury::Config<Currency = pallet_balances::Pallet<Self>> { type AccountIdToSalt: Convert<Self::AccountId, [u8; 32]>; /// The overarching event type. @@ -89,6 +90,16 @@ pub mod pallet { #[pallet::genesis_build] impl<T: Config> GenesisBuild<T> for GenesisConfig<T> { fn build(&self) { + // Treasury + frame_system::Account::<T>::mutate( + pallet_treasury::Pallet::<T>::account_id(), + |account| { + account.data.random_id = None; + account.data.free = T::ExistentialDeposit::get(); + account.providers = 1; + }, + ); + // Classic accounts for ( account_id, GenesisAccountData { @@ -118,12 +129,16 @@ pub mod pallet { #[pallet::event] #[pallet::generate_deposit(pub(super) fn deposit_event)] pub enum Event<T: Config> { + /// Force the destruction of an account because its free balance is insufficient to pay + /// the account creation price. + /// [who, balance] + ForceDestroy { + who: T::AccountId, + balance: T::Balance, + }, /// Random id assigned /// [account_id, random_id] - RandomIdAssigned { - account_id: T::AccountId, - random_id: H256, - }, + RandomIdAssigned { who: T::AccountId, random_id: H256 }, } // HOOKS // @@ -145,47 +160,58 @@ pub mod pallet { total_weight += 100_000; } else { // If the account is not self-sufficient, it must pay the account creation fees - frame_system::Pallet::<T>::inc_providers(&account_id); - let res = T::Currency::withdraw( - &account_id, - T::NewAccountPrice::get(), - frame_support::traits::WithdrawReasons::FEE, - ExistenceRequirement::KeepAlive, - ); - if let Ok(imbalance) = res { - // The fees have been succesfully collected, we should: - // 1. Increment consumers to prevent the destruction of the account before + let account_data = frame_system::Pallet::<T>::get(&account_id); + let price = T::NewAccountPrice::get(); + if account_data.free > price { + // The account can pay the new account price, we should: + // 1. Increment providers to create the account for frame_system point of view + // 2. Withdraw the "new account price" amount + // 3. Increment consumers to prevent the destruction of the account before // the random id is assigned - // 2. Manage the funds collected - // 3. Submit random id generation request - // 4. Save the id of the random generation request. - let res = - frame_system::Pallet::<T>::inc_consumers_without_limit(&account_id); + // 4. Manage the funds collected + // 5. Submit random id generation request + // 6. Save the id of the random generation request. + frame_system::Pallet::<T>::inc_providers(&account_id); + let res = <pallet_balances::Pallet<T> as Currency<T::AccountId>>::withdraw( + &account_id, + price, + frame_support::traits::WithdrawReasons::FEE, + ExistenceRequirement::KeepAlive, + ); debug_assert!( res.is_ok(), - "Cannot fail because providers are incremented just before" - ); - T::OnUnbalanced::on_unbalanced(imbalance); - let request_id = pallet_provide_randomness::Pallet::<T>::force_request( - pallet_provide_randomness::RandomnessType::RandomnessFromTwoEpochsAgo, - H256(T::AccountIdToSalt::convert(account_id.clone())), + "Cannot fail because we checked that the free balance was sufficient" ); - PendingRandomIdAssignments::<T>::insert(request_id, account_id); - total_weight += 200_000; + if let Ok(imbalance) = res { + let res = + frame_system::Pallet::<T>::inc_consumers_without_limit(&account_id); + debug_assert!( + res.is_ok(), + "Cannot fail because providers are incremented just before" + ); + T::OnUnbalanced::on_unbalanced(imbalance); + let request_id = pallet_provide_randomness::Pallet::<T>::force_request( + pallet_provide_randomness::RandomnessType::RandomnessFromTwoEpochsAgo, + H256(T::AccountIdToSalt::convert(account_id.clone())), + ); + PendingRandomIdAssignments::<T>::insert(request_id, account_id); + total_weight += 200_000; + } } else { // The charges could not be deducted, we slash the account - let res = frame_system::Pallet::<T>::dec_providers(&account_id); - debug_assert!( - res.is_ok(), - "Cannot fail because providers are incremented just before" - ); - let account_data = frame_system::Pallet::<T>::get(&account_id); - let (imbalance, rest) = pallet_balances::Pallet::<T>::slash( - &account_id, - account_data.free.saturating_add(account_data.reserved), - ); - debug_assert!(rest.is_zero()); - T::OnUnbalanced::on_unbalanced(imbalance); + let balance_to_suppr = + account_data.free.saturating_add(account_data.reserved); + // Force account data supression + frame_system::Account::<T>::mutate(&account_id, |a| { + a.data.set_balances(Default::default()) + }); + Self::deposit_event(Event::ForceDestroy { + who: account_id, + balance: balance_to_suppr, + }); + T::OnUnbalanced::on_unbalanced(pallet_balances::NegativeImbalance::new( + balance_to_suppr, + )); total_weight += 300_000; } } @@ -206,7 +232,7 @@ where account.data.random_id = Some(randomness); }); Self::deposit_event(Event::RandomIdAssigned { - account_id, + who: account_id, random_id: randomness, }); 200_000 diff --git a/runtime/common/Cargo.toml b/runtime/common/Cargo.toml index 9d4a1b324..0cceb7f4a 100644 --- a/runtime/common/Cargo.toml +++ b/runtime/common/Cargo.toml @@ -14,6 +14,7 @@ runtime-benchmarks = [ 'pallet-duniter-wot/runtime-benchmarks', 'pallet-identity/runtime-benchmarks', 'pallet-membership/runtime-benchmarks', + 'pallet-treasury/std', 'pallet-ud-accounts-storage/runtime-benchmarks', 'sp-runtime/runtime-benchmarks', ] @@ -31,6 +32,7 @@ std = [ 'pallet-identity/std', 'pallet-membership/std', 'pallet-provide-randomness/std', + 'pallet-treasury/std', 'pallet-ud-accounts-storage/std', 'serde', 'sp-arithmetic/std', @@ -90,6 +92,11 @@ features = ["historical"] git = 'https://github.com/librelois/substrate.git' branch = 'duniter-monthly-2022-02' +[dependencies.pallet-treasury] +default-features = false +git = 'https://github.com/librelois/substrate.git' +branch = 'duniter-monthly-2022-02' + [dependencies.serde] version = "1.0.101" optional = true diff --git a/runtime/common/src/handlers.rs b/runtime/common/src/handlers.rs index b9731cd6b..7e5587f55 100644 --- a/runtime/common/src/handlers.rs +++ b/runtime/common/src/handlers.rs @@ -188,3 +188,18 @@ where 0 } } + +pub struct TreasurySpendFunds<Runtime>(core::marker::PhantomData<Runtime>); +impl<Runtime> pallet_treasury::SpendFunds<Runtime> for TreasurySpendFunds<Runtime> +where + Runtime: pallet_treasury::Config, +{ + fn spend_funds( + _budget_remaining: &mut pallet_treasury::BalanceOf<Runtime>, + _imbalance: &mut pallet_treasury::PositiveImbalanceOf<Runtime>, + _total_weight: &mut Weight, + missed_any: &mut bool, + ) { + *missed_any = true; + } +} diff --git a/runtime/common/src/pallets_config.rs b/runtime/common/src/pallets_config.rs index 9ae9bbbd2..0ab744c6f 100644 --- a/runtime/common/src/pallets_config.rs +++ b/runtime/common/src/pallets_config.rs @@ -155,7 +155,7 @@ macro_rules! pallets_config { type Balance = Balance; /// The ubiquitous event type. type Event = Event; - type DustRemoval = (); + type DustRemoval = Treasury; type ExistentialDeposit = ExistentialDeposit; type AccountStore = Account; type WeightInfo = pallet_balances::weights::SubstrateWeight<Runtime>; @@ -272,7 +272,7 @@ macro_rules! pallets_config { type MaxRequests = frame_support::traits::ConstU32<1_000>; type RequestPrice = frame_support::traits::ConstU64<200>; type OnFilledRandomness = Account; - type OnUnbalanced = (); + type OnUnbalanced = Treasury; type CurrentBlockRandomness = pallet_babe::CurrentBlockRandomness<Self>; type RandomnessFromOneEpochAgo = pallet_babe::RandomnessFromOneEpochAgo<Self>; } @@ -321,6 +321,33 @@ macro_rules! pallets_config { type WeightInfo = pallet_utility::weights::SubstrateWeight<Self>; } + parameter_types! { + pub const Burn: Permill = Permill::zero(); + pub const ProposalBond: Permill = Permill::from_percent(1); + pub const ProposalBondMaximum: Option<Balance> = None; + pub const SpendPeriod: BlockNumber = DAYS; + // Treasury account address: + // gdev/gtest: 5EYCAe5ijiYfyeZ2JJCGq56LmPyNRAKzpG4QkoQkkQNB5e6Z + pub const TreasuryPalletId: PalletId = PalletId(*b"py/trsry"); + } + impl pallet_treasury::Config for Runtime { + type ApproveOrigin = TreasuryApproveOrigin; + type Burn = Burn; + type BurnDestination = (); + type Currency = Balances; + type Event = Event; + type OnSlash = Treasury; + type ProposalBond = ProposalBond; + type ProposalBondMinimum = frame_support::traits::ConstU64<10_000>; + type ProposalBondMaximum = ProposalBondMaximum; + type MaxApprovals = frame_support::traits::ConstU32<10>; + type PalletId = TreasuryPalletId; + type RejectOrigin = TreasuryRejectOrigin; + type SpendFunds = TreasurySpendFunds<Self>; + type SpendPeriod = SpendPeriod; + type WeightInfo = pallet_treasury::weights::SubstrateWeight<Self>; + } + // UNIVERSALĂ‚ DIVIDEND // impl pallet_universal_dividend::Config for Runtime { diff --git a/runtime/g1/Cargo.toml b/runtime/g1/Cargo.toml index 1f08a0648..a5c3e2567 100644 --- a/runtime/g1/Cargo.toml +++ b/runtime/g1/Cargo.toml @@ -22,8 +22,9 @@ runtime-benchmarks = [ 'frame-system/runtime-benchmarks', 'hex-literal', 'pallet-balances/runtime-benchmarks', - 'pallet-universal-dividend/runtime-benchmarks', 'pallet-timestamp/runtime-benchmarks', + 'pallet-treasury/runtime-benchmarks', + 'pallet-universal-dividend/runtime-benchmarks', 'sp-runtime/runtime-benchmarks', ] std = [ @@ -50,10 +51,11 @@ std = [ 'pallet-proxy/std', 'pallet-session/std', 'pallet-sudo/std', - 'pallet-universal-dividend/std', 'pallet-timestamp/std', 'pallet-transaction-payment-rpc-runtime-api/std', 'pallet-transaction-payment/std', + 'pallet-treasury/std', + 'pallet-universal-dividend/std', 'common-runtime/std', 'serde', 'sp-api/std', @@ -217,6 +219,11 @@ default-features = false git = 'https://github.com/librelois/substrate.git' branch = 'duniter-monthly-2022-02' +[dependencies.pallet-treasury] +default-features = false +git = 'https://github.com/librelois/substrate.git' +branch = 'duniter-monthly-2022-02' + [dependencies.pallet-utility] default-features = false git = 'https://github.com/librelois/substrate.git' diff --git a/runtime/g1/src/lib.rs b/runtime/g1/src/lib.rs index 2546c4f3c..cc89d93e3 100644 --- a/runtime/g1/src/lib.rs +++ b/runtime/g1/src/lib.rs @@ -42,6 +42,7 @@ pub use sp_runtime::{KeyTypeId, Perbill, Permill}; use common_runtime::IdtyNameValidatorImpl; use frame_support::traits::Contains; +use frame_support::PalletId; use frame_system::EnsureRoot; use pallet_grandpa::fg_primitives; use pallet_grandpa::{AuthorityId as GrandpaId, AuthorityList as GrandpaAuthorityList}; @@ -121,6 +122,8 @@ pub type Executive = frame_executive::Executive< AllPalletsWithSystem, >; +pub type SmithsInstance = Instance2; + pub struct BaseCallFilter; impl Contains<Call> for BaseCallFilter { fn contains(call: &Call) -> bool { @@ -241,6 +244,7 @@ construct_runtime!( ProvideRandomness: pallet_provide_randomness::{Pallet, Call, Storage, Event} = 62, Proxy: pallet_proxy::{Pallet, Call, Storage, Event<T>} = 63, Utility: pallet_utility::{Pallet, Call, Event} = 64, + Treasury: pallet_treasury::{Pallet, Call, Config, Storage, Event<T>} = 65, } ); diff --git a/runtime/g1/src/parameters.rs b/runtime/g1/src/parameters.rs index fa7e5e98d..a9a6684ba 100644 --- a/runtime/g1/src/parameters.rs +++ b/runtime/g1/src/parameters.rs @@ -14,11 +14,13 @@ // 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 crate::*; use common_runtime::constants::*; use common_runtime::{Balance, BlockNumber}; use frame_support::parameter_types; use frame_support::weights::constants::WEIGHT_PER_SECOND; use sp_arithmetic::Perbill; +use sp_core::u32_trait::*; use sp_runtime::transaction_validity::TransactionPriority; parameter_types! { @@ -146,7 +148,17 @@ parameter_types! { pub const SmithValidityPeriod: BlockNumber = 146 * DAYS; } +/*************/ +/* UTILITIES */ +/*************/ + // Multisig parameter_types! { pub const MaxSignatories: u16 = 5; } + +// Treasury +pub type TreasuryApproveOrigin = + pallet_collective::EnsureProportionMoreThan<_1, _2, AccountId, SmithsInstance>; +pub type TreasuryRejectOrigin = + pallet_collective::EnsureProportionMoreThan<_1, _3, AccountId, SmithsInstance>; diff --git a/runtime/gdev/Cargo.toml b/runtime/gdev/Cargo.toml index c584dd980..ef02d5472 100644 --- a/runtime/gdev/Cargo.toml +++ b/runtime/gdev/Cargo.toml @@ -23,6 +23,7 @@ runtime-benchmarks = [ 'hex-literal', 'pallet-balances/runtime-benchmarks', 'pallet-identity/runtime-benchmarks', + 'pallet-treasury/runtime-benchmarks', 'pallet-universal-dividend/runtime-benchmarks', 'common-runtime/runtime-benchmarks', 'sp-runtime/runtime-benchmarks', @@ -57,6 +58,7 @@ std = [ 'pallet-timestamp/std', 'pallet-transaction-payment-rpc-runtime-api/std', 'pallet-transaction-payment/std', + 'pallet-treasury/std', 'common-runtime/std', 'serde', 'sp-api/std', @@ -227,6 +229,12 @@ default-features = false git = 'https://github.com/librelois/substrate.git' branch = 'duniter-monthly-2022-02' +[dependencies.pallet-treasury] +default-features = false +git = 'https://github.com/librelois/substrate.git' +branch = 'duniter-monthly-2022-02' + + [dependencies.pallet-utility] default-features = false git = 'https://github.com/librelois/substrate.git' diff --git a/runtime/gdev/src/lib.rs b/runtime/gdev/src/lib.rs index 7bc127edf..d8c7ef4a5 100644 --- a/runtime/gdev/src/lib.rs +++ b/runtime/gdev/src/lib.rs @@ -42,6 +42,7 @@ pub use sp_runtime::{KeyTypeId, Perbill, Permill}; use common_runtime::IdtyNameValidatorImpl; use frame_support::traits::Contains; +use frame_support::PalletId; use frame_system::EnsureRoot; use pallet_grandpa::fg_primitives; use pallet_grandpa::{AuthorityId as GrandpaId, AuthorityList as GrandpaAuthorityList}; @@ -123,6 +124,8 @@ pub type Executive = frame_executive::Executive< AllPalletsWithSystem, >; +pub type SmithsInstance = Instance2; + pub struct BaseCallFilter; impl Contains<Call> for BaseCallFilter { fn contains(call: &Call) -> bool { @@ -131,7 +134,8 @@ impl Contains<Call> for BaseCallFilter { Call::System( frame_system::Call::remark { .. } | frame_system::Call::remark_with_event { .. } ) | Call::Membership( - pallet_membership::Call::claim_membership { .. } + pallet_membership::Call::request_membership { .. } + | pallet_membership::Call::claim_membership { .. } | pallet_membership::Call::revoke_membership { .. } ) | Call::Session(_) | Call::SmithsMembership(pallet_membership::Call::claim_membership { .. }) @@ -232,7 +236,7 @@ common_runtime::pallets_config! { impl pallet_upgrade_origin::Config for Runtime { type Event = Event; type Call = Call; - type UpgradableOrigin = pallet_collective::EnsureProportionAtLeast<_2, _3, AccountId, Instance2>; + type UpgradableOrigin = pallet_collective::EnsureProportionAtLeast<_2, _3, AccountId, SmithsInstance>; } } @@ -295,6 +299,7 @@ construct_runtime!( ProvideRandomness: pallet_provide_randomness::{Pallet, Call, Storage, Event} = 62, Proxy: pallet_proxy::{Pallet, Call, Storage, Event<T>} = 63, Utility: pallet_utility::{Pallet, Call, Event} = 64, + Treasury: pallet_treasury::{Pallet, Call, Config, Storage, Event<T>} = 65, } ); diff --git a/runtime/gdev/src/parameters.rs b/runtime/gdev/src/parameters.rs index 229c8eb25..7c11c6b7b 100644 --- a/runtime/gdev/src/parameters.rs +++ b/runtime/gdev/src/parameters.rs @@ -14,11 +14,13 @@ // 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 crate::*; use common_runtime::constants::*; use common_runtime::{Balance, BlockNumber}; use frame_support::parameter_types; use frame_support::weights::constants::WEIGHT_PER_SECOND; use sp_arithmetic::Perbill; +use sp_core::u32_trait::*; use sp_runtime::transaction_validity::TransactionPriority; parameter_types! { @@ -84,7 +86,17 @@ parameter_types! { pub const SquareMoneyGrowthRate: Perbill = Perbill::from_parts(2_381_440); } +/*************/ +/* UTILITIES */ +/*************/ + // Multisig parameter_types! { pub const MaxSignatories: u16 = 5; } + +// Treasury +pub type TreasuryApproveOrigin = + pallet_collective::EnsureProportionMoreThan<_1, _2, AccountId, SmithsInstance>; +pub type TreasuryRejectOrigin = + pallet_collective::EnsureProportionMoreThan<_1, _3, AccountId, SmithsInstance>; diff --git a/runtime/gdev/tests/integration_tests.rs b/runtime/gdev/tests/integration_tests.rs index 5d95a9f04..6ffc2f586 100644 --- a/runtime/gdev/tests/integration_tests.rs +++ b/runtime/gdev/tests/integration_tests.rs @@ -24,6 +24,11 @@ use gdev_runtime::*; use sp_keyring::AccountKeyring; use sp_runtime::MultiAddress; +#[test] +fn verify_treasury_account() { + println!("{}", Treasury::account_id()); +} + #[test] fn verify_pallet_prefixes() { let prefix = |pallet_name, storage_name| { @@ -142,6 +147,71 @@ fn test_remove_smith_identity() { }); } +#[test] +fn test_create_new_account_with_insufficient_balance() { + ExtBuilder::new(1, 3, 4) + .with_initial_balances(vec![(AccountKeyring::Alice.to_account_id(), 1_000)]) + .build() + .execute_with(|| { + run_to_block(2); + + // Should be able to transfer 2 units to a new account + assert_ok!(Balances::transfer( + frame_system::RawOrigin::Signed(AccountKeyring::Alice.to_account_id()).into(), + MultiAddress::Id(AccountKeyring::Eve.to_account_id()), + 200 + )); + let events = System::events(); + //println!("{:#?}", events); + assert_eq!(events.len(), 2); + assert_eq!( + System::events()[0].event, + Event::Balances(pallet_balances::Event::Endowed { + account: AccountKeyring::Eve.to_account_id(), + free_balance: 200, + }) + ); + assert_eq!( + System::events()[1].event, + Event::Balances(pallet_balances::Event::Transfer { + from: AccountKeyring::Alice.to_account_id(), + to: AccountKeyring::Eve.to_account_id(), + amount: 200, + }) + ); + + // At next bloc, the new account must be reaped because it's balance is not sufficient + // to pay the "new account tax" + run_to_block(3); + let events = System::events(); + //println!("{:#?}", events); + assert_eq!(events.len(), 3); + assert_eq!( + System::events()[0].event, + Event::Account(pallet_duniter_account::Event::ForceDestroy { + who: AccountKeyring::Eve.to_account_id(), + balance: 200, + }) + ); + assert_eq!( + System::events()[1].event, + Event::Balances(pallet_balances::Event::Deposit { + who: Treasury::account_id(), + amount: 200, + }) + ); + assert_eq!( + System::events()[2].event, + Event::Treasury(pallet_treasury::Event::Deposit { value: 200 }) + ); + assert_eq!( + Balances::free_balance(AccountKeyring::Eve.to_account_id()), + 0 + ); + assert_eq!(Balances::free_balance(Treasury::account_id()), 400); + }); +} + #[test] fn test_create_new_account() { ExtBuilder::new(1, 3, 4) @@ -175,12 +245,12 @@ fn test_create_new_account() { }) ); - // At next bloc, the mew account must be created, - // and new account tax should be collected + // At next bloc, the new account must be created, + // and new account tax should be collected and deposited in the terasury run_to_block(3); let events = System::events(); - //println!("{:#?}", events); - assert_eq!(events.len(), 2); + println!("{:#?}", events); + assert_eq!(events.len(), 4); assert_eq!( System::events()[0].event, Event::System(frame_system::Event::NewAccount { @@ -194,10 +264,22 @@ fn test_create_new_account() { amount: 300, }) ); + assert_eq!( + System::events()[2].event, + Event::Balances(pallet_balances::Event::Deposit { + who: Treasury::account_id(), + amount: 300, + }) + ); + assert_eq!( + System::events()[3].event, + Event::Treasury(pallet_treasury::Event::Deposit { value: 300 }) + ); assert_eq!( Balances::free_balance(AccountKeyring::Eve.to_account_id()), 200 ); + assert_eq!(Balances::free_balance(Treasury::account_id()), 500); // A random id request should be registered assert_eq!( diff --git a/runtime/gtest/Cargo.toml b/runtime/gtest/Cargo.toml index af4827930..c66f4f351 100644 --- a/runtime/gtest/Cargo.toml +++ b/runtime/gtest/Cargo.toml @@ -22,8 +22,9 @@ runtime-benchmarks = [ 'frame-system/runtime-benchmarks', 'hex-literal', 'pallet-balances/runtime-benchmarks', - 'pallet-universal-dividend/runtime-benchmarks', 'pallet-timestamp/runtime-benchmarks', + 'pallet-treasury/runtime-benchmarks', + 'pallet-universal-dividend/runtime-benchmarks', 'sp-runtime/runtime-benchmarks', ] std = [ @@ -50,10 +51,11 @@ std = [ 'pallet-multisig/std', 'pallet-session/std', 'pallet-sudo/std', - 'pallet-universal-dividend/std', 'pallet-timestamp/std', 'pallet-transaction-payment-rpc-runtime-api/std', 'pallet-transaction-payment/std', + 'pallet-treasury/std', + 'pallet-universal-dividend/std', 'common-runtime/std', 'serde', 'sp-api/std', @@ -217,6 +219,11 @@ default-features = false git = 'https://github.com/librelois/substrate.git' branch = 'duniter-monthly-2022-02' +[dependencies.pallet-treasury] +default-features = false +git = 'https://github.com/librelois/substrate.git' +branch = 'duniter-monthly-2022-02' + [dependencies.pallet-utility] default-features = false git = 'https://github.com/librelois/substrate.git' diff --git a/runtime/gtest/src/lib.rs b/runtime/gtest/src/lib.rs index be6b0723a..f980b2c10 100644 --- a/runtime/gtest/src/lib.rs +++ b/runtime/gtest/src/lib.rs @@ -42,6 +42,7 @@ pub use sp_runtime::{KeyTypeId, Perbill, Permill}; use common_runtime::IdtyNameValidatorImpl; use frame_support::traits::Contains; +use frame_support::PalletId; use frame_system::EnsureRoot; use pallet_grandpa::fg_primitives; use pallet_grandpa::{AuthorityId as GrandpaId, AuthorityList as GrandpaAuthorityList}; @@ -122,6 +123,8 @@ pub type Executive = frame_executive::Executive< AllPalletsWithSystem, >; +pub type SmithsInstance = Instance2; + pub struct BaseCallFilter; impl Contains<Call> for BaseCallFilter { fn contains(call: &Call) -> bool { @@ -242,6 +245,7 @@ construct_runtime!( ProvideRandomness: pallet_provide_randomness::{Pallet, Call, Storage, Event} = 62, Proxy: pallet_proxy::{Pallet, Call, Storage, Event<T>} = 63, Utility: pallet_utility::{Pallet, Call, Event} = 64, + Treasury: pallet_treasury::{Pallet, Call, Config, Storage, Event<T>} = 65, } ); diff --git a/runtime/gtest/src/parameters.rs b/runtime/gtest/src/parameters.rs index 5b1a0f76f..e4262ab82 100644 --- a/runtime/gtest/src/parameters.rs +++ b/runtime/gtest/src/parameters.rs @@ -14,11 +14,13 @@ // 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 crate::*; use common_runtime::constants::*; use common_runtime::{Balance, BlockNumber}; use frame_support::parameter_types; use frame_support::weights::constants::WEIGHT_PER_SECOND; use sp_arithmetic::Perbill; +use sp_core::u32_trait::*; use sp_runtime::transaction_validity::TransactionPriority; parameter_types! { @@ -146,7 +148,17 @@ parameter_types! { pub const SmithValidityPeriod: BlockNumber = 146 * DAYS; } +/*************/ +/* UTILITIES */ +/*************/ + // Multisig parameter_types! { pub const MaxSignatories: u16 = 5; } + +// Treasury +pub type TreasuryApproveOrigin = + pallet_collective::EnsureProportionMoreThan<_1, _2, AccountId, SmithsInstance>; +pub type TreasuryRejectOrigin = + pallet_collective::EnsureProportionMoreThan<_1, _3, AccountId, SmithsInstance>; -- GitLab