Skip to content

Commit

Permalink
2210 - account override for validate_fee
Browse files Browse the repository at this point in the history
  • Loading branch information
ceciEstErmat committed Aug 5, 2024
1 parent 6ac4fe3 commit 777dc56
Showing 1 changed file with 73 additions and 2 deletions.
75 changes: 73 additions & 2 deletions svm/src/transaction_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ impl<FG: ForkGraph> TransactionBatchProcessor<FG> {

let (validation_results, validate_fees_us) = measure_us!(self.validate_fees(
callbacks,
config.account_overrides,
sanitized_txs,
check_results,
&environment.feature_set,
Expand Down Expand Up @@ -360,6 +361,7 @@ impl<FG: ForkGraph> TransactionBatchProcessor<FG> {
fn validate_fees<CB: TransactionProcessingCallback>(
&self,
callbacks: &CB,
account_overrides: Option<&AccountOverrides>,
sanitized_txs: &[impl core::borrow::Borrow<SanitizedTransaction>],
check_results: Vec<TransactionCheckResult>,
feature_set: &FeatureSet,
Expand All @@ -375,6 +377,7 @@ impl<FG: ForkGraph> TransactionBatchProcessor<FG> {
let message = sanitized_tx.borrow().message();
self.validate_transaction_fee_payer(
callbacks,
account_overrides,
message,
checked_details,
feature_set,
Expand All @@ -393,6 +396,7 @@ impl<FG: ForkGraph> TransactionBatchProcessor<FG> {
fn validate_transaction_fee_payer<CB: TransactionProcessingCallback>(
&self,
callbacks: &CB,
account_overrides: Option<&AccountOverrides>,
message: &SanitizedMessage,
checked_details: CheckedTransactionDetails,
feature_set: &FeatureSet,
Expand All @@ -409,8 +413,12 @@ impl<FG: ForkGraph> TransactionBatchProcessor<FG> {
})?;

let fee_payer_address = message.fee_payer();
let Some(mut fee_payer_account) = callbacks.get_account_shared_data(fee_payer_address)
else {

let fee_payer_account = account_overrides
.and_then(|overrides| overrides.get(fee_payer_address).cloned())
.or_else(|| callbacks.get_account_shared_data(fee_payer_address));

let Some(mut fee_payer_account) = fee_payer_account else {
error_counters.account_not_found += 1;
return Err(TransactionError::AccountNotFound);
};
Expand Down Expand Up @@ -1836,6 +1844,7 @@ mod tests {
let batch_processor = TransactionBatchProcessor::<TestForkGraph>::default();
let result = batch_processor.validate_transaction_fee_payer(
&mock_bank,
None,
&message,
CheckedTransactionDetails {
nonce: None,
Expand Down Expand Up @@ -1908,6 +1917,7 @@ mod tests {
let batch_processor = TransactionBatchProcessor::<TestForkGraph>::default();
let result = batch_processor.validate_transaction_fee_payer(
&mock_bank,
None,
&message,
CheckedTransactionDetails {
nonce: None,
Expand Down Expand Up @@ -1955,6 +1965,7 @@ mod tests {
let batch_processor = TransactionBatchProcessor::<TestForkGraph>::default();
let result = batch_processor.validate_transaction_fee_payer(
&mock_bank,
None,
&message,
CheckedTransactionDetails {
nonce: None,
Expand Down Expand Up @@ -1987,6 +1998,7 @@ mod tests {
let batch_processor = TransactionBatchProcessor::<TestForkGraph>::default();
let result = batch_processor.validate_transaction_fee_payer(
&mock_bank,
None,
&message,
CheckedTransactionDetails {
nonce: None,
Expand Down Expand Up @@ -2023,6 +2035,7 @@ mod tests {
let batch_processor = TransactionBatchProcessor::<TestForkGraph>::default();
let result = batch_processor.validate_transaction_fee_payer(
&mock_bank,
None,
&message,
CheckedTransactionDetails {
nonce: None,
Expand Down Expand Up @@ -2057,6 +2070,7 @@ mod tests {
let batch_processor = TransactionBatchProcessor::<TestForkGraph>::default();
let result = batch_processor.validate_transaction_fee_payer(
&mock_bank,
None,
&message,
CheckedTransactionDetails {
nonce: None,
Expand Down Expand Up @@ -2088,6 +2102,7 @@ mod tests {
let batch_processor = TransactionBatchProcessor::<TestForkGraph>::default();
let result = batch_processor.validate_transaction_fee_payer(
&mock_bank,
None,
&message,
CheckedTransactionDetails {
nonce: None,
Expand Down Expand Up @@ -2149,6 +2164,7 @@ mod tests {
));
let result = batch_processor.validate_transaction_fee_payer(
&mock_bank,
None,
&message,
CheckedTransactionDetails {
nonce: nonce.clone(),
Expand Down Expand Up @@ -2206,6 +2222,7 @@ mod tests {
let batch_processor = TransactionBatchProcessor::<TestForkGraph>::default();
let result = batch_processor.validate_transaction_fee_payer(
&mock_bank,
None,
&message,
CheckedTransactionDetails {
nonce: None,
Expand All @@ -2221,4 +2238,58 @@ mod tests {
assert_eq!(result, Err(TransactionError::InsufficientFundsForFee));
}
}

#[test]
fn test_validate_account_override_usage_on_validate_fee() {
/*
The test setups an account override with enough lamport to pass validate fee.
The account_db has the account with minimum rent amount thus would fail the validate_free.
The test verify that the override is used with a passing test of validate fee.
*/
let lamports_per_signature = 5000;

let message =
new_unchecked_sanitized_message(Message::new(&[], Some(&Pubkey::new_unique())));

let fee_payer_address = message.fee_payer();
let transaction_fee = lamports_per_signature;
let rent_collector = RentCollector::default();
let min_balance = rent_collector.rent.minimum_balance(0);

let fee_payer_account = AccountSharedData::new(min_balance, 0, &Pubkey::default());
let mut mock_accounts = HashMap::new();
mock_accounts.insert(*fee_payer_address, fee_payer_account.clone());

let necessary_balance = min_balance + transaction_fee;
let mut account_overrides = AccountOverrides::default();
let fee_payer_account_override =
AccountSharedData::new(necessary_balance, 0, &Pubkey::default());
account_overrides.set_account(fee_payer_address, Some(fee_payer_account_override));

let mock_bank = MockBankCallback {
account_shared_data: Arc::new(RwLock::new(mock_accounts)),
};

let mut error_counters = TransactionErrorMetrics::default();
let batch_processor = TransactionBatchProcessor::<TestForkGraph>::default();

let result = batch_processor.validate_transaction_fee_payer(
&mock_bank,
Some(&account_overrides),
&message,
CheckedTransactionDetails {
nonce: None,
lamports_per_signature,
},
&FeatureSet::default(),
&FeeStructure::default(),
&rent_collector,
&mut error_counters,
);
assert!(
result.is_ok(),
"test_account_override_used: {:?}",
result.err()
);
}
}

0 comments on commit 777dc56

Please sign in to comment.