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

localnet improvement #802

Open
wants to merge 15 commits into
base: devnet-ready
Choose a base branch
from
2 changes: 1 addition & 1 deletion .github/workflows/e2e-bittensor-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,4 @@ jobs:
run: |
pwd
ls
LOCALNET_SH_PATH="${{ github.workspace }}/scripts/localnet.sh" pytest tests/e2e_tests/ -s
CHAIN="e2e_test" LOCALNET_SH_PATH="${{ github.workspace }}/scripts/localnet.sh" pytest tests/e2e_tests/ -s
120 changes: 120 additions & 0 deletions node/src/chain_spec/e2e_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// Allowed since it's actually better to panic during chain setup when there is an error
#![allow(clippy::unwrap_used)]

use super::*;

pub fn e2e_test_config() -> Result<ChainSpec, String> {
let wasm_binary = WASM_BINARY.ok_or_else(|| "Development wasm not available".to_string())?;

// 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(), 42.into());

Ok(ChainSpec::builder(
wasm_binary,
Extensions {
bad_blocks: Some(HashSet::from_iter(vec![
// Example bad block
H256::from_str(
"0xc174d485de4bc3813ac249fe078af605c74ff91d07b0a396cf75fa04f81fa312",
)
.unwrap(),
])),
..Default::default()
},
)
.with_name("Bittensor")
.with_protocol_id("bittensor")
.with_id("bittensor")
.with_chain_type(ChainType::Development)
.with_genesis_config_patch(e2e_test_genesis(
// Initial PoA authorities (Validators)
// aura | grandpa
vec![
// Keys for debug
authority_keys_from_seed("Alice"),
authority_keys_from_seed("Bob"),
],
// Pre-funded accounts
true,
))
.with_properties(properties)
.build())
}

