Skip to content

Commit 2cc05e8

Browse files
committed
Merge branch 'devnet-ready' into fix/evm-gas-estimation
2 parents d66e2ed + 9b05314 commit 2cc05e8

File tree

20 files changed

+1097
-209
lines changed

20 files changed

+1097
-209
lines changed

pallets/commitments/src/lib.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub mod pallet {
5858

5959
/// The rate limit for commitments
6060
#[pallet::constant]
61-
type RateLimit: Get<BlockNumberFor<Self>>;
61+
type DefaultRateLimit: Get<BlockNumberFor<Self>>;
6262
}
6363

6464
#[pallet::event]
@@ -83,6 +83,16 @@ pub mod pallet {
8383
CommitmentSetRateLimitExceeded,
8484
}
8585

86+
#[pallet::type_value]
87+
/// Default value for commitment rate limit.
88+
pub fn DefaultRateLimit<T: Config>() -> BlockNumberFor<T> {
89+
T::DefaultRateLimit::get()
90+
}
91+
92+
/// The rate limit for commitments
93+
#[pallet::storage]
94+
pub type RateLimit<T> = StorageValue<_, BlockNumberFor<T>, ValueQuery, DefaultRateLimit<T>>;
95+
8696
/// Identity data by account
8797
#[pallet::storage]
8898
#[pallet::getter(fn commitment_of)]
@@ -137,7 +147,7 @@ pub mod pallet {
137147
let cur_block = <frame_system::Pallet<T>>::block_number();
138148
if let Some(last_commit) = <LastCommitment<T>>::get(netuid, &who) {
139149
ensure!(
140-
cur_block >= last_commit.saturating_add(T::RateLimit::get()),
150+
cur_block >= last_commit.saturating_add(RateLimit::<T>::get()),
141151
Error::<T>::CommitmentSetRateLimitExceeded
142152
);
143153
}
@@ -173,6 +183,19 @@ pub mod pallet {
173183

174184
Ok(())
175185
}
186+
187+
/// Sudo-set the commitment rate limit
188+
#[pallet::call_index(1)]
189+
#[pallet::weight((
190+
T::WeightInfo::set_rate_limit(),
191+
DispatchClass::Operational,
192+
Pays::No
193+
))]
194+
pub fn set_rate_limit(origin: OriginFor<T>, rate_limit_blocks: u32) -> DispatchResult {
195+
ensure_root(origin)?;
196+
RateLimit::<T>::set(rate_limit_blocks.into());
197+
Ok(())
198+
}
176199
}
177200
}
178201

pallets/commitments/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl pallet_commitments::Config for Test {
8686
type CanCommit = ();
8787
type FieldDeposit = frame_support::traits::ConstU64<0>;
8888
type InitialDeposit = frame_support::traits::ConstU64<0>;
89-
type RateLimit = frame_support::traits::ConstU64<0>;
89+
type DefaultRateLimit = frame_support::traits::ConstU64<0>;
9090
}
9191

9292
// // Build genesis storage according to the mock runtime.

pallets/commitments/src/weights.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use core::marker::PhantomData;
3030
/// Weight functions needed for `pallet_commitments`.
3131
pub trait WeightInfo {
3232
fn set_commitment() -> Weight;
33+
fn set_rate_limit() -> Weight;
3334
}
3435

3536
/// Weights for `pallet_commitments` using the Substrate node and recommended hardware.
@@ -48,6 +49,11 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
4849
.saturating_add(T::DbWeight::get().reads(2_u64))
4950
.saturating_add(T::DbWeight::get().writes(2_u64))
5051
}
52+
/// Sudo setting rate limit for commitments
53+
fn set_rate_limit() -> Weight {
54+
Weight::from_parts(10_000_000, 2000)
55+
.saturating_add(RocksDbWeight::get().reads(1_u64))
56+
}
5157
}
5258

5359
// For backwards compatibility and tests.
@@ -65,4 +71,10 @@ impl WeightInfo for () {
6571
.saturating_add(RocksDbWeight::get().reads(2_u64))
6672
.saturating_add(RocksDbWeight::get().writes(2_u64))
6773
}
74+
75+
/// Sudo setting rate limit for commitments
76+
fn set_rate_limit() -> Weight {
77+
Weight::from_parts(10_000_000, 2000)
78+
.saturating_add(RocksDbWeight::get().reads(1_u64))
79+
}
6880
}

pallets/subtensor/src/coinbase/run_coinbase.rs

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ pub struct WeightsTlockPayload {
1818
}
1919

