reth_network/transactions/
policy.rs

1use crate::transactions::config::{AnnouncementFilteringPolicy, TransactionPropagationPolicy};
2use std::fmt::Debug;
3
4/// A bundle of policies that control the behavior of network components like
5/// the [`TransactionsManager`](super::TransactionsManager).
6///
7/// This trait allows for different collections of policies to be used interchangeably.
8pub trait TransactionPolicies: Send + Sync + Debug + 'static {
9    /// The type of the policy used for transaction propagation.
10    type Propagation: TransactionPropagationPolicy;
11    /// The type of the policy used for filtering transaction announcements.
12    type Announcement: AnnouncementFilteringPolicy;
13
14    /// Returns a reference to the transaction propagation policy.
15    fn propagation_policy(&self) -> &Self::Propagation;
16
17    /// Returns a mutable reference to the transaction propagation policy.
18    fn propagation_policy_mut(&mut self) -> &mut Self::Propagation;
19
20    /// Returns a reference to the announcement filtering policy.
21    fn announcement_filter(&self) -> &Self::Announcement;
22}
23
24/// A container that bundles specific implementations of transaction-related policies,
25///
26/// This struct implements the [`TransactionPolicies`] trait, providing a complete set of
27/// policies required by components like the [`TransactionsManager`](super::TransactionsManager).
28/// It holds a specific [`TransactionPropagationPolicy`] and an
29/// [`AnnouncementFilteringPolicy`].
30#[derive(Debug, Clone, Default)]
31pub struct NetworkPolicies<P, A> {
32    propagation: P,
33    announcement: A,
34}
35
36impl<P, A> NetworkPolicies<P, A> {
37    /// Creates a new bundle of network policies.
38    pub const fn new(propagation: P, announcement: A) -> Self {
39        Self { propagation, announcement }
40    }
41
42    /// Returns a new `NetworkPolicies` bundle with the `TransactionPropagationPolicy` replaced.
43    pub fn with_propagation<NewP>(self, new_propagation: NewP) -> NetworkPolicies<NewP, A>
44    where
45        NewP: TransactionPropagationPolicy,
46    {
47        NetworkPolicies::new(new_propagation, self.announcement)
48    }
49
50    /// Returns a new `NetworkPolicies` bundle with the `AnnouncementFilteringPolicy` replaced.
51    pub fn with_announcement<NewA>(self, new_announcement: NewA) -> NetworkPolicies<P, NewA>
52    where
53        NewA: AnnouncementFilteringPolicy,
54    {
55        NetworkPolicies::new(self.propagation, new_announcement)
56    }
57}
58
59impl<P, A> TransactionPolicies for NetworkPolicies<P, A>
60where
61    P: TransactionPropagationPolicy + Debug,
62    A: AnnouncementFilteringPolicy + Debug,
63{
64    type Propagation = P;
65    type Announcement = A;
66
67    fn propagation_policy(&self) -> &Self::Propagation {
68        &self.propagation
69    }
70
71    fn propagation_policy_mut(&mut self) -> &mut Self::Propagation {
72        &mut self.propagation
73    }
74
75    fn announcement_filter(&self) -> &Self::Announcement {
76        &self.announcement
77    }
78}