Skip to content

Commit d13a5ac

Browse files
committed
fix(sv-publisher): Add find_next_block_to_save query
1 parent 5451af3 commit d13a5ac

File tree

2 files changed

+52
-18
lines changed

2 files changed

+52
-18
lines changed

crates/store/src/store/store_impl.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,39 @@ pub async fn find_last_block_height(
9292
Ok(record.map(|(height,)| height.into()).unwrap_or_default())
9393
}
9494

95+
pub async fn find_next_block_to_save(
96+
db: &Db,
97+
options: QueryOptions,
98+
) -> StoreResult<BlockHeight> {
99+
let select = r#"
100+
SELECT block_height + 1 as next_height
101+
FROM blocks b1
102+
WHERE NOT EXISTS (
103+
SELECT 1
104+
FROM blocks b2
105+
WHERE b2.block_height = b1.block_height + 1
106+
)
107+
"#
108+
.to_string();
109+
110+
let mut query_builder = sqlx::QueryBuilder::new(select);
111+
112+
if let Some(ns) = options.namespace {
113+
query_builder
114+
.push(" AND subject LIKE ")
115+
.push_bind(format!("{}%", ns));
116+
}
117+
118+
query_builder.push(" ORDER BY block_height LIMIT 1");
119+
let query = query_builder.build_query_as::<(i64,)>();
120+
121+
let record: Option<(i64,)> = query
122+
.fetch_optional(&db.pool)
123+
.await
124+
.map_err(StoreError::from)?;
125+
Ok(record.map(|(height,)| height.into()).unwrap_or(1.into()))
126+
}
127+
95128
pub async fn update_block_propagation_ms(
96129
tx: &mut DbTransaction,
97130
block_height: BlockHeight,

services/publisher/src/main.rs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use fuel_streams_core::types::*;
66
use fuel_streams_store::{
77
db::{Db, DbConnectionOpts},
88
record::QueryOptions,
9-
store::find_last_block_height,
9+
store::find_next_block_to_save,
1010
};
1111
use fuel_web_utils::{
1212
server::api::build_and_spawn_web_server,
@@ -42,11 +42,12 @@ async fn main() -> anyhow::Result<()> {
4242
build_and_spawn_web_server(cli.telemetry_port, server_state).await?;
4343

4444
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?);
4647
let shutdown = Arc::new(ShutdownController::new());
4748
shutdown.clone().spawn_signal_handler();
4849

49-
tracing::info!("Last published height: {}", last_published);
50+
tracing::info!("Next block to process: {}", next_block_to_process);
5051
tracing::info!("Last block height: {}", last_block_height);
5152
tokio::select! {
5253
result = async {
@@ -55,7 +56,7 @@ async fn main() -> anyhow::Result<()> {
5556
&message_broker,
5657
&fuel_core,
5758
&last_block_height,
58-
&last_published,
59+
&next_block_to_process,
5960
shutdown.token().clone(),
6061
&telemetry,
6162
).await.map_err(|e| PublishError::Historical(e.to_string()))?;
@@ -92,38 +93,39 @@ async fn setup_db(db_url: &str) -> Result<Arc<Db>, PublishError> {
9293
Ok(db)
9394
}
9495

95-
async fn find_last_published_height(
96+
async fn find_next_block_to_process(
9697
db: &Db,
9798
) -> Result<BlockHeight, PublishError> {
9899
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?;
100101
Ok(height)
101102
}
102103

103104
fn get_historical_block_range(
104105
from_height: BlockHeight,
105-
last_published_height: BlockHeight,
106+
next_block_to_process: BlockHeight,
106107
last_block_height: BlockHeight,
107108
) -> 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
110111
} else {
111112
from_height
112113
};
113114

114-
let last_block_height = if last_block_height > from_height {
115+
let end_height = if last_block_height > start_height {
115116
*last_block_height
116117
} 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;
119120
};
120121

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();
123124
if start_height > end_height {
124125
tracing::info!("No historical blocks to process");
125126
return None;
126127
}
128+
127129
let block_count = end_height - start_height + 1;
128130
let heights: Vec<u64> = (start_height..=end_height).collect();
129131
tracing::info!(
@@ -137,20 +139,20 @@ fn process_historical_blocks(
137139
message_broker: &Arc<NatsMessageBroker>,
138140
fuel_core: &Arc<dyn FuelCoreLike>,
139141
last_block_height: &Arc<BlockHeight>,
140-
last_published_height: &Arc<BlockHeight>,
142+
next_block_to_process: &Arc<BlockHeight>,
141143
token: CancellationToken,
142144
telemetry: &Arc<Telemetry<Metrics>>,
143145
) -> tokio::task::JoinHandle<Result<(), PublishError>> {
144146
let message_broker = message_broker.clone();
145147
let fuel_core = fuel_core.clone();
146148
tokio::spawn({
147-
let last_published_height = *last_published_height.clone();
149+
let next_block_to_process = *next_block_to_process.clone();
148150
let last_block_height = *last_block_height.clone();
149151
let telemetry = telemetry.clone();
150152
async move {
151153
let Some(heights) = get_historical_block_range(
152154
from_height,
153-
last_published_height,
155+
next_block_to_process,
154156
last_block_height,
155157
) else {
156158
return Ok(());
@@ -215,7 +217,6 @@ async fn process_live_blocks(
215217
telemetry: &Arc<Telemetry<Metrics>>,
216218
) -> Result<(), PublishError> {
217219
let mut subscription = fuel_core.blocks_subscription();
218-
219220
loop {
220221
tokio::select! {
221222
_ = token.cancelled() => {

0 commit comments

Comments
 (0)