Skip to content

Commit d8acb8b

Browse files
author
Samuel Dare
committed
feat: isabella question
1 parent 06f1b9d commit d8acb8b

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

pallets/subtensor/tests/children.rs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1487,3 +1487,99 @@ fn test_children_stake_values() {
14871487
);
14881488
});
14891489
}
1490+
1491+
// SKIP_WASM_BUILD=1 RUST_LOG=info cargo test --test children -- test_get_parents_chain --exact --nocapture
1492+
#[test]
1493+
fn test_get_parents_chain() {
1494+
new_test_ext(1).execute_with(|| {
1495+
let netuid: u16 = 1;
1496+
let coldkey = U256::from(1);
1497+
let num_keys: usize = 5;
1498+
let proportion = u64::MAX / 2; // 50% stake allocation
1499+
1500+
log::info!("Test setup: netuid={}, coldkey={}, num_keys={}, proportion={}", netuid, coldkey, num_keys, proportion);
1501+
1502+
// Create a vector of hotkeys
1503+
let hotkeys: Vec<U256> = (0..num_keys).map(|i| U256::from(i as u64 + 2)).collect();
1504+
log::info!("Created hotkeys: {:?}", hotkeys);
1505+
1506+
// Add network
1507+
add_network(netuid, 13, 0);
1508+
SubtensorModule::set_max_registrations_per_block(netuid, 1000);
1509+
SubtensorModule::set_target_registrations_per_interval(netuid, 1000);
1510+
log::info!("Network added and parameters set: netuid={}", netuid);
1511+
1512+
// Register all neurons
1513+
for hotkey in &hotkeys {
1514+
register_ok_neuron(netuid, *hotkey, coldkey, 0);
1515+
log::info!("Registered neuron: hotkey={}, coldkey={}, netuid={}", hotkey, coldkey, netuid);
1516+
}
1517+
1518+
// Set up parent-child relationships
1519+
for i in 0..num_keys - 1 {
1520+
assert_ok!(SubtensorModule::do_set_children(
1521+
RuntimeOrigin::signed(coldkey),
1522+
hotkeys[i],
1523+
netuid,
1524+
vec![(proportion, hotkeys[i + 1])]
1525+
));
1526+
log::info!("Set parent-child relationship: parent={}, child={}, proportion={}", hotkeys[i], hotkeys[i + 1], proportion);
1527+
}
1528+
1529+
// Test get_parents for each hotkey
1530+
for i in 1..num_keys {
1531+
let parents = SubtensorModule::get_parents(&hotkeys[i], netuid);
1532+
log::info!("Testing get_parents for hotkey {}: {:?}", hotkeys[i], parents);
1533+
assert_eq!(
1534+
parents.len(),
1535+
1,
1536+
"Hotkey {} should have exactly one parent",
1537+
i
1538+
);
1539+
assert_eq!(
1540+
parents[0],
1541+
(proportion, hotkeys[i - 1]),
1542+
"Incorrect parent for hotkey {}",
1543+
i
1544+
);
1545+
}
1546+
1547+
// Test get_parents for the root (should be empty)
1548+
let root_parents = SubtensorModule::get_parents(&hotkeys[0], netuid);
1549+
log::info!("Testing get_parents for root hotkey {}: {:?}", hotkeys[0], root_parents);
1550+
assert!(
1551+
root_parents.is_empty(),
1552+
"Root hotkey should have no parents"
1553+
);
1554+
1555+
// Test multiple parents
1556+
let last_hotkey = hotkeys[num_keys - 1];
1557+
let new_parent = U256::from(num_keys as u64 + 2);
1558+
register_ok_neuron(netuid, new_parent, coldkey, 0);
1559+
log::info!("Registered new parent neuron: new_parent={}, coldkey={}, netuid={}", new_parent, coldkey, netuid);
1560+
1561+
assert_ok!(SubtensorModule::do_set_children(
1562+
RuntimeOrigin::signed(coldkey),
1563+
new_parent,
1564+
netuid,
1565+
vec![(proportion / 2, last_hotkey)]
1566+
));
1567+
log::info!("Set additional parent-child relationship: parent={}, child={}, proportion={}", new_parent, last_hotkey, proportion / 2);
1568+
1569+
let last_hotkey_parents = SubtensorModule::get_parents(&last_hotkey, netuid);
1570+
log::info!("Testing get_parents for last hotkey {} with multiple parents: {:?}", last_hotkey, last_hotkey_parents);
1571+
assert_eq!(
1572+
last_hotkey_parents.len(),
1573+
2,
1574+
"Last hotkey should have two parents"
1575+
);
1576+
assert!(
1577+
last_hotkey_parents.contains(&(proportion, hotkeys[num_keys - 2])),
1578+
"Last hotkey should still have its original parent"
1579+
);
1580+
assert!(
1581+
last_hotkey_parents.contains(&(proportion / 2, new_parent)),
1582+
"Last hotkey should have the new parent"
1583+
);
1584+
});
1585+
}

0 commit comments

Comments
 (0)