Skip to content

Commit 50b43d3

Browse files
committed
Improves code linting and indexing process
Refactors clippy configuration to lint tests separately, allowing `unwrap_used`, `expect_used`, and `panic` in test code. Adds total block count and latest block number to indexing logs for better monitoring. Updates tracing subscriber configuration for enhanced readability in development.
1 parent cc812da commit 50b43d3

File tree

8 files changed

+6709
-28
lines changed

8 files changed

+6709
-28
lines changed

.github/workflows/ci.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@ jobs:
2626

2727
- uses: Swatinem/rust-cache@v2
2828

29-
- name: Run Clippy
30-
run: cargo clippy --all-targets --all-features -- -D warnings -A clippy::unwrap_used -A clippy::expect_used -A clippy::panic
29+
- name: Run Clippy on production code
30+
run: cargo clippy --lib --bins -- -D warnings
31+
32+
- name: Run Clippy on tests (with panic allowed)
33+
run: cargo clippy --tests -- -D warnings -A clippy::unwrap_used -A clippy::expect_used -A clippy::panic
3134

3235
build:
3336
needs: [fmt, clippy]

Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ test: ## Run all tests
2929

3030
lint: ## Run clippy linter
3131
@echo "Running clippy..."
32-
cargo clippy --all-targets --all-features -- -D warnings -A clippy::unwrap_used -A clippy::expect_used -A clippy::panic
32+
cargo clippy --lib --bins -- -D warnings
33+
@echo "Running clippy on tests (with panic allowed)..."
34+
cargo clippy --tests -- -D warnings -A clippy::unwrap_used -A clippy::expect_used -A clippy::panic
3335

3436
format: ## Format code using rustfmt
3537
@echo "Formatting code..."

lcov.info

Lines changed: 6628 additions & 0 deletions
Large diffs are not rendered by default.

src/indexer/batch_service.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,20 @@ where
285285
}
286286
}
287287

288+
async fn get_total_block_count(&self) -> Result<i64> {
289+
let count: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM blockheaders")
290+
.fetch_one(&self.db.pool)
291+
.await?;
292+
Ok(count)
293+
}
294+
295+
async fn get_latest_block_number(&self) -> Result<i64> {
296+
let latest: Option<i64> = sqlx::query_scalar("SELECT MAX(number) FROM blockheaders")
297+
.fetch_one(&self.db.pool)
298+
.await?;
299+
Ok(latest.unwrap_or(0))
300+
}
301+
288302
fn is_backfilling_complete(
289303
current_backfilling_block_number: i64,
290304
index_start_block_number: i64,
@@ -356,9 +370,11 @@ where
356370
return Err(eyre!("Block storage verification failed: {}. This indicates a critical database consistency issue.", e));
357371
}
358372

373+
let total_blocks = self.get_total_block_count().await.unwrap_or(0);
374+
let latest_block = self.get_latest_block_number().await.unwrap_or(0);
359375
info!(
360-
"[batch_index] Successfully stored and verified blocks {} to {}",
361-
starting_block, ending_block
376+
"Verified blocks {} to {}. Total blocks in DB: {}, Latest block: {}",
377+
starting_block, ending_block, total_blocks, latest_block
362378
);
363379
return Ok(());
364380
}
@@ -574,9 +590,11 @@ where
574590
eyre!("Database transaction commit failed: {}", e)
575591
})?;
576592

593+
let total_blocks = self.get_total_block_count().await.unwrap_or(0);
594+
let latest_block = self.get_latest_block_number().await.unwrap_or(0);
577595
info!(
578-
"[batch_index] Indexing block range from {} to {} complete.",
579-
starting_block, ending_block
596+
"Indexed blocks {} to {}. Total blocks in DB: {}, Latest block: {}",
597+
starting_block, ending_block, total_blocks, latest_block
580598
);
581599
Ok(())
582600
}

