forked from commune-ai/subspace
-
Notifications
You must be signed in to change notification settings - Fork 1
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
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3866360
refac(node): do refactors in code structure of chain_spec
aripiprazole 5241192
refac(node): use map and iterate over len
aripiprazole 5ecb1b1
style(node): move local functions to the final of the file
aripiprazole 2d9bf51
refac(node): add better solution to aura grandpa
aripiprazole File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
|
@@ -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 | ||
)) | ||
} | ||
|
||
|
@@ -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. | ||
|
@@ -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(), | ||
|
@@ -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), | ||
) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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?