mirror of
https://github.com/paradigmxyz/reth.git
synced 2026-04-30 03:01:58 -04:00
refactor(net): add methods to PropagatedTransactions instead of exposing .0 (#22441)
Co-authored-by: Amp <amp@ampcode.com> Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
@@ -904,7 +904,7 @@ where
|
||||
// send hashes if any
|
||||
if let Some(new_pooled_hashes) = pooled {
|
||||
for hash in new_pooled_hashes.iter_hashes().copied() {
|
||||
propagated.0.entry(hash).or_default().push(PropagateKind::Hash(peer_id));
|
||||
propagated.record(hash, PropagateKind::Hash(peer_id));
|
||||
// mark transaction as seen by peer
|
||||
peer.seen_transactions.insert(hash);
|
||||
}
|
||||
@@ -916,7 +916,7 @@ where
|
||||
// send full transactions, if any
|
||||
if let Some(new_full_transactions) = full {
|
||||
for tx in &new_full_transactions {
|
||||
propagated.0.entry(*tx.tx_hash()).or_default().push(PropagateKind::Full(peer_id));
|
||||
propagated.record(*tx.tx_hash(), PropagateKind::Full(peer_id));
|
||||
// mark transaction as seen by peer
|
||||
peer.seen_transactions.insert(*tx.tx_hash());
|
||||
}
|
||||
@@ -926,7 +926,7 @@ where
|
||||
}
|
||||
|
||||
// Update propagated transactions metrics
|
||||
self.metrics.propagated_transactions.increment(propagated.0.len() as u64);
|
||||
self.metrics.propagated_transactions.increment(propagated.len() as u64);
|
||||
|
||||
Some(propagated)
|
||||
}
|
||||
@@ -977,7 +977,7 @@ where
|
||||
}
|
||||
|
||||
for hash in new_pooled_hashes.iter_hashes().copied() {
|
||||
propagated.0.entry(hash).or_default().push(PropagateKind::Hash(peer_id));
|
||||
propagated.record(hash, PropagateKind::Hash(peer_id));
|
||||
}
|
||||
|
||||
trace!(target: "net::tx::propagation", ?peer_id, ?new_pooled_hashes, "Propagating transactions to peer");
|
||||
@@ -986,7 +986,7 @@ where
|
||||
self.network.send_transactions_hashes(peer_id, new_pooled_hashes);
|
||||
|
||||
// Update propagated transactions metrics
|
||||
self.metrics.propagated_transactions.increment(propagated.0.len() as u64);
|
||||
self.metrics.propagated_transactions.increment(propagated.len() as u64);
|
||||
|
||||
propagated
|
||||
};
|
||||
@@ -1057,7 +1057,7 @@ where
|
||||
.truncate(SOFT_LIMIT_COUNT_HASHES_IN_NEW_POOLED_TRANSACTIONS_BROADCAST_MESSAGE);
|
||||
|
||||
for hash in new_pooled_hashes.iter_hashes().copied() {
|
||||
propagated.0.entry(hash).or_default().push(PropagateKind::Hash(*peer_id));
|
||||
propagated.record(hash, PropagateKind::Hash(*peer_id));
|
||||
// mark transaction as seen by peer
|
||||
peer.seen_transactions.insert(hash);
|
||||
}
|
||||
@@ -1071,11 +1071,7 @@ where
|
||||
// send full transactions, if any
|
||||
if let Some(new_full_transactions) = full {
|
||||
for tx in &new_full_transactions {
|
||||
propagated
|
||||
.0
|
||||
.entry(*tx.tx_hash())
|
||||
.or_default()
|
||||
.push(PropagateKind::Full(*peer_id));
|
||||
propagated.record(*tx.tx_hash(), PropagateKind::Full(*peer_id));
|
||||
// mark transaction as seen by peer
|
||||
peer.seen_transactions.insert(*tx.tx_hash());
|
||||
}
|
||||
@@ -1088,7 +1084,7 @@ where
|
||||
}
|
||||
|
||||
// Update propagated transactions metrics
|
||||
self.metrics.propagated_transactions.increment(propagated.0.len() as u64);
|
||||
self.metrics.propagated_transactions.increment(propagated.len() as u64);
|
||||
|
||||
propagated
|
||||
}
|
||||
@@ -2911,12 +2907,12 @@ mod tests {
|
||||
|
||||
let propagated =
|
||||
tx_manager.propagate_transactions(propagate.clone(), PropagationMode::Basic);
|
||||
assert_eq!(propagated.0.len(), 2);
|
||||
let prop_txs = propagated.0.get(eip1559_tx.transaction.hash()).unwrap();
|
||||
assert_eq!(propagated.len(), 2);
|
||||
let prop_txs = propagated.get(eip1559_tx.transaction.hash()).unwrap();
|
||||
assert_eq!(prop_txs.len(), 1);
|
||||
assert!(prop_txs[0].is_full());
|
||||
|
||||
let prop_txs = propagated.0.get(eip4844_tx.transaction.hash()).unwrap();
|
||||
let prop_txs = propagated.get(eip4844_tx.transaction.hash()).unwrap();
|
||||
assert_eq!(prop_txs.len(), 1);
|
||||
assert!(prop_txs[0].is_hash());
|
||||
|
||||
@@ -2927,7 +2923,7 @@ mod tests {
|
||||
|
||||
// propagate again
|
||||
let propagated = tx_manager.propagate_transactions(propagate, PropagationMode::Basic);
|
||||
assert!(propagated.0.is_empty());
|
||||
assert!(propagated.is_empty());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
|
||||
@@ -1239,11 +1239,11 @@ where
|
||||
|
||||
/// Notify about propagated transactions.
|
||||
pub fn on_propagated(&self, txs: PropagatedTransactions) {
|
||||
if txs.0.is_empty() {
|
||||
if txs.is_empty() {
|
||||
return
|
||||
}
|
||||
self.with_event_listener(|listener| {
|
||||
txs.0.into_iter().for_each(|(hash, peers)| listener.propagated(&hash, peers));
|
||||
txs.into_iter().for_each(|(hash, peers)| listener.propagated(&hash, peers));
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -823,6 +823,37 @@ impl<T: PoolTransaction> IntoIterator for AllPoolTransactions<T> {
|
||||
#[derive(Debug, Clone, Eq, PartialEq, Default)]
|
||||
pub struct PropagatedTransactions(pub HashMap<TxHash, Vec<PropagateKind>>);
|
||||
|
||||
impl PropagatedTransactions {
|
||||
/// Records a propagation of a transaction to a peer.
|
||||
pub fn record(&mut self, hash: TxHash, kind: PropagateKind) {
|
||||
self.0.entry(hash).or_default().push(kind);
|
||||
}
|
||||
|
||||
/// Returns the number of distinct transactions that were propagated.
|
||||
pub fn len(&self) -> usize {
|
||||
self.0.len()
|
||||
}
|
||||
|
||||
/// Returns true if no transactions were propagated.
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.0.is_empty()
|
||||
}
|
||||
|
||||
/// Returns the propagation info for a specific transaction.
|
||||
pub fn get(&self, hash: &TxHash) -> Option<&[PropagateKind]> {
|
||||
self.0.get(hash).map(Vec::as_slice)
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoIterator for PropagatedTransactions {
|
||||
type Item = (TxHash, Vec<PropagateKind>);
|
||||
type IntoIter = std::collections::hash_map::IntoIter<TxHash, Vec<PropagateKind>>;
|
||||
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
self.0.into_iter()
|
||||
}
|
||||
}
|
||||
|
||||
/// Represents how a transaction was propagated over the network.
|
||||
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
|
||||
@@ -994,7 +994,7 @@ fn propagate_transactions(
|
||||
if !full.is_empty() {
|
||||
if idx > max_num_full {
|
||||
for hash in &hashes {
|
||||
propagated.0.entry(*hash).or_default().push(PropagateKind::Hash(*peer_id));
|
||||
propagated.record(*hash, PropagateKind::Hash(*peer_id));
|
||||
}
|
||||
// send hashes of transactions
|
||||
self.network.send_transactions_hashes(*peer_id, hashes);
|
||||
@@ -1003,7 +1003,7 @@ fn propagate_transactions(
|
||||
self.network.send_transactions(*peer_id, full);
|
||||
|
||||
for hash in hashes {
|
||||
propagated.0.entry(hash).or_default().push(PropagateKind::Full(*peer_id));
|
||||
propagated.record(hash, PropagateKind::Full(*peer_id));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user