2021-07-09 12:55:31 -07:00
|
|
|
//! `cost_tracker` keeps tracking transaction cost per chained accounts as well as for entire block
|
2021-07-06 08:41:25 -07:00
|
|
|
//! The main functions are:
|
2022-04-07 13:42:39 -07:00
|
|
|
//! - would_fit(&tx_cost), immutable function to test if tx with tx_cost would fit into current block
|
2022-04-08 10:34:11 -07:00
|
|
|
//! - add_transaction_cost(&tx_cost), mutable function to accumulate tx_cost to tracker.
|
2021-06-01 07:16:17 -07:00
|
|
|
//!
|
2021-12-03 09:00:31 -08:00
|
|
|
use {
|
2023-06-06 09:15:38 -07:00
|
|
|
crate::{block_cost_limits::*, transaction_cost::TransactionCost},
|
2023-02-13 09:06:39 -08:00
|
|
|
solana_sdk::{
|
|
|
|
clock::Slot, pubkey::Pubkey, saturating_add_assign, transaction::TransactionError,
|
|
|
|
},
|
2022-04-21 00:38:07 -07:00
|
|
|
std::{cmp::Ordering, collections::HashMap},
|
2021-12-03 09:00:31 -08:00
|
|
|
};
|
2021-06-01 07:16:17 -07:00
|
|
|
|
2021-06-28 19:34:04 -07:00
|
|
|
const WRITABLE_ACCOUNTS_PER_BLOCK: usize = 512;
|
|
|
|
|
2022-03-13 08:58:57 -07:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
2021-10-19 12:37:33 -07:00
|
|
|
pub enum CostTrackerError {
|
|
|
|
/// would exceed block max limit
|
|
|
|
WouldExceedBlockMaxLimit,
|
|
|
|
|
2022-01-12 21:27:19 -08:00
|
|
|
/// would exceed vote max limit
|
|
|
|
WouldExceedVoteMaxLimit,
|
|
|
|
|
2021-10-19 12:37:33 -07:00
|
|
|
/// would exceed account max limit
|
|
|
|
WouldExceedAccountMaxLimit,
|
2021-12-12 12:57:18 -08:00
|
|
|
|
2022-03-13 08:58:57 -07:00
|
|
|
/// would exceed account data block limit
|
|
|
|
WouldExceedAccountDataBlockLimit,
|
|
|
|
|
|
|
|
/// would exceed account data total limit
|
|
|
|
WouldExceedAccountDataTotalLimit,
|
2021-10-19 12:37:33 -07:00
|
|
|
}
|
|
|
|
|
2023-02-13 09:06:39 -08:00
|
|
|
impl From<CostTrackerError> for TransactionError {
|
|
|
|
fn from(err: CostTrackerError) -> Self {
|
|
|
|
match err {
|
|
|
|
CostTrackerError::WouldExceedBlockMaxLimit => Self::WouldExceedMaxBlockCostLimit,
|
|
|
|
CostTrackerError::WouldExceedVoteMaxLimit => Self::WouldExceedMaxVoteCostLimit,
|
|
|
|
CostTrackerError::WouldExceedAccountMaxLimit => Self::WouldExceedMaxAccountCostLimit,
|
|
|
|
CostTrackerError::WouldExceedAccountDataBlockLimit => {
|
|
|
|
Self::WouldExceedAccountDataBlockLimit
|
|
|
|
}
|
|
|
|
CostTrackerError::WouldExceedAccountDataTotalLimit => {
|
|
|
|
Self::WouldExceedAccountDataTotalLimit
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-12 06:51:33 -07:00
|
|
|
#[derive(AbiExample, Debug)]
|
2021-06-01 07:16:17 -07:00
|
|
|
pub struct CostTracker {
|
2021-06-09 15:10:59 -07:00
|
|
|
account_cost_limit: u64,
|
|
|
|
block_cost_limit: u64,
|
2022-01-12 21:27:19 -08:00
|
|
|
vote_cost_limit: u64,
|
2021-06-09 15:10:59 -07:00
|
|
|
cost_by_writable_accounts: HashMap<Pubkey, u64>,
|
|
|
|
block_cost: u64,
|
2022-01-12 21:27:19 -08:00
|
|
|
vote_cost: u64,
|
2021-10-24 20:19:23 -07:00
|
|
|
transaction_count: u64,
|
2021-12-12 12:57:18 -08:00
|
|
|
account_data_size: u64,
|
2022-03-13 08:58:57 -07:00
|
|
|
|
|
|
|
/// The amount of total account data size remaining. If `Some`, then do not add transactions
|
|
|
|
/// that would cause `account_data_size` to exceed this limit.
|
|
|
|
account_data_size_limit: Option<u64>,
|
2021-06-01 07:16:17 -07:00
|
|
|
}
|
|
|
|
|
2021-10-12 06:51:33 -07:00
|
|
|
impl Default for CostTracker {
|
|
|
|
fn default() -> Self {
|
2022-02-22 08:44:23 -08:00
|
|
|
// Clippy doesn't like asserts in const contexts, so need to explicitly allow them. For
|
|
|
|
// more info, see this issue: https://github.com/rust-lang/rust-clippy/issues/8159
|
|
|
|
#![allow(clippy::assertions_on_constants)]
|
|
|
|
const _: () = assert!(MAX_WRITABLE_ACCOUNT_UNITS <= MAX_BLOCK_UNITS);
|
|
|
|
const _: () = assert!(MAX_VOTE_UNITS <= MAX_BLOCK_UNITS);
|
2021-10-12 06:51:33 -07:00
|
|
|
|
2021-06-01 07:16:17 -07:00
|
|
|
Self {
|
2022-02-22 08:44:23 -08:00
|
|
|
account_cost_limit: MAX_WRITABLE_ACCOUNT_UNITS,
|
|
|
|
block_cost_limit: MAX_BLOCK_UNITS,
|
|
|
|
vote_cost_limit: MAX_VOTE_UNITS,
|
2021-06-28 19:34:04 -07:00
|
|
|
cost_by_writable_accounts: HashMap::with_capacity(WRITABLE_ACCOUNTS_PER_BLOCK),
|
2021-06-01 07:16:17 -07:00
|
|
|
block_cost: 0,
|
2022-01-12 21:27:19 -08:00
|
|
|
vote_cost: 0,
|
2021-10-24 20:19:23 -07:00
|
|
|
transaction_count: 0,
|
2021-12-12 12:57:18 -08:00
|
|
|
account_data_size: 0,
|
2022-03-13 08:58:57 -07:00
|
|
|
account_data_size_limit: None,
|
2021-06-01 07:16:17 -07:00
|
|
|
}
|
|
|
|
}
|
2022-02-22 08:44:23 -08:00
|
|
|
}
|
2021-06-01 07:16:17 -07:00
|
|
|
|
2022-02-22 08:44:23 -08:00
|
|
|
impl CostTracker {
|
2022-03-13 08:58:57 -07:00
|
|
|
/// Construct and new CostTracker and set the account data size limit.
|
|
|
|
#[must_use]
|
|
|
|
pub fn new_with_account_data_size_limit(account_data_size_limit: Option<u64>) -> Self {
|
|
|
|
Self {
|
|
|
|
account_data_size_limit,
|
|
|
|
..Self::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-05 21:24:58 -07:00
|
|
|
/// allows to adjust limits initiated during construction
|
2022-01-12 21:27:19 -08:00
|
|
|
pub fn set_limits(
|
|
|
|
&mut self,
|
|
|
|
account_cost_limit: u64,
|
|
|
|
block_cost_limit: u64,
|
|
|
|
vote_cost_limit: u64,
|
|
|
|
) {
|
2021-10-19 12:37:33 -07:00
|
|
|
self.account_cost_limit = account_cost_limit;
|
|
|
|
self.block_cost_limit = block_cost_limit;
|
2022-01-12 21:27:19 -08:00
|
|
|
self.vote_cost_limit = vote_cost_limit;
|
2021-10-19 12:37:33 -07:00
|
|
|
}
|
|
|
|
|
2022-04-08 10:34:11 -07:00
|
|
|
pub fn try_add(&mut self, tx_cost: &TransactionCost) -> Result<u64, CostTrackerError> {
|
2022-04-07 13:42:39 -07:00
|
|
|
self.would_fit(tx_cost)?;
|
2022-04-08 05:22:31 -07:00
|
|
|
self.add_transaction_cost(tx_cost);
|
2022-04-07 13:42:39 -07:00
|
|
|
Ok(self.block_cost)
|
2021-07-06 08:41:25 -07:00
|
|
|
}
|
|
|
|
|
2022-04-21 00:38:07 -07:00
|
|
|
pub fn update_execution_cost(
|
|
|
|
&mut self,
|
|
|
|
estimated_tx_cost: &TransactionCost,
|
|
|
|
actual_execution_units: u64,
|
|
|
|
) {
|
|
|
|
let estimated_execution_units = estimated_tx_cost.bpf_execution_cost;
|
|
|
|
match actual_execution_units.cmp(&estimated_execution_units) {
|
|
|
|
Ordering::Equal => (),
|
|
|
|
Ordering::Greater => {
|
|
|
|
self.add_transaction_execution_cost(
|
|
|
|
estimated_tx_cost,
|
|
|
|
actual_execution_units - estimated_execution_units,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Ordering::Less => {
|
|
|
|
self.sub_transaction_execution_cost(
|
|
|
|
estimated_tx_cost,
|
|
|
|
estimated_execution_units - actual_execution_units,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-08 05:22:31 -07:00
|
|
|
pub fn remove(&mut self, tx_cost: &TransactionCost) {
|
|
|
|
self.remove_transaction_cost(tx_cost);
|
2021-06-01 07:16:17 -07:00
|
|
|
}
|
|
|
|
|
2022-04-08 06:35:16 -07:00
|
|
|
pub fn block_cost(&self) -> u64 {
|
|
|
|
self.block_cost
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn transaction_count(&self) -> u64 {
|
|
|
|
self.transaction_count
|
|
|
|
}
|
|
|
|
|
2021-10-24 20:19:23 -07:00
|
|
|
pub fn report_stats(&self, bank_slot: Slot) {
|
|
|
|
// skip reporting if block is empty
|
|
|
|
if self.transaction_count == 0 {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let (costliest_account, costliest_account_cost) = self.find_costliest_account();
|
|
|
|
|
|
|
|
datapoint_info!(
|
|
|
|
"cost_tracker_stats",
|
|
|
|
("bank_slot", bank_slot as i64, i64),
|
|
|
|
("block_cost", self.block_cost as i64, i64),
|
2022-01-12 21:27:19 -08:00
|
|
|
("vote_cost", self.vote_cost as i64, i64),
|
2021-10-24 20:19:23 -07:00
|
|
|
("transaction_count", self.transaction_count as i64, i64),
|
2022-04-21 00:38:07 -07:00
|
|
|
("number_of_accounts", self.number_of_accounts() as i64, i64),
|
2021-10-24 20:19:23 -07:00
|
|
|
("costliest_account", costliest_account.to_string(), String),
|
|
|
|
("costliest_account_cost", costliest_account_cost as i64, i64),
|
2021-12-12 12:57:18 -08:00
|
|
|
("account_data_size", self.account_data_size, i64),
|
2021-10-24 20:19:23 -07:00
|
|
|
);
|
|
|
|
}
|
2021-10-05 06:57:39 -07:00
|
|
|
|
2021-10-24 20:19:23 -07:00
|
|
|
fn find_costliest_account(&self) -> (Pubkey, u64) {
|
2022-09-23 13:57:27 -07:00
|
|
|
self.cost_by_writable_accounts
|
|
|
|
.iter()
|
|
|
|
.max_by_key(|(_, &cost)| cost)
|
|
|
|
.map(|(&pubkey, &cost)| (pubkey, cost))
|
|
|
|
.unwrap_or_default()
|
2021-10-24 20:19:23 -07:00
|
|
|
}
|
|
|
|
|
2022-04-07 13:42:39 -07:00
|
|
|
fn would_fit(&self, tx_cost: &TransactionCost) -> Result<(), CostTrackerError> {
|
2022-12-02 08:21:25 -08:00
|
|
|
let cost: u64 = tx_cost.sum();
|
|
|
|
let vote_cost = if tx_cost.is_simple_vote { cost } else { 0 };
|
2022-04-07 13:42:39 -07:00
|
|
|
|
2021-06-01 07:16:17 -07:00
|
|
|
// check against the total package cost
|
2022-01-12 21:27:19 -08:00
|
|
|
if self.block_cost.saturating_add(cost) > self.block_cost_limit {
|
2021-10-19 12:37:33 -07:00
|
|
|
return Err(CostTrackerError::WouldExceedBlockMaxLimit);
|
2021-06-01 07:16:17 -07:00
|
|
|
}
|
|
|
|
|
2022-01-12 21:27:19 -08:00
|
|
|
// if vote transaction, check if it exceeds vote_transaction_limit
|
2022-04-07 13:42:39 -07:00
|
|
|
if self.vote_cost.saturating_add(vote_cost) > self.vote_cost_limit {
|
2022-01-12 21:27:19 -08:00
|
|
|
return Err(CostTrackerError::WouldExceedVoteMaxLimit);
|
|
|
|
}
|
|
|
|
|
2021-06-01 07:16:17 -07:00
|
|
|
// check if the transaction itself is more costly than the account_cost_limit
|
2021-12-12 12:57:18 -08:00
|
|
|
if cost > self.account_cost_limit {
|
2021-10-19 12:37:33 -07:00
|
|
|
return Err(CostTrackerError::WouldExceedAccountMaxLimit);
|
2021-06-01 07:16:17 -07:00
|
|
|
}
|
|
|
|
|
2022-03-13 08:58:57 -07:00
|
|
|
// NOTE: Check if the total accounts data size is exceeded *before* the block accounts data
|
|
|
|
// size. This way, transactions are not unnecessarily retried.
|
2022-12-02 08:21:25 -08:00
|
|
|
let account_data_size = self
|
|
|
|
.account_data_size
|
|
|
|
.saturating_add(tx_cost.account_data_size);
|
2022-03-13 08:58:57 -07:00
|
|
|
if let Some(account_data_size_limit) = self.account_data_size_limit {
|
|
|
|
if account_data_size > account_data_size_limit {
|
|
|
|
return Err(CostTrackerError::WouldExceedAccountDataTotalLimit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-16 10:57:24 -07:00
|
|
|
if account_data_size > MAX_BLOCK_ACCOUNTS_DATA_SIZE_DELTA {
|
2022-03-13 08:58:57 -07:00
|
|
|
return Err(CostTrackerError::WouldExceedAccountDataBlockLimit);
|
2021-12-12 12:57:18 -08:00
|
|
|
}
|
|
|
|
|
2021-06-01 07:16:17 -07:00
|
|
|
// check each account against account_cost_limit,
|
2022-12-02 08:21:25 -08:00
|
|
|
for account_key in tx_cost.writable_accounts.iter() {
|
2021-06-18 06:34:46 -07:00
|
|
|
match self.cost_by_writable_accounts.get(account_key) {
|
2021-06-01 07:16:17 -07:00
|
|
|
Some(chained_cost) => {
|
2022-01-12 21:27:19 -08:00
|
|
|
if chained_cost.saturating_add(cost) > self.account_cost_limit {
|
2021-10-19 12:37:33 -07:00
|
|
|
return Err(CostTrackerError::WouldExceedAccountMaxLimit);
|
2021-06-01 07:16:17 -07:00
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => continue,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-04-08 05:22:31 -07:00
|
|
|
fn add_transaction_cost(&mut self, tx_cost: &TransactionCost) {
|
2022-12-02 08:21:25 -08:00
|
|
|
self.add_transaction_execution_cost(tx_cost, tx_cost.sum());
|
|
|
|
saturating_add_assign!(self.account_data_size, tx_cost.account_data_size);
|
2022-04-21 00:38:07 -07:00
|
|
|
saturating_add_assign!(self.transaction_count, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn remove_transaction_cost(&mut self, tx_cost: &TransactionCost) {
|
|
|
|
let cost = tx_cost.sum();
|
|
|
|
self.sub_transaction_execution_cost(tx_cost, cost);
|
|
|
|
self.account_data_size = self
|
|
|
|
.account_data_size
|
|
|
|
.saturating_sub(tx_cost.account_data_size);
|
|
|
|
self.transaction_count = self.transaction_count.saturating_sub(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Apply additional actual execution units to cost_tracker
|
|
|
|
fn add_transaction_execution_cost(&mut self, tx_cost: &TransactionCost, adjustment: u64) {
|
2022-12-02 08:21:25 -08:00
|
|
|
for account_key in tx_cost.writable_accounts.iter() {
|
2022-01-12 21:27:19 -08:00
|
|
|
let account_cost = self
|
2021-06-01 07:16:17 -07:00
|
|
|
.cost_by_writable_accounts
|
|
|
|
.entry(*account_key)
|
2022-01-12 21:27:19 -08:00
|
|
|
.or_insert(0);
|
2022-04-21 00:38:07 -07:00
|
|
|
*account_cost = account_cost.saturating_add(adjustment);
|
2022-01-12 21:27:19 -08:00
|
|
|
}
|
2022-04-21 00:38:07 -07:00
|
|
|
self.block_cost = self.block_cost.saturating_add(adjustment);
|
2022-12-02 08:21:25 -08:00
|
|
|
if tx_cost.is_simple_vote {
|
2022-04-21 00:38:07 -07:00
|
|
|
self.vote_cost = self.vote_cost.saturating_add(adjustment);
|
2021-06-01 07:16:17 -07:00
|
|
|
}
|
|
|
|
}
|
2022-04-08 05:22:31 -07:00
|
|
|
|
2022-11-14 13:36:59 -08:00
|
|
|
/// Subtract extra execution units from cost_tracker
|
2022-04-21 00:38:07 -07:00
|
|
|
fn sub_transaction_execution_cost(&mut self, tx_cost: &TransactionCost, adjustment: u64) {
|
2022-04-08 05:22:31 -07:00
|
|
|
for account_key in tx_cost.writable_accounts.iter() {
|
|
|
|
let account_cost = self
|
|
|
|
.cost_by_writable_accounts
|
|
|
|
.entry(*account_key)
|
|
|
|
.or_insert(0);
|
2022-04-21 00:38:07 -07:00
|
|
|
*account_cost = account_cost.saturating_sub(adjustment);
|
2022-04-08 05:22:31 -07:00
|
|
|
}
|
2022-04-21 00:38:07 -07:00
|
|
|
self.block_cost = self.block_cost.saturating_sub(adjustment);
|
2022-04-08 05:22:31 -07:00
|
|
|
if tx_cost.is_simple_vote {
|
2022-04-21 00:38:07 -07:00
|
|
|
self.vote_cost = self.vote_cost.saturating_sub(adjustment);
|
2022-04-08 05:22:31 -07:00
|
|
|
}
|
2022-04-21 00:38:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// count number of none-zero CU accounts
|
|
|
|
fn number_of_accounts(&self) -> usize {
|
|
|
|
self.cost_by_writable_accounts
|
2022-09-23 13:57:27 -07:00
|
|
|
.values()
|
|
|
|
.filter(|units| **units > 0)
|
|
|
|
.count()
|
2022-04-09 21:07:18 -07:00
|
|
|
}
|
2021-06-01 07:16:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2021-12-03 09:00:31 -08:00
|
|
|
use {
|
|
|
|
super::*,
|
|
|
|
crate::{
|
|
|
|
bank::Bank,
|
|
|
|
genesis_utils::{create_genesis_config, GenesisConfigInfo},
|
|
|
|
},
|
|
|
|
solana_sdk::{
|
|
|
|
hash::Hash,
|
|
|
|
signature::{Keypair, Signer},
|
|
|
|
system_transaction,
|
2022-04-08 10:34:11 -07:00
|
|
|
transaction::{
|
|
|
|
MessageHash, SanitizedTransaction, SimpleAddressLoader, VersionedTransaction,
|
|
|
|
},
|
2021-12-03 09:00:31 -08:00
|
|
|
},
|
2022-01-12 21:27:19 -08:00
|
|
|
solana_vote_program::vote_transaction,
|
2021-12-03 09:00:31 -08:00
|
|
|
std::{cmp, sync::Arc},
|
2021-06-01 07:16:17 -07:00
|
|
|
};
|
|
|
|
|
2022-02-22 08:44:23 -08:00
|
|
|
impl CostTracker {
|
2022-03-13 08:58:57 -07:00
|
|
|
fn new(
|
|
|
|
account_cost_limit: u64,
|
|
|
|
block_cost_limit: u64,
|
|
|
|
vote_cost_limit: u64,
|
|
|
|
account_data_size_limit: Option<u64>,
|
|
|
|
) -> Self {
|
2022-02-22 08:44:23 -08:00
|
|
|
assert!(account_cost_limit <= block_cost_limit);
|
|
|
|
assert!(vote_cost_limit <= block_cost_limit);
|
|
|
|
Self {
|
|
|
|
account_cost_limit,
|
|
|
|
block_cost_limit,
|
|
|
|
vote_cost_limit,
|
2022-03-13 08:58:57 -07:00
|
|
|
account_data_size_limit,
|
2022-02-22 08:44:23 -08:00
|
|
|
..Self::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-01 07:16:17 -07:00
|
|
|
fn test_setup() -> (Keypair, Hash) {
|
|
|
|
solana_logger::setup();
|
|
|
|
let GenesisConfigInfo {
|
|
|
|
genesis_config,
|
|
|
|
mint_keypair,
|
|
|
|
..
|
|
|
|
} = create_genesis_config(10);
|
2021-08-05 12:50:25 -07:00
|
|
|
let bank = Arc::new(Bank::new_no_wallclock_throttle_for_tests(&genesis_config));
|
2021-06-01 07:16:17 -07:00
|
|
|
let start_hash = bank.last_blockhash();
|
|
|
|
(mint_keypair, start_hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn build_simple_transaction(
|
|
|
|
mint_keypair: &Keypair,
|
|
|
|
start_hash: &Hash,
|
2022-04-07 13:42:39 -07:00
|
|
|
) -> (SanitizedTransaction, TransactionCost) {
|
2021-06-01 07:16:17 -07:00
|
|
|
let keypair = Keypair::new();
|
2022-01-12 21:27:19 -08:00
|
|
|
let simple_transaction = SanitizedTransaction::from_transaction_for_tests(
|
|
|
|
system_transaction::transfer(mint_keypair, &keypair.pubkey(), 2, *start_hash),
|
|
|
|
);
|
2022-04-07 13:42:39 -07:00
|
|
|
let mut tx_cost = TransactionCost::new_with_capacity(1);
|
2022-04-19 11:25:47 -07:00
|
|
|
tx_cost.bpf_execution_cost = 5;
|
2022-04-07 13:42:39 -07:00
|
|
|
tx_cost.writable_accounts.push(mint_keypair.pubkey());
|
2021-06-01 07:16:17 -07:00
|
|
|
|
2022-04-07 13:42:39 -07:00
|
|
|
(simple_transaction, tx_cost)
|
2021-06-01 07:16:17 -07:00
|
|
|
}
|
|
|
|
|
2022-01-12 21:27:19 -08:00
|
|
|
fn build_simple_vote_transaction(
|
|
|
|
mint_keypair: &Keypair,
|
|
|
|
start_hash: &Hash,
|
2022-04-07 13:42:39 -07:00
|
|
|
) -> (SanitizedTransaction, TransactionCost) {
|
2022-01-12 21:27:19 -08:00
|
|
|
let keypair = Keypair::new();
|
|
|
|
let transaction = vote_transaction::new_vote_transaction(
|
|
|
|
vec![42],
|
|
|
|
Hash::default(),
|
|
|
|
*start_hash,
|
|
|
|
mint_keypair,
|
|
|
|
&keypair,
|
|
|
|
&keypair,
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
let vote_transaction = SanitizedTransaction::try_create(
|
|
|
|
VersionedTransaction::from(transaction),
|
2022-03-14 21:02:22 -07:00
|
|
|
MessageHash::Compute,
|
2022-01-12 21:27:19 -08:00
|
|
|
Some(true),
|
2022-03-14 21:02:22 -07:00
|
|
|
SimpleAddressLoader::Disabled,
|
2022-01-12 21:27:19 -08:00
|
|
|
)
|
|
|
|
.unwrap();
|
2022-04-07 13:42:39 -07:00
|
|
|
let mut tx_cost = TransactionCost::new_with_capacity(1);
|
2022-04-19 11:25:47 -07:00
|
|
|
tx_cost.bpf_execution_cost = 10;
|
2022-04-07 13:42:39 -07:00
|
|
|
tx_cost.writable_accounts.push(mint_keypair.pubkey());
|
|
|
|
tx_cost.is_simple_vote = true;
|
|
|
|
|
|
|
|
(vote_transaction, tx_cost)
|
2022-01-12 21:27:19 -08:00
|
|
|
}
|
|
|
|
|
2021-06-01 07:16:17 -07:00
|
|
|
#[test]
|
|
|
|
fn test_cost_tracker_initialization() {
|
2022-03-13 08:58:57 -07:00
|
|
|
let testee = CostTracker::new(10, 11, 8, None);
|
2021-06-01 07:16:17 -07:00
|
|
|
assert_eq!(10, testee.account_cost_limit);
|
|
|
|
assert_eq!(11, testee.block_cost_limit);
|
2022-01-12 21:27:19 -08:00
|
|
|
assert_eq!(8, testee.vote_cost_limit);
|
2021-06-01 07:16:17 -07:00
|
|
|
assert_eq!(0, testee.cost_by_writable_accounts.len());
|
|
|
|
assert_eq!(0, testee.block_cost);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_cost_tracker_ok_add_one() {
|
|
|
|
let (mint_keypair, start_hash) = test_setup();
|
2022-04-07 13:42:39 -07:00
|
|
|
let (_tx, tx_cost) = build_simple_transaction(&mint_keypair, &start_hash);
|
|
|
|
let cost = tx_cost.sum();
|
2021-06-01 07:16:17 -07:00
|
|
|
|
|
|
|
// build testee to have capacity for one simple transaction
|
2022-03-13 08:58:57 -07:00
|
|
|
let mut testee = CostTracker::new(cost, cost, cost, None);
|
2022-04-07 13:42:39 -07:00
|
|
|
assert!(testee.would_fit(&tx_cost).is_ok());
|
2022-04-08 10:34:11 -07:00
|
|
|
testee.add_transaction_cost(&tx_cost);
|
2021-06-01 07:16:17 -07:00
|
|
|
assert_eq!(cost, testee.block_cost);
|
2022-01-12 21:27:19 -08:00
|
|
|
assert_eq!(0, testee.vote_cost);
|
|
|
|
let (_costliest_account, costliest_account_cost) = testee.find_costliest_account();
|
|
|
|
assert_eq!(cost, costliest_account_cost);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_cost_tracker_ok_add_one_vote() {
|
|
|
|
let (mint_keypair, start_hash) = test_setup();
|
2022-04-07 13:42:39 -07:00
|
|
|
let (_tx, tx_cost) = build_simple_vote_transaction(&mint_keypair, &start_hash);
|
|
|
|
let cost = tx_cost.sum();
|
2022-01-12 21:27:19 -08:00
|
|
|
|
|
|
|
// build testee to have capacity for one simple transaction
|
2022-03-13 08:58:57 -07:00
|
|
|
let mut testee = CostTracker::new(cost, cost, cost, None);
|
2022-04-07 13:42:39 -07:00
|
|
|
assert!(testee.would_fit(&tx_cost).is_ok());
|
2022-04-08 10:34:11 -07:00
|
|
|
testee.add_transaction_cost(&tx_cost);
|
2022-01-12 21:27:19 -08:00
|
|
|
assert_eq!(cost, testee.block_cost);
|
|
|
|
assert_eq!(cost, testee.vote_cost);
|
|
|
|
let (_costliest_account, costliest_account_cost) = testee.find_costliest_account();
|
|
|
|
assert_eq!(cost, costliest_account_cost);
|
2021-06-01 07:16:17 -07:00
|
|
|
}
|
|
|
|
|
2021-12-12 12:57:18 -08:00
|
|
|
#[test]
|
|
|
|
fn test_cost_tracker_add_data() {
|
|
|
|
let (mint_keypair, start_hash) = test_setup();
|
2022-04-07 13:42:39 -07:00
|
|
|
let (_tx, mut tx_cost) = build_simple_transaction(&mint_keypair, &start_hash);
|
|
|
|
tx_cost.account_data_size = 1;
|
|
|
|
let cost = tx_cost.sum();
|
2021-12-12 12:57:18 -08:00
|
|
|
|
|
|
|
// build testee to have capacity for one simple transaction
|
2022-03-13 08:58:57 -07:00
|
|
|
let mut testee = CostTracker::new(cost, cost, cost, None);
|
2022-04-07 13:42:39 -07:00
|
|
|
assert!(testee.would_fit(&tx_cost).is_ok());
|
2021-12-12 12:57:18 -08:00
|
|
|
let old = testee.account_data_size;
|
2022-04-08 10:34:11 -07:00
|
|
|
testee.add_transaction_cost(&tx_cost);
|
2021-12-12 12:57:18 -08:00
|
|
|
assert_eq!(old + 1, testee.account_data_size);
|
|
|
|
}
|
|
|
|
|
2021-06-01 07:16:17 -07:00
|
|
|
#[test]
|
|
|
|
fn test_cost_tracker_ok_add_two_same_accounts() {
|
|
|
|
let (mint_keypair, start_hash) = test_setup();
|
|
|
|
// build two transactions with same signed account
|
2022-04-07 13:42:39 -07:00
|
|
|
let (_tx1, tx_cost1) = build_simple_transaction(&mint_keypair, &start_hash);
|
|
|
|
let cost1 = tx_cost1.sum();
|
|
|
|
let (_tx2, tx_cost2) = build_simple_transaction(&mint_keypair, &start_hash);
|
|
|
|
let cost2 = tx_cost2.sum();
|
2021-06-01 07:16:17 -07:00
|
|
|
|
|
|
|
// build testee to have capacity for two simple transactions, with same accounts
|
2022-03-13 08:58:57 -07:00
|
|
|
let mut testee = CostTracker::new(cost1 + cost2, cost1 + cost2, cost1 + cost2, None);
|
2021-06-01 07:16:17 -07:00
|
|
|
{
|
2022-04-07 13:42:39 -07:00
|
|
|
assert!(testee.would_fit(&tx_cost1).is_ok());
|
2022-04-08 10:34:11 -07:00
|
|
|
testee.add_transaction_cost(&tx_cost1);
|
2021-06-01 07:16:17 -07:00
|
|
|
}
|
|
|
|
{
|
2022-04-07 13:42:39 -07:00
|
|
|
assert!(testee.would_fit(&tx_cost2).is_ok());
|
2022-04-08 10:34:11 -07:00
|
|
|
testee.add_transaction_cost(&tx_cost2);
|
2021-06-01 07:16:17 -07:00
|
|
|
}
|
|
|
|
assert_eq!(cost1 + cost2, testee.block_cost);
|
|
|
|
assert_eq!(1, testee.cost_by_writable_accounts.len());
|
2022-01-12 21:27:19 -08:00
|
|
|
let (_ccostliest_account, costliest_account_cost) = testee.find_costliest_account();
|
|
|
|
assert_eq!(cost1 + cost2, costliest_account_cost);
|
2021-06-01 07:16:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_cost_tracker_ok_add_two_diff_accounts() {
|
|
|
|
let (mint_keypair, start_hash) = test_setup();
|
|
|
|
// build two transactions with diff accounts
|
|
|
|
let second_account = Keypair::new();
|
2022-04-07 13:42:39 -07:00
|
|
|
let (_tx1, tx_cost1) = build_simple_transaction(&mint_keypair, &start_hash);
|
|
|
|
let cost1 = tx_cost1.sum();
|
|
|
|
let (_tx2, tx_cost2) = build_simple_transaction(&second_account, &start_hash);
|
|
|
|
let cost2 = tx_cost2.sum();
|
2021-06-01 07:16:17 -07:00
|
|
|
|
|
|
|
// build testee to have capacity for two simple transactions, with same accounts
|
2022-03-13 08:58:57 -07:00
|
|
|
let mut testee =
|
|
|
|
CostTracker::new(cmp::max(cost1, cost2), cost1 + cost2, cost1 + cost2, None);
|
2021-06-01 07:16:17 -07:00
|
|
|
{
|
2022-04-07 13:42:39 -07:00
|
|
|
assert!(testee.would_fit(&tx_cost1).is_ok());
|
2022-04-08 10:34:11 -07:00
|
|
|
testee.add_transaction_cost(&tx_cost1);
|
2021-06-01 07:16:17 -07:00
|
|
|
}
|
|
|
|
{
|
2022-04-07 13:42:39 -07:00
|
|
|
assert!(testee.would_fit(&tx_cost2).is_ok());
|
2022-04-08 10:34:11 -07:00
|
|
|
testee.add_transaction_cost(&tx_cost2);
|
2021-06-01 07:16:17 -07:00
|
|
|
}
|
|
|
|
assert_eq!(cost1 + cost2, testee.block_cost);
|
|
|
|
assert_eq!(2, testee.cost_by_writable_accounts.len());
|
2022-01-12 21:27:19 -08:00
|
|
|
let (_ccostliest_account, costliest_account_cost) = testee.find_costliest_account();
|
|
|
|
assert_eq!(std::cmp::max(cost1, cost2), costliest_account_cost);
|
2021-06-01 07:16:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_cost_tracker_chain_reach_limit() {
|
|
|
|
let (mint_keypair, start_hash) = test_setup();
|
|
|
|
// build two transactions with same signed account
|
2022-04-07 13:42:39 -07:00
|
|
|
let (_tx1, tx_cost1) = build_simple_transaction(&mint_keypair, &start_hash);
|
|
|
|
let cost1 = tx_cost1.sum();
|
|
|
|
let (_tx2, tx_cost2) = build_simple_transaction(&mint_keypair, &start_hash);
|
|
|
|
let cost2 = tx_cost2.sum();
|
2021-06-01 07:16:17 -07:00
|
|
|
|
|
|
|
// build testee to have capacity for two simple transactions, but not for same accounts
|
2022-03-13 08:58:57 -07:00
|
|
|
let mut testee =
|
|
|
|
CostTracker::new(cmp::min(cost1, cost2), cost1 + cost2, cost1 + cost2, None);
|
2021-06-01 07:16:17 -07:00
|
|
|
// should have room for first transaction
|
|
|
|
{
|
2022-04-07 13:42:39 -07:00
|
|
|
assert!(testee.would_fit(&tx_cost1).is_ok());
|
2022-04-08 10:34:11 -07:00
|
|
|
testee.add_transaction_cost(&tx_cost1);
|
2021-06-01 07:16:17 -07:00
|
|
|
}
|
|
|
|
// but no more sapce on the same chain (same signer account)
|
|
|
|
{
|
2022-04-07 13:42:39 -07:00
|
|
|
assert!(testee.would_fit(&tx_cost2).is_err());
|
2021-06-01 07:16:17 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_cost_tracker_reach_limit() {
|
|
|
|
let (mint_keypair, start_hash) = test_setup();
|
|
|
|
// build two transactions with diff accounts
|
|
|
|
let second_account = Keypair::new();
|
2022-04-07 13:42:39 -07:00
|
|
|
let (_tx1, tx_cost1) = build_simple_transaction(&mint_keypair, &start_hash);
|
|
|
|
let cost1 = tx_cost1.sum();
|
|
|
|
let (_tx2, tx_cost2) = build_simple_transaction(&second_account, &start_hash);
|
|
|
|
let cost2 = tx_cost2.sum();
|
2021-06-01 07:16:17 -07:00
|
|
|
|
|
|
|
// build testee to have capacity for each chain, but not enough room for both transactions
|
2022-03-13 08:58:57 -07:00
|
|
|
let mut testee = CostTracker::new(
|
|
|
|
cmp::max(cost1, cost2),
|
|
|
|
cost1 + cost2 - 1,
|
|
|
|
cost1 + cost2 - 1,
|
|
|
|
None,
|
|
|
|
);
|
2021-06-01 07:16:17 -07:00
|
|
|
// should have room for first transaction
|
|
|
|
{
|
2022-04-07 13:42:39 -07:00
|
|
|
assert!(testee.would_fit(&tx_cost1).is_ok());
|
2022-04-08 10:34:11 -07:00
|
|
|
testee.add_transaction_cost(&tx_cost1);
|
2021-06-01 07:16:17 -07:00
|
|
|
}
|
|
|
|
// but no more room for package as whole
|
|
|
|
{
|
2022-04-07 13:42:39 -07:00
|
|
|
assert!(testee.would_fit(&tx_cost2).is_err());
|
2022-01-12 21:27:19 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_cost_tracker_reach_vote_limit() {
|
|
|
|
let (mint_keypair, start_hash) = test_setup();
|
|
|
|
// build two mocking vote transactions with diff accounts
|
|
|
|
let second_account = Keypair::new();
|
2022-04-07 13:42:39 -07:00
|
|
|
let (_tx1, tx_cost1) = build_simple_vote_transaction(&mint_keypair, &start_hash);
|
|
|
|
let (_tx2, tx_cost2) = build_simple_vote_transaction(&second_account, &start_hash);
|
|
|
|
let cost1 = tx_cost1.sum();
|
|
|
|
let cost2 = tx_cost2.sum();
|
2022-01-12 21:27:19 -08:00
|
|
|
|
|
|
|
// build testee to have capacity for each chain, but not enough room for both votes
|
2022-03-13 08:58:57 -07:00
|
|
|
let mut testee = CostTracker::new(
|
|
|
|
cmp::max(cost1, cost2),
|
|
|
|
cost1 + cost2,
|
|
|
|
cost1 + cost2 - 1,
|
|
|
|
None,
|
|
|
|
);
|
2022-01-12 21:27:19 -08:00
|
|
|
// should have room for first vote
|
|
|
|
{
|
2022-04-07 13:42:39 -07:00
|
|
|
assert!(testee.would_fit(&tx_cost1).is_ok());
|
2022-04-08 10:34:11 -07:00
|
|
|
testee.add_transaction_cost(&tx_cost1);
|
2022-01-12 21:27:19 -08:00
|
|
|
}
|
|
|
|
// but no more room for package as whole
|
|
|
|
{
|
2022-04-07 13:42:39 -07:00
|
|
|
assert!(testee.would_fit(&tx_cost2).is_err());
|
2022-01-12 21:27:19 -08:00
|
|
|
}
|
|
|
|
// however there is room for none-vote tx3
|
|
|
|
{
|
|
|
|
let third_account = Keypair::new();
|
2022-04-07 13:42:39 -07:00
|
|
|
let (_tx3, tx_cost3) = build_simple_transaction(&third_account, &start_hash);
|
|
|
|
assert!(testee.would_fit(&tx_cost3).is_ok());
|
2021-06-01 07:16:17 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-12 12:57:18 -08:00
|
|
|
#[test]
|
2022-03-13 08:58:57 -07:00
|
|
|
fn test_cost_tracker_reach_data_block_limit() {
|
2021-12-12 12:57:18 -08:00
|
|
|
let (mint_keypair, start_hash) = test_setup();
|
|
|
|
// build two transactions with diff accounts
|
|
|
|
let second_account = Keypair::new();
|
2022-04-07 13:42:39 -07:00
|
|
|
let (_tx1, mut tx_cost1) = build_simple_transaction(&mint_keypair, &start_hash);
|
|
|
|
let (_tx2, mut tx_cost2) = build_simple_transaction(&second_account, &start_hash);
|
2022-08-16 10:57:24 -07:00
|
|
|
tx_cost1.account_data_size = MAX_BLOCK_ACCOUNTS_DATA_SIZE_DELTA;
|
|
|
|
tx_cost2.account_data_size = MAX_BLOCK_ACCOUNTS_DATA_SIZE_DELTA + 1;
|
2022-04-07 13:42:39 -07:00
|
|
|
let cost1 = tx_cost1.sum();
|
|
|
|
let cost2 = tx_cost2.sum();
|
2021-12-12 12:57:18 -08:00
|
|
|
|
|
|
|
// build testee that passes
|
2022-03-13 08:58:57 -07:00
|
|
|
let testee = CostTracker::new(
|
|
|
|
cmp::max(cost1, cost2),
|
|
|
|
cost1 + cost2 - 1,
|
|
|
|
cost1 + cost2 - 1,
|
|
|
|
None,
|
|
|
|
);
|
2022-04-07 13:42:39 -07:00
|
|
|
assert!(testee.would_fit(&tx_cost1).is_ok());
|
2021-12-12 12:57:18 -08:00
|
|
|
// data is too big
|
2022-03-13 08:58:57 -07:00
|
|
|
assert_eq!(
|
2022-04-07 13:42:39 -07:00
|
|
|
testee.would_fit(&tx_cost2),
|
2022-03-13 08:58:57 -07:00
|
|
|
Err(CostTrackerError::WouldExceedAccountDataBlockLimit),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_cost_tracker_reach_data_total_limit() {
|
|
|
|
let (mint_keypair, start_hash) = test_setup();
|
|
|
|
// build two transactions with diff accounts
|
|
|
|
let second_account = Keypair::new();
|
2022-04-07 13:42:39 -07:00
|
|
|
let (_tx1, mut tx_cost1) = build_simple_transaction(&mint_keypair, &start_hash);
|
|
|
|
let (_tx2, mut tx_cost2) = build_simple_transaction(&second_account, &start_hash);
|
|
|
|
let remaining_account_data_size = 1234;
|
|
|
|
tx_cost1.account_data_size = remaining_account_data_size;
|
|
|
|
tx_cost2.account_data_size = remaining_account_data_size + 1;
|
|
|
|
let cost1 = tx_cost1.sum();
|
|
|
|
let cost2 = tx_cost2.sum();
|
2022-03-13 08:58:57 -07:00
|
|
|
|
|
|
|
// build testee that passes
|
|
|
|
let testee = CostTracker::new(
|
|
|
|
cmp::max(cost1, cost2),
|
|
|
|
cost1 + cost2 - 1,
|
|
|
|
cost1 + cost2 - 1,
|
|
|
|
Some(remaining_account_data_size),
|
|
|
|
);
|
2022-04-07 13:42:39 -07:00
|
|
|
assert!(testee.would_fit(&tx_cost1).is_ok());
|
2022-03-13 08:58:57 -07:00
|
|
|
// data is too big
|
|
|
|
assert_eq!(
|
2022-04-07 13:42:39 -07:00
|
|
|
testee.would_fit(&tx_cost2),
|
2022-03-13 08:58:57 -07:00
|
|
|
Err(CostTrackerError::WouldExceedAccountDataTotalLimit),
|
|
|
|
);
|
2021-12-12 12:57:18 -08:00
|
|
|
}
|
|
|
|
|
2022-04-07 13:42:39 -07:00
|
|
|
#[test]
|
2022-04-08 10:34:11 -07:00
|
|
|
fn test_cost_tracker_remove() {
|
2022-04-07 13:42:39 -07:00
|
|
|
let (mint_keypair, start_hash) = test_setup();
|
|
|
|
// build two transactions with diff accounts
|
|
|
|
let second_account = Keypair::new();
|
2022-04-08 10:34:11 -07:00
|
|
|
let (_tx1, tx_cost1) = build_simple_transaction(&mint_keypair, &start_hash);
|
|
|
|
let (_tx2, tx_cost2) = build_simple_transaction(&second_account, &start_hash);
|
2022-04-07 13:42:39 -07:00
|
|
|
let cost1 = tx_cost1.sum();
|
|
|
|
let cost2 = tx_cost2.sum();
|
|
|
|
// build testee
|
|
|
|
let mut testee = CostTracker::new(cost1 + cost2, cost1 + cost2, cost1 + cost2, None);
|
|
|
|
|
2022-04-08 10:34:11 -07:00
|
|
|
assert!(testee.try_add(&tx_cost1).is_ok());
|
|
|
|
assert!(testee.try_add(&tx_cost2).is_ok());
|
|
|
|
assert_eq!(cost1 + cost2, testee.block_cost);
|
2022-04-07 13:42:39 -07:00
|
|
|
|
2022-04-08 10:34:11 -07:00
|
|
|
// removing a tx_cost affects block_cost
|
|
|
|
testee.remove(&tx_cost1);
|
|
|
|
assert_eq!(cost2, testee.block_cost);
|
2022-04-07 13:42:39 -07:00
|
|
|
|
2022-04-08 10:34:11 -07:00
|
|
|
// add back tx1
|
|
|
|
assert!(testee.try_add(&tx_cost1).is_ok());
|
2022-04-07 13:42:39 -07:00
|
|
|
assert_eq!(cost1 + cost2, testee.block_cost);
|
2022-04-08 10:34:11 -07:00
|
|
|
|
|
|
|
// cannot add tx1 again, cost limit would be exceeded
|
|
|
|
assert!(testee.try_add(&tx_cost1).is_err());
|
2022-04-07 13:42:39 -07:00
|
|
|
}
|
|
|
|
|
2021-06-01 07:16:17 -07:00
|
|
|
#[test]
|
2021-10-19 12:37:33 -07:00
|
|
|
fn test_cost_tracker_try_add_is_atomic() {
|
2021-06-01 07:16:17 -07:00
|
|
|
let acct1 = Pubkey::new_unique();
|
|
|
|
let acct2 = Pubkey::new_unique();
|
|
|
|
let acct3 = Pubkey::new_unique();
|
|
|
|
let cost = 100;
|
|
|
|
let account_max = cost * 2;
|
|
|
|
let block_max = account_max * 3; // for three accts
|
|
|
|
|
2022-03-13 08:58:57 -07:00
|
|
|
let mut testee = CostTracker::new(account_max, block_max, block_max, None);
|
2021-06-01 07:16:17 -07:00
|
|
|
|
|
|
|
// case 1: a tx writes to 3 accounts, should success, we will have:
|
|
|
|
// | acct1 | $cost |
|
|
|
|
// | acct2 | $cost |
|
2021-10-24 20:19:23 -07:00
|
|
|
// | acct3 | $cost |
|
2021-06-01 07:16:17 -07:00
|
|
|
// and block_cost = $cost
|
|
|
|
{
|
|
|
|
let tx_cost = TransactionCost {
|
|
|
|
writable_accounts: vec![acct1, acct2, acct3],
|
2022-04-19 11:25:47 -07:00
|
|
|
bpf_execution_cost: cost,
|
2021-10-08 12:48:50 -07:00
|
|
|
..TransactionCost::default()
|
2021-06-01 07:16:17 -07:00
|
|
|
};
|
2022-04-08 10:34:11 -07:00
|
|
|
assert!(testee.try_add(&tx_cost).is_ok());
|
2021-10-24 20:19:23 -07:00
|
|
|
let (_costliest_account, costliest_account_cost) = testee.find_costliest_account();
|
|
|
|
assert_eq!(cost, testee.block_cost);
|
|
|
|
assert_eq!(3, testee.cost_by_writable_accounts.len());
|
|
|
|
assert_eq!(cost, costliest_account_cost);
|
2021-06-01 07:16:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// case 2: add tx writes to acct2 with $cost, should succeed, result to
|
|
|
|
// | acct1 | $cost |
|
|
|
|
// | acct2 | $cost * 2 |
|
2021-10-24 20:19:23 -07:00
|
|
|
// | acct3 | $cost |
|
2021-06-01 07:16:17 -07:00
|
|
|
// and block_cost = $cost * 2
|
|
|
|
{
|
|
|
|
let tx_cost = TransactionCost {
|
|
|
|
writable_accounts: vec![acct2],
|
2022-04-19 11:25:47 -07:00
|
|
|
bpf_execution_cost: cost,
|
2021-10-08 12:48:50 -07:00
|
|
|
..TransactionCost::default()
|
2021-06-01 07:16:17 -07:00
|
|
|
};
|
2022-04-08 10:34:11 -07:00
|
|
|
assert!(testee.try_add(&tx_cost).is_ok());
|
2021-10-24 20:19:23 -07:00
|
|
|
let (costliest_account, costliest_account_cost) = testee.find_costliest_account();
|
|
|
|
assert_eq!(cost * 2, testee.block_cost);
|
|
|
|
assert_eq!(3, testee.cost_by_writable_accounts.len());
|
|
|
|
assert_eq!(cost * 2, costliest_account_cost);
|
|
|
|
assert_eq!(acct2, costliest_account);
|
2021-06-01 07:16:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// case 3: add tx writes to [acct1, acct2], acct2 exceeds limit, should failed atomically,
|
|
|
|
// we shoudl still have:
|
|
|
|
// | acct1 | $cost |
|
2021-10-24 20:19:23 -07:00
|
|
|
// | acct2 | $cost * 2 |
|
|
|
|
// | acct3 | $cost |
|
|
|
|
// and block_cost = $cost * 2
|
2021-06-01 07:16:17 -07:00
|
|
|
{
|
|
|
|
let tx_cost = TransactionCost {
|
|
|
|
writable_accounts: vec![acct1, acct2],
|
2022-04-19 11:25:47 -07:00
|
|
|
bpf_execution_cost: cost,
|
2021-10-08 12:48:50 -07:00
|
|
|
..TransactionCost::default()
|
2021-06-01 07:16:17 -07:00
|
|
|
};
|
2022-04-08 10:34:11 -07:00
|
|
|
assert!(testee.try_add(&tx_cost).is_err());
|
2021-10-24 20:19:23 -07:00
|
|
|
let (costliest_account, costliest_account_cost) = testee.find_costliest_account();
|
|
|
|
assert_eq!(cost * 2, testee.block_cost);
|
|
|
|
assert_eq!(3, testee.cost_by_writable_accounts.len());
|
|
|
|
assert_eq!(cost * 2, costliest_account_cost);
|
|
|
|
assert_eq!(acct2, costliest_account);
|
2021-06-01 07:16:17 -07:00
|
|
|
}
|
|
|
|
}
|
2022-04-21 00:38:07 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_adjust_transaction_execution_cost() {
|
|
|
|
let acct1 = Pubkey::new_unique();
|
|
|
|
let acct2 = Pubkey::new_unique();
|
|
|
|
let acct3 = Pubkey::new_unique();
|
|
|
|
let cost = 100;
|
|
|
|
let account_max = cost * 2;
|
|
|
|
let block_max = account_max * 3; // for three accts
|
|
|
|
|
|
|
|
let mut testee = CostTracker::new(account_max, block_max, block_max, None);
|
|
|
|
let tx_cost = TransactionCost {
|
|
|
|
writable_accounts: vec![acct1, acct2, acct3],
|
|
|
|
bpf_execution_cost: cost,
|
|
|
|
..TransactionCost::default()
|
|
|
|
};
|
|
|
|
let mut expected_block_cost = tx_cost.sum();
|
|
|
|
let expected_tx_count = 1;
|
|
|
|
assert!(testee.try_add(&tx_cost).is_ok());
|
|
|
|
assert_eq!(expected_block_cost, testee.block_cost());
|
|
|
|
assert_eq!(expected_tx_count, testee.transaction_count());
|
|
|
|
testee
|
|
|
|
.cost_by_writable_accounts
|
|
|
|
.iter()
|
|
|
|
.for_each(|(_key, units)| {
|
|
|
|
assert_eq!(expected_block_cost, *units);
|
|
|
|
});
|
|
|
|
|
|
|
|
// adjust up
|
|
|
|
{
|
|
|
|
let adjustment = 50u64;
|
|
|
|
testee.add_transaction_execution_cost(&tx_cost, adjustment);
|
|
|
|
expected_block_cost += 50;
|
|
|
|
assert_eq!(expected_block_cost, testee.block_cost());
|
|
|
|
assert_eq!(expected_tx_count, testee.transaction_count());
|
|
|
|
testee
|
|
|
|
.cost_by_writable_accounts
|
|
|
|
.iter()
|
|
|
|
.for_each(|(_key, units)| {
|
|
|
|
assert_eq!(expected_block_cost, *units);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// adjust down
|
|
|
|
{
|
|
|
|
let adjustment = 50u64;
|
|
|
|
testee.sub_transaction_execution_cost(&tx_cost, adjustment);
|
|
|
|
expected_block_cost -= 50;
|
|
|
|
assert_eq!(expected_block_cost, testee.block_cost());
|
|
|
|
assert_eq!(expected_tx_count, testee.transaction_count());
|
|
|
|
testee
|
|
|
|
.cost_by_writable_accounts
|
|
|
|
.iter()
|
|
|
|
.for_each(|(_key, units)| {
|
|
|
|
assert_eq!(expected_block_cost, *units);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// adjust overflow
|
|
|
|
{
|
|
|
|
testee.add_transaction_execution_cost(&tx_cost, u64::MAX);
|
|
|
|
// expect block cost set to limit
|
|
|
|
assert_eq!(u64::MAX, testee.block_cost());
|
|
|
|
assert_eq!(expected_tx_count, testee.transaction_count());
|
|
|
|
testee
|
|
|
|
.cost_by_writable_accounts
|
|
|
|
.iter()
|
|
|
|
.for_each(|(_key, units)| {
|
|
|
|
assert_eq!(u64::MAX, *units);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// adjust underflow
|
|
|
|
{
|
|
|
|
testee.sub_transaction_execution_cost(&tx_cost, u64::MAX);
|
|
|
|
// expect block cost set to limit
|
|
|
|
assert_eq!(u64::MIN, testee.block_cost());
|
|
|
|
assert_eq!(expected_tx_count, testee.transaction_count());
|
|
|
|
testee
|
|
|
|
.cost_by_writable_accounts
|
|
|
|
.iter()
|
|
|
|
.for_each(|(_key, units)| {
|
|
|
|
assert_eq!(u64::MIN, *units);
|
|
|
|
});
|
|
|
|
// assert the number of non-empty accounts is zero, but map
|
|
|
|
// still contains 3 account
|
|
|
|
assert_eq!(0, testee.number_of_accounts());
|
|
|
|
assert_eq!(3, testee.cost_by_writable_accounts.len());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_update_execution_cost() {
|
|
|
|
let acct1 = Pubkey::new_unique();
|
|
|
|
let acct2 = Pubkey::new_unique();
|
|
|
|
let acct3 = Pubkey::new_unique();
|
|
|
|
let cost = 100;
|
|
|
|
|
|
|
|
let tx_cost = TransactionCost {
|
|
|
|
writable_accounts: vec![acct1, acct2, acct3],
|
|
|
|
bpf_execution_cost: cost,
|
|
|
|
..TransactionCost::default()
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut cost_tracker = CostTracker::default();
|
|
|
|
|
|
|
|
// Assert OK to add tx_cost
|
|
|
|
assert!(cost_tracker.try_add(&tx_cost).is_ok());
|
|
|
|
let (_costliest_account, costliest_account_cost) = cost_tracker.find_costliest_account();
|
|
|
|
assert_eq!(cost, cost_tracker.block_cost);
|
|
|
|
assert_eq!(cost, costliest_account_cost);
|
|
|
|
assert_eq!(1, cost_tracker.transaction_count);
|
|
|
|
|
|
|
|
// assert no-change if actual units is same as estimated units
|
|
|
|
let mut expected_cost = cost;
|
|
|
|
cost_tracker.update_execution_cost(&tx_cost, cost);
|
|
|
|
let (_costliest_account, costliest_account_cost) = cost_tracker.find_costliest_account();
|
|
|
|
assert_eq!(expected_cost, cost_tracker.block_cost);
|
|
|
|
assert_eq!(expected_cost, costliest_account_cost);
|
|
|
|
assert_eq!(1, cost_tracker.transaction_count);
|
|
|
|
|
|
|
|
// assert cost are adjusted down
|
|
|
|
let reduced_units = 3;
|
|
|
|
expected_cost -= reduced_units;
|
|
|
|
cost_tracker.update_execution_cost(&tx_cost, cost - reduced_units);
|
|
|
|
let (_costliest_account, costliest_account_cost) = cost_tracker.find_costliest_account();
|
|
|
|
assert_eq!(expected_cost, cost_tracker.block_cost);
|
|
|
|
assert_eq!(expected_cost, costliest_account_cost);
|
|
|
|
assert_eq!(1, cost_tracker.transaction_count);
|
|
|
|
|
|
|
|
// assert cost are adjusted up
|
|
|
|
let increased_units = 1;
|
|
|
|
expected_cost += increased_units;
|
|
|
|
cost_tracker.update_execution_cost(&tx_cost, cost + increased_units);
|
|
|
|
let (_costliest_account, costliest_account_cost) = cost_tracker.find_costliest_account();
|
|
|
|
assert_eq!(expected_cost, cost_tracker.block_cost);
|
|
|
|
assert_eq!(expected_cost, costliest_account_cost);
|
|
|
|
assert_eq!(1, cost_tracker.transaction_count);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_remove_transaction_cost() {
|
|
|
|
let mut cost_tracker = CostTracker::default();
|
|
|
|
|
|
|
|
let cost = 100u64;
|
|
|
|
let tx_cost = TransactionCost {
|
|
|
|
writable_accounts: vec![Pubkey::new_unique()],
|
|
|
|
bpf_execution_cost: cost,
|
|
|
|
..TransactionCost::default()
|
|
|
|
};
|
|
|
|
|
|
|
|
cost_tracker.add_transaction_cost(&tx_cost);
|
|
|
|
// assert cost_tracker is reverted to default
|
|
|
|
assert_eq!(1, cost_tracker.transaction_count);
|
|
|
|
assert_eq!(1, cost_tracker.number_of_accounts());
|
|
|
|
assert_eq!(cost, cost_tracker.block_cost);
|
|
|
|
assert_eq!(0, cost_tracker.vote_cost);
|
|
|
|
assert_eq!(0, cost_tracker.account_data_size);
|
|
|
|
|
|
|
|
cost_tracker.remove_transaction_cost(&tx_cost);
|
|
|
|
// assert cost_tracker is reverted to default
|
|
|
|
assert_eq!(0, cost_tracker.transaction_count);
|
|
|
|
assert_eq!(0, cost_tracker.number_of_accounts());
|
|
|
|
assert_eq!(0, cost_tracker.block_cost);
|
|
|
|
assert_eq!(0, cost_tracker.vote_cost);
|
|
|
|
assert_eq!(0, cost_tracker.account_data_size);
|
|
|
|
}
|
2021-06-01 07:16:17 -07:00
|
|
|
}
|