Skip to content

Commit

Permalink
feat(torii-indexer): add option for strict model reader block (#2954)
Browse files Browse the repository at this point in the history
* feat(torii-indexer): add option for strict model reader block

* event processor config
  • Loading branch information
Larkooo authored Jan 30, 2025
1 parent 91da65d commit f4b733d
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 14 deletions.
15 changes: 15 additions & 0 deletions crates/torii/cli/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,16 @@ pub struct IndexingOptions {
)]
#[serde(default)]
pub world_block: u64,

/// Whether or not to read models from the block number they were registered in.
/// If false, models will be read from the latest block.
#[arg(
long = "indexing.strict_model_reader",
default_value_t = false,
help = "Whether or not to read models from the block number they were registered in."
)]
#[serde(default)]
pub strict_model_reader: bool,
}

impl Default for IndexingOptions {
Expand All @@ -184,6 +194,7 @@ impl Default for IndexingOptions {
max_concurrent_tasks: DEFAULT_MAX_CONCURRENT_TASKS,
namespaces: vec![],
world_block: 0,
strict_model_reader: false,
}
}
}
Expand Down Expand Up @@ -226,6 +237,10 @@ impl IndexingOptions {
if self.world_block == 0 {
self.world_block = other.world_block;
}

if !self.strict_model_reader {
self.strict_model_reader = other.strict_model_reader;
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/torii/indexer/src/processors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub mod upgrade_model;
pub struct EventProcessorConfig {
pub historical_events: HashSet<String>,
pub namespaces: HashSet<String>,
pub strict_model_reader: bool,
}

impl EventProcessorConfig {
Expand Down
10 changes: 7 additions & 3 deletions crates/torii/indexer/src/processors/register_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use async_trait::async_trait;
use dojo_world::contracts::abigen::world::Event as WorldEvent;
use dojo_world::contracts::model::ModelReader;
use dojo_world::contracts::world::WorldContractReader;
use starknet::core::types::Event;
use starknet::core::types::{BlockId, Event};
use starknet::providers::Provider;
use torii_sqlite::Sql;
use tracing::{debug, info};
Expand Down Expand Up @@ -47,7 +47,7 @@ where
&self,
world: &WorldContractReader<P>,
db: &mut Sql,
_block_number: u64,
block_number: u64,
block_timestamp: u64,
_event_id: &str,
event: &Event,
Expand Down Expand Up @@ -79,7 +79,11 @@ where

// Called model here by language, but it's an event. Torii rework will make clear
// distinction.
let model = world.model_reader(&namespace, &name).await?;
let model = if config.strict_model_reader {
world.model_reader_with_block(&namespace, &name, BlockId::Number(block_number)).await?
} else {
world.model_reader(&namespace, &name).await?
};
let schema = model.schema().await?;
let layout = model.layout().await?;

Expand Down
10 changes: 7 additions & 3 deletions crates/torii/indexer/src/processors/register_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use async_trait::async_trait;
use dojo_world::contracts::abigen::world::Event as WorldEvent;
use dojo_world::contracts::model::ModelReader;
use dojo_world::contracts::world::WorldContractReader;
use starknet::core::types::Event;
use starknet::core::types::{BlockId, Event};
use starknet::providers::Provider;
use torii_sqlite::Sql;
use tracing::{debug, info};
Expand Down Expand Up @@ -47,7 +47,7 @@ where
&self,
world: &WorldContractReader<P>,
db: &mut Sql,
_block_number: u64,
block_number: u64,
block_timestamp: u64,
_event_id: &str,
event: &Event,
Expand Down Expand Up @@ -77,7 +77,11 @@ where
return Ok(());
}

let model = world.model_reader(&namespace, &name).await?;
let model = if config.strict_model_reader {
world.model_reader_with_block(&namespace, &name, BlockId::Number(block_number)).await?
} else {
world.model_reader(&namespace, &name).await?
};
let schema = model.schema().await?;
let layout = model.layout().await?;

Expand Down
12 changes: 8 additions & 4 deletions crates/torii/indexer/src/processors/upgrade_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use async_trait::async_trait;
use dojo_world::contracts::abigen::world::Event as WorldEvent;
use dojo_world::contracts::model::ModelReader;
use dojo_world::contracts::world::WorldContractReader;
use starknet::core::types::Event;
use starknet::core::types::{BlockId, Event};
use starknet::providers::Provider;
use torii_sqlite::Sql;
use tracing::{debug, info};
Expand Down Expand Up @@ -47,11 +47,11 @@ where
&self,
world: &WorldContractReader<P>,
db: &mut Sql,
_block_number: u64,
block_number: u64,
block_timestamp: u64,
_event_id: &str,
event: &Event,
_config: &EventProcessorConfig,
config: &EventProcessorConfig,
) -> Result<(), Error> {
// Torii version is coupled to the world version, so we can expect the event to be well
// formed.
Expand Down Expand Up @@ -88,7 +88,11 @@ where
let namespace = model.namespace;
let prev_schema = model.schema;

let model = world.model_reader(&namespace, &name).await?;
let model = if config.strict_model_reader {
world.model_reader_with_block(&namespace, &name, BlockId::Number(block_number)).await?
} else {
world.model_reader(&namespace, &name).await?
};
let new_schema = model.schema().await?;
let schema_diff = new_schema.diff(&prev_schema);
// No changes to the schema. This can happen if torii is re-run with a fresh database.
Expand Down
12 changes: 8 additions & 4 deletions crates/torii/indexer/src/processors/upgrade_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use async_trait::async_trait;
use dojo_world::contracts::abigen::world::Event as WorldEvent;
use dojo_world::contracts::model::ModelReader;
use dojo_world::contracts::world::WorldContractReader;
use starknet::core::types::Event;
use starknet::core::types::{BlockId, Event};
use starknet::providers::Provider;
use torii_sqlite::Sql;
use tracing::{debug, info};
Expand Down Expand Up @@ -47,11 +47,11 @@ where
&self,
world: &WorldContractReader<P>,
db: &mut Sql,
_block_number: u64,
block_number: u64,
block_timestamp: u64,
_event_id: &str,
event: &Event,
_config: &EventProcessorConfig,
config: &EventProcessorConfig,
) -> Result<(), Error> {
// Torii version is coupled to the world version, so we can expect the event to be well
// formed.
Expand Down Expand Up @@ -86,7 +86,11 @@ where
let namespace = model.namespace;
let prev_schema = model.schema;

let model = world.model_reader(&namespace, &name).await?;
let model = if config.strict_model_reader {
world.model_reader_with_block(&namespace, &name, BlockId::Number(block_number)).await?
} else {
world.model_reader(&namespace, &name).await?
};
let new_schema = model.schema().await?;
let schema_diff = new_schema.diff(&prev_schema);
// No changes to the schema. This can happen if torii is re-run with a fresh database.
Expand Down
1 change: 1 addition & 0 deletions crates/torii/runner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ impl Runner {
polling_interval: Duration::from_millis(self.args.indexing.polling_interval),
flags,
event_processor_config: EventProcessorConfig {
strict_model_reader: self.args.indexing.strict_model_reader,
historical_events: self.args.events.historical.into_iter().collect(),
namespaces: self.args.indexing.namespaces.into_iter().collect(),
},
Expand Down

0 comments on commit f4b733d

Please sign in to comment.