Skip to content

Commit

Permalink
refactor(torii): remove executor from world table & write class hash (#…
Browse files Browse the repository at this point in the history
…2166)

* refactor(torii): remove executor from world table & write class hash

* refactor: world class hash column NOT NULL

* fmt

* refactor: use correct world class hash

* chore: remove clippy

* fix: fmt

* fmt

* refactor: latets to pending tag

* fix: fix typo and clippy

---------

Co-authored-by: glihm <[email protected]>
  • Loading branch information
Larkooo and glihm authored Jul 12, 2024
1 parent 600cfca commit dd576ae
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 38 deletions.
8 changes: 5 additions & 3 deletions bin/torii/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
Expand Down
14 changes: 11 additions & 3 deletions crates/torii/core/src/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,24 @@ pub struct Sql {
}

impl Sql {
pub async fn new(pool: Pool<Sqlite>, world_address: Felt) -> Result<Self> {
pub async fn new(
pool: Pool<Sqlite>,
world_address: Felt,
world_class_hash: Felt,
) -> Result<Self> {
let mut query_queue = QueryQueue::new(pool.clone());

query_queue.enqueue(
"INSERT OR IGNORE INTO indexers (id, head) VALUES (?, ?)",
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?;
Expand Down
16 changes: 14 additions & 2 deletions crates/torii/core/src/sql_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions crates/torii/graphql/src/tests/metadata_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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=";
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion crates/torii/graphql/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ pub async fn spinup_types_test() -> Result<SqlitePool> {

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.");

Expand Down
10 changes: 5 additions & 5 deletions crates/torii/graphql/src/tests/subscription_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 0 additions & 4 deletions crates/torii/grpc/proto/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
18 changes: 3 additions & 15 deletions crates/torii/grpc/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,8 @@ impl DojoWorld {

impl DojoWorld {
pub async fn metadata(&self) -> Result<proto::types::WorldMetadata, Error> {
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)
Expand Down Expand Up @@ -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(
Expand Down
11 changes: 9 additions & 2 deletions crates/torii/grpc/src/server/tests/entities_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion crates/torii/libp2p/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
26 changes: 26 additions & 0 deletions crates/torii/migrations/20240709134347_remove_world_executor.sql
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit dd576ae

Please sign in to comment.