Skip to content

Commit be42e5b

Browse files
committed
add tests to check eligibility and ineligibility for rewards if miner mPower less than daily minimum
1 parent 2427e2b commit be42e5b

File tree

2 files changed

+177
-15
lines changed

2 files changed

+177
-15
lines changed

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

Lines changed: 80 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,16 @@ pub mod pallet {
109109
BalanceOf<T>,
110110
>;
111111

112+
#[pallet::storage]
113+
#[pallet::getter(fn mpower_of_account_for_date)]
114+
pub(super) type MPowerForAccountForDate<T: Config> = StorageMap<_, Blake2_128Concat,
115+
(
116+
Date,
117+
T::AccountId,
118+
),
119+
u128,
120+
>;
121+
112122
#[pallet::storage]
113123
#[pallet::getter(fn rewards_allowance_dhx_for_date_remaining)]
114124
pub(super) type RewardsAllowanceDHXForDateRemaining<T: Config> = StorageMap<_, Blake2_128Concat,
@@ -395,6 +405,10 @@ pub mod pallet {
395405
/// \[date, amount_dhx_bonded, account_dhx_bonded\]
396406
SetBondedDHXOfAccountForDateStored(Date, BalanceOf<T>, T::AccountId),
397407

408+
/// Storage of the mPower of an account on a specific date.
409+
/// \[date, amount_mpower, account\]
410+
SetMPowerOfAccountForDateStored(Date, u128, T::AccountId),
411+
398412
/// Storage of the default daily reward allowance in DHX by an origin account.
399413
/// \[amount_dhx, sender\]
400414
SetRewardsAllowanceDHXDailyStored(BalanceOf<T>, T::AccountId),
@@ -985,12 +999,26 @@ pub mod pallet {
985999
}
9861000
// println!("min_mpower_daily_u128 {:#?}", min_mpower_daily_u128);
9871001

988-
// TODO - move this into off-chain workers function
1002+
// TODO - integrate this with functionality of off-chain workers function where we
1003+
// fetch the mpower from off-chain and store it with `set_mpower_of_account_for_date`
9891004
// TODO - fetch the mPower of the miner currently being iterated to check if it's greater than the min.
9901005
// mPower that is required
991-
let mpower_miner_u128: u128 = 5u128;
1006+
let mut mpower_current_u128: u128 = 0u128;
1007+
let _mpower_current_u128 = <MPowerForAccountForDate<T>>::get((start_of_requested_date_millis.clone(), miner.clone()));
1008+
match _mpower_current_u128 {
1009+
None => {
1010+
log::error!("Unable to get_mpower_of_account_for_date {:?}", start_of_requested_date_millis.clone());
1011+
println!("Unable to get_mpower_of_account_for_date {:?}", start_of_requested_date_millis.clone());
1012+
},
1013+
Some(x) => {
1014+
mpower_current_u128 = x;
1015+
}
1016+
}
1017+
log::info!("mpower_current_u128 {:#?}, {:?}", mpower_current_u128, start_of_requested_date_millis.clone());
1018+
// println!("mpower_current_u128 {:#?}, {:?}", mpower_current_u128, start_of_requested_date_millis.clone());
1019+
9921020
let mut has_min_mpower_daily = false;
993-
if mpower_miner_u128 >= min_mpower_daily_u128 {
1021+
if mpower_current_u128 >= min_mpower_daily_u128 {
9941022
has_min_mpower_daily = true;
9951023
}
9961024
log::info!("has_min_mpower_daily: {:?} {:?}", has_min_mpower_daily.clone(), miner.clone());
@@ -1995,16 +2023,28 @@ pub mod pallet {
19952023

19962024
fn convert_u64_in_milliseconds_to_start_of_date(date_as_u64_millis: u64) -> Result<Date, DispatchError> {
19972025
let date_as_u64_secs = date_as_u64_millis.clone() / 1000u64;
2026+
log::info!("convert_u64_in_milliseconds_to_start_of_date - date_as_u64_secs: {:?}", date_as_u64_secs.clone());
19982027
// https://docs.rs/chrono/0.4.6/chrono/naive/struct.NaiveDateTime.html#method.from_timestamp
19992028
let date = NaiveDateTime::from_timestamp(i64::try_from(date_as_u64_secs).unwrap(), 0).date();
2000-
log::info!("convert_u64_in_milliseconds_to_start_of_date - date_as_u64_secs: {:?}", date_as_u64_secs.clone());
20012029

20022030
let date_start_millis = date.and_hms(0, 0, 0).timestamp() * 1000;
20032031
log::info!("convert_u64_in_milliseconds_to_start_of_date - date_start_millis: {:?}", date_start_millis.clone());
20042032
log::info!("convert_u64_in_milliseconds_to_start_of_date - Timestamp requested Date: {:?}", date);
20052033
return Ok(date_start_millis);
20062034
}
20072035

2036+
fn convert_i64_in_milliseconds_to_start_of_date(date_as_i64_millis: i64) -> Result<Date, DispatchError> {
2037+
let date_as_i64_secs = date_as_i64_millis.clone() / 1000i64;
2038+
log::info!("convert_i64_in_milliseconds_to_start_of_date - date_as_i64_secs: {:?}", date_as_i64_secs.clone());
2039+
// https://docs.rs/chrono/0.4.6/chrono/naive/struct.NaiveDateTime.html#method.from_timestamp
2040+
let date = NaiveDateTime::from_timestamp(i64::try_from(date_as_i64_secs).unwrap(), 0).date();
2041+
2042+
let date_start_millis = date.and_hms(0, 0, 0).timestamp() * 1000;
2043+
log::info!("convert_i64_in_milliseconds_to_start_of_date - date_start_millis: {:?}", date_start_millis.clone());
2044+
log::info!("convert_i64_in_milliseconds_to_start_of_date - Timestamp requested Date: {:?}", date);
2045+
return Ok(date_start_millis);
2046+
}
2047+
20082048
fn convert_balance_to_u128(balance: BalanceOf<T>) -> Result<u128, DispatchError> {
20092049
let balance_as_u128;
20102050

@@ -2101,6 +2141,42 @@ pub mod pallet {
21012141
Ok(bonded_dhx_current_u128.clone())
21022142
}
21032143

2144+
// we need to set the mPower for the next start date so it's available from off-chain in time
2145+
pub fn set_mpower_of_account_for_date(account_id: T::AccountId, mpower: u128, next_start_date: Date) -> Result<u128, DispatchError> {
2146+
// // Note: we DO need the following as we're using the current timestamp, rather than a function parameter.
2147+
// let timestamp: <T as pallet_timestamp::Config>::Moment = <pallet_timestamp::Pallet<T>>::get();
2148+
// let requested_date_as_u64 = Self::convert_moment_to_u64_in_milliseconds(timestamp.clone())?;
2149+
// log::info!("set_mpower_of_account_for_date - requested_date_as_u64: {:?}", requested_date_as_u64.clone());
2150+
2151+
// convert the requested date/time to the start of that day date/time to signify that date for lookup
2152+
// i.e. 21 Apr @ 1420 -> 21 Apr @ 0000
2153+
let start_of_next_start_date_millis = Self::convert_i64_in_milliseconds_to_start_of_date(next_start_date.clone())?;
2154+
2155+
let mpower_current_u128 = mpower.clone();
2156+
2157+
// Update storage. Override the default that may have been set in on_initialize
2158+
<MPowerForAccountForDate<T>>::insert(
2159+
(
2160+
start_of_next_start_date_millis.clone(),
2161+
account_id.clone(),
2162+
),
2163+
mpower_current_u128.clone(),
2164+
);
2165+
log::info!("set_mpower_of_account_for_date - start_of_next_start_date_millis: {:?}", &start_of_next_start_date_millis);
2166+
log::info!("set_mpower_of_account_for_date - account_id: {:?}", &account_id);
2167+
log::info!("set_mpower_of_account_for_date - mpower_current: {:?}", &mpower_current_u128);
2168+
2169+
// Emit an event.
2170+
Self::deposit_event(Event::SetMPowerOfAccountForDateStored(
2171+
start_of_next_start_date_millis.clone(),
2172+
mpower_current_u128.clone(),
2173+
account_id.clone(),
2174+
));
2175+
2176+
// Return a successful DispatchResultWithPostInfo
2177+
Ok(mpower_current_u128.clone())
2178+
}
2179+
21042180
fn get_min_bonded_dhx_daily() -> Result<(BalanceOf<T>, u128), DispatchError> {
21052181
let mut min_bonded_dhx_daily: BalanceOf<T> = 10u32.into(); // initialize
21062182
let mut min_bonded_dhx_daily_u128: u128 = TEN;

0 commit comments

Comments
 (0)