Wrong rpc inflation rate (#14063)

* Fix wrong inflation calculation in rpc

* Reorder

* Fix test
This commit is contained in:
Ryo Onodera 2020-12-11 16:42:39 +09:00 committed by GitHub
parent 4fba7e6865
commit 09bd412b13
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 14 deletions

View File

@ -347,13 +347,12 @@ impl JsonRpcRequestProcessor {
let bank = self.bank(None); let bank = self.bank(None);
let epoch = bank.epoch(); let epoch = bank.epoch();
let inflation = bank.inflation(); let inflation = bank.inflation();
let year = let slot_in_year = bank.slot_in_year_for_inflation();
(bank.epoch_schedule().get_last_slot_in_epoch(epoch)) as f64 / bank.slots_per_year();
RpcInflationRate { RpcInflationRate {
total: inflation.total(year), total: inflation.total(slot_in_year),
validator: inflation.validator(year), validator: inflation.validator(slot_in_year),
foundation: inflation.foundation(year), foundation: inflation.foundation(slot_in_year),
epoch, epoch,
} }
} }
@ -3307,12 +3306,11 @@ pub mod tests {
}; };
let inflation = bank.inflation(); let inflation = bank.inflation();
let epoch = bank.epoch(); let epoch = bank.epoch();
let year = let slot_in_year = bank.slot_in_year_for_inflation();
(bank.epoch_schedule().get_last_slot_in_epoch(epoch)) as f64 / bank.slots_per_year();
let expected_inflation_rate = RpcInflationRate { let expected_inflation_rate = RpcInflationRate {
total: inflation.total(year), total: inflation.total(slot_in_year),
validator: inflation.validator(year), validator: inflation.validator(slot_in_year),
foundation: inflation.foundation(year), foundation: inflation.foundation(slot_in_year),
epoch, epoch,
}; };
assert_eq!(inflation_rate, expected_inflation_rate); assert_eq!(inflation_rate, expected_inflation_rate);

View File

@ -1511,6 +1511,13 @@ impl Bank {
self.epoch_schedule.get_first_slot_in_epoch(self.epoch()) - inflation_start_slot self.epoch_schedule.get_first_slot_in_epoch(self.epoch()) - inflation_start_slot
} }
pub fn slot_in_year_for_inflation(&self) -> f64 {
let num_slots = self.get_inflation_num_slots();
// calculated as: num_slots / (slots / year)
num_slots as f64 / self.slots_per_year
}
// update rewards based on the previous epoch // update rewards based on the previous epoch
fn update_rewards( fn update_rewards(
&mut self, &mut self,
@ -1522,10 +1529,7 @@ impl Bank {
} }
// if I'm the first Bank in an epoch, count, claim, disburse rewards from Inflation // if I'm the first Bank in an epoch, count, claim, disburse rewards from Inflation
// calculated as: num_slots / (slots / year) let slot_in_year = self.slot_in_year_for_inflation();
let num_slots = self.get_inflation_num_slots();
let slot_in_year = num_slots as f64 / self.slots_per_year;
let epoch_duration_in_years = self.epoch_duration_in_years(prev_epoch); let epoch_duration_in_years = self.epoch_duration_in_years(prev_epoch);
let (validator_rate, foundation_rate) = { let (validator_rate, foundation_rate) = {