Skip to content

Commit 100f6e2

Browse files
committed
Add rate limit test for committing timelocked weights
1 parent 6eff7f6 commit 100f6e2

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

pallets/subtensor/src/tests/subsubnet.rs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,3 +1135,93 @@ fn test_crv3_above_subsubnet_count_fails() {
11351135
);
11361136
});
11371137
}
1138+
1139+
#[test]
1140+
fn test_do_commit_crv3_sub_weights_committing_too_fast() {
1141+
new_test_ext(1).execute_with(|| {
1142+
let netuid = NetUid::from(1);
1143+
let subid = SubId::from(1u8);
1144+
let hotkey: AccountId = U256::from(1);
1145+
let commit_data_1: Vec<u8> = vec![1, 2, 3];
1146+
let commit_data_2: Vec<u8> = vec![4, 5, 6];
1147+
let reveal_round: u64 = 1000;
1148+
1149+
add_network(netuid, 5, 0);
1150+
SubsubnetCountCurrent::<Test>::insert(netuid, SubId::from(2u8)); // allow subids {0,1}
1151+
1152+
register_ok_neuron(netuid, hotkey, U256::from(2), 100_000);
1153+
SubtensorModule::set_weights_set_rate_limit(netuid, 5);
1154+
SubtensorModule::set_commit_reveal_weights_enabled(netuid, true);
1155+
1156+
let uid = SubtensorModule::get_uid_for_net_and_hotkey(netuid, &hotkey).expect("uid");
1157+
let idx1 = SubtensorModule::get_subsubnet_storage_index(netuid, subid);
1158+
SubtensorModule::set_last_update_for_uid(idx1, uid, 0);
1159+
1160+
// make validator with stake
1161+
SubtensorModule::set_stake_threshold(0);
1162+
SubtensorModule::set_validator_permit_for_uid(netuid, uid, true);
1163+
SubtensorModule::add_balance_to_coldkey_account(&U256::from(2), 1);
1164+
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(&hotkey, &U256::from(2), netuid, 1.into());
1165+
1166+
// first commit OK on subid=1
1167+
assert_ok!(SubtensorModule::commit_timelocked_sub_weights(
1168+
RuntimeOrigin::signed(hotkey),
1169+
netuid,
1170+
subid,
1171+
commit_data_1.clone().try_into().expect("bounded"),
1172+
reveal_round,
1173+
SubtensorModule::get_commit_reveal_weights_version()
1174+
));
1175+
1176+
// immediate second commit on SAME subid blocked
1177+
assert_noop!(
1178+
SubtensorModule::commit_timelocked_sub_weights(
1179+
RuntimeOrigin::signed(hotkey),
1180+
netuid,
1181+
subid,
1182+
commit_data_2.clone().try_into().expect("bounded"),
1183+
reveal_round,
1184+
SubtensorModule::get_commit_reveal_weights_version()
1185+
),
1186+
Error::<Test>::CommittingWeightsTooFast
1187+
);
1188+
1189+
// BUT committing too soon on a DIFFERENT subid is allowed
1190+
let other_subid = SubId::from(0u8);
1191+
let idx0 = SubtensorModule::get_subsubnet_storage_index(netuid, other_subid);
1192+
SubtensorModule::set_last_update_for_uid(idx0, uid, 0); // baseline like above
1193+
assert_ok!(SubtensorModule::commit_timelocked_sub_weights(
1194+
RuntimeOrigin::signed(hotkey),
1195+
netuid,
1196+
other_subid,
1197+
commit_data_2.clone().try_into().expect("bounded"),
1198+
reveal_round,
1199+
SubtensorModule::get_commit_reveal_weights_version()
1200+
));
1201+
1202+
// still too fast on original subid after 2 blocks
1203+
step_block(2);
1204+
assert_noop!(
1205+
SubtensorModule::commit_timelocked_sub_weights(
1206+
RuntimeOrigin::signed(hotkey),
1207+
netuid,
1208+
subid,
1209+
commit_data_2.clone().try_into().expect("bounded"),
1210+
reveal_round,
1211+
SubtensorModule::get_commit_reveal_weights_version()
1212+
),
1213+
Error::<Test>::CommittingWeightsTooFast
1214+
);
1215+
1216+
// after enough blocks, OK again on original subid
1217+
step_block(3);
1218+
assert_ok!(SubtensorModule::commit_timelocked_sub_weights(
1219+
RuntimeOrigin::signed(hotkey),
1220+
netuid,
1221+
subid,
1222+
commit_data_2.try_into().expect("bounded"),
1223+
reveal_round,
1224+
SubtensorModule::get_commit_reveal_weights_version()
1225+
));
1226+
});
1227+
}

0 commit comments

Comments
 (0)