2020
impl<T: Config> Pallet<T> {
21-
pub fn get_root_divs_in_alpha(netuid: u16, alpha_out_emission: I96F32) -> I96F32 {
21+
pub fn get_root_divs_in_alpha(
22+
netuid: u16,
23+
alpha_out_emission: I96F32,
24+
validator_proportion: I96F32,
25+
) -> I96F32 {
2226
// Get total TAO on root.
2327
let total_root_tao: I96F32 = I96F32::from_num(SubnetTAO::<T>::get(0));
2428
// Get total ALPHA on subnet.
@@ -32,7 +36,8 @@ impl<T: Config> Pallet<T> {
3236
// Get root proportion of alpha_out dividends.
3337
let root_divs_in_alpha: I96F32 = root_proportion
3438
.saturating_mul(alpha_out_emission)
35-
.saturating_mul(I96F32::from_num(0.41));
39+
.saturating_mul(validator_proportion); // % of emission that goes to *all* validators.
40+
3641
// Return
3742
root_divs_in_alpha
3843
}
@@ -207,9 +212,14 @@ impl<T: Config> Pallet<T> {
207212
remaining_emission
208213
);
209214

215+
// Validators get 50% of remaining emission.
216+
let validator_proportion: I96F32 = I96F32::from_num(0.5);
210217
// Get proportion of alpha out emission as root divs.
211-
let root_emission_in_alpha: I96F32 =
212-
Self::get_root_divs_in_alpha(*netuid, I96F32::from_num(remaining_emission));
218+
let root_emission_in_alpha: I96F32 = Self::get_root_divs_in_alpha(
219+
*netuid,
220+
I96F32::from_num(remaining_emission),
221+
validator_proportion,
222+
);
213223
// Subtract root divs from alpha divs.
214224
let pending_alpha_emission: I96F32 =
215225
I96F32::from_num(remaining_emission).saturating_sub(root_emission_in_alpha);
@@ -221,10 +231,18 @@ impl<T: Config> Pallet<T> {
221231
PendingRootDivs::<T>::mutate(*netuid, |total| {
222232
*total = total.saturating_add(root_emission_in_tao);
223233
});
234+
// Accumulate alpha that was swapped for the pending root divs.
235+
PendingAlphaSwapped::<T>::mutate(*netuid, |total| {
236+
*total = total.saturating_add(root_emission_in_alpha.to_num::<u64>());
237+
});
224238
// Accumulate alpha emission in pending.
225239
PendingEmission::<T>::mutate(*netuid, |total| {
226240
*total = total.saturating_add(pending_alpha_emission.to_num::<u64>());
227241
});
242+
// Accumulate the owner cut in pending.
243+
PendingOwnerCut::<T>::mutate(*netuid, |total| {
244+
*total = total.saturating_add(owner_cut);
245+
});
228246
}
229247

230248
// --- 5. Drain pending emission through the subnet based on tempo.
@@ -247,18 +265,24 @@ impl<T: Config> Pallet<T> {
247265
let pending_emission: u64 = PendingEmission::<T>::get(netuid);
248266
PendingEmission::<T>::insert(netuid, 0);
249267

250-
// 5.2.2 Get and drain the subnet pending root divs.
268+
// 5.2.2a Get and drain the subnet pending root divs.
251269
let pending_root_divs: u64 = PendingRootDivs::<T>::get(netuid);
252270
PendingRootDivs::<T>::insert(netuid, 0);
253271

254-
// 5.2.3 Get owner cut.
255-
let owner_cut: u64 = *owner_cuts.get(&netuid).unwrap_or(&0);
272+
// 5.2.2b Get this amount as alpha that was swapped for pending root divs.
273+
let pending_alpha_swapped: u64 = PendingAlphaSwapped::<T>::get(netuid);
274+
PendingAlphaSwapped::<T>::insert(netuid, 0);
275+
276+
// 5.2.3 Get owner cut and drain.
277+
let owner_cut: u64 = PendingOwnerCut::<T>::get(netuid);
278+
PendingOwnerCut::<T>::insert(netuid, 0);
256279

257280
// 5.2.4 Drain pending root divs, alpha emission, and owner cut.
258281
Self::drain_pending_emission(
259282
netuid,
260283
pending_emission,
261284
pending_root_divs,
285+
pending_alpha_swapped,
262286
owner_cut,
263287
);
264288
} else {
@@ -272,19 +296,24 @@ impl<T: Config> Pallet<T> {
272296
netuid: u16,
273297
pending_alpha_emission: u64,
274298
pending_root_divs: u64,
299+
pending_alpha_swapped: u64,
275300
owner_cut: u64,
276301
) {
277302
log::debug!(
278-
"Draining pending alpha emission for netuid {:?}: {:?}, with pending root divs {:?}, and owner cut {:?}",
303+
"Draining pending alpha emission for netuid {:?}: {:?}, with pending root divs {:?}, pending alpha swapped {:?}, and owner cut {:?}",
279304
netuid,
280305
pending_alpha_emission,
281306
pending_root_divs,
307+
pending_alpha_swapped,
282308
owner_cut
283309
);
284310

285311
// Run the epoch() --> hotkey emission.
286-
let hotkey_emission: Vec<(T::AccountId, u64, u64)> =
287-
Self::epoch(netuid, pending_alpha_emission);
312+
// Needs to run on the full emission to the subnet.
313+
let hotkey_emission: Vec<(T::AccountId, u64, u64)> = Self::epoch(
314+
netuid,
315+
pending_alpha_emission.saturating_add(pending_alpha_swapped),
316+
);
288317
log::debug!(
289318
"Hotkey emission for netuid {:?}: {:?}",
290319
netuid,

0 commit comments

Comments
 (0)