Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleaning/Refactoring of the dojo-core crate. #2187

Merged
merged 3 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ jobs:
- uses: actions/checkout@v3
- uses: software-mansion/setup-scarb@v1
with:
scarb-version: "2.6.3"
scarb-version: "2.7.0-rc.3"
- run: |
scarb --manifest-path examples/spawn-and-move/Scarb.toml fmt --check
scarb --manifest-path crates/dojo-core/Scarb.toml fmt --check
Expand Down
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
scarb 2.6.4
scarb 2.7.0-rc.3
2 changes: 1 addition & 1 deletion crates/benches/contracts/src/tests/test_world.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod tests {
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};

// import test utils
use dojo::test_utils::{spawn_test_world, deploy_contract};
use dojo::utils::test::{spawn_test_world, deploy_contract};

// import test utils
use dojo_starter::{
Expand Down
4 changes: 2 additions & 2 deletions crates/dojo-bindgen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ fn gather_dojo_data(
// interfaces.
let mut systems = vec![];
let interface_blacklist =
["dojo::world::IWorldProvider", "dojo::components::upgradeable::IUpgradeable"];
["dojo::world::IWorldProvider", "dojo::contract::upgradeable::IUpgradeable"];

for (interface, funcs) in &tokens.interfaces {
if !interface_blacklist.contains(&interface.as_str()) {
Expand Down Expand Up @@ -183,7 +183,7 @@ fn filter_model_tokens(tokens: &TokenizedAbi) -> TokenizedAbi {
// All types from introspect module can also be removed as the clients does not rely on them.
// Events are also always empty at model contract level.
fn skip_token(token: &Token) -> bool {
if token.type_path().starts_with("dojo::database::introspect") {
if token.type_path().starts_with("dojo::model::introspect") {
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion crates/dojo-core/Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ dependencies = [
[[package]]
name = "dojo_plugin"
version = "0.7.3"
source = "git+https://github.com/dojoengine/dojo?branch=bump-cairo-2.7#17bc842f2f87b6ac0472d196efc11f01a5e55add"
source = "git+https://github.com/dojoengine/dojo?rev=d90b52b#d90b52b89749ac8af82f352dc08aa0b1378cfae6"
2 changes: 1 addition & 1 deletion crates/dojo-core/Scarb.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
cairo-version = "2.7.0-rc.3"
edition = "2023_11"
edition = "2024_07"
description = "The Dojo Core library for autonomous worlds."
name = "dojo"
version = "0.7.3"
Expand Down
1 change: 0 additions & 1 deletion crates/dojo-core/src/components.cairo

This file was deleted.

2 changes: 0 additions & 2 deletions crates/dojo-core/src/config.cairo

This file was deleted.

39 changes: 0 additions & 39 deletions crates/dojo-core/src/config/interface.cairo

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use dojo::world::IWorldDispatcher;

#[starknet::contract]
pub mod base {
use starknet::{ClassHash, get_caller_address};
use dojo::world::IWorldDispatcher;
use dojo::world::IWorldProvider;
use starknet::{ClassHash, ContractAddress, get_caller_address};
use starknet::storage::{StoragePointerReadAccess, StoragePointerWriteAccess};

use dojo::components::upgradeable::upgradeable as upgradeable_component;
use dojo::contract::upgradeable::upgradeable as upgradeable_component;
use dojo::world::{IWorldProvider, IWorldDispatcher, IWorldDispatcherTrait};

component!(path: upgradeable_component, storage: upgradeable, event: UpgradeableEvent);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ pub trait IUpgradeable<T> {

#[starknet::component]
pub mod upgradeable {
use core::num::traits::Zero;
use core::starknet::SyscallResultTrait;
use starknet::ClassHash;
use starknet::ContractAddress;
use starknet::get_caller_address;

use starknet::{ClassHash, ContractAddress, get_caller_address};
use starknet::syscalls::replace_class_syscall;

use dojo::world::{IWorldProvider, IWorldProviderDispatcher, IWorldDispatcher};
use core::num::traits::Zero;

#[storage]
struct Storage {}
pub struct Storage {}

#[event]
#[derive(Drop, starknet::Event)]
Expand Down
29 changes: 0 additions & 29 deletions crates/dojo-core/src/database/utils.cairo

This file was deleted.

116 changes: 79 additions & 37 deletions crates/dojo-core/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,39 +1,81 @@
pub mod base;
#[cfg(test)]
mod base_test;
pub mod config;
pub mod database;
#[cfg(test)]
mod database_test;
pub mod interfaces;
pub mod model;
#[cfg(test)]
mod model_test;
pub mod contract;
pub mod packing;
#[cfg(test)]
mod packing_test;
pub mod world;
#[cfg(test)]
mod world_test;
pub mod utils;
#[cfg(test)]
mod utils_test;

// Since Scarb 2.6.0 there's an optimization that does not
// build tests for dependencies and it's not configurable.
//
// To expose correctly the test utils for a package using dojo-core,
// we need to it in the `lib` target or using the `#[cfg(target: "test")]`
// attribute.
//
// Since `test_utils` is using `TEST_CLASS_HASH` to factorize some deployment
// core, we place it under the test target manually.
#[cfg(target: "test")]
pub mod test_utils;
pub mod contract {
mod base_contract;
pub use base_contract::base;
pub mod contract;
pub use contract::{IContract, IContractDispatcher, IContractDispatcherTrait};
pub mod upgradeable;
}

#[cfg(test)]
mod benchmarks;
pub mod model {
pub mod introspect;
pub mod layout;
pub use layout::{Layout, FieldLayout};

pub mod model;
pub use model::{
Model, ModelIndex, ModelEntity, IModel, IModelDispatcher, IModelDispatcherTrait,
deploy_and_get_metadata
};

pub mod components;
pub mod resource_metadata;
pub mod metadata;
pub use metadata::{ResourceMetadata, ResourceMetadataTrait, resource_metadata};
pub(crate) use metadata::{initial_address, initial_class_hash};
}

pub(crate) mod storage {
pub(crate) mod database;
pub(crate) mod packing;
pub(crate) mod layout;
pub(crate) mod storage;
}

pub mod utils {
// Since Scarb 2.6.0 there's an optimization that does not
// build tests for dependencies and it's not configurable.
//
// To expose correctly the test utils for a package using dojo-core,
// we need to it in the `lib` target or using the `#[cfg(target: "test")]`
// attribute.
//
// Since `test_utils` is using `TEST_CLASS_HASH` to factorize some deployment
// core, we place it under the test target manually.
#[cfg(target: "test")]
pub mod test;

pub mod utils;
pub use utils::{
bytearray_hash, entity_id_from_keys, find_field_layout, find_model_field_layout, any_none,
sum, combine_key
};
}

pub mod world {
pub(crate) mod update;
pub(crate) mod config;

mod world_contract;
pub use world_contract::{
world, IWorld, IWorldDispatcher, IWorldDispatcherTrait, IWorldProvider,
IWorldProviderDispatcher, IWorldProviderDispatcherTrait
};
pub(crate) use world_contract::{
IUpgradeableWorld, IUpgradeableWorldDispatcher, IUpgradeableWorldDispatcherTrait
};
}

#[cfg(test)]
mod tests {
mod model {
mod introspect;
mod model;
}
mod storage {
mod database;
mod packing;
mod storage;
}
mod base;
mod benchmarks;
mod world;
mod utils;
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,4 @@
#[derive(Copy, Drop, Serde, Debug, PartialEq)]
pub struct FieldLayout {
pub selector: felt252,
pub layout: Layout
}

#[derive(Copy, Drop, Serde, Debug, PartialEq)]
pub enum Layout {
Fixed: Span<u8>,
Struct: Span<FieldLayout>,
Tuple: Span<Layout>,
// We can't have `Layout` here as it will cause infinite recursion.
// And `Box` is not serializable. So using a Span, even if it's to have
// one element, does the trick.
Array: Span<Layout>,
ByteArray,
// there is one layout per variant.
// the `selector` field identifies the variant
// the `layout` defines the variant data (could be empty for variant without data).
Enum: Span<FieldLayout>,
}
use dojo::model::{Layout, FieldLayout};

#[derive(Copy, Drop, Serde)]
pub enum Ty {
Expand Down Expand Up @@ -260,10 +240,11 @@ pub impl Introspect_option<T, +Introspect<T>> of Introspect<Option<T>> {
fn layout() -> Layout {
Layout::Enum(
array![
FieldLayout { // Some
dojo::model::FieldLayout { // Some
selector: 0, layout: Introspect::<T>::layout() },
FieldLayout { // None
selector: 1, layout: Layout::Fixed(array![].span()) },
dojo::model::FieldLayout { // None
selector: 1, layout: Layout::Fixed(array![].span())
},
]
.span()
)
Expand Down
35 changes: 35 additions & 0 deletions crates/dojo-core/src/model/layout.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use dojo::storage::packing::calculate_packed_size;

#[derive(Copy, Drop, Serde, Debug, PartialEq)]
pub struct FieldLayout {
pub selector: felt252,
pub layout: Layout
}

#[derive(Copy, Drop, Serde, Debug, PartialEq)]
pub enum Layout {
Fixed: Span<u8>,
Struct: Span<FieldLayout>,
Tuple: Span<Layout>,
// We can't have `Layout` here as it will cause infinite recursion.
// And `Box` is not serializable. So using a Span, even if it's to have
// one element, does the trick.
Array: Span<Layout>,
ByteArray,
// there is one layout per variant.
// the `selector` field identifies the variant
// the `layout` defines the variant data (could be empty for variant without data).
Enum: Span<FieldLayout>,
}

/// Compute the full size in bytes of a layout, when all the fields
/// are bit-packed.
/// Could be None if at least a field has a dynamic size.
pub fn compute_packed_size(layout: Layout) -> Option<usize> {
if let Layout::Fixed(layout) = layout {
let mut span_layout = layout;
Option::Some(calculate_packed_size(ref span_layout))
} else {
Option::None
}
}
Loading
Loading