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

Handle namespace for models and contracts #2023

Merged
merged 8 commits into from
Jun 18, 2024

Conversation

remybar
Copy link
Contributor

@remybar remybar commented May 31, 2024

Namespace feature

This PR adds the concept of namespace for world elements such as models and systems.
Namespaces will give us more flexibility to organize models and systems together, and come
with a finer-grained authorization management.

Basics

Basically, a namespace is a ByteArray which has to be registered to the world before being used.

Each world element belongs to a namespace.
By default, the namespace is the main package_id,
which is the name field specified in the package section of the Scarb.toml file.
So, for small projects, no need to worry about this namespace concept, all their models and systems will belong to the same default namespace.

But for bigger projects, this default namespace can be overriden when defining a model with #[dojo::model(namespace: "YOUR_NAMESPACE")] or a system with #[dojo::contract(namespace: "YOUR_NAMESPACE")].

To get an unique selector for a model in a namespace, the model selector is now computed using the following formula: selector = hash(hash(namespace), hash(model_name)) where hash() is the poseidon hash function. So, instead of using the selector! macro which only accepts hardcoded strings, the poseidon hash of a ByteArray is computed on the serialized ByteArray (so, an array of felt252).

Auth

Dojo Auth system works with 2 roles: Owner and Writer.
A Owner must be an account contract. It owns a resources (world, models, systems or namespaces) and can grant/revoke Owner and Writer roles to other contracts.
A Writer can be any kind of contract and has write access to a resource.

Basically, to write to a resource:

  • the caller account must be
    • an owner of the world,
    • OR an owner of the resource,
    • OR has the Writer role for the resource,
  • OR the caller contract must have the Writer role for the resource.

To write to a model, there is another level of authorization, so:

  • the caller account must be
    • an owner of the world,
    • OR an owner of the model namespace,
    • OR an owner of the model,
    • OR has the Writer role for the model namespace,
    • OR has the Writer role for the model,
  • OR the caller contract must have the Writer role for the model namespace OR the model.

In brief, having write access to a namespace gives write access to any model of the namespace.

Naming

Since model and namespace selectors are hashes of ByteArray, it is important to normalise the way these
ByteArray are stored, and especially their cases. So:

  • since a model or a system name come from Cairo code, it keeps the case used in the code (mainly Pascal case in Cairo),
  • a namespace must use lower case characters (a-z) and underscore (_) only,
  • there is no more qualified path, each world element is identified by an unique name built by concatenating the namespace and the element name, separated by a :,
  • this unique name is also used to name the related manifest file except that : is replaced by _.

