plumb some rent (#5610)
* plumb some rent * nits * fixups * fixups * fixups
This commit is contained in:
@ -1,11 +1,9 @@
|
||||
use crate::accounts_db::{
|
||||
AccountInfo, AccountStorage, AccountsDB, AppendVecId, ErrorCounters, InstructionAccounts,
|
||||
InstructionCredits, InstructionLoaders,
|
||||
};
|
||||
use crate::accounts_db::{AccountInfo, AccountStorage, AccountsDB, AppendVecId, ErrorCounters};
|
||||
use crate::accounts_index::{AccountsIndex, Fork};
|
||||
use crate::append_vec::StoredAccount;
|
||||
use crate::blockhash_queue::BlockhashQueue;
|
||||
use crate::message_processor::has_duplicates;
|
||||
use crate::rent_collector::RentCollector;
|
||||
use bincode::serialize;
|
||||
use log::*;
|
||||
use rayon::slice::ParallelSliceMut;
|
||||
@ -46,6 +44,12 @@ pub struct Accounts {
|
||||
credit_only_account_locks: Arc<RwLock<Option<HashMap<Pubkey, CreditOnlyLock>>>>,
|
||||
}
|
||||
|
||||
// for the load instructions
|
||||
pub type TransactionAccounts = Vec<Account>;
|
||||
pub type TransactionCredits = Vec<u64>;
|
||||
pub type TransactionRents = Vec<u64>;
|
||||
pub type TransactionLoaders = Vec<Vec<(Pubkey, Account)>>;
|
||||
|
||||
impl Accounts {
|
||||
pub fn new(paths: Option<String>) -> Self {
|
||||
let accounts_db = Arc::new(AccountsDB::new(paths));
|
||||
@ -82,7 +86,8 @@ impl Accounts {
|
||||
tx: &Transaction,
|
||||
fee: u64,
|
||||
error_counters: &mut ErrorCounters,
|
||||
) -> Result<(Vec<Account>, InstructionCredits)> {
|
||||
rent_collector: &RentCollector,
|
||||
) -> Result<(TransactionAccounts, TransactionCredits, TransactionRents)> {
|
||||
// Copy all the accounts
|
||||
let message = tx.message();
|
||||
if tx.signatures.is_empty() && fee != 0 {
|
||||
@ -96,30 +101,35 @@ impl Accounts {
|
||||
|
||||
// There is no way to predict what program will execute without an error
|
||||
// If a fee can pay for execution then the program will be scheduled
|
||||
let mut called_accounts: Vec<Account> = vec![];
|
||||
let mut credits: InstructionCredits = vec![];
|
||||
for key in message.account_keys.iter() {
|
||||
if !message.program_ids().contains(&key) {
|
||||
called_accounts.push(
|
||||
AccountsDB::load(storage, ancestors, accounts_index, key)
|
||||
.map(|(account, _)| account)
|
||||
.unwrap_or_default(),
|
||||
);
|
||||
credits.push(0);
|
||||
}
|
||||
let mut accounts: TransactionAccounts = vec![];
|
||||
let mut credits: TransactionCredits = vec![];
|
||||
let mut rents: TransactionRents = vec![];
|
||||
for key in message
|
||||
.account_keys
|
||||
.iter()
|
||||
.filter(|key| !message.program_ids().contains(&key))
|
||||
{
|
||||
let (account, rent) = AccountsDB::load(storage, ancestors, accounts_index, key)
|
||||
.and_then(|(account, _)| rent_collector.update(account))
|
||||
.unwrap_or_default();
|
||||
|
||||
accounts.push(account);
|
||||
credits.push(0);
|
||||
rents.push(rent);
|
||||
}
|
||||
if called_accounts.is_empty() || called_accounts[0].lamports == 0 {
|
||||
|
||||
if accounts.is_empty() || accounts[0].lamports == 0 {
|
||||
error_counters.account_not_found += 1;
|
||||
Err(TransactionError::AccountNotFound)
|
||||
} else if called_accounts[0].owner != system_program::id() {
|
||||
} else if accounts[0].owner != system_program::id() {
|
||||
error_counters.invalid_account_for_fee += 1;
|
||||
Err(TransactionError::InvalidAccountForFee)
|
||||
} else if called_accounts[0].lamports < fee {
|
||||
} else if accounts[0].lamports < fee {
|
||||
error_counters.insufficient_funds += 1;
|
||||
Err(TransactionError::InsufficientFundsForFee)
|
||||
} else {
|
||||
called_accounts[0].lamports -= fee;
|
||||
Ok((called_accounts, credits))
|
||||
accounts[0].lamports -= fee;
|
||||
Ok((accounts, credits, rents))
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -174,7 +184,7 @@ impl Accounts {
|
||||
accounts_index: &AccountsIndex<AccountInfo>,
|
||||
tx: &Transaction,
|
||||
error_counters: &mut ErrorCounters,
|
||||
) -> Result<Vec<Vec<(Pubkey, Account)>>> {
|
||||
) -> Result<TransactionLoaders> {
|
||||
let message = tx.message();
|
||||
message
|
||||
.instructions
|
||||
@ -203,7 +213,15 @@ impl Accounts {
|
||||
lock_results: Vec<Result<()>>,
|
||||
hash_queue: &BlockhashQueue,
|
||||
error_counters: &mut ErrorCounters,
|
||||
) -> Vec<Result<(InstructionAccounts, InstructionLoaders, InstructionCredits)>> {
|
||||
rent_collector: &RentCollector,
|
||||
) -> Vec<
|
||||
Result<(
|
||||
TransactionAccounts,
|
||||
TransactionLoaders,
|
||||
TransactionCredits,
|
||||
TransactionRents,
|
||||
)>,
|
||||
> {
|
||||
//PERF: hold the lock to scan for the references, but not to clone the accounts
|
||||
//TODO: two locks usually leads to deadlocks, should this be one structure?
|
||||
let accounts_index = self.accounts_db.accounts_index.read().unwrap();
|
||||
@ -217,13 +235,14 @@ impl Accounts {
|
||||
.ok_or(TransactionError::BlockhashNotFound)?;
|
||||
|
||||
let fee = fee_calculator.calculate_fee(tx.message());
|
||||
let (accounts, credits) = Self::load_tx_accounts(
|
||||
let (accounts, credits, rents) = Self::load_tx_accounts(
|
||||
&storage,
|
||||
ancestors,
|
||||
&accounts_index,
|
||||
tx,
|
||||
fee,
|
||||
error_counters,
|
||||
rent_collector,
|
||||
)?;
|
||||
let loaders = Self::load_loaders(
|
||||
&storage,
|
||||
@ -232,7 +251,7 @@ impl Accounts {
|
||||
tx,
|
||||
error_counters,
|
||||
)?;
|
||||
Ok((accounts, loaders, credits))
|
||||
Ok((accounts, loaders, credits, rents))
|
||||
}
|
||||
(_, Err(e)) => Err(e),
|
||||
})
|
||||
@ -503,7 +522,12 @@ impl Accounts {
|
||||
fork: Fork,
|
||||
txs: &[Transaction],
|
||||
res: &[Result<()>],
|
||||
loaded: &mut [Result<(InstructionAccounts, InstructionLoaders, InstructionCredits)>],
|
||||
loaded: &mut [Result<(
|
||||
TransactionAccounts,
|
||||
TransactionLoaders,
|
||||
TransactionCredits,
|
||||
TransactionRents,
|
||||
)>],
|
||||
) {
|
||||
let accounts_to_store = self.collect_accounts_to_store(txs, res, loaded);
|
||||
self.accounts_db.store(fork, &accounts_to_store);
|
||||
@ -578,7 +602,12 @@ impl Accounts {
|
||||
&self,
|
||||
txs: &'a [Transaction],
|
||||
res: &'a [Result<()>],
|
||||
loaded: &'a mut [Result<(InstructionAccounts, InstructionLoaders, InstructionCredits)>],
|
||||
loaded: &'a mut [Result<(
|
||||
TransactionAccounts,
|
||||
TransactionLoaders,
|
||||
TransactionCredits,
|
||||
TransactionRents,
|
||||
)>],
|
||||
) -> Vec<(&'a Pubkey, &'a Account)> {
|
||||
let mut accounts = Vec::new();
|
||||
for (i, raccs) in loaded.iter_mut().enumerate() {
|
||||
@ -651,7 +680,14 @@ mod tests {
|
||||
ka: &Vec<(Pubkey, Account)>,
|
||||
fee_calculator: &FeeCalculator,
|
||||
error_counters: &mut ErrorCounters,
|
||||
) -> Vec<Result<(InstructionAccounts, InstructionLoaders, InstructionCredits)>> {
|
||||
) -> Vec<
|
||||
Result<(
|
||||
TransactionAccounts,
|
||||
TransactionLoaders,
|
||||
TransactionCredits,
|
||||
TransactionRents,
|
||||
)>,
|
||||
> {
|
||||
let mut hash_queue = BlockhashQueue::new(100);
|
||||
hash_queue.register_hash(&tx.message().recent_blockhash, &fee_calculator);
|
||||
let accounts = Accounts::new(None);
|
||||
@ -660,8 +696,15 @@ mod tests {
|
||||
}
|
||||
|
||||
let ancestors = vec![(0, 0)].into_iter().collect();
|
||||
let res =
|
||||
accounts.load_accounts(&ancestors, &[tx], vec![Ok(())], &hash_queue, error_counters);
|
||||
let rent_collector = RentCollector::default();
|
||||
let res = accounts.load_accounts(
|
||||
&ancestors,
|
||||
&[tx],
|
||||
vec![Ok(())],
|
||||
&hash_queue,
|
||||
error_counters,
|
||||
&rent_collector,
|
||||
);
|
||||
res
|
||||
}
|
||||
|
||||
@ -669,7 +712,14 @@ mod tests {
|
||||
tx: Transaction,
|
||||
ka: &Vec<(Pubkey, Account)>,
|
||||
error_counters: &mut ErrorCounters,
|
||||
) -> Vec<Result<(InstructionAccounts, InstructionLoaders, InstructionCredits)>> {
|
||||
) -> Vec<
|
||||
Result<(
|
||||
TransactionAccounts,
|
||||
TransactionLoaders,
|
||||
TransactionCredits,
|
||||
TransactionRents,
|
||||
)>,
|
||||
> {
|
||||
let fee_calculator = FeeCalculator::default();
|
||||
load_accounts_with_fee(tx, ka, &fee_calculator, error_counters)
|
||||
}
|
||||
@ -825,10 +875,12 @@ mod tests {
|
||||
let key0 = keypair.pubkey();
|
||||
let key1 = Pubkey::new(&[5u8; 32]);
|
||||
|
||||
let account = Account::new(1, 1, &Pubkey::default());
|
||||
let mut account = Account::new(1, 1, &Pubkey::default());
|
||||
account.rent_epoch = 1;
|
||||
accounts.push((key0, account));
|
||||
|
||||
let account = Account::new(2, 1, &Pubkey::default());
|
||||
let mut account = Account::new(2, 1, &Pubkey::default());
|
||||
account.rent_epoch = 1;
|
||||
accounts.push((key1, account));
|
||||
|
||||
let instructions = vec![CompiledInstruction::new(2, &(), vec![0, 1])];
|
||||
@ -845,13 +897,18 @@ mod tests {
|
||||
assert_eq!(error_counters.account_not_found, 0);
|
||||
assert_eq!(loaded_accounts.len(), 1);
|
||||
match &loaded_accounts[0] {
|
||||
Ok((instruction_accounts, instruction_loaders, instruction_credits)) => {
|
||||
assert_eq!(instruction_accounts.len(), 2);
|
||||
assert_eq!(instruction_accounts[0], accounts[0].1);
|
||||
assert_eq!(instruction_loaders.len(), 1);
|
||||
assert_eq!(instruction_loaders[0].len(), 0);
|
||||
assert_eq!(instruction_credits.len(), 2);
|
||||
assert_eq!(instruction_credits, &vec![0, 0]);
|
||||
Ok((
|
||||
transaction_accounts,
|
||||
transaction_loaders,
|
||||
transaction_credits,
|
||||
_transaction_rents,
|
||||
)) => {
|
||||
assert_eq!(transaction_accounts.len(), 2);
|
||||
assert_eq!(transaction_accounts[0], accounts[0].1);
|
||||
assert_eq!(transaction_loaders.len(), 1);
|
||||
assert_eq!(transaction_loaders[0].len(), 0);
|
||||
assert_eq!(transaction_credits.len(), 2);
|
||||
assert_eq!(transaction_credits, &vec![0, 0]);
|
||||
}
|
||||
Err(e) => Err(e).unwrap(),
|
||||
}
|
||||
@ -996,21 +1053,25 @@ mod tests {
|
||||
let key2 = Pubkey::new(&[6u8; 32]);
|
||||
let key3 = Pubkey::new(&[7u8; 32]);
|
||||
|
||||
let account = Account::new(1, 1, &Pubkey::default());
|
||||
let mut account = Account::new(1, 1, &Pubkey::default());
|
||||
account.rent_epoch = 1;
|
||||
accounts.push((key0, account));
|
||||
|
||||
let mut account = Account::new(40, 1, &Pubkey::default());
|
||||
account.executable = true;
|
||||
account.rent_epoch = 1;
|
||||
account.owner = native_loader::id();
|
||||
accounts.push((key1, account));
|
||||
|
||||
let mut account = Account::new(41, 1, &Pubkey::default());
|
||||
account.executable = true;
|
||||
account.rent_epoch = 1;
|
||||
account.owner = key1;
|
||||
accounts.push((key2, account));
|
||||
|
||||
let mut account = Account::new(42, 1, &Pubkey::default());
|
||||
account.executable = true;
|
||||
account.rent_epoch = 1;
|
||||
account.owner = key2;
|
||||
accounts.push((key3, account));
|
||||
|
||||
@ -1031,15 +1092,20 @@ mod tests {
|
||||
assert_eq!(error_counters.account_not_found, 0);
|
||||
assert_eq!(loaded_accounts.len(), 1);
|
||||
match &loaded_accounts[0] {
|
||||
Ok((instruction_accounts, instruction_loaders, instruction_credits)) => {
|
||||
assert_eq!(instruction_accounts.len(), 1);
|
||||
assert_eq!(instruction_accounts[0], accounts[0].1);
|
||||
assert_eq!(instruction_loaders.len(), 2);
|
||||
assert_eq!(instruction_loaders[0].len(), 1);
|
||||
assert_eq!(instruction_loaders[1].len(), 2);
|
||||
assert_eq!(instruction_credits.len(), 1);
|
||||
assert_eq!(instruction_credits, &vec![0]);
|
||||
for loaders in instruction_loaders.iter() {
|
||||
Ok((
|
||||
transaction_accounts,
|
||||
transaction_loaders,
|
||||
transaction_credits,
|
||||
_transaction_rents,
|
||||
)) => {
|
||||
assert_eq!(transaction_accounts.len(), 1);
|
||||
assert_eq!(transaction_accounts[0], accounts[0].1);
|
||||
assert_eq!(transaction_loaders.len(), 2);
|
||||
assert_eq!(transaction_loaders[0].len(), 1);
|
||||
assert_eq!(transaction_loaders[1].len(), 2);
|
||||
assert_eq!(transaction_credits.len(), 1);
|
||||
assert_eq!(transaction_credits, &vec![0]);
|
||||
for loaders in transaction_loaders.iter() {
|
||||
for (i, accounts_subset) in loaders.iter().enumerate() {
|
||||
// +1 to skip first not loader account
|
||||
assert_eq![accounts_subset.1, accounts[i + 1].1];
|
||||
@ -1475,22 +1541,26 @@ mod tests {
|
||||
let account1 = Account::new(2, 0, &Pubkey::default());
|
||||
let account2 = Account::new(3, 0, &Pubkey::default());
|
||||
|
||||
let instruction_accounts0 = vec![account0, account2.clone()];
|
||||
let instruction_loaders0 = vec![];
|
||||
let instruction_credits0 = vec![0, 2];
|
||||
let transaction_accounts0 = vec![account0, account2.clone()];
|
||||
let transaction_loaders0 = vec![];
|
||||
let transaction_credits0 = vec![0, 2];
|
||||
let transaction_rents0 = vec![0, 0];
|
||||
let loaded0 = Ok((
|
||||
instruction_accounts0,
|
||||
instruction_loaders0,
|
||||
instruction_credits0,
|
||||
transaction_accounts0,
|
||||
transaction_loaders0,
|
||||
transaction_credits0,
|
||||
transaction_rents0,
|
||||
));
|
||||
|
||||
let instruction_accounts1 = vec![account1, account2.clone()];
|
||||
let instruction_loaders1 = vec![];
|
||||
let instruction_credits1 = vec![0, 3];
|
||||
let transaction_accounts1 = vec![account1, account2.clone()];
|
||||
let transaction_loaders1 = vec![];
|
||||
let transaction_credits1 = vec![0, 3];
|
||||
let transaction_rents1 = vec![0, 0];
|
||||
let loaded1 = Ok((
|
||||
instruction_accounts1,
|
||||
instruction_loaders1,
|
||||
instruction_credits1,
|
||||
transaction_accounts1,
|
||||
transaction_loaders1,
|
||||
transaction_credits1,
|
||||
transaction_rents1,
|
||||
));
|
||||
|
||||
let mut loaded = vec![loaded0, loaded1];
|
||||
|
@ -30,7 +30,7 @@ use serde::de::{MapAccess, Visitor};
|
||||
use serde::ser::{SerializeMap, Serializer};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use solana_measure::measure::Measure;
|
||||
use solana_sdk::account::{Account, LamportCredit};
|
||||
use solana_sdk::account::Account;
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::fmt;
|
||||
@ -76,9 +76,6 @@ pub struct AccountInfo {
|
||||
}
|
||||
/// An offset into the AccountsDB::storage vector
|
||||
pub type AppendVecId = usize;
|
||||
pub type InstructionAccounts = Vec<Account>;
|
||||
pub type InstructionCredits = Vec<LamportCredit>;
|
||||
pub type InstructionLoaders = Vec<Vec<(Pubkey, Account)>>;
|
||||
|
||||
// Each fork has a set of storage entries.
|
||||
type ForkStores = HashMap<usize, Arc<AccountStorageEntry>>;
|
||||
|
@ -1,8 +1,7 @@
|
||||
use bincode::{deserialize_from, serialize_into, serialized_size};
|
||||
use memmap::MmapMut;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use solana_sdk::account::Account;
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
use solana_sdk::{account::Account, pubkey::Pubkey, Epoch};
|
||||
use std::fmt;
|
||||
use std::fs::{create_dir_all, remove_file, OpenOptions};
|
||||
use std::io;
|
||||
@ -38,6 +37,8 @@ pub struct AccountBalance {
|
||||
pub owner: Pubkey,
|
||||
/// this account's data contains a loaded program (and is now read-only)
|
||||
pub executable: bool,
|
||||
/// the epoch at which this account will next owe rent
|
||||
pub rent_epoch: Epoch,
|
||||
}
|
||||
|
||||
/// References to Memory Mapped memory
|
||||
@ -57,6 +58,7 @@ impl<'a> StoredAccount<'a> {
|
||||
lamports: self.balance.lamports,
|
||||
owner: self.balance.owner,
|
||||
executable: self.balance.executable,
|
||||
rent_epoch: self.balance.rent_epoch,
|
||||
data: self.data.to_vec(),
|
||||
}
|
||||
}
|
||||
@ -281,6 +283,7 @@ impl AppendVec {
|
||||
lamports: account.lamports,
|
||||
owner: account.owner,
|
||||
executable: account.executable,
|
||||
rent_epoch: account.rent_epoch,
|
||||
};
|
||||
let balance_ptr = &balance as *const AccountBalance;
|
||||
let data_len = storage_meta.data_len as usize;
|
||||
|
@ -2,23 +2,26 @@
|
||||
//! programs. It offers a high-level API that signs transactions
|
||||
//! on behalf of the caller, and a low-level API for when they have
|
||||
//! already been signed and verified.
|
||||
use crate::accounts::Accounts;
|
||||
use crate::accounts_db::{
|
||||
AccountStorageEntry, AccountsDBSerialize, AppendVecId, ErrorCounters, InstructionAccounts,
|
||||
InstructionCredits, InstructionLoaders,
|
||||
use crate::{
|
||||
accounts::{
|
||||
Accounts, TransactionAccounts, TransactionCredits, TransactionLoaders, TransactionRents,
|
||||
},
|
||||
accounts_db::{AccountStorageEntry, AccountsDBSerialize, AppendVecId, ErrorCounters},
|
||||
accounts_index::Fork,
|
||||
blockhash_queue::BlockhashQueue,
|
||||
epoch_schedule::EpochSchedule,
|
||||
locked_accounts_results::LockedAccountsResults,
|
||||
message_processor::{MessageProcessor, ProcessInstruction},
|
||||
rent_collector::RentCollector,
|
||||
serde_utils::{
|
||||
deserialize_atomicbool, deserialize_atomicusize, serialize_atomicbool,
|
||||
serialize_atomicusize,
|
||||
},
|
||||
stakes::Stakes,
|
||||
status_cache::{SlotDelta, StatusCache},
|
||||
storage_utils,
|
||||
storage_utils::StorageAccounts,
|
||||
};
|
||||
use crate::accounts_index::Fork;
|
||||
use crate::blockhash_queue::BlockhashQueue;
|
||||
use crate::epoch_schedule::EpochSchedule;
|
||||
use crate::locked_accounts_results::LockedAccountsResults;
|
||||
use crate::message_processor::{MessageProcessor, ProcessInstruction};
|
||||
use crate::serde_utils::{
|
||||
deserialize_atomicbool, deserialize_atomicusize, serialize_atomicbool, serialize_atomicusize,
|
||||
};
|
||||
use crate::stakes::Stakes;
|
||||
use crate::status_cache::{SlotDelta, StatusCache};
|
||||
use crate::storage_utils;
|
||||
use crate::storage_utils::StorageAccounts;
|
||||
use bincode::{deserialize_from, serialize_into};
|
||||
use byteorder::{ByteOrder, LittleEndian};
|
||||
use log::*;
|
||||
@ -197,8 +200,11 @@ pub struct Bank {
|
||||
/// The number of slots per Storage segment
|
||||
slots_per_segment: u64,
|
||||
|
||||
/// Bank fork (i.e. slot, i.e. block)
|
||||
slot: u64,
|
||||
/// Bank slot (i.e. block)
|
||||
slot: Slot,
|
||||
|
||||
/// Bank epoch
|
||||
epoch: Epoch,
|
||||
|
||||
/// Bank height in term of banks
|
||||
bank_height: u64,
|
||||
@ -214,6 +220,9 @@ pub struct Bank {
|
||||
/// Latest transaction fees for transactions processed by this bank
|
||||
fee_calculator: FeeCalculator,
|
||||
|
||||
/// latest rent collector, knows the epoch
|
||||
rent_collector: RentCollector,
|
||||
|
||||
/// initialized from genesis
|
||||
epoch_schedule: EpochSchedule,
|
||||
|
||||
@ -282,17 +291,22 @@ impl Bank {
|
||||
let src = StatusCacheRc {
|
||||
status_cache: parent.src.status_cache.clone(),
|
||||
};
|
||||
let epoch_schedule = parent.epoch_schedule;
|
||||
let epoch = epoch_schedule.get_epoch(slot);
|
||||
|
||||
let mut new = Bank {
|
||||
rc,
|
||||
src,
|
||||
slot,
|
||||
epoch,
|
||||
blockhash_queue: RwLock::new(parent.blockhash_queue.read().unwrap().clone()),
|
||||
|
||||
// TODO: clean this up, soo much special-case copying...
|
||||
ticks_per_slot: parent.ticks_per_slot,
|
||||
slots_per_segment: parent.slots_per_segment,
|
||||
slots_per_year: parent.slots_per_year,
|
||||
epoch_schedule: parent.epoch_schedule,
|
||||
slot,
|
||||
epoch_schedule,
|
||||
rent_collector: parent.rent_collector.clone_with_epoch(epoch),
|
||||
max_tick_height: (slot + 1) * parent.ticks_per_slot - 1,
|
||||
bank_height: parent.bank_height + 1,
|
||||
fee_calculator: FeeCalculator::new_derived(
|
||||
@ -300,15 +314,15 @@ impl Bank {
|
||||
parent.signature_count(),
|
||||
),
|
||||
capitalization: AtomicUsize::new(parent.capitalization() as usize),
|
||||
inflation: parent.inflation.clone(),
|
||||
inflation: parent.inflation,
|
||||
transaction_count: AtomicUsize::new(parent.transaction_count() as usize),
|
||||
stakes: RwLock::new(parent.stakes.read().unwrap().clone_with_epoch(0)),
|
||||
stakes: RwLock::new(parent.stakes.read().unwrap().clone_with_epoch(epoch)),
|
||||
epoch_stakes: parent.epoch_stakes.clone(),
|
||||
storage_accounts: RwLock::new(parent.storage_accounts.read().unwrap().clone()),
|
||||
parent_hash: parent.hash(),
|
||||
collector_id: *collector_id,
|
||||
collector_fees: AtomicUsize::new(0),
|
||||
ancestors: HashMap::new(),
|
||||
epoch_stakes: HashMap::new(),
|
||||
hash: RwLock::new(Hash::default()),
|
||||
is_delta: AtomicBool::new(false),
|
||||
tick_height: AtomicUsize::new(parent.tick_height.load(Ordering::Relaxed)),
|
||||
@ -316,28 +330,21 @@ impl Bank {
|
||||
message_processor: MessageProcessor::default(),
|
||||
};
|
||||
|
||||
{
|
||||
*new.stakes.write().unwrap() =
|
||||
parent.stakes.read().unwrap().clone_with_epoch(new.epoch());
|
||||
}
|
||||
|
||||
datapoint_info!(
|
||||
"bank-new_from_parent-heights",
|
||||
("slot_height", slot, i64),
|
||||
("bank_height", new.bank_height, i64)
|
||||
);
|
||||
|
||||
new.epoch_stakes = {
|
||||
let mut epoch_stakes = parent.epoch_stakes.clone();
|
||||
let epoch = new.get_stakers_epoch(new.slot);
|
||||
// update epoch_vote_states cache
|
||||
// if my parent didn't populate for this epoch, we've
|
||||
// crossed a boundary
|
||||
if epoch_stakes.get(&epoch).is_none() {
|
||||
epoch_stakes.insert(epoch, new.stakes.read().unwrap().clone());
|
||||
}
|
||||
epoch_stakes
|
||||
};
|
||||
let stakers_epoch = epoch_schedule.get_stakers_epoch(slot);
|
||||
// update epoch_stakes cache
|
||||
// if my parent didn't populate for this staker's epoch, we've
|
||||
// crossed a boundary
|
||||
if new.epoch_stakes.get(&stakers_epoch).is_none() {
|
||||
new.epoch_stakes
|
||||
.insert(stakers_epoch, new.stakes.read().unwrap().clone());
|
||||
}
|
||||
|
||||
new.ancestors.insert(new.slot(), 0);
|
||||
new.parents().iter().enumerate().for_each(|(i, p)| {
|
||||
new.ancestors.insert(p.slot(), i + 1);
|
||||
@ -375,7 +382,7 @@ impl Bank {
|
||||
}
|
||||
|
||||
pub fn epoch(&self) -> u64 {
|
||||
self.epoch_schedule.get_epoch(self.slot)
|
||||
self.epoch
|
||||
}
|
||||
|
||||
pub fn freeze_lock(&self) -> RwLockReadGuard<Hash> {
|
||||
@ -611,7 +618,7 @@ impl Bank {
|
||||
genesis_block.epoch_warmup,
|
||||
);
|
||||
|
||||
self.inflation = genesis_block.inflation.clone();
|
||||
self.inflation = genesis_block.inflation;
|
||||
|
||||
// Add additional native programs specified in the genesis block
|
||||
for (name, program_id) in &genesis_block.native_instruction_processors {
|
||||
@ -781,13 +788,21 @@ impl Bank {
|
||||
txs: &[Transaction],
|
||||
results: Vec<Result<()>>,
|
||||
error_counters: &mut ErrorCounters,
|
||||
) -> Vec<Result<(InstructionAccounts, InstructionLoaders, InstructionCredits)>> {
|
||||
) -> Vec<
|
||||
Result<(
|
||||
TransactionAccounts,
|
||||
TransactionLoaders,
|
||||
TransactionCredits,
|
||||
TransactionRents,
|
||||
)>,
|
||||
> {
|
||||
self.rc.accounts.load_accounts(
|
||||
&self.ancestors,
|
||||
txs,
|
||||
results,
|
||||
&self.blockhash_queue.read().unwrap(),
|
||||
error_counters,
|
||||
&self.rent_collector,
|
||||
)
|
||||
}
|
||||
fn check_refs(
|
||||
@ -932,7 +947,14 @@ impl Bank {
|
||||
lock_results: &LockedAccountsResults,
|
||||
max_age: usize,
|
||||
) -> (
|
||||
Vec<Result<(InstructionAccounts, InstructionLoaders, InstructionCredits)>>,
|
||||
Vec<
|
||||
Result<(
|
||||
TransactionAccounts,
|
||||
TransactionLoaders,
|
||||
TransactionCredits,
|
||||
TransactionRents,
|
||||
)>,
|
||||
>,
|
||||
Vec<Result<()>>,
|
||||
Vec<usize>,
|
||||
usize,
|
||||
@ -970,7 +992,7 @@ impl Bank {
|
||||
.zip(txs.iter())
|
||||
.map(|(accs, tx)| match accs {
|
||||
Err(e) => Err(e.clone()),
|
||||
Ok((ref mut accounts, ref mut loaders, ref mut credits)) => {
|
||||
Ok((ref mut accounts, ref mut loaders, ref mut credits, ref mut _rents)) => {
|
||||
signature_count += tx.message().header.num_required_signatures as usize;
|
||||
self.message_processor
|
||||
.process_message(tx.message(), loaders, accounts, credits)
|
||||
@ -1061,9 +1083,10 @@ impl Bank {
|
||||
&self,
|
||||
txs: &[Transaction],
|
||||
loaded_accounts: &mut [Result<(
|
||||
InstructionAccounts,
|
||||
InstructionLoaders,
|
||||
InstructionCredits,
|
||||
TransactionAccounts,
|
||||
TransactionLoaders,
|
||||
TransactionCredits,
|
||||
TransactionRents,
|
||||
)>],
|
||||
executed: &[Result<()>],
|
||||
tx_count: usize,
|
||||
@ -1332,7 +1355,12 @@ impl Bank {
|
||||
&self,
|
||||
txs: &[Transaction],
|
||||
res: &[Result<()>],
|
||||
loaded: &[Result<(InstructionAccounts, InstructionLoaders, InstructionCredits)>],
|
||||
loaded: &[Result<(
|
||||
TransactionAccounts,
|
||||
TransactionLoaders,
|
||||
TransactionCredits,
|
||||
TransactionRents,
|
||||
)>],
|
||||
) {
|
||||
for (i, raccs) in loaded.iter().enumerate() {
|
||||
if res[i].is_err() || raccs.is_err() {
|
||||
|
@ -12,6 +12,7 @@ pub mod loader_utils;
|
||||
pub mod locked_accounts_results;
|
||||
pub mod message_processor;
|
||||
mod native_loader;
|
||||
pub mod rent_collector;
|
||||
mod serde_utils;
|
||||
pub mod stakes;
|
||||
pub mod status_cache;
|
||||
|
65
runtime/src/rent_collector.rs
Normal file
65
runtime/src/rent_collector.rs
Normal file
@ -0,0 +1,65 @@
|
||||
//! calculate and collect rent from Accounts
|
||||
use crate::epoch_schedule::EpochSchedule;
|
||||
use solana_sdk::{account::Account, rent::Rent, timing::Epoch};
|
||||
|
||||
#[derive(Default, Serialize, Deserialize, Clone)]
|
||||
pub struct RentCollector {
|
||||
pub epoch: Epoch,
|
||||
pub epoch_schedule: EpochSchedule,
|
||||
pub slots_per_year: f64,
|
||||
pub rent: Rent,
|
||||
}
|
||||
|
||||
impl RentCollector {
|
||||
pub fn new(
|
||||
epoch: Epoch,
|
||||
epoch_schedule: &EpochSchedule,
|
||||
slots_per_year: f64,
|
||||
rent: &Rent,
|
||||
) -> Self {
|
||||
Self {
|
||||
epoch,
|
||||
epoch_schedule: *epoch_schedule,
|
||||
slots_per_year,
|
||||
rent: *rent,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn clone_with_epoch(&self, epoch: Epoch) -> Self {
|
||||
Self {
|
||||
epoch,
|
||||
..self.clone()
|
||||
}
|
||||
}
|
||||
// updates this account's lamports and status and returns
|
||||
// the account rent collected, if any
|
||||
//
|
||||
pub fn update(&self, mut account: Account) -> Option<(Account, u64)> {
|
||||
if account.data.is_empty() || account.rent_epoch > self.epoch {
|
||||
Some((account, 0))
|
||||
} else {
|
||||
let slots_elapsed: u64 = (account.rent_epoch..=self.epoch)
|
||||
.map(|epoch| self.epoch_schedule.get_slots_in_epoch(epoch + 1))
|
||||
.sum();
|
||||
|
||||
let (rent_due, exempt) = self.rent.due(
|
||||
account.lamports,
|
||||
account.data.len(),
|
||||
slots_elapsed as f64 / self.slots_per_year,
|
||||
);
|
||||
|
||||
if exempt || rent_due != 0 {
|
||||
if account.lamports > rent_due {
|
||||
account.rent_epoch = self.epoch + 1;
|
||||
account.lamports -= rent_due;
|
||||
Some((account, rent_due))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
// maybe collect rent later, leave account alone
|
||||
Some((account, 0))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -247,10 +247,8 @@ mod tests {
|
||||
|
||||
let populated_key = Pubkey::new_rand();
|
||||
let mut populated_account = Account {
|
||||
lamports: 0,
|
||||
data: vec![0, 1, 2, 3],
|
||||
owner: Pubkey::default(),
|
||||
executable: false,
|
||||
..Account::default()
|
||||
};
|
||||
let unchanged_account = populated_account.clone();
|
||||
|
||||
|
Reference in New Issue
Block a user