src/indexer/main.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,14 @@ pub async fn main() -> Result<()> {
3939
.ok()
4040
.and_then(|s| s.parse::<u64>().ok());
4141

42-
// Initialize tracing subscriber
43-
fmt().init();
42+
// Initialize tracing subscriber with human-readable format
43+
fmt()
44+
.with_target(false)
45+
.with_thread_ids(false)
46+
.with_file(false)
47+
.with_line_number(false)
48+
.compact()
49+
.init();
4450

4551
let should_terminate = Arc::new(AtomicBool::new(false));
4652

src/indexer/quick_service.rs

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::{
99
use eyre::{eyre, Result};
1010
use futures::future::try_join_all;
1111
use tokio::task;
12-
use tracing::{error, info, warn};
12+
use tracing::{debug, error, info, warn};
1313

1414
use crate::{
1515
db::DbConnection,
@@ -274,7 +274,7 @@ where
274274
}
275275

276276
async fn wait_for_new_blocks(&self, new_latest_block: BlockNumber) -> Result<()> {
277-
info!(
277+
debug!(
278278
"No new block finalized. Latest: {}. Sleeping for {}s...",
279279
new_latest_block, self.config.poll_interval
280280
);
@@ -311,7 +311,8 @@ where
311311
.await,
312312
Ok(())
313313
) {
314-
Self::log_indexing_success(starting_block, ending_block);
314+
self.log_indexing_success(starting_block, ending_block)
315+
.await;
315316
return Ok(());
316317
}
317318

@@ -340,10 +341,26 @@ where
340341
.await
341342
}
342343

343-
fn log_indexing_success(starting_block: i64, ending_block: i64) {
344+
async fn get_total_block_count(&self) -> Result<i64> {
345+
let count: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM blockheaders")
346+
.fetch_one(&self.db.pool)
347+
.await?;
348+
Ok(count)
349+
}
350+
351+
async fn get_latest_block_number(&self) -> Result<i64> {
352+
let latest: Option<i64> = sqlx::query_scalar("SELECT MAX(number) FROM blockheaders")
353+
.fetch_one(&self.db.pool)
354+
.await?;
355+
Ok(latest.unwrap_or(0))
356+
}
357+
358+
async fn log_indexing_success(&self, starting_block: i64, ending_block: i64) {
359+
let total_blocks = self.get_total_block_count().await.unwrap_or(0);
360+
let latest_block = self.get_latest_block_number().await.unwrap_or(0);
344361
info!(
345-
"[quick_index] Indexing block range from {} to {} complete.",
346-
starting_block, ending_block
362+
"Indexed blocks {} to {}. Total blocks in DB: {}, Latest block: {}",
363+
starting_block, ending_block, total_blocks, latest_block
347364
);
348365
}
349366

@@ -525,14 +542,16 @@ where
525542
}
526543

527544
// Verify ending block matches
528-
let last_block_num = convert_hex_string_to_i64(&sorted_headers.last().unwrap().number)
529-
.map_err(|e| {
530-
eyre!(
531-
"Invalid block number in last header: '{}' failed to parse with error: {}",
532-
sorted_headers.last().unwrap().number,
533-
e
534-
)
535-
})?;
545+
let last_header = sorted_headers
546+
.last()
547+
.ok_or_else(|| eyre!("No headers available after sorting (this should not happen)"))?;
548+
let last_block_num = convert_hex_string_to_i64(&last_header.number).map_err(|e| {
549+
eyre!(
550+
"Invalid block number in last header: '{}' failed to parse with error: {}",
551+
last_header.number,
552+
e
553+
)
554+
})?;
536555
if last_block_num != ending_block {
537556
return Err(eyre!(
538557
"Ending block mismatch: expected {}, got {}",

src/main.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,15 @@ enum Mode {
4141
async fn main() -> Result<()> {
4242
dotenvy::dotenv().ok();
4343

44-
// Initialize tracing subscriber
45-
fmt().with_env_filter(EnvFilter::from_default_env()).init();
44+
// Initialize tracing subscriber with human-readable format
45+
fmt()
46+
.with_env_filter(EnvFilter::from_default_env())
47+
.with_target(false)
48+
.with_thread_ids(false)
49+
.with_file(false)
50+
.with_line_number(false)
51+
.compact()
52+
.init();
4653

4754
info!("Starting Indexer");
4855

src/rpc/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -798,9 +798,7 @@ mod integration_tests {
798798
// Get one of the tx to test.
799799
let tx = header.transactions[0].clone();
800800

801-
let expected_tx = tx.clone();
802-
803-
let BlockTransaction::Full(expected_tx) = expected_tx else {
801+
let BlockTransaction::Full(expected_tx) = tx.clone() else {
804802
panic!("unexpected error due to tx type, should not happen.")
805803
};
806804

0 commit comments

Comments
 (0)