Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into 9-implementation-of-t…
Browse files Browse the repository at this point in the history
…he-where-api
  • Loading branch information
tarrencev committed Nov 22, 2023
2 parents 520a765 + b3f4997 commit 49f5504
Show file tree
Hide file tree
Showing 35 changed files with 1,187 additions and 331 deletions.
31 changes: 16 additions & 15 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ edition = "2021"
license = "Apache-2.0"
license-file = "LICENSE"
repository = "https://github.com/dojoengine/dojo/"
version = "0.3.11"
version = "0.3.12"

[profile.performance]
codegen-units = 1
Expand Down
6 changes: 3 additions & 3 deletions crates/dojo-core/Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ version = 1

[[package]]
name = "dojo"
version = "0.3.11"
version = "0.3.12"
dependencies = [
"dojo_plugin",
]

[[package]]
name = "dojo_plugin"
version = "0.3.11"
source = "git+https://github.com/dojoengine/dojo?tag=v0.3.11#1e651b5d4d3b79b14a7d8aa29a92062fcb9e6659"
version = "0.3.12"
source = "git+https://github.com/dojoengine/dojo?tag=v0.3.12#12d58f29ec53454317f1f6d265007a053d279288"
4 changes: 2 additions & 2 deletions crates/dojo-core/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
cairo-version = "2.3.1"
description = "The Dojo Core library for autonomous worlds."
name = "dojo"
version = "0.3.11"
version = "0.3.12"

[dependencies]
dojo_plugin = { git = "https://github.com/dojoengine/dojo", tag = "v0.3.11" }
dojo_plugin = { git = "https://github.com/dojoengine/dojo", tag = "v0.3.12" }
starknet = "2.3.1"
39 changes: 19 additions & 20 deletions crates/dojo-core/src/base.cairo
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
use dojo::world::IWorldDispatcher;

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

#[starknet::contract]
mod base {
use starknet::{ClassHash, get_caller_address};

use dojo::upgradable::{IUpgradeable, UpgradeableTrait};
use dojo::world::IWorldDispatcher;
use dojo::world::IWorldProvider;

use dojo::components::upgradeable::upgradeable as upgradeable_component;

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

#[event]
#[derive(Drop, starknet::Event)]
enum Event {
UpgradeableEvent: upgradeable_component::Event
}

#[storage]
struct Storage {
world_dispatcher: IWorldDispatcher,
#[substorage(v0)]
upgradeable: upgradeable_component::Storage,
}

#[constructor]
Expand All @@ -23,19 +29,12 @@ mod base {
}

#[external(v0)]
fn world(self: @ContractState) -> IWorldDispatcher {
self.world_dispatcher.read()
}

#[external(v0)]
impl Upgradeable of IUpgradeable<ContractState> {
/// Upgrade contract implementation to new_class_hash
///
/// # Arguments
///
/// * `new_class_hash` - The new implementation class hahs.
fn upgrade(ref self: ContractState, new_class_hash: ClassHash) {
UpgradeableTrait::upgrade(new_class_hash);
impl WorldProviderImpl of IWorldProvider<ContractState> {
fn world(self: @ContractState) -> IWorldDispatcher {
self.world_dispatcher.read()
}
}

#[abi(embed_v0)]
impl UpgradableImpl = upgradeable_component::UpgradableImpl<ContractState>;
}
33 changes: 27 additions & 6 deletions crates/dojo-core/src/base_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ use starknet::ClassHash;
use traits::TryInto;

use dojo::base::base;
use dojo::upgradable::{IUpgradeableDispatcher, IUpgradeableDispatcherTrait};
use dojo::test_utils::deploy_contract;
use dojo::components::upgradeable::{IUpgradeableDispatcher, IUpgradeableDispatcherTrait};
use dojo::test_utils::{spawn_test_world};
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};


#[starknet::contract]
mod contract_upgrade {
Expand All @@ -29,15 +31,34 @@ mod contract_upgrade {

use contract_upgrade::{IQuantumLeapDispatcher, IQuantumLeapDispatcherTrait};

// Utils
fn deploy_world() -> IWorldDispatcher {
spawn_test_world(array![])
}

#[test]
#[available_gas(6000000)]
fn test_upgrade() {
let base_address = deploy_contract(base::TEST_CLASS_HASH, array![].span());
let upgradable_dispatcher = IUpgradeableDispatcher { contract_address: base_address };
fn test_upgrade_from_world() {
let world = deploy_world();

let base_address = world.deploy_contract('salt', base::TEST_CLASS_HASH.try_into().unwrap());
let new_class_hash: ClassHash = contract_upgrade::TEST_CLASS_HASH.try_into().unwrap();
upgradable_dispatcher.upgrade(new_class_hash);

world.upgrade_contract(base_address, new_class_hash);

let quantum_dispatcher = IQuantumLeapDispatcher { contract_address: base_address };
assert(quantum_dispatcher.plz_more_tps() == 'daddy', 'quantum leap failed');
}

#[test]
#[available_gas(6000000)]
#[should_panic(expected: ('must be called by world', 'ENTRYPOINT_FAILED'))]
fn test_upgrade_direct() {
let world = deploy_world();

let base_address = world.deploy_contract('salt', base::TEST_CLASS_HASH.try_into().unwrap());
let new_class_hash: ClassHash = contract_upgrade::TEST_CLASS_HASH.try_into().unwrap();

let upgradeable_dispatcher = IUpgradeableDispatcher { contract_address: base_address };
upgradeable_dispatcher.upgrade(new_class_hash);
}
1 change: 1 addition & 0 deletions crates/dojo-core/src/components.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod upgradeable;
56 changes: 56 additions & 0 deletions crates/dojo-core/src/components/upgradeable.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use starknet::ClassHash;

#[starknet::interface]
trait IUpgradeable<T> {
fn upgrade(ref self: T, new_class_hash: ClassHash);
}

#[starknet::component]
mod upgradeable {
use starknet::ClassHash;
use starknet::ContractAddress;
use starknet::get_caller_address;
use starknet::syscalls::replace_class_syscall;
use dojo::world::{IWorldProvider, IWorldProviderDispatcher, IWorldDispatcher};

#[storage]
struct Storage {}

#[event]
#[derive(Drop, starknet::Event)]
enum Event {
Upgraded: Upgraded,
}

#[derive(Drop, starknet::Event)]
struct Upgraded {
class_hash: ClassHash
}

mod Errors {
const INVALID_CLASS: felt252 = 'class_hash cannot be zero';
const INVALID_CALLER: felt252 = 'must be called by world';
const INVALID_WORLD_ADDRESS: felt252 = 'invalid world address';
}

#[embeddable_as(UpgradableImpl)]
impl Upgradable<
TContractState, +HasComponent<TContractState>, +IWorldProvider<TContractState>
> 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(),
Errors::INVALID_WORLD_ADDRESS
);
assert(
get_caller_address() == self.get_contract().world().contract_address,
Errors::INVALID_CALLER
);
assert(new_class_hash.is_non_zero(), Errors::INVALID_CLASS);

replace_class_syscall(new_class_hash).unwrap();

self.emit(Upgraded { class_hash: new_class_hash });
}
}
}
5 changes: 3 additions & 2 deletions crates/dojo-core/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ mod packing_test;
mod world;
#[cfg(test)]
mod world_test;
mod upgradable;

#[cfg(test)]
mod test_utils;

#[cfg(test)]
mod benchmarks;
mod benchmarks;

mod components;
19 changes: 0 additions & 19 deletions crates/dojo-core/src/upgradable.cairo

This file was deleted.

Loading

0 comments on commit 49f5504

Please sign in to comment.