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

refac(node): do refactors in code structure of chain_spec #25

Closed
wants to merge 4 commits into from
Closed
Changes from all 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
208 changes: 94 additions & 114 deletions node/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,78 +100,37 @@ pub fn generate_config(network: String) -> Result<ChainSpec, String> {
let state: SubspaceJSONState =
json::from_slice(&bytes).map_err(|e| format!("Error parsing genesis file: {}", e))?;

let mut subnets: Vec<Subnet> = Vec::new();
let subnets: Vec<Subnet> = state.subnets.into_iter().map(deserialize_subnet).collect();
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() {
let (
name,
tempo,
immunity_period,
min_allowed_weights,
max_allowed_weights,
max_allowed_uids,
burn_rate,
min_stake,
founder,
) = subnet;

subnets.push((
name.as_bytes().to_vec(),
tempo,
immunity_period,
min_allowed_weights,
max_allowed_weights,
max_allowed_uids,
burn_rate,
min_stake,
sp_runtime::AccountId32::from(
<sr25519::Public as Ss58Codec>::from_ss58check(&founder).unwrap(),
),
));

for netuid in 0..subnets.len() {
// Add modules
modules.push(Vec::new());
for module in state.modules[netuid].iter() {
modules.push(vec![]);
for (id, name, address, weights) in state.modules[netuid].iter() {
modules[netuid].push((
sp_runtime::AccountId32::from(
// module_key
<sr25519::Public as Ss58Codec>::from_ss58check(&module.0).unwrap(),
),
module.1.as_bytes().to_vec(), // name
module.2.as_bytes().to_vec(), // address
module.3.iter().map(|(a, b)| (*a, *b)).collect(), // weights: Convert to tuples
new_account_id(id),
name.as_bytes().to_vec(),
address.as_bytes().to_vec(),
weights.iter().map(|(a, b)| (*a, *b)).collect(),
));
}

// Add stake to
Comment on lines +109 to +119

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let mut subnet_modules = Vec::with_capacity(state.modules[netuid].len());
for ... {
  subnet_modules.push((new_account_id, ...));
}
modules.push(subnet_modules);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where am I supposed to write this?

stake_to.push(Vec::new());
for (key_str, key_stake_to) in state.stake_to[netuid].iter() {
for (key, key_stake_to) in state.stake_to[netuid].iter() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same change here

stake_to[netuid].push((
sp_runtime::AccountId32::from(
<sr25519::Public as Ss58Codec>::from_ss58check(key_str).unwrap(),
),
key_stake_to
.iter()
.map(|(a, b)| {
(
sp_runtime::AccountId32::from(
<sr25519::Public as Ss58Codec>::from_ss58check(a).unwrap(),
),
*b,
)
})
.collect(),
new_account_id(key),
key_stake_to.iter().map(|(a, b)| (new_account_id(a), *b)).collect(),
));
}
}

let mut processed_balances: Vec<(sp_runtime::AccountId32, u64)> = Vec::new();
for (key_str, amount) in state.balances.iter() {
let key = <sr25519::Public as Ss58Codec>::from_ss58check(key_str).unwrap();
let key_account = sp_runtime::AccountId32::from(key);

processed_balances.push((key_account, *amount));
}
let processed_balances: Vec<(sp_runtime::AccountId32, u64)> = state
.balances
.iter()
.map(|(id, amount)| (new_account_id(id), *amount))
.collect();

// Give front-ends necessary data to present to users
let mut properties = sc_service::Properties::new();
Expand All @@ -180,43 +139,42 @@ pub fn generate_config(network: String) -> Result<ChainSpec, String> {
properties.insert("ss58Format".into(), 13116.into());

Ok(ChainSpec::from_genesis(
// Name
"commune",
// ID
"commune",
"commune", // Name
"commune", // ID
ChainType::Development,
move || {
// Sudo account
let root =
Ss58Codec::from_ss58check("5FXymAnjbb7p57pNyfdLb6YCdzm73ZhVq6oFF1AdCEPEg8Uw")
.unwrap();

// Initial PoA authorities (Validators)
// aura | grandpa
let initial_authorities = vec![
// Keys for debug
authority_keys_from_seed("Alice"),
authority_keys_from_seed("Bob"),
];
network_genesis(
wasm_binary,
// Initial PoA authorities (Validators)
// aura | grandpa
vec![
// Keys for debug
authority_keys_from_seed("Alice"),
authority_keys_from_seed("Bob"),
],
// Sudo account
Ss58Codec::from_ss58check("5FXymAnjbb7p57pNyfdLb6YCdzm73ZhVq6oFF1AdCEPEg8Uw")
.unwrap(),
// Pre-funded a
processed_balances.clone(), // balances
modules.clone(), // modules,
subnets.clone(), // subnets,
stake_to.clone(), // stake_to,
state.block,
initial_authorities,
root,
processed_balances.clone(),
SubspaceModuleConfig {
// Add names to storage.
modules: modules.clone(),
subnets: subnets.clone(),
block: state.block,
stake_to: stake_to.clone(),
},
)
},
// Bootnodes
vec![],
// Telemetry
None,
// Protocol ID
Some("commune"),
None,
// Properties
Some(properties),
// Extensions
None,
vec![], // Bootnodes
None, // Telemetry
Some("commune"), // Protocol ID
None, //
Some(properties), // Properties
None, // Extensions
))
}

Expand All @@ -234,13 +192,15 @@ fn network_genesis(
initial_authorities: Vec<(AuraId, GrandpaId)>,
root_key: AccountId,
balances: Vec<(AccountId, u64)>,
modules: Vec<Vec<(AccountId, Vec<u8>, Vec<u8>, Vec<(u16, u16)>)>>,
subnets: Vec<(Vec<u8>, u16, u16, u16, u16, u16, u16, u64, AccountId)>,
stake_to: Vec<Vec<(AccountId, Vec<(AccountId, u64)>)>>,
block: u32,
module: SubspaceModuleConfig,
) -> RuntimeGenesisConfig {
use node_subspace_runtime::EVMConfig;

let (aura, grandpa): (Vec<_>, Vec<_>) = initial_authorities
.into_iter()
.map(|(aura, grandpa)| (aura, (grandpa, 1)))
.unzip();

RuntimeGenesisConfig {
system: SystemConfig {
// Add Wasm runtime to storage.
Expand All @@ -252,39 +212,29 @@ fn network_genesis(
//balances: balances.iter().cloned().map(|k| k).collect(),
balances: balances.to_vec(),
},
aura: AuraConfig {
authorities: initial_authorities.iter().map(|x| (x.0.clone())).collect(),
},
aura: AuraConfig { authorities: aura },
grandpa: GrandpaConfig {
authorities: initial_authorities.iter().map(|x| (x.1.clone(), 1)).collect(),
authorities: grandpa,
..Default::default()
},
sudo: SudoConfig {
// Assign network admin rights.
key: Some(root_key),
},
transaction_payment: Default::default(),
subspace_module: SubspaceModuleConfig {
// Add names to storage.
modules,
subnets,
block,
stake_to,
},
subspace_module: module,
// EVM Compatibility
evm_chain_id: Default::default(),
evm: EVMConfig {
accounts: Precompiles::used_addresses()
.map(|addr| {
(
addr,
fp_evm::GenesisAccount {
balance: Default::default(),
code: Default::default(),
nonce: Default::default(),
storage: Default::default(),
},
)
let account = fp_evm::GenesisAccount {
balance: Default::default(),
code: Default::default(),
nonce: Default::default(),
storage: Default::default(),
};
(addr, account)
})
.collect(),
_marker: Default::default(),
Expand All @@ -293,3 +243,33 @@ fn network_genesis(
base_fee: Default::default(),
}
}

fn new_account_id(id: &str) -> sp_runtime::AccountId32 {
sp_runtime::AccountId32::from(<sr25519::Public as Ss58Codec>::from_ss58check(id).unwrap())
}

fn deserialize_subnet(
(
name,
tempo,
immunity_period,
min_allowed_weights,
max_allowed_weights,
max_allowed_uids,
burn_rate,
min_stake,
founder,
): JSONSubnet,
) -> Subnet {
(
name.as_bytes().to_vec(),
tempo,
immunity_period,
min_allowed_weights,
max_allowed_weights,
max_allowed_uids,
burn_rate,
min_stake,
new_account_id(&founder),
)
}
Loading