Skip to content

Commit

Permalink
Merge remote-tracking branch 'dojo/main' into fix/sozo-tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
glihm committed Sep 25, 2024
2 parents 7f5bbe7 + 8453b63 commit 8eaa1c2
Show file tree
Hide file tree
Showing 41 changed files with 528 additions and 516 deletions.
38 changes: 17 additions & 21 deletions bin/saya/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::time::Duration;
use cairo_proof_parser::output::extract_output;
use katana_primitives::contract::ContractAddress;
use katana_primitives::state::StateUpdates;
use katana_primitives::{address, felt};
use saya_core::prover::extract::program_input_from_program_output;
use saya_core::prover::{
prove_diff, HttpProverParams, MessageToAppchain, MessageToStarknet, ProgramInput, ProveProgram,
Expand Down Expand Up @@ -43,60 +44,55 @@ async fn test_program_input_from_program_output() -> anyhow::Result<()> {
config_hash: Felt::from_str("104").unwrap(),
message_to_starknet_segment: vec![
MessageToStarknet {
from_address: ContractAddress::from(Felt::from_str("105").unwrap()),
to_address: ContractAddress::from(Felt::from_str("106").unwrap()),
from_address: address!("105"),
to_address: address!("106"),
payload: vec![Felt::from_str("107").unwrap()],
},
MessageToStarknet {
from_address: ContractAddress::from(Felt::from_str("105").unwrap()),
to_address: ContractAddress::from(Felt::from_str("106").unwrap()),
from_address: address!("105"),
to_address: address!("106"),
payload: vec![Felt::from_str("107").unwrap()],
},
],
message_to_appchain_segment: vec![
MessageToAppchain {
from_address: ContractAddress::from(Felt::from_str("108").unwrap()),
to_address: ContractAddress::from(Felt::from_str("109").unwrap()),
from_address: address!("108"),
to_address: address!("109"),
nonce: Felt::from_str("110").unwrap(),
selector: Felt::from_str("111").unwrap(),
payload: vec![Felt::from_str("112").unwrap()],
},
MessageToAppchain {
from_address: ContractAddress::from(Felt::from_str("108").unwrap()),
to_address: ContractAddress::from(Felt::from_str("109").unwrap()),
from_address: address!("108"),
to_address: address!("109"),
nonce: Felt::from_str("110").unwrap(),
selector: Felt::from_str("111").unwrap(),
payload: vec![Felt::from_str("112").unwrap()],
},
],
state_updates: StateUpdates {
nonce_updates: {
let mut map = std::collections::HashMap::new();
map.insert(
ContractAddress::from(Felt::from_str("1111").unwrap()),
Felt::from_str("22222").unwrap(),
);
let mut map = std::collections::BTreeMap::new();
map.insert(address!("1111"), felt!("22222"));
map
},
storage_updates: vec![(
ContractAddress::from(Felt::from_str("333")?),
address!("333"),
vec![(Felt::from_str("4444")?, Felt::from_str("555")?)].into_iter().collect(),
)]
.into_iter()
.collect(),
contract_updates: {
let mut map = std::collections::HashMap::new();
map.insert(
ContractAddress::from(Felt::from_str("66666").unwrap()),
Felt::from_str("7777").unwrap(),
);
deployed_contracts: {
let mut map = std::collections::BTreeMap::new();
map.insert(address!("66666"), felt!("7777"));
map
},
declared_classes: {
let mut map = std::collections::HashMap::new();
let mut map = std::collections::BTreeMap::new();
map.insert(Felt::from_str("88888").unwrap(), Felt::from_str("99999").unwrap());
map
},
..Default::default()
},
world_da: None,
};
Expand Down
4 changes: 4 additions & 0 deletions crates/dojo-core/src/world/errors.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ pub fn resource_not_registered(resource: felt252) -> ByteArray {
format!("Resource `{}` is not registered", resource)
}

pub fn resource_already_registered(resource: felt252) -> ByteArray {
format!("Resource `{}` is already registered", resource)
}

pub fn not_owner(caller: ContractAddress, resource: felt252) -> ByteArray {
format!("Caller `{:?}` is not the owner of the resource `{}`", caller, resource)
}
Expand Down
6 changes: 6 additions & 0 deletions crates/dojo-core/src/world/world_contract.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,12 @@ pub mod world {
self.assert_caller_namespace_write_access(@namespace, namespace_hash);

let selector = dispatcher.selector();

match self.resources.read(selector) {
Resource::Unregistered => {},
_ => panic_with_byte_array(@errors::resource_already_registered(selector))
};

self.owners.write((selector, caller), true);
self.resources.write(selector, Resource::Contract((class_hash, contract_address)));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,3 @@ dependencies = [
[[package]]
name = "dojo"
version = "1.0.0-alpha.4"
dependencies = [
"dojo_plugin",
]

[[package]]
name = "dojo_plugin"
version = "1.0.0-alpha.4"
source = "git+/~https://github.com/dojoengine/dojo?rev=f15def33#f15def330c0d099e79351d11c197f63e8cc1ff36"
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
kind = "Class"
class_hash = "0x6f4515274ee23404789c3351a77107d0ec07508530119822046600ca6948d6e"
original_class_hash = "0x6f4515274ee23404789c3351a77107d0ec07508530119822046600ca6948d6e"
class_hash = "0x5c4271c8cd454ceb8049d2b0724c99d12c2ef8077fc6ad325b18978f614aab0"
original_class_hash = "0x5c4271c8cd454ceb8049d2b0724c99d12c2ef8077fc6ad325b18978f614aab0"
abi = "manifests/dev/base/abis/dojo-world.json"
tag = "dojo-world"
manifest_name = "dojo-world"
9 changes: 8 additions & 1 deletion crates/dojo-world/src/config/namespace_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,21 @@ use serde::Deserialize;
pub const NAMESPACE_CFG_PREFIX: &str = "nm|";
pub const DEFAULT_NAMESPACE_CFG_KEY: &str = "namespace_default";
pub const DOJO_MANIFESTS_DIR_CFG_KEY: &str = "dojo_manifests_dir";
pub const DEFAULT_NAMESPACE: &str = "DEFAULT_NAMESPACE";

/// Namespace configuration.
#[derive(Debug, Clone, Default, Deserialize)]
#[derive(Debug, Clone, Deserialize)]
pub struct NamespaceConfig {
pub default: String,
pub mappings: Option<HashMap<String, String>>,
}

impl Default for NamespaceConfig {
fn default() -> Self {
NamespaceConfig { default: DEFAULT_NAMESPACE.to_string(), mappings: None }
}
}

impl NamespaceConfig {
/// Creates a new namespace configuration with a default namespace.
pub fn new(default: &str) -> Self {
Expand Down
6 changes: 3 additions & 3 deletions crates/katana/controller/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::collections::BTreeMap;

use alloy_primitives::U256;
use anyhow::Result;
Expand Down Expand Up @@ -131,7 +131,7 @@ pub mod json {
fn get_contract_storage(
credential_id: CredentialID,
public_key: CoseKey,
) -> Result<HashMap<StorageKey, StorageValue>> {
) -> Result<BTreeMap<StorageKey, StorageValue>> {
use slot::account_sdk::signers::DeviceError;
use webauthn_rs_proto::auth::PublicKeyCredentialRequestOptions;
use webauthn_rs_proto::{
Expand Down Expand Up @@ -186,7 +186,7 @@ fn get_contract_storage(
let storage = get_storage_var_address(MULTIPLE_OWNERS_COMPONENT_SUB_STORAGE, &[guid])?;

// 1 for boolean True in Cairo. Refer to the provided link above.
let storage_mapping = HashMap::from([(storage, Felt::ONE)]);
let storage_mapping = BTreeMap::from([(storage, Felt::ONE)]);

Ok(storage_mapping)
}
Expand Down
14 changes: 7 additions & 7 deletions crates/katana/executor/src/implementation/blockifier/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ mod tests {
DEFAULT_OZ_ACCOUNT_CONTRACT_CASM,
};
use katana_primitives::utils::class::{parse_compiled_class, parse_sierra_class};
use katana_primitives::Felt;
use katana_primitives::{address, Felt};
use katana_provider::providers::in_memory::InMemoryProvider;
use katana_provider::traits::contract::ContractClassWriter;
use katana_provider::traits::state::{StateFactoryProvider, StateProvider, StateWriter};
Expand All @@ -256,7 +256,7 @@ mod tests {
}

fn state_provider() -> Box<dyn StateProvider> {
let address = ContractAddress::from(felt!("0x67"));
let address = address!("0x67");
let nonce = felt!("0x7");
let storage_key = felt!("0x1");
let storage_value = felt!("0x2");
Expand Down Expand Up @@ -284,7 +284,7 @@ mod tests {
let state = state_provider();
let cached_state = CachedState::new(StateProviderDb::new(state));

let address = ContractAddress::from(felt!("0x67"));
let address = address!("0x67");
let legacy_class_hash = felt!("0x111");
let storage_key = felt!("0x1");

Expand Down Expand Up @@ -319,7 +319,7 @@ mod tests {
let sp = state_provider();

// cache_state native data
let new_address = ContractAddress::from(felt!("0xdead"));
let new_address = address!("0xdead");
let new_storage_key = felt!("0xf00");
let new_storage_value = felt!("0xba");
let new_legacy_class_hash = felt!("0x1234");
Expand Down Expand Up @@ -391,7 +391,7 @@ mod tests {
// assert that can fetch data from the underlyign state provider
let sp: Box<dyn StateProvider> = Box::new(cached_state);

let address = ContractAddress::from(felt!("0x67"));
let address = address!("0x67");
let class_hash = felt!("0x123");
let legacy_class_hash = felt!("0x111");

Expand Down Expand Up @@ -454,7 +454,7 @@ mod tests {
fn fetch_non_existant_data() -> anyhow::Result<()> {
let db = InMemoryProvider::new();

let address = ContractAddress::from(felt!("0x1"));
let address = address!("0x1");
let class_hash = felt!("0x123");
let storage_key = felt!("0x1");

Expand All @@ -463,7 +463,7 @@ mod tests {
// only return None if the contract does not exist. The intended behaviour for
// StateProvider::storage is to return None if the storage key or contract address
// does not exist.
let edge_address = ContractAddress::from(felt!("0x2"));
let edge_address = address!("0x2");
db.set_class_hash_of_contract(edge_address, class_hash)?;

let sp = db.latest()?;
Expand Down
36 changes: 21 additions & 15 deletions crates/katana/executor/src/implementation/blockifier/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::{BTreeMap, HashMap};
use std::collections::BTreeMap;
use std::num::NonZeroU128;
use std::sync::Arc;

Expand Down Expand Up @@ -403,12 +403,15 @@ pub(super) fn state_update_from_cached_state<S: StateDb>(

let state_diff = state.0.lock().inner.to_state_diff().unwrap();

let mut declared_compiled_classes: HashMap<katana_primitives::class::ClassHash, CompiledClass> =
HashMap::new();
let mut declared_sierra_classes: HashMap<
let mut declared_compiled_classes: BTreeMap<
katana_primitives::class::ClassHash,
CompiledClass,
> = BTreeMap::new();

let mut declared_sierra_classes: BTreeMap<
katana_primitives::class::ClassHash,
FlattenedSierraClass,
> = HashMap::new();
> = BTreeMap::new();

for class_hash in state_diff.compiled_class_hashes.keys() {
let hash = class_hash.0;
Expand All @@ -427,27 +430,29 @@ pub(super) fn state_update_from_cached_state<S: StateDb>(
.nonces
.into_iter()
.map(|(key, value)| (to_address(key), value.0))
.collect::<HashMap<
.collect::<BTreeMap<
katana_primitives::contract::ContractAddress,
katana_primitives::contract::Nonce,
>>();

let storage_updates =
state_diff.storage.into_iter().fold(HashMap::new(), |mut storage, ((addr, key), value)| {
let entry: &mut HashMap<
let storage_updates = state_diff.storage.into_iter().fold(
BTreeMap::new(),
|mut storage, ((addr, key), value)| {
let entry: &mut BTreeMap<
katana_primitives::contract::StorageKey,
katana_primitives::contract::StorageValue,
> = storage.entry(to_address(addr)).or_default();
entry.insert(*key.0.key(), value);
storage
});
},
);

let contract_updates =
let deployed_contracts =
state_diff
.class_hashes
.into_iter()
.map(|(key, value)| (to_address(key), value.0))
.collect::<HashMap<
.collect::<BTreeMap<
katana_primitives::contract::ContractAddress,
katana_primitives::class::ClassHash,
>>();
Expand All @@ -457,7 +462,7 @@ pub(super) fn state_update_from_cached_state<S: StateDb>(
.compiled_class_hashes
.into_iter()
.map(|(key, value)| (key.0, value.0))
.collect::<HashMap<
.collect::<BTreeMap<
katana_primitives::class::ClassHash,
katana_primitives::class::CompiledClassHash,
>>();
Expand All @@ -468,8 +473,9 @@ pub(super) fn state_update_from_cached_state<S: StateDb>(
state_updates: StateUpdates {
nonce_updates,
storage_updates,
contract_updates,
deployed_contracts,
declared_classes,
..Default::default()
},
}
}
Expand Down Expand Up @@ -651,7 +657,7 @@ fn to_l2_l1_messages(
#[cfg(test)]
mod tests {

use std::collections::HashSet;
use std::collections::{HashMap, HashSet};

use katana_cairo::cairo_vm::types::builtin_name::BuiltinName;
use katana_cairo::cairo_vm::vm::runners::cairo_runner::ExecutionResources;
Expand Down
20 changes: 10 additions & 10 deletions crates/katana/executor/tests/executor.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod fixtures;

use std::collections::HashMap;
use std::collections::BTreeMap;

use fixtures::{state_provider, valid_blocks};
use katana_executor::{ExecutionOutput, ExecutionResult, ExecutorFactory};
Expand All @@ -11,7 +11,7 @@ use katana_primitives::genesis::constant::{
DEFAULT_OZ_ACCOUNT_CONTRACT_CLASS_HASH, DEFAULT_PREFUNDED_ACCOUNT_BALANCE, DEFAULT_UDC_ADDRESS,
};
use katana_primitives::transaction::TxWithHash;
use katana_primitives::Felt;
use katana_primitives::{address, Felt};
use katana_provider::traits::state::StateProvider;
use starknet::core::utils::{
get_storage_var_address, get_udc_deployed_address, UdcUniqueSettings, UdcUniqueness,
Expand All @@ -26,11 +26,10 @@ fn test_executor_with_valid_blocks_impl<EF: ExecutorFactory>(
let cfg_env = factory.cfg();

// the contract address of the main account used to send most of the transactions
let main_account: ContractAddress =
felt!("0x6b86e40118f29ebe393a75469b4d926c7a44c2e2681b6d319520b7c1156d114").into();
let main_account =
address!("0x6b86e40118f29ebe393a75469b4d926c7a44c2e2681b6d319520b7c1156d114");
// the contract address of the account deployed using the `DeployAccount` tx
let new_acc: ContractAddress =
felt!("0x3ddfa445a70b927497249f94ff7431fc2e2abc761a34417fd4891beb7c2db85").into();
let new_acc = address!("0x3ddfa445a70b927497249f94ff7431fc2e2abc761a34417fd4891beb7c2db85");

let mut executor = factory.with_state(state);
let mut expected_txs: Vec<TxWithHash> = Vec::with_capacity(3);
Expand Down Expand Up @@ -283,16 +282,17 @@ fn test_executor_with_valid_blocks_impl<EF: ExecutorFactory>(
assert_eq!(actual_txs, expected_txs);

let actual_nonce_updates = states.state_updates.nonce_updates;
let expected_nonce_updates = HashMap::from([(main_account, felt!("3")), (new_acc, felt!("1"))]);
let expected_nonce_updates =
BTreeMap::from([(main_account, felt!("3")), (new_acc, felt!("1"))]);

let actual_declared_classes = states.state_updates.declared_classes;
let expected_declared_classes = HashMap::from([(
let expected_declared_classes = BTreeMap::from([(
felt!("0x420"),
felt!("0x016c6081eb34ad1e0c5513234ed0c025b3c7f305902d291bad534cd6474c85bc"),
)]);

let actual_contract_deployed = states.state_updates.contract_updates;
let expected_contract_deployed = HashMap::from([
let actual_contract_deployed = states.state_updates.deployed_contracts;
let expected_contract_deployed = BTreeMap::from([
(new_acc, DEFAULT_OZ_ACCOUNT_CONTRACT_CLASS_HASH),
(deployed_contract.into(), DEFAULT_LEGACY_ERC20_CONTRACT_CLASS_HASH),
]);
Expand Down
Loading

0 comments on commit 8eaa1c2

Please sign in to comment.