Skip to content

Commit

Permalink
More logging & metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
Dzejkop committed Dec 11, 2023
1 parent a82dcd9 commit dad7ff5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
18 changes: 10 additions & 8 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,8 @@ impl Database {
Ok(())
}

pub async fn handle_soft_reorgs(&self) -> eyre::Result<()> {
/// Returns a list of soft reorged txs
pub async fn handle_soft_reorgs(&self) -> eyre::Result<Vec<String>> {
let mut tx = self.pool.begin().await?;

// Fetch txs which have valid tx hash different than what is actually mined
Expand Down Expand Up @@ -448,10 +449,11 @@ impl Database {

tx.commit().await?;

Ok(())
Ok(tx_ids)
}

pub async fn handle_hard_reorgs(&self) -> eyre::Result<()> {
/// Returns a list of hard reorged txs
pub async fn handle_hard_reorgs(&self) -> eyre::Result<Vec<String>> {
let mut tx = self.pool.begin().await?;

// Fetch txs which are marked as mined
Expand All @@ -466,10 +468,10 @@ impl Database {
LEFT JOIN block_txs bt ON h.tx_hash = bt.tx_hash
WHERE s.status = $1
)
SELECT t.id
FROM reorg_candidates t
GROUP BY t.id
HAVING COUNT(t.chain_id) = 0
SELECT r.id
FROM reorg_candidates r
GROUP BY r.id
HAVING COUNT(r.chain_id) = 0
"#,
)
.bind(TxStatus::Mined)
Expand Down Expand Up @@ -504,7 +506,7 @@ impl Database {

tx.commit().await?;

Ok(())
Ok(tx_ids)
}

/// Marks txs as mined if the associated tx hash is present in a block
Expand Down
18 changes: 16 additions & 2 deletions src/tasks/handle_reorgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ pub async fn handle_hard_reorgs(app: Arc<App>) -> eyre::Result<()> {
loop {
tracing::info!("Handling hard reorgs");

app.db.handle_hard_reorgs().await?;
let reorged_txs = app.db.handle_hard_reorgs().await?;

Check warning on line 14 in src/tasks/handle_reorgs.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/tx-sitter-monolith/tx-sitter-monolith/src/tasks/handle_reorgs.rs

for tx in reorged_txs {
tracing::info!(
id = tx,
"Tx hard reorged"
);
}

tokio::time::sleep(Duration::from_secs(
TIME_BETWEEN_HARD_REORGS_SECONDS as u64,
Expand All @@ -24,7 +31,14 @@ pub async fn handle_soft_reorgs(app: Arc<App>) -> eyre::Result<()> {
loop {
tracing::info!("Handling soft reorgs");

app.db.handle_soft_reorgs().await?;
let txs = app.db.handle_soft_reorgs().await?;

Check warning on line 34 in src/tasks/handle_reorgs.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/tx-sitter-monolith/tx-sitter-monolith/src/tasks/handle_reorgs.rs

for tx in txs {
tracing::info!(
id = tx,
"Tx soft reorged"
);
}

tokio::time::sleep(Duration::from_secs(
TIME_BETWEEN_SOFT_REORGS_SECONDS as u64,
Expand Down

0 comments on commit dad7ff5

Please sign in to comment.