diff --git a/bin/torii/src/main.rs b/bin/torii/src/main.rs index 2346771da7..fd0849842f 100644 --- a/bin/torii/src/main.rs +++ b/bin/torii/src/main.rs @@ -20,9 +20,9 @@ use dojo_metrics::{metrics_process, prometheus_exporter}; use dojo_world::contracts::world::WorldContractReader; use sqlx::sqlite::{SqliteConnectOptions, SqlitePoolOptions}; use sqlx::SqlitePool; -use starknet::core::types::Felt; +use starknet::core::types::{BlockId, BlockTag, Felt}; use starknet::providers::jsonrpc::HttpTransport; -use starknet::providers::JsonRpcClient; +use starknet::providers::{JsonRpcClient, Provider}; use tokio::sync::broadcast; use tokio::sync::broadcast::Sender; use tokio_stream::StreamExt; @@ -160,7 +160,9 @@ async fn main() -> anyhow::Result<()> { // Get world address let world = WorldContractReader::new(args.world_address, &provider); - let db = Sql::new(pool.clone(), args.world_address).await?; + let class_hash = + provider.get_class_hash_at(BlockId::Tag(BlockTag::Pending), args.world_address).await?; + let db = Sql::new(pool.clone(), args.world_address, class_hash).await?; let processors = Processors { event: vec![ Box::new(RegisterModelProcessor), diff --git a/crates/torii/core/src/sql.rs b/crates/torii/core/src/sql.rs index a16f5c1d5a..125a050f1d 100644 --- a/crates/torii/core/src/sql.rs +++ b/crates/torii/core/src/sql.rs @@ -37,7 +37,11 @@ pub struct Sql { } impl Sql { - pub async fn new(pool: Pool, world_address: Felt) -> Result { + pub async fn new( + pool: Pool, + world_address: Felt, + world_class_hash: Felt, + ) -> Result { let mut query_queue = QueryQueue::new(pool.clone()); query_queue.enqueue( @@ -45,8 +49,12 @@ impl Sql { vec![Argument::FieldElement(world_address), Argument::Int(0)], ); query_queue.enqueue( - "INSERT OR IGNORE INTO worlds (id, world_address) VALUES (?, ?)", - vec![Argument::FieldElement(world_address), Argument::FieldElement(world_address)], + "INSERT OR IGNORE INTO worlds (id, world_address, world_class_hash) VALUES (?, ?, ?)", + vec![ + Argument::FieldElement(world_address), + Argument::FieldElement(world_address), + Argument::FieldElement(world_class_hash), + ], ); query_queue.execute_all().await?; diff --git a/crates/torii/core/src/sql_test.rs b/crates/torii/core/src/sql_test.rs index 480a06e6ce..d2c0351c12 100644 --- a/crates/torii/core/src/sql_test.rs +++ b/crates/torii/core/src/sql_test.rs @@ -125,7 +125,13 @@ async fn test_load_from_remote() { TransactionWaiter::new(tx.transaction_hash, &provider).await.unwrap(); - let mut db = Sql::new(pool.clone(), world_address).await.unwrap(); + let mut db = Sql::new( + pool.clone(), + world_address, + provider.get_class_hash_at(BlockId::Tag(BlockTag::Pending), world_address).await.unwrap(), + ) + .await + .unwrap(); let _ = bootstrap_engine(world, db.clone(), &provider).await; let _block_timestamp = 1710754478_u64; @@ -303,7 +309,13 @@ async fn test_load_from_remote_del() { tokio::time::sleep(tokio::time::Duration::from_secs(1)).await; - let mut db = Sql::new(pool.clone(), world_address).await.unwrap(); + let mut db = Sql::new( + pool.clone(), + world_address, + provider.get_class_hash_at(BlockId::Tag(BlockTag::Pending), world_address).await.unwrap(), + ) + .await + .unwrap(); let _ = bootstrap_engine(world, db.clone(), &provider).await; assert_eq!(count_table("dojo_examples-PlayerConfig", &pool).await, 0); diff --git a/crates/torii/graphql/src/tests/metadata_test.rs b/crates/torii/graphql/src/tests/metadata_test.rs index 85ca2340fe..9c917da494 100644 --- a/crates/torii/graphql/src/tests/metadata_test.rs +++ b/crates/torii/graphql/src/tests/metadata_test.rs @@ -47,7 +47,7 @@ mod tests { #[sqlx::test(migrations = "../migrations")] async fn test_metadata(pool: SqlitePool) { - let mut db = Sql::new(pool.clone(), Felt::ZERO).await.unwrap(); + let mut db = Sql::new(pool.clone(), Felt::ZERO, Felt::ZERO).await.unwrap(); let schema = build_schema(&pool).await.unwrap(); let cover_img = "QWxsIHlvdXIgYmFzZSBiZWxvbmcgdG8gdXM="; @@ -97,7 +97,7 @@ mod tests { #[sqlx::test(migrations = "../migrations")] async fn test_empty_content(pool: SqlitePool) { - let mut db = Sql::new(pool.clone(), Felt::ZERO).await.unwrap(); + let mut db = Sql::new(pool.clone(), Felt::ZERO, Felt::ZERO).await.unwrap(); let schema = build_schema(&pool).await.unwrap(); db.set_metadata(&RESOURCE, URI, BLOCK_TIMESTAMP); diff --git a/crates/torii/graphql/src/tests/mod.rs b/crates/torii/graphql/src/tests/mod.rs index 8bfba50696..54db3b0b0a 100644 --- a/crates/torii/graphql/src/tests/mod.rs +++ b/crates/torii/graphql/src/tests/mod.rs @@ -303,7 +303,7 @@ pub async fn spinup_types_test() -> Result { migration.resolve_variable(migration.world.clone().unwrap().contract_address).unwrap(); - let db = Sql::new(pool.clone(), migration.world_address().unwrap()).await.unwrap(); + let db = Sql::new(pool.clone(), migration.world_address().unwrap(), Felt::ZERO).await.unwrap(); let sequencer = KatanaRunner::new().expect("Failed to start runner."); diff --git a/crates/torii/graphql/src/tests/subscription_test.rs b/crates/torii/graphql/src/tests/subscription_test.rs index 015df8789c..c0bbc98bbb 100644 --- a/crates/torii/graphql/src/tests/subscription_test.rs +++ b/crates/torii/graphql/src/tests/subscription_test.rs @@ -21,7 +21,7 @@ mod tests { #[sqlx::test(migrations = "../migrations")] #[serial] async fn test_entity_subscription(pool: SqlitePool) { - let mut db = Sql::new(pool.clone(), Felt::ZERO).await.unwrap(); + let mut db = Sql::new(pool.clone(), Felt::ZERO, Felt::ZERO).await.unwrap(); model_fixtures(&mut db).await; // 0. Preprocess expected entity value @@ -147,7 +147,7 @@ mod tests { #[sqlx::test(migrations = "../migrations")] #[serial] async fn test_entity_subscription_with_id(pool: SqlitePool) { - let mut db = Sql::new(pool.clone(), Felt::ZERO).await.unwrap(); + let mut db = Sql::new(pool.clone(), Felt::ZERO, Felt::ZERO).await.unwrap(); model_fixtures(&mut db).await; // 0. Preprocess expected entity value @@ -252,7 +252,7 @@ mod tests { #[sqlx::test(migrations = "../migrations")] #[serial] async fn test_model_subscription(pool: SqlitePool) { - let mut db = Sql::new(pool.clone(), Felt::ZERO).await.unwrap(); + let mut db = Sql::new(pool.clone(), Felt::ZERO, Felt::ZERO).await.unwrap(); // 0. Preprocess model value let namespace = "types_test".to_string(); let model_name = "Subrecord".to_string(); @@ -316,7 +316,7 @@ mod tests { #[sqlx::test(migrations = "../migrations")] #[serial] async fn test_model_subscription_with_id(pool: SqlitePool) { - let mut db = Sql::new(pool.clone(), Felt::ZERO).await.unwrap(); + let mut db = Sql::new(pool.clone(), Felt::ZERO, Felt::ZERO).await.unwrap(); // 0. Preprocess model value let namespace = "types_test".to_string(); let model_name = "Subrecord".to_string(); @@ -381,7 +381,7 @@ mod tests { #[sqlx::test(migrations = "../migrations")] #[serial] async fn test_event_emitted(pool: SqlitePool) { - let mut db = Sql::new(pool.clone(), Felt::ZERO).await.unwrap(); + let mut db = Sql::new(pool.clone(), Felt::ZERO, Felt::ZERO).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/proto/types.proto b/crates/torii/grpc/proto/types.proto index fdb49a1f92..729972b3ad 100644 --- a/crates/torii/grpc/proto/types.proto +++ b/crates/torii/grpc/proto/types.proto @@ -8,10 +8,6 @@ message WorldMetadata { string world_address = 1; // The hex-encoded class hash of the world. string world_class_hash = 2; - // The hex-encoded address of the executor. - string executor_address = 3; - // The hex-encoded class hash of the executor. - string executor_class_hash = 4; // A list of metadata for all registered components in the world. repeated ModelMetadata models = 5; } diff --git a/crates/torii/grpc/src/server/mod.rs b/crates/torii/grpc/src/server/mod.rs index 742d7ec9de..f667a60ba3 100644 --- a/crates/torii/grpc/src/server/mod.rs +++ b/crates/torii/grpc/src/server/mod.rs @@ -113,14 +113,8 @@ impl DojoWorld { impl DojoWorld { pub async fn metadata(&self) -> Result { - let (world_address, world_class_hash, executor_address, executor_class_hash): ( - String, - String, - String, - String, - ) = sqlx::query_as(&format!( - "SELECT world_address, world_class_hash, executor_address, executor_class_hash FROM \ - worlds WHERE id = '{:#x}'", + let (world_address, world_class_hash): (String, String) = sqlx::query_as(&format!( + "SELECT world_address, world_class_hash FROM worlds WHERE id = '{:#x}'", self.world_address )) .fetch_one(&self.pool) @@ -163,13 +157,7 @@ impl DojoWorld { }); } - Ok(proto::types::WorldMetadata { - world_address, - world_class_hash, - executor_address, - executor_class_hash, - models: models_metadata, - }) + Ok(proto::types::WorldMetadata { world_address, world_class_hash, models: models_metadata }) } async fn entities_all( diff --git a/crates/torii/grpc/src/server/tests/entities_test.rs b/crates/torii/grpc/src/server/tests/entities_test.rs index 205fda416e..d38dcccb35 100644 --- a/crates/torii/grpc/src/server/tests/entities_test.rs +++ b/crates/torii/grpc/src/server/tests/entities_test.rs @@ -14,9 +14,10 @@ use scarb::ops; use sozo_ops::migration::execute_strategy; use sqlx::sqlite::{SqliteConnectOptions, SqlitePoolOptions}; use starknet::accounts::{Account, Call}; +use starknet::core::types::{BlockId, BlockTag}; use starknet::core::utils::get_selector_from_name; use starknet::providers::jsonrpc::HttpTransport; -use starknet::providers::JsonRpcClient; +use starknet::providers::{JsonRpcClient, Provider}; use starknet_crypto::poseidon_hash_many; use tokio::sync::broadcast; use torii_core::engine::{Engine, EngineConfig, Processors}; @@ -95,7 +96,13 @@ async fn test_entities_queries() { TransactionWaiter::new(tx.transaction_hash, &provider).await.unwrap(); - let db = Sql::new(pool.clone(), world_address).await.unwrap(); + let db = Sql::new( + pool.clone(), + world_address, + provider.get_class_hash_at(BlockId::Tag(BlockTag::Pending), world_address).await.unwrap(), + ) + .await + .unwrap(); let (shutdown_tx, _) = broadcast::channel(1); let mut engine = Engine::new( diff --git a/crates/torii/libp2p/src/tests.rs b/crates/torii/libp2p/src/tests.rs index 2ae78be058..f52caff1d5 100644 --- a/crates/torii/libp2p/src/tests.rs +++ b/crates/torii/libp2p/src/tests.rs @@ -304,7 +304,7 @@ mod test { let account = sequencer.account_data(0); - let mut db = Sql::new(pool.clone(), Felt::from_bytes_be(&[0; 32])).await?; + let mut db = Sql::new(pool.clone(), Felt::ZERO, Felt::ZERO).await?; // Register the model of our Message db.register_model( diff --git a/crates/torii/migrations/20240709134347_remove_world_executor.sql b/crates/torii/migrations/20240709134347_remove_world_executor.sql new file mode 100644 index 0000000000..ed5e08fb3e --- /dev/null +++ b/crates/torii/migrations/20240709134347_remove_world_executor.sql @@ -0,0 +1,26 @@ +-- NOTE: sqlite does not support deleteing columns. Workaround is to create new table, copy, and delete old. + +-- Create new table without executor_address and executor_class_hash columns +CREATE TABLE worlds_new ( + id TEXT PRIMARY KEY NOT NULL, + world_address TEXT NOT NULL, + world_class_hash TEXT NOT NULL, + created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP +); + +-- Copy from old worlds +INSERT INTO worlds_new (id, world_address, world_class_hash, created_at) +SELECT id, world_address, world_class_hash, created_at +FROM worlds; + +-- Disable foreign keys constraint so we can delete worlds +PRAGMA foreign_keys = OFF; + +-- Drop old worlds +DROP TABLE worlds; + +-- Rename table and recreate indexes +ALTER TABLE worlds_new RENAME TO worlds; + +-- Renable foreign keys +PRAGMA foreign_keys = ON; \ No newline at end of file