1
- use std:: { path :: Path , sync:: Arc , time:: Instant } ;
1
+ use std:: { sync:: Arc , time:: Instant } ;
2
2
3
3
use anyhow:: Result ;
4
- use bytes:: Bytes ;
5
4
use clap:: Parser ;
6
5
use fuel_data_parser:: DataEncoder ;
7
6
use fuel_streams_domains:: {
@@ -11,9 +10,8 @@ use fuel_streams_domains::{
11
10
} ;
12
11
use fuel_streams_types:: BlockHeight ;
13
12
use fuel_web_utils:: { shutdown:: ShutdownController , tracing:: init_tracing} ;
14
- use surrealkv:: { Options , Store } ;
15
13
use sv_dune:: {
16
- helpers:: BatchCalculator ,
14
+ helpers:: { BatchCalculator , Store } ,
17
15
processor:: BlocksProcessor ,
18
16
Cli ,
19
17
DuneError ,
@@ -32,7 +30,7 @@ async fn main() -> Result<()> {
32
30
33
31
let opts = QueryOptions :: default ( ) ;
34
32
let last_height_indexed = Block :: find_last_block_height ( & db, & opts) . await ?;
35
- let last_height_saved = get_last_block_saved ( & store ) . await ?;
33
+ let last_height_saved = store . get_last_block_saved ( ) . await ?;
36
34
if * last_height_saved >= * last_height_indexed {
37
35
tracing:: info!( "Last block saved is up to date" ) ;
38
36
return Ok ( ( ) ) ;
@@ -71,19 +69,7 @@ async fn setup_db(db_url: &str) -> Result<Arc<Db>, DuneError> {
71
69
}
72
70
73
71
fn setup_store ( cli : & Cli ) -> Result < Store > {
74
- let mut opts = Options :: new ( ) ;
75
- let storage_dir = match cli. storage_file_dir . as_ref ( ) {
76
- Some ( file_dir) => Path :: new ( file_dir) . to_path_buf ( ) ,
77
- None => {
78
- let manifest_dir = env ! ( "CARGO_MANIFEST_DIR" ) ;
79
- let manifest_dir = Path :: new ( manifest_dir) . join ( "output/storage" ) ;
80
- manifest_dir. to_path_buf ( )
81
- }
82
- } ;
83
- opts. dir = storage_dir. to_owned ( ) ;
84
- opts. disk_persistence = true ;
85
- let store = Store :: new ( opts) ?;
86
- Ok ( store)
72
+ Store :: new ( cli. storage_file_dir . as_deref ( ) )
87
73
}
88
74
89
75
async fn process_blocks (
@@ -112,6 +98,18 @@ async fn process_blocks(
112
98
break ;
113
99
}
114
100
101
+ // Check if we should continue processing based on max_blocks
102
+ if !store
103
+ . should_continue_processing ( cli. max_blocks_to_store )
104
+ . await ?
105
+ {
106
+ tracing:: info!(
107
+ "Reached maximum blocks to store ({}), stopping processing" ,
108
+ cli. max_blocks_to_store. unwrap( )
109
+ ) ;
110
+ break ;
111
+ }
112
+
115
113
let start_time = Instant :: now ( ) ;
116
114
let batch_end = std:: cmp:: min (
117
115
current_start + ( BATCH_SIZE - 1 ) as u32 ,
@@ -131,7 +129,8 @@ async fn process_blocks(
131
129
132
130
process_batch ( & processor, & blocks_and_txs, shutdown) . await ?;
133
131
if let Some ( ( block, _) ) = blocks_and_txs. last ( ) {
134
- save_last_block ( store, block) . await ?;
132
+ store. save_last_block ( block) . await ?;
133
+ store. save_total_blocks ( blocks_and_txs. len ( ) as u16 ) . await ?;
135
134
}
136
135
tracing:: info!( "Batch processed in {:?}" , start_time. elapsed( ) ) ;
137
136
current_start = batch_end + 1 ;
@@ -230,31 +229,6 @@ async fn get_initial_height(
230
229
}
231
230
}
232
231
233
- async fn get_last_block_saved ( store : & Store ) -> anyhow:: Result < BlockHeight > {
234
- let block_height = {
235
- let mut txn = store. begin ( ) ?;
236
- let key = Bytes :: from ( "last_block_saved" ) ;
237
- match txn. get ( & key) ? {
238
- Some ( value) => {
239
- Ok :: < BlockHeight , DuneError > ( serde_json:: from_slice ( & value) ?)
240
- }
241
- None => Ok ( BlockHeight :: from ( 0 ) ) ,
242
- }
243
- } ?;
244
- Ok ( block_height)
245
- }
246
-
247
- async fn save_last_block ( store : & Store , block : & Block ) -> Result < ( ) > {
248
- {
249
- let mut txn = store. begin ( ) ?;
250
- let key = Bytes :: from ( "last_block_saved" ) ;
251
- let value = block. height . encode_json ( ) ?;
252
- txn. set ( & key, & value) ?;
253
- txn. commit ( ) ?;
254
- }
255
- Ok ( ( ) )
256
- }
257
-
258
232
async fn get_blocks_and_transactions (
259
233
db : & Arc < Db > ,
260
234
start_height : BlockHeight ,
0 commit comments