Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

uses named fields for RentResullt::CollectRent enum variant #26449

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions runtime/src/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1713,9 +1713,9 @@ mod tests {
let mut error_counters = TransactionErrorMetrics::default();
let rent_collector = RentCollector::new(
0,
&EpochSchedule::default(),
EpochSchedule::default(),
500_000.0,
&Rent {
Rent {
lamports_per_byte_year: 42,
..Rent::default()
},
Expand Down
7 changes: 4 additions & 3 deletions runtime/src/accounts_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6231,7 +6231,8 @@ impl AccountsDb {
)
}

pub fn update_accounts_hash_test(&self, slot: Slot, ancestors: &Ancestors) -> (Hash, u64) {
#[cfg(test)]
fn update_accounts_hash_test(&self, slot: Slot, ancestors: &Ancestors) -> (Hash, u64) {
self.update_accounts_hash_with_index_option(
true,
true,
Expand Down Expand Up @@ -8151,9 +8152,9 @@ impl AccountsDb {
let schedule = genesis_config.epoch_schedule;
let rent_collector = RentCollector::new(
schedule.get_epoch(max_slot),
&schedule,
schedule,
genesis_config.slots_per_year(),
&genesis_config.rent,
genesis_config.rent,
);
let accounts_data_len = AtomicU64::new(0);

Expand Down
4 changes: 2 additions & 2 deletions runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3536,9 +3536,9 @@ impl Bank {

self.rent_collector = RentCollector::new(
self.epoch,
self.epoch_schedule(),
*self.epoch_schedule(),
self.slots_per_year,
&genesis_config.rent,
genesis_config.rent,
);

// Add additional builtin programs specified in the genesis config
Expand Down
36 changes: 21 additions & 15 deletions runtime/src/expected_rent_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ impl SlotInfoInEpoch {
impl ExpectedRentCollection {
/// 'account' is being loaded from 'storage_slot' in 'bank_slot'
/// adjusts 'account.rent_epoch' if we skipped the last rewrite on this account
pub fn maybe_update_rent_epoch_on_load(
pub(crate) fn maybe_update_rent_epoch_on_load(
account: &mut AccountSharedData,
storage_slot: &SlotInfoInEpoch,
bank_slot: &SlotInfoInEpoch,
Expand Down Expand Up @@ -302,14 +302,17 @@ impl ExpectedRentCollection {
pubkey: &Pubkey,
rewrites_skipped_this_slot: &Rewrites,
) -> Option<Epoch> {
if let RentResult::CollectRent((next_epoch, rent_due)) =
rent_collector.calculate_rent_result(pubkey, account, None)
let next_epoch = match rent_collector.calculate_rent_result(pubkey, account, None) {
RentResult::LeaveAloneNoRent => return None,
RentResult::CollectRent {
new_rent_epoch,
rent_due: 0,
} => new_rent_epoch,
// Rent is due on this account in this epoch,
// so we did not skip a rewrite.
RentResult::CollectRent { .. } => return None,
};
{
if rent_due != 0 {
// rent is due on this account in this epoch, so we did not skip a rewrite
return None;
}

// grab epoch infno for bank slot and storage slot
let bank_info = bank_slot.get_epoch_info(epoch_schedule);
let (current_epoch, partition_from_current_slot) =
Expand Down Expand Up @@ -533,7 +536,10 @@ impl ExpectedRentCollection {
rent_collector.calculate_rent_result(pubkey, loaded_account, filler_account_suffix);
let current_rent_epoch = loaded_account.rent_epoch();
let new_rent_epoch = match rent_result {
RentResult::CollectRent((next_epoch, rent_due)) => {
RentResult::CollectRent {
new_rent_epoch: next_epoch,
rent_due,
} => {
if next_epoch > current_rent_epoch && rent_due != 0 {
// this is an account that would have had rent collected since this storage slot, so just use the hash we have since there must be a newer version of this account already in a newer slot
// It would be a waste of time to recalcluate a hash.
Expand Down Expand Up @@ -595,9 +601,9 @@ pub mod tests {
let genesis_config = GenesisConfig::default();
let mut rent_collector = RentCollector::new(
epoch,
&epoch_schedule,
epoch_schedule,
genesis_config.slots_per_year(),
&genesis_config.rent,
genesis_config.rent,
);
rent_collector.rent.lamports_per_byte_year = 0; // temporarily disable rent
let find_unskipped_slot = Some;
Expand Down Expand Up @@ -976,9 +982,9 @@ pub mod tests {
let genesis_config = GenesisConfig::default();
let mut rent_collector = RentCollector::new(
epoch,
&epoch_schedule,
epoch_schedule,
genesis_config.slots_per_year(),
&genesis_config.rent,
genesis_config.rent,
);
rent_collector.rent.lamports_per_byte_year = 0; // temporarily disable rent

Expand Down Expand Up @@ -1169,9 +1175,9 @@ pub mod tests {
let genesis_config = GenesisConfig::default();
let mut rent_collector = RentCollector::new(
epoch,
&epoch_schedule,
epoch_schedule,
genesis_config.slots_per_year(),
&genesis_config.rent,
genesis_config.rent,
);
rent_collector.rent.lamports_per_byte_year = 0; // temporarily disable rent

Expand Down
101 changes: 47 additions & 54 deletions runtime/src/rent_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,44 +34,33 @@ impl Default for RentCollector {

/// when rent is collected for this account, this is the action to apply to the account
#[derive(Debug)]
pub enum RentResult {
pub(crate) enum RentResult {
/// maybe collect rent later, leave account alone
LeaveAloneNoRent,
/// collect rent
/// value is (new rent epoch, lamports of rent_due)
CollectRent((Epoch, u64)),
CollectRent {
new_rent_epoch: Epoch,
rent_due: u64, // lamports
},
}

impl RentCollector {
pub fn new(
pub(crate) fn new(
epoch: Epoch,
epoch_schedule: &EpochSchedule,
epoch_schedule: EpochSchedule,
slots_per_year: f64,
rent: &Rent,
rent: Rent,
) -> Self {
Self {
epoch,
epoch_schedule: *epoch_schedule,
epoch_schedule,
slots_per_year,
rent: *rent,
rent,
}
}

pub fn clone_with_epoch(&self, epoch: Epoch) -> Self {
self.clone_with_epoch_and_rate(epoch, self.rent.lamports_per_byte_year)
}

pub fn clone_with_epoch_and_rate(&self, epoch: Epoch, lamports_per_byte_year: u64) -> Self {
let rent = if lamports_per_byte_year != self.rent.lamports_per_byte_year {
Rent {
lamports_per_byte_year,
..self.rent
}
} else {
self.rent
};
pub(crate) fn clone_with_epoch(&self, epoch: Epoch) -> Self {
Self {
rent,
epoch,
..self.clone()
}
Expand All @@ -85,7 +74,7 @@ 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 {
pub(crate) fn get_rent_due(&self, account: &impl ReadableAccount) -> RentDue {
if self
.rent
.is_exempt(account.lamports(), account.data().len())
Expand Down Expand Up @@ -127,36 +116,40 @@ impl RentCollector {
// This is NOT thread safe at some level. If we try to collect from the same account in
// parallel, we may collect twice.
#[must_use = "add to Bank::collected_rent"]
pub fn collect_from_existing_account(
pub(crate) fn collect_from_existing_account(
&self,
address: &Pubkey,
account: &mut AccountSharedData,
filler_account_suffix: Option<&Pubkey>,
) -> CollectedInfo {
match self.calculate_rent_result(address, account, filler_account_suffix) {
RentResult::LeaveAloneNoRent => CollectedInfo::default(),
RentResult::CollectRent((next_epoch, rent_due)) => {
account.set_rent_epoch(next_epoch);

let begin_lamports = account.lamports();
account.saturating_sub_lamports(rent_due);
let end_lamports = account.lamports();
let mut account_data_len_reclaimed = 0;
if end_lamports == 0 {
account_data_len_reclaimed = account.data().len() as u64;
*account = AccountSharedData::default();
RentResult::CollectRent {
new_rent_epoch,
rent_due,
} => match account.lamports().checked_sub(rent_due) {
None | Some(0) => {
let account = std::mem::take(account);
CollectedInfo {
rent_amount: account.lamports(),
account_data_len_reclaimed: account.data().len() as u64,
}
}
CollectedInfo {
rent_amount: begin_lamports - end_lamports,
account_data_len_reclaimed,
Some(lamports) => {
account.set_lamports(lamports);
account.set_rent_epoch(new_rent_epoch);
CollectedInfo {
rent_amount: rent_due,
account_data_len_reclaimed: 0u64,
}
}
}
},
}
}

/// determine what should happen to collect rent from this account
#[must_use]
pub fn calculate_rent_result(
pub(crate) fn calculate_rent_result(
&self,
address: &Pubkey,
account: &impl ReadableAccount,
Expand All @@ -165,25 +158,25 @@ impl RentCollector {
if self.can_skip_rent_collection(address, account, filler_account_suffix) {
return RentResult::LeaveAloneNoRent;
}

let rent_due = self.get_rent_due(account);
if let RentDue::Paying(0) = rent_due {
// maybe collect rent later, leave account alone
return RentResult::LeaveAloneNoRent;
match self.get_rent_due(account) {
// Rent isn't collected for the next epoch.
// Make sure to check exempt status again later in current epoch.
RentDue::Exempt => RentResult::CollectRent {
new_rent_epoch: self.epoch,
rent_due: 0,
},
// Maybe collect rent later, leave account alone.
RentDue::Paying(0) => RentResult::LeaveAloneNoRent,
// Rent is collected for next epoch.
RentDue::Paying(rent_due) => RentResult::CollectRent {
new_rent_epoch: self.epoch + 1,
rent_due,
},
}

let new_rent_epoch = match rent_due {
// Rent isn't collected for the next epoch
// Make sure to check exempt status again later in current epoch
RentDue::Exempt => self.epoch,
// Rent is collected for next epoch
RentDue::Paying(_) => self.epoch + 1,
};
RentResult::CollectRent((new_rent_epoch, rent_due.lamports()))
}

#[must_use = "add to Bank::collected_rent"]
pub fn collect_from_created_account(
pub(crate) fn collect_from_created_account(
&self,
address: &Pubkey,
account: &mut AccountSharedData,
Expand Down