crates/dojo-lang/src/plugin.rs Outdated Show resolved Hide resolved
crates/dojo-lang/src/plugin.rs Outdated Show resolved Hide resolved
@remybar remybar force-pushed the namespace-feat branch 3 times, most recently from e8d9eb6 to 63edbfc Compare June 8, 2024 10:46
@remybar remybar force-pushed the namespace-feat branch 8 times, most recently from e73a9b4 to 8d98dc7 Compare June 17, 2024 04:24
lambda-0x and others added 7 commits June 17, 2024 07:51
…dy deployed (dojoengine#2058)

* fix(migrate): when `init_calldata` depends on contract that was already deployed

* add tests
* Ensure katana runs in debian:bookworm-slim

* chore: switch to cartridge fork and adjust CI to 4 core to build Katana

* chore: add old prover-sdk version to check CI fail

* fix katana bin permissions

* chore: add ensure-docker as requirement for big jobs

* chore: point to http-prover using rustls-tls

* dbg

* wip

* chore: bump to artifacts v4

* fix: typo

* wip

* fix: don't use path explicitely

* wip

* fix: fix artifacts managment

* add ls

* fix: use other path with checkout

* fix: remove prints

---------

Co-authored-by: glihm <[email protected]>
dojo-core: update gas logs

Co-authored-by: glihm <[email protected]>
…ne#2063)

* fix: allow the use of self and world in an exclusive manner

* fix: fmt

* fix: cairo fmt

* fix: ensure a warning is emitted with generate trait and world

* fix: cairo fmt
@glihm glihm changed the base branch from main to feat/namespace June 18, 2024 20:34
@glihm glihm marked this pull request as ready for review June 18, 2024 20:41
@glihm glihm merged commit 93ec22d into dojoengine:feat/namespace Jun 18, 2024
1 of 13 checks passed
glihm added a commit that referenced this pull request Jul 7, 2024
* Namespace feature

* Handle namespace for models and contracts (#2023)

* fix(migrate): when `init_calldata` depends on contract that was already deployed (#2058)

* fix(migrate): when `init_calldata` depends on contract that was already deployed

* add tests

* fix(ci): ensure katana runs in debian:bookworm-slim (#2073)

* Ensure katana runs in debian:bookworm-slim

* chore: switch to cartridge fork and adjust CI to 4 core to build Katana

* chore: add old prover-sdk version to check CI fail

* fix katana bin permissions

* chore: add ensure-docker as requirement for big jobs

* chore: point to http-prover using rustls-tls

* dbg

* wip

* chore: bump to artifacts v4

* fix: typo

* wip

* fix: don't use path explicitely

* wip

* fix: fix artifacts managment

* add ls

* fix: use other path with checkout

* fix: remove prints

---------

Co-authored-by: glihm <[email protected]>

* [dojo-core]: update gas logs (#1877)

dojo-core: update gas logs

Co-authored-by: glihm <[email protected]>

* Prepare release: v0.7.1 (#2076)

Co-authored-by: glihm <[email protected]>

* fix: allow the use of self and world in an exclusive manner (#2063)

* fix: allow the use of self and world in an exclusive manner

* fix: fmt

* fix: cairo fmt

* fix: ensure a warning is emitted with generate trait and world

* fix: cairo fmt

* Update devcontainer image: v0.7.1 (#2077)

Co-authored-by: glihm <[email protected]>

* Namespace feature

---------

Co-authored-by: lambda-0x <[email protected]>
Co-authored-by: Tarrence van As <[email protected]>
Co-authored-by: glihm <[email protected]>
Co-authored-by: notV4l <[email protected]>
Co-authored-by: glihm <[email protected]>

* fix: update manifests

* fix: update manifests

* fix: update world address in Scarb.toml

* fix world_test

* Namespace feature improvements (#2108)

* standardize model/system interface for name/namespace/selectors

* wip

* Overlays (#2127)

* load custom overlay manifests

* update metadata management + some fixes

* update sozo commands with namespace

* fix: change tag separator to `-` (#2132)

* wip

* fix tests

* update sozo auth command + auto-auth feature (#2134)

* update sozo auth command + auto-auth feature

* fix: fix some tests

---------

Co-authored-by: glihm <[email protected]>

* replace {WORLD,BASE}_CONTRACT_NAME by {WORLD,BASE}_CONTRACT_TAG (#2136)

* replace {WORLD,BASE}_CONTRACT_NAME by {WORLD,BASE}_CONTRACT_TAG

* fix: ensure model selector correctly computed in tests

* fix: wip on tests and use Katana Runner

---------

Co-authored-by: glihm <[email protected]>

* fix(sozo): migration with seed and world address both provided (#2135)

* initial commit

* add seed to manifest and remove from cli

* add overlays for dojo_init

* make tests compile

* fix some tests

* fix lints

* fix: use same name for seed tests

---------

Co-authored-by: glihm <[email protected]>

* feat(torii-core): namespace in processor & db migration (#2130)

* feat(torii-core): namespace in processor & db migration

* refactor: selector logic and model readers for new namespace

* feat: compute correct selector on model register

* fix: contract address in event data

* refactor: update proto for new namespace

* feat: update torii client for new namespaces

* refactor: cache and schema types

* feat: rebase from main update to new types

* feat: update grpc service to support namespaces

* feat: add namespace to broker

* feat: fix libp2p and update grpc subs for pattern on models

* feat: finish up refactor on grpc

* chore: revert changes on proto to use onlky model field

* feat: first graphql support

* fix: composite clauses & wrap up

* fix: storage and tests

* refactor: storage to use selector

* fix: storage subscriptions

* escape composite tbales

* Overlays (#2127)

* load custom overlay manifests

* update metadata management + some fixes

* update sozo commands with namespace

* fix: change tag separator to `-` (#2132)

* wip

* fix tests

* merge and remove print

* fix: remove print

* fix: fmt and clippy wip

* fix: graphql tests

* model tests

* query test

* fmt

* fix: query

* chore: graphql subscription tests

* wip: fix testing

* wip: wip on testing

* fix: fix typo

* fix: use correct model name

* wip: testing

* feat: add namespace to model object

* fix: fix subscription tests

* fix: fix tests

* fix: fmt

* fix: clippy

* wip: latest torii test

* fix: fmt

* fix(torii-libp2p): validate namespace message

* fix(torii-libp2p): escape table name

* fmt

---------

Co-authored-by: Rémy Baranx <[email protected]>
Co-authored-by: glihm <[email protected]>

* fix: fix compiler diags by rewriting the function with rewrite node (#2141)

* fix: fix compiler diags by rewriting the function with rewrite node

* fix: ensure dojo init also shows correct diagnostics

* fix: fmt + clippy

* fix: ensure impl is correctly impl with RewriteNode

* fix: add missing mapping to interfaces

* fix: ensure wasm compatibility

* fix: test fixes for wasm32 changes (#2144)

* fix: test fixes for wasm32 changes

* fix: fix cairo tests

* fix: clean subcommand (#2122)

* fix: clean should only remove target/manifest file of particular profile by defaul

* fix lints

* fix formatting

* fix: ensure every target is considered independently

---------

Co-authored-by: glihm <[email protected]>

* fix: uncomment tests

* fix: add more tests

* fix: fix metadata test

* fix: add tests and fix metadata

* fix: attempt to fix flaky test with 1s delay

---------

Co-authored-by: [email protected] <[email protected]>
Co-authored-by: lambda-0x <[email protected]>
Co-authored-by: Tarrence van As <[email protected]>
Co-authored-by: notV4l <[email protected]>
Co-authored-by: glihm <[email protected]>
Co-authored-by: notV4l <[email protected]>
Co-authored-by: Larko <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants