diff --git a/Cargo.lock b/Cargo.lock index c6f2cb862617d4539946d0203e2c3f010a40b3cd..a22e6dc49bed79c7a2e93ecd44f62016759d81e3 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 10907697032a690e72bcb4c56cc05cc5f32629b4..ae6497975272c73dda24b2541f6e2066b06c65eb 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 7a5eeb50b6a6610c0e932aa52b5b6705bc3dcf40..490178cf7a009985e55b2bda325401e6c4cd0f72 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 68dd55784082262fbd1c25d5af3eeef27f6ade0e..d06efc2219d48eaa83f42e1f42d8e23fe3fe9b97 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 1182f72a02857cce192b314abd9a38e3c661f21b..e1beb6b255d1dd2ef0402e6908d108b1df04e751 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 ed5a4d9cfce917a41fc865260fba6967279bc2a0..782745a6ade3d2ef25ab4b21716f6101077629be 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 c8fb20374958e8f49c5cf6cfc2f678d43f4ffc85..af414220e5c6a5ace42c4309585700555bf80d0b 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 49444c2dc1a288cd30e35f51bc4a0362ea6e6767..3815715b86a2d1d1a66a20e98ebb5ac0d532dd60 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};