Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(torii-indexer): add option for strict model reader block #2954

Merged
merged 4 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions crates/torii/cli/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@ impl Default for RelayOptions {
#[derive(Debug, clap::Args, Clone, Serialize, Deserialize, PartialEq)]
#[command(next_help_heading = "Indexing options")]
pub struct IndexingOptions {
/// 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,

/// Chunk size of the events page when indexing using events
#[arg(long = "indexing.events_chunk_size", default_value_t = DEFAULT_EVENTS_CHUNK_SIZE, help = "Chunk size of the events page to fetch from the sequencer.")]
#[serde(default = "default_events_chunk_size")]
Expand Down Expand Up @@ -175,6 +185,7 @@ pub struct IndexingOptions {
impl Default for IndexingOptions {
fn default() -> Self {
Self {
strict_model_reader: false,
events_chunk_size: DEFAULT_EVENTS_CHUNK_SIZE,
blocks_chunk_size: DEFAULT_BLOCKS_CHUNK_SIZE,
pending: true,
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
Loading