Skip to content

Commit df73d7a

Browse files
committed
remove set timestamp before block 1. fix set_bonded_dhx_of_account_for_date and remove BondedDHXData type alias
1 parent 439f565 commit df73d7a

File tree

2 files changed

+114
-99
lines changed

2 files changed

+114
-99
lines changed

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

Lines changed: 71 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,6 @@ pub mod pallet {
7070
type BalanceFromBalancePallet<T> = <T as pallet_balances::Config>::Balance;
7171
type Date = i64;
7272

73-
#[derive(Encode, Decode, Debug, Default, Clone, Eq, PartialEq)]
74-
#[cfg_attr(feature = "std", derive())]
75-
pub struct BondedDHXForAccountData<U, V, W> {
76-
pub account_id: U,
77-
pub bonded_dhx_current: V,
78-
pub requestor_account_id: W,
79-
}
80-
81-
type BondedData<T> = BondedDHXForAccountData<
82-
<T as frame_system::Config>::AccountId,
83-
BalanceOf<T>,
84-
<T as frame_system::Config>::AccountId,
85-
>;
8673

8774
/// Configure the pallet by specifying the parameters and types on which it depends.
8875
#[pallet::config]
@@ -107,8 +94,11 @@ pub mod pallet {
10794
#[pallet::storage]
10895
#[pallet::getter(fn bonded_dhx_of_account_for_date)]
10996
pub(super) type BondedDHXForAccountForDate<T: Config> = StorageMap<_, Blake2_128Concat,
110-
Date,
111-
BondedData<T>
97+
(
98+
Date,
99+
T::AccountId,
100+
),
101+
BalanceOf<T>,
112102
>;
113103

114104
#[pallet::storage]
@@ -279,9 +269,9 @@ pub mod pallet {
279269
/// \[cooling_off_period_days\]
280270
SetCoolingOffPeriodDaysStored(u32),
281271

282-
/// Storage of the bonded DHX of an account on a specific date by a requesting origin account.
283-
/// \[date, amount_dhx_bonded, account_dhx_bonded, sender\]
284-
SetBondedDHXOfAccountForDateStored(Date, BondedData<T>, T::AccountId, T::AccountId),
272+
/// Storage of the bonded DHX of an account on a specific date.
273+
/// \[date, amount_dhx_bonded, account_dhx_bonded\]
274+
SetBondedDHXOfAccountForDateStored(Date, BalanceOf<T>, T::AccountId),
285275

286276
/// Storage of the default daily reward allowance in DHX by an origin account.
287277
/// \[amount_dhx, sender\]
@@ -438,8 +428,8 @@ pub mod pallet {
438428
// Test with 2x registered miners each with values like `25133000000000000000000u128`, which is over
439429
// half of 5000 DHX daily allowance (of 2500 DHX), but in that case we split the rewards
440430
// (i.e. 25,133 DHX locked at 10:1 gives 2513 DHX reward)
441-
let mut locks_first_amount_as_u128 = 25_133_000_000_000_000_000_000u128;
442431

432+
let mut locks_first_amount_as_u128 = 25_133_000_000_000_000_000_000u128;
443433
let locked_vec = <pallet_balances::Pallet<T>>::locks(miner.clone()).into_inner();
444434
if locked_vec.len() != 0 {
445435
let locks_first_amount: <T as pallet_balances::Config>::Balance =
@@ -476,6 +466,22 @@ pub mod pallet {
476466
// reasons: Reasons::Misc,
477467
// },
478468

469+
let bonded_dhx_current_u128;
470+
let _bonded_dhx_current_u128 = Self::set_bonded_dhx_of_account_for_date(
471+
miner.clone(),
472+
locks_first_amount_as_u128.clone()
473+
);
474+
match _bonded_dhx_current_u128 {
475+
Err(_e) => {
476+
log::error!("Unable to set_bonded_dhx_of_account_for_date");
477+
return 0;
478+
},
479+
Ok(ref x) => {
480+
bonded_dhx_current_u128 = x;
481+
}
482+
}
483+
log::info!("set_bonded_dhx_of_account_for_date: {:?} {:?}", start_of_requested_date_millis.clone(), bonded_dhx_current_u128.clone());
484+
479485
// TODO - refactor to use `convert_balance_to_u128` instead of all the following
480486
let min_bonded_dhx_daily;
481487
let min_bonded_dhx_daily_to_try = <MinBondedDHXDaily<T>>::get();
@@ -1281,59 +1287,6 @@ pub mod pallet {
12811287
Ok(())
12821288
}
12831289

1284-
// customised by governance at any time. this function allows us to change it each year
1285-
// https://docs.google.com/spreadsheets/d/1W2AzOH9Cs9oCR8UYfYCbpmd9X7hp-USbYXL7AuwMY_Q/edit#gid=970997021
1286-
#[pallet::weight(10_000 + T::DbWeight::get().writes(1))]
1287-
pub fn set_bonded_dhx_of_account_for_date(origin: OriginFor<T>, account_id: T::AccountId) -> DispatchResult {
1288-
let _who = ensure_signed(origin)?;
1289-
1290-
// Note: we DO need the following as we're using the current timestamp, rather than a function parameter.
1291-
let timestamp: <T as pallet_timestamp::Config>::Moment = <pallet_timestamp::Pallet<T>>::get();
1292-
let requested_date_as_u64 = Self::convert_moment_to_u64_in_milliseconds(timestamp.clone())?;
1293-
log::info!("set_bonded_dhx_of_account_for_date - requested_date_as_u64: {:?}", requested_date_as_u64.clone());
1294-
1295-
// convert the requested date/time to the start of that day date/time to signify that date for lookup
1296-
// i.e. 21 Apr @ 1420 -> 21 Apr @ 0000
1297-
let start_of_requested_date_millis = Self::convert_u64_in_milliseconds_to_start_of_date(requested_date_as_u64.clone())?;
1298-
1299-
// TODO - fetch from democracy or elections
1300-
let bonded_dhx_current_u128 = 1000u128;
1301-
1302-
let bonded_dhx_current;
1303-
let _bonded_dhx_current = Self::convert_u128_to_balance(bonded_dhx_current_u128.clone());
1304-
match _bonded_dhx_current {
1305-
Err(_e) => {
1306-
log::error!("Unable to convert u128 to balance for bonded_dhx_current");
1307-
return Err(DispatchError::Other("Unable to convert u128 to balance for bonded_dhx_current"));
1308-
},
1309-
Ok(ref x) => {
1310-
bonded_dhx_current = x;
1311-
}
1312-
}
1313-
1314-
let bonded_data: BondedData<T> = BondedDHXForAccountData {
1315-
account_id: account_id.clone(),
1316-
bonded_dhx_current: bonded_dhx_current.clone(),
1317-
requestor_account_id: _who.clone(),
1318-
};
1319-
1320-
// Update storage. Override the default that may have been set in on_initialize
1321-
<BondedDHXForAccountForDate<T>>::insert(start_of_requested_date_millis.clone(), &bonded_data);
1322-
log::info!("set_bonded_dhx_of_account_for_date - account_id: {:?}", &account_id);
1323-
log::info!("set_bonded_dhx_of_account_for_date - bonded_data: {:?}", &bonded_data);
1324-
1325-
// Emit an event.
1326-
Self::deposit_event(Event::SetBondedDHXOfAccountForDateStored(
1327-
start_of_requested_date_millis.clone(),
1328-
bonded_data.clone(),
1329-
account_id.clone(),
1330-
_who.clone(),
1331-
));
1332-
1333-
// Return a successful DispatchResultWithPostInfo
1334-
Ok(())
1335-
}
1336-
13371290
// TODO: we need to change this in future so it is only modifiable by governance,
13381291
// rather than just any user
13391292
#[pallet::weight(10_000 + T::DbWeight::get().writes(1))]
@@ -1526,5 +1479,51 @@ pub mod pallet {
15261479

15271480
return Ok(blocknumber_u64);
15281481
}
1482+
1483+
fn set_bonded_dhx_of_account_for_date(account_id: T::AccountId, bonded_dhx: u128) -> Result<u128, DispatchError> {
1484+
// Note: we DO need the following as we're using the current timestamp, rather than a function parameter.
1485+
let timestamp: <T as pallet_timestamp::Config>::Moment = <pallet_timestamp::Pallet<T>>::get();
1486+
let requested_date_as_u64 = Self::convert_moment_to_u64_in_milliseconds(timestamp.clone())?;
1487+
log::info!("set_bonded_dhx_of_account_for_date - requested_date_as_u64: {:?}", requested_date_as_u64.clone());
1488+
1489+
// convert the requested date/time to the start of that day date/time to signify that date for lookup
1490+
// i.e. 21 Apr @ 1420 -> 21 Apr @ 0000
1491+
let start_of_requested_date_millis = Self::convert_u64_in_milliseconds_to_start_of_date(requested_date_as_u64.clone())?;
1492+
1493+
let bonded_dhx_current_u128 = bonded_dhx.clone();
1494+
1495+
let bonded_dhx_current;
1496+
let _bonded_dhx_current = Self::convert_u128_to_balance(bonded_dhx_current_u128.clone());
1497+
match _bonded_dhx_current {
1498+
Err(_e) => {
1499+
log::error!("Unable to convert u128 to balance for bonded_dhx_current");
1500+
return Err(DispatchError::Other("Unable to convert u128 to balance for bonded_dhx_current"));
1501+
},
1502+
Ok(ref x) => {
1503+
bonded_dhx_current = x;
1504+
}
1505+
}
1506+
1507+
// Update storage. Override the default that may have been set in on_initialize
1508+
<BondedDHXForAccountForDate<T>>::insert(
1509+
(
1510+
start_of_requested_date_millis.clone(),
1511+
account_id.clone(),
1512+
),
1513+
bonded_dhx_current.clone(),
1514+
);
1515+
log::info!("set_bonded_dhx_of_account_for_date - account_id: {:?}", &account_id);
1516+
log::info!("set_bonded_dhx_of_account_for_date - bonded_dhx_current: {:?}", &bonded_dhx_current);
1517+
1518+
// Emit an event.
1519+
Self::deposit_event(Event::SetBondedDHXOfAccountForDateStored(
1520+
start_of_requested_date_millis.clone(),
1521+
bonded_dhx_current.clone(),
1522+
account_id.clone(),
1523+
));
1524+
1525+
// Return a successful DispatchResultWithPostInfo
1526+
Ok(bonded_dhx_current_u128.clone())
1527+
}
15291528
}
15301529
}

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

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use super::{Call, Event, *};
22
use crate::{mock::*, Error};
3-
use crate::{BondedDHXForAccountData};
43
use codec::Encode;
54
use frame_support::{assert_noop, assert_ok,
65
weights::{DispatchClass, DispatchInfo, GetDispatchInfo},
@@ -62,38 +61,55 @@ fn it_distributes_rewards_automatically_in_on_finalize() {
6261
5000_u128,
6362
));
6463

65-
// https://www.epochconverter.com/
64+
assert_eq!(MiningRewardsAllowanceTestModule::registered_dhx_miners(), Some(vec![1, 2, 3]));
65+
assert_eq!(MiningRewardsAllowanceTestModule::min_bonded_dhx_daily(), Some(10));
66+
assert_eq!(MiningRewardsAllowanceTestModule::cooling_off_period_days(), Some(1));
67+
assert_eq!(MiningRewardsAllowanceTestModule::rewards_allowance_dhx_daily(), Some(5_000u128));
68+
69+
// since the timestamp is 0 (corresponds to 1970-01-01) at block number #1, we early exit from on_initialize in
70+
// that block in the implementation and do not set any storage values associated with the date until block #2.
71+
// in the tests we could set the timestamp before we run on_initialize(1), but that wouldn't reflect reality.
72+
MiningRewardsAllowanceTestModule::on_initialize(1);
73+
6674
// 27th August 2021 @ ~7am is 1630049371000
6775
// where milliseconds/day 86400000
6876
// 27th August 2021 @ 12am is 1630022400000 (start of day)
6977
Timestamp::set_timestamp(1630049371000u64);
70-
MiningRewardsAllowanceTestModule::on_initialize(1);
71-
// System::on_initialize(1);
72-
// System::on_finalize(1);
73-
// System::set_block_number(1);
78+
MiningRewardsAllowanceTestModule::on_initialize(2);
79+
// System::on_initialize(2);
80+
// System::on_finalize(2);
81+
// System::set_block_number(2);
7482

75-
assert_eq!(MiningRewardsAllowanceTestModule::registered_dhx_miners(), Some(vec![1, 2, 3]));
76-
assert_eq!(MiningRewardsAllowanceTestModule::min_bonded_dhx_daily(), Some(10));
77-
assert_eq!(MiningRewardsAllowanceTestModule::cooling_off_period_days(), Some(1));
78-
assert_eq!(MiningRewardsAllowanceTestModule::rewards_allowance_dhx_daily(), Some(5_000u128));
79-
// check that on_initialize has populated this storage value automatically for the start of the current date
8083
assert_eq!(MiningRewardsAllowanceTestModule::rewards_allowance_dhx_for_date(1630022400000), Some(5_000u128));
84+
assert_eq!(MiningRewardsAllowanceTestModule::rewards_allowance_dhx_for_date_distributed(1630022400000), Some(false));
8185

86+
// https://www.epochconverter.com/
8287
// 28th August 2021 @ ~7am is 1635406274000
88+
// where milliseconds/day 86400000
8389
// 28th August 2021 @ 12am is 1635379200000 (start of day)
8490
Timestamp::set_timestamp(1635406274000u64);
85-
MiningRewardsAllowanceTestModule::on_initialize(2);
91+
MiningRewardsAllowanceTestModule::on_initialize(3);
8692

93+
// check that on_initialize has populated this storage value automatically for the start of the current date
8794
// still cooling off so no rewards distributed on this date
8895
assert_eq!(MiningRewardsAllowanceTestModule::rewards_allowance_dhx_for_date(1635379200000), Some(5_000u128));
96+
assert_eq!(MiningRewardsAllowanceTestModule::rewards_allowance_dhx_for_date_distributed(1635379200000), Some(false));
97+
98+
assert_eq!(MiningRewardsAllowanceTestModule::bonded_dhx_of_account_for_date((1635379200000, 1)), Some(25_133_000_000_000_000_000_000u128));
99+
assert_eq!(MiningRewardsAllowanceTestModule::bonded_dhx_of_account_for_date((1635379200000, 2)), Some(25_133_000_000_000_000_000_000u128));
100+
assert_eq!(MiningRewardsAllowanceTestModule::bonded_dhx_of_account_for_date((1635379200000, 3)), Some(25_133_000_000_000_000_000_000u128));
89101

90102
// 29th August 2021 @ ~7am is 1630220400000
91103
// 29th August 2021 @ 12am is 1630195200000 (start of day)
92104
Timestamp::set_timestamp(1630195200000u64);
93-
MiningRewardsAllowanceTestModule::on_initialize(3);
105+
MiningRewardsAllowanceTestModule::on_initialize(4);
94106

95107
assert_eq!(MiningRewardsAllowanceTestModule::rewards_allowance_dhx_for_date(1630195200000), Some(5_000u128));
96108

109+
assert_eq!(MiningRewardsAllowanceTestModule::bonded_dhx_of_account_for_date((1630195200000, 1)), Some(25_133_000_000_000_000_000_000u128));
110+
assert_eq!(MiningRewardsAllowanceTestModule::bonded_dhx_of_account_for_date((1630195200000, 2)), Some(25_133_000_000_000_000_000_000u128));
111+
assert_eq!(MiningRewardsAllowanceTestModule::bonded_dhx_of_account_for_date((1630195200000, 3)), Some(25_133_000_000_000_000_000_000u128));
112+
97113
// i.e. for example, if locked is 25_133_000_000_000_000_000_000u128, which is 25,133 DHX,
98114
// then with 10:1 each of the 3x accounts get 2513.3 DHX, which is ~7538.9 DHX combined
99115
assert_eq!(MiningRewardsAllowanceTestModule::rewards_aggregated_dhx_for_all_miners_for_date(1630195200000), Some(7_539_900_000_000_000_000_000u128));
@@ -107,6 +123,20 @@ fn it_distributes_rewards_automatically_in_on_finalize() {
107123
})
108124
}
109125

126+
#[test]
127+
#[ignore]
128+
fn it_distributes_rewards_automatically_in_on_finalize_for_large_amounts() {
129+
new_test_ext().execute_with(|| {
130+
// TODO - create a test that instead of using a hard-coded value for `locks_first_amount_as_u128`
131+
// that is in the implementation, it instead sets the locked value of each of then using frame_balances
132+
// for the 3x miners, since we can then store that with `set_bonded_dhx_of_account_for_date` and
133+
// then use that easier for the tests too for trying different values that they have bonded.
134+
//
135+
// in this test we'll test that it distributes rewards when each of their account balances are very large
136+
// (i.e. a third of the total supply) 33_333_333_333_000_000_000_000_000u128
137+
})
138+
}
139+
110140
#[test]
111141
fn it_sets_rewards_allowance_with_timestamp() {
112142
new_test_ext().execute_with(|| {
@@ -128,25 +158,11 @@ fn it_sets_rewards_allowance_with_timestamp() {
128158
1630049371000
129159
));
130160

131-
assert_ok!(MiningRewardsAllowanceTestModule::set_bonded_dhx_of_account_for_date(
132-
Origin::signed(0),
133-
1
134-
));
135-
136161
// Verify Storage
137162
assert_eq!(MiningRewardsAllowanceTestModule::rewards_allowance_dhx_daily(), Some(5_000u128));
138163

139164
assert_eq!(MiningRewardsAllowanceTestModule::rewards_allowance_dhx_for_date(1630022400000), Some(5_000u128));
140165

141-
assert_eq!(
142-
MiningRewardsAllowanceTestModule::bonded_dhx_of_account_for_date(1630022400000),
143-
Some(BondedDHXForAccountData {
144-
account_id: 1u128,
145-
bonded_dhx_current: 1_000u128,
146-
requestor_account_id: 0u128,
147-
})
148-
);
149-
150166
assert_ok!(MiningRewardsAllowanceTestModule::change_remaining_rewards_allowance_dhx_for_date(
151167
Origin::signed(0),
152168
500,

0 commit comments

Comments
 (0)