Skip to content
Snippets Groups Projects
Commit dfd744af authored by Éloïs's avatar Éloïs
Browse files

opti(randomness): index requests by epoch to avoid items rotation

parent 6736a4f4
No related branches found
No related tags found
1 merge request!36feat(runtimes): add account random id & provide randomness calls
This commit is part of merge request !36. Comments created here will be created in the context of that merge request.
......@@ -831,6 +831,7 @@ dependencies = [
"frame-system",
"log",
"pallet-authority-members",
"pallet-babe",
"pallet-balances",
"pallet-certification",
"pallet-duniter-account",
......
......@@ -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 },
);
}
}
}
......
......@@ -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'
......
......@@ -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 {
......
......@@ -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;
......
......@@ -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};
......
......@@ -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;
......
......@@ -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};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment