@@ -6,7 +6,7 @@ use fuel_streams_core::types::*;
6
6
use fuel_streams_store:: {
7
7
db:: { Db , DbConnectionOpts } ,
8
8
record:: QueryOptions ,
9
- store:: find_last_block_height ,
9
+ store:: find_next_block_to_save ,
10
10
} ;
11
11
use fuel_web_utils:: {
12
12
server:: api:: build_and_spawn_web_server,
@@ -42,11 +42,12 @@ async fn main() -> anyhow::Result<()> {
42
42
build_and_spawn_web_server ( cli. telemetry_port , server_state) . await ?;
43
43
44
44
let last_block_height = Arc :: new ( fuel_core. get_latest_block_height ( ) ?) ;
45
- let last_published = Arc :: new ( find_last_published_height ( & db) . await ?) ;
45
+ let next_block_to_process =
46
+ Arc :: new ( find_next_block_to_process ( & db) . await ?) ;
46
47
let shutdown = Arc :: new ( ShutdownController :: new ( ) ) ;
47
48
shutdown. clone ( ) . spawn_signal_handler ( ) ;
48
49
49
- tracing:: info!( "Last published height : {}" , last_published ) ;
50
+ tracing:: info!( "Next block to process : {}" , next_block_to_process ) ;
50
51
tracing:: info!( "Last block height: {}" , last_block_height) ;
51
52
tokio:: select! {
52
53
result = async {
@@ -55,7 +56,7 @@ async fn main() -> anyhow::Result<()> {
55
56
& message_broker,
56
57
& fuel_core,
57
58
& last_block_height,
58
- & last_published ,
59
+ & next_block_to_process ,
59
60
shutdown. token( ) . clone( ) ,
60
61
& telemetry,
61
62
) . await . map_err( |e| PublishError :: Historical ( e. to_string( ) ) ) ?;
@@ -92,38 +93,39 @@ async fn setup_db(db_url: &str) -> Result<Arc<Db>, PublishError> {
92
93
Ok ( db)
93
94
}
94
95
95
- async fn find_last_published_height (
96
+ async fn find_next_block_to_process (
96
97
db : & Db ,
97
98
) -> Result < BlockHeight , PublishError > {
98
99
let opts = QueryOptions :: default ( ) ;
99
- let height = find_last_block_height ( db, opts) . await ?;
100
+ let height = find_next_block_to_save ( db, opts) . await ?;
100
101
Ok ( height)
101
102
}
102
103
103
104
fn get_historical_block_range (
104
105
from_height : BlockHeight ,
105
- last_published_height : BlockHeight ,
106
+ next_block_to_process : BlockHeight ,
106
107
last_block_height : BlockHeight ,
107
108
) -> Option < Vec < u64 > > {
108
- let last_published_height = if last_published_height > from_height {
109
- last_published_height
109
+ let start_height = if next_block_to_process > from_height {
110
+ next_block_to_process
110
111
} else {
111
112
from_height
112
113
} ;
113
114
114
- let last_block_height = if last_block_height > from_height {
115
+ let end_height = if last_block_height > start_height {
115
116
* last_block_height
116
117
} else {
117
- tracing:: error!( "Last block height is less than from height" ) ;
118
- * last_block_height
118
+ tracing:: error!( "Last block height is less than start height" ) ;
119
+ return None ;
119
120
} ;
120
121
121
- let start_height = * last_published_height + 1 ;
122
- let end_height = last_block_height ;
122
+ let end_height = end_height . into ( ) ;
123
+ let start_height = start_height . into ( ) ;
123
124
if start_height > end_height {
124
125
tracing:: info!( "No historical blocks to process" ) ;
125
126
return None ;
126
127
}
128
+
127
129
let block_count = end_height - start_height + 1 ;
128
130
let heights: Vec < u64 > = ( start_height..=end_height) . collect ( ) ;
129
131
tracing:: info!(
@@ -137,20 +139,20 @@ fn process_historical_blocks(
137
139
message_broker : & Arc < NatsMessageBroker > ,
138
140
fuel_core : & Arc < dyn FuelCoreLike > ,
139
141
last_block_height : & Arc < BlockHeight > ,
140
- last_published_height : & Arc < BlockHeight > ,
142
+ next_block_to_process : & Arc < BlockHeight > ,
141
143
token : CancellationToken ,
142
144
telemetry : & Arc < Telemetry < Metrics > > ,
143
145
) -> tokio:: task:: JoinHandle < Result < ( ) , PublishError > > {
144
146
let message_broker = message_broker. clone ( ) ;
145
147
let fuel_core = fuel_core. clone ( ) ;
146
148
tokio:: spawn ( {
147
- let last_published_height = * last_published_height . clone ( ) ;
149
+ let next_block_to_process = * next_block_to_process . clone ( ) ;
148
150
let last_block_height = * last_block_height. clone ( ) ;
149
151
let telemetry = telemetry. clone ( ) ;
150
152
async move {
151
153
let Some ( heights) = get_historical_block_range (
152
154
from_height,
153
- last_published_height ,
155
+ next_block_to_process ,
154
156
last_block_height,
155
157
) else {
156
158
return Ok ( ( ) ) ;
@@ -215,7 +217,6 @@ async fn process_live_blocks(
215
217
telemetry : & Arc < Telemetry < Metrics > > ,
216
218
) -> Result < ( ) , PublishError > {
217
219
let mut subscription = fuel_core. blocks_subscription ( ) ;
218
-
219
220
loop {
220
221
tokio:: select! {
221
222
_ = token. cancelled( ) => {
0 commit comments