1
- //! Bundler service responsible for managing bundles.
2
- use std:: sync:: Arc ;
3
-
4
- use super :: oauth:: Authenticator ;
5
-
1
+ //! Bundler service responsible for fetching bundles and sending them to the simulator.
6
2
pub use crate :: config:: BuilderConfig ;
7
-
3
+ use crate :: tasks :: oauth :: Authenticator ;
8
4
use oauth2:: TokenResponse ;
9
5
use reqwest:: Url ;
10
6
use serde:: { Deserialize , Serialize } ;
11
7
use tokio:: sync:: mpsc:: { unbounded_channel, UnboundedReceiver } ;
12
8
use tokio:: task:: JoinHandle ;
13
9
use zenith_types:: ZenithEthBundle ;
14
10
15
- /// Holds a Signet bundle from the cache that has a unique identifier
16
- /// and a Zenith bundle
11
+ /// Holds a bundle from the cache with a unique ID and a Zenith bundle.
17
12
#[ derive( Debug , Clone , Serialize , Deserialize ) ]
18
13
pub struct Bundle {
19
14
/// Cache identifier for the bundle
@@ -22,38 +17,37 @@ pub struct Bundle {
22
17
pub bundle : ZenithEthBundle ,
23
18
}
24
19
25
- impl PartialEq for Bundle {
26
- fn eq ( & self , other : & Self ) -> bool {
27
- self . id == other. id
28
- }
29
- }
30
-
31
- impl Eq for Bundle { }
32
-
33
20
/// Response from the tx-pool containing a list of bundles.
34
21
#[ derive( Debug , Clone , Serialize , Deserialize ) ]
35
22
pub struct TxPoolBundleResponse {
36
- /// Bundle responses are availabel on the bundles property
23
+ /// Bundle responses are available on the bundles property.
37
24
pub bundles : Vec < Bundle > ,
38
25
}
39
26
40
- /// The BundlePoller polls the tx-pool for bundles and manages the seen bundles .
27
+ /// The BundlePoller polls the tx-pool for bundles.
41
28
#[ derive( Debug , Clone ) ]
42
29
pub struct BundlePoller {
43
30
/// The builder configuration values.
44
31
pub config : BuilderConfig ,
45
32
/// Authentication module that periodically fetches and stores auth tokens.
46
33
pub authenticator : Authenticator ,
34
+ /// Defines the interval at which the bundler polls the tx-pool for bundles.
35
+ pub poll_interval_ms : u64 ,
47
36
}
48
37
49
- /// Implements a poller for the block builder to pull bundles from the tx cache .
38
+ /// Implements a poller for the block builder to pull bundles from the tx-pool .
50
39
impl BundlePoller {
51
40
/// Creates a new BundlePoller from the provided builder config.
52
41
pub fn new ( config : & BuilderConfig , authenticator : Authenticator ) -> Self {
53
- Self { config : config. clone ( ) , authenticator }
42
+ Self { config : config. clone ( ) , authenticator, poll_interval_ms : 1000 }
54
43
}
55
44
56
- /// Fetches bundles from the transaction cache and returns the (oldest? random?) bundle in the cache.
45
+ /// Creates a new BundlePoller from the provided builder config and with the specified poll interval in ms.
46
+ pub fn new_with_poll_interval_ms ( config : & BuilderConfig , authenticator : Authenticator ) -> Self {
47
+ Self { config : config. clone ( ) , authenticator, poll_interval_ms : 1000 }
48
+ }
49
+
50
+ /// Fetches bundles from the transaction cache and returns them.
57
51
pub async fn check_bundle_cache ( & mut self ) -> eyre:: Result < Vec < Bundle > > {
58
52
let bundle_url: Url = Url :: parse ( & self . config . tx_pool_url ) ?. join ( "bundles" ) ?;
59
53
let token = self . authenticator . fetch_oauth_token ( ) . await ?;
@@ -71,23 +65,31 @@ impl BundlePoller {
71
65
Ok ( resp. bundles )
72
66
}
73
67
74
- /// Spawns a task that simply sends out any bundles it ever finds
75
- pub fn spawn ( mut self ) -> ( UnboundedReceiver < Arc < Bundle > > , JoinHandle < ( ) > ) {
68
+ /// Spawns a task that sends bundles it finds to its channel sender.
69
+ pub fn spawn ( mut self ) -> ( UnboundedReceiver < Bundle > , JoinHandle < ( ) > ) {
76
70
let ( outbound, inbound) = unbounded_channel ( ) ;
77
71
let jh = tokio:: spawn ( async move {
78
72
loop {
79
73
if let Ok ( bundles) = self . check_bundle_cache ( ) . await {
80
74
tracing:: debug!( count = ?bundles. len( ) , "found bundles" ) ;
81
- for bundle in bundles. iter ( ) {
82
- if let Err ( err) = outbound. send ( Arc :: new ( bundle. clone ( ) ) ) {
83
- tracing:: error!( err = ?err, "Failed to send bundle" ) ;
75
+ for bundle in bundles. into_iter ( ) {
76
+ if let Err ( err) = outbound. send ( bundle) {
77
+ tracing:: error!( err = ?err, "Failed to send bundle - channel full " ) ;
84
78
}
85
79
}
86
80
}
87
- tokio:: time:: sleep ( tokio:: time:: Duration :: from_secs ( 1 ) ) . await ;
81
+ tokio:: time:: sleep ( tokio:: time:: Duration :: from_millis ( self . poll_interval_ms ) ) . await ;
88
82
}
89
83
} ) ;
90
84
91
85
( inbound, jh)
92
86
}
93
87
}
88
+
89
+ impl PartialEq for Bundle {
90
+ fn eq ( & self , other : & Self ) -> bool {
91
+ self . id == other. id
92
+ }
93
+ }
94
+
95
+ impl Eq for Bundle { }
0 commit comments