From c36380baa10b3e1986e5ae4aee96027f9914aa41 Mon Sep 17 00:00:00 2001 From: Rubberbandits Date: Thu, 22 Jun 2023 18:24:44 -0700 Subject: [PATCH 1/2] Add local chain to chain_spec for fast testing with localnet --- node/src/chain_spec.rs | 122 +++++++++++++++++++++++++++++++++++++++++ node/src/command.rs | 1 + 2 files changed, 123 insertions(+) diff --git a/node/src/chain_spec.rs b/node/src/chain_spec.rs index 000cfa3b7..a963af901 100644 --- a/node/src/chain_spec.rs +++ b/node/src/chain_spec.rs @@ -363,3 +363,125 @@ fn finney_genesis( }, } } + +pub fn localnet_config() -> Result { + let path: PathBuf = std::path::PathBuf::from("./snapshot.json"); + let wasm_binary = WASM_BINARY.ok_or_else(|| "Development wasm not available".to_string())?; + + // We mmap the file into memory first, as this is *a lot* faster than using + // `serde_json::from_reader`. See https://github.com/serde-rs/json/issues/160 + let file = File::open(&path) + .map_err(|e| format!("Error opening genesis file `{}`: {}", path.display(), e))?; + + // SAFETY: `mmap` is fundamentally unsafe since technically the file can change + // underneath us while it is mapped; in practice it's unlikely to be a problem + let bytes = unsafe { + memmap2::Mmap::map(&file) + .map_err(|e| format!("Error mmaping genesis file `{}`: {}", path.display(), e))? + }; + + let old_state: ColdkeyHotkeys = + json::from_slice(&bytes).map_err(|e| format!("Error parsing genesis file: {}", e))?; + + let mut processed_stakes: Vec<(sp_runtime::AccountId32, Vec<(sp_runtime::AccountId32, (u64, u16))>)> = Vec::new(); + for (coldkey_str, hotkeys) in old_state.stakes.iter() { + let coldkey = ::from_ss58check(&coldkey_str).unwrap(); + let coldkey_account = sp_runtime::AccountId32::from(coldkey); + + let mut processed_hotkeys: Vec<(sp_runtime::AccountId32, (u64, u16))> = Vec::new(); + + for (hotkey_str, amount_uid) in hotkeys.iter() { + let (amount, uid) = amount_uid; + let hotkey = ::from_ss58check(&hotkey_str).unwrap(); + let hotkey_account = sp_runtime::AccountId32::from(hotkey); + + processed_hotkeys.push((hotkey_account, (*amount, *uid))); + } + + processed_stakes.push((coldkey_account, processed_hotkeys)); + } + + let mut balances_issuance: u64 = 0; + let mut processed_balances: Vec<(sp_runtime::AccountId32, u64)> = Vec::new(); + for (key_str, amount) in old_state.balances.iter() { + let key = ::from_ss58check(&key_str).unwrap(); + let key_account = sp_runtime::AccountId32::from(key); + + processed_balances.push((key_account, *amount)); + balances_issuance += *amount; + } + + // Give front-ends necessary data to present to users + let mut properties = sc_service::Properties::new(); + properties.insert("tokenSymbol".into(), "TAO".into()); + properties.insert("tokenDecimals".into(), 9.into()); + properties.insert("ss58Format".into(), 13116.into()); + + Ok(ChainSpec::from_genesis( + // Name + "Bittensor", + // ID + "bittensor", + ChainType::Development, + move || { + localnet_genesis( + wasm_binary, + // Initial PoA authorities (Validators) + // aura | grandpa + vec![ + // Keys for debug + authority_keys_from_seed("Alice"), + authority_keys_from_seed("Bob"), + ], + // Pre-funded accounts + true, + ) + }, + // Bootnodes + vec![], + // Telemetry + None, + // Protocol ID + Some("bittensor"), + None, + // Properties + Some(properties), + // Extensions + None, + )) +} + +fn localnet_genesis( + wasm_binary: &[u8], + initial_authorities: Vec<(AuraId, GrandpaId)>, + _enable_println: bool, +) -> GenesisConfig { + GenesisConfig { + system: SystemConfig { + // Add Wasm runtime to storage. + code: wasm_binary.to_vec(), + }, + balances: BalancesConfig { + // Configure sudo balance + balances: vec![ + (get_account_id_from_seed::("Alice"), 1000000000000), + (get_account_id_from_seed::("Bob"), 1000000000000), + (get_account_id_from_seed::("Charlie"), 1000000000000), + (get_account_id_from_seed::("Dave"), 2000000000), + (get_account_id_from_seed::("Eve"), 2000000000), + (get_account_id_from_seed::("Ferdie"), 2000000000), + ] + }, + aura: AuraConfig { + authorities: initial_authorities.iter().map(|x| (x.0.clone())).collect(), + }, + grandpa: GrandpaConfig { + authorities: initial_authorities.iter().map(|x| (x.1.clone(), 1)).collect(), + }, + sudo: SudoConfig { + key: Some(Ss58Codec::from_ss58check("5GpzQgpiAKHMWNSH3RN4GLf96GVTDct9QxYEFAY7LWcVzTbx").unwrap()), + }, + transaction_payment: Default::default(), + subtensor_module: Default::default(), + } +} diff --git a/node/src/command.rs b/node/src/command.rs index 18d22d8c8..bab290d11 100644 --- a/node/src/command.rs +++ b/node/src/command.rs @@ -44,6 +44,7 @@ impl SubstrateCli for Cli { fn load_spec(&self, id: &str) -> Result, String> { Ok(match id { + "local" => Box::new(chain_spec::localnet_config()?), "finney" => Box::new(chain_spec::finney_mainnet_config()?), "" |"test_finney" => Box::new(chain_spec::finney_testnet_config()?), path => From f5c87758aef4f1b942712821801d53f269dd461f Mon Sep 17 00:00:00 2001 From: Ayden Brewer Date: Mon, 26 Jun 2023 14:45:56 -0700 Subject: [PATCH 2/2] Update node/src/chain_spec.rs Co-authored-by: Cameron Fairchild --- node/src/chain_spec.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/node/src/chain_spec.rs b/node/src/chain_spec.rs index a963af901..5566cc652 100644 --- a/node/src/chain_spec.rs +++ b/node/src/chain_spec.rs @@ -479,7 +479,7 @@ fn localnet_genesis( authorities: initial_authorities.iter().map(|x| (x.1.clone(), 1)).collect(), }, sudo: SudoConfig { - key: Some(Ss58Codec::from_ss58check("5GpzQgpiAKHMWNSH3RN4GLf96GVTDct9QxYEFAY7LWcVzTbx").unwrap()), + key: Some(get_account_id_from_seed::("Alice")), }, transaction_payment: Default::default(), subtensor_module: Default::default(),