Skip to content

Commit

Permalink
chore: structure chain spec patches better
Browse files Browse the repository at this point in the history
  • Loading branch information
saiintbrisson committed Jul 5, 2024
1 parent 152329a commit 81d60db
Show file tree
Hide file tree
Showing 10 changed files with 194 additions and 262 deletions.
9 changes: 9 additions & 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
members = [
"node",
"pallets/subspace",
"pallets/subspace/genesis-config",
"pallets/governance",
"pallets/governance/api",
"pallets/faucet",
Expand Down
1 change: 1 addition & 0 deletions node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ frame-benchmarking-cli.workspace = true
# Local Dependencies
node-subspace-runtime.path = "../runtime"

pallet-subspace-genesis-config.path = "../pallets/subspace/genesis-config"
subspace-rpc.path = "../pallets/subspace/rpc"

[build-dependencies]
Expand Down
145 changes: 41 additions & 104 deletions node/src/chain_spec.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use node_subspace_runtime::{AccountId, RuntimeGenesisConfig, WASM_BINARY};
use pallet_subspace_genesis_config::{ConfigModule, ConfigSubnet};
use sc_service::ChainType;
use serde::Deserialize;
use sp_consensus_aura::sr25519::AuthorityId as AuraId;
Expand All @@ -21,35 +22,6 @@ pub fn authority_keys_from_seed(s: &str) -> (AuraId, GrandpaId) {
(get_from_seed::<AuraId>(s), get_from_seed::<GrandpaId>(s))
}

/// (name, tempo, immunity_period, min_allowed_weights, max_allowed_weights,
/// max_allowed_uids, min_stake, founder)
pub type JSONSubnet = (String, u16, u16, u16, u16, u16, u64, String);

/// (key, name, address, stake, weights)
pub type JSONModule = (String, String, String, Vec<(u16, u16)>);

/// (module_key, amount)
pub type JSONStakeTo = (String, Vec<(String, u64)>);

/// (name, tempo, immunity_period, min_allowed_weights, max_allowed_weights,
/// max_allowed_uids, min_stake, founder)
pub type Subnet = (
Vec<u8>,
u16,
u16,
u16,
u16,
u16,
u64,
sp_runtime::AccountId32,
);

/// (key, name, address, stake, weights)
pub type Module = (sp_runtime::AccountId32, Vec<u8>, Vec<u8>, Vec<(u16, u16)>);

/// (module_key, amount)
pub type StakeTo = (sp_runtime::AccountId32, Vec<(sp_runtime::AccountId32, u64)>);

/// A struct containing the patch values for the default chain spec.
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
Expand All @@ -58,13 +30,7 @@ struct ChainSpecPatch {
balances: std::collections::HashMap<String, u64>,

#[serde(default)]
subnets: Vec<JSONSubnet>,

#[serde(default)]
modules: Vec<Vec<JSONModule>>,

#[serde(default)]
stake_to: Vec<Vec<JSONStakeTo>>,
subnets: Vec<ConfigSubnet<String, String>>,

#[serde(default)]
block: u32,
Expand All @@ -80,61 +46,42 @@ pub fn generate_config(path: &str) -> Result<ChainSpec, String> {
let state: ChainSpecPatch =
serde_json::from_reader(&file).map_err(|e| format!("Error parsing spec file: {e}"))?;

let mut subnets: Vec<Subnet> = Vec::new();
let mut modules: Vec<Vec<Module>> = Vec::new();
let mut stake_to: Vec<Vec<StakeTo>> = Vec::new();

for (netuid, subnet) in state.subnets.into_iter().enumerate() {
subnets.push((
subnet.0.as_bytes().to_vec(),
subnet.1,
subnet.2,
subnet.3,
subnet.4,
subnet.5,
subnet.6,
account_id_from_str(&subnet.7),
));

let subnet_module = state.modules.get(netuid).map_or_else(
Vec::new, // Changed from || Vec::new()
|module| {
module
.iter()
.map(|(key, name, addr, weights)| {
(
account_id_from_str(key),
name.as_bytes().to_vec(),
addr.as_bytes().to_vec(),
weights.iter().map(|(a, b)| (*a, *b)).collect(),
)
})
.collect()
},
);
modules.push(subnet_module);

let subnet_stake_to = state.stake_to.get(netuid).map_or_else(
Vec::new, // Changed from || Vec::new()
|stake_to_subnet| {
stake_to_subnet
.iter()
.map(|(key, key_stake_to)| {
let key = account_id_from_str(key);
let key_stake_to = key_stake_to
.iter()
.map(|(a, b)| {
let key = account_id_from_str(a);
(key, *b)
})
.collect();
(key, key_stake_to)
})
.collect()
},
);
stake_to.push(subnet_stake_to);
}
let subnets: Vec<_> = state
.subnets
.into_iter()
.map(|subnet| {
let modules = subnet
.modules
.into_iter()
.map(|module| ModuleData {
key: account_id_from_str(&module.key),
name: module.name.as_bytes().to_vec(),
address: module.address.as_bytes().to_vec(),
weights: module.weights,
stake_from: module.stake_from.map(|stake_from| {
stake_from
.into_iter()
.map(|(key, stake)| (account_id_from_str(&key), stake))
.collect()
}),
})
.collect();

SubnetData {
name: subnet.name.as_bytes().to_vec(),
founder: account_id_from_str(&subnet.founder),

tempo: subnet.tempo,
immunity_period: subnet.immunity_period,
min_allowed_weights: subnet.min_allowed_weights,
max_allowed_weights: subnet.max_allowed_weights,
max_allowed_uids: subnet.max_allowed_uids,
min_stake: subnet.min_stake,

modules,
}
})
.collect();

let processed_balances: Vec<_> = state
.balances
Expand All @@ -155,9 +102,7 @@ pub fn generate_config(path: &str) -> Result<ChainSpec, String> {
],
account_id_from_str("5FXymAnjbb7p57pNyfdLb6YCdzm73ZhVq6oFF1AdCEPEg8Uw"),
processed_balances,
modules,
subnets,
stake_to,
state.block,
);

Expand All @@ -173,22 +118,16 @@ pub fn generate_config(path: &str) -> Result<ChainSpec, String> {
.build())
}

type ModuleData = (AccountId, Vec<u8>, Vec<u8>, Vec<(u16, u16)>);
type Modules = Vec<Vec<ModuleData>>;
type SubnetData = (Vec<u8>, u16, u16, u16, u16, u16, u64, AccountId);
type SubnetData = ConfigSubnet<Vec<u8>, sp_runtime::AccountId32>;
type ModuleData = ConfigModule<Vec<u8>, sp_runtime::AccountId32>;

type Subnets = Vec<SubnetData>;
type StakeToData = (AccountId, Vec<(AccountId, u64)>);
type StakeToVec = Vec<Vec<StakeToData>>;

// Configure initial storage state for FRAME modules.
#[allow(clippy::too_many_arguments)]
fn genesis_patch(
initial_authorities: &[(AuraId, GrandpaId)],
root_key: AccountId,
balances: Vec<(AccountId, u64)>,
modules: Modules,
subnets: Subnets,
stake_to: StakeToVec,
block: u32,
) -> serde_json::Value {
serde_json::json!({
Expand All @@ -205,10 +144,8 @@ fn genesis_patch(
"key": Some(root_key),
},
"subspaceModule": {
"modules": modules,
"subnets": subnets,
"block": block,
"stakeTo": stake_to,
},
})
}
1 change: 1 addition & 0 deletions pallets/subspace/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ log.workspace = true
substrate-fixed.workspace = true
pallet-transaction-payment.workspace = true

pallet-subspace-genesis-config.path = "./genesis-config"
pallet-governance-api = { path = "../governance/api", default-features = false }
pallet-subnet-emission-api = { path = "../subnet_emission/api", default-features = false }

Expand Down
14 changes: 14 additions & 0 deletions pallets/subspace/genesis-config/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "pallet-subspace-genesis-config"
authors = ["Commune Nucleus Team"]
version = "1.0.0"
edition = "2021"

[lints]
workspace = true

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
serde.workspace = true
28 changes: 28 additions & 0 deletions pallets/subspace/genesis-config/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#![no_std]

extern crate alloc;

use alloc::vec::Vec;
use serde::{Deserialize, Serialize};

#[derive(Debug, Deserialize, Serialize)]
pub struct ConfigSubnet<Name, AccountId> {
pub name: Name,
pub founder: AccountId,
pub tempo: Option<u16>,
pub immunity_period: Option<u16>,
pub min_allowed_weights: Option<u16>,
pub max_allowed_weights: Option<u16>,
pub max_allowed_uids: Option<u16>,
pub min_stake: Option<u64>,
pub modules: Vec<ConfigModule<Name, AccountId>>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct ConfigModule<Name, AccountId> {
pub key: AccountId,
pub name: Name,
pub address: Name,
pub weights: Option<Vec<(u16, u16)>>,
pub stake_from: Option<Vec<(AccountId, u64)>>,
}
Loading

0 comments on commit 81d60db

Please sign in to comment.