Skip to content

Commit

Permalink
Merge pull request #41 from chaindexing/re-introduce-chain-reorg-doma…
Browse files Browse the repository at this point in the history
…in-in-chaindexing-processes

Re introduce chain reorg domain in chaindexing processes
  • Loading branch information
Jurshsmith authored Nov 3, 2023
2 parents 435ebfc + cad0022 commit 465f805
Show file tree
Hide file tree
Showing 6 changed files with 205 additions and 220 deletions.
5 changes: 2 additions & 3 deletions chaindexing/src/event_handlers.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
use std::{sync::Arc, time::Duration};

mod handle_events;
mod handled_events;
mod maybe_handle_chain_reorg;

use tokio::{sync::Mutex, time::interval};

use crate::{contracts::Contracts, events::Event, ChaindexingRepo, Config, Repo};
use crate::{ChaindexingRepoRawQueryTxnClient, HasRawQueryClient};

use handle_events::HandleEvents;
use handled_events::MaybeBacktrackHandledEvents;

#[derive(Clone)]
pub struct EventHandlerContext<'a> {
Expand Down Expand Up @@ -63,7 +62,7 @@ impl EventHandlers {
.await;

let state_migrations = Contracts::get_state_migrations(&config.contracts);
MaybeBacktrackHandledEvents::run(
maybe_handle_chain_reorg::run(
conn.clone(),
&mut raw_query_client,
&state_migrations,
Expand Down
61 changes: 0 additions & 61 deletions chaindexing/src/event_handlers/handled_events.rs

This file was deleted.

57 changes: 57 additions & 0 deletions chaindexing/src/event_handlers/maybe_handle_chain_reorg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use std::sync::Arc;

use tokio::sync::Mutex;

use crate::ChaindexingRepo;
use crate::{
ChaindexingRepoConn, ChaindexingRepoRawQueryClient, ExecutesWithRawQuery, HasRawQueryClient,
Repo,
};
use crate::{ContractStateMigrations, ContractStates};
use crate::{ReorgedBlock, ReorgedBlocks};

pub async fn run<'a>(
conn: Arc<Mutex<ChaindexingRepoConn<'a>>>,
raw_query_client: &mut ChaindexingRepoRawQueryClient,
state_migrations: &Vec<Arc<dyn ContractStateMigrations>>,
) {
let mut conn = conn.lock().await;
let reorged_blocks = ChaindexingRepo::get_unhandled_reorged_blocks(&mut conn).await;

if !reorged_blocks.is_empty() {
let raw_query_txn_client =
ChaindexingRepo::get_raw_query_txn_client(raw_query_client).await;

let reorged_blocks = ReorgedBlocks::only_earliest_per_chain(&reorged_blocks);

for ReorgedBlock {
block_number,
chain_id,
..
} in &reorged_blocks
{
ContractStates::backtrack_states(
state_migrations,
*chain_id,
*block_number,
&raw_query_txn_client,
)
.await;
ChaindexingRepo::update_every_next_block_number_to_handle_from_in_txn(
&raw_query_txn_client,
*chain_id,
*block_number,
)
.await
}

let reorged_block_ids = ReorgedBlocks::get_ids(&reorged_blocks);
ChaindexingRepo::update_reorged_blocks_as_handled_in_txn(
&raw_query_txn_client,
&reorged_block_ids,
)
.await;

ChaindexingRepo::commit_raw_query_txns(raw_query_txn_client).await;
}
}
5 changes: 2 additions & 3 deletions chaindexing/src/events_ingester.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod ingest_events;
mod ingested_events;
mod maybe_handle_chain_reorg;

use std::collections::HashMap;
use std::sync::Arc;
Expand All @@ -16,7 +16,6 @@ use tokio::sync::Mutex;
use tokio::time::{interval, sleep};

use ingest_events::IngestEvents;
use ingested_events::MaybeBacktrackIngestedEvents;

use crate::chain_reorg::Execution;
use crate::contracts::Contract;
Expand Down Expand Up @@ -163,7 +162,7 @@ impl EventsIngester {
)
.await?;

MaybeBacktrackIngestedEvents::run(
maybe_handle_chain_reorg::run(
&mut conn,
contract_addresses.clone(),
contracts,
Expand Down
153 changes: 0 additions & 153 deletions chaindexing/src/events_ingester/ingested_events.rs

This file was deleted.

Loading

0 comments on commit 465f805

Please sign in to comment.