|
2 | 2 | #![allow(clippy::unwrap_used)]
|
3 | 3 | #![allow(clippy::arithmetic_side_effects)]
|
4 | 4 | use super::mock::*;
|
| 5 | +use approx::assert_abs_diff_eq; |
5 | 6 | use frame_support::{assert_err, assert_noop, assert_ok};
|
6 | 7 |
|
7 | 8 | use crate::{utils::rate_limiting::TransactionType, *};
|
@@ -3091,168 +3092,168 @@ fn test_childkey_take_drain_validator_take() {
|
3091 | 3092 | // - Sets up a network with two parents, a child, and a weight setter
|
3092 | 3093 | // - Establishes parent-child relationships with different stake proportions
|
3093 | 3094 | // - Sets weights on the child and one parent
|
3094 |
| -// - Runs an epoch with a hardcoded emission value |
| 3095 | +// - Runs an epoch |
3095 | 3096 | // - Checks the emission distribution among parents, child, and weight setter
|
3096 | 3097 | // - Verifies that all parties received emissions and the total stake increased correctly
|
3097 | 3098 | // SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --test coinbase test_childkey_multiple_parents_emission -- --nocapture
|
3098 | 3099 | #[test]
|
3099 | 3100 | fn test_childkey_multiple_parents_emission() {
|
3100 | 3101 | new_test_ext(1).execute_with(|| {
|
3101 |
| - assert!(false); |
3102 |
| - |
3103 |
| - // let netuid: u16 = 1; |
3104 |
| - // add_network(netuid, 1, 0); |
3105 |
| - |
3106 |
| - // // Set registration parameters and emission tempo |
3107 |
| - // SubtensorModule::set_max_registrations_per_block(netuid, 1000); |
3108 |
| - // SubtensorModule::set_target_registrations_per_interval(netuid, 1000); |
3109 |
| - // SubtensorModule::set_hotkey_emission_tempo(10); |
3110 |
| - |
3111 |
| - // // Define hotkeys and coldkeys |
3112 |
| - // let parent1: U256 = U256::from(1); |
3113 |
| - // let parent2: U256 = U256::from(2); |
3114 |
| - // let child: U256 = U256::from(3); |
3115 |
| - // let weight_setter: U256 = U256::from(4); |
3116 |
| - // let coldkey_parent1: U256 = U256::from(100); |
3117 |
| - // let coldkey_parent2: U256 = U256::from(101); |
3118 |
| - // let coldkey_child: U256 = U256::from(102); |
3119 |
| - // let coldkey_weight_setter: U256 = U256::from(103); |
3120 |
| - |
3121 |
| - // // Register neurons and add initial stakes |
3122 |
| - // let initial_stakes: Vec<(U256, U256, u64)> = vec![ |
3123 |
| - // (coldkey_parent1, parent1, 200_000), |
3124 |
| - // (coldkey_parent2, parent2, 150_000), |
3125 |
| - // (coldkey_child, child, 20_000), |
3126 |
| - // (coldkey_weight_setter, weight_setter, 100_000), |
3127 |
| - // ]; |
3128 |
| - |
3129 |
| - // for (coldkey, hotkey, stake) in initial_stakes.iter() { |
3130 |
| - // SubtensorModule::add_balance_to_coldkey_account(coldkey, *stake); |
3131 |
| - // register_ok_neuron(netuid, *hotkey, *coldkey, 0); |
3132 |
| - // SubtensorModule::increase_stake_on_coldkey_hotkey_account(coldkey, hotkey, *stake); |
3133 |
| - // } |
3134 |
| - |
3135 |
| - // SubtensorModule::set_weights_set_rate_limit(netuid, 0); |
3136 |
| - // step_block(2); |
| 3102 | + let subnet_owner_coldkey = U256::from(1001); |
| 3103 | + let subnet_owner_hotkey = U256::from(1002); |
| 3104 | + let netuid = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey); |
| 3105 | + Tempo::<Test>::insert(netuid, 10); // run epoch every 10 blocks |
3137 | 3106 |
|
3138 |
| - // // Set parent-child relationships |
3139 |
| - // mock_set_children(&coldkey_parent1, &parent1, netuid, &[(100_000, child)]); |
3140 |
| - // mock_set_children(&coldkey_parent2, &parent2, netuid, &[(75_000, child)]); |
| 3107 | + // Set registration parameters and emission tempo |
| 3108 | + SubtensorModule::set_max_registrations_per_block(netuid, 1000); |
| 3109 | + SubtensorModule::set_target_registrations_per_interval(netuid, 1000); |
3141 | 3110 |
|
3142 |
| - // // Set weights |
3143 |
| - // let uids: Vec<u16> = vec![0, 1, 2]; |
3144 |
| - // let values: Vec<u16> = vec![0, 65354, 65354]; |
3145 |
| - // let version_key = SubtensorModule::get_weights_version_key(netuid); |
3146 |
| - // assert_ok!(SubtensorModule::set_weights( |
3147 |
| - // RuntimeOrigin::signed(weight_setter), |
3148 |
| - // netuid, |
3149 |
| - // uids, |
3150 |
| - // values, |
3151 |
| - // version_key |
3152 |
| - // )); |
| 3111 | + // Define hotkeys and coldkeys |
| 3112 | + let parent1: U256 = U256::from(1); |
| 3113 | + let parent2: U256 = U256::from(2); |
| 3114 | + let child: U256 = U256::from(3); |
| 3115 | + let weight_setter: U256 = U256::from(4); |
| 3116 | + let coldkey_parent1: U256 = U256::from(100); |
| 3117 | + let coldkey_parent2: U256 = U256::from(101); |
| 3118 | + let coldkey_child: U256 = U256::from(102); |
| 3119 | + let coldkey_weight_setter: U256 = U256::from(103); |
| 3120 | + |
| 3121 | + // Register neurons and add initial stakes |
| 3122 | + let initial_stakes: Vec<(bool, U256, U256, u64)> = vec![ |
| 3123 | + (false, coldkey_parent1, parent1, 200_000), |
| 3124 | + (true, coldkey_parent2, parent2, 150_000), |
| 3125 | + (true, coldkey_child, child, 20_000), |
| 3126 | + (true, coldkey_weight_setter, weight_setter, 100_000), |
| 3127 | + ]; |
| 3128 | + |
| 3129 | + for (register, coldkey, hotkey, stake) in initial_stakes.iter() { |
| 3130 | + SubtensorModule::add_balance_to_coldkey_account(coldkey, *stake); |
| 3131 | + if *register { |
| 3132 | + // Register a neuron |
| 3133 | + register_ok_neuron(netuid, *hotkey, *coldkey, 0); |
| 3134 | + } else { |
| 3135 | + // Just create hotkey account |
| 3136 | + SubtensorModule::create_account_if_non_existent(coldkey, hotkey); |
| 3137 | + } |
| 3138 | + assert_ok!(SubtensorModule::add_stake( |
| 3139 | + RuntimeOrigin::signed(*coldkey), |
| 3140 | + *hotkey, |
| 3141 | + netuid, |
| 3142 | + *stake |
| 3143 | + )); |
| 3144 | + } |
3153 | 3145 |
|
3154 |
| - // // Run epoch with a hardcoded emission value |
3155 |
| - // let hardcoded_emission: u64 = 1_000_000_000; // 1 billion |
3156 |
| - // let hotkey_emission: Vec<(U256, u64, u64)> = |
3157 |
| - // SubtensorModule::epoch(netuid, hardcoded_emission); |
| 3146 | + SubtensorModule::set_weights_set_rate_limit(netuid, 0); |
| 3147 | + step_block(2); |
3158 | 3148 |
|
3159 |
| - // // Process the hotkey emission results |
3160 |
| - // for (hotkey, mining_emission, validator_emission) in hotkey_emission { |
3161 |
| - // SubtensorModule::accumulate_hotkey_emission( |
3162 |
| - // &hotkey, |
3163 |
| - // netuid, |
3164 |
| - // validator_emission, |
3165 |
| - // mining_emission, |
3166 |
| - // ); |
3167 |
| - // log::debug!( |
3168 |
| - // "Accumulated emissions on hotkey {:?} for netuid {:?}: mining {:?}, validator {:?}", |
3169 |
| - // hotkey, |
3170 |
| - // netuid, |
3171 |
| - // mining_emission, |
3172 |
| - // validator_emission |
3173 |
| - // ); |
3174 |
| - // } |
| 3149 | + // Set parent-child relationships |
| 3150 | + mock_set_children(&coldkey_parent1, &parent1, netuid, &[(u64::MAX, child)]); |
| 3151 | + mock_set_children(&coldkey_parent2, &parent2, netuid, &[(u64::MAX / 2, child)]); |
| 3152 | + ChildkeyTake::<Test>::insert(child, netuid, u16::MAX/5); |
3175 | 3153 |
|
3176 |
| - // step_block(11); |
| 3154 | + // Set weights (subnet owner is uid 0, ignore him) |
| 3155 | + let uids: Vec<u16> = vec![1, 2]; |
| 3156 | + let values: Vec<u16> = vec![65354, 65354]; |
| 3157 | + let version_key = SubtensorModule::get_weights_version_key(netuid); |
| 3158 | + ValidatorPermit::<Test>::insert(netuid, vec![true, true, true, true]); |
| 3159 | + assert_ok!(SubtensorModule::set_weights( |
| 3160 | + RuntimeOrigin::signed(weight_setter), |
| 3161 | + netuid, |
| 3162 | + uids, |
| 3163 | + values, |
| 3164 | + version_key |
| 3165 | + )); |
3177 | 3166 |
|
3178 |
| - // // Check emission distribution |
3179 |
| - // let stakes: Vec<(U256, U256, &str)> = vec![ |
3180 |
| - // (coldkey_parent1, parent1, "Parent1"), |
3181 |
| - // (coldkey_parent2, parent2, "Parent2"), |
3182 |
| - // (coldkey_child, child, "Child"), |
3183 |
| - // (coldkey_weight_setter, weight_setter, "Weight setter"), |
3184 |
| - // ]; |
3185 |
| - |
3186 |
| - // for (coldkey, hotkey, name) in stakes.iter() { |
3187 |
| - // let stake = SubtensorModule::get_stake_for_coldkey_and_hotkey(coldkey, hotkey); |
3188 |
| - // let stake_on_subnet = SubtensorModule::get_stake_for_hotkey_on_subnet(hotkey, netuid); |
3189 |
| - // log::debug!( |
3190 |
| - // "{} stake: {:?}, {} stake on subnet: {:?}", |
3191 |
| - // name, |
3192 |
| - // stake, |
3193 |
| - // name, |
3194 |
| - // stake_on_subnet |
3195 |
| - // ); |
3196 |
| - // } |
| 3167 | + // Wait until epoch |
| 3168 | + let start_block = SubtensorModule::get_current_block_as_u64(); |
| 3169 | + loop { |
| 3170 | + let current_block = SubtensorModule::get_current_block_as_u64(); |
| 3171 | + if SubtensorModule::should_run_epoch(netuid, current_block) { |
| 3172 | + step_block(1); |
| 3173 | + break; |
| 3174 | + } |
| 3175 | + step_block(1); |
| 3176 | + } |
| 3177 | + let total_emission = |
| 3178 | + SubtensorModule::get_block_emission().unwrap_or(0) * |
| 3179 | + (SubtensorModule::get_current_block_as_u64() - start_block + 1); |
| 3180 | + |
| 3181 | + // Check emission distribution |
| 3182 | + let stakes: Vec<(U256, U256, &str)> = vec![ |
| 3183 | + (coldkey_parent1, parent1, "Parent1"), |
| 3184 | + (coldkey_parent2, parent2, "Parent2"), |
| 3185 | + (coldkey_child, child, "Child"), |
| 3186 | + (coldkey_weight_setter, weight_setter, "Weight setter"), |
| 3187 | + ]; |
| 3188 | + |
| 3189 | + for (coldkey, hotkey, name) in stakes.iter() { |
| 3190 | + let stake_on_subnet = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(hotkey, coldkey, netuid); |
| 3191 | + log::debug!( |
| 3192 | + "{} stake on subnet: {:?}", |
| 3193 | + name, |
| 3194 | + stake_on_subnet |
| 3195 | + ); |
| 3196 | + } |
3197 | 3197 |
|
3198 |
| - // let parent1_stake = |
3199 |
| - // SubtensorModule::get_stake_for_coldkey_and_hotkey(&coldkey_parent1, &parent1); |
3200 |
| - // let parent2_stake = |
3201 |
| - // SubtensorModule::get_stake_for_coldkey_and_hotkey(&coldkey_parent2, &parent2); |
3202 |
| - // let child_stake = SubtensorModule::get_stake_for_coldkey_and_hotkey(&coldkey_child, &child); |
3203 |
| - // let weight_setter_stake = SubtensorModule::get_stake_for_coldkey_and_hotkey( |
3204 |
| - // &coldkey_weight_setter, |
3205 |
| - // &weight_setter, |
3206 |
| - // ); |
| 3198 | + let parent1_stake = |
| 3199 | + SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&parent1, &coldkey_parent1, netuid); |
| 3200 | + let parent2_stake = |
| 3201 | + SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&parent2, &coldkey_parent2, netuid); |
| 3202 | + let child_stake = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&child, &coldkey_child, netuid); |
| 3203 | + let weight_setter_stake = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( |
| 3204 | + &weight_setter, |
| 3205 | + &coldkey_weight_setter, |
| 3206 | + netuid, |
| 3207 | + ); |
3207 | 3208 |
|
3208 |
| - // assert!( |
3209 |
| - // parent1_stake > 200_000, |
3210 |
| - // "Parent1 should have received emission" |
3211 |
| - // ); |
3212 |
| - // assert!( |
3213 |
| - // parent2_stake > 150_000, |
3214 |
| - // "Parent2 should have received emission" |
3215 |
| - // ); |
3216 |
| - // assert!(child_stake > 20_000, "Child should have received emission"); |
3217 |
| - // assert!( |
3218 |
| - // weight_setter_stake > 100_000, |
3219 |
| - // "Weight setter should have received emission" |
3220 |
| - // ); |
| 3209 | + assert!( |
| 3210 | + parent1_stake > 200_000, |
| 3211 | + "Parent1 should have received emission" |
| 3212 | + ); |
| 3213 | + assert!( |
| 3214 | + parent2_stake > 150_000, |
| 3215 | + "Parent2 should have received emission" |
| 3216 | + ); |
| 3217 | + assert!(child_stake > 20_000, "Child should have received emission"); |
| 3218 | + assert!( |
| 3219 | + weight_setter_stake > 100_000, |
| 3220 | + "Weight setter should have received emission" |
| 3221 | + ); |
3221 | 3222 |
|
3222 |
| - // // Check individual stake increases |
3223 |
| - // let parent1_stake_increase = parent1_stake - 200_000; |
3224 |
| - // let parent2_stake_increase = parent2_stake - 150_000; |
3225 |
| - // let child_stake_increase = child_stake - 20_000; |
| 3223 | + // Check individual stake increases |
| 3224 | + let parent1_stake_increase = parent1_stake - 200_000; |
| 3225 | + let parent2_stake_increase = parent2_stake - 150_000; |
| 3226 | + let child_stake_increase = child_stake - 20_000; |
3226 | 3227 |
|
3227 |
| - // log::debug!( |
3228 |
| - // "Stake increases - Parent1: {}, Parent2: {}, Child: {}", |
3229 |
| - // parent1_stake_increase, |
3230 |
| - // parent2_stake_increase, |
3231 |
| - // child_stake_increase |
3232 |
| - // ); |
| 3228 | + log::debug!( |
| 3229 | + "Stake increases - Parent1: {}, Parent2: {}, Child: {}", |
| 3230 | + parent1_stake_increase, |
| 3231 | + parent2_stake_increase, |
| 3232 | + child_stake_increase |
| 3233 | + ); |
3233 | 3234 |
|
3234 |
| - // // Assert that all neurons received some emission |
3235 |
| - // assert!( |
3236 |
| - // parent1_stake_increase > 0, |
3237 |
| - // "Parent1 should have received some emission" |
3238 |
| - // ); |
3239 |
| - // assert!( |
3240 |
| - // parent2_stake_increase > 0, |
3241 |
| - // "Parent2 should have received some emission" |
3242 |
| - // ); |
3243 |
| - // assert!( |
3244 |
| - // child_stake_increase > 0, |
3245 |
| - // "Child should have received some emission" |
3246 |
| - // ); |
| 3235 | + // Assert that all neurons received some emission |
| 3236 | + assert!( |
| 3237 | + parent1_stake_increase > 0, |
| 3238 | + "Parent1 should have received some emission" |
| 3239 | + ); |
| 3240 | + assert!( |
| 3241 | + parent2_stake_increase > 0, |
| 3242 | + "Parent2 should have received some emission" |
| 3243 | + ); |
| 3244 | + assert!( |
| 3245 | + child_stake_increase > 0, |
| 3246 | + "Child should have received some emission" |
| 3247 | + ); |
3247 | 3248 |
|
3248 |
| - // // Check that the total stake has increased by the hardcoded emission amount |
3249 |
| - // let total_stake = parent1_stake + parent2_stake + child_stake + weight_setter_stake; |
3250 |
| - // let initial_total_stake: u64 = initial_stakes.iter().map(|(_, _, stake)| stake).sum(); |
3251 |
| - // assert_eq!( |
3252 |
| - // total_stake, |
3253 |
| - // initial_total_stake + hardcoded_emission - 2, // U64::MAX normalization rounding error |
3254 |
| - // "Total stake should have increased by the hardcoded emission amount" |
3255 |
| - // ); |
| 3249 | + // Check that the total stake has increased by the emission amount |
| 3250 | + let total_stake = parent1_stake + parent2_stake + child_stake + weight_setter_stake; |
| 3251 | + let initial_total_stake: u64 = initial_stakes.iter().map(|(_, _, _, stake)| stake).sum(); |
| 3252 | + assert_abs_diff_eq!( |
| 3253 | + total_stake, |
| 3254 | + initial_total_stake + total_emission - 2, |
| 3255 | + epsilon = total_emission / 10_000 |
| 3256 | + ); |
3256 | 3257 | });
|
3257 | 3258 | }
|
3258 | 3259 |
|
|
0 commit comments