diff --git a/pallets/subspace/tests/delegate_staking.rs b/pallets/subspace/tests/delegate_staking.rs index 7cebdbac9..5a523298c 100644 --- a/pallets/subspace/tests/delegate_staking.rs +++ b/pallets/subspace/tests/delegate_staking.rs @@ -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 @@ -39,20 +38,24 @@ 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 = - 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::>(); + let miner_uids = miner_keys + .iter() + .map(|key| SubspaceModule::get_uid_for_key(netuid, key)) + .collect::>(); let miner_weights = vec![1; miner_uids.len()]; - let delegate_keys: Vec = - (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 = @@ -60,38 +63,22 @@ fn test_ownership_ratio() { 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::>(); - let delegate_stakes_before = delegate_keys - .iter() - .map(|k| SubspaceModule::get_stake_to_module(netuid, k, &voter_key)) - .collect::>(); - let delegate_total_tokens_before = delegate_balances_before - .iter() - .zip(delegate_stakes_before.clone()) - .map(|(a, x)| a + x) - .sum::(); - let total_balance = keys .iter() .map(SubspaceModule::get_balance) @@ -118,68 +105,19 @@ fn test_ownership_ratio() { .sum::(); 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::(); - println!("total_emissions: {:?}", total_emissions); - - let delegate_balances = - delegate_keys.iter().map(SubspaceModule::get_balance).collect::>(); - let delegate_stakes = delegate_keys - .iter() - .map(|k| SubspaceModule::get_stake_to_module(netuid, k, &voter_key)) - .collect::>(); - let delegate_total_tokens = delegate_balances - .iter() - .zip(delegate_stakes.clone()) - .map(|(a, x)| a + x) - .sum::(); - 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 = delegate_stakes - .iter() - .zip(delegate_stakes_before.clone()) - .map(|(a, x)| a - x) - .collect::>(); - - let total_new_tokens = founder_new_tokens + delegate_new_tokens.iter().sum::(); - - 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) @@ -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); }); } diff --git a/pallets/subspace/tests/staking.rs b/pallets/subspace/tests/staking.rs index c6fa01cda..b63410f84 100644 --- a/pallets/subspace/tests/staking.rs +++ b/pallets/subspace/tests/staking.rs @@ -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 @@ -38,44 +37,40 @@ fn test_stake() { let max_uids: u16 = 10; let token_amount: u64 = 1_000_000_000; let netuids: [u16; 4] = core::array::from_fn(|i| i as u16); - let amount_staked_vector: Vec = netuids.iter().map(|_| 10 * token_amount).collect(); + let stake_amounts: Vec = netuids.iter().map(|_| 10 * token_amount).collect(); let mut total_stake: u64 = 0; let mut subnet_stake: u64 = 0; for netuid in netuids { - println!("NETUID: {}", netuid); - let amount_staked = amount_staked_vector[netuid as usize]; - let key_vector: Vec = - (0..max_uids).map(|i| U256::from(i + max_uids * netuid)).collect(); + let staked_amount = stake_amounts[netuid as usize]; + let key_vector = + (0..max_uids).map(|i| U256::from(i + max_uids * netuid)).collect::>(); for key in key_vector.iter() { - println!( - " KEY {} KEY STAKE {} STAKING AMOUNT {} ", - key, - SubspaceModule::get_stake(netuid, key), - amount_staked - ); - - assert_ok!(register_module(netuid, *key, amount_staked)); - // add_stake_and_balance(netuid, *key, amount_staked); - println!( - " KEY STAKE {} STAKING AMOUNT {} ", - SubspaceModule::get_stake(netuid, key), - amount_staked - ); + assert_ok!(register_module(netuid, *key, staked_amount)); // SubspaceModule::add_stake(get_origin(*key), netuid, amount_staked); - assert_eq!(SubspaceModule::get_stake(netuid, key), amount_staked); + assert_eq!(SubspaceModule::get_stake(netuid, key), staked_amount); assert_eq!(SubspaceModule::get_balance(key), 1); // REMOVE STAKE - SubspaceModule::remove_stake(get_origin(*key), netuid, *key, amount_staked); - assert_eq!(SubspaceModule::get_balance(key), amount_staked + 1); + assert_ok!(SubspaceModule::remove_stake( + get_origin(*key), + netuid, + *key, + staked_amount + )); + assert_eq!(SubspaceModule::get_balance(key), staked_amount + 1); assert_eq!(SubspaceModule::get_stake(netuid, key), 0); // ADD STAKE AGAIN LOL - SubspaceModule::add_stake(get_origin(*key), netuid, *key, amount_staked); - assert_eq!(SubspaceModule::get_stake(netuid, key), amount_staked); + assert_ok!(SubspaceModule::add_stake( + get_origin(*key), + netuid, + *key, + staked_amount + )); + assert_eq!(SubspaceModule::get_stake(netuid, key), staked_amount); assert_eq!(SubspaceModule::get_balance(key), 1); // AT THE END WE SHOULD HAVE THE SAME TOTAL STAKE @@ -85,11 +80,6 @@ fn test_stake() { total_stake += subnet_stake; assert_eq!(SubspaceModule::total_stake(), total_stake); subnet_stake = 0; - println!("TOTAL STAKE: {}", total_stake); - println!( - "TOTAL SUBNET STAKE: {}", - SubspaceModule::get_total_subnet_stake(netuid) - ); } }); } @@ -99,12 +89,9 @@ fn test_multiple_stake() { new_test_ext().execute_with(|| { let n: u16 = 10; let stake_amount: u64 = 10_000_000_000; - let _total_stake: u64 = 0; let netuid: u16 = 0; - let _subnet_stake: u64 = 0; - let _uid: u16 = 0; - let num_staked_modules: u16 = 10; - let total_stake: u64 = stake_amount * num_staked_modules as u64; + let staked_modules: u16 = 10; + let total_stake: u64 = stake_amount * staked_modules as u64; register_n_modules(netuid, n, 0); let controler_key = U256::from(n + 1); @@ -114,25 +101,20 @@ fn test_multiple_stake() { let keys: Vec = SubspaceModule::get_keys(netuid); // stake to all modules + let stake_amounts: Vec = vec![stake_amount; staked_modules as usize]; - let stake_amounts: Vec = vec![stake_amount; num_staked_modules as usize]; - - println!("STAKE AMOUNTS: {:?}", stake_amounts); - let total_actual_stake: u64 = - keys.clone().into_iter().map(|k| SubspaceModule::get_stake(netuid, &k)).sum(); - let staker_balance = SubspaceModule::get_balance(&controler_key); - println!("TOTAL ACTUAL STAKE: {}", total_actual_stake); - println!("TOTAL STAKE: {}", total_stake); - println!("STAKER BALANCE: {}", staker_balance); - SubspaceModule::add_stake_multiple( + assert_ok!(SubspaceModule::add_stake_multiple( get_origin(controler_key), netuid, keys.clone(), stake_amounts.clone(), - ); + )); - let total_actual_stake: u64 = - keys.clone().into_iter().map(|k| SubspaceModule::get_stake(netuid, &k)).sum(); + let total_actual_stake = keys + .clone() + .into_iter() + .map(|k| SubspaceModule::get_stake(netuid, &k)) + .sum::(); let staker_balance = SubspaceModule::get_balance(&controler_key); assert_eq!( @@ -146,15 +128,18 @@ fn test_multiple_stake() { ); // unstake from all modules - SubspaceModule::remove_stake_multiple( + assert_ok!(SubspaceModule::remove_stake_multiple( get_origin(controler_key), netuid, keys.clone(), stake_amounts.clone(), - ); + )); - let total_actual_stake: u64 = - keys.clone().into_iter().map(|k| SubspaceModule::get_stake(netuid, &k)).sum(); + let total_actual_stake = keys + .clone() + .into_iter() + .map(|k| SubspaceModule::get_stake(netuid, &k)) + .sum::(); let staker_balance = SubspaceModule::get_balance(&controler_key); assert_eq!( total_actual_stake, 0, @@ -172,25 +157,30 @@ fn test_transfer_stake() { new_test_ext().execute_with(|| { let n: u16 = 10; let stake_amount: u64 = 10_000_000_000; - let _total_stake: u64 = 0; let netuid: u16 = 0; - let _subnet_stake: u64 = 0; - let _uid: u16 = 0; - let num_staked_modules: u16 = 10; - let _total_stake: u64 = stake_amount * num_staked_modules as u64; register_n_modules(netuid, n, stake_amount); let keys: Vec = SubspaceModule::get_keys(netuid); - - SubspaceModule::transfer_stake(get_origin(keys[0]), netuid, keys[0], keys[1], stake_amount); + assert_ok!(SubspaceModule::transfer_stake( + get_origin(keys[0]), + netuid, + keys[0], + keys[1], + stake_amount + )); let key0_stake = SubspaceModule::get_stake(netuid, &keys[0]); let key1_stake = SubspaceModule::get_stake(netuid, &keys[1]); assert_eq!(key0_stake, 0); assert_eq!(key1_stake, stake_amount * 2); - - SubspaceModule::transfer_stake(get_origin(keys[0]), netuid, keys[1], keys[0], stake_amount); + assert_ok!(SubspaceModule::transfer_stake( + get_origin(keys[0]), + netuid, + keys[1], + keys[0], + stake_amount + )); let key0_stake = SubspaceModule::get_stake(netuid, &keys[0]); let key1_stake = SubspaceModule::get_stake(netuid, &keys[1]); @@ -205,43 +195,32 @@ fn test_delegate_stake() { let max_uids: u16 = 10; let token_amount: u64 = 1_000_000_000; let netuids: Vec = [0, 1, 2, 3].to_vec(); - let amount_staked_vector: Vec = netuids.iter().map(|_i| 10 * token_amount).collect(); + let staked_amounts: Vec = netuids.iter().map(|_i| 10 * token_amount).collect(); let mut total_stake: u64 = 0; let mut subnet_stake: u64 = 0; - for i in netuids.iter() { - let netuid = *i; - println!("NETUID: {}", netuid); - let amount_staked = amount_staked_vector[netuid as usize]; - let key_vector: Vec = - (0..max_uids).map(|i| U256::from(i + max_uids * netuid)).collect(); - let delegate_key_vector: Vec = key_vector.iter().map(|i| (*i + 1)).collect(); - - for (i, key) in key_vector.iter().enumerate() { - println!( - " KEY {} KEY STAKE {} STAKING AMOUNT {} ", - key, - SubspaceModule::get_stake(netuid, key), - amount_staked - ); + for netuid in netuids.into_iter() { + let staked_amount = staked_amounts[netuid as usize]; + let keys = + (0..max_uids).map(|i| U256::from(i + max_uids * netuid)).collect::>(); + let delegate_keys: Vec = keys.iter().map(|i| (*i + 1)).collect(); - let delegate_key: U256 = delegate_key_vector[i]; - add_balance(delegate_key, amount_staked + 1); + for (i, key) in keys.iter().enumerate() { + let delegate_key: U256 = delegate_keys[i]; + add_balance(delegate_key, staked_amount + 1); - register_module(netuid, *key, 0); - // add_stake_and_balance(netuid, *key, amount_staked); - println!( - " DELEGATE KEY STAKE {} STAKING AMOUNT {} ", - SubspaceModule::get_stake(netuid, &delegate_key), - amount_staked - ); + assert_ok!(register_module(netuid, *key, 0)); + assert_ok!(SubspaceModule::add_stake( + get_origin(delegate_key), + netuid, + *key, + staked_amount + )); - SubspaceModule::add_stake(get_origin(delegate_key), netuid, *key, amount_staked); let uid = SubspaceModule::get_uid_for_key(netuid, key); - // SubspaceModule::add_stake(get_origin(*key), netuid, amount_staked); assert_eq!( SubspaceModule::get_stake_for_uid(netuid, uid), - amount_staked + staked_amount ); assert_eq!(SubspaceModule::get_balance(&delegate_key), 1); assert_eq!( @@ -249,10 +228,15 @@ fn test_delegate_stake() { 1 ); // REMOVE STAKE - SubspaceModule::remove_stake(get_origin(delegate_key), netuid, *key, amount_staked); + assert_ok!(SubspaceModule::remove_stake( + get_origin(delegate_key), + netuid, + *key, + staked_amount + )); assert_eq!( SubspaceModule::get_balance(&delegate_key), - amount_staked + 1 + staked_amount + 1 ); assert_eq!(SubspaceModule::get_stake_for_uid(netuid, uid), 0); assert_eq!( @@ -261,10 +245,15 @@ fn test_delegate_stake() { ); // ADD STAKE AGAIN LOL - SubspaceModule::add_stake(get_origin(delegate_key), netuid, *key, amount_staked); + assert_ok!(SubspaceModule::add_stake( + get_origin(delegate_key), + netuid, + *key, + staked_amount + )); assert_eq!( SubspaceModule::get_stake_for_uid(netuid, uid), - amount_staked + staked_amount ); assert_eq!(SubspaceModule::get_balance(&delegate_key), 1); assert_eq!( @@ -279,11 +268,6 @@ fn test_delegate_stake() { total_stake += subnet_stake; assert_eq!(SubspaceModule::total_stake(), total_stake); subnet_stake = 0; - println!("TOTAL STAKE: {}", total_stake); - println!( - "TOTAL SUBNET STAKE: {}", - SubspaceModule::get_total_subnet_stake(netuid) - ); } }); } @@ -301,47 +285,34 @@ fn test_ownership_ratio() { for k in &keys { let delegate_keys: Vec = (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 delegate_key in delegate_keys.iter() { + add_balance(*delegate_key, stake_per_module + 1); } - let pre_delegate_stake_from_vector = SubspaceModule::get_stake_from_vector(netuid, k); - assert_eq!(pre_delegate_stake_from_vector.len(), 1); // +1 for the module itself, +1 for the delegate key on + let pre_delegate_stake = SubspaceModule::get_stake_from_vector(netuid, k); + assert_eq!(pre_delegate_stake.len(), 1); // +1 for the module itself, +1 for the delegate key on - println!("KEY: {}", k); - for (i, d) in delegate_keys.iter().enumerate() { - println!("DELEGATE KEY: {}", d); - SubspaceModule::add_stake(get_origin(*d), netuid, *k, stake_per_module); + for (i, delegate_key) in delegate_keys.iter().enumerate() { + assert_ok!(SubspaceModule::add_stake( + get_origin(*delegate_key), + netuid, + *k, + stake_per_module + )); let stake_from_vector = SubspaceModule::get_stake_from_vector(netuid, k); - assert_eq!( - stake_from_vector.len(), - pre_delegate_stake_from_vector.len() + i + 1 - ); + assert_eq!(stake_from_vector.len(), pre_delegate_stake.len() + i + 1); } - let ownership_ratios: Vec<(U256, I64F64)> = - SubspaceModule::get_ownership_ratios(netuid, k); - assert_eq!(ownership_ratios.len(), delegate_keys.len() + 1); - println!("OWNERSHIP RATIOS: {:?}", ownership_ratios); - // step_block(); + let ownership_ratios = SubspaceModule::get_ownership_ratios(netuid, k); + assert_eq!(ownership_ratios.len(), delegate_keys.len() + 1); step_epoch(netuid); let stake_from_vector = SubspaceModule::get_stake_from_vector(netuid, k); let stake: u64 = SubspaceModule::get_stake(netuid, k); - 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: {}", stake); - println!("SUMED STAKE: {}", sumed_stake); - println!("TOTAL STAKE: {}", total_stake); - - assert_eq!(stake, sumed_stake); + let sum: u64 = stake_from_vector.iter().fold(0, |acc, (_, x)| acc + x); - // for (d_a, o) in ownership_ratios.iter() { - // println!("OWNERSHIP RATIO: {}", o); - - // } + assert_eq!(stake, sum); } }); } @@ -358,6 +329,11 @@ fn test_min_stake() { SubspaceModule::set_min_stake(netuid, min_stake - 100); - SubspaceModule::remove_stake(get_origin(keys[0]), netuid, keys[0], 10_000_000_000); + assert_ok!(SubspaceModule::remove_stake( + get_origin(keys[0]), + netuid, + keys[0], + 10_000_000_000 + )); }); }