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

test_rent_exempt_temporal_escape works in passes #29460

Merged
merged 1 commit into from
Jan 2, 2023
Merged
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
80 changes: 43 additions & 37 deletions runtime/src/rent_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,43 +488,49 @@ mod tests {

#[test]
fn test_rent_exempt_temporal_escape() {
let mut account = AccountSharedData::default();
let epoch = 3;
let huge_lamports = 123_456_789_012;
let tiny_lamports = 789_012;
let pubkey = solana_sdk::pubkey::new_rand();

account.set_lamports(huge_lamports);
assert_eq!(account.rent_epoch(), 0);

// create a tested rent collector
let rent_collector = default_rent_collector_clone_with_epoch(epoch);

// this test fails with set_exempt_rent_epoch_max = true atm
let set_exempt_rent_epoch_max = false;

// first mark account as being collected while being rent-exempt
let collected = rent_collector.collect_from_existing_account(
&pubkey,
&mut account,
None, // filler_account_suffix
set_exempt_rent_epoch_max,
);
assert_eq!(account.lamports(), huge_lamports);
assert_eq!(collected, CollectedInfo::default());

// decrease the balance not to be rent-exempt
account.set_lamports(tiny_lamports);

// ... and trigger another rent collection on the same epoch and check that rent is working
let collected = rent_collector.collect_from_existing_account(
&pubkey,
&mut account,
None, // filler_account_suffix
set_exempt_rent_epoch_max,
);
assert_eq!(account.lamports(), tiny_lamports - collected.rent_amount);
assert_ne!(collected, CollectedInfo::default());
for set_exempt_rent_epoch_max in [false, true] {
for pass in 0..2 {
let mut account = AccountSharedData::default();
let epoch = 3;
let huge_lamports = 123_456_789_012;
let tiny_lamports = 789_012;
let pubkey = solana_sdk::pubkey::new_rand();

assert_eq!(account.rent_epoch(), 0);

// create a tested rent collector
let rent_collector = default_rent_collector_clone_with_epoch(epoch);

if pass == 0 {
account.set_lamports(huge_lamports);
// first mark account as being collected while being rent-exempt
let collected = rent_collector.collect_from_existing_account(
&pubkey,
&mut account,
None, // filler_account_suffix
set_exempt_rent_epoch_max,
);
assert_eq!(account.lamports(), huge_lamports);
assert_eq!(collected, CollectedInfo::default());
continue;
}

// decrease the balance not to be rent-exempt
// In a real validator, it is not legal to reduce an account's lamports such that the account becomes rent paying.
// So, pass == 0 above tests the case of rent that is exempt. pass == 1 tests the case where we are rent paying.
account.set_lamports(tiny_lamports);

// ... and trigger another rent collection on the same epoch and check that rent is working
let collected = rent_collector.collect_from_existing_account(
&pubkey,
&mut account,
None, // filler_account_suffix
set_exempt_rent_epoch_max,
);
assert_eq!(account.lamports(), tiny_lamports - collected.rent_amount);
assert_ne!(collected, CollectedInfo::default());
}
}
}

#[test]
Expand Down