feat: add append_pooled_transaction_elements (#20654)

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
Karl Yu
2025-12-30 05:00:40 +08:00
committed by GitHub
parent b7883953c4
commit d564d9ba36
4 changed files with 65 additions and 19 deletions

View File

@@ -554,6 +554,15 @@ where
self.pool.get_pooled_transaction_elements(tx_hashes, limit)
}
fn append_pooled_transaction_elements(
&self,
tx_hashes: &[TxHash],
limit: GetPooledTransactionLimit,
out: &mut Vec<<<V as TransactionValidator>::Transaction as PoolTransaction>::Pooled>,
) {
self.pool.append_pooled_transaction_elements(tx_hashes, limit, out)
}
fn get_pooled_transaction_element(
&self,
tx_hash: TxHash,

View File

@@ -155,6 +155,14 @@ impl<T: EthPoolTransaction> TransactionPool for NoopTransactionPool<T> {
vec![]
}
fn append_pooled_transaction_elements(
&self,
_tx_hashes: &[TxHash],
_limit: GetPooledTransactionLimit,
_out: &mut Vec<<Self::Transaction as PoolTransaction>::Pooled>,
) {
}
fn get_pooled_transaction_element(
&self,
_tx_hash: TxHash,

View File

@@ -328,6 +328,33 @@ where
);
}
/// Extends the given vector with pooled transactions for the given hashes that are allowed to
/// be propagated.
pub fn append_pooled_transaction_elements(
&self,
tx_hashes: &[TxHash],
limit: GetPooledTransactionLimit,
out: &mut Vec<<<V as TransactionValidator>::Transaction as PoolTransaction>::Pooled>,
) where
<V as TransactionValidator>::Transaction: EthPoolTransaction,
{
let transactions = self.get_all_propagatable(tx_hashes);
let mut size = 0;
for transaction in transactions {
let encoded_len = transaction.encoded_length();
let Some(pooled) = self.to_pooled_transaction(transaction) else {
continue;
};
size += encoded_len;
out.push(pooled.into_inner());
if limit.exceeds(size) {
break
}
}
}
/// Extends the given vector with the hashes of all transactions in the pool that can be
/// propagated.
pub fn append_pooled_transactions_hashes(&self, out: &mut Vec<TxHash>) {
@@ -409,23 +436,9 @@ where
where
<V as TransactionValidator>::Transaction: EthPoolTransaction,
{
let transactions = self.get_all_propagatable(tx_hashes);
let mut elements = Vec::with_capacity(transactions.len());
let mut size = 0;
for transaction in transactions {
let encoded_len = transaction.encoded_length();
let Some(pooled) = self.to_pooled_transaction(transaction) else {
continue;
};
size += encoded_len;
elements.push(pooled.into_inner());
if limit.exceeds(size) {
break
}
}
let mut elements = Vec::new();
self.append_pooled_transaction_elements(&tx_hashes, limit, &mut elements);
elements.shrink_to_fit();
elements
}
@@ -1133,12 +1146,13 @@ where
/// If no transaction exists, it is skipped.
fn get_all_propagatable(
&self,
txs: Vec<TxHash>,
txs: &[TxHash],
) -> Vec<Arc<ValidPoolTransaction<T::Transaction>>> {
if txs.is_empty() {
return Vec::new()
}
self.get_pool_data().get_all(txs).filter(|tx| tx.propagate).collect()
let pool = self.get_pool_data();
txs.iter().filter_map(|tx| pool.get(tx).filter(|tx| tx.propagate)).collect()
}
/// Notify about propagated transactions.

View File

@@ -346,6 +346,21 @@ pub trait TransactionPool: Clone + Debug + Send + Sync {
limit: GetPooledTransactionLimit,
) -> Vec<<Self::Transaction as PoolTransaction>::Pooled>;
/// Extends the given vector with pooled transactions for the given hashes that are allowed to
/// be propagated.
///
/// This adheres to the expected behavior of [`Self::get_pooled_transaction_elements`].
///
/// Consumer: P2P
fn append_pooled_transaction_elements(
&self,
tx_hashes: &[TxHash],
limit: GetPooledTransactionLimit,
out: &mut Vec<<Self::Transaction as PoolTransaction>::Pooled>,
) {
out.extend(self.get_pooled_transaction_elements(tx_hashes.to_vec(), limit));
}
/// Returns the pooled transaction variant for the given transaction hash.
///
/// This adheres to the expected behavior of