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 2 commits
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 @@
#[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,

Check warning on line 101 in crates/torii/cli/src/options.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/cli/src/options.rs#L101

Added line #L101 was not covered by tests

/// 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 @@
impl Default for IndexingOptions {
fn default() -> Self {
Self {
strict_model_reader: false,

Check warning on line 188 in crates/torii/cli/src/options.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/cli/src/options.rs#L188

Added line #L188 was not covered by tests
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 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 @@
&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 @@

// 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?

Check warning on line 83 in crates/torii/indexer/src/processors/register_event.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/indexer/src/processors/register_event.rs#L83

Added line #L83 was not covered by tests
} 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 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 @@
&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 @@
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?

Check warning on line 81 in crates/torii/indexer/src/processors/register_model.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/indexer/src/processors/register_model.rs#L81

Added line #L81 was not covered by tests
} 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 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 @@
&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 @@
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?

Check warning on line 92 in crates/torii/indexer/src/processors/upgrade_event.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/indexer/src/processors/upgrade_event.rs#L91-L92

Added lines #L91 - L92 were not covered by tests
} else {
world.model_reader(&namespace, &name).await?

Check warning on line 94 in crates/torii/indexer/src/processors/upgrade_event.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/indexer/src/processors/upgrade_event.rs#L94

Added line #L94 was not covered by tests
};
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 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 @@
&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 @@
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?

Check warning on line 90 in crates/torii/indexer/src/processors/upgrade_model.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/indexer/src/processors/upgrade_model.rs#L89-L90

Added lines #L89 - L90 were not covered by tests
} else {
world.model_reader(&namespace, &name).await?

Check warning on line 92 in crates/torii/indexer/src/processors/upgrade_model.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/indexer/src/processors/upgrade_model.rs#L92

Added line #L92 was not covered by tests
};
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 @@
polling_interval: Duration::from_millis(self.args.indexing.polling_interval),
flags,
event_processor_config: EventProcessorConfig {
strict_model_reader: self.args.indexing.strict_model_reader,

Check warning on line 180 in crates/torii/runner/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/runner/src/lib.rs#L180

Added line #L180 was not covered by tests
historical_events: self.args.events.historical.into_iter().collect(),
namespaces: self.args.indexing.namespaces.into_iter().collect(),
},
Expand Down
Loading