Skip to content

Commit

Permalink
fatp: avoid some duplications in update_view_with_mempool
Browse files Browse the repository at this point in the history
  • Loading branch information
michalkucharczyk committed Nov 14, 2024
1 parent b47b174 commit 2d0bbf8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1115,46 +1115,35 @@ where
self.active_views_count()
);
let included_xts = self.extrinsics_included_since_finalized(view.at.hash).await;
let xts = self.mempool.clone_unwatched();

let mut all_submitted_count = 0;
if !xts.is_empty() {
let unwatched_count = xts.len();
let xts_with_src = xts
.into_iter()
.filter(|(hash, _)| !view.pool.validated_pool().pool.read().is_imported(hash))
.filter(|(hash, _)| !included_xts.contains(&hash))
.map(|(_, tx)| (tx.source(), tx.tx()));

let results = view.submit_many(xts_with_src).await;
all_submitted_count = results.len();
log::debug!(target: LOG_TARGET, "update_view_with_mempool: at {:?} unwatched {}/{}", view.at.hash, all_submitted_count, unwatched_count);
}

let watched_xts_filtered = watched_xts
let (hashes, xts_filtered): (Vec<_>, Vec<_>) = watched_xts
.into_iter()
.filter(|(hash, _)| !view.pool.validated_pool().pool.read().is_imported(hash))
.chain(self.mempool.clone_unwatched().into_iter())
.filter(|(hash, _)| !view.is_imported(hash))
.filter(|(hash, _)| !included_xts.contains(&hash))
.map(|(tx_hash, tx)| (tx_hash, tx.source(), tx.tx()))
.collect::<Vec<_>>();

let watched_submitted_count = watched_xts_filtered.len();
.map(|(tx_hash, tx)| (tx_hash, (tx.source(), tx.tx())))
.unzip();

let hashes = watched_xts_filtered.iter().map(|i| i.0).collect::<Vec<_>>();
let watched_results = view
.submit_many(watched_xts_filtered.into_iter().map(|i| (i.1, i.2)))
.submit_many(xts_filtered)
.await
.into_iter()
.zip(hashes)
.map(|(result, tx_hash)| result.or_else(|_| Err(tx_hash)))
.collect::<Vec<_>>();

log::debug!(target: LOG_TARGET, "update_view_with_mempool: at {:?} watched {}/{}", view.at.hash, watched_submitted_count, self.mempool_len().1);
let submitted_count = watched_results.len();

all_submitted_count += watched_submitted_count;
let _ = all_submitted_count
.try_into()
.map(|v| self.metrics.report(|metrics| metrics.submitted_from_mempool_txs.inc_by(v)));
log::debug!(
target: LOG_TARGET,
"update_view_with_mempool: at {:?} submitted {}/{}",
view.at.hash,
submitted_count,
self.mempool.len()
);

self.metrics
.report(|metrics| metrics.submitted_from_mempool_txs.inc_by(submitted_count as _));

// if there are no views yet, and a single newly created view is reporting error, just send
// out the invalid event, and remove transaction.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,11 @@ where
(transactions.len() - watched_count, watched_count)
}

/// Returns a total number of transactions kept withing mempool.
pub fn len(&self) -> usize {
self.transactions.read().len()
}

/// Returns the number of bytes used by all extrinsics in the the pool.
#[cfg(test)]
pub fn bytes(&self) -> usize {
Expand Down

0 comments on commit 2d0bbf8

Please sign in to comment.