From 28a320d81571f776c439a4090aedcceffaf5fbbb Mon Sep 17 00:00:00 2001 From: Pankaj Garg Date: Wed, 7 Feb 2024 16:38:42 -0800 Subject: [PATCH] SVM: Move `RentCollector` to sdk (#35122) --- accounts-bench/src/main.rs | 4 ++-- accounts-db/src/accounts_db.rs | 2 +- accounts-db/src/accounts_hash.rs | 2 +- accounts-db/src/lib.rs | 1 - accounts-db/src/tiered_storage.rs | 2 +- accounts-db/src/tiered_storage/hot.rs | 6 ++++-- runtime/benches/accounts.rs | 2 +- runtime/src/bank.rs | 2 +- runtime/src/bank/serde_snapshot.rs | 2 +- runtime/src/bank/tests.rs | 2 +- runtime/src/serde_snapshot.rs | 2 +- runtime/src/serde_snapshot/tests.rs | 2 +- runtime/src/snapshot_package.rs | 6 ++++-- sdk/src/lib.rs | 1 + {accounts-db => sdk}/src/rent_collector.rs | 17 ++++++++++++----- svm/src/account_loader.rs | 5 +++-- svm/src/transaction_processor.rs | 2 +- 17 files changed, 36 insertions(+), 24 deletions(-) rename {accounts-db => sdk}/src/rent_collector.rs (97%) diff --git a/accounts-bench/src/main.rs b/accounts-bench/src/main.rs index 88d15ea72482aa..9437485e1e6533 100644 --- a/accounts-bench/src/main.rs +++ b/accounts-bench/src/main.rs @@ -14,11 +14,11 @@ use { }, accounts_index::AccountSecondaryIndexes, ancestors::Ancestors, - rent_collector::RentCollector, }, solana_measure::measure::Measure, solana_sdk::{ - genesis_config::ClusterType, pubkey::Pubkey, sysvar::epoch_schedule::EpochSchedule, + genesis_config::ClusterType, pubkey::Pubkey, rent_collector::RentCollector, + sysvar::epoch_schedule::EpochSchedule, }, std::{env, fs, path::PathBuf, sync::Arc}, }; diff --git a/accounts-db/src/accounts_db.rs b/accounts-db/src/accounts_db.rs index 2853bb7a05edb6..c89cf45e320971 100644 --- a/accounts-db/src/accounts_db.rs +++ b/accounts-db/src/accounts_db.rs @@ -67,7 +67,6 @@ use { partitioned_rewards::{PartitionedEpochRewardsConfig, TestPartitionedEpochRewards}, pubkey_bins::PubkeyBinCalculator24, read_only_accounts_cache::ReadOnlyAccountsCache, - rent_collector::RentCollector, sorted_storages::SortedStorages, storable_accounts::StorableAccounts, u64_align, utils, @@ -92,6 +91,7 @@ use { genesis_config::{ClusterType, GenesisConfig}, hash::Hash, pubkey::Pubkey, + rent_collector::RentCollector, saturating_add_assign, timing::AtomicInterval, transaction::SanitizedTransaction, diff --git a/accounts-db/src/accounts_hash.rs b/accounts-db/src/accounts_hash.rs index 78662a04157744..cb75369d52d182 100644 --- a/accounts-db/src/accounts_hash.rs +++ b/accounts-db/src/accounts_hash.rs @@ -4,7 +4,6 @@ use { active_stats::{ActiveStatItem, ActiveStats}, ancestors::Ancestors, pubkey_bins::PubkeyBinCalculator24, - rent_collector::RentCollector, }, bytemuck::{Pod, Zeroable}, log::*, @@ -14,6 +13,7 @@ use { solana_sdk::{ hash::{Hash, Hasher}, pubkey::Pubkey, + rent_collector::RentCollector, slot_history::Slot, sysvar::epoch_schedule::EpochSchedule, }, diff --git a/accounts-db/src/lib.rs b/accounts-db/src/lib.rs index 1af013aab982f7..fe20e0aab2766c 100644 --- a/accounts-db/src/lib.rs +++ b/accounts-db/src/lib.rs @@ -34,7 +34,6 @@ pub mod nonce_info; pub mod partitioned_rewards; mod pubkey_bins; mod read_only_accounts_cache; -pub mod rent_collector; mod rolling_bit_field; pub mod secondary_index; pub mod shared_buffer_reader; diff --git a/accounts-db/src/tiered_storage.rs b/accounts-db/src/tiered_storage.rs index 92a4f0869e0c2a..f0a23150e2fa70 100644 --- a/accounts-db/src/tiered_storage.rs +++ b/accounts-db/src/tiered_storage.rs @@ -165,12 +165,12 @@ mod tests { hot::HOT_FORMAT, index::IndexOffset, owners::OWNER_NO_OWNER, - solana_accounts_db::rent_collector::RENT_EXEMPT_RENT_EPOCH, solana_sdk::{ account::{Account, AccountSharedData}, clock::Slot, hash::Hash, pubkey::Pubkey, + rent_collector::RENT_EXEMPT_RENT_EPOCH, system_instruction::MAX_PERMITTED_DATA_LENGTH, }, std::{ diff --git a/accounts-db/src/tiered_storage/hot.rs b/accounts-db/src/tiered_storage/hot.rs index 54091313cb9de7..7db9e90d65d353 100644 --- a/accounts-db/src/tiered_storage/hot.rs +++ b/accounts-db/src/tiered_storage/hot.rs @@ -5,7 +5,6 @@ use { account_storage::meta::{StoredAccountInfo, StoredAccountMeta}, accounts_file::MatchAccountOwnerError, accounts_hash::AccountHash, - rent_collector::RENT_EXEMPT_RENT_EPOCH, tiered_storage::{ byte_block, file::TieredStorageFile, @@ -22,7 +21,10 @@ use { bytemuck::{Pod, Zeroable}, memmap2::{Mmap, MmapOptions}, modular_bitfield::prelude::*, - solana_sdk::{account::ReadableAccount, pubkey::Pubkey, stake_history::Epoch}, + solana_sdk::{ + account::ReadableAccount, pubkey::Pubkey, rent_collector::RENT_EXEMPT_RENT_EPOCH, + stake_history::Epoch, + }, std::{borrow::Borrow, fs::OpenOptions, option::Option, path::Path}, }; diff --git a/runtime/benches/accounts.rs b/runtime/benches/accounts.rs index 7efc0a11ac0d75..fb81ce4716553e 100644 --- a/runtime/benches/accounts.rs +++ b/runtime/benches/accounts.rs @@ -16,7 +16,6 @@ use { accounts_index::{AccountSecondaryIndexes, ScanConfig}, ancestors::Ancestors, epoch_accounts_hash::EpochAccountsHash, - rent_collector::RentCollector, }, solana_runtime::bank::*, solana_sdk::{ @@ -25,6 +24,7 @@ use { hash::Hash, lamports::LamportsError, pubkey::Pubkey, + rent_collector::RentCollector, sysvar::epoch_schedule::EpochSchedule, }, std::{ diff --git a/runtime/src/bank.rs b/runtime/src/bank.rs index 8a25cdd31a23be..52546ec4efe703 100644 --- a/runtime/src/bank.rs +++ b/runtime/src/bank.rs @@ -88,7 +88,6 @@ use { epoch_accounts_hash::EpochAccountsHash, nonce_info::{NonceInfo, NoncePartial}, partitioned_rewards::PartitionedEpochRewardsConfig, - rent_collector::{CollectedInfo, RentCollector, RENT_EXEMPT_RENT_EPOCH}, sorted_storages::SortedStorages, stake_rewards::StakeReward, storable_accounts::StorableAccounts, @@ -142,6 +141,7 @@ use { precompiles::get_precompiles, pubkey::Pubkey, rent::RentDue, + rent_collector::{CollectedInfo, RentCollector, RENT_EXEMPT_RENT_EPOCH}, rent_debits::RentDebits, reward_info::RewardInfo, saturating_add_assign, diff --git a/runtime/src/bank/serde_snapshot.rs b/runtime/src/bank/serde_snapshot.rs index 6af86976dde926..8b78efbcf3e11a 100644 --- a/runtime/src/bank/serde_snapshot.rs +++ b/runtime/src/bank/serde_snapshot.rs @@ -605,7 +605,7 @@ mod tests { // This some what long test harness is required to freeze the ABI of // Bank's serialization due to versioned nature - #[frozen_abi(digest = "77zuTwvAGH5Rf28XHUNkRWsrcJ8uMyARMCZZMg9BBu5S")] + #[frozen_abi(digest = "7BH2s2Y1yKy396c3ixC4TTyvvpkyenAvWDSiZvY5yb7P")] #[derive(Serialize, AbiExample)] pub struct BankAbiTestWrapperNewer { #[serde(serialize_with = "wrapper_newer")] diff --git a/runtime/src/bank/tests.rs b/runtime/src/bank/tests.rs index 8c1f35e2d99ac0..19eca1d61ad8a8 100644 --- a/runtime/src/bank/tests.rs +++ b/runtime/src/bank/tests.rs @@ -36,7 +36,6 @@ use { inline_spl_token, nonce_info::NonceFull, partitioned_rewards::TestPartitionedEpochRewards, - rent_collector::RENT_EXEMPT_RENT_EPOCH, transaction_results::DurableNonceFee, }, solana_logger, @@ -88,6 +87,7 @@ use { program::MAX_RETURN_DATA, pubkey::Pubkey, rent::Rent, + rent_collector::RENT_EXEMPT_RENT_EPOCH, reward_type::RewardType, secp256k1_program, signature::{keypair_from_seed, Keypair, Signature, Signer}, diff --git a/runtime/src/serde_snapshot.rs b/runtime/src/serde_snapshot.rs index e38ea904686b40..4b066976d49048 100644 --- a/runtime/src/serde_snapshot.rs +++ b/runtime/src/serde_snapshot.rs @@ -25,7 +25,6 @@ use { accounts_update_notifier_interface::AccountsUpdateNotifier, blockhash_queue::BlockhashQueue, epoch_accounts_hash::EpochAccountsHash, - rent_collector::RentCollector, }, solana_measure::measure::Measure, solana_sdk::{ @@ -38,6 +37,7 @@ use { hash::Hash, inflation::Inflation, pubkey::Pubkey, + rent_collector::RentCollector, }, solana_svm::runtime_config::RuntimeConfig, std::{ diff --git a/runtime/src/serde_snapshot/tests.rs b/runtime/src/serde_snapshot/tests.rs index f9d45b372f5fc4..510069c92662fc 100644 --- a/runtime/src/serde_snapshot/tests.rs +++ b/runtime/src/serde_snapshot/tests.rs @@ -23,7 +23,6 @@ mod serde_snapshot_tests { accounts_hash::AccountsHash, accounts_index::AccountSecondaryIndexes, ancestors::Ancestors, - rent_collector::RentCollector, }, solana_sdk::{ account::{AccountSharedData, ReadableAccount}, @@ -32,6 +31,7 @@ mod serde_snapshot_tests { genesis_config::{ClusterType, GenesisConfig}, hash::Hash, pubkey::Pubkey, + rent_collector::RentCollector, }, std::{ io::{BufReader, Cursor, Read, Write}, diff --git a/runtime/src/snapshot_package.rs b/runtime/src/snapshot_package.rs index 99af3ebbe6ee2a..55a4b13744b4f4 100644 --- a/runtime/src/snapshot_package.rs +++ b/runtime/src/snapshot_package.rs @@ -11,9 +11,11 @@ use { accounts_db::{AccountStorageEntry, AccountsDb}, accounts_hash::{AccountsHash, AccountsHashKind}, epoch_accounts_hash::EpochAccountsHash, - rent_collector::RentCollector, }, - solana_sdk::{clock::Slot, feature_set, sysvar::epoch_schedule::EpochSchedule}, + solana_sdk::{ + clock::Slot, feature_set, rent_collector::RentCollector, + sysvar::epoch_schedule::EpochSchedule, + }, std::{ path::{Path, PathBuf}, sync::Arc, diff --git a/sdk/src/lib.rs b/sdk/src/lib.rs index 98576e4f36bfb6..233154ce72bcae 100644 --- a/sdk/src/lib.rs +++ b/sdk/src/lib.rs @@ -92,6 +92,7 @@ pub mod program_utils; pub mod pubkey; pub mod quic; pub mod recent_blockhashes_account; +pub mod rent_collector; pub mod rent_debits; pub mod reward_info; pub mod reward_type; diff --git a/accounts-db/src/rent_collector.rs b/sdk/src/rent_collector.rs similarity index 97% rename from accounts-db/src/rent_collector.rs rename to sdk/src/rent_collector.rs index 0bdb03291e8c5f..1de6ce19950dbd 100644 --- a/accounts-db/src/rent_collector.rs +++ b/sdk/src/rent_collector.rs @@ -1,3 +1,5 @@ +#![cfg(feature = "full")] + //! calculate and collect rent from Accounts use solana_sdk::{ account::{AccountSharedData, ReadableAccount, WritableAccount}, @@ -87,7 +89,10 @@ impl RentCollector { } else { let account_rent_epoch = account.rent_epoch(); let slots_elapsed: u64 = (account_rent_epoch..=self.epoch) - .map(|epoch| self.epoch_schedule.get_slots_in_epoch(epoch + 1)) + .map(|epoch| { + self.epoch_schedule + .get_slots_in_epoch(epoch.saturating_add(1)) + }) .sum(); // avoid infinite rent in rust 1.45 @@ -165,7 +170,7 @@ impl RentCollector { RentDue::Paying(0) => RentResult::NoRentCollectionNow, // Rent is collected for next epoch. RentDue::Paying(rent_due) => RentResult::CollectRent { - new_rent_epoch: self.epoch + 1, + new_rent_epoch: self.epoch.saturating_add(1), rent_due, }, } @@ -185,14 +190,16 @@ impl std::ops::Add for CollectedInfo { type Output = Self; fn add(self, other: Self) -> Self { Self { - rent_amount: self.rent_amount + other.rent_amount, - account_data_len_reclaimed: self.account_data_len_reclaimed - + other.account_data_len_reclaimed, + rent_amount: self.rent_amount.saturating_add(other.rent_amount), + account_data_len_reclaimed: self + .account_data_len_reclaimed + .saturating_add(other.account_data_len_reclaimed), } } } impl std::ops::AddAssign for CollectedInfo { + #![allow(clippy::arithmetic_side_effects)] fn add_assign(&mut self, other: Self) { *self = *self + other; } diff --git a/svm/src/account_loader.rs b/svm/src/account_loader.rs index fd0b975df74764..947d77dd2ac150 100644 --- a/svm/src/account_loader.rs +++ b/svm/src/account_loader.rs @@ -9,7 +9,6 @@ use { solana_accounts_db::{ accounts::{LoadedTransaction, TransactionLoadResult, TransactionRent}, nonce_info::NonceFull, - rent_collector::{RentCollector, RENT_EXEMPT_RENT_EPOCH}, transaction_results::TransactionCheckResult, }, solana_program_runtime::{ @@ -28,6 +27,7 @@ use { nonce::State as NonceState, pubkey::Pubkey, rent::RentDue, + rent_collector::{RentCollector, RENT_EXEMPT_RENT_EPOCH}, rent_debits::RentDebits, saturating_add_assign, sysvar::{self, instructions::construct_instructions_data}, @@ -453,7 +453,7 @@ mod tests { nonce::state::Versions as NonceVersions, solana_accounts_db::{ accounts::Accounts, accounts_db::AccountsDb, accounts_file::MatchAccountOwnerError, - ancestors::Ancestors, rent_collector::RentCollector, + ancestors::Ancestors, }, solana_program_runtime::{ compute_budget_processor, @@ -470,6 +470,7 @@ mod tests { message::{Message, SanitizedMessage}, nonce, rent::Rent, + rent_collector::RentCollector, signature::{Keypair, Signer}, system_program, sysvar, transaction::{Result, Transaction, TransactionError}, diff --git a/svm/src/transaction_processor.rs b/svm/src/transaction_processor.rs index 837dc5e7fd4ce8..be874dc9f9128f 100644 --- a/svm/src/transaction_processor.rs +++ b/svm/src/transaction_processor.rs @@ -9,7 +9,6 @@ use { solana_accounts_db::{ accounts::{LoadedTransaction, TransactionLoadResult}, accounts_file::MatchAccountOwnerError, - rent_collector::RentCollector, transaction_results::{ DurableNonceFee, TransactionCheckResult, TransactionExecutionDetails, TransactionExecutionResult, @@ -43,6 +42,7 @@ use { message::SanitizedMessage, native_loader, pubkey::Pubkey, + rent_collector::RentCollector, saturating_add_assign, transaction::{self, SanitizedTransaction, TransactionError}, transaction_context::{ExecutionRecord, TransactionContext},