From dfd744af616a162345f2be8a3a4911c42ba2bda1 Mon Sep 17 00:00:00 2001 From: librelois <c@elo.tf> Date: Sun, 20 Feb 2022 18:25:54 +0100 Subject: [PATCH] opti(randomness): index requests by epoch to avoid items rotation --- Cargo.lock | 1 + pallets/provide-randomness/src/lib.rs | 70 +++++++++++++-------------- runtime/common/Cargo.toml | 6 +++ runtime/common/src/lib.rs | 9 ++++ runtime/common/src/pallets_config.rs | 1 + runtime/g1/src/lib.rs | 2 +- runtime/gdev/src/lib.rs | 2 +- runtime/gtest/src/lib.rs | 2 +- 8 files changed, 53 insertions(+), 40 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c6f2cb862..a22e6dc49 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -831,6 +831,7 @@ dependencies = [ "frame-system", "log", "pallet-authority-members", + "pallet-babe", "pallet-balances", "pallet-certification", "pallet-duniter-account", diff --git a/pallets/provide-randomness/src/lib.rs b/pallets/provide-randomness/src/lib.rs index 109076970..ae6497975 100644 --- a/pallets/provide-randomness/src/lib.rs +++ b/pallets/provide-randomness/src/lib.rs @@ -69,6 +69,8 @@ pub mod pallet { type Currency: Currency<Self::AccountId>; /// The overarching event type. type Event: From<Event> + IsType<<Self as frame_system::Config>::Event>; + /// Get the current epoch index + type GetCurrentEpochIndex: Get<u64>; /// Maximum number of not yet filled requests #[pallet::constant] type MaxRequests: Get<u32>; @@ -88,7 +90,7 @@ pub mod pallet { // STORAGE // #[pallet::storage] - pub(super) type NewEpoch<T: Config> = StorageValue<_, bool, ValueQuery>; + pub(super) type NexEpochHookIn<T: Config> = StorageValue<_, u8, ValueQuery>; #[pallet::storage] pub(super) type RequestIdProvider<T: Config> = StorageValue<_, RequestId, ValueQuery>; @@ -98,20 +100,9 @@ pub mod pallet { pub type RequestsReadyAtNextBlock<T: Config> = StorageValue<_, Vec<Request>, ValueQuery>; #[pallet::storage] - #[pallet::getter(fn requests_ready_at_next_epoch)] - pub type RequestsReadyAtNextEpoch<T: Config> = StorageValue<_, Vec<Request>, ValueQuery>; - - #[pallet::storage] - #[pallet::getter(fn requests_ready_in_two_epochs)] - pub type RequestsReadyInTwoEpochs<T: Config> = StorageValue<_, Vec<Request>, ValueQuery>; - - #[pallet::storage] - #[pallet::getter(fn requests_ready_in_three_epochs)] - pub type RequestsReadyInThreeEpochs<T: Config> = StorageValue<_, Vec<Request>, ValueQuery>; - - #[pallet::storage] - #[pallet::getter(fn pending_requests)] - pub type PendingRequests<T: Config> = StorageValue<_, Vec<Request>, ValueQuery>; + #[pallet::getter(fn requests_ready_at_epoch)] + pub type RequestsReadyAtEpoch<T: Config> = + StorageMap<_, Twox64Concat, u64, Vec<Request>, ValueQuery>; #[pallet::storage] #[pallet::getter(fn requests_ids)] @@ -189,10 +180,14 @@ pub mod pallet { total_weight += 100_000; } - if NewEpoch::<T>::get() { - NewEpoch::<T>::put(false); + let next_epoch_hook_in = NexEpochHookIn::<T>::mutate(|next_in| { + core::mem::replace(next_in, next_in.saturating_sub(1)) + }); + if next_epoch_hook_in == 1 { total_weight += 100_000; - for Request { request_id, salt } in RequestsReadyAtNextEpoch::<T>::take() { + for Request { request_id, salt } in + RequestsReadyAtEpoch::<T>::take(T::GetCurrentEpochIndex::get()) + { let randomness = T::RandomnessFromOneEpochAgo::random(salt.as_ref()).0; total_weight += T::OnFilledRandomness::on_filled_randomness(request_id, randomness); @@ -202,18 +197,6 @@ pub mod pallet { }); total_weight += 100_000; } - - total_weight += 200_000; - let requests_ready_at_next_epoch = RequestsReadyInTwoEpochs::<T>::take(); - RequestsReadyAtNextEpoch::<T>::put(requests_ready_at_next_epoch); - - total_weight += 200_000; - let requests_ready_in_two_epochs = RequestsReadyInThreeEpochs::<T>::take(); - RequestsReadyInTwoEpochs::<T>::put(requests_ready_in_two_epochs); - - total_weight += 200_000; - let requests_ready_in_three_epochs = PendingRequests::<T>::take(); - RequestsReadyInThreeEpochs::<T>::put(requests_ready_in_three_epochs); } total_weight @@ -243,7 +226,7 @@ pub mod pallet { Self::apply_request(randomness_type, salt) } pub fn on_new_epoch() { - NewEpoch::<T>::put(true); + NexEpochHookIn::<T>::put(5) } } @@ -265,22 +248,35 @@ pub mod pallet { core::mem::replace(next_request_id, next_request_id.saturating_add(1)) }); RequestsIds::<T>::insert(request_id, ()); + let current_epoch = T::GetCurrentEpochIndex::get(); match randomness_type { RandomnessType::RandomnessFromPreviousBlock => { RequestsReadyAtNextBlock::<T>::append(Request { request_id, salt }); } RandomnessType::RandomnessFromOneEpochAgo => { - if NewEpoch::<T>::get() { - RequestsReadyInThreeEpochs::<T>::append(Request { request_id, salt }); + if NexEpochHookIn::<T>::get() > 1 { + RequestsReadyAtEpoch::<T>::append( + current_epoch + 3, + Request { request_id, salt }, + ); } else { - RequestsReadyInTwoEpochs::<T>::append(Request { request_id, salt }); + RequestsReadyAtEpoch::<T>::append( + current_epoch + 2, + Request { request_id, salt }, + ); } } RandomnessType::RandomnessFromTwoEpochsAgo => { - if NewEpoch::<T>::get() { - PendingRequests::<T>::append(Request { request_id, salt }); + if NexEpochHookIn::<T>::get() > 1 { + RequestsReadyAtEpoch::<T>::append( + current_epoch + 4, + Request { request_id, salt }, + ); } else { - RequestsReadyInThreeEpochs::<T>::append(Request { request_id, salt }); + RequestsReadyAtEpoch::<T>::append( + current_epoch + 3, + Request { request_id, salt }, + ); } } } diff --git a/runtime/common/Cargo.toml b/runtime/common/Cargo.toml index 7a5eeb50b..490178cf7 100644 --- a/runtime/common/Cargo.toml +++ b/runtime/common/Cargo.toml @@ -23,6 +23,7 @@ std = [ 'frame-system/std', 'log/std', 'pallet-authority-members/std', + 'pallet-babe/std', 'pallet-balances/std', 'pallet-certification/std', 'pallet-duniter-account/std', @@ -73,6 +74,11 @@ default-features = false git = 'https://github.com/librelois/substrate.git' branch = 'duniter-monthly-2022-02' +[dependencies.pallet-babe] +default-features = false +git = 'https://github.com/librelois/substrate.git' +branch = 'duniter-monthly-2022-02' + [dependencies.pallet-balances] default-features = false git = 'https://github.com/librelois/substrate.git' diff --git a/runtime/common/src/lib.rs b/runtime/common/src/lib.rs index 68dd55784..d06efc221 100644 --- a/runtime/common/src/lib.rs +++ b/runtime/common/src/lib.rs @@ -72,6 +72,15 @@ impl sp_runtime::traits::Convert<AccountId, Option<entities::ValidatorFullIdenti } } +pub struct GetCurrentEpochIndex<Runtime>(core::marker::PhantomData<Runtime>); +impl<Runtime: pallet_babe::Config> frame_support::pallet_prelude::Get<u64> + for GetCurrentEpochIndex<Runtime> +{ + fn get() -> u64 { + pallet_babe::Pallet::<Runtime>::epoch_index() + } +} + pub struct IdtyNameValidatorImpl; impl pallet_identity::traits::IdtyNameValidator for IdtyNameValidatorImpl { fn validate(idty_name: &pallet_identity::IdtyName) -> bool { diff --git a/runtime/common/src/pallets_config.rs b/runtime/common/src/pallets_config.rs index 1182f72a0..e1beb6b25 100644 --- a/runtime/common/src/pallets_config.rs +++ b/runtime/common/src/pallets_config.rs @@ -279,6 +279,7 @@ macro_rules! pallets_config { impl pallet_provide_randomness::Config for Runtime { type Currency = Balances; type Event = Event; + type GetCurrentEpochIndex = GetCurrentEpochIndex<Self>; type MaxRequests = frame_support::traits::ConstU32<1_000>; type RequestPrice = frame_support::traits::ConstU64<200>; type OnFilledRandomness = Account; diff --git a/runtime/g1/src/lib.rs b/runtime/g1/src/lib.rs index ed5a4d9cf..782745a6a 100644 --- a/runtime/g1/src/lib.rs +++ b/runtime/g1/src/lib.rs @@ -27,7 +27,7 @@ pub mod parameters; pub use self::parameters::*; pub use common_runtime::{ constants::*, entities::*, handlers::*, AccountId, Address, Balance, BlockNumber, - FullIdentificationOfImpl, Hash, Header, IdtyIndex, Index, Signature, + FullIdentificationOfImpl, GetCurrentEpochIndex, Hash, Header, 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 c8fb20374..af414220e 100644 --- a/runtime/gdev/src/lib.rs +++ b/runtime/gdev/src/lib.rs @@ -27,7 +27,7 @@ pub mod parameters; pub use self::parameters::*; pub use common_runtime::{ constants::*, entities::*, handlers::*, AccountId, Address, Balance, BlockNumber, - FullIdentificationOfImpl, Hash, Header, IdtyIndex, Index, Signature, + FullIdentificationOfImpl, GetCurrentEpochIndex, Hash, Header, 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 49444c2dc..3815715b8 100644 --- a/runtime/gtest/src/lib.rs +++ b/runtime/gtest/src/lib.rs @@ -27,7 +27,7 @@ pub mod parameters; pub use self::parameters::*; pub use common_runtime::{ constants::*, entities::*, handlers::*, AccountId, Address, Balance, BlockNumber, - FullIdentificationOfImpl, Hash, Header, IdtyIndex, Index, Signature, + FullIdentificationOfImpl, GetCurrentEpochIndex, Hash, Header, IdtyIndex, Index, Signature, }; pub use pallet_balances::Call as BalancesCall; pub use pallet_identity::{IdtyStatus, IdtyValue}; -- GitLab