Skip to content
This repository has been archived by the owner on Nov 11, 2024. It is now read-only.

Commit

Permalink
feat: use dynamic namespaces (#31)
Browse files Browse the repository at this point in the history
* feat: use dynamic namespaces

* fix: port the new testing suite with dynamic namespaces

* fix: ensure events namespace permission is correctly checked

* fmt and remove ci since dojo-core is only for archiving

* add cairo fmt back
  • Loading branch information
glihm authored Nov 5, 2024
1 parent 7215d47 commit 969004b
Show file tree
Hide file tree
Showing 70 changed files with 2,625 additions and 2,558 deletions.
12 changes: 1 addition & 11 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,13 @@ env:
RUST_VERSION: 1.80.0

jobs:
test:
runs-on: ubuntu-latest-4-cores
container:
image: ghcr.io/dojoengine/dojo-core-dev:5995840
steps:
- uses: actions/checkout@v3
- uses: Swatinem/rust-cache@v2
- run: |
scripts/tests.sh
cairofmt:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: software-mansion/setup-scarb@v1
with:
scarb-version: "2.7.1"
scarb-version: "2.8.4"
- run: |
bash scripts/cairo_fmt.sh --check
Expand Down
37 changes: 14 additions & 23 deletions crates/compiler/src/plugin/semantics/test_data/get
Original file line number Diff line number Diff line change
Expand Up @@ -132,28 +132,9 @@ Block(
pattern: Variable(
__Health,
),
expr: FunctionCall(
ExprFunctionCall {
function: ?4::get::<core::felt252, core::felt252Drop, core::Felt252Serde>,
args: [
Value(
Snapshot(
ExprSnapshot {
inner: Var(
LocalVarId(test::world),
),
ty: @dojo::world::iworld::IWorldDispatcher,
},
),
),
Value(
Var(
LocalVarId(test::key),
),
),
],
coupon_arg: None,
ty: test::Health,
expr: Missing(
ExprMissing {
ty: <missing>,
},
),
},
Expand All @@ -169,7 +150,17 @@ Block(
)

//! > semantic_diagnostics
error: Trait has no implementation in context: dojo::model::model::ModelStore::<test::Health>.
error: Identifier not found.
--> lib.cairo:12:1
get!(world, key, (Health))
^************************^

warning[E0001]: Unused variable. Consider ignoring by prefixing with `_`.
--> lib.cairo:10:22
fn test_func() { let key: felt252 = 0xb0b;
^*^

warning[E0001]: Unused variable. Consider ignoring by prefixing with `_`.
--> lib.cairo:11:5
let world = IWorldDispatcher{contract_address: 0x0.try_into().unwrap()}; {
^***^
99 changes: 53 additions & 46 deletions crates/compiler/src/plugin/semantics/test_data/set
Original file line number Diff line number Diff line change
Expand Up @@ -84,52 +84,9 @@ Block(
statements: [
Expr(
StatementExpr {
expr: FunctionCall(
ExprFunctionCall {
function: dojo::model::model::ModelStoreImpl::<test::Health, dojo::model::model::ModelImpl::<test::Health, test::HealthModelParser, test::health_definition::HealthDefinitionImpl::<test::Health>, test::HealthSerde>, test::HealthDrop>::set,
args: [
Value(
Var(
LocalVarId(test::world),
),
),
Value(
Snapshot(
ExprSnapshot {
inner: StructCtor(
ExprStructCtor {
concrete_struct_id: test::Health,
members: [
(
MemberId(test::id),
Literal(
ExprLiteral {
value: 2827,
ty: core::integer::u32,
},
),
),
(
MemberId(test::health),
Literal(
ExprLiteral {
value: 79,
ty: core::integer::u16,
},
),
),
],
base_struct: None,
ty: test::Health,
},
),
ty: @test::Health,
},
),
),
],
coupon_arg: None,
ty: (),
expr: Missing(
ExprMissing {
ty: <missing>,
},
),
},
Expand All @@ -141,7 +98,57 @@ Block(
)

//! > semantic_diagnostics
error: Identifier not found.
--> lib.cairo:12:1
set!(world, (Health{id: 0xb0b, health: 79}))
^******************************************^

warning[E0001]: Unused variable. Consider ignoring by prefixing with `_`.
--> lib.cairo:10:22
fn test_func() { let key: felt252 = 0xb0b;
^*^

warning[E0001]: Unused variable. Consider ignoring by prefixing with `_`.
--> lib.cairo:11:5
let world = IWorldDispatcher{contract_address: 0x0.try_into().unwrap()}; {
^***^

error: Identifier not found.
--> lib.cairo:3:1
#[derive(Copy, Drop, Serde)]
^**************************^

error: Impl not found.
--> lib.cairo:3:1
#[derive(Copy, Drop, Serde)]
^**************************^

error: Identifier not found.
--> lib.cairo:3:1
#[derive(Copy, Drop, Serde)]
^**************************^

error: Impl item function `HealthDefinitionImpl::namespace` is not a member of trait `ModelDefinition`.
--> lib.cairo:3:1
#[derive(Copy, Drop, Serde)]
^**************************^

error: Impl item function `HealthDefinitionImpl::tag` is not a member of trait `ModelDefinition`.
--> lib.cairo:3:1
#[derive(Copy, Drop, Serde)]
^**************************^

error: Impl item function `HealthDefinitionImpl::selector` is not a member of trait `ModelDefinition`.
--> lib.cairo:3:1
#[derive(Copy, Drop, Serde)]
^**************************^

error: Impl item function `HealthDefinitionImpl::name_hash` is not a member of trait `ModelDefinition`.
--> lib.cairo:3:1
#[derive(Copy, Drop, Serde)]
^**************************^

error: Impl item function `HealthDefinitionImpl::namespace_hash` is not a member of trait `ModelDefinition`.
--> lib.cairo:3:1
#[derive(Copy, Drop, Serde)]
^**************************^
10 changes: 9 additions & 1 deletion crates/contracts/Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,12 @@ version = 1

[[package]]
name = "dojo"
version = "1.0.0-rc.1"
version = "1.0.0-rc.0"
dependencies = [
"dojo_plugin",
]

[[package]]
name = "dojo_plugin"
version = "2.8.4"
source = "git+https://github.com/dojoengine/dojo?branch=feat%2Fdojo-1-rc0#3bf1276163fe9dbfe0f65775de8363e358510c77"
6 changes: 5 additions & 1 deletion crates/contracts/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ cairo-version = "=2.8.4"
edition = "2024_07"
description = "The Dojo Core library for autonomous worlds."
name = "dojo"
version = "1.0.0-rc.1"
version = "1.0.0-rc.0"

[dependencies]
starknet = "=2.8.4"
dojo_plugin = { git = "https://github.com/dojoengine/dojo", branch = "feat/dojo-1-rc0" }

[dev-dependencies]
cairo_test = "=2.8.4"

[lib]

Expand Down
10 changes: 6 additions & 4 deletions crates/contracts/src/contract/components/upgradeable.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub mod upgradeable_cpt {

pub mod Errors {
pub const INVALID_CLASS: felt252 = 'class_hash cannot be zero';
pub const INVALID_CLASS_CONTENT: felt252 = 'class_hash not world provider';
pub const INVALID_CLASS_CONTENT: felt252 = 'class_hash not Dojo IContract';
pub const INVALID_CALLER: felt252 = 'must be called by world';
pub const INVALID_WORLD_ADDRESS: felt252 = 'invalid world address';
}
Expand All @@ -41,17 +41,19 @@ pub mod upgradeable_cpt {
> of super::IUpgradeable<ComponentState<TContractState>> {
fn upgrade(ref self: ComponentState<TContractState>, new_class_hash: ClassHash) {
assert(
self.get_contract().world().contract_address.is_non_zero(),
self.get_contract().world_dispatcher().contract_address.is_non_zero(),
Errors::INVALID_WORLD_ADDRESS
);
assert(
get_caller_address() == self.get_contract().world().contract_address,
get_caller_address() == self.get_contract().world_dispatcher().contract_address,
Errors::INVALID_CALLER
);
assert(new_class_hash.is_non_zero(), Errors::INVALID_CLASS);

// Seems like the match doesn't catch the error is the entrypoint is
// not found.
match starknet::syscalls::library_call_syscall(
new_class_hash, selector!("world"), [].span(),
new_class_hash, selector!("dojo_name"), [].span(),
) {
Result::Ok(_) => {
replace_class_syscall(new_class_hash).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions crates/contracts/src/contract/components/world_provider.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use dojo::world::IWorldDispatcher;

#[starknet::interface]
pub trait IWorldProvider<T> {
fn world(self: @T) -> IWorldDispatcher;
fn world_dispatcher(self: @T) -> IWorldDispatcher;
}

#[starknet::component]
Expand All @@ -21,7 +21,7 @@ pub mod world_provider_cpt {
pub impl WorldProvider<
TContractState, +HasComponent<TContractState>
> of super::IWorldProvider<ComponentState<TContractState>> {
fn world(self: @ComponentState<TContractState>) -> IWorldDispatcher {
fn world_dispatcher(self: @ComponentState<TContractState>) -> IWorldDispatcher {
self.world_dispatcher.read()
}
}
Expand Down
10 changes: 0 additions & 10 deletions crates/contracts/src/contract/contract.cairo

This file was deleted.

4 changes: 4 additions & 0 deletions crates/contracts/src/contract/interface.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[starknet::interface]
pub trait IContract<T> {
fn dojo_name(self: @T) -> ByteArray;
}
42 changes: 2 additions & 40 deletions crates/contracts/src/event/event.cairo
Original file line number Diff line number Diff line change
@@ -1,61 +1,23 @@
use dojo::meta::Layout;
use dojo::meta::introspect::Ty;
use dojo::world::IWorldDispatcher;

#[derive(Drop, Serde, Debug, PartialEq)]
pub struct EventDefinition {
pub name: ByteArray,
pub namespace: ByteArray,
pub namespace_selector: felt252,
pub version: u8,
pub layout: Layout,
pub schema: Ty
}

pub trait Event<T> {
fn emit(self: @T, world: IWorldDispatcher);

fn name() -> ByteArray;
fn namespace() -> ByteArray;
fn tag() -> ByteArray;

fn version() -> u8;

fn selector() -> felt252;
fn instance_selector(self: @T) -> felt252;

fn name_hash() -> felt252;
fn namespace_hash() -> felt252;

fn definition() -> EventDefinition;

fn layout() -> Layout;
fn schema() -> Ty;

fn historical() -> bool;
fn keys(self: @T) -> Span<felt252>;
fn values(self: @T) -> Span<felt252>;
}

#[starknet::interface]
pub trait IEvent<T> {
fn name(self: @T) -> ByteArray;
fn namespace(self: @T) -> ByteArray;
fn tag(self: @T) -> ByteArray;

fn version(self: @T) -> u8;

fn selector(self: @T) -> felt252;
fn name_hash(self: @T) -> felt252;
fn namespace_hash(self: @T) -> felt252;

fn definition(self: @T) -> EventDefinition;

fn layout(self: @T) -> Layout;
fn schema(self: @T) -> Ty;
}

#[cfg(target: "test")]
pub trait EventTest<T> {
fn emit_test(self: @T, world: IWorldDispatcher);
/// Returns the selector of the model computed for the given namespace hash.
fn selector(namespace_hash: felt252) -> felt252;
}
13 changes: 13 additions & 0 deletions crates/contracts/src/event/interface.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use dojo::meta::Layout;
use dojo::meta::introspect::Ty;

use super::EventDefinition;

#[starknet::interface]
pub trait IEvent<T> {
fn dojo_name(self: @T) -> ByteArray;
fn version(self: @T) -> u8;
fn definition(self: @T) -> EventDefinition;
fn layout(self: @T) -> Layout;
fn schema(self: @T) -> Ty;
}
8 changes: 8 additions & 0 deletions crates/contracts/src/event/storage.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/// A `EventStorage` trait that abstracts where the storage is and how events are emitted.
pub trait EventStorage<S, E> {
fn emit_event(ref self: S, event: @E);
}

pub trait EventStorageTest<S, E> {
fn emit_event_test(ref self: S, event: @E);
}
Loading

0 comments on commit 969004b

Please sign in to comment.