Skip to content

Commit

Permalink
fix(sozo): ensure correct event fetching + namespace whitelisting + g…
Browse files Browse the repository at this point in the history
…uest mode (#2937)

* fix: ensure sozo fetches all events even on public networks + add namespaces whitelisting

* fix: run linters

* fix: use correct default values and delimiter for namespaces
  • Loading branch information
glihm authored Jan 21, 2025
1 parent ad3e8fa commit ac35492
Show file tree
Hide file tree
Showing 9 changed files with 243 additions and 55 deletions.
37 changes: 24 additions & 13 deletions bin/sozo/src/commands/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ async fn match_event<P: Provider + Send + Sync>(
),
),
WorldEvent::ModelUpgraded(e) => {
let tag = tags.get(&e.selector).unwrap();
let tag = get_tag(e.selector, &tags);
(
format!("Model upgraded ({})", tag),
format!(
Expand All @@ -198,7 +198,7 @@ async fn match_event<P: Provider + Send + Sync>(
)
}
WorldEvent::EventUpgraded(e) => {
let tag = tags.get(&e.selector).unwrap();
let tag = get_tag(e.selector, &tags);
(
format!("Event upgraded ({})", tag),
format!(
Expand All @@ -209,14 +209,14 @@ async fn match_event<P: Provider + Send + Sync>(
)
}
WorldEvent::ContractUpgraded(e) => {
let tag = tags.get(&e.selector).unwrap();
let tag = get_tag(e.selector, &tags);
(
format!("Contract upgraded ({})", tag),
format!("Selector: {:#066x}\nClass hash: {:#066x}", e.selector, e.class_hash.0,),
)
}
WorldEvent::ContractInitialized(e) => {
let tag = tags.get(&e.selector).unwrap();
let tag = get_tag(e.selector, &tags);
(
format!("Contract initialized ({})", tag),
format!(
Expand All @@ -231,7 +231,7 @@ async fn match_event<P: Provider + Send + Sync>(
)
}
WorldEvent::WriterUpdated(e) => {
let tag = tags.get(&e.resource).unwrap();
let tag = get_tag(e.resource, &tags);
let grantee =
if let Some(selector) = contract_selectors_from_address.get(&e.contract.into()) {
tags.get(selector).unwrap().to_string()
Expand All @@ -245,7 +245,7 @@ async fn match_event<P: Provider + Send + Sync>(
)
}
WorldEvent::OwnerUpdated(e) => {
let tag = tags.get(&e.resource).unwrap();
let tag = get_tag(e.resource, &tags);
let grantee =
if let Some(selector) = contract_selectors_from_address.get(&e.contract.into()) {
tags.get(selector).unwrap().to_string()
Expand All @@ -259,15 +259,20 @@ async fn match_event<P: Provider + Send + Sync>(
)
}
WorldEvent::StoreSetRecord(e) => {
let tag = tags.get(&e.selector).unwrap();
let (record, _, _) = model::model_get(
let tag = get_tag(e.selector, &tags);
let record = if let Ok((record_str, _, _)) = model::model_get(
tag.clone(),
e.keys.clone(),
world_diff.world_info.address,
provider,
block_id,
)
.await?;
.await
{
record_str
} else {
"NOT_AVAILABLE".to_string()
};

(
format!("Store set record ({})", tag),
Expand All @@ -290,7 +295,7 @@ async fn match_event<P: Provider + Send + Sync>(
)
}
WorldEvent::StoreUpdateRecord(e) => {
let tag = tags.get(&e.selector).unwrap();
let tag = get_tag(e.selector, &tags);
// TODO: model value impl + print.
(
format!("Store update record ({})", tag),
Expand All @@ -307,7 +312,7 @@ async fn match_event<P: Provider + Send + Sync>(
)
}
WorldEvent::StoreUpdateMember(e) => {
let tag = tags.get(&e.selector).unwrap();
let tag = get_tag(e.selector, &tags);
// TODO: pretty print of the value.
(
format!("Store update member ({})", tag),
Expand All @@ -326,14 +331,14 @@ async fn match_event<P: Provider + Send + Sync>(
)
}
WorldEvent::StoreDelRecord(e) => {
let tag = tags.get(&e.selector).unwrap();
let tag = get_tag(e.selector, &tags);
(
format!("Store del record ({})", tag),
format!("Selector: {:#066x}\nEntity ID: {:#066x}", e.selector, e.entity_id,),
)
}
WorldEvent::EventEmitted(e) => {
let tag = tags.get(&e.selector).unwrap();
let tag = get_tag(e.selector, &tags);
let contract_tag = if let Some(selector) =
contract_selectors_from_address.get(&e.system_address.into())
{
Expand Down Expand Up @@ -373,3 +378,9 @@ async fn match_event<P: Provider + Send + Sync>(

Ok(())
}

/// Returns the tag for a selector, or the selector itself if it's not found.
#[inline]
fn get_tag(selector: Felt, tags: &HashMap<&Felt, String>) -> String {
tags.get(&selector).unwrap_or(&format!("external-{:#066x}", selector)).to_string()
}
3 changes: 3 additions & 0 deletions bin/sozo/src/commands/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ impl MigrateArgs {

let mut spinner = MigrationUi::new(Some("Evaluating world diff..."));

let is_guest = world.guest;

let (world_diff, account, rpc_url) = utils::get_world_diff_and_account(
account,
starknet,
Expand All @@ -79,6 +81,7 @@ impl MigrateArgs {
txn_config,
ws.load_profile_config()?,
rpc_url,
is_guest,
);

let MigrationResult { manifest, has_changes } =
Expand Down
13 changes: 13 additions & 0 deletions bin/sozo/src/commands/options/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@ pub struct WorldOptions {
#[arg(long = "world", env = DOJO_WORLD_ADDRESS_ENV_VAR)]
#[arg(global = true)]
pub world_address: Option<Felt>,

#[arg(long, default_value = "false")]
#[arg(help = "Whether the migration is a guest migration, which means the migration is \
performed on a world you are not the owner of.")]
pub guest: bool,

#[arg(
long,
help = "Whitelisted namespaces, separated by commas. If not provided, all namespaces will \
be fetched."
)]
#[arg(value_delimiter = ',', num_args = 0..)]
pub namespaces: Vec<String>,
}

impl WorldOptions {
Expand Down
3 changes: 2 additions & 1 deletion bin/sozo/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub fn get_world_address(
let deterministic_world_address = world_local.deterministic_world_address()?;

if let Some(wa) = world.address(env)? {
if wa != deterministic_world_address {
if wa != deterministic_world_address && !world.guest {
println!(
"{}",
format!(
Expand Down Expand Up @@ -166,6 +166,7 @@ pub async fn get_world_diff_and_provider(
world_local,
&provider,
env.and_then(|e| e.world_block),
&world.namespaces,
)
.await?;

Expand Down
17 changes: 15 additions & 2 deletions crates/dojo/world/src/diff/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ impl WorldDiff {
world_local: WorldLocal,
provider: P,
from_block: Option<u64>,
whitelisted_namespaces: &[String],
) -> Result<Self>
where
P: Provider,
Expand All @@ -170,7 +171,13 @@ impl WorldDiff {
.get_class_hash_at(BlockId::Tag(BlockTag::Pending), world_address)
.await
{
Err(ProviderError::StarknetError(StarknetError::ContractNotFound)) => Ok(false),
Err(ProviderError::StarknetError(StarknetError::ContractNotFound)) => {
trace!(
contract_address = format!("{:#066x}", world_address),
"World not deployed."
);
Ok(false)
}
Ok(_) => {
trace!(
contract_address = format!("{:#066x}", world_address),
Expand All @@ -181,9 +188,15 @@ impl WorldDiff {
Err(e) => Err(e),
}?;

let namespaces = if whitelisted_namespaces.is_empty() {
None
} else {
Some(whitelisted_namespaces.to_vec())
};

if is_deployed {
let world_remote =
WorldRemote::from_events(world_address, &provider, from_block).await?;
WorldRemote::from_events(world_address, &provider, from_block, namespaces).await?;

Ok(Self::new(world_local, world_remote))
} else {
Expand Down
Loading

0 comments on commit ac35492

Please sign in to comment.