refactor rent_due for normal case of exempt (#23350)
This commit is contained in:
parent
ee3fc39f1c
commit
611d745241
|
@ -83,19 +83,26 @@ impl RentCollector {
|
||||||
/// given an account that 'should_collect_rent'
|
/// given an account that 'should_collect_rent'
|
||||||
/// returns (amount rent due, is_exempt_from_rent)
|
/// returns (amount rent due, is_exempt_from_rent)
|
||||||
pub fn get_rent_due(&self, account: &impl ReadableAccount) -> RentDue {
|
pub fn get_rent_due(&self, account: &impl ReadableAccount) -> RentDue {
|
||||||
let slots_elapsed: u64 = (account.rent_epoch()..=self.epoch)
|
if self
|
||||||
.map(|epoch| self.epoch_schedule.get_slots_in_epoch(epoch + 1))
|
.rent
|
||||||
.sum();
|
.is_exempt(account.lamports(), account.data().len())
|
||||||
|
{
|
||||||
// avoid infinite rent in rust 1.45
|
RentDue::Exempt
|
||||||
let years_elapsed = if self.slots_per_year != 0.0 {
|
|
||||||
slots_elapsed as f64 / self.slots_per_year
|
|
||||||
} else {
|
} else {
|
||||||
0.0
|
let slots_elapsed: u64 = (account.rent_epoch()..=self.epoch)
|
||||||
};
|
.map(|epoch| self.epoch_schedule.get_slots_in_epoch(epoch + 1))
|
||||||
|
.sum();
|
||||||
|
|
||||||
self.rent
|
// avoid infinite rent in rust 1.45
|
||||||
.due(account.lamports(), account.data().len(), years_elapsed)
|
let years_elapsed = if self.slots_per_year != 0.0 {
|
||||||
|
slots_elapsed as f64 / self.slots_per_year
|
||||||
|
} else {
|
||||||
|
0.0
|
||||||
|
};
|
||||||
|
|
||||||
|
// we know this account is not exempt
|
||||||
|
RentDue::Paying(self.rent.due_amount(account.data().len(), years_elapsed))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Updates the account's lamports and status, and returns the amount of rent collected, if any.
|
// Updates the account's lamports and status, and returns the amount of rent collected, if any.
|
||||||
|
|
|
@ -68,12 +68,17 @@ impl Rent {
|
||||||
if self.is_exempt(balance, data_len) {
|
if self.is_exempt(balance, data_len) {
|
||||||
RentDue::Exempt
|
RentDue::Exempt
|
||||||
} else {
|
} else {
|
||||||
let actual_data_len = data_len as u64 + ACCOUNT_STORAGE_OVERHEAD;
|
RentDue::Paying(self.due_amount(data_len, years_elapsed))
|
||||||
let lamports_per_year = self.lamports_per_byte_year * actual_data_len;
|
|
||||||
RentDue::Paying((lamports_per_year as f64 * years_elapsed) as u64)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// rent due for account that is known to be not exempt
|
||||||
|
pub fn due_amount(&self, data_len: usize, years_elapsed: f64) -> u64 {
|
||||||
|
let actual_data_len = data_len as u64 + ACCOUNT_STORAGE_OVERHEAD;
|
||||||
|
let lamports_per_year = self.lamports_per_byte_year * actual_data_len;
|
||||||
|
(lamports_per_year as f64 * years_elapsed) as u64
|
||||||
|
}
|
||||||
|
|
||||||
pub fn free() -> Self {
|
pub fn free() -> Self {
|
||||||
Self {
|
Self {
|
||||||
lamports_per_byte_year: 0,
|
lamports_per_byte_year: 0,
|
||||||
|
|
Loading…
Reference in New Issue