Skip to content

Commit 5f6a26e

Browse files
committed
wip - adding min mpower setup
1 parent 2211881 commit 5f6a26e

File tree

3 files changed

+134
-17
lines changed

3 files changed

+134
-17
lines changed

node/src/chain_spec.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -941,6 +941,8 @@ fn testnet_genesis(
941941
rewards_accumulated_dhx_for_miner_for_date: Default::default(),
942942
min_bonded_dhx_daily: TEN, // 10 DHX
943943
min_bonded_dhx_daily_default: TEN, // 10 DHX
944+
min_mpower_daily: 5u128,
945+
min_mpower_daily_default: 5u128,
944946
cooling_off_period_days: 7u32,
945947
cooling_off_period_days_remaining: vec![
946948
(
@@ -1068,6 +1070,8 @@ fn mainnet_genesis(
10681070
rewards_accumulated_dhx_for_miner_for_date: Default::default(),
10691071
min_bonded_dhx_daily: TEN, // 10 DHX
10701072
min_bonded_dhx_daily_default: TEN, // 10 DHX
1073+
min_mpower_daily: 5u128,
1074+
min_mpower_daily_default: 5u128,
10711075
cooling_off_period_days: 7u32,
10721076
cooling_off_period_days_remaining: vec![
10731077
(

pallets/mining/rewards-allowance/src/lib.rs

Lines changed: 109 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub mod pallet {
3333
use codec::{
3434
Decode,
3535
Encode,
36+
// MaxEncodedLen,
3637
};
3738
use frame_support::{dispatch::DispatchResult, pallet_prelude::*,
3839
traits::{
@@ -89,6 +90,8 @@ pub mod pallet {
8990
#[pallet::pallet]
9091
#[pallet::generate_store(pub(super) trait Store)]
9192
pub struct Pallet<T>(_);
93+
// #[pallet::generate_storage_info]
94+
// pub struct Pallet<T>(PhantomData<T>);
9295

9396
// The pallet's runtime storage items.
9497
// https://substrate.dev/docs/en/knowledgebase/runtime/storage
@@ -206,6 +209,14 @@ pub mod pallet {
206209
#[pallet::getter(fn min_bonded_dhx_daily_default)]
207210
pub(super) type MinBondedDHXDailyDefault<T: Config> = StorageValue<_, BalanceOf<T>>;
208211

212+
#[pallet::storage]
213+
#[pallet::getter(fn min_mpower_daily)]
214+
pub(super) type MinMPowerDaily<T: Config> = StorageValue<_, u128>;
215+
216+
#[pallet::storage]
217+
#[pallet::getter(fn min_mpower_daily_default)]
218+
pub(super) type MinMPowerDailyDefault<T: Config> = StorageValue<_, u128>;
219+
209220
#[pallet::storage]
210221
#[pallet::getter(fn cooling_off_period_days)]
211222
pub(super) type CoolingOffPeriodDays<T: Config> = StorageValue<_, u32>;
@@ -220,9 +231,12 @@ pub mod pallet {
220231
// current date for a miner (i.e. only reduce the days remaining once per day per miner)
221232
Date,
222233
u32, // days remaining
223-
// 0: unbonded (i.e. never bonded, or finished cool-down period and no longer bonding)
224-
// 1: bonded/bonding (i.e. waiting in the cool-down period before start getting rewards or eligible for rewards)
234+
// 0:
235+
// unbonded (i.e. never bonded, insufficient bonded, or finished cool-down period and no longer bonding) and
236+
// insufficient mPower (i.e. less than min. mPower)
237+
// 1: bonded/bonding/mPower (i.e. waiting in the cool-down period before start getting rewards or eligible for rewards)
225238
// 2: unbonding (i.e. if they are bonding less than the threshold whilst getting rewards,
239+
// or no longer have the min. mPower, then
226240
// this unbonding starts and they must wait until it finishes, which is when this value
227241
// would be set to 0u32, before bonding and then waiting for the cool-down period all over again)
228242
u32,
@@ -250,6 +264,8 @@ pub mod pallet {
250264
pub registered_dhx_miners: Vec<T::AccountId>,
251265
pub min_bonded_dhx_daily: BalanceOf<T>,
252266
pub min_bonded_dhx_daily_default: BalanceOf<T>,
267+
pub min_mpower_daily: u128,
268+
pub min_mpower_daily_default: u128,
253269
pub cooling_off_period_days: u32,
254270
pub cooling_off_period_days_remaining: Vec<(T::AccountId, (Date, u32, u32))>,
255271
}
@@ -284,6 +300,8 @@ pub mod pallet {
284300
],
285301
min_bonded_dhx_daily: Default::default(),
286302
min_bonded_dhx_daily_default: Default::default(),
303+
min_mpower_daily: 5u128,
304+
min_mpower_daily_default: 5u128,
287305
cooling_off_period_days: Default::default(),
288306
// Note: this doesn't seem to work, even if it's just `vec![Default::default()]` it doesn't use
289307
// the defaults in chain_spec.rs, so we set defaults later with `let mut cooling_off_period_days_remaining`
@@ -333,6 +351,8 @@ pub mod pallet {
333351
}
334352
<MinBondedDHXDaily<T>>::put(&self.min_bonded_dhx_daily);
335353
<MinBondedDHXDailyDefault<T>>::put(&self.min_bonded_dhx_daily_default);
354+
<MinMPowerDaily<T>>::put(&self.min_mpower_daily);
355+
<MinMPowerDailyDefault<T>>::put(&self.min_mpower_daily_default);
336356
<CoolingOffPeriodDays<T>>::put(&self.cooling_off_period_days);
337357
for (a, (b, c, d)) in &self.cooling_off_period_days_remaining {
338358
<CoolingOffPeriodDaysRemaining<T>>::insert(a, (b, c, d));
@@ -355,11 +375,16 @@ pub mod pallet {
355375
/// \[sender]
356376
SetRegisteredDHXMiner(T::AccountId),
357377

358-
/// Storage of the default minimum DHX that must be bonded by each registered DHX miner each day
378+
/// Storage of the minimum DHX that must be bonded by each registered DHX miner each day
359379
/// to be eligible for rewards
360380
/// \[amount_dhx, sender\]
361381
SetMinBondedDHXDailyStored(BalanceOf<T>, T::AccountId),
362382

383+
/// Storage of the minimum mPower that must be held by each registered DHX miner each day
384+
/// to be eligible for rewards
385+
/// \[amount_mpower, sender\]
386+
SetMinMPowerDailyStored(u128, T::AccountId),
387+
363388
/// Storage of the default cooling off period in days
364389
/// \[cooling_off_period_days\]
365390
SetCoolingOffPeriodDaysStored(u32),
@@ -528,6 +553,15 @@ pub mod pallet {
528553
} else {
529554
log::info!("Unable to get min_bonded_dhx_daily_default");
530555
}
556+
// println!("min_bonded_dhx_daily_default {:?}", min_bonded_dhx_daily_default);
557+
558+
let mut min_mpower_daily_default: u128 = 5u128;
559+
if let Some(_min_mpower_daily_default) = <MinMPowerDailyDefault<T>>::get() {
560+
min_mpower_daily_default = _min_mpower_daily_default;
561+
} else {
562+
log::info!("Unable to get min_mpower_daily_default");
563+
}
564+
// println!("min_mpower_daily_default {:?}", min_mpower_daily_default);
531565

532566
let mut rm_current_period_days_remaining = (
533567
0.into(),
@@ -558,6 +592,7 @@ pub mod pallet {
558592
<RewardsMultiplierNextPeriodDays<T>>::put(rm_default_period_days.clone());
559593
<RewardsMultiplierReset<T>>::put(false);
560594
<MinBondedDHXDaily<T>>::put(min_bonded_dhx_daily_default.clone());
595+
<MinMPowerDaily<T>>::put(min_mpower_daily_default.clone());
561596
}
562597

563598
let block_two = 2u32;
@@ -862,6 +897,33 @@ pub mod pallet {
862897
}
863898
log::info!("is_bonding_min_dhx: {:?} {:?}", is_bonding_min_dhx.clone(), miner.clone());
864899

900+
// TODO - move this into off-chain workers function
901+
let mut min_mpower_daily_u128: u128 = 5u128;
902+
if let Some(_min_mpower_daily_u128) = <MinMPowerDaily<T>>::get() {
903+
min_mpower_daily_u128 = _min_mpower_daily_u128;
904+
} else {
905+
log::error!("Unable to retrieve min. mPower daily as u128");
906+
}
907+
// println!("min_mpower_daily_u128 {:#?}", min_mpower_daily_u128);
908+
909+
// TODO - move this into off-chain workers function
910+
// TODO - fetch the mPower of the miner currently being iterated to check if it's greater than the min.
911+
// mPower that is required
912+
let mpower_miner_u128: u128 = 5u128;
913+
let mut has_min_mpower_daily = false;
914+
if mpower_miner_u128 >= min_mpower_daily_u128 {
915+
has_min_mpower_daily = true;
916+
}
917+
log::info!("has_min_mpower_daily: {:?} {:?}", has_min_mpower_daily.clone(), miner.clone());
918+
// println!("has_min_mpower_daily {:#?}", has_min_mpower_daily);
919+
920+
// TODO - after fetching their mPower from the off-chain workers function where we iterate through
921+
// the registered DHX miners too, we need to incorporate it
922+
// into the recording the aggregated and accumulated rewards and the distribution of those rewards that
923+
// are done in on_initialize.
924+
// See Dhx-pop-mining-automatic.md in https://mxc.atlassian.net/browse/MMD-717 that explains off-chain worker
925+
// aspects
926+
865927
let cooling_off_period_days;
866928
if let Some(_cooling_off_period_days) = <CoolingOffPeriodDays<T>>::get() {
867929
cooling_off_period_days = _cooling_off_period_days;
@@ -887,14 +949,15 @@ pub mod pallet {
887949
log::info!("Unable to retrieve cooling off period days remaining for given miner, using default {:?}", miner.clone());
888950
}
889951
log::info!("cooling_off_period_days_remaining {:?} {:?} {:?}", start_of_requested_date_millis.clone(), cooling_off_period_days_remaining, miner.clone());
890-
// if cooling_off_period_days_remaining.2 is 0u32, it means we haven't recognised they that are bonding yet (unbonded),
952+
// if cooling_off_period_days_remaining.2 is 0u32, it means we haven't recognised they that have the min. bonded yet (or unbonded),
891953
// they aren't currently bonding, they haven't started cooling off to start bonding,
892954
// or have already finished cooling down after bonding.
893-
// so if we detect they are now bonding above the min. then we should start at max. remaining days
955+
// so if we detect they are now bonding above the min. or have above the min. mPower then we should start at max. remaining days
894956
// before starting to decrement on subsequent blocks
895957
if
896958
cooling_off_period_days_remaining.2 == 0u32 &&
897-
is_bonding_min_dhx == true
959+
is_bonding_min_dhx == true &&
960+
has_min_mpower_daily == true
898961
{
899962
<CoolingOffPeriodDaysRemaining<T>>::insert(
900963
miner.clone(),
@@ -906,14 +969,15 @@ pub mod pallet {
906969
);
907970
log::info!("Added CoolingOffPeriodDaysRemaining for miner {:?} {:?} {:?}", start_of_requested_date_millis.clone(), miner.clone(), cooling_off_period_days.clone());
908971
// if cooling_off_period_days_remaining.0 is not the start of the current date
909-
// (since if they just started bonding and we just set days remaining to 7, or we already decremented
972+
// (since if they just started with min. bonded dhx and min. mPower and we just set days remaining to 7, or we already decremented
910973
// a miner's days remaining for the current date, then we want to wait until the next day until we
911974
// decrement another day).
912975
// if cooling_off_period_days_remaining.1 is Some(above 0), then decrement, but not eligible yet for rewards.
913976
} else if
914977
cooling_off_period_days_remaining.0 != start_of_requested_date_millis.clone() &&
915978
cooling_off_period_days_remaining.1 > 0u32 &&
916-
is_bonding_min_dhx == true
979+
is_bonding_min_dhx == true &&
980+
has_min_mpower_daily == true
917981
{
918982
// println!("[reducing_days] block: {:#?}, miner: {:#?}, date_start: {:#?} remain_days: {:#?}", _n, miner_count, start_of_requested_date_millis, cooling_off_period_days_remaining);
919983
let old_cooling_off_period_days_remaining = cooling_off_period_days_remaining.1.clone();
@@ -957,7 +1021,8 @@ pub mod pallet {
9571021
cooling_off_period_days_remaining.0 != start_of_requested_date_millis.clone() &&
9581022
cooling_off_period_days_remaining.1 == 0u32 &&
9591023
cooling_off_period_days_remaining.2 == 1u32 &&
960-
is_bonding_min_dhx == true
1024+
is_bonding_min_dhx == true &&
1025+
has_min_mpower_daily == true
9611026
{
9621027
// println!("[eligible] block: {:#?}, miner: {:#?}, date_start: {:#?} remain_days: {:#?}", _n, miner_count, start_of_requested_date_millis, cooling_off_period_days_remaining);
9631028

@@ -982,6 +1047,12 @@ pub mod pallet {
9821047
continue;
9831048
}
9841049

1050+
// TODO - calculate the daily reward for the miner in DHX based on their mPower
1051+
// and add that to the new_rewards_aggregated_dhx_daily_as_u128 (which currently only
1052+
// includes the proportion of their reward based on their bonded DHX tokens) of all
1053+
// miner's for that day, and also add that to the accumulated rewards for that specific
1054+
// miner on that day.
1055+
9851056
// calculate the daily reward for the miner in DHX based on their bonded DHX.
9861057
// it should be a proportion taking other eligible miner's who are eligible for
9871058
// daily rewards into account since we want to split them fairly.
@@ -1097,15 +1168,15 @@ pub mod pallet {
10971168
);
10981169
log::info!("Added RewardsAccumulatedDHXForMinerForDate for miner {:?} {:?} {:?}", start_of_requested_date_millis.clone(), miner.clone(), daily_reward_for_miner.clone());
10991170

1100-
// if they stop bonding the min dhx, and
1171+
// if they stop bonding the min dhx or stop having min. mPower, and
11011172
// if cooling_off_period_days_remaining.1 is Some(0),
11021173
// and if cooling_off_period_days_remaining.2 is 1 (where they had just been bonding and getting rewards)
1103-
// so since we detected they are no longer bonding above the min. then we should start at max. remaining days
1104-
// before starting to decrement on subsequent blocks
1174+
// so since we detected they are no longer bonding above the min. or have less than min. mPower
1175+
// then we should start at max. remaining days before starting to decrement on subsequent blocks
11051176
} else if
11061177
cooling_off_period_days_remaining.1 == 0u32 &&
11071178
cooling_off_period_days_remaining.2 == 1u32 &&
1108-
is_bonding_min_dhx == false
1179+
(is_bonding_min_dhx == false || has_min_mpower_daily == false)
11091180
{
11101181
// Write the new value to storage
11111182
<CoolingOffPeriodDaysRemaining<T>>::insert(
@@ -1120,13 +1191,15 @@ pub mod pallet {
11201191
log::info!("Unbonding detected for miner. Starting cooling down period {:?} {:?}", miner.clone(), cooling_off_period_days.clone());
11211192

11221193
// if cooling_off_period_days_remaining.0 is not the start of the current date
1123-
// (since if they just started un-bonding and we just set days remaining to 7, or we already decremented
1194+
// (since if they just started un-bonding or just had less than min. mPower
1195+
// and we just set days remaining to 7, or we already decremented
11241196
// a miner's days remaining for the current date, then we want to wait until the next day until we
11251197
// decrement another day).
11261198
// if cooling_off_period_days_remaining.1 is Some(above 0), then decrement,
11271199
// but not yet completely unbonded so cannot withdraw yet
1128-
// note: we don't care if they stop bonding below the min. dhx during the cooling off period,
1129-
// as the user needs to learn that they should always been bonding the min. to
1200+
// note: we don't care if they stop bonding below the min. dhx or have less than min. mPower
1201+
// during the cooling off period,
1202+
// as the user needs to learn that they should always been bonding the min. and have min. mPower to
11301203
// maintain rewards, otherwise they have to wait for entire cooling down period and
11311204
// then the cooling off period again.
11321205
//
@@ -1158,7 +1231,7 @@ pub mod pallet {
11581231
(
11591232
start_of_requested_date_millis.clone(),
11601233
new_cooling_off_period_days_remaining.clone(),
1161-
2u32, // they have unbonded again, waiting to finish cooling down period
1234+
2u32, // they have unbonded or have less than min. mPower again, waiting to finish cooling down period
11621235
),
11631236
);
11641237

@@ -1193,6 +1266,9 @@ pub mod pallet {
11931266

11941267
log::info!("Finished initial loop of registered miners");
11951268

1269+
// TODO - consider the miner's mPower that we have fetched. it should have been added earlier above
1270+
// to the aggregated (all miners for that day) and accumulated (specific miner for a day) rewards
1271+
11961272
// fetch accumulated total rewards for all registered miners for the day
11971273
// TODO - we've done this twice, create a function to fetch it
11981274
let mut rewards_aggregated_dhx_daily: BalanceOf<T> = 0u32.into(); // initialize
@@ -1626,6 +1702,21 @@ pub mod pallet {
16261702
Ok(())
16271703
}
16281704

1705+
#[pallet::weight(10_000 + T::DbWeight::get().writes(1))]
1706+
pub fn set_min_mpower_daily(origin: OriginFor<T>, min_mpower_daily: u128) -> DispatchResult {
1707+
let _sender: T::AccountId = ensure_signed(origin)?;
1708+
1709+
<MinMPowerDaily<T>>::put(&min_mpower_daily.clone());
1710+
log::info!("set_min_mpower_daily: {:?}", &min_mpower_daily);
1711+
1712+
Self::deposit_event(Event::SetMinMPowerDailyStored(
1713+
min_mpower_daily.clone(),
1714+
_sender.clone(),
1715+
));
1716+
1717+
Ok(())
1718+
}
1719+
16291720
#[pallet::weight(10_000 + T::DbWeight::get().writes(1))]
16301721
pub fn set_cooling_off_period_days(origin: OriginFor<T>, cooling_off_period_days: u32) -> DispatchResult {
16311722
let _sender: T::AccountId = ensure_signed(origin)?;
@@ -1896,6 +1987,7 @@ pub mod pallet {
18961987
if let Some(_min_bonded_dhx_daily) = <MinBondedDHXDaily<T>>::get() {
18971988
min_bonded_dhx_daily = _min_bonded_dhx_daily;
18981989
} else {
1990+
// TODO - if this fails we could try and fetch the min_bonded_dhx_daily_default instead as a fallback
18991991
log::error!("Unable to retrieve any min. bonded DHX daily");
19001992
return Err(DispatchError::Other("Unable to retrieve any min. bonded DHX daily"));
19011993
}

pallets/mining/rewards-allowance/src/tests.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,27 @@ fn setup_preimage() {
302302
});
303303
}
304304

305+
#[test]
306+
fn it_sets_min_mpower_daily() {
307+
new_test_ext().execute_with(|| {
308+
assert_ok!(MiningRewardsAllowanceTestModule::set_min_mpower_daily(
309+
Origin::signed(1),
310+
5u128,
311+
));
312+
});
313+
}
314+
315+
#[test]
316+
#[ignore]
317+
fn it_allows_us_to_retrieve_genesis_value_for_min_mpower_daily() {
318+
new_test_ext().execute_with(|| {
319+
// FIXME - why doesn't it set the values we added in the chain_spec.rs at genesis
320+
// https://matrix.to/#/!HzySYSaIhtyWrwiwEV:matrix.org/$163424903366086IiiUH:matrix.org?via=matrix.parity.io&via=corepaper.org&via=matrix.org
321+
MiningRewardsAllowanceTestModule::on_initialize(1);
322+
assert_eq!(MiningRewardsAllowanceTestModule::min_mpower_daily(), Some(5u128));
323+
});
324+
}
325+
305326
fn distribute_rewards(amount_bonded_each_miner: u128) {
306327
assert_ok!(MiningRewardsAllowanceTestModule::set_registered_dhx_miner(Origin::signed(1)));
307328
assert_ok!(MiningRewardsAllowanceTestModule::set_registered_dhx_miner(Origin::signed(2)));

0 commit comments

Comments
 (0)