fn e2e_test_genesis(
initial_authorities: Vec<(AuraId, GrandpaId)>,
_enable_println: bool,
) -> serde_json::Value {
let mut balances = vec![
(
get_account_id_from_seed::<sr25519::Public>("Alice"),
1000000000000000u128,
),
(
get_account_id_from_seed::<sr25519::Public>("Bob"),
1000000000000000u128,
),
(
get_account_id_from_seed::<sr25519::Public>("Charlie"),
1000000000000000u128,
),
(
get_account_id_from_seed::<sr25519::Public>("Dave"),
2000000000000u128,
),
(
get_account_id_from_seed::<sr25519::Public>("Eve"),
2000000000000u128,
),
(
get_account_id_from_seed::<sr25519::Public>("Ferdie"),
2000000000000u128,
),
];

// Check if the environment variable is set
if let Ok(bt_wallet) = env::var("BT_DEFAULT_TOKEN_WALLET") {
if let Ok(decoded_wallet) = Ss58Codec::from_ss58check(&bt_wallet) {
balances.push((decoded_wallet, 1_000_000_000_000_000u128));
} else {
eprintln!("Invalid format for BT_DEFAULT_TOKEN_WALLET.");
}
}

let trimvirate_members: Vec<AccountId> = bounded_vec![
get_account_id_from_seed::<sr25519::Public>("Alice"),
get_account_id_from_seed::<sr25519::Public>("Bob"),
get_account_id_from_seed::<sr25519::Public>("Charlie"),
];

let senate_members: Vec<AccountId> = bounded_vec![
get_account_id_from_seed::<sr25519::Public>("Dave"),
get_account_id_from_seed::<sr25519::Public>("Eve"),
get_account_id_from_seed::<sr25519::Public>("Ferdie"),
];

serde_json::json!({
"balances": { "balances": balances },
"aura": {
"authorities": initial_authorities.iter().map(|x| (x.0.clone())).collect::<Vec<_>>()
},
"grandpa": {
"authorities": initial_authorities
.iter()
.map(|x| (x.1.clone(), 1))
.collect::<Vec<_>>()
},
"sudo": {
"key": Some(get_account_id_from_seed::<sr25519::Public>("Alice"))
},
"triumvirateMembers": {
"members": trimvirate_members
},
"senateMembers": {
"members": senate_members,
},
})
}
81 changes: 70 additions & 11 deletions node/src/chain_spec/localnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ pub fn localnet_config() -> Result<ChainSpec, String> {
properties.insert("tokenSymbol".into(), "TAO".into());
properties.insert("tokenDecimals".into(), 9.into());
properties.insert("ss58Format".into(), 42.into());
let genesis = localnet_genesis(
// Initial PoA authorities (Validators)
// aura | grandpa
vec![
// Keys for debug
authority_keys_from_seed("Alice"),
authority_keys_from_seed("Bob"),
],
// Pre-funded accounts
false,
);

Ok(ChainSpec::builder(
wasm_binary,
Expand All @@ -29,17 +40,7 @@ pub fn localnet_config() -> Result<ChainSpec, String> {
.with_protocol_id("bittensor")
.with_id("bittensor")
.with_chain_type(ChainType::Development)
.with_genesis_config_patch(localnet_genesis(
// Initial PoA authorities (Validators)
// aura | grandpa
vec![
// Keys for debug
authority_keys_from_seed("Alice"),
authority_keys_from_seed("Bob"),
],
// Pre-funded accounts
true,
))
.with_genesis_config_patch(genesis)
.with_properties(properties)
.build())
}
Expand Down Expand Up @@ -75,6 +76,50 @@ fn localnet_genesis(
),
];

let root_validator = (
get_account_id_from_seed::<sr25519::Public>("RootValidator"),
// 10000 TAO
10_000_000_000_000_u128,
);

let subnet_validator = (
get_account_id_from_seed::<sr25519::Public>("SubnetValidator"),
// 2000 TAO
2_000_000_000_000_u128,
);

let miners = [
(
get_account_id_from_seed::<sr25519::Public>("Miner1"),
// 10 TAO
10_000_000_000_u128,
),
(
get_account_id_from_seed::<sr25519::Public>("Miner2"),
// 10 TAO
10_000_000_000_u128,
),
(
get_account_id_from_seed::<sr25519::Public>("Miner3"),
// 10 TAO
10_000_000_000_u128,
),
(
get_account_id_from_seed::<sr25519::Public>("Miner4"),
// 10 TAO
10_000_000_000_u128,
),
(
get_account_id_from_seed::<sr25519::Public>("Miner5"),
// 10 TAO
10_000_000_000_u128,
),
];

balances.push(root_validator.clone());
balances.push(subnet_validator.clone());
balances.append(&mut miners.to_vec());

// Check if the environment variable is set
if let Ok(bt_wallet) = env::var("BT_DEFAULT_TOKEN_WALLET") {
if let Ok(decoded_wallet) = Ss58Codec::from_ss58check(&bt_wallet) {
Expand All @@ -96,6 +141,7 @@ fn localnet_genesis(
get_account_id_from_seed::<sr25519::Public>("Ferdie"),
];

let alice_account = get_account_id_from_seed::<sr25519::Public>("Alice");
serde_json::json!({
"balances": { "balances": balances },
"aura": {
Expand All @@ -116,5 +162,18 @@ fn localnet_genesis(
"senateMembers": {
"members": senate_members,
},
"subtensorModule": {
"initializeNetwork1": true,
"initializeNetwork3": false,
"rootColdkeyValidator": Some(vec![alice_account.clone(), root_validator.0]),
"subnetColdkeyValidator": Some(vec![alice_account.clone(), subnet_validator.0]),
"miners": Some(vec![
(alice_account.clone(), miners[0].0.clone()),
(alice_account.clone(), miners[1].0.clone()),
(alice_account.clone(), miners[2].0.clone()),
(alice_account.clone(), miners[3].0.clone()),
(alice_account.clone(), miners[4].0.clone()),
]),
},
})
}
1 change: 1 addition & 0 deletions node/src/chain_spec/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Allowed since it's actually better to panic during chain setup when there is an error
#![allow(clippy::unwrap_used)]

pub mod e2e_test;
pub mod finney;
pub mod localnet;
pub mod testnet;
Expand Down
1 change: 1 addition & 0 deletions node/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ impl SubstrateCli for Cli {
fn load_spec(&self, id: &str) -> Result<Box<dyn sc_service::ChainSpec>, String> {
Ok(match id {
"local" => Box::new(chain_spec::localnet::localnet_config()?),
"e2e_test" => Box::new(chain_spec::e2e_test::e2e_test_config()?),
"finney" => Box::new(chain_spec::finney::finney_mainnet_config()?),
"" | "test_finney" => Box::new(chain_spec::testnet::finney_testnet_config()?),
path => Box::new(chain_spec::ChainSpec::from_json_file(
Expand Down
38 changes: 22 additions & 16 deletions pallets/subtensor/src/coinbase/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1240,27 +1240,33 @@ impl<T: Config> Pallet<T> {
/// - The lock cost for the network.
///
pub fn get_network_lock_cost() -> u64 {
let last_lock = Self::get_network_last_lock();
let min_lock = Self::get_network_min_lock();
let last_lock_block = Self::get_network_last_lock_block();
let current_block = Self::get_current_block_as_u64();
let lock_reduction_interval = Self::get_lock_reduction_interval();
let mult = if last_lock_block == 0 { 1 } else { 2 };
#[cfg(feature = "pow-faucet")]
return 0_u64;

let mut lock_cost = last_lock.saturating_mul(mult).saturating_sub(
last_lock
.saturating_div(lock_reduction_interval)
.saturating_mul(current_block.saturating_sub(last_lock_block)),
);
#[cfg(not(feature = "pow-faucet"))]
{
let last_lock = Self::get_network_last_lock();
let min_lock = Self::get_network_min_lock();
let last_lock_block = Self::get_network_last_lock_block();
let current_block = Self::get_current_block_as_u64();
let lock_reduction_interval = Self::get_lock_reduction_interval();
let mult = if last_lock_block == 0 { 1 } else { 2 };

let mut lock_cost = last_lock.saturating_mul(mult).saturating_sub(
last_lock
.saturating_div(lock_reduction_interval)
.saturating_mul(current_block.saturating_sub(last_lock_block)),
);

if lock_cost < min_lock {
lock_cost = min_lock;
}
if lock_cost < min_lock {
lock_cost = min_lock;
}

log::debug!( "last_lock: {:?}, min_lock: {:?}, last_lock_block: {:?}, lock_reduction_interval: {:?}, current_block: {:?}, mult: {:?} lock_cost: {:?}",
log::debug!( "last_lock: {:?}, min_lock: {:?}, last_lock_block: {:?}, lock_reduction_interval: {:?}, current_block: {:?}, mult: {:?} lock_cost: {:?}",
last_lock, min_lock, last_lock_block, lock_reduction_interval, current_block, mult, lock_cost);

lock_cost
lock_cost
}
}

/// This function is used to determine which subnet to prune when the total number of networks has reached the limit.
Expand Down
15 changes: 15 additions & 0 deletions pallets/subtensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1234,13 +1234,28 @@ pub mod pallet {
pub stakes: Vec<(T::AccountId, Vec<(T::AccountId, (u64, u16))>)>,
/// The total issued balance in genesis
pub balances_issuance: u64,
/// Flag to initialize network 1.
pub initialize_network_1: bool,
/// Flag to initialize network 3.
pub initialize_network_3: bool,
/// The root validator account ID.
pub root_coldkey_validator: Option<Vec<T::AccountId>>,
/// The subnet validator account ID.
pub subnet_coldkey_validator: Option<Vec<T::AccountId>>,
/// The miner account IDs.
pub miners: Option<Vec<(T::AccountId, T::AccountId)>>,
}

impl<T: Config> Default for GenesisConfig<T> {
fn default() -> Self {
Self {
stakes: Default::default(),
balances_issuance: 0,
initialize_network_1: false,
initialize_network_3: true,
root_coldkey_validator: None,
subnet_coldkey_validator: None,
miners: None,
}
}
}
Expand Down
Loading
Loading