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

fix(torii): only parse events from world address #688

Merged
merged 2 commits into from
Jul 30, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 5 additions & 4 deletions crates/torii/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ mod tests;
#[command(author, version, about, long_about = None)]
struct Args {
/// The world to index
#[arg(short, long, default_value = "0x420")]
#[arg(short, long)]
world_address: FieldElement,
/// The rpc endpoint to use
#[arg(long, default_value = "http://localhost:5050")]
Expand All @@ -46,8 +46,8 @@ struct Args {
#[arg(short, long)]
manifest: Option<Utf8PathBuf>,
/// Specify a block to start indexing from, ignored if stored head exists
#[arg(short, long)]
start_block: Option<u64>,
#[arg(short, long, default_value = "0")]
start_block: u64,
}

#[tokio::main]
Expand Down Expand Up @@ -95,7 +95,8 @@ async fn main() -> anyhow::Result<()> {
..Processors::default()
};

let indexer = Indexer::new(&state, &provider, processors, manifest, args.start_block);
let indexer =
Indexer::new(&state, &provider, processors, manifest, args.world_address, args.start_block);
let graphql = start_graphql(&pool);

tokio::select! {
Expand Down
29 changes: 20 additions & 9 deletions crates/torii/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use starknet::core::types::{
use starknet::core::utils::get_selector_from_name;
use starknet::providers::jsonrpc::{JsonRpcClient, JsonRpcTransport};
use starknet::providers::Provider;
use starknet_crypto::FieldElement;
use tokio::time::sleep;
use tracing::{error, info, warn};

Expand All @@ -30,19 +31,24 @@ impl<S: State, T: JsonRpcTransport + Sync + Send> Default for Processors<S, T> {
#[derive(Debug)]
pub struct EngineConfig {
pub block_time: Duration,
pub world_address: FieldElement,
pub start_block: u64,
}

impl Default for EngineConfig {
fn default() -> Self {
Self { block_time: Duration::from_secs(1) }
Self {
block_time: Duration::from_secs(1),
world_address: FieldElement::ZERO,
start_block: 0,
}
}
}

pub struct Engine<'a, S: State + Executable, T: JsonRpcTransport + Sync + Send> {
storage: &'a S,
provider: &'a JsonRpcClient<T>,
processors: Processors<S, T>,
start_block: Option<u64>,
config: EngineConfig,
}

Expand All @@ -51,22 +57,22 @@ impl<'a, S: State + Executable, T: JsonRpcTransport + Sync + Send> Engine<'a, S,
storage: &'a S,
provider: &'a JsonRpcClient<T>,
processors: Processors<S, T>,
start_block: Option<u64>,
config: EngineConfig,
) -> Self {
Self { storage, provider, processors, start_block, config }
Self { storage, provider, processors, config }
}

pub async fn start(&self) -> Result<(), Box<dyn Error>> {
let storage_head = self.storage.head().await?;

let mut current_block_number = match (storage_head, self.start_block) {
(0, Some(start_block)) => start_block,
(_, Some(_)) => {
warn!("start block ignored, stored head exists and will be used instead");
let mut current_block_number = match storage_head {
0 => self.config.start_block,
_ => {
if self.config.start_block != 0 {
warn!("start block ignored, stored head exists and will be used instead");
}
storage_head
}
(_, None) => storage_head,
};

loop {
Expand Down Expand Up @@ -155,6 +161,11 @@ impl<'a, S: State + Executable, T: JsonRpcTransport + Sync + Send> Engine<'a, S,

if let TransactionReceipt::Invoke(invoke_receipt) = receipt.clone() {
for event in &invoke_receipt.events {
if event.from_address != self.config.world_address {
info!("event not from world address, skipping");
continue;
}

process_event(
self.storage,
self.provider,
Expand Down
12 changes: 9 additions & 3 deletions crates/torii/src/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::error::Error;

use dojo_world::manifest::Manifest;
use starknet::providers::jsonrpc::{JsonRpcClient, JsonRpcTransport};
use starknet_crypto::FieldElement;
use tracing::info;

use crate::engine::{Engine, EngineConfig, Processors};
Expand All @@ -22,10 +23,15 @@ impl<'a, S: State + Executable, T: JsonRpcTransport + Sync + Send> Indexer<'a, S
provider: &'a JsonRpcClient<T>,
processors: Processors<S, T>,
manifest: Manifest,
start_block: Option<u64>,
world_address: FieldElement,
start_block: u64,
) -> Self {
let engine =
Engine::new(storage, provider, processors, start_block, EngineConfig::default());
let engine = Engine::new(
storage,
provider,
processors,
EngineConfig { world_address, start_block, ..Default::default() },
);
Self { storage, provider, engine, manifest }
}

Expand Down