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

Genesis config for pallet-ddc-clusters #153

Merged
merged 4 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node/service/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ pub fn cere_dev_genesis(
transaction_payment: Default::default(),
ddc_customers: Default::default(),
nomination_pools: Default::default(),
ddc_clusters: Default::default(),
}
}

Expand Down
2 changes: 2 additions & 0 deletions pallets/ddc-clusters/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ hex-literal = "^0.3.1"
pallet-contracts = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.31" }
pallet-ddc-nodes = { version = "4.7.0", default-features = false, path = "../ddc-nodes" }
scale-info = { version = "2.1.2", default-features = false, features = ["derive"] }
serde = { version = "1.0.136", default-features = false, features = ["derive"], optional = true }
sp-runtime = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.31" }
sp-std = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.31" }

Expand All @@ -29,6 +30,7 @@ substrate-test-utils = { git = "https://github.com/paritytech/substrate.git", br
default = ["std"]
std = [
"codec/std",
"serde",
"ddc-primitives/std",
"frame-support/std",
"frame-system/std",
Expand Down
4 changes: 4 additions & 0 deletions pallets/ddc-clusters/src/cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ use codec::{Decode, Encode};
use ddc_primitives::{ClusterId, ClusterParams};
use frame_support::{pallet_prelude::*, parameter_types};
use scale_info::TypeInfo;
#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};

parameter_types! {
pub MaxClusterParamsLen: u16 = 2048;
}

#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, PartialEq)]
pub struct Cluster<AccountId> {
pub cluster_id: ClusterId,
Expand All @@ -16,6 +19,7 @@ pub struct Cluster<AccountId> {
pub props: ClusterProps<AccountId>,
}

#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, PartialEq)]
pub struct ClusterProps<AccountId> {
pub node_provider_auth_contract: AccountId,
Expand Down
55 changes: 55 additions & 0 deletions pallets/ddc-clusters/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use ddc_traits::{
staking::{StakingVisitor, StakingVisitorError},
};
use frame_support::{
assert_ok,
pallet_prelude::*,
traits::{Currency, LockableCurrency},
};
Expand Down Expand Up @@ -118,6 +119,60 @@ pub mod pallet {
OptionQuery,
>;

#[pallet::genesis_config]
pub struct GenesisConfig<T: Config> {
rakanalh marked this conversation as resolved.
Show resolved Hide resolved
pub clusters: Vec<Cluster<T::AccountId>>,
#[allow(clippy::type_complexity)]
pub clusters_gov_params: Vec<(ClusterId, ClusterGovParams<BalanceOf<T>, T::BlockNumber>)>,
pub clusters_nodes: Vec<(ClusterId, Vec<NodePubKey>)>,
}

#[cfg(feature = "std")]
impl<T: Config> Default for GenesisConfig<T> {
fn default() -> Self {
GenesisConfig {
clusters: Default::default(),
clusters_gov_params: Default::default(),
clusters_nodes: Default::default(),
}
}
}

#[pallet::genesis_build]
impl<T: Config> GenesisBuild<T> for GenesisConfig<T>
where
T::AccountId: UncheckedFrom<T::Hash> + AsRef<[u8]>,
{
fn build(&self) {
for cluster in &self.clusters {
assert_ok!(Pallet::<T>::create_cluster(
frame_system::Origin::<T>::Root.into(),
cluster.cluster_id,
cluster.manager_id.clone(),
cluster.reserve_id.clone(),
ClusterParams::<T::AccountId> {
node_provider_auth_contract: cluster
.props
.node_provider_auth_contract
.clone(),
},
self.clusters_gov_params
.iter()
.find(|(id, _)| id == &cluster.cluster_id)
.unwrap()
.1
.clone(),
));

for (cluster_id, nodes) in &self.clusters_nodes {
for node_pub_key in nodes {
<ClustersNodes<T>>::insert(cluster_id, node_pub_key, true);
}
}
}
}
}

#[pallet::call]
impl<T: Config> Pallet<T>
where
Expand Down
1 change: 1 addition & 0 deletions primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub struct ClusterParams<AccountId> {
}

// ClusterGovParams includes Governance sensetive parameters
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, PartialEq)]
#[scale_info(skip_type_params(Balance, BlockNumber, T))]
pub struct ClusterGovParams<Balance, BlockNumber> {
Expand Down
Loading