Skip to content

Commit ac5bc9a

Browse files
committed
add test for overflow
1 parent 6c8d794 commit ac5bc9a

File tree

1 file changed

+156
-1
lines changed

1 file changed

+156
-1
lines changed

pallets/subtensor/tests/coinbase.rs

Lines changed: 156 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#![allow(unused, clippy::indexing_slicing, clippy::panic, clippy::unwrap_used)]
22
use crate::mock::*;
33
mod mock;
4-
// use frame_support::{assert_err, assert_ok};
4+
use frame_support::assert_ok;
55
use sp_core::U256;
6+
use substrate_fixed::types::I64F64;
67

78
// Test the ability to hash all sorts of hotkeys.
89
#[test]
@@ -154,3 +155,157 @@ fn test_set_and_get_hotkey_emission_tempo() {
154155
assert_eq!(updated_tempo, new_tempo);
155156
});
156157
}
158+
159+
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --test coinbase test_coinbase_nominator_drainage_overflow -- --nocapture
160+
#[test]
161+
fn test_coinbase_nominator_drainage_overflow() {
162+
new_test_ext(1).execute_with(|| {
163+
// 1. Set up the network and accounts
164+
let netuid: u16 = 1;
165+
let hotkey = U256::from(0);
166+
let coldkey = U256::from(3);
167+
let nominator1 = U256::from(1);
168+
let nominator2 = U256::from(2);
169+
170+
log::debug!("Setting up network with netuid: {}", netuid);
171+
log::debug!("Hotkey: {:?}, Coldkey: {:?}", hotkey, coldkey);
172+
log::debug!("Nominators: {:?}, {:?}", nominator1, nominator2);
173+
174+
// 2. Create network and register neuron
175+
add_network(netuid, 1, 0);
176+
register_ok_neuron(netuid, hotkey, coldkey, 100000);
177+
SubtensorModule::create_account_if_non_existent(&coldkey, &hotkey);
178+
179+
log::debug!("Network created and neuron registered");
180+
181+
// 3. Set up balances and stakes
182+
SubtensorModule::add_balance_to_coldkey_account(&coldkey, 1000);
183+
SubtensorModule::add_balance_to_coldkey_account(&nominator1, 1500);
184+
SubtensorModule::add_balance_to_coldkey_account(&nominator2, 1500);
185+
186+
log::debug!("Balances added to accounts");
187+
188+
// 4. Make the hotkey a delegate
189+
let vali_take = (u16::MAX as u64 / 10);
190+
assert_ok!(SubtensorModule::do_become_delegate(
191+
RuntimeOrigin::signed(coldkey),
192+
hotkey,
193+
vali_take as u16
194+
));
195+
196+
log::debug!("Hotkey became a delegate with minimum take");
197+
198+
// Add stakes for nominators
199+
// Add the stake directly to their coldkey-hotkey account
200+
// This bypasses the accounting in stake delta
201+
SubtensorModule::increase_stake_on_coldkey_hotkey_account(&nominator1, &hotkey, 5e9 as u64);
202+
SubtensorModule::increase_stake_on_coldkey_hotkey_account(&nominator2, &hotkey, 5e9 as u64);
203+
let initial_stake = 5e9 as u64;
204+
205+
// Log the stakes for hotkey, nominator1, and nominator2
206+
log::debug!(
207+
"Initial stakes - Hotkey: {}, Nominator1: {}, Nominator2: {}",
208+
SubtensorModule::get_stake_for_coldkey_and_hotkey(&coldkey, &hotkey),
209+
SubtensorModule::get_stake_for_coldkey_and_hotkey(&nominator1, &hotkey),
210+
SubtensorModule::get_stake_for_coldkey_and_hotkey(&nominator2, &hotkey)
211+
);
212+
log::debug!("Stakes added for nominators");
213+
214+
// 5. Set emission and verify initial states
215+
let to_emit = 20_000e9 as u64;
216+
SubtensorModule::set_emission_values(&[netuid], vec![to_emit]).unwrap();
217+
assert_eq!(SubtensorModule::get_subnet_emission_value(netuid), to_emit);
218+
assert_eq!(SubtensorModule::get_pending_hotkey_emission(&hotkey), 0);
219+
assert_eq!(
220+
SubtensorModule::get_total_stake_for_hotkey(&hotkey),
221+
initial_stake * 2
222+
);
223+
assert_eq!(SubtensorModule::get_pending_emission(netuid), 0);
224+
225+
log::debug!("Emission set and initial states verified");
226+
227+
// 6. Set hotkey emission tempo
228+
SubtensorModule::set_hotkey_emission_tempo(1);
229+
log::debug!("Hotkey emission tempo set to 1");
230+
231+
// 7. Simulate blocks and check emissions
232+
next_block();
233+
assert_eq!(SubtensorModule::get_pending_emission(netuid), to_emit);
234+
log::debug!(
235+
"After first block, pending emission: {}",
236+
SubtensorModule::get_pending_emission(netuid)
237+
);
238+
239+
next_block();
240+
assert_eq!(SubtensorModule::get_pending_emission(netuid), 0);
241+
assert_eq!(SubtensorModule::get_pending_hotkey_emission(&hotkey), 0);
242+
log::debug!("After second block, pending emission drained");
243+
244+
// 8. Check final stakes
245+
let hotkey_stake = SubtensorModule::get_stake_for_coldkey_and_hotkey(&coldkey, &hotkey);
246+
let nominator1_stake =
247+
SubtensorModule::get_stake_for_coldkey_and_hotkey(&nominator1, &hotkey);
248+
let nominator2_stake =
249+
SubtensorModule::get_stake_for_coldkey_and_hotkey(&nominator2, &hotkey);
250+
251+
log::debug!(
252+
"Final stakes - Hotkey: {}, Nominator1: {}, Nominator2: {}",
253+
hotkey_stake,
254+
nominator1_stake,
255+
nominator2_stake
256+
);
257+
258+
// 9. Verify distribution
259+
let total_emission = to_emit * 2; // to_emit per block for 2 blocks
260+
let hotkey_emission = (I64F64::from_num(total_emission) / I64F64::from_num(u16::MAX)
261+
* I64F64::from_num(vali_take))
262+
.to_num::<u64>();
263+
let remaining_emission = total_emission - hotkey_emission;
264+
let nominator_emission = remaining_emission / 2;
265+
266+
log::debug!(
267+
"Calculated emissions - Hotkey: {}, Each Nominator: {}",
268+
hotkey_emission,
269+
nominator_emission
270+
);
271+
272+
// Debug: Print the actual stakes
273+
log::debug!("Actual hotkey stake: {}", hotkey_stake);
274+
log::debug!("Actual nominator1 stake: {}", nominator1_stake);
275+
log::debug!("Actual nominator2 stake: {}", nominator2_stake);
276+
277+
// Debug: Check the total stake for the hotkey
278+
let total_stake = SubtensorModule::get_total_stake_for_hotkey(&hotkey);
279+
log::debug!("Total stake for hotkey: {}", total_stake);
280+
281+
// Assertions
282+
let expected_hotkey_stake = 4_000e9 as u64;
283+
let eps = 0.5e9 as u64;
284+
assert!(
285+
hotkey_stake >= expected_hotkey_stake - eps
286+
&& hotkey_stake <= expected_hotkey_stake + eps,
287+
"Hotkey stake mismatch - expected: {}, actual: {}",
288+
expected_hotkey_stake,
289+
hotkey_stake
290+
);
291+
assert_eq!(
292+
nominator1_stake,
293+
initial_stake + nominator_emission,
294+
"Nominator1 stake mismatch"
295+
);
296+
assert_eq!(
297+
nominator2_stake,
298+
initial_stake + nominator_emission,
299+
"Nominator2 stake mismatch"
300+
);
301+
302+
// 10. Check total stake
303+
assert_eq!(
304+
total_stake,
305+
initial_stake + initial_stake + total_emission,
306+
"Total stake mismatch"
307+
);
308+
309+
log::debug!("Test completed");
310+
});
311+
}

0 commit comments

Comments
 (0)