Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Distribution feature for the storage pallet. #2552

Merged
merged 25 commits into from
Aug 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
89b167b
runtime: storage: Add `create distribution bucket family’ extrinsic.
shamil-gadelshin Jul 5, 2021
72b791c
runtime: Fix tests.
shamil-gadelshin Jul 6, 2021
553a723
runtime: storage: Add ‘MaxDistributionBucketFamilyNumber’ constant.
shamil-gadelshin Jul 6, 2021
5b43656
runtime: storage: Add extrinsic.
shamil-gadelshin Jul 6, 2021
e795e23
runtime: storage: Add extrinsic.
shamil-gadelshin Jul 6, 2021
77c96bd
runtime: storage: Add extrinsic.
shamil-gadelshin Jul 6, 2021
5f1cbb3
runtime: storage: Add extrinsic.
shamil-gadelshin Jul 6, 2021
a77f9db
runtime: storage: Add exrinsic.
shamil-gadelshin Jul 8, 2021
86aad9c
runtime: storage: Fix bug.
shamil-gadelshin Jul 8, 2021
377c899
runtime: storage: Add extrinsic.
shamil-gadelshin Jul 8, 2021
c9ae45d
runtime: storage: Add extrinsic.
shamil-gadelshin Jul 8, 2021
418d17a
runtime: storage: Add extrinsic.
shamil-gadelshin Jul 8, 2021
5a2fcc0
runtime: storage: Add random distribution buckets pick.
shamil-gadelshin Jul 9, 2021
60fc740
runtime: storage: Add extrinsic.
shamil-gadelshin Jul 12, 2021
585ad8b
runtime: storage: Add extrinsic.
shamil-gadelshin Jul 12, 2021
1890f35
runtime: storage: Add extrinsic.
shamil-gadelshin Jul 12, 2021
9f10469
runtime: storage: Add extrinsic.
shamil-gadelshin Jul 12, 2021
eb6d409
runtime: storage: Add constant.
shamil-gadelshin Jul 13, 2021
600f1d8
runtime: storage: Add some tests.
shamil-gadelshin Jul 13, 2021
13ed8fa
runtime: storage: Fix comments.
shamil-gadelshin Jul 13, 2021
473402d
runtime: storage: Fix merge conflicts.
shamil-gadelshin Jul 28, 2021
62da918
runtime: storage: Add an extrinsic.
shamil-gadelshin Jul 29, 2021
d6ae303
runtime: storage: Change distriubution bucket.
shamil-gadelshin Jul 29, 2021
a3a15f0
runtime: storage: refactor the code.
shamil-gadelshin Jul 29, 2021
dca15ca
runtime: storage: Change -Object postfix to -Record.
shamil-gadelshin Aug 2, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions node/src/chain_spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ use sp_runtime::Perbill;
use node_runtime::{
membership, wasm_binary_unwrap, AuthorityDiscoveryConfig, BabeConfig, Balance, BalancesConfig,
ContentDirectoryConfig, ContentDirectoryWorkingGroupConfig, ContentWorkingGroupConfig,
CouncilConfig, CouncilElectionConfig, ElectionParameters, ForumConfig, GrandpaConfig,
ImOnlineConfig, MembersConfig, Moment, ProposalsCodexConfig, SessionConfig, SessionKeys,
Signature, StakerStatus, StakingConfig, StorageWorkingGroupConfig, SudoConfig, SystemConfig,
VersionedStoreConfig, VersionedStorePermissionsConfig, DAYS,
CouncilConfig, CouncilElectionConfig, DistributionWorkingGroupConfig, ElectionParameters,
ForumConfig, GrandpaConfig, ImOnlineConfig, MembersConfig, Moment, ProposalsCodexConfig,
SessionConfig, SessionKeys, Signature, StakerStatus, StakingConfig, StorageWorkingGroupConfig,
SudoConfig, SystemConfig, VersionedStoreConfig, VersionedStorePermissionsConfig, DAYS,
};

// Exported to be used by chain-spec-builder
Expand Down Expand Up @@ -319,6 +319,13 @@ pub fn testnet_genesis(
worker_application_human_readable_text_constraint: default_text_constraint,
worker_exit_rationale_text_constraint: default_text_constraint,
}),
working_group_Instance4: Some(DistributionWorkingGroupConfig {
phantom: Default::default(),
working_group_mint_capacity: 0,
opening_human_readable_text_constraint: default_text_constraint,
worker_application_human_readable_text_constraint: default_text_constraint,
worker_exit_rationale_text_constraint: default_text_constraint,
}),
content_directory: Some({
ContentDirectoryConfig {
class_by_id: vec![],
Expand Down
3 changes: 3 additions & 0 deletions runtime-modules/common/src/working_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ pub enum WorkingGroup {

/// Storage working group: working_group::Instance3.
Content = 3isize,

/// Distribution working group: working_group::Instance4.
Distribution = 4isize,
}
94 changes: 94 additions & 0 deletions runtime-modules/storage/src/distribution_bucket_picker.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#![warn(missing_docs)]

use frame_support::traits::Randomness;
use sp_arithmetic::traits::Zero;
use sp_runtime::SaturatedConversion;
use sp_std::cell::RefCell;
use sp_std::collections::btree_set::BTreeSet;
use sp_std::marker::PhantomData;
use sp_std::rc::Rc;
use sp_std::vec::Vec;

use crate::{DynamicBagType, Module, Trait};

// Generates distribution bucket IDs to assign to a new dynamic bag.
pub(crate) struct DistributionBucketPicker<T> {
trait_marker: PhantomData<T>,
}

impl<T: Trait> DistributionBucketPicker<T> {
// Get random distribution buckets from distribution bucket families using the dynamic bag
// creation policy.
pub(crate) fn pick_distribution_buckets(
bag_type: DynamicBagType,
) -> BTreeSet<T::DistributionBucketId> {
let creation_policy = Module::<T>::get_dynamic_bag_creation_policy(bag_type);

if creation_policy.no_distribution_buckets_required() {
return BTreeSet::new();
}

// Randomness for all bucket family.
// let random_seed = RefCell::new(Module::<T>::get_initial_random_seed());
let random_seed = Rc::new(RefCell::new(Module::<T>::get_initial_random_seed()));

creation_policy
.families
.iter()
.filter_map(|(family_id, bucket_num)| {
Module::<T>::ensure_distribution_bucket_family_exists(family_id)
.ok()
.map(|fam| (fam, bucket_num))
})
.map(|(family, bucket_num)| {
let filtered_ids = family
.distribution_buckets
.iter()
.filter_map(|(id, bucket)| bucket.accepting_new_bags.then(|| *id))
.collect::<Vec<_>>();

(filtered_ids, bucket_num)
})
.map(|(bucket_ids, bucket_num)| {
Self::get_random_distribution_buckets(bucket_ids, *bucket_num, random_seed.clone())
})
.flatten()
.collect::<BTreeSet<_>>()
}

// Get random bucket IDs from the ID collection.
pub fn get_random_distribution_buckets(
ids: Vec<T::DistributionBucketId>,
bucket_number: u32,
seed: Rc<RefCell<T::Hash>>, // seed: RefCell<T::Hash>
) -> BTreeSet<T::DistributionBucketId> {
let mut working_ids = ids;
let mut result_ids = BTreeSet::default();

for _ in 0..bucket_number {
if working_ids.is_empty() {
break;
}

let current_seed = Self::advance_random_seed(seed.clone());

let upper_bound = working_ids.len() as u64 - 1;
let index =
Module::<T>::random_index(current_seed.as_ref(), upper_bound).saturated_into();
result_ids.insert(working_ids.remove(index));
}

result_ids
}

// Changes the internal seed value of the container and returns new random seed.
fn advance_random_seed(seed: Rc<RefCell<T::Hash>>) -> T::Hash {
// Cannot create randomness in the initial block (Substrate error).
if <frame_system::Module<T>>::block_number() == Zero::zero() {
return Module::<T>::get_initial_random_seed();
}

let current_seed = *seed.borrow();
seed.replace(T::Randomness::random(current_seed.as_ref()))
}
}
Loading