Move transaction sanitization earlier in the pipeline (#18655)
* Move transaction sanitization earlier in the pipeline * Renamed HashedTransaction to SanitizedTransaction * Implement deref for sanitized transaction * bring back process_transactions test method * Use sanitized transactions for cost model calculation
This commit is contained in:
@ -1,52 +0,0 @@
|
||||
#![cfg(feature = "full")]
|
||||
|
||||
use crate::{hash::Hash, transaction::Transaction};
|
||||
use std::borrow::Cow;
|
||||
|
||||
/// Transaction and the hash of its message
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct HashedTransaction<'a> {
|
||||
transaction: Cow<'a, Transaction>,
|
||||
pub message_hash: Hash,
|
||||
}
|
||||
|
||||
impl<'a> HashedTransaction<'a> {
|
||||
pub fn new(transaction: Cow<'a, Transaction>, message_hash: Hash) -> Self {
|
||||
Self {
|
||||
transaction,
|
||||
message_hash,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn transaction(&self) -> &Transaction {
|
||||
self.transaction.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<Transaction> for HashedTransaction<'_> {
|
||||
fn from(transaction: Transaction) -> Self {
|
||||
Self {
|
||||
message_hash: transaction.message().hash(),
|
||||
transaction: Cow::Owned(transaction),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a Transaction> for HashedTransaction<'a> {
|
||||
fn from(transaction: &'a Transaction) -> Self {
|
||||
Self {
|
||||
message_hash: transaction.message().hash(),
|
||||
transaction: Cow::Borrowed(transaction),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait HashedTransactionSlice<'a> {
|
||||
fn as_transactions_iter(&'a self) -> Box<dyn Iterator<Item = &'a Transaction> + '_>;
|
||||
}
|
||||
|
||||
impl<'a> HashedTransactionSlice<'a> for [HashedTransaction<'a>] {
|
||||
fn as_transactions_iter(&'a self) -> Box<dyn Iterator<Item = &'a Transaction> + '_> {
|
||||
Box::new(self.iter().map(|h| h.transaction.as_ref()))
|
||||
}
|
||||
}
|
@ -26,7 +26,6 @@ pub mod feature_set;
|
||||
pub mod genesis_config;
|
||||
pub mod hard_forks;
|
||||
pub mod hash;
|
||||
pub mod hashed_transaction;
|
||||
pub mod inflation;
|
||||
pub mod keyed_account;
|
||||
pub mod log;
|
||||
@ -40,6 +39,7 @@ pub mod program_utils;
|
||||
pub mod pubkey;
|
||||
pub mod recent_blockhashes_account;
|
||||
pub mod rpc_port;
|
||||
pub mod sanitized_transaction;
|
||||
pub mod secp256k1_instruction;
|
||||
pub mod shred_version;
|
||||
pub mod signature;
|
||||
|
87
sdk/src/sanitized_transaction.rs
Normal file
87
sdk/src/sanitized_transaction.rs
Normal file
@ -0,0 +1,87 @@
|
||||
#![cfg(feature = "full")]
|
||||
|
||||
use crate::{
|
||||
hash::Hash,
|
||||
sanitize::Sanitize,
|
||||
transaction::{Result, Transaction, TransactionError},
|
||||
};
|
||||
use std::{borrow::Cow, convert::TryFrom, ops::Deref};
|
||||
|
||||
/// Sanitized transaction and the hash of its message
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct SanitizedTransaction<'a> {
|
||||
transaction: Cow<'a, Transaction>,
|
||||
pub message_hash: Hash,
|
||||
}
|
||||
|
||||
impl<'a> SanitizedTransaction<'a> {
|
||||
pub fn try_create(transaction: Cow<'a, Transaction>, message_hash: Hash) -> Result<Self> {
|
||||
transaction.sanitize()?;
|
||||
if Self::has_duplicates(&transaction.message.account_keys) {
|
||||
return Err(TransactionError::AccountLoadedTwice);
|
||||
}
|
||||
|
||||
Ok(Self {
|
||||
transaction,
|
||||
message_hash,
|
||||
})
|
||||
}
|
||||
|
||||
/// Return true if the slice has any duplicate elements
|
||||
pub fn has_duplicates<T: PartialEq>(xs: &[T]) -> bool {
|
||||
// Note: This is an O(n^2) algorithm, but requires no heap allocations. The benchmark
|
||||
// `bench_has_duplicates` in benches/message_processor.rs shows that this implementation is
|
||||
// ~50 times faster than using HashSet for very short slices.
|
||||
for i in 1..xs.len() {
|
||||
#[allow(clippy::integer_arithmetic)]
|
||||
if xs[i..].contains(&xs[i - 1]) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for SanitizedTransaction<'_> {
|
||||
type Target = Transaction;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.transaction
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> TryFrom<Transaction> for SanitizedTransaction<'_> {
|
||||
type Error = TransactionError;
|
||||
fn try_from(transaction: Transaction) -> Result<Self> {
|
||||
let message_hash = transaction.message().hash();
|
||||
Self::try_create(Cow::Owned(transaction), message_hash)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> TryFrom<&'a Transaction> for SanitizedTransaction<'a> {
|
||||
type Error = TransactionError;
|
||||
fn try_from(transaction: &'a Transaction) -> Result<Self> {
|
||||
let message_hash = transaction.message().hash();
|
||||
Self::try_create(Cow::Borrowed(transaction), message_hash)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait SanitizedTransactionSlice<'a> {
|
||||
fn as_transactions_iter(&'a self) -> Box<dyn Iterator<Item = &'a Transaction> + '_>;
|
||||
}
|
||||
|
||||
impl<'a> SanitizedTransactionSlice<'a> for [SanitizedTransaction<'a>] {
|
||||
fn as_transactions_iter(&'a self) -> Box<dyn Iterator<Item = &'a Transaction> + '_> {
|
||||
Box::new(self.iter().map(Deref::deref))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_has_duplicates() {
|
||||
assert!(!SanitizedTransaction::has_duplicates(&[1, 2]));
|
||||
assert!(SanitizedTransaction::has_duplicates(&[1, 2, 1]));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user