Skip to content

Commit

Permalink
Code cleanup in account_rent_state (#34941)
Browse files Browse the repository at this point in the history
  • Loading branch information
pgarg66 authored Jan 25, 2024
1 parent 5b59930 commit b04765f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 64 deletions.
109 changes: 55 additions & 54 deletions runtime/src/accounts/account_rent_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,66 +56,67 @@ impl RentState {
}
}
}
}

pub(super) fn submit_rent_state_metrics(pre_rent_state: &RentState, post_rent_state: &RentState) {
match (pre_rent_state, post_rent_state) {
(&RentState::Uninitialized, &RentState::RentPaying { .. }) => {
inc_new_counter_info!("rent_paying_err-new_account", 1);
}
(&RentState::RentPaying { .. }, &RentState::RentPaying { .. }) => {
inc_new_counter_info!("rent_paying_ok-legacy", 1);
}
(_, &RentState::RentPaying { .. }) => {
inc_new_counter_info!("rent_paying_err-other", 1);
fn submit_rent_state_metrics(pre_rent_state: &Self, post_rent_state: &Self) {
match (pre_rent_state, post_rent_state) {
(&RentState::Uninitialized, &RentState::RentPaying { .. }) => {
inc_new_counter_info!("rent_paying_err-new_account", 1);
}
(&RentState::RentPaying { .. }, &RentState::RentPaying { .. }) => {
inc_new_counter_info!("rent_paying_ok-legacy", 1);
}
(_, &RentState::RentPaying { .. }) => {
inc_new_counter_info!("rent_paying_err-other", 1);
}
_ => {}
}
_ => {}
}
}

pub(crate) fn check_rent_state(
pre_rent_state: Option<&RentState>,
post_rent_state: Option<&RentState>,
transaction_context: &TransactionContext,
index: IndexOfAccount,
) -> Result<()> {
if let Some((pre_rent_state, post_rent_state)) = pre_rent_state.zip(post_rent_state) {
let expect_msg = "account must exist at TransactionContext index if rent-states are Some";
check_rent_state_with_account(
pre_rent_state,
post_rent_state,
transaction_context
.get_key_of_account_at_index(index)
.expect(expect_msg),
&transaction_context
.get_account_at_index(index)
.expect(expect_msg)
.borrow(),
index,
)?;
pub(crate) fn check_rent_state(
pre_rent_state: Option<&Self>,
post_rent_state: Option<&Self>,
transaction_context: &TransactionContext,
index: IndexOfAccount,
) -> Result<()> {
if let Some((pre_rent_state, post_rent_state)) = pre_rent_state.zip(post_rent_state) {
let expect_msg =
"account must exist at TransactionContext index if rent-states are Some";
Self::check_rent_state_with_account(
pre_rent_state,
post_rent_state,
transaction_context
.get_key_of_account_at_index(index)
.expect(expect_msg),
&transaction_context
.get_account_at_index(index)
.expect(expect_msg)
.borrow(),
index,
)?;
}
Ok(())
}
Ok(())
}

pub(super) fn check_rent_state_with_account(
pre_rent_state: &RentState,
post_rent_state: &RentState,
address: &Pubkey,
account_state: &AccountSharedData,
account_index: IndexOfAccount,
) -> Result<()> {
submit_rent_state_metrics(pre_rent_state, post_rent_state);
if !solana_sdk::incinerator::check_id(address)
&& !post_rent_state.transition_allowed_from(pre_rent_state)
{
debug!(
"Account {} not rent exempt, state {:?}",
address, account_state,
);
let account_index = account_index as u8;
Err(TransactionError::InsufficientFundsForRent { account_index })
} else {
Ok(())
pub(super) fn check_rent_state_with_account(
pre_rent_state: &Self,
post_rent_state: &Self,
address: &Pubkey,
account_state: &AccountSharedData,
account_index: IndexOfAccount,
) -> Result<()> {
Self::submit_rent_state_metrics(pre_rent_state, post_rent_state);
if !solana_sdk::incinerator::check_id(address)
&& !post_rent_state.transition_allowed_from(pre_rent_state)
{
debug!(
"Account {} not rent exempt, state {:?}",
address, account_state,
);
let account_index = account_index as u8;
Err(TransactionError::InsufficientFundsForRent { account_index })
} else {
Ok(())
}
}
}

Expand Down
7 changes: 2 additions & 5 deletions runtime/src/accounts/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
pub mod account_rent_state;

use {
crate::{
accounts::account_rent_state::{check_rent_state_with_account, RentState},
bank::RewardInterval,
},
crate::{accounts::account_rent_state::RentState, bank::RewardInterval},
itertools::Itertools,
log::warn,
solana_accounts_db::{
Expand Down Expand Up @@ -476,7 +473,7 @@ pub fn validate_fee_payer(
.map_err(|_| TransactionError::InsufficientFundsForFee)?;

let payer_post_rent_state = RentState::from_account(payer_account, &rent_collector.rent);
check_rent_state_with_account(
RentState::check_rent_state_with_account(
&payer_pre_rent_state,
&payer_post_rent_state,
payer_address,
Expand Down
7 changes: 2 additions & 5 deletions runtime/src/bank/transaction_account_state_info.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use {
crate::{
accounts::account_rent_state::{check_rent_state, RentState},
bank::Bank,
},
crate::{accounts::account_rent_state::RentState, bank::Bank},
solana_sdk::{
account::ReadableAccount,
message::SanitizedMessage,
Expand Down Expand Up @@ -63,7 +60,7 @@ impl Bank {
for (i, (pre_state_info, post_state_info)) in
pre_state_infos.iter().zip(post_state_infos).enumerate()
{
check_rent_state(
RentState::check_rent_state(
pre_state_info.rent_state.as_ref(),
post_state_info.rent_state.as_ref(),
transaction_context,
Expand Down

0 comments on commit b04765f

Please sign in to comment.