From e3bdc6970bd59fa4a326804b7d9e6d1787f5aad5 Mon Sep 17 00:00:00 2001 From: broody Date: Fri, 28 Jul 2023 16:36:55 -0700 Subject: [PATCH 1/2] fix(torii): only parse events from world address --- crates/torii/src/cli.rs | 5 +++-- crates/torii/src/engine.rs | 10 +++++++++- crates/torii/src/indexer.rs | 12 ++++++++++-- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/crates/torii/src/cli.rs b/crates/torii/src/cli.rs index 014a9498d5..d1266be55a 100644 --- a/crates/torii/src/cli.rs +++ b/crates/torii/src/cli.rs @@ -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")] @@ -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! { diff --git a/crates/torii/src/engine.rs b/crates/torii/src/engine.rs index 0c3a494691..64b688de93 100644 --- a/crates/torii/src/engine.rs +++ b/crates/torii/src/engine.rs @@ -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}; @@ -42,6 +43,7 @@ pub struct Engine<'a, S: State + Executable, T: JsonRpcTransport + Sync + Send> storage: &'a S, provider: &'a JsonRpcClient, processors: Processors, + world_address: FieldElement, start_block: Option, config: EngineConfig, } @@ -51,10 +53,11 @@ impl<'a, S: State + Executable, T: JsonRpcTransport + Sync + Send> Engine<'a, S, storage: &'a S, provider: &'a JsonRpcClient, processors: Processors, + world_address: FieldElement, start_block: Option, config: EngineConfig, ) -> Self { - Self { storage, provider, processors, start_block, config } + Self { storage, provider, processors, world_address, start_block, config } } pub async fn start(&self) -> Result<(), Box> { @@ -155,6 +158,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.world_address { + info!("event not from world address, skipping"); + continue; + } + process_event( self.storage, self.provider, diff --git a/crates/torii/src/indexer.rs b/crates/torii/src/indexer.rs index 6c5bc4119a..1a22be34b1 100644 --- a/crates/torii/src/indexer.rs +++ b/crates/torii/src/indexer.rs @@ -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}; @@ -22,10 +23,17 @@ impl<'a, S: State + Executable, T: JsonRpcTransport + Sync + Send> Indexer<'a, S provider: &'a JsonRpcClient, processors: Processors, manifest: Manifest, + world_address: FieldElement, start_block: Option, ) -> Self { - let engine = - Engine::new(storage, provider, processors, start_block, EngineConfig::default()); + let engine = Engine::new( + storage, + provider, + processors, + world_address, + start_block, + EngineConfig::default(), + ); Self { storage, provider, engine, manifest } } From c921f76861431041546c81f3b8147e501501ab9c Mon Sep 17 00:00:00 2001 From: broody Date: Fri, 28 Jul 2023 21:25:34 -0700 Subject: [PATCH 2/2] move start_block and world_address to engine config --- crates/torii/src/cli.rs | 4 ++-- crates/torii/src/engine.rs | 27 +++++++++++++++------------ crates/torii/src/indexer.rs | 6 ++---- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/crates/torii/src/cli.rs b/crates/torii/src/cli.rs index d1266be55a..f0fd831224 100644 --- a/crates/torii/src/cli.rs +++ b/crates/torii/src/cli.rs @@ -46,8 +46,8 @@ struct Args { #[arg(short, long)] manifest: Option, /// Specify a block to start indexing from, ignored if stored head exists - #[arg(short, long)] - start_block: Option, + #[arg(short, long, default_value = "0")] + start_block: u64, } #[tokio::main] diff --git a/crates/torii/src/engine.rs b/crates/torii/src/engine.rs index 64b688de93..9ca1b5ef46 100644 --- a/crates/torii/src/engine.rs +++ b/crates/torii/src/engine.rs @@ -31,11 +31,17 @@ impl Default for Processors { #[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, + } } } @@ -43,8 +49,6 @@ pub struct Engine<'a, S: State + Executable, T: JsonRpcTransport + Sync + Send> storage: &'a S, provider: &'a JsonRpcClient, processors: Processors, - world_address: FieldElement, - start_block: Option, config: EngineConfig, } @@ -53,23 +57,22 @@ impl<'a, S: State + Executable, T: JsonRpcTransport + Sync + Send> Engine<'a, S, storage: &'a S, provider: &'a JsonRpcClient, processors: Processors, - world_address: FieldElement, - start_block: Option, config: EngineConfig, ) -> Self { - Self { storage, provider, processors, world_address, start_block, config } + Self { storage, provider, processors, config } } pub async fn start(&self) -> Result<(), Box> { 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 { @@ -158,7 +161,7 @@ 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.world_address { + if event.from_address != self.config.world_address { info!("event not from world address, skipping"); continue; } diff --git a/crates/torii/src/indexer.rs b/crates/torii/src/indexer.rs index 1a22be34b1..75426ed6cc 100644 --- a/crates/torii/src/indexer.rs +++ b/crates/torii/src/indexer.rs @@ -24,15 +24,13 @@ impl<'a, S: State + Executable, T: JsonRpcTransport + Sync + Send> Indexer<'a, S processors: Processors, manifest: Manifest, world_address: FieldElement, - start_block: Option, + start_block: u64, ) -> Self { let engine = Engine::new( storage, provider, processors, - world_address, - start_block, - EngineConfig::default(), + EngineConfig { world_address, start_block, ..Default::default() }, ); Self { storage, provider, engine, manifest } }