From 971cdbbb1fa1878a4dfead69c24c49098d092db6 Mon Sep 17 00:00:00 2001 From: "Jeff Washington (jwash)" Date: Fri, 25 Feb 2022 09:56:48 -0600 Subject: [PATCH] refactor rent_due for normal case of exempt --- runtime/src/rent_collector.rs | 31 +++++++++++++++++++------------ sdk/program/src/rent.rs | 11 ++++++++--- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/runtime/src/rent_collector.rs b/runtime/src/rent_collector.rs index dbf7eda3a61623..df378a40123895 100644 --- a/runtime/src/rent_collector.rs +++ b/runtime/src/rent_collector.rs @@ -83,19 +83,26 @@ impl RentCollector { /// given an account that 'should_collect_rent' /// returns (amount rent due, is_exempt_from_rent) pub fn get_rent_due(&self, account: &impl ReadableAccount) -> RentDue { - let slots_elapsed: u64 = (account.rent_epoch()..=self.epoch) - .map(|epoch| self.epoch_schedule.get_slots_in_epoch(epoch + 1)) - .sum(); - - // avoid infinite rent in rust 1.45 - let years_elapsed = if self.slots_per_year != 0.0 { - slots_elapsed as f64 / self.slots_per_year + if self + .rent + .is_exempt(account.lamports(), account.data().len()) + { + RentDue::Exempt } else { - 0.0 - }; - - self.rent - .due(account.lamports(), account.data().len(), years_elapsed) + let slots_elapsed: u64 = (account.rent_epoch()..=self.epoch) + .map(|epoch| self.epoch_schedule.get_slots_in_epoch(epoch + 1)) + .sum(); + + // avoid infinite rent in rust 1.45 + 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 + self.rent.paying_due(account.data().len(), years_elapsed) + } } // Updates the account's lamports and status, and returns the amount of rent collected, if any. diff --git a/sdk/program/src/rent.rs b/sdk/program/src/rent.rs index 88f6aeb8d4fa77..ccf83bbfb14145 100644 --- a/sdk/program/src/rent.rs +++ b/sdk/program/src/rent.rs @@ -68,12 +68,17 @@ impl Rent { if self.is_exempt(balance, data_len) { RentDue::Exempt } else { - let actual_data_len = data_len as u64 + ACCOUNT_STORAGE_OVERHEAD; - let lamports_per_year = self.lamports_per_byte_year * actual_data_len; - RentDue::Paying((lamports_per_year as f64 * years_elapsed) as u64) + self.paying_due(data_len, years_elapsed) } } + /// rent due for account that is known to be not exempt + pub fn paying_due(&self, data_len: usize, years_elapsed: f64) -> RentDue { + let actual_data_len = data_len as u64 + ACCOUNT_STORAGE_OVERHEAD; + let lamports_per_year = self.lamports_per_byte_year * actual_data_len; + RentDue::Paying((lamports_per_year as f64 * years_elapsed) as u64) + } + pub fn free() -> Self { Self { lamports_per_byte_year: 0,