Skip to content

Commit

Permalink
add tests for collect_from_existing_account
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffwashington committed Dec 9, 2022
1 parent 1269ba6 commit cf5b77d
Showing 1 changed file with 93 additions and 1 deletion.
94 changes: 93 additions & 1 deletion runtime/src/rent_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,25 +251,62 @@ mod tests {
rent_collector.calculate_rent_result(&Pubkey::default(), &account, None),
RentResult::NoRentCollectionNow,
));
{
let mut account_clone = account.clone();
assert_eq!(
rent_collector.collect_from_existing_account(
&Pubkey::default(),
&mut account_clone,
None
),
CollectedInfo::default()
);
assert_eq!(account_clone, account);
}

account.set_executable(true);
assert!(matches!(
rent_collector.calculate_rent_result(&Pubkey::default(), &account, None),
RentResult::Exempt
));
{
let mut account_clone = account.clone();
assert_eq!(
rent_collector.collect_from_existing_account(
&Pubkey::default(),
&mut account_clone,
None
),
CollectedInfo::default()
);
assert_eq!(account_clone, account);
}

account.set_executable(false);
assert!(matches!(
rent_collector.calculate_rent_result(&incinerator::id(), &account, None),
RentResult::Exempt
));
{
let mut account_clone = account.clone();
assert_eq!(
rent_collector.collect_from_existing_account(
&incinerator::id(),
&mut account_clone,
None
),
CollectedInfo::default()
);
assert_eq!(account_clone, account);
}

// try a few combinations of rent collector rent epoch and collecting rent with and without filler accounts specified (but we aren't a filler)
let filler_account = solana_sdk::pubkey::new_rand();

for filler_accounts in [None, Some(&filler_account)] {
for (rent_epoch, rent_due_expected) in [(2, 2), (3, 5)] {
rent_collector.epoch = rent_epoch;
account.set_lamports(10);
account.set_rent_epoch(1);
let new_rent_epoch_expected = rent_collector.epoch + 1;
assert!(
Expand All @@ -280,6 +317,25 @@ mod tests {
"{:?}",
rent_collector.calculate_rent_result(&Pubkey::default(), &account, None)
);

{
let mut account_clone = account.clone();
assert_eq!(
rent_collector.collect_from_existing_account(
&Pubkey::default(),
&mut account_clone,
filler_accounts
),
CollectedInfo {
rent_amount: rent_due_expected,
account_data_len_reclaimed: 0
}
);
let mut account_expected = account.clone();
account_expected.set_lamports(account.lamports() - rent_due_expected);
account_expected.set_rent_epoch(new_rent_epoch_expected);
assert_eq!(account_clone, account_expected);
}
}
}

Expand All @@ -289,6 +345,18 @@ mod tests {
rent_collector.calculate_rent_result(&Pubkey::default(), &account, None),
RentResult::Exempt,
));
{
let mut account_clone = account.clone();
assert_eq!(
rent_collector.collect_from_existing_account(
&Pubkey::default(),
&mut account_clone,
None
),
CollectedInfo::default()
);
assert_eq!(account_clone, account);
}

// enough lamports to make us exempt
// but, our rent_epoch is set in the future, so we can't know if we are exempt yet or not.
Expand All @@ -298,14 +366,38 @@ mod tests {
rent_collector.calculate_rent_result(&Pubkey::default(), &account, None),
RentResult::NoRentCollectionNow,
));
{
let mut account_clone = account.clone();
assert_eq!(
rent_collector.collect_from_existing_account(
&Pubkey::default(),
&mut account_clone,
None
),
CollectedInfo::default()
);
assert_eq!(account_clone, account);
}

// filler accounts are exempt
account.set_rent_epoch(1);
account.set_lamports(1);
account.set_lamports(10);
assert!(matches!(
rent_collector.calculate_rent_result(&filler_account, &account, Some(&filler_account)),
RentResult::Exempt,
));
{
let mut account_clone = account.clone();
assert_eq!(
rent_collector.collect_from_existing_account(
&filler_account,
&mut account_clone,
Some(&filler_account)
),
CollectedInfo::default()
);
assert_eq!(account_clone, account);
}
}

#[test]
Expand Down

0 comments on commit cf5b77d

Please sign in to comment.