Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use HashSet for transactions_by_peers #6893

Merged
merged 2 commits into from
Feb 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions crates/net/network/src/transactions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ pub struct TransactionsManager<Pool> {
///
/// This way we can track incoming transactions and prevent multiple pool imports for the same
/// transaction
transactions_by_peers: HashMap<TxHash, Vec<PeerId>>,
transactions_by_peers: HashMap<TxHash, HashSet<PeerId>>,
/// Transactions that are currently imported into the `Pool`.
///
/// The import process includes:
Expand Down Expand Up @@ -1038,15 +1038,15 @@ where
match self.transactions_by_peers.entry(*tx.hash()) {
Entry::Occupied(mut entry) => {
// transaction was already inserted
entry.get_mut().push(peer_id);
entry.get_mut().insert(peer_id);
}
Entry::Vacant(entry) => {
if !self.bad_imports.contains(tx.hash()) {
// this is a new transaction that should be imported into the pool
let pool_transaction = <Pool::Transaction as FromRecoveredPooledTransaction>::from_recovered_pooled_transaction(tx);
new_txs.push(pool_transaction);

entry.insert(vec![peer_id]);
entry.insert(HashSet::from([peer_id]));
} else {
trace!(target: "net::tx",
peer_id=format!("{peer_id:#}"),
Expand Down Expand Up @@ -1155,7 +1155,7 @@ where
self.bad_imports.insert(hash);
}

/// Returns `true` if [`TransactionsManager`] has capacity to request pending hashes. Returns
/// Returns `true` if [`TransactionsManager`] has capacity to request pending hashes. Returns
/// `false` if [`TransactionsManager`] is operating close to full capacity.
fn has_capacity_for_fetching_pending_hashes(&self) -> bool {
self.pending_pool_imports_info
Expand Down Expand Up @@ -1905,10 +1905,11 @@ mod tests {
peer_id: *handle1.peer_id(),
msg: Transactions(vec![signed_tx.clone()]),
});
assert_eq!(
*handle1.peer_id(),
transactions.transactions_by_peers.get(&signed_tx.hash()).unwrap()[0]
);
assert!(transactions
.transactions_by_peers
.get(&signed_tx.hash())
.unwrap()
.contains(handle1.peer_id()));

// advance the transaction manager future
poll_fn(|cx| {
Expand Down
Loading