Skip to content

Commit 5d48fae

Browse files
author
Samuel Dare
committed
chore: PR review comments
1 parent d8acb8b commit 5d48fae

File tree

5 files changed

+117
-17
lines changed

5 files changed

+117
-17
lines changed

pallets/subtensor/src/lib.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -628,8 +628,15 @@ pub mod pallet {
628628
StorageMap<_, Blake2_128Concat, T::AccountId, u16, ValueQuery, DefaultDelegateTake<T>>;
629629
#[pallet::storage]
630630
/// DMAP ( hot, netuid ) --> take | Returns the hotkey childkey take for a specific subnet
631-
pub type ChildkeyTake<T: Config> =
632-
StorageMap<_, Blake2_128Concat, (T::AccountId, u16), u16, ValueQuery>;
631+
pub type ChildkeyTake<T: Config> = StorageDoubleMap<
632+
_,
633+
Blake2_128Concat,
634+
T::AccountId, // First key: hotkey
635+
Identity,
636+
u16, // Second key: netuid
637+
u16, // Value: take
638+
ValueQuery,
639+
>;
633640

634641
#[pallet::storage]
635642
/// DMAP ( hot, cold ) --> stake | Returns the stake under a coldkey prefixed by hotkey.

pallets/subtensor/src/staking/set_children.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ impl<T: Config> Pallet<T> {
130130
// --- 7.1. Insert my new children + proportion list into the map.
131131
ChildKeys::<T>::insert(hotkey.clone(), netuid, children.clone());
132132

133+
if children.is_empty() {
134+
// If there are no children, remove the ChildkeyTake value
135+
ChildkeyTake::<T>::remove(hotkey.clone(), netuid);
136+
}
137+
133138
// --- 7.2. Update the parents list for my new children.
134139
for (proportion, new_child_i) in children.clone().iter() {
135140
// --- 8.2.1. Get the child's parents on this network.
@@ -243,7 +248,7 @@ impl<T: Config> Pallet<T> {
243248
);
244249

245250
// Set the new childkey take value for the given hotkey and network
246-
ChildkeyTake::<T>::insert((hotkey.clone(), netuid), take);
251+
ChildkeyTake::<T>::insert(hotkey.clone(), netuid, take);
247252

248253
// TODO: Consider adding a check to ensure the hotkey is registered on the specified network (netuid)
249254
// before setting the childkey take. This could prevent setting takes for non-existent or
@@ -286,6 +291,6 @@ impl<T: Config> Pallet<T> {
286291
/// * `u16` - The childkey take value. This is a percentage represented as a value between 0 and 10000,
287292
/// where 10000 represents 100%.
288293
pub fn get_childkey_take(hotkey: &T::AccountId, netuid: u16) -> u16 {
289-
ChildkeyTake::<T>::get((hotkey, netuid))
294+
ChildkeyTake::<T>::get(hotkey, netuid)
290295
}
291296
}

pallets/subtensor/tests/children.rs

Lines changed: 98 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,7 +1497,13 @@ fn test_get_parents_chain() {
14971497
let num_keys: usize = 5;
14981498
let proportion = u64::MAX / 2; // 50% stake allocation
14991499

1500-
log::info!("Test setup: netuid={}, coldkey={}, num_keys={}, proportion={}", netuid, coldkey, num_keys, proportion);
1500+
log::info!(
1501+
"Test setup: netuid={}, coldkey={}, num_keys={}, proportion={}",
1502+
netuid,
1503+
coldkey,
1504+
num_keys,
1505+
proportion
1506+
);
15011507

15021508
// Create a vector of hotkeys
15031509
let hotkeys: Vec<U256> = (0..num_keys).map(|i| U256::from(i as u64 + 2)).collect();
@@ -1512,7 +1518,12 @@ fn test_get_parents_chain() {
15121518
// Register all neurons
15131519
for hotkey in &hotkeys {
15141520
register_ok_neuron(netuid, *hotkey, coldkey, 0);
1515-
log::info!("Registered neuron: hotkey={}, coldkey={}, netuid={}", hotkey, coldkey, netuid);
1521+
log::info!(
1522+
"Registered neuron: hotkey={}, coldkey={}, netuid={}",
1523+
hotkey,
1524+
coldkey,
1525+
netuid
1526+
);
15161527
}
15171528

15181529
// Set up parent-child relationships
@@ -1523,13 +1534,22 @@ fn test_get_parents_chain() {
15231534
netuid,
15241535
vec![(proportion, hotkeys[i + 1])]
15251536
));
1526-
log::info!("Set parent-child relationship: parent={}, child={}, proportion={}", hotkeys[i], hotkeys[i + 1], proportion);
1537+
log::info!(
1538+
"Set parent-child relationship: parent={}, child={}, proportion={}",
1539+
hotkeys[i],
1540+
hotkeys[i + 1],
1541+
proportion
1542+
);
15271543
}
15281544

15291545
// Test get_parents for each hotkey
15301546
for i in 1..num_keys {
15311547
let parents = SubtensorModule::get_parents(&hotkeys[i], netuid);
1532-
log::info!("Testing get_parents for hotkey {}: {:?}", hotkeys[i], parents);
1548+
log::info!(
1549+
"Testing get_parents for hotkey {}: {:?}",
1550+
hotkeys[i],
1551+
parents
1552+
);
15331553
assert_eq!(
15341554
parents.len(),
15351555
1,
@@ -1546,7 +1566,11 @@ fn test_get_parents_chain() {
15461566

15471567
// Test get_parents for the root (should be empty)
15481568
let root_parents = SubtensorModule::get_parents(&hotkeys[0], netuid);
1549-
log::info!("Testing get_parents for root hotkey {}: {:?}", hotkeys[0], root_parents);
1569+
log::info!(
1570+
"Testing get_parents for root hotkey {}: {:?}",
1571+
hotkeys[0],
1572+
root_parents
1573+
);
15501574
assert!(
15511575
root_parents.is_empty(),
15521576
"Root hotkey should have no parents"
@@ -1556,18 +1580,32 @@ fn test_get_parents_chain() {
15561580
let last_hotkey = hotkeys[num_keys - 1];
15571581
let new_parent = U256::from(num_keys as u64 + 2);
15581582
register_ok_neuron(netuid, new_parent, coldkey, 0);
1559-
log::info!("Registered new parent neuron: new_parent={}, coldkey={}, netuid={}", new_parent, coldkey, netuid);
1583+
log::info!(
1584+
"Registered new parent neuron: new_parent={}, coldkey={}, netuid={}",
1585+
new_parent,
1586+
coldkey,
1587+
netuid
1588+
);
15601589

15611590
assert_ok!(SubtensorModule::do_set_children(
15621591
RuntimeOrigin::signed(coldkey),
15631592
new_parent,
15641593
netuid,
15651594
vec![(proportion / 2, last_hotkey)]
15661595
));
1567-
log::info!("Set additional parent-child relationship: parent={}, child={}, proportion={}", new_parent, last_hotkey, proportion / 2);
1596+
log::info!(
1597+
"Set additional parent-child relationship: parent={}, child={}, proportion={}",
1598+
new_parent,
1599+
last_hotkey,
1600+
proportion / 2
1601+
);
15681602

15691603
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);
1604+
log::info!(
1605+
"Testing get_parents for last hotkey {} with multiple parents: {:?}",
1606+
last_hotkey,
1607+
last_hotkey_parents
1608+
);
15711609
assert_eq!(
15721610
last_hotkey_parents.len(),
15731611
2,
@@ -1583,3 +1621,55 @@ fn test_get_parents_chain() {
15831621
);
15841622
});
15851623
}
1624+
1625+
// SKIP_WASM_BUILD=1 RUST_LOG=info cargo test --test children -- test_childkey_take_removal_on_empty_children --exact --nocapture
1626+
#[test]
1627+
fn test_childkey_take_removal_on_empty_children() {
1628+
new_test_ext(1).execute_with(|| {
1629+
let coldkey = U256::from(1);
1630+
let hotkey = U256::from(2);
1631+
let child = U256::from(3);
1632+
let netuid: u16 = 1;
1633+
let proportion: u64 = 1000;
1634+
1635+
// Add network and register hotkey
1636+
add_network(netuid, 13, 0);
1637+
register_ok_neuron(netuid, hotkey, coldkey, 0);
1638+
1639+
// Set a child and childkey take
1640+
assert_ok!(SubtensorModule::do_set_children(
1641+
RuntimeOrigin::signed(coldkey),
1642+
hotkey,
1643+
netuid,
1644+
vec![(proportion, child)]
1645+
));
1646+
1647+
let take: u16 = u16::MAX / 10; // 10% take
1648+
assert_ok!(SubtensorModule::set_childkey_take(
1649+
RuntimeOrigin::signed(coldkey),
1650+
hotkey,
1651+
netuid,
1652+
take
1653+
));
1654+
1655+
// Verify childkey take is set
1656+
assert_eq!(SubtensorModule::get_childkey_take(&hotkey, netuid), take);
1657+
1658+
// Remove all children
1659+
assert_ok!(SubtensorModule::do_set_children(
1660+
RuntimeOrigin::signed(coldkey),
1661+
hotkey,
1662+
netuid,
1663+
vec![]
1664+
));
1665+
1666+
// Verify children are removed
1667+
let children = SubtensorModule::get_children(&hotkey, netuid);
1668+
assert!(children.is_empty());
1669+
1670+
// Verify childkey take is removed
1671+
assert_eq!(SubtensorModule::get_childkey_take(&hotkey, netuid), 0);
1672+
// Verify childkey take storage is empty
1673+
assert_eq!(ChildkeyTake::<Test>::get(hotkey, netuid), 0);
1674+
});
1675+
}

runtime/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -858,8 +858,8 @@ parameter_types! {
858858
pub const SubtensorInitialPruningScore : u16 = u16::MAX;
859859
pub const SubtensorInitialBondsMovingAverage: u64 = 900_000;
860860
pub const SubtensorInitialDefaultTake: u16 = 11_796; // 18% honest number.
861-
pub const SubtensorInitialMinDelegateTake: u16 = 5_898; // 9%
862-
pub const SubtensorInitialDefaultChildKeyTake: u16 = 11_796; // 18% honest number.
861+
pub const SubtensorInitialMinDelegateTake: u16 = 0; // Allow 0% delegate take
862+
pub const SubtensorInitialDefaultChildKeyTake: u16 = 0; // Allow 0% childkey take
863863
pub const SubtensorInitialMinChildKeyTake: u16 = 5_898; // 9%
864864
pub const SubtensorInitialWeightsVersionKey: u64 = 0;
865865
pub const SubtensorInitialMinDifficulty: u64 = 10_000_000;

scripts/test_specific.sh

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
pallet="${3:-pallet-subtensor}"
22
features="${4:-pow-faucet}"
33

4-
# RUST_LOG="pallet_subtensor=info" cargo test --release --features=$features -p $pallet --test $1 -- $2 --nocapture --exact
5-
6-
RUST_LOG=DEBUG cargo test --release --features=$features -p $pallet --test $1 -- $2 --nocapture --exact
4+
SKIP_WASM_BUILD=1 RUST_LOG=DEBUG cargo test --release --features=$features -p $pallet --test $1 -- $2 --nocapture --exact

0 commit comments

Comments
 (0)