From 8c5f828676589abecea9c0cd09c7114df25c521a Mon Sep 17 00:00:00 2001 From: librelois <c@elo.tf> Date: Sun, 3 Jul 2022 01:45:59 +0200 Subject: [PATCH] migrate pallet id integration tests to manual ud --- pallets/universal-dividend/src/lib.rs | 49 ++++-- pallets/universal-dividend/src/mock.rs | 5 +- pallets/universal-dividend/src/tests.rs | 222 ++++++++++++++++++------ pallets/universal-dividend/src/types.rs | 2 + 4 files changed, 210 insertions(+), 68 deletions(-) diff --git a/pallets/universal-dividend/src/lib.rs b/pallets/universal-dividend/src/lib.rs index 9c0507589..78b4e87d9 100644 --- a/pallets/universal-dividend/src/lib.rs +++ b/pallets/universal-dividend/src/lib.rs @@ -53,7 +53,7 @@ pub mod pallet { #[pallet::pallet] #[pallet::generate_store(pub(super) trait Store)] #[pallet::storage_version(STORAGE_VERSION)] - #[pallet::without_storage_info] + //#[pallet::without_storage_info] pub struct Pallet<T>(_); #[pallet::config] @@ -107,6 +107,18 @@ pub mod pallet { pub type CurrentUdIndex<T: Config> = StorageValue<_, UdIndex, ValueQuery, DefaultForCurrentUdIndex>; + #[cfg(test)] + #[pallet::storage] + pub type TestMembers<T: Config> = StorageMap< + _, + Blake2_128Concat, + T::AccountId, + FirstEligibleUd, + ValueQuery, + GetDefault, + ConstU32<300_000>, + >; + /// Total quantity of money created by universal dividend (does not take into account the possible destruction of money) #[pallet::storage] #[pallet::getter(fn total_money_created)] @@ -130,6 +142,8 @@ pub mod pallet { pub first_reeval: T::BlockNumber, pub first_ud: BalanceOf<T>, pub initial_monetary_mass: BalanceOf<T>, + #[cfg(test)] + pub initial_members: Vec<T::AccountId>, } #[cfg(feature = "std")] @@ -139,6 +153,8 @@ pub mod pallet { first_reeval: Default::default(), first_ud: Default::default(), initial_monetary_mass: Default::default(), + #[cfg(test)] + initial_members: Default::default(), } } } @@ -157,6 +173,13 @@ pub mod pallet { .try_push((1, self.first_ud)) .expect("MaxPastReeval should be greather than zero"); PastReevals::<T>::put(past_reevals); + + #[cfg(test)] + { + for member in &self.initial_members { + TestMembers::<T>::insert(member, FirstEligibleUd::min()); + } + } } } @@ -228,7 +251,7 @@ pub mod pallet { let ud_amount = <CurrentUd<T>>::get(); let monetary_mass = <MonetaryMass<T>>::get(); - // TODO inc ud index + // Increment ud index let ud_index = CurrentUdIndex::<T>::mutate(|next_ud_index| { core::mem::replace(next_ud_index, next_ud_index.saturating_add(1)) }); @@ -252,21 +275,21 @@ pub mod pallet { maybe_first_eligible_ud { let current_ud_index = CurrentUdIndex::<T>::get(); - let (uds_count, uds_total) = if first_ud_index.get() > current_ud_index { - (0, Zero::zero()) + if first_ud_index.get() >= current_ud_index { + Ok((0, Zero::zero())) } else { - compute_claim_uds::compute_claim_uds( + let (uds_count, uds_total) = compute_claim_uds::compute_claim_uds( current_ud_index, first_ud_index.get(), PastReevals::<T>::get().into_iter(), - ) - }; - let _ = core::mem::replace( - first_ud_index, - core::num::NonZeroU16::new(current_ud_index.saturating_add(1)) - .expect("unrecahble because with added 1"), - ); - Ok((uds_count, uds_total)) + ); + let _ = core::mem::replace( + first_ud_index, + core::num::NonZeroU16::new(current_ud_index) + .expect("unrechable because current_ud_index is never zero."), + ); + Ok((uds_count, uds_total)) + } } else { Err::<_, DispatchError>(Error::<T>::AccountNotAllowedToClaimUds.into()) } diff --git a/pallets/universal-dividend/src/mock.rs b/pallets/universal-dividend/src/mock.rs index 2e2e46113..e424d7670 100644 --- a/pallets/universal-dividend/src/mock.rs +++ b/pallets/universal-dividend/src/mock.rs @@ -18,7 +18,7 @@ use super::*; use crate::{self as pallet_universal_dividend}; use frame_support::{ parameter_types, - traits::{Everything, OnFinalize, OnInitialize}, + traits::{Everything, OnFinalize, OnInitialize, StorageMapShim}, }; use frame_system as system; use sp_core::H256; @@ -127,7 +127,7 @@ impl pallet_universal_dividend::Config for Test { type Event = Event; type MaxPastReeval = frame_support::traits::ConstU32<2>; type MembersCount = MembersCount; - type MembersStorage = FakeWot; + type MembersStorage = StorageMapShim<crate::TestMembers<Test>, (), u64, FirstEligibleUd>; type SquareMoneyGrowthRate = SquareMoneyGrowthRate; type UdCreationPeriod = UdCreationPeriod; type UdReevalPeriod = UdReevalPeriod; @@ -153,6 +153,7 @@ pub fn run_to_block(n: u64) { while System::block_number() < n { UniversalDividend::on_finalize(System::block_number()); System::on_finalize(System::block_number()); + System::reset_events(); System::set_block_number(System::block_number() + 1); System::on_initialize(System::block_number()); UniversalDividend::on_initialize(System::block_number()); diff --git a/pallets/universal-dividend/src/tests.rs b/pallets/universal-dividend/src/tests.rs index 21c2d187d..518dfd77f 100644 --- a/pallets/universal-dividend/src/tests.rs +++ b/pallets/universal-dividend/src/tests.rs @@ -15,15 +15,15 @@ // along with Substrate-Libre-Currency. If not, see <https://www.gnu.org/licenses/>. use crate::mock::*; -use frame_system::{EventRecord, Phase}; +use frame_support::{assert_err, assert_ok}; #[test] -#[ignore] -fn test_ud_creation() { +fn test_claim_uds() { new_test_ext(UniversalDividendConfig { first_reeval: 8, first_ud: 1_000, initial_monetary_mass: 0, + initial_members: vec![1, 2, 3], }) .execute_with(|| { // In the beginning there was no money @@ -33,83 +33,199 @@ fn test_ud_creation() { assert_eq!(Balances::free_balance(4), 0); assert_eq!(UniversalDividend::total_money_created(), 0); - // The first UD must be created in block #2 + // Alice can claim UDs, but this should be a no-op. + run_to_block(1); + assert_ok!(UniversalDividend::claim_uds(Origin::signed(1))); + System::assert_has_event(Event::UniversalDividend(crate::Event::UdsClaimed { + count: 0, + total: 0, + who: 1, + })); + assert_eq!(Balances::free_balance(1), 0); + + // Dave is not a member, he can't claim UDs + assert_err!( + UniversalDividend::claim_uds(Origin::signed(4)), + crate::Error::<Test>::AccountNotAllowedToClaimUds + ); + + // At block #2, the first UD must be created, but nobody should receive money run_to_block(2); + assert_eq!(UniversalDividend::total_money_created(), 3_000); + assert_eq!(Balances::free_balance(1), 0); + assert_eq!(Balances::free_balance(2), 0); + assert_eq!(Balances::free_balance(3), 0); + assert_eq!(Balances::free_balance(4), 0); + + // Alice can claim UDs, and this time she must receive exactly one UD + assert_ok!(UniversalDividend::claim_uds(Origin::signed(1))); + System::assert_has_event(Event::UniversalDividend(crate::Event::UdsClaimed { + count: 1, + total: 1_000, + who: 1, + })); assert_eq!(Balances::free_balance(1), 1_000); - assert_eq!(Balances::free_balance(2), 1_000); - assert_eq!(Balances::free_balance(3), 1_000); + // Others members should not receive any UDs with Alice claim + assert_eq!(Balances::free_balance(2), 0); + assert_eq!(Balances::free_balance(3), 0); assert_eq!(Balances::free_balance(4), 0); - assert_eq!(UniversalDividend::total_money_created(), 3_000); - // Block #2 must generate 7 events, 2 events per new account fed, plus 1 event for the creation of the UD. - let events = System::events(); - println!("events: {:#?}", events); - assert_eq!(events.len(), 10); - assert_eq!( - events[9], - EventRecord { - phase: Phase::Initialization, - event: Event::UniversalDividend(crate::Event::NewUdCreated { - amount: 1_000, - index: 1, - monetary_mass: 3_000, - members_count: 3, - }), - topics: vec![], - } + // At block #4, the second UD must be created, but nobody should receive money + run_to_block(4); + assert_eq!(UniversalDividend::total_money_created(), 6_000); + assert_eq!(Balances::free_balance(1), 1_000); + assert_eq!(Balances::free_balance(2), 0); + assert_eq!(Balances::free_balance(3), 0); + assert_eq!(Balances::free_balance(4), 0); + + // Alice can claim UDs, And she must receive exactly one UD (the second one) + assert_ok!(UniversalDividend::claim_uds(Origin::signed(1))); + System::assert_has_event(Event::UniversalDividend(crate::Event::UdsClaimed { + count: 1, + total: 1_000, + who: 1, + })); + assert_eq!(Balances::free_balance(1), 2_000); + // Others members should not receive any UDs with Alice claim + assert_eq!(Balances::free_balance(2), 0); + assert_eq!(Balances::free_balance(3), 0); + assert_eq!(Balances::free_balance(4), 0); + + // Bob can claim UDs, he must receive exactly two UDs + assert_ok!(UniversalDividend::claim_uds(Origin::signed(2))); + System::assert_has_event(Event::UniversalDividend(crate::Event::UdsClaimed { + count: 2, + total: 2_000, + who: 2, + })); + assert_eq!(Balances::free_balance(2), 2_000); + // Others members should not receive any UDs with Alice and Bob claims + assert_eq!(Balances::free_balance(3), 0); + assert_eq!(Balances::free_balance(4), 0); + + // Dave is still not a member, he still can't claim UDs. + assert_err!( + UniversalDividend::claim_uds(Origin::signed(4)), + crate::Error::<Test>::AccountNotAllowedToClaimUds ); + // At block #8, the first reevaluated UD should be created + run_to_block(8); + assert_eq!(UniversalDividend::total_money_created(), 12_225); + + // Charlie can claim all his UDs at once, he must receive exactly four UDs + assert_ok!(UniversalDividend::claim_uds(Origin::signed(3))); + System::assert_has_event(Event::UniversalDividend(crate::Event::UdsClaimed { + count: 4, + total: 4_075, + who: 3, + })); + assert_eq!(Balances::free_balance(3), 4_075); + }); +} + +#[test] +fn test_ud_creation() { + new_test_ext(UniversalDividendConfig { + first_reeval: 8, + first_ud: 1_000, + initial_monetary_mass: 0, + initial_members: vec![1, 2, 3], + }) + .execute_with(|| { + // In the beginning there was no money + assert_eq!(Balances::free_balance(1), 0); + assert_eq!(Balances::free_balance(2), 0); + assert_eq!(Balances::free_balance(3), 0); + assert_eq!(Balances::free_balance(4), 0); + assert_eq!(UniversalDividend::total_money_created(), 0); + + // The first UD must be created in block #2 + run_to_block(2); + System::assert_has_event(Event::UniversalDividend(crate::Event::NewUdCreated { + amount: 1_000, + index: 1, + monetary_mass: 3_000, + members_count: 3, + })); + assert_eq!(UniversalDividend::total_money_created(), 3_000); + /*assert_eq!(Balances::free_balance(1), 1_000); + assert_eq!(Balances::free_balance(2), 1_000); + assert_eq!(Balances::free_balance(3), 1_000); + assert_eq!(Balances::free_balance(4), 0);*/ + // The second UD must be created in block #4 run_to_block(4); - assert_eq!(Balances::free_balance(1), 2_000); + System::assert_has_event(Event::UniversalDividend(crate::Event::NewUdCreated { + amount: 1_000, + index: 2, + monetary_mass: 6_000, + members_count: 3, + })); + assert_eq!(UniversalDividend::total_money_created(), 6_000); + /*assert_eq!(Balances::free_balance(1), 2_000); assert_eq!(Balances::free_balance(2), 2_000); assert_eq!(Balances::free_balance(3), 2_000); - assert_eq!(Balances::free_balance(4), 0); - assert_eq!(UniversalDividend::total_money_created(), 6_000); - - /*// Block #4 must generate 4 events, 1 event per account fed, plus 1 event for the creation of the UD. - let events = System::events(); - println!("{:?}", events); - assert_eq!(events.len(), 4); - assert_eq!( - events[3], - EventRecord { - phase: Phase::Initialization, - event: Event::UniversalDividend(crate::Event::NewUdCreated(1000, 3)), - topics: vec![], - } - );*/ + assert_eq!(Balances::free_balance(4), 0);*/ // The third UD must be created in block #6 run_to_block(6); - assert_eq!(Balances::free_balance(1), 3_000); + System::assert_has_event(Event::UniversalDividend(crate::Event::NewUdCreated { + amount: 1_000, + index: 3, + monetary_mass: 9_000, + members_count: 3, + })); + assert_eq!(UniversalDividend::total_money_created(), 9_000); + /*assert_eq!(Balances::free_balance(1), 3_000); assert_eq!(Balances::free_balance(2), 3_000); assert_eq!(Balances::free_balance(3), 3_000); - assert_eq!(Balances::free_balance(4), 0); - assert_eq!(UniversalDividend::total_money_created(), 9_000); + assert_eq!(Balances::free_balance(4), 0);*/ // Block #8 should cause a re-evaluation of UD run_to_block(8); - assert_eq!(Balances::free_balance(1), 4_075); + System::assert_has_event(Event::UniversalDividend(crate::Event::UdReevalued { + new_ud_amount: 1_075, + monetary_mass: 9_000, + members_count: 3, + })); + // Then, the first reevalued UD should be created + System::assert_has_event(Event::UniversalDividend(crate::Event::NewUdCreated { + amount: 1_075, + index: 4, + monetary_mass: 12_225, + members_count: 3, + })); + assert_eq!(UniversalDividend::total_money_created(), 12_225); + /*assert_eq!(Balances::free_balance(1), 4_075); assert_eq!(Balances::free_balance(2), 4_075); assert_eq!(Balances::free_balance(3), 4_075); - assert_eq!(Balances::free_balance(4), 0); - assert_eq!(UniversalDividend::total_money_created(), 12_225); + assert_eq!(Balances::free_balance(4), 0);*/ // Block #10 #12 and #14should creates the reevalued UD run_to_block(14); - assert_eq!(Balances::free_balance(1), 7_300); - assert_eq!(Balances::free_balance(2), 7_300); - assert_eq!(Balances::free_balance(3), 7_300); - assert_eq!(Balances::free_balance(4), 0); + System::assert_has_event(Event::UniversalDividend(crate::Event::NewUdCreated { + amount: 1_075, + index: 7, + monetary_mass: 21_900, + members_count: 3, + })); assert_eq!(UniversalDividend::total_money_created(), 21_900); // Block #16 should cause a second re-evaluation of UD run_to_block(16); - assert_eq!(Balances::free_balance(1), 8_557); - assert_eq!(Balances::free_balance(2), 8_557); - assert_eq!(Balances::free_balance(3), 8_557); - assert_eq!(Balances::free_balance(4), 0); + System::assert_has_event(Event::UniversalDividend(crate::Event::UdReevalued { + new_ud_amount: 1_257, + monetary_mass: 21_900, + members_count: 3, + })); + // Then, the reevalued UD should be created + System::assert_has_event(Event::UniversalDividend(crate::Event::NewUdCreated { + amount: 1_257, + index: 8, + monetary_mass: 25_671, + members_count: 3, + })); assert_eq!(UniversalDividend::total_money_created(), 25_671); }); } diff --git a/pallets/universal-dividend/src/types.rs b/pallets/universal-dividend/src/types.rs index 16464350c..837efa9de 100644 --- a/pallets/universal-dividend/src/types.rs +++ b/pallets/universal-dividend/src/types.rs @@ -63,6 +63,8 @@ impl Encode for FirstEligibleUd { } } +impl codec::EncodeLike for FirstEligibleUd {} + impl Decode for FirstEligibleUd { fn decode<I: Input>(input: &mut I) -> Result<Self, Error> { Ok(match NonZeroU16::new(Decode::decode(input)?) { -- GitLab