refactor calculate_vesting_factor
This commit is contained in:
parent
30c95f6010
commit
ec64df7889
1 changed files with 12 additions and 25 deletions
|
|
@ -746,42 +746,29 @@ double calculate_vesting_factor(const database& d, const account_object& stake_a
|
||||||
|
|
||||||
FC_ASSERT(period_start <= now && now <= period_end);
|
FC_ASSERT(period_start <= now && now <= period_end);
|
||||||
|
|
||||||
// get in what period we are
|
// get in what sub period we are
|
||||||
uint32_t current_period = 0;
|
uint32_t current_subperiod = 0;
|
||||||
std::list<uint32_t> period_list(number_of_subperiods);
|
std::list<uint32_t> period_list(number_of_subperiods);
|
||||||
std::iota(period_list.begin(), period_list.end(), 1); // todo: std::iota(period_list, 1); ??
|
std::iota(period_list.begin(), period_list.end(), 1);
|
||||||
|
|
||||||
std::for_each(period_list.begin(), period_list.end(),[&](uint32_t period) {
|
std::for_each(period_list.begin(), period_list.end(),[&](uint32_t period) {
|
||||||
if(seconds_since_period_start > vesting_subperiod * (period - 1) &&
|
if(seconds_since_period_start > vesting_subperiod * (period - 1) &&
|
||||||
seconds_since_period_start < vesting_subperiod * period)
|
seconds_since_period_start < vesting_subperiod * period)
|
||||||
current_period = period;
|
current_subperiod = period;
|
||||||
});
|
});
|
||||||
|
|
||||||
if(current_period == 0 || current_period > number_of_subperiods) return 0;
|
if(current_subperiod == 0 || current_subperiod > number_of_subperiods) return 0;
|
||||||
|
|
||||||
// coefficient calculation is: (n-1)/number_of_subperiods
|
double numerator = number_of_subperiods - current_subperiod + 1;
|
||||||
double n = number_of_subperiods + 1;
|
|
||||||
|
|
||||||
// todo: improve this loop
|
auto last_period_start = period_start + fc::seconds(vesting_subperiod * (current_subperiod - 1));
|
||||||
std::list<uint32_t> subperiod_list(current_period);
|
auto last_period_end = period_start + fc::seconds(vesting_subperiod * (current_subperiod));
|
||||||
std::iota(subperiod_list.begin(), subperiod_list.end(), 1);
|
|
||||||
|
|
||||||
for(auto subperiod: subperiod_list)
|
// reset n if account voted last period
|
||||||
{
|
if (last_date_voted < last_period_start && last_date_voted >= last_period_end)
|
||||||
if (current_period - subperiod > 0) {
|
numerator = number_of_subperiods;
|
||||||
n = number_of_subperiods - current_period + 2;
|
|
||||||
|
|
||||||
if (last_date_voted <
|
vesting_factor = numerator / number_of_subperiods;
|
||||||
(period_start + fc::seconds(vesting_subperiod * (current_period - subperiod - 1))) &&
|
|
||||||
last_date_voted >= (period_start + fc::seconds(vesting_subperiod * (current_period - subperiod))) &&
|
|
||||||
last_date_voted >= period_start) {
|
|
||||||
|
|
||||||
n = number_of_subperiods + 1;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
vesting_factor = (n - 1) / number_of_subperiods;
|
|
||||||
|
|
||||||
return vesting_factor;
|
return vesting_factor;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue