1
- use crate :: { BlobberError , BlobberResult , Blobs , FetchResult } ;
1
+ use crate :: { BlobFetcher , BlobberError , BlobberResult , Blobs , FetchResult } ;
2
2
use alloy:: consensus:: { SidecarCoder , SimpleCoder , Transaction as _} ;
3
3
use alloy:: eips:: eip7691:: MAX_BLOBS_PER_BLOCK_ELECTRA ;
4
4
use alloy:: eips:: merge:: EPOCH_SLOTS ;
5
5
use alloy:: primitives:: { B256 , Bytes , keccak256} ;
6
+ use core:: fmt;
6
7
use reth:: transaction_pool:: TransactionPool ;
7
8
use reth:: { network:: cache:: LruMap , primitives:: Receipt } ;
8
9
use signet_extract:: ExtractedEvent ;
9
10
use signet_zenith:: Zenith :: BlockSubmitted ;
10
11
use signet_zenith:: ZenithBlock ;
12
+ use std:: marker:: PhantomData ;
11
13
use std:: {
12
14
sync:: { Arc , Mutex } ,
13
15
time:: Duration ,
@@ -37,11 +39,13 @@ enum CacheInst {
37
39
38
40
/// Handle for the cache.
39
41
#[ derive( Debug , Clone ) ]
40
- pub struct CacheHandle {
42
+ pub struct CacheHandle < Coder = SimpleCoder > {
41
43
sender : mpsc:: Sender < CacheInst > ,
44
+
45
+ _coder : PhantomData < Coder > ,
42
46
}
43
47
44
- impl CacheHandle {
48
+ impl < Coder > CacheHandle < Coder > {
45
49
/// Sends a cache instruction.
46
50
async fn send ( & self , inst : CacheInst ) {
47
51
let _ = self . sender . send ( inst) . await ;
@@ -71,12 +75,14 @@ impl CacheHandle {
71
75
72
76
/// Fetch the blobs using [`Self::fetch_blobs`] and decode them to get the
73
77
/// Zenith block data using the provided coder.
74
- pub async fn fetch_and_decode_with_coder < C : SidecarCoder > (
78
+ pub async fn fetch_and_decode (
75
79
& self ,
76
80
slot : usize ,
77
81
extract : & ExtractedEvent < ' _ , Receipt , BlockSubmitted > ,
78
- mut coder : C ,
79
- ) -> BlobberResult < Bytes > {
82
+ ) -> BlobberResult < Bytes >
83
+ where
84
+ Coder : SidecarCoder + Default ,
85
+ {
80
86
let tx_hash = extract. tx_hash ( ) ;
81
87
let versioned_hashes = extract
82
88
. tx
@@ -87,23 +93,13 @@ impl CacheHandle {
87
93
88
94
let blobs = self . fetch_blobs ( slot, tx_hash, versioned_hashes. to_owned ( ) ) . await ?;
89
95
90
- coder
96
+ Coder :: default ( )
91
97
. decode_all ( blobs. as_ref ( ) )
92
98
. ok_or_else ( BlobberError :: blob_decode_error) ?
93
99
. into_iter ( )
94
100
. find ( |data| keccak256 ( data) == extract. block_data_hash ( ) )
95
101
. map ( Into :: into)
96
- . ok_or_else ( || BlobberError :: block_data_not_found ( tx_hash) )
97
- }
98
-
99
- /// Fetch the blobs using [`Self::fetch_blobs`] and decode them using
100
- /// [`SimpleCoder`] to get the Zenith block data.
101
- pub async fn fech_and_decode (
102
- & self ,
103
- slot : usize ,
104
- extract : & ExtractedEvent < ' _ , Receipt , BlockSubmitted > ,
105
- ) -> BlobberResult < Bytes > {
106
- self . fetch_and_decode_with_coder ( slot, extract, SimpleCoder :: default ( ) ) . await
102
+ . ok_or_else ( || BlobberError :: block_data_not_found ( extract. block_data_hash ( ) ) )
107
103
}
108
104
109
105
/// Fetch the blobs, decode them using the provided coder, and construct a
@@ -117,15 +113,17 @@ impl CacheHandle {
117
113
/// decoded (e.g., due to a malformatted blob).
118
114
/// - `Err(FetchError)` if there was an unrecoverable error fetching the
119
115
/// blobs.
120
- pub async fn signet_block_with_coder < C : SidecarCoder > (
116
+ pub async fn signet_block (
121
117
& self ,
122
118
host_block_number : u64 ,
123
119
slot : usize ,
124
120
extract : & ExtractedEvent < ' _ , Receipt , BlockSubmitted > ,
125
- coder : C ,
126
- ) -> FetchResult < ZenithBlock > {
121
+ ) -> FetchResult < ZenithBlock >
122
+ where
123
+ Coder : SidecarCoder + Default ,
124
+ {
127
125
let header = extract. ru_header ( host_block_number) ;
128
- let block_data = match self . fetch_and_decode_with_coder ( slot, extract, coder ) . await {
126
+ let block_data = match self . fetch_and_decode ( slot, extract) . await {
129
127
Ok ( buf) => buf,
130
128
Err ( BlobberError :: Decode ( _) ) => {
131
129
trace ! ( "Failed to decode block data" ) ;
@@ -135,44 +133,24 @@ impl CacheHandle {
135
133
} ;
136
134
Ok ( ZenithBlock :: from_header_and_data ( header, block_data) )
137
135
}
138
-
139
- /// Fetch the blobs, decode them using [`SimpleCoder`], and construct a
140
- /// Zenith block from the header and data.
141
- ///
142
- /// # Returns
143
- ///
144
- /// - `Ok(ZenithBlock)` if the block was successfully fetched and
145
- /// decoded.
146
- /// - `Ok(ZenithBlock)` with an EMPTY BLOCK if the block_data could not be
147
- /// decoded (e.g., due to a malformatted blob).
148
- /// - `Err(FetchError)` if there was an unrecoverable error fetching the
149
- /// blobs.
150
- pub async fn signet_block (
151
- & self ,
152
- host_block_number : u64 ,
153
- slot : usize ,
154
- extract : & ExtractedEvent < ' _ , Receipt , BlockSubmitted > ,
155
- ) -> FetchResult < ZenithBlock > {
156
- self . signet_block_with_coder ( host_block_number, slot, extract, SimpleCoder :: default ( ) ) . await
157
- }
158
136
}
159
137
160
138
/// Retrieves blobs and stores them in a cache for later use.
161
139
pub struct BlobCacher < Pool > {
162
- fetcher : crate :: BlobFetcher < Pool > ,
140
+ fetcher : BlobFetcher < Pool > ,
163
141
164
142
cache : Mutex < LruMap < ( usize , B256 ) , Blobs > > ,
165
143
}
166
144
167
- impl < Pool : core :: fmt:: Debug > core :: fmt:: Debug for BlobCacher < Pool > {
168
- fn fmt ( & self , f : & mut core :: fmt:: Formatter < ' _ > ) -> core :: fmt:: Result {
145
+ impl < Pool : fmt:: Debug > fmt:: Debug for BlobCacher < Pool > {
146
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
169
147
f. debug_struct ( "BlobCacher" ) . field ( "fetcher" , & self . fetcher ) . finish_non_exhaustive ( )
170
148
}
171
149
}
172
150
173
151
impl < Pool : TransactionPool + ' static > BlobCacher < Pool > {
174
152
/// Creates a new `BlobCacher` with the provided extractor and cache size.
175
- pub fn new ( fetcher : crate :: BlobFetcher < Pool > ) -> Self {
153
+ pub fn new ( fetcher : BlobFetcher < Pool > ) -> Self {
176
154
Self { fetcher, cache : LruMap :: new ( BLOB_CACHE_SIZE ) . into ( ) }
177
155
}
178
156
@@ -237,10 +215,10 @@ impl<Pool: TransactionPool + 'static> BlobCacher<Pool> {
237
215
///
238
216
/// # Panics
239
217
/// This function will panic if the cache task fails to spawn.
240
- pub fn spawn ( self ) -> CacheHandle {
218
+ pub fn spawn < C : SidecarCoder + Default > ( self ) -> CacheHandle < C > {
241
219
let ( sender, inst) = mpsc:: channel ( CACHE_REQUEST_CHANNEL_SIZE ) ;
242
220
tokio:: spawn ( Arc :: new ( self ) . task_future ( inst) ) ;
243
- CacheHandle { sender }
221
+ CacheHandle { sender, _coder : PhantomData }
244
222
}
245
223
}
246
224
@@ -256,7 +234,6 @@ mod tests {
256
234
rlp:: encode,
257
235
signers:: { SignerSync , local:: PrivateKeySigner } ,
258
236
} ;
259
- use init4_bin_base:: utils:: calc:: SlotCalculator ;
260
237
use reth:: primitives:: Transaction ;
261
238
use reth_transaction_pool:: {
262
239
PoolTransaction , TransactionOrigin ,
@@ -272,7 +249,6 @@ mod tests {
272
249
let test = signet_constants:: KnownChains :: Test ;
273
250
274
251
let constants: SignetSystemConstants = test. try_into ( ) . unwrap ( ) ;
275
- let calc = SlotCalculator :: new ( 0 , 0 , 12 ) ;
276
252
277
253
let explorer_url = "https://api.holesky.blobscan.com/" ;
278
254
let client = reqwest:: Client :: builder ( ) . use_rustls_tls ( ) ;
@@ -308,9 +284,8 @@ mod tests {
308
284
. with_explorer_url ( explorer_url)
309
285
. with_client_builder ( client)
310
286
. unwrap ( )
311
- . with_slot_calculator ( calc)
312
287
. build_cache ( ) ?;
313
- let handle = cache. spawn ( ) ;
288
+ let handle = cache. spawn :: < SimpleCoder > ( ) ;
314
289
315
290
let got = handle
316
291
. fetch_blobs (
0 commit comments