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(pallets/subspace): remove printlns and assert ok in staking test #28

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
98 changes: 14 additions & 84 deletions pallets/subspace/tests/delegate_staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ mod mock;
use frame_support::assert_ok;
use mock::*;
use sp_core::U256;
use substrate_fixed::types::I64F64;

// /***********************************************************
// staking::add_stake() tests
Expand Down Expand Up @@ -39,59 +38,47 @@ fn test_ownership_ratio() {
let num_modules: u16 = 10;
let tempo = 1;
let stake_per_module: u64 = 1_000_000_000;

register_n_modules(netuid, num_modules, stake_per_module);
SubspaceModule::set_tempo(netuid, tempo);

let keys = SubspaceModule::get_keys(netuid);
let voter_key = keys[0];
let miner_keys = keys[1..].to_vec();
let miner_uids: Vec<u16> =
miner_keys.iter().map(|k| SubspaceModule::get_uid_for_key(netuid, k)).collect();
let delegate_keys = (0..num_modules)
.map(|key| U256::from(key + num_modules + 1))
.collect::<Vec<U256>>();
let miner_uids = miner_keys
.iter()
.map(|key| SubspaceModule::get_uid_for_key(netuid, key))
.collect::<Vec<u16>>();
let miner_weights = vec![1; miner_uids.len()];

let delegate_keys: Vec<U256> =
(0..num_modules).map(|i| U256::from(i + num_modules + 1)).collect();
for d in delegate_keys.iter() {
add_balance(*d, stake_per_module + 1);
for key in delegate_keys.iter() {
add_balance(*key, stake_per_module + 1);
}

let pre_delegate_stake_from_vector =
SubspaceModule::get_stake_from_vector(netuid, &voter_key);
assert_eq!(pre_delegate_stake_from_vector.len(), 1); // +1 for the module itself, +1 for the delegate key on

for (i, d) in delegate_keys.iter().enumerate() {
println!("DELEGATE KEY: {}", d);
assert_ok!(SubspaceModule::add_stake(
get_origin(*d),
netuid,
voter_key,
stake_per_module
));

let stake_from_vector = SubspaceModule::get_stake_from_vector(netuid, &voter_key);
assert_eq!(
stake_from_vector.len(),
pre_delegate_stake_from_vector.len() + i + 1
);
}
let ownership_ratios: Vec<(U256, I64F64)> =
SubspaceModule::get_ownership_ratios(netuid, &voter_key);
let ownership_ratios = SubspaceModule::get_ownership_ratios(netuid, &voter_key);
assert_eq!(ownership_ratios.len(), delegate_keys.len() + 1);

let founder_tokens_before = SubspaceModule::get_balance(&voter_key)
+ SubspaceModule::get_stake_to_module(netuid, &voter_key, &voter_key);

let delegate_balances_before =
delegate_keys.iter().map(SubspaceModule::get_balance).collect::<Vec<u64>>();
let delegate_stakes_before = delegate_keys
.iter()
.map(|k| SubspaceModule::get_stake_to_module(netuid, k, &voter_key))
.collect::<Vec<u64>>();
let delegate_total_tokens_before = delegate_balances_before
.iter()
.zip(delegate_stakes_before.clone())
.map(|(a, x)| a + x)
.sum::<u64>();

let total_balance = keys
.iter()
.map(SubspaceModule::get_balance)
Expand All @@ -118,68 +105,19 @@ fn test_ownership_ratio() {
.sum::<u64>();
let total_tokens_before =
total_balance + total_stake + total_delegate_stake + total_delegate_balance;
println!("total_tokens_before: {:?}", total_tokens_before);

println!("delegate_balances before: {:?}", delegate_balances_before);
println!("delegate_stakes before: {:?}", delegate_stakes_before);
println!(
"delegate_total_tokens before: {:?}",
delegate_total_tokens_before
);

let result = SubspaceModule::set_weights(
assert_ok!(SubspaceModule::set_weights(
get_origin(voter_key),
netuid,
miner_uids.clone(),
miner_weights.clone(),
);

assert_ok!(result);
));

step_epoch(netuid);

let dividends = SubspaceModule::get_dividends(netuid);
let incentives = SubspaceModule::get_incentives(netuid);
let emissions = SubspaceModule::get_emissions(netuid);

println!("dividends: {:?}", dividends);
println!("incentives: {:?}", incentives);
println!("emissions: {:?}", emissions);
let total_emissions = emissions.iter().sum::<u64>();

println!("total_emissions: {:?}", total_emissions);

let delegate_balances =
delegate_keys.iter().map(SubspaceModule::get_balance).collect::<Vec<u64>>();
let delegate_stakes = delegate_keys
.iter()
.map(|k| SubspaceModule::get_stake_to_module(netuid, k, &voter_key))
.collect::<Vec<u64>>();
let delegate_total_tokens = delegate_balances
.iter()
.zip(delegate_stakes.clone())
.map(|(a, x)| a + x)
.sum::<u64>();
let founder_tokens = SubspaceModule::get_balance(&voter_key)
+ SubspaceModule::get_stake_to_module(netuid, &voter_key, &voter_key);
let founder_new_tokens = founder_tokens - founder_tokens_before;
let delegate_new_tokens: Vec<u64> = delegate_stakes
.iter()
.zip(delegate_stakes_before.clone())
.map(|(a, x)| a - x)
.collect::<Vec<u64>>();

let total_new_tokens = founder_new_tokens + delegate_new_tokens.iter().sum::<u64>();

println!("owner_ratios: {:?}", ownership_ratios);
println!("total_new_tokens: {:?}", total_new_tokens);
println!("founder_tokens: {:?}", founder_tokens);
println!("delegate_balances: {:?}", delegate_balances);
println!("delegate_stakes: {:?}", delegate_stakes);
println!("delegate_total_tokens: {:?}", delegate_total_tokens);
println!("founder_new_tokens: {:?}", founder_new_tokens);
println!("delegate_new_tokens: {:?}", delegate_new_tokens);

let total_balance = keys
.iter()
.map(SubspaceModule::get_balance)
Expand Down Expand Up @@ -207,14 +145,6 @@ fn test_ownership_ratio() {
let total_tokens_after =
total_balance + total_stake + total_delegate_stake + total_delegate_balance;
let total_new_tokens = total_tokens_after - total_tokens_before;
println!("total_tokens_after: {:?}", total_tokens_before);
println!("total_new_tokens: {:?}", total_new_tokens);
assert_eq!(total_new_tokens, total_emissions);

let stake_from_vector = SubspaceModule::get_stake_from_vector(netuid, &voter_key);
let _stake: u64 = SubspaceModule::get_stake(netuid, &voter_key);
let _sumed_stake: u64 = stake_from_vector.iter().fold(0, |acc, (_a, x)| acc + x);
let _total_stake: u64 = SubspaceModule::get_total_subnet_stake(netuid);
println!("stake_from_vector: {:?}", stake_from_vector);
});
}
Loading
Loading