Skip to content

Commit

Permalink
fix(torii/processor): flush the queue for store_set_record (#2426)
Browse files Browse the repository at this point in the history
commit-id:fc30f247
  • Loading branch information
lambda-0x authored Sep 14, 2024
1 parent 838082e commit 4fa99ad
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
3 changes: 3 additions & 0 deletions crates/torii/core/src/processors/store_set_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ where
entity.deserialize(&mut keys_and_unpacked)?;

db.set_entity(entity, event_id, block_timestamp, entity_id, model_id, &keys_str).await?;
// early flush the queue because this writes entity keys to a table which is required
// by other store_* events
db.execute().await?;
Ok(())
}
}
92 changes: 92 additions & 0 deletions crates/torii/core/src/sql_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ use crate::processors::generate_event_processors_map;
use crate::processors::register_model::RegisterModelProcessor;
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::sql::Sql;

pub async fn bootstrap_engine<P>(
Expand All @@ -42,6 +44,8 @@ where
event: generate_event_processors_map(vec![
Box::new(RegisterModelProcessor),
Box::new(StoreSetRecordProcessor),
Box::new(StoreUpdateRecordProcessor),
Box::new(StoreUpdateMemberProcessor),
Box::new(StoreDelRecordProcessor),
])?,
..Processors::default()
Expand Down Expand Up @@ -369,6 +373,94 @@ async fn test_get_entity_keys() {
db.execute().await.unwrap();
}

// Start of Selection
#[tokio::test(flavor = "multi_thread")]
async fn test_update_with_set_record() {
// Initialize the SQLite in-memory database
let options =
SqliteConnectOptions::from_str("sqlite::memory:").unwrap().create_if_missing(true);
let pool = SqlitePoolOptions::new().max_connections(5).connect_with(options).await.unwrap();
sqlx::migrate!("../migrations").run(&pool).await.unwrap();

// Set up the compiler test environment
let setup = CompilerTestSetup::from_examples("../../dojo-core", "../../../examples/");
let config = setup.build_test_config("spawn-and-move", Profile::DEV);

let ws = scarb::ops::read_workspace(config.manifest_path(), &config).unwrap();
let manifest_path = Utf8PathBuf::from(config.manifest_path().parent().unwrap());
let target_dir = Utf8PathBuf::from(ws.target_dir().to_string()).join("dev");

// Configure and start the KatanaRunner
let seq_config = KatanaRunnerConfig { n_accounts: 10, ..Default::default() }
.with_db_dir(copy_spawn_and_move_db().as_str());

let sequencer = KatanaRunner::new_with_config(seq_config).expect("Failed to start runner.");
let account = sequencer.account(0);

// Prepare migration with world and seed
let (strat, _) = prepare_migration_with_world_and_seed(
manifest_path,
target_dir,
None,
"dojo_examples",
"dojo_examples",
)
.unwrap();

let actions = strat.contracts.first().unwrap();
let actions_address = get_contract_address(
actions.salt,
strat.base.as_ref().unwrap().diff.local_class_hash,
&[],
strat.world_address,
);

let world = WorldContract::new(strat.world_address, &account);

// Grant writer permissions
let res = world
.grant_writer(&compute_bytearray_hash("dojo_examples"), &ContractAddress(actions_address))
.send_with_cfg(&TxnConfig::init_wait())
.await
.unwrap();

TransactionWaiter::new(res.transaction_hash, &account.provider()).await.unwrap();

// Send spawn transaction
let spawn_res = account
.execute_v1(vec![Call {
to: actions_address,
selector: get_selector_from_name("spawn").unwrap(),
calldata: vec![],
}])
.send_with_cfg(&TxnConfig::init_wait())
.await
.unwrap();

TransactionWaiter::new(spawn_res.transaction_hash, &account.provider()).await.unwrap();

// Send move transaction
let move_res = account
.execute_v1(vec![Call {
to: actions_address,
selector: get_selector_from_name("move").unwrap(),
calldata: vec![Felt::ZERO],
}])
.send_with_cfg(&TxnConfig::init_wait())
.await
.unwrap();

TransactionWaiter::new(move_res.transaction_hash, &account.provider()).await.unwrap();

let world_reader = WorldContractReader::new(strat.world_address, account.provider());

let db = Sql::new(pool.clone(), world_reader.address).await.unwrap();

// Expect bootstrap_engine to error out due to the existing bug
let result = bootstrap_engine(world_reader, db.clone(), account.provider()).await;
assert!(result.is_ok(), "bootstrap_engine should not fail");
}

/// Count the number of rows in a table.
///
/// # Arguments
Expand Down

0 comments on commit 4fa99ad

Please sign in to comment.