reth_network/transactions/
policy.rs

1use crate::transactions::config::{AnnouncementFilteringPolicy, TransactionPropagationPolicy};
2use reth_eth_wire::NetworkPrimitives;
3use std::fmt::Debug;
4
5/// A container that bundles specific implementations of transaction-related policies,
6///
7/// This struct provides a complete set of policies required by components like the
8/// [`TransactionsManager`](super::TransactionsManager). It holds a specific
9/// [`TransactionPropagationPolicy`] and an [`AnnouncementFilteringPolicy`].
10#[derive(Debug)]
11pub struct NetworkPolicies<N: NetworkPrimitives> {
12    propagation: Box<dyn TransactionPropagationPolicy<N>>,
13    announcement: Box<dyn AnnouncementFilteringPolicy<N>>,
14}
15
16impl<N: NetworkPrimitives> NetworkPolicies<N> {
17    /// Creates a new bundle of network policies.
18    pub fn new(
19        propagation: impl TransactionPropagationPolicy<N>,
20        announcement: impl AnnouncementFilteringPolicy<N>,
21    ) -> Self {
22        Self { propagation: Box::new(propagation), announcement: Box::new(announcement) }
23    }
24
25    /// Returns a new `NetworkPolicies` bundle with the `TransactionPropagationPolicy` replaced.
26    pub fn with_propagation(self, new_propagation: impl TransactionPropagationPolicy<N>) -> Self {
27        Self { propagation: Box::new(new_propagation), announcement: self.announcement }
28    }
29
30    /// Returns a new `NetworkPolicies` bundle with the `AnnouncementFilteringPolicy` replaced.
31    pub fn with_announcement(self, new_announcement: impl AnnouncementFilteringPolicy<N>) -> Self {
32        Self { propagation: self.propagation, announcement: Box::new(new_announcement) }
33    }
34
35    /// Returns a reference to the transaction propagation policy.
36    pub fn propagation_policy(&self) -> &dyn TransactionPropagationPolicy<N> {
37        &*self.propagation
38    }
39
40    /// Returns a mutable reference to the transaction propagation policy.
41    pub fn propagation_policy_mut(&mut self) -> &mut dyn TransactionPropagationPolicy<N> {
42        &mut *self.propagation
43    }
44
45    /// Returns a reference to the announcement filtering policy.
46    pub fn announcement_filter(&self) -> &dyn AnnouncementFilteringPolicy<N> {
47        &*self.announcement
48    }
49}