From 4879b20167dc0490246d17524d0275b768e2585e Mon Sep 17 00:00:00 2001 From: Harsh Bajpai Date: Tue, 17 Oct 2023 09:44:24 +0530 Subject: [PATCH 1/8] feat: add upgradable component --- crates/contracts/src/components.cairo | 2 +- .../contracts/src/components/upgradable.cairo | 39 ++++++++ .../src/components/upgradeable.cairo | 3 - .../contracts/src/kakarot_core/kakarot.cairo | 15 ++- .../src/tests/test_kakarot_core.cairo | 18 ++++ .../src/tests/test_upgradeable.cairo | 99 ++++++++++++++++++- 6 files changed, 167 insertions(+), 9 deletions(-) create mode 100644 crates/contracts/src/components/upgradable.cairo delete mode 100644 crates/contracts/src/components/upgradeable.cairo diff --git a/crates/contracts/src/components.cairo b/crates/contracts/src/components.cairo index 23acc1b07..792e4420f 100644 --- a/crates/contracts/src/components.cairo +++ b/crates/contracts/src/components.cairo @@ -1,2 +1,2 @@ mod ownable; -mod upgradeable; +mod upgradable; diff --git a/crates/contracts/src/components/upgradable.cairo b/crates/contracts/src/components/upgradable.cairo new file mode 100644 index 000000000..de162e570 --- /dev/null +++ b/crates/contracts/src/components/upgradable.cairo @@ -0,0 +1,39 @@ +use starknet::{replace_class_syscall, ClassHash}; + +#[starknet::interface] +trait IUpgradable { + fn upgrade_contract(ref self: TContractState, class_hash: ClassHash); +} + + +#[starknet::component] +mod upgradable_component { + use starknet::ClassHash; + use starknet::info::get_caller_address; + + #[storage] + struct Storage {} + + #[event] + #[derive(Drop, starknet::Event)] + enum Event { + ContractUpgrated: ContractUpgrated + } + + #[derive(Drop, starknet::Event)] + struct ContractUpgrated { + new_class_hash: ClassHash + } + + #[embeddable_as(UpgradableImpl)] + impl Upgradable< + TContractState, +HasComponent + > of super::IUpgradable> { + fn upgrade_contract( + ref self: ComponentState, class_hash: starknet::ClassHash + ) { + starknet::replace_class_syscall(class_hash); + self.emit(ContractUpgrated { new_class_hash: class_hash }); + } + } +} diff --git a/crates/contracts/src/components/upgradeable.cairo b/crates/contracts/src/components/upgradeable.cairo deleted file mode 100644 index 60088299d..000000000 --- a/crates/contracts/src/components/upgradeable.cairo +++ /dev/null @@ -1,3 +0,0 @@ -// TODO - - diff --git a/crates/contracts/src/kakarot_core/kakarot.cairo b/crates/contracts/src/kakarot_core/kakarot.cairo index a68d16d42..4402aff04 100644 --- a/crates/contracts/src/kakarot_core/kakarot.cairo +++ b/crates/contracts/src/kakarot_core/kakarot.cairo @@ -19,6 +19,8 @@ struct ContractAccountStorage { mod KakarotCore { use contracts::components::ownable::ownable_component::InternalTrait; use contracts::components::ownable::{ownable_component}; + use contracts::components::upgradable::IUpgradable; + use contracts::components::upgradable::{upgradable_component}; use contracts::kakarot_core::interface::IKakarotCore; use contracts::kakarot_core::interface; use core::hash::{HashStateExTrait, HashStateTrait}; @@ -35,12 +37,15 @@ mod KakarotCore { use utils::traits::U256TryIntoContractAddress; component!(path: ownable_component, storage: ownable, event: OwnableEvent); + component!(path: upgradable_component, storage: upgradable, event: UpgradableEvent); #[abi(embed_v0)] impl OwnableImpl = ownable_component::Ownable; impl OwnableInternalImpl = ownable_component::InternalImpl; + impl UpgradableImpl = upgradable_component::UpgradableImpl; + #[storage] struct Storage { /// Kakarot storage for accounts: Externally Owned Accounts (EOA) and Contract Accounts (CA) @@ -64,12 +69,15 @@ mod KakarotCore { // Components #[substorage(v0)] ownable: ownable_component::Storage, + #[substorage(v0)] + upgradable: upgradable_component::Storage, } #[event] #[derive(Drop, starknet::Event)] enum Event { OwnableEvent: ownable_component::Event, + UpgradableEvent: upgradable_component::Event, EOADeployed: EOADeployed, } @@ -248,9 +256,9 @@ mod KakarotCore { /// Upgrade the KakarotCore smart contract /// Using replace_class_syscall - fn upgrade( - ref self: ContractState, new_class_hash: ClassHash - ) { //TODO: implement upgrade logic + fn upgrade(ref self: ContractState, new_class_hash: ClassHash) { + self.ownable.assert_only_owner(); + self.upgradable.upgrade_contract(new_class_hash); } } @@ -269,4 +277,3 @@ mod KakarotCore { } } } - diff --git a/crates/contracts/src/tests/test_kakarot_core.cairo b/crates/contracts/src/tests/test_kakarot_core.cairo index d7f992d5e..4cfb1d36f 100644 --- a/crates/contracts/src/tests/test_kakarot_core.cairo +++ b/crates/contracts/src/tests/test_kakarot_core.cairo @@ -1,5 +1,8 @@ use contracts::components::ownable::ownable_component; use contracts::kakarot_core::{interface::IExtendedKakarotCoreDispatcherImpl, KakarotCore}; +use contracts::tests::test_upgradeable::{ + MockContractUpdatableV1, IMockContractUpdatableDispatcher, IMockContractUpdatableDispatcherTrait +}; use contracts::tests::utils; use debug::PrintTrait; use eoa::externally_owned_account::ExternallyOwnedAccount; @@ -97,3 +100,18 @@ fn test_kakarot_core_compute_starknet_address() { assert(eoa_starknet_address == expected_starknet_address, 'wrong starknet address'); } +#[test] +#[available_gas(20000000)] +fn test_kakarot_core_upgrade_contract() { + let kakarot_core = utils::deploy_kakarot_core(test_utils::native_token()); + let class_hash: ClassHash = MockContractUpdatableV1::TEST_CLASS_HASH.try_into().unwrap(); + + testing::set_contract_address(utils::other_starknet_address()); + kakarot_core.upgrade(class_hash); + + let version = IMockContractUpdatableDispatcher { + contract_address: kakarot_core.contract_address + } + .version(); + assert(version == 1, 'version is not 1'); +} diff --git a/crates/contracts/src/tests/test_upgradeable.cairo b/crates/contracts/src/tests/test_upgradeable.cairo index 60088299d..dec4fe4fd 100644 --- a/crates/contracts/src/tests/test_upgradeable.cairo +++ b/crates/contracts/src/tests/test_upgradeable.cairo @@ -1,3 +1,100 @@ -// TODO +use MockContractUpdatableV0::HasComponentImpl_upgradable_component; +use contracts::components::upgradable::{IUpgradableDispatcher, IUpgradableDispatcherTrait}; +use contracts::components::upgradable::{upgradable_component}; +use contracts::tests::utils; +use debug::PrintTrait; +use serde::Serde; +use starknet::{deploy_syscall, ClassHash, ContractAddress, testing}; +use upgradable_component::{UpgradableImpl}; +#[starknet::interface] +trait IMockContractUpdatable { + fn version(self: @TContractState) -> felt252; +} + +#[starknet::contract] +mod MockContractUpdatableV0 { + use contracts::components::upgradable::{upgradable_component}; + use super::IMockContractUpdatable; + component!(path: upgradable_component, storage: upgradable, event: UpgradableEvent); + + #[abi(embed_v0)] + impl UpgradableImpl = upgradable_component::UpgradableImpl; + + #[storage] + struct Storage { + #[substorage(v0)] + upgradable: upgradable_component::Storage + } + + #[event] + #[derive(Drop, starknet::Event)] + enum Event { + UpgradableEvent: upgradable_component::Event + } + + #[external(v0)] + impl MockContractUpdatableImpl of IMockContractUpdatable { + fn version(self: @ContractState) -> felt252 { + 0 + } + } +} + +type TestingState = upgradable_component::ComponentState; + +impl TestingStateDefault of Default { + fn default() -> TestingState { + upgradable_component::component_state_for_testing() + } +} + +#[test] +#[available_gas(500000)] +fn test_upgradable_update_contract() { + let (contract_address, _) = deploy_syscall( + MockContractUpdatableV0::TEST_CLASS_HASH.try_into().unwrap(), 0, array![].span(), false + ) + .unwrap(); + + let version = IMockContractUpdatableDispatcher { contract_address: contract_address }.version(); + + assert(version == 0, 'version is not 0'); + + let mut call_data: Array = array![]; + + let new_class_hash: ClassHash = MockContractUpdatableV1::TEST_CLASS_HASH.try_into().unwrap(); + + IUpgradableDispatcher { contract_address: contract_address }.upgrade_contract(new_class_hash); + + let version = IMockContractUpdatableDispatcher { contract_address: contract_address }.version(); + assert(version == 1, 'version is not 1'); +} + + +#[starknet::contract] +mod MockContractUpdatableV1 { + use contracts::components::upgradable::{upgradable_component}; + use super::IMockContractUpdatable; + component!(path: upgradable_component, storage: upgradable, event: UpgradableEvent); + + #[storage] + struct Storage { + #[substorage(v0)] + upgradable: upgradable_component::Storage + } + + #[event] + #[derive(Drop, starknet::Event)] + enum Event { + UpgradableEvent: upgradable_component::Event + } + + #[external(v0)] + impl MockContractUpdatableImpl of IMockContractUpdatable { + fn version(self: @ContractState) -> felt252 { + 1 + } + } +} From cac6874e8fa73be1438e3fd88018a7ed08cee966 Mon Sep 17 00:00:00 2001 From: Harsh Bajpai Date: Tue, 17 Oct 2023 10:40:23 +0530 Subject: [PATCH 2/8] dev: change updradable to updradeable + change updatable to upgradeable --- crates/contracts/src/components.cairo | 2 +- .../{upgradable.cairo => upgradeable.cairo} | 10 ++-- .../contracts/src/kakarot_core/kakarot.cairo | 14 ++--- .../src/tests/test_kakarot_core.cairo | 7 ++- .../src/tests/test_upgradeable.cairo | 58 ++++++++++--------- 5 files changed, 47 insertions(+), 44 deletions(-) rename crates/contracts/src/components/{upgradable.cairo => upgradeable.cairo} (81%) diff --git a/crates/contracts/src/components.cairo b/crates/contracts/src/components.cairo index 792e4420f..23acc1b07 100644 --- a/crates/contracts/src/components.cairo +++ b/crates/contracts/src/components.cairo @@ -1,2 +1,2 @@ mod ownable; -mod upgradable; +mod upgradeable; diff --git a/crates/contracts/src/components/upgradable.cairo b/crates/contracts/src/components/upgradeable.cairo similarity index 81% rename from crates/contracts/src/components/upgradable.cairo rename to crates/contracts/src/components/upgradeable.cairo index de162e570..1068f6f4a 100644 --- a/crates/contracts/src/components/upgradable.cairo +++ b/crates/contracts/src/components/upgradeable.cairo @@ -1,13 +1,13 @@ use starknet::{replace_class_syscall, ClassHash}; #[starknet::interface] -trait IUpgradable { +trait Iupgradeable { fn upgrade_contract(ref self: TContractState, class_hash: ClassHash); } #[starknet::component] -mod upgradable_component { +mod upgradeable_component { use starknet::ClassHash; use starknet::info::get_caller_address; @@ -25,10 +25,10 @@ mod upgradable_component { new_class_hash: ClassHash } - #[embeddable_as(UpgradableImpl)] - impl Upgradable< + #[embeddable_as(upgradeableImpl)] + impl upgradeable< TContractState, +HasComponent - > of super::IUpgradable> { + > of super::Iupgradeable> { fn upgrade_contract( ref self: ComponentState, class_hash: starknet::ClassHash ) { diff --git a/crates/contracts/src/kakarot_core/kakarot.cairo b/crates/contracts/src/kakarot_core/kakarot.cairo index 4402aff04..11720456c 100644 --- a/crates/contracts/src/kakarot_core/kakarot.cairo +++ b/crates/contracts/src/kakarot_core/kakarot.cairo @@ -19,8 +19,8 @@ struct ContractAccountStorage { mod KakarotCore { use contracts::components::ownable::ownable_component::InternalTrait; use contracts::components::ownable::{ownable_component}; - use contracts::components::upgradable::IUpgradable; - use contracts::components::upgradable::{upgradable_component}; + use contracts::components::upgradeable::Iupgradeable; + use contracts::components::upgradeable::{upgradeable_component}; use contracts::kakarot_core::interface::IKakarotCore; use contracts::kakarot_core::interface; use core::hash::{HashStateExTrait, HashStateTrait}; @@ -37,14 +37,14 @@ mod KakarotCore { use utils::traits::U256TryIntoContractAddress; component!(path: ownable_component, storage: ownable, event: OwnableEvent); - component!(path: upgradable_component, storage: upgradable, event: UpgradableEvent); + component!(path: upgradeable_component, storage: upgradeable, event: upgradeableEvent); #[abi(embed_v0)] impl OwnableImpl = ownable_component::Ownable; impl OwnableInternalImpl = ownable_component::InternalImpl; - impl UpgradableImpl = upgradable_component::UpgradableImpl; + impl upgradeableImpl = upgradeable_component::upgradeableImpl; #[storage] struct Storage { @@ -70,14 +70,14 @@ mod KakarotCore { #[substorage(v0)] ownable: ownable_component::Storage, #[substorage(v0)] - upgradable: upgradable_component::Storage, + upgradeable: upgradeable_component::Storage, } #[event] #[derive(Drop, starknet::Event)] enum Event { OwnableEvent: ownable_component::Event, - UpgradableEvent: upgradable_component::Event, + upgradeableEvent: upgradeable_component::Event, EOADeployed: EOADeployed, } @@ -258,7 +258,7 @@ mod KakarotCore { /// Using replace_class_syscall fn upgrade(ref self: ContractState, new_class_hash: ClassHash) { self.ownable.assert_only_owner(); - self.upgradable.upgrade_contract(new_class_hash); + self.upgradeable.upgrade_contract(new_class_hash); } } diff --git a/crates/contracts/src/tests/test_kakarot_core.cairo b/crates/contracts/src/tests/test_kakarot_core.cairo index 4cfb1d36f..bd70aa56c 100644 --- a/crates/contracts/src/tests/test_kakarot_core.cairo +++ b/crates/contracts/src/tests/test_kakarot_core.cairo @@ -1,7 +1,8 @@ use contracts::components::ownable::ownable_component; use contracts::kakarot_core::{interface::IExtendedKakarotCoreDispatcherImpl, KakarotCore}; use contracts::tests::test_upgradeable::{ - MockContractUpdatableV1, IMockContractUpdatableDispatcher, IMockContractUpdatableDispatcherTrait + MockContractUpgradeableV1, IMockContractUpgradeableDispatcher, + IMockContractUpgradeableDispatcherTrait }; use contracts::tests::utils; use debug::PrintTrait; @@ -104,12 +105,12 @@ fn test_kakarot_core_compute_starknet_address() { #[available_gas(20000000)] fn test_kakarot_core_upgrade_contract() { let kakarot_core = utils::deploy_kakarot_core(test_utils::native_token()); - let class_hash: ClassHash = MockContractUpdatableV1::TEST_CLASS_HASH.try_into().unwrap(); + let class_hash: ClassHash = MockContractUpgradeableV1::TEST_CLASS_HASH.try_into().unwrap(); testing::set_contract_address(utils::other_starknet_address()); kakarot_core.upgrade(class_hash); - let version = IMockContractUpdatableDispatcher { + let version = IMockContractUpgradeableDispatcher { contract_address: kakarot_core.contract_address } .version(); diff --git a/crates/contracts/src/tests/test_upgradeable.cairo b/crates/contracts/src/tests/test_upgradeable.cairo index dec4fe4fd..21e84adfd 100644 --- a/crates/contracts/src/tests/test_upgradeable.cairo +++ b/crates/contracts/src/tests/test_upgradeable.cairo @@ -1,98 +1,100 @@ -use MockContractUpdatableV0::HasComponentImpl_upgradable_component; -use contracts::components::upgradable::{IUpgradableDispatcher, IUpgradableDispatcherTrait}; -use contracts::components::upgradable::{upgradable_component}; +use MockContractUpgradeableV0::HasComponentImpl_upgradeable_component; +use contracts::components::upgradeable::{IupgradeableDispatcher, IupgradeableDispatcherTrait}; +use contracts::components::upgradeable::{upgradeable_component}; use contracts::tests::utils; use debug::PrintTrait; use serde::Serde; use starknet::{deploy_syscall, ClassHash, ContractAddress, testing}; -use upgradable_component::{UpgradableImpl}; +use upgradeable_component::{upgradeableImpl}; #[starknet::interface] -trait IMockContractUpdatable { +trait IMockContractUpgradeable { fn version(self: @TContractState) -> felt252; } #[starknet::contract] -mod MockContractUpdatableV0 { - use contracts::components::upgradable::{upgradable_component}; - use super::IMockContractUpdatable; - component!(path: upgradable_component, storage: upgradable, event: UpgradableEvent); +mod MockContractUpgradeableV0 { + use contracts::components::upgradeable::{upgradeable_component}; + use super::IMockContractUpgradeable; + component!(path: upgradeable_component, storage: upgradeable, event: upgradeableEvent); #[abi(embed_v0)] - impl UpgradableImpl = upgradable_component::UpgradableImpl; + impl upgradeableImpl = upgradeable_component::upgradeableImpl; #[storage] struct Storage { #[substorage(v0)] - upgradable: upgradable_component::Storage + upgradeable: upgradeable_component::Storage } #[event] #[derive(Drop, starknet::Event)] enum Event { - UpgradableEvent: upgradable_component::Event + upgradeableEvent: upgradeable_component::Event } #[external(v0)] - impl MockContractUpdatableImpl of IMockContractUpdatable { + impl MockContractUpgradeableImpl of IMockContractUpgradeable { fn version(self: @ContractState) -> felt252 { 0 } } } -type TestingState = upgradable_component::ComponentState; +type TestingState = upgradeable_component::ComponentState; impl TestingStateDefault of Default { fn default() -> TestingState { - upgradable_component::component_state_for_testing() + upgradeable_component::component_state_for_testing() } } #[test] #[available_gas(500000)] -fn test_upgradable_update_contract() { +fn test_upgradeable_update_contract() { let (contract_address, _) = deploy_syscall( - MockContractUpdatableV0::TEST_CLASS_HASH.try_into().unwrap(), 0, array![].span(), false + MockContractUpgradeableV0::TEST_CLASS_HASH.try_into().unwrap(), 0, array![].span(), false ) .unwrap(); - let version = IMockContractUpdatableDispatcher { contract_address: contract_address }.version(); + let version = IMockContractUpgradeableDispatcher { contract_address: contract_address } + .version(); assert(version == 0, 'version is not 0'); let mut call_data: Array = array![]; - let new_class_hash: ClassHash = MockContractUpdatableV1::TEST_CLASS_HASH.try_into().unwrap(); + let new_class_hash: ClassHash = MockContractUpgradeableV1::TEST_CLASS_HASH.try_into().unwrap(); - IUpgradableDispatcher { contract_address: contract_address }.upgrade_contract(new_class_hash); + IupgradeableDispatcher { contract_address: contract_address }.upgrade_contract(new_class_hash); - let version = IMockContractUpdatableDispatcher { contract_address: contract_address }.version(); + let version = IMockContractUpgradeableDispatcher { contract_address: contract_address } + .version(); assert(version == 1, 'version is not 1'); } #[starknet::contract] -mod MockContractUpdatableV1 { - use contracts::components::upgradable::{upgradable_component}; - use super::IMockContractUpdatable; - component!(path: upgradable_component, storage: upgradable, event: UpgradableEvent); +mod MockContractUpgradeableV1 { + use contracts::components::upgradeable::{upgradeable_component}; + use super::IMockContractUpgradeable; + component!(path: upgradeable_component, storage: upgradeable, event: upgradeableEvent); #[storage] struct Storage { #[substorage(v0)] - upgradable: upgradable_component::Storage + upgradeable: upgradeable_component::Storage } #[event] #[derive(Drop, starknet::Event)] enum Event { - UpgradableEvent: upgradable_component::Event + upgradeableEvent: upgradeable_component::Event } #[external(v0)] - impl MockContractUpdatableImpl of IMockContractUpdatable { + impl MockContractUpgradeableImpl of IMockContractUpgradeable { fn version(self: @ContractState) -> felt252 { 1 } From 03f5be8d21129d7790167afe484df179b1a7fb92 Mon Sep 17 00:00:00 2001 From: Harsh Bajpai Date: Tue, 17 Oct 2023 16:24:51 +0530 Subject: [PATCH 3/8] dev: change upgratable to upgradeable --- crates/contracts/src/components/upgradeable.cairo | 14 +++++++------- crates/contracts/src/kakarot_core/kakarot.cairo | 4 ++-- crates/contracts/src/tests/test_upgradeable.cairo | 12 ++++++------ 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/crates/contracts/src/components/upgradeable.cairo b/crates/contracts/src/components/upgradeable.cairo index 1068f6f4a..86f913dc0 100644 --- a/crates/contracts/src/components/upgradeable.cairo +++ b/crates/contracts/src/components/upgradeable.cairo @@ -1,7 +1,7 @@ use starknet::{replace_class_syscall, ClassHash}; #[starknet::interface] -trait Iupgradeable { +trait IUpgradeable { fn upgrade_contract(ref self: TContractState, class_hash: ClassHash); } @@ -17,23 +17,23 @@ mod upgradeable_component { #[event] #[derive(Drop, starknet::Event)] enum Event { - ContractUpgrated: ContractUpgrated + ContractUpgraded: ContractUpgraded } #[derive(Drop, starknet::Event)] - struct ContractUpgrated { + struct ContractUpgraded { new_class_hash: ClassHash } - #[embeddable_as(upgradeableImpl)] - impl upgradeable< + #[embeddable_as(Upgradeable)] + impl UpgradeableImpl< TContractState, +HasComponent - > of super::Iupgradeable> { + > of super::IUpgradeable> { fn upgrade_contract( ref self: ComponentState, class_hash: starknet::ClassHash ) { starknet::replace_class_syscall(class_hash); - self.emit(ContractUpgrated { new_class_hash: class_hash }); + self.emit(ContractUpgraded { new_class_hash: class_hash }); } } } diff --git a/crates/contracts/src/kakarot_core/kakarot.cairo b/crates/contracts/src/kakarot_core/kakarot.cairo index 11720456c..7a138da0c 100644 --- a/crates/contracts/src/kakarot_core/kakarot.cairo +++ b/crates/contracts/src/kakarot_core/kakarot.cairo @@ -19,7 +19,7 @@ struct ContractAccountStorage { mod KakarotCore { use contracts::components::ownable::ownable_component::InternalTrait; use contracts::components::ownable::{ownable_component}; - use contracts::components::upgradeable::Iupgradeable; + use contracts::components::upgradeable::IUpgradeable; use contracts::components::upgradeable::{upgradeable_component}; use contracts::kakarot_core::interface::IKakarotCore; use contracts::kakarot_core::interface; @@ -44,7 +44,7 @@ mod KakarotCore { impl OwnableInternalImpl = ownable_component::InternalImpl; - impl upgradeableImpl = upgradeable_component::upgradeableImpl; + impl upgradeableImpl = upgradeable_component::UpgradeableImpl; #[storage] struct Storage { diff --git a/crates/contracts/src/tests/test_upgradeable.cairo b/crates/contracts/src/tests/test_upgradeable.cairo index 21e84adfd..b7c28ad63 100644 --- a/crates/contracts/src/tests/test_upgradeable.cairo +++ b/crates/contracts/src/tests/test_upgradeable.cairo @@ -1,12 +1,12 @@ use MockContractUpgradeableV0::HasComponentImpl_upgradeable_component; -use contracts::components::upgradeable::{IupgradeableDispatcher, IupgradeableDispatcherTrait}; +use contracts::components::upgradeable::{IUpgradeableDispatcher, IUpgradeableDispatcherTrait}; use contracts::components::upgradeable::{upgradeable_component}; use contracts::tests::utils; use debug::PrintTrait; use serde::Serde; use starknet::{deploy_syscall, ClassHash, ContractAddress, testing}; -use upgradeable_component::{upgradeableImpl}; +use upgradeable_component::{UpgradeableImpl}; #[starknet::interface] trait IMockContractUpgradeable { @@ -17,10 +17,10 @@ trait IMockContractUpgradeable { mod MockContractUpgradeableV0 { use contracts::components::upgradeable::{upgradeable_component}; use super::IMockContractUpgradeable; - component!(path: upgradeable_component, storage: upgradeable, event: upgradeableEvent); + component!(path: upgradeable_component, storage: upgradeable, event: UpgradeableEvent); #[abi(embed_v0)] - impl upgradeableImpl = upgradeable_component::upgradeableImpl; + impl upgradeableImpl = upgradeable_component::Upgradeable; #[storage] struct Storage { @@ -31,7 +31,7 @@ mod MockContractUpgradeableV0 { #[event] #[derive(Drop, starknet::Event)] enum Event { - upgradeableEvent: upgradeable_component::Event + UpgradeableEvent: upgradeable_component::Event } #[external(v0)] @@ -67,7 +67,7 @@ fn test_upgradeable_update_contract() { let new_class_hash: ClassHash = MockContractUpgradeableV1::TEST_CLASS_HASH.try_into().unwrap(); - IupgradeableDispatcher { contract_address: contract_address }.upgrade_contract(new_class_hash); + IUpgradeableDispatcher { contract_address: contract_address }.upgrade_contract(new_class_hash); let version = IMockContractUpgradeableDispatcher { contract_address: contract_address } .version(); From b85f3ad13b4524b7972710f2235da04166e8b585 Mon Sep 17 00:00:00 2001 From: Harsh Bajpai Date: Tue, 17 Oct 2023 16:31:55 +0530 Subject: [PATCH 4/8] dev: minor changes on casing --- .../contracts/src/kakarot_core/kakarot.cairo | 5 +- .../src/tests/test_upgradeable.cairo | 49 +++++++++---------- 2 files changed, 26 insertions(+), 28 deletions(-) diff --git a/crates/contracts/src/kakarot_core/kakarot.cairo b/crates/contracts/src/kakarot_core/kakarot.cairo index 7a138da0c..dab9bfb35 100644 --- a/crates/contracts/src/kakarot_core/kakarot.cairo +++ b/crates/contracts/src/kakarot_core/kakarot.cairo @@ -19,8 +19,7 @@ struct ContractAccountStorage { mod KakarotCore { use contracts::components::ownable::ownable_component::InternalTrait; use contracts::components::ownable::{ownable_component}; - use contracts::components::upgradeable::IUpgradeable; - use contracts::components::upgradeable::{upgradeable_component}; + use contracts::components::upgradeable::{IUpgradeable, upgradeable_component}; use contracts::kakarot_core::interface::IKakarotCore; use contracts::kakarot_core::interface; use core::hash::{HashStateExTrait, HashStateTrait}; @@ -44,7 +43,7 @@ mod KakarotCore { impl OwnableInternalImpl = ownable_component::InternalImpl; - impl upgradeableImpl = upgradeable_component::UpgradeableImpl; + impl upgradeableImpl = upgradeable_component::Upgradeable; #[storage] struct Storage { diff --git a/crates/contracts/src/tests/test_upgradeable.cairo b/crates/contracts/src/tests/test_upgradeable.cairo index b7c28ad63..e25828696 100644 --- a/crates/contracts/src/tests/test_upgradeable.cairo +++ b/crates/contracts/src/tests/test_upgradeable.cairo @@ -50,31 +50,6 @@ impl TestingStateDefault of Default { } } -#[test] -#[available_gas(500000)] -fn test_upgradeable_update_contract() { - let (contract_address, _) = deploy_syscall( - MockContractUpgradeableV0::TEST_CLASS_HASH.try_into().unwrap(), 0, array![].span(), false - ) - .unwrap(); - - let version = IMockContractUpgradeableDispatcher { contract_address: contract_address } - .version(); - - assert(version == 0, 'version is not 0'); - - let mut call_data: Array = array![]; - - let new_class_hash: ClassHash = MockContractUpgradeableV1::TEST_CLASS_HASH.try_into().unwrap(); - - IUpgradeableDispatcher { contract_address: contract_address }.upgrade_contract(new_class_hash); - - let version = IMockContractUpgradeableDispatcher { contract_address: contract_address } - .version(); - assert(version == 1, 'version is not 1'); -} - - #[starknet::contract] mod MockContractUpgradeableV1 { use contracts::components::upgradeable::{upgradeable_component}; @@ -100,3 +75,27 @@ mod MockContractUpgradeableV1 { } } } + +#[test] +#[available_gas(500000)] +fn test_upgradeable_update_contract() { + let (contract_address, _) = deploy_syscall( + MockContractUpgradeableV0::TEST_CLASS_HASH.try_into().unwrap(), 0, array![].span(), false + ) + .unwrap(); + + let version = IMockContractUpgradeableDispatcher { contract_address: contract_address } + .version(); + + assert(version == 0, 'version is not 0'); + + let mut call_data: Array = array![]; + + let new_class_hash: ClassHash = MockContractUpgradeableV1::TEST_CLASS_HASH.try_into().unwrap(); + + IUpgradeableDispatcher { contract_address: contract_address }.upgrade_contract(new_class_hash); + + let version = IMockContractUpgradeableDispatcher { contract_address: contract_address } + .version(); + assert(version == 1, 'version is not 1'); +} From 71262afb3722ce5b0a6b2e17c5be27ca88ccf5d9 Mon Sep 17 00:00:00 2001 From: Harsh Bajpai Date: Tue, 17 Oct 2023 16:34:53 +0530 Subject: [PATCH 5/8] dev: rename function parameter to `new_class_hash` from `class_hash` for `upgrade_contract` --- crates/contracts/src/components/upgradeable.cairo | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/contracts/src/components/upgradeable.cairo b/crates/contracts/src/components/upgradeable.cairo index 86f913dc0..373f57c35 100644 --- a/crates/contracts/src/components/upgradeable.cairo +++ b/crates/contracts/src/components/upgradeable.cairo @@ -2,7 +2,7 @@ use starknet::{replace_class_syscall, ClassHash}; #[starknet::interface] trait IUpgradeable { - fn upgrade_contract(ref self: TContractState, class_hash: ClassHash); + fn upgrade_contract(ref self: TContractState, new_class_hash: ClassHash); } @@ -30,10 +30,10 @@ mod upgradeable_component { TContractState, +HasComponent > of super::IUpgradeable> { fn upgrade_contract( - ref self: ComponentState, class_hash: starknet::ClassHash + ref self: ComponentState, new_class_hash: starknet::ClassHash ) { - starknet::replace_class_syscall(class_hash); - self.emit(ContractUpgraded { new_class_hash: class_hash }); + starknet::replace_class_syscall(new_class_hash); + self.emit(ContractUpgraded { new_class_hash: new_class_hash }); } } } From 7428bba9613a1f76acf82ab0662b641391de8ad5 Mon Sep 17 00:00:00 2001 From: Harsh Bajpai Date: Tue, 17 Oct 2023 19:38:17 +0530 Subject: [PATCH 6/8] dev: remove unused code + fix casing --- crates/contracts/src/kakarot_core/kakarot.cairo | 2 +- crates/contracts/src/tests/test_upgradeable.cairo | 8 -------- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/crates/contracts/src/kakarot_core/kakarot.cairo b/crates/contracts/src/kakarot_core/kakarot.cairo index dab9bfb35..22fbb755d 100644 --- a/crates/contracts/src/kakarot_core/kakarot.cairo +++ b/crates/contracts/src/kakarot_core/kakarot.cairo @@ -43,7 +43,7 @@ mod KakarotCore { impl OwnableInternalImpl = ownable_component::InternalImpl; - impl upgradeableImpl = upgradeable_component::Upgradeable; + impl UpgradeableImpl = upgradeable_component::Upgradeable; #[storage] struct Storage { diff --git a/crates/contracts/src/tests/test_upgradeable.cairo b/crates/contracts/src/tests/test_upgradeable.cairo index e25828696..e418b89b3 100644 --- a/crates/contracts/src/tests/test_upgradeable.cairo +++ b/crates/contracts/src/tests/test_upgradeable.cairo @@ -42,14 +42,6 @@ mod MockContractUpgradeableV0 { } } -type TestingState = upgradeable_component::ComponentState; - -impl TestingStateDefault of Default { - fn default() -> TestingState { - upgradeable_component::component_state_for_testing() - } -} - #[starknet::contract] mod MockContractUpgradeableV1 { use contracts::components::upgradeable::{upgradeable_component}; From 7034e51444a617997647b86ac78a330310096221 Mon Sep 17 00:00:00 2001 From: Harsh Bajpai Date: Tue, 17 Oct 2023 19:40:14 +0530 Subject: [PATCH 7/8] dev: fix casing --- crates/contracts/src/kakarot_core/kakarot.cairo | 2 +- crates/contracts/src/tests/test_upgradeable.cairo | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/contracts/src/kakarot_core/kakarot.cairo b/crates/contracts/src/kakarot_core/kakarot.cairo index 22fbb755d..1d6c93561 100644 --- a/crates/contracts/src/kakarot_core/kakarot.cairo +++ b/crates/contracts/src/kakarot_core/kakarot.cairo @@ -76,7 +76,7 @@ mod KakarotCore { #[derive(Drop, starknet::Event)] enum Event { OwnableEvent: ownable_component::Event, - upgradeableEvent: upgradeable_component::Event, + UpgradeableEvent: upgradeable_component::Event, EOADeployed: EOADeployed, } diff --git a/crates/contracts/src/tests/test_upgradeable.cairo b/crates/contracts/src/tests/test_upgradeable.cairo index e418b89b3..8dc06d977 100644 --- a/crates/contracts/src/tests/test_upgradeable.cairo +++ b/crates/contracts/src/tests/test_upgradeable.cairo @@ -20,7 +20,7 @@ mod MockContractUpgradeableV0 { component!(path: upgradeable_component, storage: upgradeable, event: UpgradeableEvent); #[abi(embed_v0)] - impl upgradeableImpl = upgradeable_component::Upgradeable; + impl UpgradeableImpl = upgradeable_component::Upgradeable; #[storage] struct Storage { From 088b89c1af3c0eff7a7eae5f51d68e6fdb8b3a2c Mon Sep 17 00:00:00 2001 From: Harsh Bajpai Date: Tue, 17 Oct 2023 19:40:50 +0530 Subject: [PATCH 8/8] dev: fix casing --- crates/contracts/src/kakarot_core/kakarot.cairo | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/contracts/src/kakarot_core/kakarot.cairo b/crates/contracts/src/kakarot_core/kakarot.cairo index 1d6c93561..4a9581769 100644 --- a/crates/contracts/src/kakarot_core/kakarot.cairo +++ b/crates/contracts/src/kakarot_core/kakarot.cairo @@ -36,7 +36,7 @@ mod KakarotCore { use utils::traits::U256TryIntoContractAddress; component!(path: ownable_component, storage: ownable, event: OwnableEvent); - component!(path: upgradeable_component, storage: upgradeable, event: upgradeableEvent); + component!(path: upgradeable_component, storage: upgradeable, event: UpgradeableEvent); #[abi(embed_v0)] impl OwnableImpl = ownable_component::Ownable;