Skip to content

Commit e1518b0

Browse files
committed
move clear pending into run_coinbase and pass pending to drain_pending
1 parent 122eda3 commit e1518b0

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

pallets/subtensor/src/coinbase/run_coinbase.rs

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -224,31 +224,35 @@ impl<T: Config> Pallet<T> {
224224
// Restart counters.
225225
BlocksSinceLastStep::<T>::insert(netuid, 0);
226226
LastMechansimStepBlock::<T>::insert(netuid, current_block);
227+
228+
// 5.2 Get and drain the subnet pending emission.
229+
let pending_emission: u64 = PendingEmission::<T>::get(netuid);
230+
PendingEmission::<T>::insert(netuid, 0);
231+
227232
// Drain pending root divs and alpha emission.
228-
Self::drain_pending_emission(netuid);
233+
Self::drain_pending_emission(netuid, pending_emission);
229234
} else {
230235
// Increment
231236
BlocksSinceLastStep::<T>::mutate(netuid, |total| *total = total.saturating_add(1));
232237
}
233238
}
234239
}
235240

236-
pub fn drain_pending_emission(netuid: u16) {
237-
// 7.2 Get and drain the subnet pending emission.
238-
let alpha_out: u64 = PendingEmission::<T>::get(netuid);
241+
pub fn drain_pending_emission(netuid: u16, pending_emission: u64) {
242+
let alpha_out: u64 = pending_emission;
243+
239244
log::debug!(
240245
"Draining pending emission for netuid {:?}: {:?}",
241246
netuid,
242247
alpha_out
243248
);
244-
PendingEmission::<T>::insert(netuid, 0);
245249

246-
// 7.4 Distribute the 18% owner cut.
250+
// 6.1 Distribute the 18% owner cut.
247251
let owner_cut: u64 = I96F32::from_num(alpha_out)
248252
.saturating_mul(Self::get_float_subnet_owner_cut())
249253
.to_num::<u64>();
250254
log::debug!("Owner cut for netuid {:?}: {:?}", netuid, owner_cut);
251-
// 7.4.1: Check for existence of owner cold/hot pair and distribute emission directly to them.
255+
// 6.1.1: Check for existence of owner cold/hot pair and distribute emission directly to them.
252256
if let Ok(owner_coldkey) = SubnetOwner::<T>::try_get(netuid) {
253257
if let Ok(owner_hotkey) = SubnetOwnerHotkey::<T>::try_get(netuid) {
254258
// Increase stake for both coldkey and hotkey on the subnet
@@ -268,7 +272,7 @@ impl<T: Config> Pallet<T> {
268272
remaining_emission
269273
);
270274

271-
// 7.5 Run the epoch() --> hotkey emission.
275+
// 6.2 Run the epoch() --> hotkey emission.
272276
let hotkey_emission: Vec<(T::AccountId, u64, u64)> =
273277
Self::epoch(netuid, remaining_emission);
274278
log::debug!(
@@ -277,7 +281,7 @@ impl<T: Config> Pallet<T> {
277281
hotkey_emission
278282
);
279283

280-
// 7.6 Pay out the hotkey alpha dividends.
284+
// 6.3 Pay out the hotkey alpha dividends.
281285
// First clear the netuid from HotkeyDividends
282286
let mut total_root_alpha_divs: u64 = 0;
283287
let mut root_alpha_divs: BTreeMap<T::AccountId, u64> = BTreeMap::new();
@@ -290,7 +294,7 @@ impl<T: Config> Pallet<T> {
290294
dividends
291295
);
292296

293-
// 7.6.1: Distribute mining incentive immediately.
297+
// 6.3.1: Distribute mining incentive immediately.
294298
Self::increase_stake_for_hotkey_and_coldkey_on_subnet(
295299
&hotkey.clone(),
296300
&Owner::<T>::get(hotkey.clone()),
@@ -304,7 +308,7 @@ impl<T: Config> Pallet<T> {
304308
incentive
305309
);
306310

307-
// 7.6.2: Get dividend tuples for parents and self based on childkey relationships and child-take.
311+
// 6.3.2: Get dividend tuples for parents and self based on childkey relationships and child-take.
308312
let dividend_tuples: Vec<(T::AccountId, u64)> =
309313
Self::get_parent_dividends(&hotkey, netuid, dividends);
310314
log::debug!(
@@ -314,7 +318,7 @@ impl<T: Config> Pallet<T> {
314318
dividend_tuples
315319
);
316320

317-
// 7.6.3 Pay out dividends to hotkeys based on the local vs root proportion.
321+
// 6.3.3 Pay out dividends to hotkeys based on the local vs root proportion.
318322
for (hotkey_j, divs_j) in dividend_tuples {
319323
log::debug!(
320324
"Processing dividend for hotkey {:?} to hotkey {:?}: {:?}",
@@ -323,7 +327,7 @@ impl<T: Config> Pallet<T> {
323327
divs_j
324328
);
325329

326-
// 7.6.3.1: Remove the hotkey take straight off the top.
330+
// 6.3.3.1: Remove the hotkey take straight off the top.
327331
let take_prop: I96F32 = I96F32::from_num(Self::get_hotkey_take(&hotkey_j))
328332
.checked_div(I96F32::from_num(u16::MAX))
329333
.unwrap_or(I96F32::from_num(0.0));
@@ -336,7 +340,7 @@ impl<T: Config> Pallet<T> {
336340
rem_divs_j
337341
);
338342

339-
// 7.6.3.2: Distribute validator take automatically.
343+
// 6.3.3.2: Distribute validator take automatically.
340344
Self::increase_stake_for_hotkey_and_coldkey_on_subnet(
341345
&hotkey_j,
342346
&Owner::<T>::get(hotkey_j.clone()),
@@ -350,7 +354,7 @@ impl<T: Config> Pallet<T> {
350354
validator_take.to_num::<u64>()
351355
);
352356

353-
// 7.6.3.3: Get the local alpha and root alpha.
357+
// 6.3.3.3: Get the local alpha and root alpha.
354358
let hotkey_tao: I96F32 = I96F32::from_num(Self::get_stake_for_hotkey_on_subnet(
355359
&hotkey,
356360
Self::get_root_netuid(),
@@ -360,7 +364,7 @@ impl<T: Config> Pallet<T> {
360364
I96F32::from_num(Self::get_stake_for_hotkey_on_subnet(&hotkey, netuid));
361365
log::debug!("Hotkey tao for hotkey {:?} on root netuid: {:?}, hotkey tao as alpha: {:?}, hotkey alpha: {:?}", hotkey, hotkey_tao, hotkey_tao_as_alpha, hotkey_alpha);
362366

363-
// 7.6.3.3 Compute alpha and root proportions.
367+
// 6.3.3.4 Compute alpha and root proportions.
364368
let alpha_prop: I96F32 = hotkey_alpha
365369
.checked_div(hotkey_alpha.saturating_add(hotkey_tao_as_alpha))
366370
.unwrap_or(I96F32::from_num(0.0));
@@ -373,15 +377,15 @@ impl<T: Config> Pallet<T> {
373377
root_prop
374378
);
375379

376-
// 7.6.3.5: Compute alpha and root dividends
380+
// 6.3.3.5: Compute alpha and root dividends
377381
let root_divs: I96F32 = rem_divs_j.saturating_mul(root_prop);
378382
log::debug!(
379383
"Alpha dividends: {:?}, root dividends: {:?}",
380384
rem_divs_j,
381385
root_divs
382386
);
383387

384-
// 7.6.3.6. Store the root alpha divs under hotkey_j
388+
// 6.3.3.6. Store the root alpha divs under hotkey_j
385389
root_alpha_divs
386390
.entry(hotkey_j.clone())
387391
.and_modify(|e| *e = e.saturating_add(root_divs.to_num::<u64>()))
@@ -394,7 +398,7 @@ impl<T: Config> Pallet<T> {
394398
root_divs.to_num::<u64>()
395399
);
396400

397-
// 7.6.3.7: Distribute the alpha divs to the hotkey.
401+
// 6.3.3.7: Distribute the alpha divs to the hotkey.
398402
Self::increase_stake_for_hotkey_on_subnet(
399403
&hotkey_j,
400404
netuid,
@@ -407,7 +411,7 @@ impl<T: Config> Pallet<T> {
407411
rem_divs_j.to_num::<u64>()
408412
);
409413

410-
// 7.6.3.9: Record dividends for this hotkey on this subnet.
414+
// 6.3.3.8: Record dividends for this hotkey on this subnet.
411415
HotkeyDividendsPerSubnet::<T>::mutate(netuid, hotkey_j.clone(), |divs| {
412416
*divs = divs.saturating_add(divs_j);
413417
});
@@ -449,7 +453,7 @@ impl<T: Config> Pallet<T> {
449453
root_divs_to_pay
450454
);
451455

452-
// 7.6.3.9: Record dividends for this hotkey on this subnet.
456+
// 6.3.3.9: Record dividends for this hotkey on this subnet.
453457
RootDividendsPerSubnet::<T>::mutate(netuid, hotkey_j.clone(), |divs| {
454458
*divs = divs.saturating_add(root_divs_to_pay);
455459
});

0 commit comments

Comments
 (0)