diff --git a/Cargo.lock b/Cargo.lock index 2ac3c5dc7b..c6c53afa5b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3589,6 +3589,7 @@ dependencies = [ "anstyle", "clap_lex", "strsim 0.11.1", + "terminal_size", ] [[package]] @@ -3600,6 +3601,20 @@ dependencies = [ "clap", ] +[[package]] +name = "clap_config" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46efb9cbf691f5505d0b7b2c8055aec0c9a770eaac8a06834b6d84b5be93279a" +dependencies = [ + "clap", + "heck 0.5.0", + "proc-macro2", + "quote", + "serde", + "syn 2.0.77", +] + [[package]] name = "clap_derive" version = "4.5.18" @@ -14462,6 +14477,16 @@ dependencies = [ "winapi", ] +[[package]] +name = "terminal_size" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21bebf2b7c9e0a515f6e0f8c51dc0f8e4696391e6f1ff30379559f8365fb0df7" +dependencies = [ + "rustix 0.38.37", + "windows-sys 0.48.0", +] + [[package]] name = "termtree" version = "0.4.1" @@ -14952,6 +14977,7 @@ dependencies = [ "camino", "chrono", "clap", + "clap_config", "ctrlc", "dojo-metrics", "dojo-types 1.0.0-rc.1", @@ -14975,6 +15001,7 @@ dependencies = [ "tokio", "tokio-stream", "tokio-util", + "toml 0.8.19", "torii-core", "torii-graphql", "torii-grpc", @@ -15050,7 +15077,6 @@ dependencies = [ "thiserror", "tokio", "tokio-util", - "toml 0.8.19", "tracing", ] diff --git a/bin/torii/Cargo.toml b/bin/torii/Cargo.toml index aadbd390cd..71ff5ec916 100644 --- a/bin/torii/Cargo.toml +++ b/bin/torii/Cargo.toml @@ -40,6 +40,7 @@ torii-grpc = { workspace = true, features = [ "server" ] } torii-relay.workspace = true torii-server.workspace = true tower.workspace = true +toml.workspace = true tower-http.workspace = true tracing-subscriber.workspace = true @@ -47,6 +48,7 @@ tracing.workspace = true url.workspace = true webbrowser = "0.8" tempfile.workspace = true +clap_config = "0.1.1" [dev-dependencies] camino.workspace = true diff --git a/bin/torii/src/main.rs b/bin/torii/src/main.rs index c55da2f464..a6dc11c24f 100644 --- a/bin/torii/src/main.rs +++ b/bin/torii/src/main.rs @@ -11,7 +11,6 @@ //! for more info. use std::cmp; -use std::collections::VecDeque; use std::net::SocketAddr; use std::path::PathBuf; use std::str::FromStr; @@ -19,7 +18,8 @@ use std::sync::Arc; use std::time::Duration; use anyhow::Context; -use clap::{ArgAction, Parser}; +use clap::{ArgAction, CommandFactory, FromArgMatches, Parser}; +use clap_config::ClapConfig; use dojo_metrics::exporters::prometheus::PrometheusRecorder; use dojo_utils::parse::{parse_socket_address, parse_url}; use dojo_world::contracts::world::WorldContractReader; @@ -37,9 +37,10 @@ use tokio_stream::StreamExt; use torii_core::engine::{Engine, EngineConfig, IndexingFlags, Processors}; use torii_core::executor::Executor; use torii_core::processors::store_transaction::StoreTransactionProcessor; +use torii_core::processors::EventProcessorConfig; use torii_core::simple_broker::SimpleBroker; use torii_core::sql::Sql; -use torii_core::types::{Contract, ContractType, Model, ToriiConfig}; +use torii_core::types::{Contract, ContractType, Model}; use torii_server::proxy::Proxy; use tracing::{error, info}; use tracing_subscriber::{fmt, EnvFilter}; @@ -48,7 +49,7 @@ use url::{form_urlencoded, Url}; pub(crate) const LOG_TARGET: &str = "torii::cli"; /// Dojo World Indexer -#[derive(Parser, Debug)] +#[derive(ClapConfig, Parser, Debug)] #[command(name = "torii", author, version, about, long_about = None)] struct Args { /// The world to index @@ -91,9 +92,8 @@ struct Args { /// Specify allowed origins for api endpoints (comma-separated list of allowed origins, or "*" /// for all) - #[arg(long)] - #[arg(value_delimiter = ',')] - allowed_origins: Option>, + #[arg(long, value_delimiter = ',')] + allowed_origins: Vec, /// The external url of the server, used for configuring the GraphQL Playground in a hosted /// environment @@ -139,32 +139,38 @@ struct Args { index_raw_events: bool, /// ERC contract addresses to index - #[arg(long, value_parser = parse_erc_contracts)] - #[arg(conflicts_with = "config")] - contracts: Option>, + #[arg(long, value_delimiter = ',', value_parser = parse_erc_contract)] + contracts: Vec, + + /// Event messages that are going to be treated as historical + /// A list of the model tags (namespace-name) + #[arg(long, value_delimiter = ',')] + historical_events: Vec, /// Configuration file #[arg(long)] + #[clap_config(skip)] config: Option, } #[tokio::main] async fn main() -> anyhow::Result<()> { - let args = Args::parse(); - - let mut config = if let Some(path) = args.config { - ToriiConfig::load_from_path(&path)? + let matches = ::command().get_matches(); + let mut args = if let Some(path) = matches.get_one::("config") { + let config: ArgsConfig = toml::from_str(&std::fs::read_to_string(path)?)?; + Args::from_merged(matches, Some(config)) } else { - let mut config = ToriiConfig::default(); - - if let Some(contracts) = args.contracts { - config.contracts = VecDeque::from(contracts); - } + Args::from_arg_matches(&matches)? + }; - config + let world_address = if let Some(world_address) = args.world_address { + world_address + } else { + return Err(anyhow::anyhow!("Please specify a world address.")); }; - let world_address = verify_single_world_address(args.world_address, &mut config)?; + // let mut contracts = parse_erc_contracts(&args.contracts)?; + args.contracts.push(Contract { address: world_address, r#type: ContractType::WORLD }); let filter_layer = EnvFilter::try_from_default_env() .unwrap_or_else(|_| EnvFilter::new("info,hyper_reverse_proxy=off")); @@ -210,15 +216,12 @@ async fn main() -> anyhow::Result<()> { // Get world address let world = WorldContractReader::new(world_address, provider.clone()); - let contracts = - config.contracts.iter().map(|contract| (contract.address, contract.r#type)).collect(); - let (mut executor, sender) = Executor::new(pool.clone(), shutdown_tx.clone()).await?; tokio::spawn(async move { executor.run().await.unwrap(); }); - let db = Sql::new(pool.clone(), sender.clone(), &contracts).await?; + let db = Sql::new(pool.clone(), sender.clone(), &args.contracts).await?; let processors = Processors { transaction: vec![Box::new(StoreTransactionProcessor)], @@ -248,10 +251,13 @@ async fn main() -> anyhow::Result<()> { index_pending: args.index_pending, polling_interval: Duration::from_millis(args.polling_interval), flags, + event_processor_config: EventProcessorConfig { + historical_events: args.historical_events.into_iter().collect(), + }, }, shutdown_tx.clone(), Some(block_tx), - Arc::new(contracts), + &args.contracts, ); let shutdown_rx = shutdown_tx.subscribe(); @@ -270,7 +276,12 @@ async fn main() -> anyhow::Result<()> { ) .expect("Failed to start libp2p relay server"); - let proxy_server = Arc::new(Proxy::new(args.addr, args.allowed_origins, Some(grpc_addr), None)); + let proxy_server = Arc::new(Proxy::new( + args.addr, + if args.allowed_origins.is_empty() { None } else { Some(args.allowed_origins) }, + Some(grpc_addr), + None, + )); let graphql_server = spawn_rebuilding_graphql_server( shutdown_tx.clone(), @@ -321,26 +332,6 @@ async fn main() -> anyhow::Result<()> { Ok(()) } -// Verifies that the world address is defined at most once -// and returns the world address -fn verify_single_world_address( - world_address: Option, - config: &mut ToriiConfig, -) -> anyhow::Result { - let world_from_config = - config.contracts.iter().find(|c| c.r#type == ContractType::WORLD).map(|c| c.address); - - match (world_address, world_from_config) { - (Some(_), Some(_)) => Err(anyhow::anyhow!("World address specified multiple times")), - (Some(addr), _) => { - config.contracts.push_front(Contract { address: addr, r#type: ContractType::WORLD }); - Ok(addr) - } - (_, Some(addr)) => Ok(addr), - (None, None) => Err(anyhow::anyhow!("World address not specified")), - } -} - async fn spawn_rebuilding_graphql_server( shutdown_tx: Sender<()>, pool: Arc, @@ -368,25 +359,20 @@ async fn spawn_rebuilding_graphql_server( // Parses clap cli argument which is expected to be in the format: // - erc_type:address:start_block // - address:start_block (erc_type defaults to ERC20) -fn parse_erc_contracts(s: &str) -> anyhow::Result> { - let parts: Vec<&str> = s.split(',').collect(); - let mut contracts = Vec::new(); - for part in parts { - match part.split(':').collect::>().as_slice() { - [r#type, address] => { - let r#type = r#type.parse::()?; - let address = Felt::from_str(address) - .with_context(|| format!("Expected address, found {}", address))?; - contracts.push(Contract { address, r#type }); - } - [address] => { - let r#type = ContractType::WORLD; - let address = Felt::from_str(address) - .with_context(|| format!("Expected address, found {}", address))?; - contracts.push(Contract { address, r#type }); +fn parse_erc_contract(part: &str) -> anyhow::Result { + match part.split(':').collect::>().as_slice() { + [r#type, address] => { + let r#type = r#type.parse::()?; + if r#type == ContractType::WORLD { + return Err(anyhow::anyhow!( + "World address cannot be specified as an ERC contract" + )); } - _ => return Err(anyhow::anyhow!("Invalid contract format")), + + let address = Felt::from_str(address) + .with_context(|| format!("Expected address, found {}", address))?; + Ok(Contract { address, r#type }) } + _ => Err(anyhow::anyhow!("Invalid contract format")), } - Ok(contracts) } diff --git a/bin/torii/torii.toml b/bin/torii/torii.toml index 93a444170f..ee843ea651 100644 --- a/bin/torii/torii.toml +++ b/bin/torii/torii.toml @@ -1,6 +1,14 @@ # Example configuration file for Torii -# contracts = [ -# { type = "WORLD", address = "" }, -# { type = "ERC20", address = "" }, -# { type = "ERC721", address = "" }, -# ] \ No newline at end of file +world_address="0x054d0f13bf3fb5f15a8674c5204aad35e3022af96bcc23bdbd16b7e297ffd399" +addr="0.0.0.0:8080" +rpc="http://0.0.0.0:5050" + +historical_events=["ns-Moved", "ns-Spawned"] + +[[contracts]] +type="ERC20" +address="0x0" + +[[contracts]] +type="ERC721" +address="0x123" \ No newline at end of file diff --git a/crates/torii/core/Cargo.toml b/crates/torii/core/Cargo.toml index 345ded02e9..aac4d9010f 100644 --- a/crates/torii/core/Cargo.toml +++ b/crates/torii/core/Cargo.toml @@ -34,7 +34,6 @@ thiserror.workspace = true tokio = { version = "1.32.0", features = [ "sync", "macros" ], default-features = true } # tokio-stream = "0.1.11" tokio-util.workspace = true -toml.workspace = true tracing.workspace = true [dev-dependencies] diff --git a/crates/torii/core/src/engine.rs b/crates/torii/core/src/engine.rs index f060500f89..b73b66bcf9 100644 --- a/crates/torii/core/src/engine.rs +++ b/crates/torii/core/src/engine.rs @@ -37,9 +37,11 @@ use crate::processors::store_del_record::StoreDelRecordProcessor; use crate::processors::store_set_record::StoreSetRecordProcessor; use crate::processors::store_update_member::StoreUpdateMemberProcessor; use crate::processors::store_update_record::StoreUpdateRecordProcessor; -use crate::processors::{BlockProcessor, EventProcessor, TransactionProcessor}; +use crate::processors::{ + BlockProcessor, EventProcessor, EventProcessorConfig, TransactionProcessor, +}; use crate::sql::{Cursors, Sql}; -use crate::types::ContractType; +use crate::types::{Contract, ContractType}; type EventProcessorMap

= HashMap>>>; @@ -142,6 +144,7 @@ pub struct EngineConfig { pub index_pending: bool, pub max_concurrent_tasks: usize, pub flags: IndexingFlags, + pub event_processor_config: EventProcessorConfig, } impl Default for EngineConfig { @@ -154,6 +157,7 @@ impl Default for EngineConfig { index_pending: true, max_concurrent_tasks: 100, flags: IndexingFlags::empty(), + event_processor_config: EventProcessorConfig::default(), } } } @@ -217,8 +221,12 @@ impl Engine

{ config: EngineConfig, shutdown_tx: Sender<()>, block_tx: Option>, - contracts: Arc>, + contracts: &[Contract], ) -> Self { + let contracts = Arc::new( + contracts.iter().map(|contract| (contract.address, contract.r#type)).collect(), + ); + Self { world: Arc::new(world), db, @@ -574,6 +582,7 @@ impl Engine

{ let semaphore = semaphore.clone(); let processors = self.processors.clone(); + let event_processor_config = self.config.event_processor_config.clone(); handles.push(tokio::spawn(async move { let _permit = semaphore.acquire().await?; let mut local_db = db.clone(); @@ -586,7 +595,7 @@ impl Engine

{ debug!(target: LOG_TARGET, event_name = processor.event_key(), task_id = %task_id, "Processing parallelized event."); if let Err(e) = processor - .process(&world, &mut local_db, block_number, block_timestamp, &event_id, &event) + .process(&world, &mut local_db, block_number, block_timestamp, &event_id, &event, &event_processor_config) .await { error!(target: LOG_TARGET, event_name = processor.event_key(), error = %e, task_id = %task_id, "Processing parallelized event."); @@ -796,6 +805,7 @@ impl Engine

{ block_timestamp, event_id, event, + &self.config.event_processor_config, ) .await { @@ -855,6 +865,7 @@ impl Engine

{ block_timestamp, event_id, event, + &self.config.event_processor_config, ) .await { diff --git a/crates/torii/core/src/processors/erc20_legacy_transfer.rs b/crates/torii/core/src/processors/erc20_legacy_transfer.rs index bf4fd33e49..4ed17416bc 100644 --- a/crates/torii/core/src/processors/erc20_legacy_transfer.rs +++ b/crates/torii/core/src/processors/erc20_legacy_transfer.rs @@ -6,7 +6,7 @@ use starknet::core::types::{Event, U256}; use starknet::providers::Provider; use tracing::debug; -use super::EventProcessor; +use super::{EventProcessor, EventProcessorConfig}; use crate::sql::Sql; pub(crate) const LOG_TARGET: &str = "torii_core::processors::erc20_legacy_transfer"; @@ -42,6 +42,7 @@ where block_timestamp: u64, event_id: &str, event: &Event, + _config: &EventProcessorConfig, ) -> Result<(), Error> { let token_address = event.from_address; let from = event.data[0]; diff --git a/crates/torii/core/src/processors/erc20_transfer.rs b/crates/torii/core/src/processors/erc20_transfer.rs index 7ed1620503..64f50d13a2 100644 --- a/crates/torii/core/src/processors/erc20_transfer.rs +++ b/crates/torii/core/src/processors/erc20_transfer.rs @@ -6,7 +6,7 @@ use starknet::core::types::{Event, U256}; use starknet::providers::Provider; use tracing::debug; -use super::EventProcessor; +use super::{EventProcessor, EventProcessorConfig}; use crate::sql::Sql; pub(crate) const LOG_TARGET: &str = "torii_core::processors::erc20_transfer"; @@ -42,6 +42,7 @@ where block_timestamp: u64, event_id: &str, event: &Event, + _config: &EventProcessorConfig, ) -> Result<(), Error> { let token_address = event.from_address; let from = event.keys[1]; diff --git a/crates/torii/core/src/processors/erc721_legacy_transfer.rs b/crates/torii/core/src/processors/erc721_legacy_transfer.rs index 198a1ebbd9..b3fdcbbfe8 100644 --- a/crates/torii/core/src/processors/erc721_legacy_transfer.rs +++ b/crates/torii/core/src/processors/erc721_legacy_transfer.rs @@ -6,7 +6,7 @@ use starknet::core::types::{Event, U256}; use starknet::providers::Provider; use tracing::debug; -use super::EventProcessor; +use super::{EventProcessor, EventProcessorConfig}; use crate::sql::Sql; pub(crate) const LOG_TARGET: &str = "torii_core::processors::erc721_legacy_transfer"; @@ -42,6 +42,7 @@ where block_timestamp: u64, event_id: &str, event: &Event, + _config: &EventProcessorConfig, ) -> Result<(), Error> { let token_address = event.from_address; let from = event.data[0]; diff --git a/crates/torii/core/src/processors/erc721_transfer.rs b/crates/torii/core/src/processors/erc721_transfer.rs index 349bdbea24..266ea18e51 100644 --- a/crates/torii/core/src/processors/erc721_transfer.rs +++ b/crates/torii/core/src/processors/erc721_transfer.rs @@ -6,7 +6,7 @@ use starknet::core::types::{Event, U256}; use starknet::providers::Provider; use tracing::debug; -use super::EventProcessor; +use super::{EventProcessor, EventProcessorConfig}; use crate::sql::Sql; pub(crate) const LOG_TARGET: &str = "torii_core::processors::erc721_transfer"; @@ -42,6 +42,7 @@ where block_timestamp: u64, event_id: &str, event: &Event, + _config: &EventProcessorConfig, ) -> Result<(), Error> { let token_address = event.from_address; let from = event.keys[1]; diff --git a/crates/torii/core/src/processors/event_message.rs b/crates/torii/core/src/processors/event_message.rs index 6236cda856..2fbb982284 100644 --- a/crates/torii/core/src/processors/event_message.rs +++ b/crates/torii/core/src/processors/event_message.rs @@ -6,7 +6,7 @@ use starknet::core::types::{Event, Felt}; use starknet::providers::Provider; use tracing::info; -use super::EventProcessor; +use super::{EventProcessor, EventProcessorConfig}; use crate::sql::Sql; pub(crate) const LOG_TARGET: &str = "torii_core::processors::event_message"; @@ -35,6 +35,7 @@ where block_timestamp: u64, event_id: &str, event: &Event, + config: &EventProcessorConfig, ) -> Result<(), Error> { // Torii version is coupled to the world version, so we can expect the event to be well // formed. @@ -72,7 +73,8 @@ where entity.deserialize(&mut keys_and_unpacked)?; // TODO: this must come from some torii's configuration. - let historical = false; + let historical = + config.historical_events.contains(&format!("{}-{}", model.namespace, model.name)); db.set_event_message(entity, event_id, block_timestamp, historical).await?; Ok(()) } diff --git a/crates/torii/core/src/processors/metadata_update.rs b/crates/torii/core/src/processors/metadata_update.rs index 8a1b68f7c2..76a9f37c12 100644 --- a/crates/torii/core/src/processors/metadata_update.rs +++ b/crates/torii/core/src/processors/metadata_update.rs @@ -15,7 +15,7 @@ use starknet::providers::Provider; use tokio_util::bytes::Bytes; use tracing::{error, info}; -use super::EventProcessor; +use super::{EventProcessor, EventProcessorConfig}; use crate::sql::Sql; const IPFS_URL: &str = "https://cartridge.infura-ipfs.io/ipfs/"; @@ -47,6 +47,7 @@ where block_timestamp: u64, _event_id: &str, event: &Event, + _config: &EventProcessorConfig, ) -> Result<(), Error> { // Torii version is coupled to the world version, so we can expect the event to be well // formed. diff --git a/crates/torii/core/src/processors/mod.rs b/crates/torii/core/src/processors/mod.rs index 58dad65928..fa24de5e9b 100644 --- a/crates/torii/core/src/processors/mod.rs +++ b/crates/torii/core/src/processors/mod.rs @@ -1,3 +1,5 @@ +use std::collections::HashSet; + use anyhow::{Error, Result}; use async_trait::async_trait; use dojo_world::contracts::world::WorldContractReader; @@ -24,6 +26,11 @@ pub mod store_update_record; const MODEL_INDEX: usize = 0; const ENTITY_ID_INDEX: usize = 1; +#[derive(Clone, Debug, Default)] +pub struct EventProcessorConfig { + pub historical_events: HashSet, +} + #[async_trait] pub trait EventProcessor

: Send + Sync where @@ -46,6 +53,7 @@ where block_timestamp: u64, event_id: &str, event: &Event, + _config: &EventProcessorConfig, ) -> Result<(), Error>; } diff --git a/crates/torii/core/src/processors/raw_event.rs b/crates/torii/core/src/processors/raw_event.rs index 079247dc54..ff496ef74b 100644 --- a/crates/torii/core/src/processors/raw_event.rs +++ b/crates/torii/core/src/processors/raw_event.rs @@ -4,7 +4,7 @@ use dojo_world::contracts::world::WorldContractReader; use starknet::core::types::Event; use starknet::providers::Provider; -use super::EventProcessor; +use super::{EventProcessor, EventProcessorConfig}; use crate::sql::Sql; #[derive(Default, Debug)] @@ -31,6 +31,7 @@ where _block_timestamp: u64, _event_id: &str, _event: &Event, + _config: &EventProcessorConfig, ) -> Result<(), Error> { // We can choose to consider them, or not. diff --git a/crates/torii/core/src/processors/register_event.rs b/crates/torii/core/src/processors/register_event.rs index 79ab0067f0..6b5f0af9a0 100644 --- a/crates/torii/core/src/processors/register_event.rs +++ b/crates/torii/core/src/processors/register_event.rs @@ -7,7 +7,7 @@ use starknet::core::types::Event; use starknet::providers::Provider; use tracing::{debug, info}; -use super::EventProcessor; +use super::{EventProcessor, EventProcessorConfig}; use crate::sql::Sql; pub(crate) const LOG_TARGET: &str = "torii_core::processors::register_event"; @@ -38,6 +38,7 @@ where block_timestamp: u64, _event_id: &str, event: &Event, + _config: &EventProcessorConfig, ) -> Result<(), Error> { // Torii version is coupled to the world version, so we can expect the event to be well // formed. diff --git a/crates/torii/core/src/processors/register_model.rs b/crates/torii/core/src/processors/register_model.rs index 6f25230b39..58c7333a2f 100644 --- a/crates/torii/core/src/processors/register_model.rs +++ b/crates/torii/core/src/processors/register_model.rs @@ -7,7 +7,7 @@ use starknet::core::types::Event; use starknet::providers::Provider; use tracing::{debug, info}; -use super::EventProcessor; +use super::{EventProcessor, EventProcessorConfig}; use crate::sql::Sql; pub(crate) const LOG_TARGET: &str = "torii_core::processors::register_model"; @@ -38,6 +38,7 @@ where block_timestamp: u64, _event_id: &str, event: &Event, + _config: &EventProcessorConfig, ) -> Result<(), Error> { // Torii version is coupled to the world version, so we can expect the event to be well // formed. diff --git a/crates/torii/core/src/processors/store_del_record.rs b/crates/torii/core/src/processors/store_del_record.rs index 99f8ba579d..ad380885e1 100644 --- a/crates/torii/core/src/processors/store_del_record.rs +++ b/crates/torii/core/src/processors/store_del_record.rs @@ -6,7 +6,7 @@ use starknet::core::types::Event; use starknet::providers::Provider; use tracing::info; -use super::EventProcessor; +use super::{EventProcessor, EventProcessorConfig}; use crate::sql::Sql; pub(crate) const LOG_TARGET: &str = "torii_core::processors::store_del_record"; @@ -35,6 +35,7 @@ where block_timestamp: u64, event_id: &str, event: &Event, + _config: &EventProcessorConfig, ) -> Result<(), Error> { // Torii version is coupled to the world version, so we can expect the event to be well // formed. diff --git a/crates/torii/core/src/processors/store_set_record.rs b/crates/torii/core/src/processors/store_set_record.rs index 5faebc9855..fdbdd14646 100644 --- a/crates/torii/core/src/processors/store_set_record.rs +++ b/crates/torii/core/src/processors/store_set_record.rs @@ -6,7 +6,7 @@ use starknet::core::types::Event; use starknet::providers::Provider; use tracing::info; -use super::EventProcessor; +use super::{EventProcessor, EventProcessorConfig}; use crate::sql::utils::felts_to_sql_string; use crate::sql::Sql; @@ -36,6 +36,7 @@ where block_timestamp: u64, event_id: &str, event: &Event, + _config: &EventProcessorConfig, ) -> Result<(), Error> { // Torii version is coupled to the world version, so we can expect the event to be well // formed. diff --git a/crates/torii/core/src/processors/store_update_member.rs b/crates/torii/core/src/processors/store_update_member.rs index 567e9e18d0..632d5999c6 100644 --- a/crates/torii/core/src/processors/store_update_member.rs +++ b/crates/torii/core/src/processors/store_update_member.rs @@ -9,7 +9,7 @@ use starknet::core::utils::get_selector_from_name; use starknet::providers::Provider; use tracing::{info, warn}; -use super::EventProcessor; +use super::{EventProcessor, EventProcessorConfig}; use crate::processors::{ENTITY_ID_INDEX, MODEL_INDEX}; use crate::sql::Sql; @@ -50,6 +50,7 @@ where block_timestamp: u64, event_id: &str, event: &Event, + _config: &EventProcessorConfig, ) -> Result<(), Error> { let model_id = event.data[MODEL_INDEX]; let entity_id = event.data[ENTITY_ID_INDEX]; diff --git a/crates/torii/core/src/processors/store_update_record.rs b/crates/torii/core/src/processors/store_update_record.rs index ae4bfdac91..4cde69dc68 100644 --- a/crates/torii/core/src/processors/store_update_record.rs +++ b/crates/torii/core/src/processors/store_update_record.rs @@ -7,7 +7,7 @@ use starknet::core::types::Event; use starknet::providers::Provider; use tracing::info; -use super::EventProcessor; +use super::{EventProcessor, EventProcessorConfig}; use crate::sql::Sql; pub(crate) const LOG_TARGET: &str = "torii_core::processors::store_update_record"; @@ -36,6 +36,7 @@ where block_timestamp: u64, event_id: &str, event: &Event, + _config: &EventProcessorConfig, ) -> Result<(), Error> { // Torii version is coupled to the world version, so we can expect the event to be well // formed. diff --git a/crates/torii/core/src/sql/mod.rs b/crates/torii/core/src/sql/mod.rs index 3c20222a9a..ae72371039 100644 --- a/crates/torii/core/src/sql/mod.rs +++ b/crates/torii/core/src/sql/mod.rs @@ -20,7 +20,7 @@ use crate::executor::{ Argument, DeleteEntityQuery, EventMessageQuery, QueryMessage, QueryType, ResetCursorsQuery, SetHeadQuery, UpdateCursorsQuery, }; -use crate::types::ContractType; +use crate::types::Contract; use crate::utils::utc_dt_string_from_timestamp; type IsEventMessage = bool; @@ -59,7 +59,7 @@ impl Sql { pub async fn new( pool: Pool, executor: UnboundedSender, - contracts: &HashMap, + contracts: &Vec, ) -> Result { for contract in contracts { executor.send(QueryMessage::other( @@ -67,9 +67,9 @@ impl Sql { ?, ?)" .to_string(), vec![ - Argument::FieldElement(*contract.0), - Argument::FieldElement(*contract.0), - Argument::String(contract.1.to_string()), + Argument::FieldElement(contract.address), + Argument::FieldElement(contract.address), + Argument::String(contract.r#type.to_string()), ], ))?; } diff --git a/crates/torii/core/src/sql/test.rs b/crates/torii/core/src/sql/test.rs index fd1539b49c..65076fffa3 100644 --- a/crates/torii/core/src/sql/test.rs +++ b/crates/torii/core/src/sql/test.rs @@ -24,7 +24,7 @@ use tokio::sync::broadcast; use crate::engine::{Engine, EngineConfig, Processors}; use crate::executor::Executor; use crate::sql::Sql; -use crate::types::ContractType; +use crate::types::{Contract, ContractType}; pub async fn bootstrap_engine

( world: WorldContractReader

, @@ -45,7 +45,7 @@ where EngineConfig::default(), shutdown_tx, None, - Arc::new(HashMap::from([(world_address, ContractType::WORLD)])), + &[Contract { address: world_address, r#type: ContractType::WORLD }], ); let data = engine.fetch_range(0, to, &HashMap::new()).await.unwrap(); @@ -127,7 +127,7 @@ async fn test_load_from_remote(sequencer: &RunnerCtx) { let db = Sql::new( pool.clone(), sender.clone(), - &HashMap::from([(world_reader.address, ContractType::WORLD)]), + &vec![Contract { address: world_reader.address, r#type: ContractType::WORLD }], ) .await .unwrap(); @@ -285,7 +285,7 @@ async fn test_load_from_remote_del(sequencer: &RunnerCtx) { let db = Sql::new( pool.clone(), sender.clone(), - &HashMap::from([(world_reader.address, ContractType::WORLD)]), + &vec![Contract { address: world_reader.address, r#type: ContractType::WORLD }], ) .await .unwrap(); @@ -371,7 +371,7 @@ async fn test_update_with_set_record(sequencer: &RunnerCtx) { let db = Sql::new( pool.clone(), sender.clone(), - &HashMap::from([(world_reader.address, ContractType::WORLD)]), + &vec![Contract { address: world_reader.address, r#type: ContractType::WORLD }], ) .await .unwrap(); diff --git a/crates/torii/core/src/types.rs b/crates/torii/core/src/types.rs index a9ecf79a0d..96d2f68ca4 100644 --- a/crates/torii/core/src/types.rs +++ b/crates/torii/core/src/types.rs @@ -1,6 +1,4 @@ use core::fmt; -use std::collections::VecDeque; -use std::path::PathBuf; use std::str::FromStr; use chrono::{DateTime, Utc}; @@ -123,28 +121,13 @@ pub struct Event { pub executed_at: DateTime, pub created_at: DateTime, } - -#[derive(Default, Deserialize, Debug, Clone)] -pub struct ToriiConfig { - /// contract addresses to index - pub contracts: VecDeque, -} - -impl ToriiConfig { - pub fn load_from_path(path: &PathBuf) -> Result { - let config = std::fs::read_to_string(path)?; - let config: Self = toml::from_str(&config)?; - Ok(config) - } -} - -#[derive(Deserialize, Debug, Clone, Copy)] +#[derive(Serialize, Deserialize, Debug, Clone, Copy)] pub struct Contract { pub address: Felt, pub r#type: ContractType, } -#[derive(Deserialize, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub enum ContractType { WORLD, ERC20, diff --git a/crates/torii/graphql/src/tests/metadata_test.rs b/crates/torii/graphql/src/tests/metadata_test.rs index d92cca5854..d6b00ea72b 100644 --- a/crates/torii/graphql/src/tests/metadata_test.rs +++ b/crates/torii/graphql/src/tests/metadata_test.rs @@ -1,14 +1,12 @@ #[cfg(test)] mod tests { - use std::collections::HashMap; - use dojo_world::config::{ProfileConfig, WorldMetadata}; use sqlx::SqlitePool; use starknet::core::types::Felt; use tokio::sync::broadcast; use torii_core::executor::Executor; use torii_core::sql::Sql; - use torii_core::types::ContractType; + use torii_core::types::{Contract, ContractType}; use crate::schema::build_schema; use crate::tests::{run_graphql_query, Connection, Content, Metadata as SqlMetadata, Social}; @@ -58,10 +56,13 @@ mod tests { tokio::spawn(async move { executor.run().await.unwrap(); }); - let mut db = - Sql::new(pool.clone(), sender, &HashMap::from([(Felt::ZERO, ContractType::WORLD)])) - .await - .unwrap(); + let mut db = Sql::new( + pool.clone(), + sender, + &vec![Contract { address: Felt::ZERO, r#type: ContractType::WORLD }], + ) + .await + .unwrap(); let schema = build_schema(&pool).await.unwrap(); let cover_img = "QWxsIHlvdXIgYmFzZSBiZWxvbmcgdG8gdXM="; @@ -119,10 +120,13 @@ mod tests { tokio::spawn(async move { executor.run().await.unwrap(); }); - let mut db = - Sql::new(pool.clone(), sender, &HashMap::from([(Felt::ZERO, ContractType::WORLD)])) - .await - .unwrap(); + let mut db = Sql::new( + pool.clone(), + sender, + &vec![Contract { address: Felt::ZERO, r#type: ContractType::WORLD }], + ) + .await + .unwrap(); let schema = build_schema(&pool).await.unwrap(); db.set_metadata(&RESOURCE, URI, BLOCK_TIMESTAMP).unwrap(); diff --git a/crates/torii/graphql/src/tests/mod.rs b/crates/torii/graphql/src/tests/mod.rs index 7a54dcce72..c21419bbba 100644 --- a/crates/torii/graphql/src/tests/mod.rs +++ b/crates/torii/graphql/src/tests/mod.rs @@ -30,7 +30,7 @@ use tokio_stream::StreamExt; use torii_core::engine::{Engine, EngineConfig, Processors}; use torii_core::executor::Executor; use torii_core::sql::Sql; -use torii_core::types::ContractType; +use torii_core::types::{Contract, ContractType}; mod entities_test; mod events_test; @@ -345,9 +345,13 @@ pub async fn spinup_types_test(path: &str) -> Result { tokio::spawn(async move { executor.run().await.unwrap(); }); - let db = Sql::new(pool.clone(), sender, &HashMap::from([(world_address, ContractType::WORLD)])) - .await - .unwrap(); + let db = Sql::new( + pool.clone(), + sender, + &vec![Contract { address: world_address, r#type: ContractType::WORLD }], + ) + .await + .unwrap(); let (shutdown_tx, _) = broadcast::channel(1); let mut engine = Engine::new( @@ -358,7 +362,7 @@ pub async fn spinup_types_test(path: &str) -> Result { EngineConfig::default(), shutdown_tx, None, - Arc::new(HashMap::from([(world_address, ContractType::WORLD)])), + &[Contract { address: world_address, r#type: ContractType::WORLD }], ); let to = account.provider().block_hash_and_number().await?.block_number; diff --git a/crates/torii/graphql/src/tests/subscription_test.rs b/crates/torii/graphql/src/tests/subscription_test.rs index 11ef4585eb..583779c97d 100644 --- a/crates/torii/graphql/src/tests/subscription_test.rs +++ b/crates/torii/graphql/src/tests/subscription_test.rs @@ -1,6 +1,5 @@ #[cfg(test)] mod tests { - use std::collections::HashMap; use std::str::FromStr; use std::time::Duration; @@ -17,7 +16,7 @@ mod tests { use torii_core::executor::Executor; use torii_core::sql::utils::felts_to_sql_string; use torii_core::sql::Sql; - use torii_core::types::ContractType; + use torii_core::types::{Contract, ContractType}; use crate::tests::{model_fixtures, run_graphql_subscription}; use crate::utils; @@ -31,10 +30,13 @@ mod tests { tokio::spawn(async move { executor.run().await.unwrap(); }); - let mut db = - Sql::new(pool.clone(), sender, &HashMap::from([(Felt::ZERO, ContractType::WORLD)])) - .await - .unwrap(); + let mut db = Sql::new( + pool.clone(), + sender, + &vec![Contract { address: Felt::ZERO, r#type: ContractType::WORLD }], + ) + .await + .unwrap(); model_fixtures(&mut db).await; // 0. Preprocess expected entity value @@ -175,10 +177,13 @@ mod tests { tokio::spawn(async move { executor.run().await.unwrap(); }); - let mut db = - Sql::new(pool.clone(), sender, &HashMap::from([(Felt::ZERO, ContractType::WORLD)])) - .await - .unwrap(); + let mut db = Sql::new( + pool.clone(), + sender, + &vec![Contract { address: Felt::ZERO, r#type: ContractType::WORLD }], + ) + .await + .unwrap(); model_fixtures(&mut db).await; // 0. Preprocess expected entity value @@ -299,10 +304,13 @@ mod tests { tokio::spawn(async move { executor.run().await.unwrap(); }); - let mut db = - Sql::new(pool.clone(), sender, &HashMap::from([(Felt::ZERO, ContractType::WORLD)])) - .await - .unwrap(); + let mut db = Sql::new( + pool.clone(), + sender, + &vec![Contract { address: Felt::ZERO, r#type: ContractType::WORLD }], + ) + .await + .unwrap(); // 0. Preprocess model value let namespace = "types_test".to_string(); let model_name = "Subrecord".to_string(); @@ -373,10 +381,13 @@ mod tests { tokio::spawn(async move { executor.run().await.unwrap(); }); - let mut db = - Sql::new(pool.clone(), sender, &HashMap::from([(Felt::ZERO, ContractType::WORLD)])) - .await - .unwrap(); + let mut db = Sql::new( + pool.clone(), + sender, + &vec![Contract { address: Felt::ZERO, r#type: ContractType::WORLD }], + ) + .await + .unwrap(); // 0. Preprocess model value let namespace = "types_test".to_string(); let model_name = "Subrecord".to_string(); @@ -448,10 +459,13 @@ mod tests { tokio::spawn(async move { executor.run().await.unwrap(); }); - let mut db = - Sql::new(pool.clone(), sender, &HashMap::from([(Felt::ZERO, ContractType::WORLD)])) - .await - .unwrap(); + let mut db = Sql::new( + pool.clone(), + sender, + &vec![Contract { address: Felt::ZERO, r#type: ContractType::WORLD }], + ) + .await + .unwrap(); let block_timestamp: u64 = 1710754478_u64; let (tx, mut rx) = mpsc::channel(7); tokio::spawn(async move { diff --git a/crates/torii/grpc/src/server/tests/entities_test.rs b/crates/torii/grpc/src/server/tests/entities_test.rs index e7996092e9..9a0bb39f31 100644 --- a/crates/torii/grpc/src/server/tests/entities_test.rs +++ b/crates/torii/grpc/src/server/tests/entities_test.rs @@ -25,7 +25,7 @@ use tokio::sync::broadcast; use torii_core::engine::{Engine, EngineConfig, Processors}; use torii_core::executor::Executor; use torii_core::sql::Sql; -use torii_core::types::ContractType; +use torii_core::types::{Contract, ContractType}; use crate::proto::types::KeysClause; use crate::server::DojoWorld; @@ -92,9 +92,13 @@ async fn test_entities_queries(sequencer: &RunnerCtx) { tokio::spawn(async move { executor.run().await.unwrap(); }); - let db = Sql::new(pool.clone(), sender, &HashMap::from([(world_address, ContractType::WORLD)])) - .await - .unwrap(); + let db = Sql::new( + pool.clone(), + sender, + &vec![Contract { address: world_address, r#type: ContractType::WORLD }], + ) + .await + .unwrap(); let (shutdown_tx, _) = broadcast::channel(1); let mut engine = Engine::new( @@ -105,7 +109,7 @@ async fn test_entities_queries(sequencer: &RunnerCtx) { EngineConfig::default(), shutdown_tx, None, - Arc::new(HashMap::from([(world_address, ContractType::WORLD)])), + &[Contract { address: world_address, r#type: ContractType::WORLD }], ); let to = provider.block_hash_and_number().await.unwrap().block_number; diff --git a/crates/torii/libp2p/src/tests.rs b/crates/torii/libp2p/src/tests.rs index e033d6c5fb..0156093935 100644 --- a/crates/torii/libp2p/src/tests.rs +++ b/crates/torii/libp2p/src/tests.rs @@ -524,7 +524,6 @@ mod test { #[cfg(not(target_arch = "wasm32"))] #[tokio::test] async fn test_client_messaging() -> Result<(), Box> { - use std::collections::HashMap; use std::time::Duration; use dojo_types::schema::{Member, Struct, Ty}; @@ -541,7 +540,7 @@ mod test { use tokio::time::sleep; use torii_core::executor::Executor; use torii_core::sql::Sql; - use torii_core::types::ContractType; + use torii_core::types::{Contract, ContractType}; use crate::server::Relay; use crate::typed_data::{Domain, Field, SimpleField, TypedData}; @@ -578,10 +577,13 @@ mod test { tokio::spawn(async move { executor.run().await.unwrap(); }); - let mut db = - Sql::new(pool.clone(), sender, &HashMap::from([(Felt::ZERO, ContractType::WORLD)])) - .await - .unwrap(); + let mut db = Sql::new( + pool.clone(), + sender, + &vec![Contract { address: Felt::ZERO, r#type: ContractType::WORLD }], + ) + .await + .unwrap(); // Register the model of our Message db.register_model(