From aeb26324efc5794d237198a2756b2606e99197f6 Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 31 May 2023 13:34:09 -0400 Subject: [PATCH 01/42] add camel support --- src/openzeppelin/access/ownable.cairo | 99 ++++++++++++++++++++++----- 1 file changed, 81 insertions(+), 18 deletions(-) diff --git a/src/openzeppelin/access/ownable.cairo b/src/openzeppelin/access/ownable.cairo index 69bf5b2b1..c91b76766 100644 --- a/src/openzeppelin/access/ownable.cairo +++ b/src/openzeppelin/access/ownable.cairo @@ -1,5 +1,28 @@ +use starknet::ContractAddress; + +#[abi] +trait IOwnable { + #[view] + fn owner() -> ContractAddress; + #[external] + fn transfer_ownership(new_owner: ContractAddress); + #[external] + fn renounce_ownership(); +} + +#[abi] +trait IOwnableCamel { + #[view] + fn owner() -> ContractAddress; + #[external] + fn transferOwnership(newOwner: ContractAddress); + #[external] + fn renounceOwnership(); +} + #[contract] mod Ownable { + use super::{IOwnable, IOwnableCamel}; use starknet::ContractAddress; use starknet::get_caller_address; use zeroable::Zeroable; @@ -11,6 +34,64 @@ mod Ownable { #[event] fn OwnershipTransferred(previous_owner: ContractAddress, new_owner: ContractAddress) {} + impl OwnableImpl of IOwnable { + fn owner() -> ContractAddress { + _owner::read() + } + + fn transfer_ownership(new_owner: ContractAddress) { + assert(!new_owner.is_zero(), 'New owner is the zero address'); + assert_only_owner(); + _transfer_ownership(new_owner); + } + + fn renounce_ownership() { + assert_only_owner(); + _transfer_ownership(Zeroable::zero()); + } + } + + impl OwnableCamelImpl of IOwnableCamel { + fn owner() -> ContractAddress { + OwnableImpl::owner() + } + + fn transferOwnership(newOwner: ContractAddress) { + OwnableImpl::transfer_ownership(newOwner); + } + + fn renounceOwnership() { + OwnableImpl::renounce_ownership(); + } + } + + #[view] + fn owner() -> ContractAddress { + OwnableImpl::owner() + } + + #[external] + fn transfer_ownership(new_owner: ContractAddress) { + OwnableImpl::transfer_ownership(new_owner); + } + + #[external] + fn transferOwnership(newOwner: ContractAddress) { + OwnableCamelImpl::transferOwnership(newOwner); + } + + #[external] + fn renounce_ownership() { + OwnableImpl::renounce_ownership(); + } + + #[external] + fn renounceOwnership() { + OwnableCamelImpl::renounceOwnership(); + } + + // Internals + #[internal] fn initializer() { let caller: ContractAddress = get_caller_address(); @@ -25,24 +106,6 @@ mod Ownable { assert(caller == owner, 'Caller is not the owner'); } - #[internal] - fn owner() -> ContractAddress { - _owner::read() - } - - #[internal] - fn transfer_ownership(new_owner: ContractAddress) { - assert(!new_owner.is_zero(), 'New owner is the zero address'); - assert_only_owner(); - _transfer_ownership(new_owner); - } - - #[internal] - fn renounce_ownership() { - assert_only_owner(); - _transfer_ownership(Zeroable::zero()); - } - #[internal] fn _transfer_ownership(new_owner: ContractAddress) { let previous_owner: ContractAddress = _owner::read(); From 65d610761796277f71df2fbae9d1bebe662b60ea Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 31 May 2023 13:34:27 -0400 Subject: [PATCH 02/42] add camel tests --- src/openzeppelin/tests/test_ownable.cairo | 72 +++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/src/openzeppelin/tests/test_ownable.cairo b/src/openzeppelin/tests/test_ownable.cairo index 424cc0546..cbd1161db 100644 --- a/src/openzeppelin/tests/test_ownable.cairo +++ b/src/openzeppelin/tests/test_ownable.cairo @@ -22,6 +22,10 @@ fn setup() { Ownable::initializer(); } +/// +/// initializer +/// + #[test] #[available_gas(2000000)] fn test_initializer() { @@ -30,6 +34,10 @@ fn test_initializer() { assert(Ownable::owner() == OWNER(), 'Owner should be set'); } +/// +/// transfer_ownership & transferOwnership +/// + #[test] #[available_gas(2000000)] fn test_transfer_ownership() { @@ -63,6 +71,43 @@ fn test_transfer_ownership_from_nonowner() { Ownable::transfer_ownership(OTHER()); } +#[test] +#[available_gas(2000000)] +fn test_transferOwnership() { + setup(); + Ownable::transferOwnership(OTHER()); + assert(Ownable::owner() == OTHER(), 'Should transfer ownership'); +} + +#[test] +#[available_gas(2000000)] +#[should_panic(expected: ('New owner is the zero address', ))] +fn test_transferOwnership_to_zero() { + setup(); + Ownable::transferOwnership(ZERO()); +} + +#[test] +#[available_gas(2000000)] +#[should_panic(expected: ('Caller is the zero address', ))] +fn test_transferOwnership_from_zero() { + assert(Ownable::owner() == ZERO(), 'Should be zero with no owner'); + Ownable::transferOwnership(OTHER()); +} + +#[test] +#[available_gas(2000000)] +#[should_panic(expected: ('Caller is not the owner', ))] +fn test_transferOwnership_from_nonowner() { + setup(); + testing::set_caller_address(OTHER()); + Ownable::transferOwnership(OTHER()); +} + +/// +/// renounce_ownership & renounceOwnership +/// + #[test] #[available_gas(2000000)] fn test_renounce_ownership() { @@ -88,3 +133,30 @@ fn test_renounce_ownership_from_nonowner() { testing::set_caller_address(OTHER()); Ownable::renounce_ownership(); } + +#[test] +#[available_gas(2000000)] +fn test_renounceOwnership() { + setup(); + Ownable::renounceOwnership(); + assert(Ownable::owner() == ZERO(), 'Should renounce ownership'); +} + +#[test] +#[available_gas(2000000)] +#[should_panic(expected: ('Caller is the zero address', ))] +fn test_renounceOwnership_from_zero_address() { + setup(); + testing::set_caller_address(ZERO()); + Ownable::renounceOwnership(); +} + +#[test] +#[available_gas(2000000)] +#[should_panic(expected: ('Caller is not the owner', ))] +fn test_renounceOwnership_from_nonowner() { + setup(); + testing::set_caller_address(OTHER()); + Ownable::renounceOwnership(); +} + From e57a6e26bb54ddecb6388c0ba05621ac7b6b97d4 Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 16 Jun 2023 23:05:56 -0400 Subject: [PATCH 03/42] nest ownable into dir --- src/openzeppelin/access/ownable.cairo | 118 +----------------- src/openzeppelin/access/ownable/ownable.cairo | 93 ++++++++++++++ 2 files changed, 97 insertions(+), 114 deletions(-) create mode 100644 src/openzeppelin/access/ownable/ownable.cairo diff --git a/src/openzeppelin/access/ownable.cairo b/src/openzeppelin/access/ownable.cairo index c91b76766..7b8e812b9 100644 --- a/src/openzeppelin/access/ownable.cairo +++ b/src/openzeppelin/access/ownable.cairo @@ -1,115 +1,5 @@ -use starknet::ContractAddress; +mod ownable; +use ownable::Ownable; -#[abi] -trait IOwnable { - #[view] - fn owner() -> ContractAddress; - #[external] - fn transfer_ownership(new_owner: ContractAddress); - #[external] - fn renounce_ownership(); -} - -#[abi] -trait IOwnableCamel { - #[view] - fn owner() -> ContractAddress; - #[external] - fn transferOwnership(newOwner: ContractAddress); - #[external] - fn renounceOwnership(); -} - -#[contract] -mod Ownable { - use super::{IOwnable, IOwnableCamel}; - use starknet::ContractAddress; - use starknet::get_caller_address; - use zeroable::Zeroable; - - struct Storage { - _owner: ContractAddress - } - - #[event] - fn OwnershipTransferred(previous_owner: ContractAddress, new_owner: ContractAddress) {} - - impl OwnableImpl of IOwnable { - fn owner() -> ContractAddress { - _owner::read() - } - - fn transfer_ownership(new_owner: ContractAddress) { - assert(!new_owner.is_zero(), 'New owner is the zero address'); - assert_only_owner(); - _transfer_ownership(new_owner); - } - - fn renounce_ownership() { - assert_only_owner(); - _transfer_ownership(Zeroable::zero()); - } - } - - impl OwnableCamelImpl of IOwnableCamel { - fn owner() -> ContractAddress { - OwnableImpl::owner() - } - - fn transferOwnership(newOwner: ContractAddress) { - OwnableImpl::transfer_ownership(newOwner); - } - - fn renounceOwnership() { - OwnableImpl::renounce_ownership(); - } - } - - #[view] - fn owner() -> ContractAddress { - OwnableImpl::owner() - } - - #[external] - fn transfer_ownership(new_owner: ContractAddress) { - OwnableImpl::transfer_ownership(new_owner); - } - - #[external] - fn transferOwnership(newOwner: ContractAddress) { - OwnableCamelImpl::transferOwnership(newOwner); - } - - #[external] - fn renounce_ownership() { - OwnableImpl::renounce_ownership(); - } - - #[external] - fn renounceOwnership() { - OwnableCamelImpl::renounceOwnership(); - } - - // Internals - - #[internal] - fn initializer() { - let caller: ContractAddress = get_caller_address(); - _transfer_ownership(caller); - } - - #[internal] - fn assert_only_owner() { - let owner: ContractAddress = _owner::read(); - let caller: ContractAddress = get_caller_address(); - assert(!caller.is_zero(), 'Caller is the zero address'); - assert(caller == owner, 'Caller is not the owner'); - } - - #[internal] - fn _transfer_ownership(new_owner: ContractAddress) { - let previous_owner: ContractAddress = _owner::read(); - _owner::write(new_owner); - OwnershipTransferred(previous_owner, new_owner); - } -} +mod interface; +mod dual_ownable; diff --git a/src/openzeppelin/access/ownable/ownable.cairo b/src/openzeppelin/access/ownable/ownable.cairo new file mode 100644 index 000000000..1027d4ca7 --- /dev/null +++ b/src/openzeppelin/access/ownable/ownable.cairo @@ -0,0 +1,93 @@ +#[contract] +mod Ownable { + use openzeppelin::access::ownable::interface; + use starknet::ContractAddress; + use starknet::get_caller_address; + use zeroable::Zeroable; + + struct Storage { + _owner: ContractAddress + } + + #[event] + fn OwnershipTransferred(previous_owner: ContractAddress, new_owner: ContractAddress) {} + + impl OwnableImpl of interface::IOwnable { + fn owner() -> ContractAddress { + _owner::read() + } + + fn transfer_ownership(new_owner: ContractAddress) { + assert(!new_owner.is_zero(), 'New owner is the zero address'); + assert_only_owner(); + _transfer_ownership(new_owner); + } + + fn renounce_ownership() { + assert_only_owner(); + _transfer_ownership(Zeroable::zero()); + } + } + + impl OwnableCamelImpl of interface::IOwnableCamel { + fn owner() -> ContractAddress { + OwnableImpl::owner() + } + + fn transferOwnership(newOwner: ContractAddress) { + OwnableImpl::transfer_ownership(newOwner); + } + + fn renounceOwnership() { + OwnableImpl::renounce_ownership(); + } + } + + #[view] + fn owner() -> ContractAddress { + OwnableImpl::owner() + } + + #[external] + fn transfer_ownership(new_owner: ContractAddress) { + OwnableImpl::transfer_ownership(new_owner); + } + + #[external] + fn transferOwnership(newOwner: ContractAddress) { + OwnableCamelImpl::transferOwnership(newOwner); + } + + #[external] + fn renounce_ownership() { + OwnableImpl::renounce_ownership(); + } + + #[external] + fn renounceOwnership() { + OwnableCamelImpl::renounceOwnership(); + } + + // Internals + + #[internal] + fn initializer() { + let caller: ContractAddress = get_caller_address(); + _transfer_ownership(caller); + } + + #[internal] + fn assert_only_owner() { + let owner: ContractAddress = _owner::read(); + let caller: ContractAddress = get_caller_address(); + assert(!caller.is_zero(), 'Caller is the zero address'); + assert(caller == owner, 'Caller is not the owner'); + } + + #[internal] + fn _transfer_ownership(new_owner: ContractAddress) { + let previous_owner: ContractAddress = _owner::read(); + _owner::write(new_owner); + OwnershipTransferred(previous_owner, new_owner); + } +} From 28b62e76053aa75581a1d2baf9b06d43c2989539 Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 16 Jun 2023 23:06:20 -0400 Subject: [PATCH 04/42] move interface --- .../access/ownable/interface.cairo | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 src/openzeppelin/access/ownable/interface.cairo diff --git a/src/openzeppelin/access/ownable/interface.cairo b/src/openzeppelin/access/ownable/interface.cairo new file mode 100644 index 000000000..9570e5437 --- /dev/null +++ b/src/openzeppelin/access/ownable/interface.cairo @@ -0,0 +1,21 @@ +use starknet::ContractAddress; + +#[abi] +trait IOwnable { + #[view] + fn owner() -> ContractAddress; + #[external] + fn transfer_ownership(new_owner: ContractAddress); + #[external] + fn renounce_ownership(); +} + +#[abi] +trait IOwnableCamel { + #[view] + fn owner() -> ContractAddress; + #[external] + fn transferOwnership(newOwner: ContractAddress); + #[external] + fn renounceOwnership(); +} \ No newline at end of file From e65f141c3648364d38f4b87e84554b5600262e8a Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 16 Jun 2023 23:06:47 -0400 Subject: [PATCH 05/42] add dual ownable --- .../access/ownable/dual_ownable.cairo | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/openzeppelin/access/ownable/dual_ownable.cairo diff --git a/src/openzeppelin/access/ownable/dual_ownable.cairo b/src/openzeppelin/access/ownable/dual_ownable.cairo new file mode 100644 index 000000000..2d7d1b547 --- /dev/null +++ b/src/openzeppelin/access/ownable/dual_ownable.cairo @@ -0,0 +1,62 @@ +use array::SpanTrait; +use array::ArrayTrait; +use core::result::ResultTrait; +use option::OptionTrait; +use starknet::ContractAddress; +use starknet::Felt252TryIntoContractAddress; +use starknet::SyscallResultTrait; +use starknet::call_contract_syscall; +use traits::Into; +use traits::TryInto; + +use openzeppelin::utils::try_selector_with_fallback; +use openzeppelin::utils::Felt252TryIntoBool; +use openzeppelin::utils::constants; + +#[derive(Copy, Drop)] +struct DualCaseOwnable { + contract_address: ContractAddress +} + +trait DualCaseOwnableTrait { + fn owner(self: @DualCaseOwnable) -> ContractAddress; + fn transfer_ownership(self: @DualCaseOwnable, new_owner: ContractAddress); + fn renounce_ownership(self: @DualCaseOwnable); +} + +impl DualCaseOwnableImpl of DualCaseOwnableTrait { + fn owner(self: @DualCaseOwnable) -> ContractAddress { + (*call_contract_syscall( + *self.contract_address, constants::OWNER_SELECTOR, ArrayTrait::new().span() + ) + .unwrap_syscall() + .at(0)) + .try_into() + .unwrap() + } + + fn transfer_ownership(self: @DualCaseOwnable, new_owner: ContractAddress) { + let snake_selector = constants::TRANSFER_OWNERSHIP_SELECTOR; + let camel_selector = constants::TRANSFEROWNERSHIP_SELECTOR; + + let mut args = ArrayTrait::new(); + args.append(new_owner.into()); + + try_selector_with_fallback( + *self.contract_address, snake_selector, camel_selector, args.span() + ) + .unwrap_syscall(); + } + + fn renounce_ownership(self: @DualCaseOwnable) { + let snake_selector = constants::RENOUNCE_OWNERSHIP_SELECTOR; + let camel_selector = constants::RENOUNCEOWNERSHIP_SELECTOR; + + let mut args = ArrayTrait::new(); + + try_selector_with_fallback( + *self.contract_address, snake_selector, camel_selector, args.span() + ) + .unwrap_syscall(); + } +} From cdff9821736e8c6bcb84a8b1af99e067520a7fb6 Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 16 Jun 2023 23:07:13 -0400 Subject: [PATCH 06/42] add const selectors --- src/openzeppelin/utils/constants.cairo | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/openzeppelin/utils/constants.cairo b/src/openzeppelin/utils/constants.cairo index d7346bf55..a13dd646f 100644 --- a/src/openzeppelin/utils/constants.cairo +++ b/src/openzeppelin/utils/constants.cairo @@ -36,3 +36,13 @@ const IACCESSCONTROL_ID: u32 = 0x7965db0b_u32; // const DEFAULT_ADMIN_ROLE: felt252 = 0; + +// +// Selectors +// + +const OWNER_SELECTOR: felt252 = 0x2016836a56b71f0d02689e69e326f4f4c1b9057164ef592671cf0d37c8040c0; +const TRANSFER_OWNERSHIP_SELECTOR: felt252 = 0x2a3bb1eaa05b77c4b0eeee0116a3177c6d62319dd7149ae148185d9e09de74a; +const TRANSFEROWNERSHIP_SELECTOR: felt252 = 0x14a390f291e2e1f29874769efdef47ddad94d76f77ff516fad206a385e8995f; +const RENOUNCE_OWNERSHIP_SELECTOR: felt252 = 0x52580a92c73f4428f1a260c5d768ef462b25955307de00f99957df119865d; +const RENOUNCEOWNERSHIP_SELECTOR: felt252 = 0xd5d33d590e6660853069b37a2aea67c6fdaa0268626bc760350b590490feb5; From 96645d809024bd32af09d31cc037c565866902c3 Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 16 Jun 2023 23:08:02 -0400 Subject: [PATCH 07/42] add try_selector_with_fallback and bool impl --- src/openzeppelin/utils.cairo | 42 +++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/src/openzeppelin/utils.cairo b/src/openzeppelin/utils.cairo index 8dab269b1..bb2473af0 100644 --- a/src/openzeppelin/utils.cairo +++ b/src/openzeppelin/utils.cairo @@ -1,10 +1,50 @@ +use starknet::call_contract_syscall; +use starknet::ContractAddress; +use starknet::SyscallResult; +use option::OptionTrait; use array::ArrayTrait; use array::SpanTrait; use box::BoxTrait; -use option::OptionTrait; mod constants; mod serde; +fn try_selector_with_fallback( + target: ContractAddress, snake_selector: felt252, camel_selector: felt252, args: Span +) -> SyscallResult> { + match call_contract_syscall(target, snake_selector, args) { + Result::Ok(r) => Result::Ok(r), + Result::Err(r) => { + if *r.at(0) == 'ENTRYPOINT_NOT_FOUND' { + return call_contract_syscall(target, camel_selector, args); + } else { + Result::Err(r) + } + } + } +} + +impl BoolIntoFelt252 of Into { + fn into(self: bool) -> felt252 { + if self { + return 1; + } else { + return 0; + } + } +} + +impl Felt252TryIntoBool of TryInto { + fn try_into(self: felt252) -> Option { + if self == 0 { + Option::Some(false) + } else if self == 1 { + Option::Some(true) + } else { + Option::None(()) + } + } +} + #[inline(always)] fn check_gas() { match gas::withdraw_gas() { From 6b5bd775157268773f7347c1f19f86a776a6906e Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 16 Jun 2023 23:09:23 -0400 Subject: [PATCH 08/42] nest access tests in test dirs --- .../tests/test_accesscontrol.cairo | 291 ------------------ src/openzeppelin/tests/test_ownable.cairo | 162 ---------- 2 files changed, 453 deletions(-) delete mode 100644 src/openzeppelin/tests/test_accesscontrol.cairo delete mode 100644 src/openzeppelin/tests/test_ownable.cairo diff --git a/src/openzeppelin/tests/test_accesscontrol.cairo b/src/openzeppelin/tests/test_accesscontrol.cairo deleted file mode 100644 index 16b0a41fa..000000000 --- a/src/openzeppelin/tests/test_accesscontrol.cairo +++ /dev/null @@ -1,291 +0,0 @@ -use openzeppelin::access::accesscontrol::AccessControl; -use openzeppelin::access::accesscontrol::DEFAULT_ADMIN_ROLE; -use openzeppelin::access::accesscontrol::IACCESSCONTROL_ID; -use starknet::contract_address_const; -use starknet::ContractAddress; -use starknet::testing; - -const ROLE: felt252 = 41; -const OTHER_ROLE: felt252 = 42; - -fn ADMIN() -> ContractAddress { - contract_address_const::<1>() -} - -fn AUTHORIZED() -> ContractAddress { - contract_address_const::<2>() -} - -fn OTHER() -> ContractAddress { - contract_address_const::<3>() -} - -fn OTHER_ADMIN() -> ContractAddress { - contract_address_const::<4>() -} - -fn setup() { - AccessControl::_grant_role(DEFAULT_ADMIN_ROLE, ADMIN()); - testing::set_caller_address(ADMIN()); -} - -// -// initializer -// - -#[test] -#[available_gas(2000000)] -fn test_initializer() { - AccessControl::initializer(); - assert(AccessControl::supports_interface(IACCESSCONTROL_ID), 'Should support own interface'); -} - -// -// has_role -// - -#[test] -#[available_gas(2000000)] -fn test_has_role() { - setup(); - assert(!AccessControl::has_role(ROLE, AUTHORIZED()), 'should not have role'); - AccessControl::_grant_role(ROLE, AUTHORIZED()); - assert(AccessControl::has_role(ROLE, AUTHORIZED()), 'should have role'); -} - - -// -// assert_only_role -// - -#[test] -#[available_gas(2000000)] -fn test_assert_only_role() { - setup(); - AccessControl::grant_role(ROLE, AUTHORIZED()); - testing::set_caller_address(AUTHORIZED()); - AccessControl::assert_only_role(ROLE); -} - -#[test] -#[available_gas(2000000)] -#[should_panic(expected: ('Caller is missing role', ))] -fn test_assert_only_role_unauthorized() { - setup(); - testing::set_caller_address(OTHER()); - AccessControl::assert_only_role(ROLE); -} - -#[test] -#[available_gas(2000000)] -#[should_panic(expected: ('Caller is missing role', ))] -fn test_assert_only_role_unauthorized_when_authorized_for_another_role() { - setup(); - AccessControl::grant_role(ROLE, AUTHORIZED()); - testing::set_caller_address(AUTHORIZED()); - AccessControl::assert_only_role(OTHER_ROLE); -} - -// -// grant_role -// - -#[test] -#[available_gas(2000000)] -fn test_grant_role() { - setup(); - AccessControl::grant_role(ROLE, AUTHORIZED()); - assert(AccessControl::has_role(ROLE, AUTHORIZED()), 'Role should be granted'); -} - -#[test] -#[available_gas(2000000)] -fn test_grant_role_multiple_times_for_granted_role() { - setup(); - AccessControl::grant_role(ROLE, AUTHORIZED()); - AccessControl::grant_role(ROLE, AUTHORIZED()); - assert(AccessControl::has_role(ROLE, AUTHORIZED()), 'Role should still be granted'); -} - -#[test] -#[available_gas(2000000)] -#[should_panic(expected: ('Caller is missing role', ))] -fn test_grant_role_unauthorized() { - setup(); - testing::set_caller_address(AUTHORIZED()); - AccessControl::grant_role(ROLE, AUTHORIZED()); -} - -// -// revoke_role -// - -#[test] -#[available_gas(2000000)] -fn test_revoke_role_for_role_not_granted() { - setup(); - AccessControl::revoke_role(ROLE, AUTHORIZED()); -} - -#[test] -#[available_gas(2000000)] -fn test_revoke_role_for_granted_role() { - setup(); - AccessControl::grant_role(ROLE, AUTHORIZED()); - AccessControl::revoke_role(ROLE, AUTHORIZED()); - assert(!AccessControl::has_role(ROLE, AUTHORIZED()), 'Role should be revoked'); -} - -#[test] -#[available_gas(2000000)] -fn test_revoke_role_multiple_times_for_granted_role() { - setup(); - AccessControl::grant_role(ROLE, AUTHORIZED()); - - AccessControl::revoke_role(ROLE, AUTHORIZED()); - AccessControl::revoke_role(ROLE, AUTHORIZED()); - assert(!AccessControl::has_role(ROLE, AUTHORIZED()), 'Role should still be revoked'); -} - -#[test] -#[available_gas(2000000)] -#[should_panic(expected: ('Caller is missing role', ))] -fn test_revoke_role_unauthorized() { - setup(); - - testing::set_caller_address(OTHER()); - AccessControl::revoke_role(ROLE, AUTHORIZED()); -} - -// -// renounce_role -// - -#[test] -#[available_gas(2000000)] -fn test_renounce_role_for_role_not_granted() { - setup(); - testing::set_caller_address(AUTHORIZED()); - - AccessControl::renounce_role(ROLE, AUTHORIZED()); -} - -#[test] -#[available_gas(2000000)] -fn test_renounce_role_for_granted_role() { - setup(); - AccessControl::grant_role(ROLE, AUTHORIZED()); - testing::set_caller_address(AUTHORIZED()); - - AccessControl::renounce_role(ROLE, AUTHORIZED()); - assert(!AccessControl::has_role(ROLE, AUTHORIZED()), 'Role should be renounced'); -} - -#[test] -#[available_gas(2000000)] -fn test_renounce_role_multiple_times_for_granted_role() { - setup(); - AccessControl::grant_role(ROLE, AUTHORIZED()); - testing::set_caller_address(AUTHORIZED()); - - AccessControl::renounce_role(ROLE, AUTHORIZED()); - AccessControl::renounce_role(ROLE, AUTHORIZED()); - assert(!AccessControl::has_role(ROLE, AUTHORIZED()), 'Role should still be renounced'); -} - -#[test] -#[available_gas(2000000)] -#[should_panic(expected: ('Can only renounce role for self', ))] -fn test_renounce_role_unauthorized() { - setup(); - AccessControl::grant_role(ROLE, AUTHORIZED()); - - // Admin is unauthorized caller - AccessControl::renounce_role(ROLE, AUTHORIZED()); -} - -// -// _set_role_admin -// - -#[test] -#[available_gas(2000000)] -fn test__set_role_admin() { - setup(); - assert( - AccessControl::get_role_admin(ROLE) == DEFAULT_ADMIN_ROLE, 'ROLE admin default should be 0' - ); - AccessControl::_set_role_admin(ROLE, OTHER_ROLE); - assert(AccessControl::get_role_admin(ROLE) == OTHER_ROLE, 'ROLE admin should be OTHER_ROLE'); -} - -#[test] -#[available_gas(2000000)] -fn test_new_admin_can_grant_roles() { - setup(); - AccessControl::_set_role_admin(ROLE, OTHER_ROLE); - AccessControl::grant_role(OTHER_ROLE, OTHER_ADMIN()); - - testing::set_caller_address(OTHER_ADMIN()); - AccessControl::grant_role(ROLE, AUTHORIZED()); - - assert(AccessControl::has_role(ROLE, AUTHORIZED()), 'AUTHORIZED should have ROLE'); -} - -#[test] -#[available_gas(2000000)] -fn test_new_admin_can_revoke_roles() { - setup(); - AccessControl::_set_role_admin(ROLE, OTHER_ROLE); - AccessControl::grant_role(OTHER_ROLE, OTHER_ADMIN()); - - testing::set_caller_address(OTHER_ADMIN()); - AccessControl::grant_role(ROLE, AUTHORIZED()); - AccessControl::revoke_role(ROLE, AUTHORIZED()); - - assert(!AccessControl::has_role(ROLE, AUTHORIZED()), 'AUTHORIZED should not have ROLE'); -} - -#[test] -#[available_gas(2000000)] -#[should_panic(expected: ('Caller is missing role', ))] -fn test_previous_admin_cannot_grant_roles() { - setup(); - AccessControl::_set_role_admin(ROLE, OTHER_ROLE); - - // Caller is ADMIN - AccessControl::grant_role(ROLE, AUTHORIZED()); -} - -#[test] -#[available_gas(2000000)] -#[should_panic(expected: ('Caller is missing role', ))] -fn test_previous_admin_cannot_revoke_roles() { - setup(); - AccessControl::_set_role_admin(ROLE, OTHER_ROLE); - - // Caller is ADMIN - AccessControl::revoke_role(ROLE, AUTHORIZED()); -} - -// -// default admin -// - -#[test] -#[available_gas(2000000)] -fn test_other_role_admin_is_the_default_admin_role() { - assert( - AccessControl::get_role_admin(OTHER_ROLE) == DEFAULT_ADMIN_ROLE, - 'Should be DEFAULT_ADMIN_ROLE' - ); -} - -#[test] -#[available_gas(2000000)] -fn test_default_admin_role_is_its_own_admin() { - assert( - AccessControl::get_role_admin(DEFAULT_ADMIN_ROLE) == DEFAULT_ADMIN_ROLE, - 'Should be DEFAULT_ADMIN_ROLE' - ); -} diff --git a/src/openzeppelin/tests/test_ownable.cairo b/src/openzeppelin/tests/test_ownable.cairo deleted file mode 100644 index cbd1161db..000000000 --- a/src/openzeppelin/tests/test_ownable.cairo +++ /dev/null @@ -1,162 +0,0 @@ -use openzeppelin::access::ownable::Ownable; - -use starknet::ContractAddress; -use starknet::contract_address_const; -use starknet::testing; -use zeroable::Zeroable; - -fn ZERO() -> ContractAddress { - contract_address_const::<0>() -} - -fn OWNER() -> ContractAddress { - contract_address_const::<1>() -} - -fn OTHER() -> ContractAddress { - contract_address_const::<2>() -} - -fn setup() { - testing::set_caller_address(OWNER()); - Ownable::initializer(); -} - -/// -/// initializer -/// - -#[test] -#[available_gas(2000000)] -fn test_initializer() { - assert(Ownable::owner().is_zero(), 'Should be zero'); - setup(); - assert(Ownable::owner() == OWNER(), 'Owner should be set'); -} - -/// -/// transfer_ownership & transferOwnership -/// - -#[test] -#[available_gas(2000000)] -fn test_transfer_ownership() { - setup(); - Ownable::transfer_ownership(OTHER()); - assert(Ownable::owner() == OTHER(), 'Should transfer ownership'); -} - -#[test] -#[available_gas(2000000)] -#[should_panic(expected: ('New owner is the zero address', ))] -fn test_transfer_ownership_to_zero() { - setup(); - Ownable::transfer_ownership(ZERO()); -} - -#[test] -#[available_gas(2000000)] -#[should_panic(expected: ('Caller is the zero address', ))] -fn test_transfer_ownership_from_zero() { - assert(Ownable::owner() == ZERO(), 'Should be zero with no owner'); - Ownable::transfer_ownership(OTHER()); -} - -#[test] -#[available_gas(2000000)] -#[should_panic(expected: ('Caller is not the owner', ))] -fn test_transfer_ownership_from_nonowner() { - setup(); - testing::set_caller_address(OTHER()); - Ownable::transfer_ownership(OTHER()); -} - -#[test] -#[available_gas(2000000)] -fn test_transferOwnership() { - setup(); - Ownable::transferOwnership(OTHER()); - assert(Ownable::owner() == OTHER(), 'Should transfer ownership'); -} - -#[test] -#[available_gas(2000000)] -#[should_panic(expected: ('New owner is the zero address', ))] -fn test_transferOwnership_to_zero() { - setup(); - Ownable::transferOwnership(ZERO()); -} - -#[test] -#[available_gas(2000000)] -#[should_panic(expected: ('Caller is the zero address', ))] -fn test_transferOwnership_from_zero() { - assert(Ownable::owner() == ZERO(), 'Should be zero with no owner'); - Ownable::transferOwnership(OTHER()); -} - -#[test] -#[available_gas(2000000)] -#[should_panic(expected: ('Caller is not the owner', ))] -fn test_transferOwnership_from_nonowner() { - setup(); - testing::set_caller_address(OTHER()); - Ownable::transferOwnership(OTHER()); -} - -/// -/// renounce_ownership & renounceOwnership -/// - -#[test] -#[available_gas(2000000)] -fn test_renounce_ownership() { - setup(); - Ownable::renounce_ownership(); - assert(Ownable::owner() == ZERO(), 'Should renounce ownership'); -} - -#[test] -#[available_gas(2000000)] -#[should_panic(expected: ('Caller is the zero address', ))] -fn test_renounce_ownership_from_zero_address() { - setup(); - testing::set_caller_address(ZERO()); - Ownable::renounce_ownership(); -} - -#[test] -#[available_gas(2000000)] -#[should_panic(expected: ('Caller is not the owner', ))] -fn test_renounce_ownership_from_nonowner() { - setup(); - testing::set_caller_address(OTHER()); - Ownable::renounce_ownership(); -} - -#[test] -#[available_gas(2000000)] -fn test_renounceOwnership() { - setup(); - Ownable::renounceOwnership(); - assert(Ownable::owner() == ZERO(), 'Should renounce ownership'); -} - -#[test] -#[available_gas(2000000)] -#[should_panic(expected: ('Caller is the zero address', ))] -fn test_renounceOwnership_from_zero_address() { - setup(); - testing::set_caller_address(ZERO()); - Ownable::renounceOwnership(); -} - -#[test] -#[available_gas(2000000)] -#[should_panic(expected: ('Caller is not the owner', ))] -fn test_renounceOwnership_from_nonowner() { - setup(); - testing::set_caller_address(OTHER()); - Ownable::renounceOwnership(); -} - From 693be42a96a1b98ced38b4d9b7d5643c291f1176 Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 16 Jun 2023 23:11:10 -0400 Subject: [PATCH 09/42] move accesscontrol and ownable test mods --- .../accesscontrol/test_accesscontrol.cairo | 291 ++++++++++++++++++ .../tests/access/ownable/test_ownable.cairo | 162 ++++++++++ 2 files changed, 453 insertions(+) create mode 100644 src/openzeppelin/tests/access/accesscontrol/test_accesscontrol.cairo create mode 100644 src/openzeppelin/tests/access/ownable/test_ownable.cairo diff --git a/src/openzeppelin/tests/access/accesscontrol/test_accesscontrol.cairo b/src/openzeppelin/tests/access/accesscontrol/test_accesscontrol.cairo new file mode 100644 index 000000000..16b0a41fa --- /dev/null +++ b/src/openzeppelin/tests/access/accesscontrol/test_accesscontrol.cairo @@ -0,0 +1,291 @@ +use openzeppelin::access::accesscontrol::AccessControl; +use openzeppelin::access::accesscontrol::DEFAULT_ADMIN_ROLE; +use openzeppelin::access::accesscontrol::IACCESSCONTROL_ID; +use starknet::contract_address_const; +use starknet::ContractAddress; +use starknet::testing; + +const ROLE: felt252 = 41; +const OTHER_ROLE: felt252 = 42; + +fn ADMIN() -> ContractAddress { + contract_address_const::<1>() +} + +fn AUTHORIZED() -> ContractAddress { + contract_address_const::<2>() +} + +fn OTHER() -> ContractAddress { + contract_address_const::<3>() +} + +fn OTHER_ADMIN() -> ContractAddress { + contract_address_const::<4>() +} + +fn setup() { + AccessControl::_grant_role(DEFAULT_ADMIN_ROLE, ADMIN()); + testing::set_caller_address(ADMIN()); +} + +// +// initializer +// + +#[test] +#[available_gas(2000000)] +fn test_initializer() { + AccessControl::initializer(); + assert(AccessControl::supports_interface(IACCESSCONTROL_ID), 'Should support own interface'); +} + +// +// has_role +// + +#[test] +#[available_gas(2000000)] +fn test_has_role() { + setup(); + assert(!AccessControl::has_role(ROLE, AUTHORIZED()), 'should not have role'); + AccessControl::_grant_role(ROLE, AUTHORIZED()); + assert(AccessControl::has_role(ROLE, AUTHORIZED()), 'should have role'); +} + + +// +// assert_only_role +// + +#[test] +#[available_gas(2000000)] +fn test_assert_only_role() { + setup(); + AccessControl::grant_role(ROLE, AUTHORIZED()); + testing::set_caller_address(AUTHORIZED()); + AccessControl::assert_only_role(ROLE); +} + +#[test] +#[available_gas(2000000)] +#[should_panic(expected: ('Caller is missing role', ))] +fn test_assert_only_role_unauthorized() { + setup(); + testing::set_caller_address(OTHER()); + AccessControl::assert_only_role(ROLE); +} + +#[test] +#[available_gas(2000000)] +#[should_panic(expected: ('Caller is missing role', ))] +fn test_assert_only_role_unauthorized_when_authorized_for_another_role() { + setup(); + AccessControl::grant_role(ROLE, AUTHORIZED()); + testing::set_caller_address(AUTHORIZED()); + AccessControl::assert_only_role(OTHER_ROLE); +} + +// +// grant_role +// + +#[test] +#[available_gas(2000000)] +fn test_grant_role() { + setup(); + AccessControl::grant_role(ROLE, AUTHORIZED()); + assert(AccessControl::has_role(ROLE, AUTHORIZED()), 'Role should be granted'); +} + +#[test] +#[available_gas(2000000)] +fn test_grant_role_multiple_times_for_granted_role() { + setup(); + AccessControl::grant_role(ROLE, AUTHORIZED()); + AccessControl::grant_role(ROLE, AUTHORIZED()); + assert(AccessControl::has_role(ROLE, AUTHORIZED()), 'Role should still be granted'); +} + +#[test] +#[available_gas(2000000)] +#[should_panic(expected: ('Caller is missing role', ))] +fn test_grant_role_unauthorized() { + setup(); + testing::set_caller_address(AUTHORIZED()); + AccessControl::grant_role(ROLE, AUTHORIZED()); +} + +// +// revoke_role +// + +#[test] +#[available_gas(2000000)] +fn test_revoke_role_for_role_not_granted() { + setup(); + AccessControl::revoke_role(ROLE, AUTHORIZED()); +} + +#[test] +#[available_gas(2000000)] +fn test_revoke_role_for_granted_role() { + setup(); + AccessControl::grant_role(ROLE, AUTHORIZED()); + AccessControl::revoke_role(ROLE, AUTHORIZED()); + assert(!AccessControl::has_role(ROLE, AUTHORIZED()), 'Role should be revoked'); +} + +#[test] +#[available_gas(2000000)] +fn test_revoke_role_multiple_times_for_granted_role() { + setup(); + AccessControl::grant_role(ROLE, AUTHORIZED()); + + AccessControl::revoke_role(ROLE, AUTHORIZED()); + AccessControl::revoke_role(ROLE, AUTHORIZED()); + assert(!AccessControl::has_role(ROLE, AUTHORIZED()), 'Role should still be revoked'); +} + +#[test] +#[available_gas(2000000)] +#[should_panic(expected: ('Caller is missing role', ))] +fn test_revoke_role_unauthorized() { + setup(); + + testing::set_caller_address(OTHER()); + AccessControl::revoke_role(ROLE, AUTHORIZED()); +} + +// +// renounce_role +// + +#[test] +#[available_gas(2000000)] +fn test_renounce_role_for_role_not_granted() { + setup(); + testing::set_caller_address(AUTHORIZED()); + + AccessControl::renounce_role(ROLE, AUTHORIZED()); +} + +#[test] +#[available_gas(2000000)] +fn test_renounce_role_for_granted_role() { + setup(); + AccessControl::grant_role(ROLE, AUTHORIZED()); + testing::set_caller_address(AUTHORIZED()); + + AccessControl::renounce_role(ROLE, AUTHORIZED()); + assert(!AccessControl::has_role(ROLE, AUTHORIZED()), 'Role should be renounced'); +} + +#[test] +#[available_gas(2000000)] +fn test_renounce_role_multiple_times_for_granted_role() { + setup(); + AccessControl::grant_role(ROLE, AUTHORIZED()); + testing::set_caller_address(AUTHORIZED()); + + AccessControl::renounce_role(ROLE, AUTHORIZED()); + AccessControl::renounce_role(ROLE, AUTHORIZED()); + assert(!AccessControl::has_role(ROLE, AUTHORIZED()), 'Role should still be renounced'); +} + +#[test] +#[available_gas(2000000)] +#[should_panic(expected: ('Can only renounce role for self', ))] +fn test_renounce_role_unauthorized() { + setup(); + AccessControl::grant_role(ROLE, AUTHORIZED()); + + // Admin is unauthorized caller + AccessControl::renounce_role(ROLE, AUTHORIZED()); +} + +// +// _set_role_admin +// + +#[test] +#[available_gas(2000000)] +fn test__set_role_admin() { + setup(); + assert( + AccessControl::get_role_admin(ROLE) == DEFAULT_ADMIN_ROLE, 'ROLE admin default should be 0' + ); + AccessControl::_set_role_admin(ROLE, OTHER_ROLE); + assert(AccessControl::get_role_admin(ROLE) == OTHER_ROLE, 'ROLE admin should be OTHER_ROLE'); +} + +#[test] +#[available_gas(2000000)] +fn test_new_admin_can_grant_roles() { + setup(); + AccessControl::_set_role_admin(ROLE, OTHER_ROLE); + AccessControl::grant_role(OTHER_ROLE, OTHER_ADMIN()); + + testing::set_caller_address(OTHER_ADMIN()); + AccessControl::grant_role(ROLE, AUTHORIZED()); + + assert(AccessControl::has_role(ROLE, AUTHORIZED()), 'AUTHORIZED should have ROLE'); +} + +#[test] +#[available_gas(2000000)] +fn test_new_admin_can_revoke_roles() { + setup(); + AccessControl::_set_role_admin(ROLE, OTHER_ROLE); + AccessControl::grant_role(OTHER_ROLE, OTHER_ADMIN()); + + testing::set_caller_address(OTHER_ADMIN()); + AccessControl::grant_role(ROLE, AUTHORIZED()); + AccessControl::revoke_role(ROLE, AUTHORIZED()); + + assert(!AccessControl::has_role(ROLE, AUTHORIZED()), 'AUTHORIZED should not have ROLE'); +} + +#[test] +#[available_gas(2000000)] +#[should_panic(expected: ('Caller is missing role', ))] +fn test_previous_admin_cannot_grant_roles() { + setup(); + AccessControl::_set_role_admin(ROLE, OTHER_ROLE); + + // Caller is ADMIN + AccessControl::grant_role(ROLE, AUTHORIZED()); +} + +#[test] +#[available_gas(2000000)] +#[should_panic(expected: ('Caller is missing role', ))] +fn test_previous_admin_cannot_revoke_roles() { + setup(); + AccessControl::_set_role_admin(ROLE, OTHER_ROLE); + + // Caller is ADMIN + AccessControl::revoke_role(ROLE, AUTHORIZED()); +} + +// +// default admin +// + +#[test] +#[available_gas(2000000)] +fn test_other_role_admin_is_the_default_admin_role() { + assert( + AccessControl::get_role_admin(OTHER_ROLE) == DEFAULT_ADMIN_ROLE, + 'Should be DEFAULT_ADMIN_ROLE' + ); +} + +#[test] +#[available_gas(2000000)] +fn test_default_admin_role_is_its_own_admin() { + assert( + AccessControl::get_role_admin(DEFAULT_ADMIN_ROLE) == DEFAULT_ADMIN_ROLE, + 'Should be DEFAULT_ADMIN_ROLE' + ); +} diff --git a/src/openzeppelin/tests/access/ownable/test_ownable.cairo b/src/openzeppelin/tests/access/ownable/test_ownable.cairo new file mode 100644 index 000000000..cbd1161db --- /dev/null +++ b/src/openzeppelin/tests/access/ownable/test_ownable.cairo @@ -0,0 +1,162 @@ +use openzeppelin::access::ownable::Ownable; + +use starknet::ContractAddress; +use starknet::contract_address_const; +use starknet::testing; +use zeroable::Zeroable; + +fn ZERO() -> ContractAddress { + contract_address_const::<0>() +} + +fn OWNER() -> ContractAddress { + contract_address_const::<1>() +} + +fn OTHER() -> ContractAddress { + contract_address_const::<2>() +} + +fn setup() { + testing::set_caller_address(OWNER()); + Ownable::initializer(); +} + +/// +/// initializer +/// + +#[test] +#[available_gas(2000000)] +fn test_initializer() { + assert(Ownable::owner().is_zero(), 'Should be zero'); + setup(); + assert(Ownable::owner() == OWNER(), 'Owner should be set'); +} + +/// +/// transfer_ownership & transferOwnership +/// + +#[test] +#[available_gas(2000000)] +fn test_transfer_ownership() { + setup(); + Ownable::transfer_ownership(OTHER()); + assert(Ownable::owner() == OTHER(), 'Should transfer ownership'); +} + +#[test] +#[available_gas(2000000)] +#[should_panic(expected: ('New owner is the zero address', ))] +fn test_transfer_ownership_to_zero() { + setup(); + Ownable::transfer_ownership(ZERO()); +} + +#[test] +#[available_gas(2000000)] +#[should_panic(expected: ('Caller is the zero address', ))] +fn test_transfer_ownership_from_zero() { + assert(Ownable::owner() == ZERO(), 'Should be zero with no owner'); + Ownable::transfer_ownership(OTHER()); +} + +#[test] +#[available_gas(2000000)] +#[should_panic(expected: ('Caller is not the owner', ))] +fn test_transfer_ownership_from_nonowner() { + setup(); + testing::set_caller_address(OTHER()); + Ownable::transfer_ownership(OTHER()); +} + +#[test] +#[available_gas(2000000)] +fn test_transferOwnership() { + setup(); + Ownable::transferOwnership(OTHER()); + assert(Ownable::owner() == OTHER(), 'Should transfer ownership'); +} + +#[test] +#[available_gas(2000000)] +#[should_panic(expected: ('New owner is the zero address', ))] +fn test_transferOwnership_to_zero() { + setup(); + Ownable::transferOwnership(ZERO()); +} + +#[test] +#[available_gas(2000000)] +#[should_panic(expected: ('Caller is the zero address', ))] +fn test_transferOwnership_from_zero() { + assert(Ownable::owner() == ZERO(), 'Should be zero with no owner'); + Ownable::transferOwnership(OTHER()); +} + +#[test] +#[available_gas(2000000)] +#[should_panic(expected: ('Caller is not the owner', ))] +fn test_transferOwnership_from_nonowner() { + setup(); + testing::set_caller_address(OTHER()); + Ownable::transferOwnership(OTHER()); +} + +/// +/// renounce_ownership & renounceOwnership +/// + +#[test] +#[available_gas(2000000)] +fn test_renounce_ownership() { + setup(); + Ownable::renounce_ownership(); + assert(Ownable::owner() == ZERO(), 'Should renounce ownership'); +} + +#[test] +#[available_gas(2000000)] +#[should_panic(expected: ('Caller is the zero address', ))] +fn test_renounce_ownership_from_zero_address() { + setup(); + testing::set_caller_address(ZERO()); + Ownable::renounce_ownership(); +} + +#[test] +#[available_gas(2000000)] +#[should_panic(expected: ('Caller is not the owner', ))] +fn test_renounce_ownership_from_nonowner() { + setup(); + testing::set_caller_address(OTHER()); + Ownable::renounce_ownership(); +} + +#[test] +#[available_gas(2000000)] +fn test_renounceOwnership() { + setup(); + Ownable::renounceOwnership(); + assert(Ownable::owner() == ZERO(), 'Should renounce ownership'); +} + +#[test] +#[available_gas(2000000)] +#[should_panic(expected: ('Caller is the zero address', ))] +fn test_renounceOwnership_from_zero_address() { + setup(); + testing::set_caller_address(ZERO()); + Ownable::renounceOwnership(); +} + +#[test] +#[available_gas(2000000)] +#[should_panic(expected: ('Caller is not the owner', ))] +fn test_renounceOwnership_from_nonowner() { + setup(); + testing::set_caller_address(OTHER()); + Ownable::renounceOwnership(); +} + From 7c730da700f7d72780e13302b6f438429efbe5ac Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 16 Jun 2023 23:12:10 -0400 Subject: [PATCH 10/42] move tests --- src/openzeppelin/tests/access.cairo | 2 ++ src/openzeppelin/tests/access/accesscontrol.cairo | 1 + src/openzeppelin/tests/access/ownable.cairo | 2 ++ 3 files changed, 5 insertions(+) create mode 100644 src/openzeppelin/tests/access.cairo create mode 100644 src/openzeppelin/tests/access/accesscontrol.cairo create mode 100644 src/openzeppelin/tests/access/ownable.cairo diff --git a/src/openzeppelin/tests/access.cairo b/src/openzeppelin/tests/access.cairo new file mode 100644 index 000000000..8f59b4ad1 --- /dev/null +++ b/src/openzeppelin/tests/access.cairo @@ -0,0 +1,2 @@ +mod accesscontrol; +mod ownable; diff --git a/src/openzeppelin/tests/access/accesscontrol.cairo b/src/openzeppelin/tests/access/accesscontrol.cairo new file mode 100644 index 000000000..5faefcd75 --- /dev/null +++ b/src/openzeppelin/tests/access/accesscontrol.cairo @@ -0,0 +1 @@ +mod test_accesscontrol; diff --git a/src/openzeppelin/tests/access/ownable.cairo b/src/openzeppelin/tests/access/ownable.cairo new file mode 100644 index 000000000..a1d806b50 --- /dev/null +++ b/src/openzeppelin/tests/access/ownable.cairo @@ -0,0 +1,2 @@ +mod test_ownable; +mod test_dual_ownable; From f5036c280ccd8555c4227c10c54705346fec39ab Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 16 Jun 2023 23:12:32 -0400 Subject: [PATCH 11/42] move tests --- src/openzeppelin/tests.cairo | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/openzeppelin/tests.cairo b/src/openzeppelin/tests.cairo index 5aa74d96f..d692864dd 100644 --- a/src/openzeppelin/tests.cairo +++ b/src/openzeppelin/tests.cairo @@ -1,6 +1,7 @@ -mod test_accesscontrol; +mod access; +//mod test_accesscontrol; mod test_reentrancyguard; -mod test_ownable; +//mod test_ownable; mod test_erc165; mod test_account; mod test_erc20; From 5b4e9c8d1ba52d95bc62ccf4ec99fdff585b02b7 Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 16 Jun 2023 23:13:07 -0400 Subject: [PATCH 12/42] add mocks --- src/openzeppelin/tests/mocks.cairo | 2 + .../tests/mocks/dual_ownable_mocks.cairo | 113 ++++++++++++++++++ .../tests/mocks/non_implementing_mock.cairo | 7 ++ 3 files changed, 122 insertions(+) create mode 100644 src/openzeppelin/tests/mocks/dual_ownable_mocks.cairo create mode 100644 src/openzeppelin/tests/mocks/non_implementing_mock.cairo diff --git a/src/openzeppelin/tests/mocks.cairo b/src/openzeppelin/tests/mocks.cairo index b8f645b23..a1d6f751c 100644 --- a/src/openzeppelin/tests/mocks.cairo +++ b/src/openzeppelin/tests/mocks.cairo @@ -2,3 +2,5 @@ mod reentrancy_attacker_mock; mod reentrancy_mock; mod erc721_receiver; mod mock_pausable; +mod dual_ownable_mocks; +mod non_implementing_mock; diff --git a/src/openzeppelin/tests/mocks/dual_ownable_mocks.cairo b/src/openzeppelin/tests/mocks/dual_ownable_mocks.cairo new file mode 100644 index 000000000..34cc90dc9 --- /dev/null +++ b/src/openzeppelin/tests/mocks/dual_ownable_mocks.cairo @@ -0,0 +1,113 @@ +#[contract] +mod SnakeOwnableMock { + use starknet::ContractAddress; + use openzeppelin::access::ownable::Ownable; + + #[constructor] + fn constructor() { + Ownable::initializer(); + } + + #[view] + fn owner() -> ContractAddress { + Ownable::owner() + } + + #[external] + fn transfer_ownership(new_owner: ContractAddress) { + Ownable::transfer_ownership(new_owner); + } + + #[external] + fn renounce_ownership() { + Ownable::renounce_ownership(); + } +} + +#[contract] +mod CamelOwnableMock { + use starknet::ContractAddress; + use starknet::get_caller_address; + use openzeppelin::access::ownable::Ownable; + + #[constructor] + fn constructor() { + Ownable::initializer(); + } + + #[view] + fn owner() -> ContractAddress { + Ownable::owner() + } + + #[external] + fn transferOwnership(newOwner: ContractAddress) { + Ownable::transferOwnership(newOwner); + } + + #[external] + fn renounceOwnership() { + Ownable::renounceOwnership(); + } +} + +// Although the following modules are designed to panic, +// functions still need a valid return value. We chose: +// +// false for bool + +#[contract] +mod SnakeOwnablePanicMock { + use starknet::ContractAddress; + use starknet::get_caller_address; + use openzeppelin::access::ownable::Ownable; + + #[constructor] + fn constructor() { + Ownable::initializer(); + } + + #[view] + fn owner() -> ContractAddress { + panic_with_felt252('Some error'); + Zeroable::zero() + } + + #[external] + fn transfer_ownership(new_owner: ContractAddress) { + panic_with_felt252('Some error'); + } + + #[external] + fn renounce_ownership() { + panic_with_felt252('Some error'); + } +} + +#[contract] +mod CamelOwnablePanicMock { + use starknet::ContractAddress; + use starknet::get_caller_address; + use openzeppelin::access::ownable::Ownable; + + #[constructor] + fn constructor() { + Ownable::initializer(); + } + + #[view] + fn owner() -> ContractAddress { + panic_with_felt252('Some error'); + Zeroable::zero() + } + + #[external] + fn transfer_ownership(new_owner: ContractAddress) { + panic_with_felt252('Some error'); + } + + #[external] + fn renounce_ownership() { + panic_with_felt252('Some error'); + } +} diff --git a/src/openzeppelin/tests/mocks/non_implementing_mock.cairo b/src/openzeppelin/tests/mocks/non_implementing_mock.cairo new file mode 100644 index 000000000..d614d1da7 --- /dev/null +++ b/src/openzeppelin/tests/mocks/non_implementing_mock.cairo @@ -0,0 +1,7 @@ +#[contract] +mod NonImplementingMock { + #[view] + fn nope() -> bool { + false + } +} From 0740651eaee189c0f3de43178234737aa0d0cc47 Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 16 Jun 2023 23:13:27 -0400 Subject: [PATCH 13/42] add dual ownable tests --- .../access/ownable/test_dual_ownable.cairo | 207 ++++++++++++++++++ 1 file changed, 207 insertions(+) create mode 100644 src/openzeppelin/tests/access/ownable/test_dual_ownable.cairo diff --git a/src/openzeppelin/tests/access/ownable/test_dual_ownable.cairo b/src/openzeppelin/tests/access/ownable/test_dual_ownable.cairo new file mode 100644 index 000000000..e15513a92 --- /dev/null +++ b/src/openzeppelin/tests/access/ownable/test_dual_ownable.cairo @@ -0,0 +1,207 @@ +use traits::Into; +use array::ArrayTrait; +use starknet::ContractAddress; +use starknet::contract_address_const; +use starknet::testing::set_caller_address; +use starknet::testing::set_contract_address; +use zeroable::Zeroable; + +// Dispatchers +use openzeppelin::access::ownable::interface::IOwnableDispatcher; +use openzeppelin::access::ownable::interface::IOwnableCamelDispatcher; +use openzeppelin::access::ownable::interface::IOwnableDispatcherTrait; +use openzeppelin::access::ownable::interface::IOwnableCamelDispatcherTrait; + +// Dual case Ownable +use openzeppelin::access::ownable::dual_ownable::DualCaseOwnableTrait; +use openzeppelin::access::ownable::dual_ownable::DualCaseOwnable; + +// Mocks +use openzeppelin::tests::mocks::dual_ownable_mocks::SnakeOwnableMock; +use openzeppelin::tests::mocks::dual_ownable_mocks::CamelOwnableMock; +use openzeppelin::tests::mocks::dual_ownable_mocks::SnakeOwnablePanicMock; +use openzeppelin::tests::mocks::dual_ownable_mocks::CamelOwnablePanicMock; +use openzeppelin::tests::mocks::non_implementing_mock::NonImplementingMock; + +// Other +use openzeppelin::tests::utils; +use openzeppelin::tests::utils::PanicTrait; + +/// +/// Constants +/// + +fn OWNER() -> ContractAddress { + contract_address_const::<10>() +} + +fn NEW_OWNER() -> ContractAddress { + contract_address_const::<20>() +} + +/// +/// Setup +/// + +fn setup_snake() -> (DualCaseOwnable, IOwnableDispatcher) { + let mut calldata = ArrayTrait::new(); + set_caller_address(OWNER()); + let target = utils::deploy(SnakeOwnableMock::TEST_CLASS_HASH, calldata); + (DualCaseOwnable { contract_address: target }, IOwnableDispatcher { contract_address: target }) +} + +fn setup_camel() -> (DualCaseOwnable, IOwnableDispatcher) { + let mut calldata = ArrayTrait::new(); + set_caller_address(OWNER()); + let target = utils::deploy(CamelOwnableMock::TEST_CLASS_HASH, calldata); + (DualCaseOwnable { contract_address: target }, IOwnableDispatcher { contract_address: target }) +} + +fn setup_non_ownable() -> DualCaseOwnable { + let calldata = ArrayTrait::new(); + set_caller_address(OWNER()); + let target = utils::deploy(NonImplementingMock::TEST_CLASS_HASH, calldata); + DualCaseOwnable { contract_address: target } +} + +fn setup_ownable_panic() -> (DualCaseOwnable, DualCaseOwnable) { + set_caller_address(OWNER()); + let snake_target = utils::deploy(SnakeOwnablePanicMock::TEST_CLASS_HASH, ArrayTrait::new()); + let camel_target = utils::deploy(CamelOwnablePanicMock::TEST_CLASS_HASH, ArrayTrait::new()); + ( + DualCaseOwnable { + contract_address: snake_target + }, DualCaseOwnable { + contract_address: camel_target + } + ) +} + +/// +/// Case agnostic methods +/// + +#[test] +#[available_gas(2000000)] +fn test_dual_owner() { + let (snake_dispatcher, _) = setup_snake(); + let (camel_dispatcher, _) = setup_camel(); + assert(snake_dispatcher.owner() == OWNER(), 'Should return OWNER'); + assert(camel_dispatcher.owner() == OWNER(), 'Should return OWNER'); +} + +#[test] +#[available_gas(2000000)] +#[should_panic(expected: ('ENTRYPOINT_NOT_FOUND', ))] +fn test_dual_no_owner() { + let dispatcher = setup_non_ownable(); + dispatcher.owner(); +} + +#[test] +#[available_gas(2000000)] +#[should_panic(expected: ('Some error', 'ENTRYPOINT_FAILED', ))] +fn test_dual_owner_exists_and_panics() { + let (dispatcher, _) = setup_ownable_panic(); + dispatcher.owner(); +} + +/// +/// snake_case target +/// + +#[test] +#[available_gas(2000000)] +fn test_dual_transfer_ownership() { + let (dispatcher, target) = setup_snake(); + set_contract_address(OWNER()); // Bug with test-runner + dispatcher.transfer_ownership(NEW_OWNER()); + assert(target.owner() == NEW_OWNER(), 'Should be new owner'); +} + +#[test] +#[available_gas(2000000)] +#[should_panic(expected: ('ENTRYPOINT_NOT_FOUND', ))] +fn test_dual_no_transfer_ownership() { + let dispatcher = setup_non_ownable(); + dispatcher.transfer_ownership(NEW_OWNER()); +} + +#[test] +#[available_gas(2000000)] +#[should_panic(expected: ('Some error', 'ENTRYPOINT_FAILED', ))] +fn test_dual_transfer_ownership_exists_and_panics() { + let (dispatcher, _) = setup_ownable_panic(); + dispatcher.transfer_ownership(NEW_OWNER()); +} + +#[test] +#[available_gas(2000000)] +fn test_dual_renounce_ownership() { + let (dispatcher, target) = setup_snake(); + set_contract_address(OWNER()); + dispatcher.renounce_ownership(); + assert(target.owner().is_zero(), 'Should be zero'); +} + + +#[test] +#[available_gas(2000000)] +#[should_panic(expected: ('ENTRYPOINT_NOT_FOUND', ))] +fn test_dual_no_renounce_ownership() { + let dispatcher = setup_non_ownable(); + set_contract_address(OWNER()); // Bug with test-runner + dispatcher.renounce_ownership(); +} + +#[test] +#[available_gas(2000000)] +#[should_panic(expected: ('Some error', 'ENTRYPOINT_FAILED', ))] +fn test_dual_renounce_ownership_exists_and_panics() { + let (dispatcher, _) = setup_ownable_panic(); + set_contract_address(OWNER()); // Bug with test-runner + dispatcher.renounce_ownership(); +} + +/// +/// camelCase target +/// + +#[ignore] // Bug with test-runner +#[test] +#[available_gas(2000000)] +fn test_dual_transferOwnership() { + let (dispatcher, target) = setup_camel(); + set_contract_address(OWNER()); // Bug with test-runner + dispatcher.transfer_ownership(NEW_OWNER()); + assert(target.owner() == NEW_OWNER(), 'Should change ownership'); +} + +#[test] +#[available_gas(2000000)] +#[should_panic(expected: ('Some error', 'ENTRYPOINT_FAILED', ))] +fn test_dual_transferOwnership_exists_and_panics() { + let (_, dispatcher) = setup_ownable_panic(); + set_contract_address(OWNER()); // Bug with test-runner + dispatcher.transfer_ownership(NEW_OWNER()); +} + +#[ignore] // Bug with test-runner +#[test] +#[available_gas(2000000)] +fn test_dual_renounceOwnership() { + let (dispatcher, target) = setup_camel(); + set_contract_address(OWNER()); // Bug with test-runner + dispatcher.renounce_ownership(); + assert(target.owner().is_zero(), 'Should be zero'); +} + +#[test] +#[available_gas(2000000)] +#[should_panic(expected: ('Some error', 'ENTRYPOINT_FAILED', ))] +fn test_dual_renounceOwnership_exists_and_panics() { + let (_, dispatcher) = setup_ownable_panic(); + set_contract_address(OWNER()); // Bug with test-runner + dispatcher.renounce_ownership(); +} + From e40104874b9b3a2de953aaafec47919979ee43b3 Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 16 Jun 2023 23:13:36 -0400 Subject: [PATCH 14/42] add panic traits --- src/openzeppelin/tests/utils.cairo | 38 ++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/src/openzeppelin/tests/utils.cairo b/src/openzeppelin/tests/utils.cairo index d3b720cce..f6fe21774 100644 --- a/src/openzeppelin/tests/utils.cairo +++ b/src/openzeppelin/tests/utils.cairo @@ -1,9 +1,12 @@ -use array::ArrayTrait; use core::result::ResultTrait; use option::OptionTrait; +use array::ArrayTrait; +use traits::TryInto; +use traits::Into; + +use openzeppelin::utils::BoolIntoFelt252; use starknet::class_hash::Felt252TryIntoClassHash; use starknet::ContractAddress; -use traits::TryInto; fn deploy(contract_class_hash: felt252, calldata: Array) -> ContractAddress { let (address, _) = starknet::deploy_syscall( @@ -12,3 +15,34 @@ fn deploy(contract_class_hash: felt252, calldata: Array) -> ContractAdd .unwrap(); address } + +trait PanicTrait { + fn panic(self: T); +} + +impl Felt252PanicImpl of PanicTrait { + fn panic(self: felt252) { + panic_with_felt252(self); + } +} + +impl ContractAddressPanicImpl of PanicTrait { + fn panic(self: ContractAddress) { + panic_with_felt252(self.into()); + } +} + +impl U256PanicImpl of PanicTrait { + fn panic(self: u256) { + let mut message = ArrayTrait::new(); + message.append(self.low.into()); + message.append(self.high.into()); + panic(message); + } +} + +impl BoolPanicImpl of PanicTrait { + fn panic(self: bool) { + panic_with_felt252(self.into()); + } +} From 572a36ac16f3b4cc09682607b30ec79f0a3c3382 Mon Sep 17 00:00:00 2001 From: Andrew Date: Sat, 17 Jun 2023 22:22:44 -0400 Subject: [PATCH 15/42] tidy up tests --- .../tests/access/ownable/test_dual_ownable.cairo | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/openzeppelin/tests/access/ownable/test_dual_ownable.cairo b/src/openzeppelin/tests/access/ownable/test_dual_ownable.cairo index e15513a92..247b07175 100644 --- a/src/openzeppelin/tests/access/ownable/test_dual_ownable.cairo +++ b/src/openzeppelin/tests/access/ownable/test_dual_ownable.cairo @@ -139,7 +139,7 @@ fn test_dual_transfer_ownership_exists_and_panics() { #[available_gas(2000000)] fn test_dual_renounce_ownership() { let (dispatcher, target) = setup_snake(); - set_contract_address(OWNER()); + set_contract_address(OWNER()); // Bug with test-runner dispatcher.renounce_ownership(); assert(target.owner().is_zero(), 'Should be zero'); } @@ -150,7 +150,6 @@ fn test_dual_renounce_ownership() { #[should_panic(expected: ('ENTRYPOINT_NOT_FOUND', ))] fn test_dual_no_renounce_ownership() { let dispatcher = setup_non_ownable(); - set_contract_address(OWNER()); // Bug with test-runner dispatcher.renounce_ownership(); } @@ -159,7 +158,6 @@ fn test_dual_no_renounce_ownership() { #[should_panic(expected: ('Some error', 'ENTRYPOINT_FAILED', ))] fn test_dual_renounce_ownership_exists_and_panics() { let (dispatcher, _) = setup_ownable_panic(); - set_contract_address(OWNER()); // Bug with test-runner dispatcher.renounce_ownership(); } @@ -182,7 +180,6 @@ fn test_dual_transferOwnership() { #[should_panic(expected: ('Some error', 'ENTRYPOINT_FAILED', ))] fn test_dual_transferOwnership_exists_and_panics() { let (_, dispatcher) = setup_ownable_panic(); - set_contract_address(OWNER()); // Bug with test-runner dispatcher.transfer_ownership(NEW_OWNER()); } @@ -201,7 +198,6 @@ fn test_dual_renounceOwnership() { #[should_panic(expected: ('Some error', 'ENTRYPOINT_FAILED', ))] fn test_dual_renounceOwnership_exists_and_panics() { let (_, dispatcher) = setup_ownable_panic(); - set_contract_address(OWNER()); // Bug with test-runner dispatcher.renounce_ownership(); } From 67f94c7b05e42a94e10568933b8de7e5312fa697 Mon Sep 17 00:00:00 2001 From: Andrew Date: Sat, 17 Jun 2023 22:23:09 -0400 Subject: [PATCH 16/42] fix formatting --- src/openzeppelin/access/ownable/interface.cairo | 2 +- src/openzeppelin/utils/constants.cairo | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/openzeppelin/access/ownable/interface.cairo b/src/openzeppelin/access/ownable/interface.cairo index 9570e5437..41cd27a69 100644 --- a/src/openzeppelin/access/ownable/interface.cairo +++ b/src/openzeppelin/access/ownable/interface.cairo @@ -18,4 +18,4 @@ trait IOwnableCamel { fn transferOwnership(newOwner: ContractAddress); #[external] fn renounceOwnership(); -} \ No newline at end of file +} diff --git a/src/openzeppelin/utils/constants.cairo b/src/openzeppelin/utils/constants.cairo index a13dd646f..15b90f654 100644 --- a/src/openzeppelin/utils/constants.cairo +++ b/src/openzeppelin/utils/constants.cairo @@ -42,7 +42,11 @@ const DEFAULT_ADMIN_ROLE: felt252 = 0; // const OWNER_SELECTOR: felt252 = 0x2016836a56b71f0d02689e69e326f4f4c1b9057164ef592671cf0d37c8040c0; -const TRANSFER_OWNERSHIP_SELECTOR: felt252 = 0x2a3bb1eaa05b77c4b0eeee0116a3177c6d62319dd7149ae148185d9e09de74a; -const TRANSFEROWNERSHIP_SELECTOR: felt252 = 0x14a390f291e2e1f29874769efdef47ddad94d76f77ff516fad206a385e8995f; -const RENOUNCE_OWNERSHIP_SELECTOR: felt252 = 0x52580a92c73f4428f1a260c5d768ef462b25955307de00f99957df119865d; -const RENOUNCEOWNERSHIP_SELECTOR: felt252 = 0xd5d33d590e6660853069b37a2aea67c6fdaa0268626bc760350b590490feb5; +const TRANSFER_OWNERSHIP_SELECTOR: felt252 = + 0x2a3bb1eaa05b77c4b0eeee0116a3177c6d62319dd7149ae148185d9e09de74a; +const TRANSFEROWNERSHIP_SELECTOR: felt252 = + 0x14a390f291e2e1f29874769efdef47ddad94d76f77ff516fad206a385e8995f; +const RENOUNCE_OWNERSHIP_SELECTOR: felt252 = + 0x52580a92c73f4428f1a260c5d768ef462b25955307de00f99957df119865d; +const RENOUNCEOWNERSHIP_SELECTOR: felt252 = + 0xd5d33d590e6660853069b37a2aea67c6fdaa0268626bc760350b590490feb5; From 89b2219c37670f0359eee088a24a6ed3201168fd Mon Sep 17 00:00:00 2001 From: Andrew Date: Thu, 22 Jun 2023 21:58:00 -0400 Subject: [PATCH 17/42] remove comments --- src/openzeppelin/tests.cairo | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/openzeppelin/tests.cairo b/src/openzeppelin/tests.cairo index d692864dd..afca9600d 100644 --- a/src/openzeppelin/tests.cairo +++ b/src/openzeppelin/tests.cairo @@ -1,7 +1,5 @@ mod access; -//mod test_accesscontrol; mod test_reentrancyguard; -//mod test_ownable; mod test_erc165; mod test_account; mod test_erc20; From 62aa022df04dc3ba586e12f487ad805b04c2a458 Mon Sep 17 00:00:00 2001 From: Andrew Date: Thu, 22 Jun 2023 22:15:00 -0400 Subject: [PATCH 18/42] fix hierarchy --- src/openzeppelin/tests/access.cairo | 5 +++-- src/openzeppelin/tests/access/accesscontrol.cairo | 1 - src/openzeppelin/tests/access/ownable.cairo | 2 -- .../access/{accesscontrol => }/test_accesscontrol.cairo | 0 .../tests/access/{ownable => }/test_dual_ownable.cairo | 0 .../tests/access/{ownable => }/test_ownable.cairo | 0 6 files changed, 3 insertions(+), 5 deletions(-) delete mode 100644 src/openzeppelin/tests/access/accesscontrol.cairo delete mode 100644 src/openzeppelin/tests/access/ownable.cairo rename src/openzeppelin/tests/access/{accesscontrol => }/test_accesscontrol.cairo (100%) rename src/openzeppelin/tests/access/{ownable => }/test_dual_ownable.cairo (100%) rename src/openzeppelin/tests/access/{ownable => }/test_ownable.cairo (100%) diff --git a/src/openzeppelin/tests/access.cairo b/src/openzeppelin/tests/access.cairo index 8f59b4ad1..f5857e98d 100644 --- a/src/openzeppelin/tests/access.cairo +++ b/src/openzeppelin/tests/access.cairo @@ -1,2 +1,3 @@ -mod accesscontrol; -mod ownable; +mod test_accesscontrol; +mod test_ownable; +mod test_dual_ownable; diff --git a/src/openzeppelin/tests/access/accesscontrol.cairo b/src/openzeppelin/tests/access/accesscontrol.cairo deleted file mode 100644 index 5faefcd75..000000000 --- a/src/openzeppelin/tests/access/accesscontrol.cairo +++ /dev/null @@ -1 +0,0 @@ -mod test_accesscontrol; diff --git a/src/openzeppelin/tests/access/ownable.cairo b/src/openzeppelin/tests/access/ownable.cairo deleted file mode 100644 index a1d806b50..000000000 --- a/src/openzeppelin/tests/access/ownable.cairo +++ /dev/null @@ -1,2 +0,0 @@ -mod test_ownable; -mod test_dual_ownable; diff --git a/src/openzeppelin/tests/access/accesscontrol/test_accesscontrol.cairo b/src/openzeppelin/tests/access/test_accesscontrol.cairo similarity index 100% rename from src/openzeppelin/tests/access/accesscontrol/test_accesscontrol.cairo rename to src/openzeppelin/tests/access/test_accesscontrol.cairo diff --git a/src/openzeppelin/tests/access/ownable/test_dual_ownable.cairo b/src/openzeppelin/tests/access/test_dual_ownable.cairo similarity index 100% rename from src/openzeppelin/tests/access/ownable/test_dual_ownable.cairo rename to src/openzeppelin/tests/access/test_dual_ownable.cairo diff --git a/src/openzeppelin/tests/access/ownable/test_ownable.cairo b/src/openzeppelin/tests/access/test_ownable.cairo similarity index 100% rename from src/openzeppelin/tests/access/ownable/test_ownable.cairo rename to src/openzeppelin/tests/access/test_ownable.cairo From d3c9cd87e19e0f44c6b746f01d3789109f1386a5 Mon Sep 17 00:00:00 2001 From: Andrew Date: Thu, 22 Jun 2023 22:26:12 -0400 Subject: [PATCH 19/42] remove import comments --- src/openzeppelin/tests/access/test_dual_ownable.cairo | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/openzeppelin/tests/access/test_dual_ownable.cairo b/src/openzeppelin/tests/access/test_dual_ownable.cairo index 247b07175..a7344f33e 100644 --- a/src/openzeppelin/tests/access/test_dual_ownable.cairo +++ b/src/openzeppelin/tests/access/test_dual_ownable.cairo @@ -1,29 +1,22 @@ -use traits::Into; use array::ArrayTrait; use starknet::ContractAddress; use starknet::contract_address_const; use starknet::testing::set_caller_address; use starknet::testing::set_contract_address; +use traits::Into; use zeroable::Zeroable; -// Dispatchers use openzeppelin::access::ownable::interface::IOwnableDispatcher; use openzeppelin::access::ownable::interface::IOwnableCamelDispatcher; use openzeppelin::access::ownable::interface::IOwnableDispatcherTrait; use openzeppelin::access::ownable::interface::IOwnableCamelDispatcherTrait; - -// Dual case Ownable use openzeppelin::access::ownable::dual_ownable::DualCaseOwnableTrait; use openzeppelin::access::ownable::dual_ownable::DualCaseOwnable; - -// Mocks use openzeppelin::tests::mocks::dual_ownable_mocks::SnakeOwnableMock; use openzeppelin::tests::mocks::dual_ownable_mocks::CamelOwnableMock; use openzeppelin::tests::mocks::dual_ownable_mocks::SnakeOwnablePanicMock; use openzeppelin::tests::mocks::dual_ownable_mocks::CamelOwnablePanicMock; use openzeppelin::tests::mocks::non_implementing_mock::NonImplementingMock; - -// Other use openzeppelin::tests::utils; use openzeppelin::tests::utils::PanicTrait; From ef2bcecf7e605c1ab98ea6938e4b176f18a59fc5 Mon Sep 17 00:00:00 2001 From: Andrew Date: Thu, 22 Jun 2023 22:35:03 -0400 Subject: [PATCH 20/42] remove comment --- src/openzeppelin/tests/mocks/dual_ownable_mocks.cairo | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/openzeppelin/tests/mocks/dual_ownable_mocks.cairo b/src/openzeppelin/tests/mocks/dual_ownable_mocks.cairo index 34cc90dc9..c3373ed8a 100644 --- a/src/openzeppelin/tests/mocks/dual_ownable_mocks.cairo +++ b/src/openzeppelin/tests/mocks/dual_ownable_mocks.cairo @@ -51,11 +51,6 @@ mod CamelOwnableMock { } } -// Although the following modules are designed to panic, -// functions still need a valid return value. We chose: -// -// false for bool - #[contract] mod SnakeOwnablePanicMock { use starknet::ContractAddress; From c852416a1d17328b3368e5b74a04f8d43c8884f7 Mon Sep 17 00:00:00 2001 From: Andrew Fleming Date: Thu, 22 Jun 2023 22:36:36 -0400 Subject: [PATCH 21/42] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Martín Triay --- .../tests/mocks/dual_ownable_mocks.cairo | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/src/openzeppelin/tests/mocks/dual_ownable_mocks.cairo b/src/openzeppelin/tests/mocks/dual_ownable_mocks.cairo index c3373ed8a..f84bc195e 100644 --- a/src/openzeppelin/tests/mocks/dual_ownable_mocks.cairo +++ b/src/openzeppelin/tests/mocks/dual_ownable_mocks.cairo @@ -27,7 +27,6 @@ mod SnakeOwnableMock { #[contract] mod CamelOwnableMock { use starknet::ContractAddress; - use starknet::get_caller_address; use openzeppelin::access::ownable::Ownable; #[constructor] @@ -54,13 +53,6 @@ mod CamelOwnableMock { #[contract] mod SnakeOwnablePanicMock { use starknet::ContractAddress; - use starknet::get_caller_address; - use openzeppelin::access::ownable::Ownable; - - #[constructor] - fn constructor() { - Ownable::initializer(); - } #[view] fn owner() -> ContractAddress { @@ -82,13 +74,6 @@ mod SnakeOwnablePanicMock { #[contract] mod CamelOwnablePanicMock { use starknet::ContractAddress; - use starknet::get_caller_address; - use openzeppelin::access::ownable::Ownable; - - #[constructor] - fn constructor() { - Ownable::initializer(); - } #[view] fn owner() -> ContractAddress { From 1b072d3730f4ff1ced7f3c36ca2ad5d177197958 Mon Sep 17 00:00:00 2001 From: Andrew Date: Thu, 22 Jun 2023 22:53:42 -0400 Subject: [PATCH 22/42] remove unused import --- src/openzeppelin/tests/access/test_dual_ownable.cairo | 1 - 1 file changed, 1 deletion(-) diff --git a/src/openzeppelin/tests/access/test_dual_ownable.cairo b/src/openzeppelin/tests/access/test_dual_ownable.cairo index a7344f33e..ca0a35f38 100644 --- a/src/openzeppelin/tests/access/test_dual_ownable.cairo +++ b/src/openzeppelin/tests/access/test_dual_ownable.cairo @@ -18,7 +18,6 @@ use openzeppelin::tests::mocks::dual_ownable_mocks::SnakeOwnablePanicMock; use openzeppelin::tests::mocks::dual_ownable_mocks::CamelOwnablePanicMock; use openzeppelin::tests::mocks::non_implementing_mock::NonImplementingMock; use openzeppelin::tests::utils; -use openzeppelin::tests::utils::PanicTrait; /// /// Constants From 27ffce47ead303af088bbee5f66c67ac2a165a8f Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 23 Jun 2023 03:30:51 -0400 Subject: [PATCH 23/42] fix camel dispatcher --- src/openzeppelin/tests/access/test_dual_ownable.cairo | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/openzeppelin/tests/access/test_dual_ownable.cairo b/src/openzeppelin/tests/access/test_dual_ownable.cairo index ca0a35f38..cfd9ec4a1 100644 --- a/src/openzeppelin/tests/access/test_dual_ownable.cairo +++ b/src/openzeppelin/tests/access/test_dual_ownable.cairo @@ -42,11 +42,11 @@ fn setup_snake() -> (DualCaseOwnable, IOwnableDispatcher) { (DualCaseOwnable { contract_address: target }, IOwnableDispatcher { contract_address: target }) } -fn setup_camel() -> (DualCaseOwnable, IOwnableDispatcher) { +fn setup_camel() -> (DualCaseOwnable, IOwnableCamelDispatcher) { let mut calldata = ArrayTrait::new(); set_caller_address(OWNER()); let target = utils::deploy(CamelOwnableMock::TEST_CLASS_HASH, calldata); - (DualCaseOwnable { contract_address: target }, IOwnableDispatcher { contract_address: target }) + (DualCaseOwnable { contract_address: target }, IOwnableCamelDispatcher { contract_address: target }) } fn setup_non_ownable() -> DualCaseOwnable { From 1ab8f542f3f466c96d4c7b75269ae430698de1c6 Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 23 Jun 2023 11:25:22 -0400 Subject: [PATCH 24/42] normalize assert error msg --- src/openzeppelin/tests/access/test_dual_ownable.cairo | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/openzeppelin/tests/access/test_dual_ownable.cairo b/src/openzeppelin/tests/access/test_dual_ownable.cairo index cfd9ec4a1..bfddb0b10 100644 --- a/src/openzeppelin/tests/access/test_dual_ownable.cairo +++ b/src/openzeppelin/tests/access/test_dual_ownable.cairo @@ -164,7 +164,7 @@ fn test_dual_transferOwnership() { let (dispatcher, target) = setup_camel(); set_contract_address(OWNER()); // Bug with test-runner dispatcher.transfer_ownership(NEW_OWNER()); - assert(target.owner() == NEW_OWNER(), 'Should change ownership'); + assert(target.owner() == NEW_OWNER(), 'Should be new owner'); } #[test] From c5dd73e3501c177e376e36e3b2aaa0c6f840b087 Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 23 Jun 2023 11:28:37 -0400 Subject: [PATCH 25/42] fix formatting --- src/openzeppelin/tests/access/test_dual_ownable.cairo | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/openzeppelin/tests/access/test_dual_ownable.cairo b/src/openzeppelin/tests/access/test_dual_ownable.cairo index bfddb0b10..e9e9eaf73 100644 --- a/src/openzeppelin/tests/access/test_dual_ownable.cairo +++ b/src/openzeppelin/tests/access/test_dual_ownable.cairo @@ -46,7 +46,13 @@ fn setup_camel() -> (DualCaseOwnable, IOwnableCamelDispatcher) { let mut calldata = ArrayTrait::new(); set_caller_address(OWNER()); let target = utils::deploy(CamelOwnableMock::TEST_CLASS_HASH, calldata); - (DualCaseOwnable { contract_address: target }, IOwnableCamelDispatcher { contract_address: target }) + ( + DualCaseOwnable { + contract_address: target + }, IOwnableCamelDispatcher { + contract_address: target + } + ) } fn setup_non_ownable() -> DualCaseOwnable { From 9b14aa0c583f3c0a39b97047de9742f34f7906b8 Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 23 Jun 2023 11:51:44 -0400 Subject: [PATCH 26/42] remove redundant assertion --- src/openzeppelin/tests/access/test_ownable.cairo | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/openzeppelin/tests/access/test_ownable.cairo b/src/openzeppelin/tests/access/test_ownable.cairo index cbd1161db..99a717ff6 100644 --- a/src/openzeppelin/tests/access/test_ownable.cairo +++ b/src/openzeppelin/tests/access/test_ownable.cairo @@ -58,7 +58,6 @@ fn test_transfer_ownership_to_zero() { #[available_gas(2000000)] #[should_panic(expected: ('Caller is the zero address', ))] fn test_transfer_ownership_from_zero() { - assert(Ownable::owner() == ZERO(), 'Should be zero with no owner'); Ownable::transfer_ownership(OTHER()); } @@ -91,7 +90,6 @@ fn test_transferOwnership_to_zero() { #[available_gas(2000000)] #[should_panic(expected: ('Caller is the zero address', ))] fn test_transferOwnership_from_zero() { - assert(Ownable::owner() == ZERO(), 'Should be zero with no owner'); Ownable::transferOwnership(OTHER()); } From 560b8fe2e121a341ae669f6f1dc8819862ab86d3 Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 23 Jun 2023 11:59:12 -0400 Subject: [PATCH 27/42] change selector casing --- src/openzeppelin/access/ownable/dual_ownable.cairo | 10 +++++----- src/openzeppelin/utils/constants.cairo | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/openzeppelin/access/ownable/dual_ownable.cairo b/src/openzeppelin/access/ownable/dual_ownable.cairo index 2d7d1b547..3b88fa710 100644 --- a/src/openzeppelin/access/ownable/dual_ownable.cairo +++ b/src/openzeppelin/access/ownable/dual_ownable.cairo @@ -27,7 +27,7 @@ trait DualCaseOwnableTrait { impl DualCaseOwnableImpl of DualCaseOwnableTrait { fn owner(self: @DualCaseOwnable) -> ContractAddress { (*call_contract_syscall( - *self.contract_address, constants::OWNER_SELECTOR, ArrayTrait::new().span() + *self.contract_address, constants::owner_SELECTOR, ArrayTrait::new().span() ) .unwrap_syscall() .at(0)) @@ -36,8 +36,8 @@ impl DualCaseOwnableImpl of DualCaseOwnableTrait { } fn transfer_ownership(self: @DualCaseOwnable, new_owner: ContractAddress) { - let snake_selector = constants::TRANSFER_OWNERSHIP_SELECTOR; - let camel_selector = constants::TRANSFEROWNERSHIP_SELECTOR; + let snake_selector = constants::transfer_ownership_SELECTOR; + let camel_selector = constants::transferOwnership_SELECTOR; let mut args = ArrayTrait::new(); args.append(new_owner.into()); @@ -49,8 +49,8 @@ impl DualCaseOwnableImpl of DualCaseOwnableTrait { } fn renounce_ownership(self: @DualCaseOwnable) { - let snake_selector = constants::RENOUNCE_OWNERSHIP_SELECTOR; - let camel_selector = constants::RENOUNCEOWNERSHIP_SELECTOR; + let snake_selector = constants::renounce_ownership_SELECTOR; + let camel_selector = constants::renounceOwnership_SELECTOR; let mut args = ArrayTrait::new(); diff --git a/src/openzeppelin/utils/constants.cairo b/src/openzeppelin/utils/constants.cairo index 15b90f654..ba1005021 100644 --- a/src/openzeppelin/utils/constants.cairo +++ b/src/openzeppelin/utils/constants.cairo @@ -41,12 +41,12 @@ const DEFAULT_ADMIN_ROLE: felt252 = 0; // Selectors // -const OWNER_SELECTOR: felt252 = 0x2016836a56b71f0d02689e69e326f4f4c1b9057164ef592671cf0d37c8040c0; -const TRANSFER_OWNERSHIP_SELECTOR: felt252 = +const owner_SELECTOR: felt252 = 0x2016836a56b71f0d02689e69e326f4f4c1b9057164ef592671cf0d37c8040c0; +const transfer_ownership_SELECTOR: felt252 = 0x2a3bb1eaa05b77c4b0eeee0116a3177c6d62319dd7149ae148185d9e09de74a; -const TRANSFEROWNERSHIP_SELECTOR: felt252 = +const transferOwnership_SELECTOR: felt252 = 0x14a390f291e2e1f29874769efdef47ddad94d76f77ff516fad206a385e8995f; -const RENOUNCE_OWNERSHIP_SELECTOR: felt252 = +const renounce_ownership_SELECTOR: felt252 = 0x52580a92c73f4428f1a260c5d768ef462b25955307de00f99957df119865d; -const RENOUNCEOWNERSHIP_SELECTOR: felt252 = +const renounceOwnership_SELECTOR: felt252 = 0xd5d33d590e6660853069b37a2aea67c6fdaa0268626bc760350b590490feb5; From e34964ba2afc86249a24127737794ffe9511524a Mon Sep 17 00:00:00 2001 From: Andrew Date: Sun, 25 Jun 2023 16:21:53 -0400 Subject: [PATCH 28/42] remove unused import --- src/openzeppelin/tests/access/test_dual_ownable.cairo | 1 - 1 file changed, 1 deletion(-) diff --git a/src/openzeppelin/tests/access/test_dual_ownable.cairo b/src/openzeppelin/tests/access/test_dual_ownable.cairo index e9e9eaf73..ee1811f48 100644 --- a/src/openzeppelin/tests/access/test_dual_ownable.cairo +++ b/src/openzeppelin/tests/access/test_dual_ownable.cairo @@ -3,7 +3,6 @@ use starknet::ContractAddress; use starknet::contract_address_const; use starknet::testing::set_caller_address; use starknet::testing::set_contract_address; -use traits::Into; use zeroable::Zeroable; use openzeppelin::access::ownable::interface::IOwnableDispatcher; From cb0f6507abf52bdb9e50d2c3c8fc1822a5a3a6f0 Mon Sep 17 00:00:00 2001 From: Andrew Date: Mon, 26 Jun 2023 22:38:11 -0400 Subject: [PATCH 29/42] fix comments, add ref for ignored tests --- .../tests/access/test_dual_ownable.cairo | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/openzeppelin/tests/access/test_dual_ownable.cairo b/src/openzeppelin/tests/access/test_dual_ownable.cairo index ee1811f48..5ab36fc59 100644 --- a/src/openzeppelin/tests/access/test_dual_ownable.cairo +++ b/src/openzeppelin/tests/access/test_dual_ownable.cairo @@ -111,7 +111,7 @@ fn test_dual_owner_exists_and_panics() { #[available_gas(2000000)] fn test_dual_transfer_ownership() { let (dispatcher, target) = setup_snake(); - set_contract_address(OWNER()); // Bug with test-runner + set_contract_address(OWNER()); dispatcher.transfer_ownership(NEW_OWNER()); assert(target.owner() == NEW_OWNER(), 'Should be new owner'); } @@ -136,7 +136,7 @@ fn test_dual_transfer_ownership_exists_and_panics() { #[available_gas(2000000)] fn test_dual_renounce_ownership() { let (dispatcher, target) = setup_snake(); - set_contract_address(OWNER()); // Bug with test-runner + set_contract_address(OWNER()); dispatcher.renounce_ownership(); assert(target.owner().is_zero(), 'Should be zero'); } @@ -162,12 +162,12 @@ fn test_dual_renounce_ownership_exists_and_panics() { /// camelCase target /// -#[ignore] // Bug with test-runner +#[ignore] // Potential bug mentioned here: https://github.com/starkware-libs/cairo/issues/3432#[test] #[test] #[available_gas(2000000)] fn test_dual_transferOwnership() { let (dispatcher, target) = setup_camel(); - set_contract_address(OWNER()); // Bug with test-runner + set_contract_address(OWNER()); dispatcher.transfer_ownership(NEW_OWNER()); assert(target.owner() == NEW_OWNER(), 'Should be new owner'); } @@ -180,12 +180,12 @@ fn test_dual_transferOwnership_exists_and_panics() { dispatcher.transfer_ownership(NEW_OWNER()); } -#[ignore] // Bug with test-runner +#[ignore] // Potential bug mentioned here: https://github.com/starkware-libs/cairo/issues/3432#[test] #[test] #[available_gas(2000000)] fn test_dual_renounceOwnership() { let (dispatcher, target) = setup_camel(); - set_contract_address(OWNER()); // Bug with test-runner + set_contract_address(OWNER()); dispatcher.renounce_ownership(); assert(target.owner().is_zero(), 'Should be zero'); } From c8a7fe6ccaeeadc054a84bfb19e830b86f3f37e4 Mon Sep 17 00:00:00 2001 From: Andrew Date: Tue, 27 Jun 2023 03:43:35 -0400 Subject: [PATCH 30/42] fix comments --- src/openzeppelin/tests/access/test_dual_ownable.cairo | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/openzeppelin/tests/access/test_dual_ownable.cairo b/src/openzeppelin/tests/access/test_dual_ownable.cairo index 5ab36fc59..a7eacd21f 100644 --- a/src/openzeppelin/tests/access/test_dual_ownable.cairo +++ b/src/openzeppelin/tests/access/test_dual_ownable.cairo @@ -162,7 +162,7 @@ fn test_dual_renounce_ownership_exists_and_panics() { /// camelCase target /// -#[ignore] // Potential bug mentioned here: https://github.com/starkware-libs/cairo/issues/3432#[test] +#[ignore] // Potential bug mentioned here: https://github.com/starkware-libs/cairo/issues/3432 #[test] #[available_gas(2000000)] fn test_dual_transferOwnership() { @@ -180,7 +180,7 @@ fn test_dual_transferOwnership_exists_and_panics() { dispatcher.transfer_ownership(NEW_OWNER()); } -#[ignore] // Potential bug mentioned here: https://github.com/starkware-libs/cairo/issues/3432#[test] +#[ignore] // Potential bug mentioned here: https://github.com/starkware-libs/cairo/issues/3432 #[test] #[available_gas(2000000)] fn test_dual_renounceOwnership() { From 4d80c68c9ac0b7a24d1a89f9c66abd8270a9b4eb Mon Sep 17 00:00:00 2001 From: Andrew Date: Tue, 27 Jun 2023 03:49:47 -0400 Subject: [PATCH 31/42] remove panic trait --- src/openzeppelin/tests/utils.cairo | 38 ++---------------------------- 1 file changed, 2 insertions(+), 36 deletions(-) diff --git a/src/openzeppelin/tests/utils.cairo b/src/openzeppelin/tests/utils.cairo index f6fe21774..d3b720cce 100644 --- a/src/openzeppelin/tests/utils.cairo +++ b/src/openzeppelin/tests/utils.cairo @@ -1,12 +1,9 @@ +use array::ArrayTrait; use core::result::ResultTrait; use option::OptionTrait; -use array::ArrayTrait; -use traits::TryInto; -use traits::Into; - -use openzeppelin::utils::BoolIntoFelt252; use starknet::class_hash::Felt252TryIntoClassHash; use starknet::ContractAddress; +use traits::TryInto; fn deploy(contract_class_hash: felt252, calldata: Array) -> ContractAddress { let (address, _) = starknet::deploy_syscall( @@ -15,34 +12,3 @@ fn deploy(contract_class_hash: felt252, calldata: Array) -> ContractAdd .unwrap(); address } - -trait PanicTrait { - fn panic(self: T); -} - -impl Felt252PanicImpl of PanicTrait { - fn panic(self: felt252) { - panic_with_felt252(self); - } -} - -impl ContractAddressPanicImpl of PanicTrait { - fn panic(self: ContractAddress) { - panic_with_felt252(self.into()); - } -} - -impl U256PanicImpl of PanicTrait { - fn panic(self: u256) { - let mut message = ArrayTrait::new(); - message.append(self.low.into()); - message.append(self.high.into()); - panic(message); - } -} - -impl BoolPanicImpl of PanicTrait { - fn panic(self: bool) { - panic_with_felt252(self.into()); - } -} From bf5f66bfeb996d3c15df4e4eb897df89819d8abe Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 28 Jun 2023 12:50:20 -0400 Subject: [PATCH 32/42] bump to cairo v1.1.1 --- cairo | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cairo b/cairo index ee9a0bb1e..c6b003cc4 160000 --- a/cairo +++ b/cairo @@ -1 +1 @@ -Subproject commit ee9a0bb1e496c6e5d922c3a5071d85b45f8bc268 +Subproject commit c6b003cc425907ac5fc846375a48737d5125f5b5 From 02327ad363a4b6f0ce2922072db752c85eb596d5 Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 28 Jun 2023 12:50:37 -0400 Subject: [PATCH 33/42] update Cargo --- Cargo.lock | 56 +++++++++++++++++++++++++++--------------------------- Cargo.toml | 2 +- 2 files changed, 29 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 85aa935ae..a5ca15c63 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -301,7 +301,7 @@ dependencies = [ [[package]] name = "cairo-lang-casm" -version = "1.0.0-rc0" +version = "1.1.1" dependencies = [ "cairo-lang-utils", "env_logger", @@ -318,7 +318,7 @@ dependencies = [ [[package]] name = "cairo-lang-compiler" -version = "1.0.0-rc0" +version = "1.1.1" dependencies = [ "anyhow", "cairo-lang-defs", @@ -343,7 +343,7 @@ dependencies = [ [[package]] name = "cairo-lang-debug" -version = "1.0.0-rc0" +version = "1.1.1" dependencies = [ "cairo-lang-proc-macros", "cairo-lang-utils", @@ -354,7 +354,7 @@ dependencies = [ [[package]] name = "cairo-lang-defs" -version = "1.0.0-rc0" +version = "1.1.1" dependencies = [ "cairo-lang-debug", "cairo-lang-diagnostics", @@ -375,7 +375,7 @@ dependencies = [ [[package]] name = "cairo-lang-diagnostics" -version = "1.0.0-rc0" +version = "1.1.1" dependencies = [ "cairo-lang-filesystem", "cairo-lang-proc-macros", @@ -390,7 +390,7 @@ dependencies = [ [[package]] name = "cairo-lang-eq-solver" -version = "1.0.0-rc0" +version = "1.1.1" dependencies = [ "cairo-lang-utils", "env_logger", @@ -404,7 +404,7 @@ dependencies = [ [[package]] name = "cairo-lang-filesystem" -version = "1.0.0-rc0" +version = "1.1.1" dependencies = [ "cairo-lang-debug", "cairo-lang-utils", @@ -419,7 +419,7 @@ dependencies = [ [[package]] name = "cairo-lang-formatter" -version = "1.0.0-rc0" +version = "1.1.1" dependencies = [ "anyhow", "cairo-lang-diagnostics", @@ -442,7 +442,7 @@ dependencies = [ [[package]] name = "cairo-lang-language-server" -version = "1.0.0-rc0" +version = "1.1.1" dependencies = [ "anyhow", "cairo-lang-compiler", @@ -474,7 +474,7 @@ dependencies = [ [[package]] name = "cairo-lang-lowering" -version = "1.0.0-rc0" +version = "1.1.1" dependencies = [ "cairo-lang-debug", "cairo-lang-defs", @@ -503,7 +503,7 @@ dependencies = [ [[package]] name = "cairo-lang-parser" -version = "1.0.0-rc0" +version = "1.1.1" dependencies = [ "cairo-lang-diagnostics", "cairo-lang-filesystem", @@ -527,7 +527,7 @@ dependencies = [ [[package]] name = "cairo-lang-plugins" -version = "1.0.0-rc0" +version = "1.1.1" dependencies = [ "cairo-lang-debug", "cairo-lang-defs", @@ -550,7 +550,7 @@ dependencies = [ [[package]] name = "cairo-lang-proc-macros" -version = "1.0.0-rc0" +version = "1.1.1" dependencies = [ "cairo-lang-debug", "quote", @@ -559,7 +559,7 @@ dependencies = [ [[package]] name = "cairo-lang-project" -version = "1.0.0-rc0" +version = "1.1.1" dependencies = [ "cairo-lang-filesystem", "indoc", @@ -572,7 +572,7 @@ dependencies = [ [[package]] name = "cairo-lang-runner" -version = "1.0.0-rc0" +version = "1.1.1" dependencies = [ "anyhow", "ark-ff 0.4.0-alpha.7", @@ -607,7 +607,7 @@ dependencies = [ [[package]] name = "cairo-lang-semantic" -version = "1.0.0-rc0" +version = "1.1.1" dependencies = [ "assert_matches", "cairo-lang-debug", @@ -636,7 +636,7 @@ dependencies = [ [[package]] name = "cairo-lang-sierra" -version = "1.0.0-rc0" +version = "1.1.1" dependencies = [ "assert_matches", "bimap", @@ -664,7 +664,7 @@ dependencies = [ [[package]] name = "cairo-lang-sierra-ap-change" -version = "1.0.0-rc0" +version = "1.1.1" dependencies = [ "cairo-lang-eq-solver", "cairo-lang-sierra", @@ -679,7 +679,7 @@ dependencies = [ [[package]] name = "cairo-lang-sierra-gas" -version = "1.0.0-rc0" +version = "1.1.1" dependencies = [ "cairo-lang-eq-solver", "cairo-lang-sierra", @@ -696,7 +696,7 @@ dependencies = [ [[package]] name = "cairo-lang-sierra-generator" -version = "1.0.0-rc0" +version = "1.1.1" dependencies = [ "cairo-lang-debug", "cairo-lang-defs", @@ -727,7 +727,7 @@ dependencies = [ [[package]] name = "cairo-lang-sierra-to-casm" -version = "1.0.0-rc0" +version = "1.1.1" dependencies = [ "anyhow", "assert_matches", @@ -752,7 +752,7 @@ dependencies = [ [[package]] name = "cairo-lang-starknet" -version = "1.0.0-rc0" +version = "1.1.1" dependencies = [ "anyhow", "cairo-felt", @@ -796,7 +796,7 @@ dependencies = [ [[package]] name = "cairo-lang-syntax" -version = "1.0.0-rc0" +version = "1.1.1" dependencies = [ "cairo-lang-debug", "cairo-lang-filesystem", @@ -814,7 +814,7 @@ dependencies = [ [[package]] name = "cairo-lang-syntax-codegen" -version = "1.0.0-rc0" +version = "1.1.1" dependencies = [ "cairo-lang-utils", "env_logger", @@ -826,7 +826,7 @@ dependencies = [ [[package]] name = "cairo-lang-test-runner" -version = "1.0.0-rc0" +version = "1.1.1" dependencies = [ "anyhow", "cairo-felt", @@ -860,7 +860,7 @@ dependencies = [ [[package]] name = "cairo-lang-test-utils" -version = "1.0.0-rc0" +version = "1.1.1" dependencies = [ "cairo-lang-utils", "env_logger", @@ -871,7 +871,7 @@ dependencies = [ [[package]] name = "cairo-lang-utils" -version = "1.0.0-rc0" +version = "1.1.1" dependencies = [ "env_logger", "indexmap", @@ -2761,7 +2761,7 @@ dependencies = [ [[package]] name = "tests" -version = "1.0.0-rc0" +version = "1.1.1" dependencies = [ "assert_matches", "cairo-felt", diff --git a/Cargo.toml b/Cargo.toml index 49e71907f..3927b6991 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,7 +31,7 @@ members = [ ] [workspace.package] -version = "1.0.0-rc0" +version = "1.1.1" edition = "2021" repository = "https://github.com/starkware-libs/cairo/" license = "Apache-2.0" From 416516348fccf54179bbd00b1b27a7a058a1f196 Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 28 Jun 2023 12:52:04 -0400 Subject: [PATCH 34/42] remove ignore from tests --- src/openzeppelin/tests/access/test_dual_ownable.cairo | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/openzeppelin/tests/access/test_dual_ownable.cairo b/src/openzeppelin/tests/access/test_dual_ownable.cairo index a7eacd21f..dca5595f7 100644 --- a/src/openzeppelin/tests/access/test_dual_ownable.cairo +++ b/src/openzeppelin/tests/access/test_dual_ownable.cairo @@ -162,7 +162,6 @@ fn test_dual_renounce_ownership_exists_and_panics() { /// camelCase target /// -#[ignore] // Potential bug mentioned here: https://github.com/starkware-libs/cairo/issues/3432 #[test] #[available_gas(2000000)] fn test_dual_transferOwnership() { @@ -180,7 +179,6 @@ fn test_dual_transferOwnership_exists_and_panics() { dispatcher.transfer_ownership(NEW_OWNER()); } -#[ignore] // Potential bug mentioned here: https://github.com/starkware-libs/cairo/issues/3432 #[test] #[available_gas(2000000)] fn test_dual_renounceOwnership() { From d03269bb9234e4a00311899cb142bf4cbb09effb Mon Sep 17 00:00:00 2001 From: Andrew Fleming Date: Wed, 28 Jun 2023 14:23:42 -0400 Subject: [PATCH 35/42] Apply suggestions from code review Co-authored-by: Eric Nordelo --- src/openzeppelin/utils.cairo | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/openzeppelin/utils.cairo b/src/openzeppelin/utils.cairo index bb2473af0..31db6be79 100644 --- a/src/openzeppelin/utils.cairo +++ b/src/openzeppelin/utils.cairo @@ -12,12 +12,12 @@ fn try_selector_with_fallback( target: ContractAddress, snake_selector: felt252, camel_selector: felt252, args: Span ) -> SyscallResult> { match call_contract_syscall(target, snake_selector, args) { - Result::Ok(r) => Result::Ok(r), - Result::Err(r) => { - if *r.at(0) == 'ENTRYPOINT_NOT_FOUND' { + Result::Ok(ret) => Result::Ok(ret), + Result::Err(errors) => { + if *errors.at(0) == 'ENTRYPOINT_NOT_FOUND' { return call_contract_syscall(target, camel_selector, args); } else { - Result::Err(r) + Result::Err(errors) } } } From 2eef65195f6652782599d8f7e3fc2483efa8b202 Mon Sep 17 00:00:00 2001 From: Andrew Date: Thu, 29 Jun 2023 14:09:27 -0400 Subject: [PATCH 36/42] remove selectors from constants --- src/openzeppelin/utils/constants.cairo | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/src/openzeppelin/utils/constants.cairo b/src/openzeppelin/utils/constants.cairo index ba1005021..d7346bf55 100644 --- a/src/openzeppelin/utils/constants.cairo +++ b/src/openzeppelin/utils/constants.cairo @@ -36,17 +36,3 @@ const IACCESSCONTROL_ID: u32 = 0x7965db0b_u32; // const DEFAULT_ADMIN_ROLE: felt252 = 0; - -// -// Selectors -// - -const owner_SELECTOR: felt252 = 0x2016836a56b71f0d02689e69e326f4f4c1b9057164ef592671cf0d37c8040c0; -const transfer_ownership_SELECTOR: felt252 = - 0x2a3bb1eaa05b77c4b0eeee0116a3177c6d62319dd7149ae148185d9e09de74a; -const transferOwnership_SELECTOR: felt252 = - 0x14a390f291e2e1f29874769efdef47ddad94d76f77ff516fad206a385e8995f; -const renounce_ownership_SELECTOR: felt252 = - 0x52580a92c73f4428f1a260c5d768ef462b25955307de00f99957df119865d; -const renounceOwnership_SELECTOR: felt252 = - 0xd5d33d590e6660853069b37a2aea67c6fdaa0268626bc760350b590490feb5; From ae1839b5664a9ccf5f76fd20712ed8ab6382b6c7 Mon Sep 17 00:00:00 2001 From: Andrew Date: Thu, 29 Jun 2023 14:10:59 -0400 Subject: [PATCH 37/42] add selectors to selectors.cairo --- src/openzeppelin/access/ownable/dual_ownable.cairo | 14 ++++++-------- src/openzeppelin/utils.cairo | 1 + src/openzeppelin/utils/selectors.cairo | 11 +++++++++++ 3 files changed, 18 insertions(+), 8 deletions(-) create mode 100644 src/openzeppelin/utils/selectors.cairo diff --git a/src/openzeppelin/access/ownable/dual_ownable.cairo b/src/openzeppelin/access/ownable/dual_ownable.cairo index 3b88fa710..527b2f4b5 100644 --- a/src/openzeppelin/access/ownable/dual_ownable.cairo +++ b/src/openzeppelin/access/ownable/dual_ownable.cairo @@ -11,7 +11,7 @@ use traits::TryInto; use openzeppelin::utils::try_selector_with_fallback; use openzeppelin::utils::Felt252TryIntoBool; -use openzeppelin::utils::constants; +use openzeppelin::utils::selectors; #[derive(Copy, Drop)] struct DualCaseOwnable { @@ -26,9 +26,7 @@ trait DualCaseOwnableTrait { impl DualCaseOwnableImpl of DualCaseOwnableTrait { fn owner(self: @DualCaseOwnable) -> ContractAddress { - (*call_contract_syscall( - *self.contract_address, constants::owner_SELECTOR, ArrayTrait::new().span() - ) + (*call_contract_syscall(*self.contract_address, selectors::owner, ArrayTrait::new().span()) .unwrap_syscall() .at(0)) .try_into() @@ -36,8 +34,8 @@ impl DualCaseOwnableImpl of DualCaseOwnableTrait { } fn transfer_ownership(self: @DualCaseOwnable, new_owner: ContractAddress) { - let snake_selector = constants::transfer_ownership_SELECTOR; - let camel_selector = constants::transferOwnership_SELECTOR; + let snake_selector = selectors::transfer_ownership; + let camel_selector = selectors::transferOwnership; let mut args = ArrayTrait::new(); args.append(new_owner.into()); @@ -49,8 +47,8 @@ impl DualCaseOwnableImpl of DualCaseOwnableTrait { } fn renounce_ownership(self: @DualCaseOwnable) { - let snake_selector = constants::renounce_ownership_SELECTOR; - let camel_selector = constants::renounceOwnership_SELECTOR; + let snake_selector = selectors::renounce_ownership; + let camel_selector = selectors::renounceOwnership; let mut args = ArrayTrait::new(); diff --git a/src/openzeppelin/utils.cairo b/src/openzeppelin/utils.cairo index 31db6be79..433693466 100644 --- a/src/openzeppelin/utils.cairo +++ b/src/openzeppelin/utils.cairo @@ -6,6 +6,7 @@ use array::ArrayTrait; use array::SpanTrait; use box::BoxTrait; mod constants; +mod selectors; mod serde; fn try_selector_with_fallback( diff --git a/src/openzeppelin/utils/selectors.cairo b/src/openzeppelin/utils/selectors.cairo new file mode 100644 index 000000000..1f5b39676 --- /dev/null +++ b/src/openzeppelin/utils/selectors.cairo @@ -0,0 +1,11 @@ +// +// List of selectors with their starknet_keccak calculation +// + +const owner: felt252 = 0x2016836a56b71f0d02689e69e326f4f4c1b9057164ef592671cf0d37c8040c0; +const transfer_ownership: felt252 = + 0x2a3bb1eaa05b77c4b0eeee0116a3177c6d62319dd7149ae148185d9e09de74a; +const transferOwnership: felt252 = + 0x14a390f291e2e1f29874769efdef47ddad94d76f77ff516fad206a385e8995f; +const renounce_ownership: felt252 = 0x52580a92c73f4428f1a260c5d768ef462b25955307de00f99957df119865d; +const renounceOwnership: felt252 = 0xd5d33d590e6660853069b37a2aea67c6fdaa0268626bc760350b590490feb5; From c38d62f5ceab0be6e7d1b3a173092867dae4fb9b Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 30 Jun 2023 02:06:28 -0400 Subject: [PATCH 38/42] remove selector bindings --- .../access/ownable/dual_ownable.cairo | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/openzeppelin/access/ownable/dual_ownable.cairo b/src/openzeppelin/access/ownable/dual_ownable.cairo index 527b2f4b5..840c371f5 100644 --- a/src/openzeppelin/access/ownable/dual_ownable.cairo +++ b/src/openzeppelin/access/ownable/dual_ownable.cairo @@ -34,26 +34,26 @@ impl DualCaseOwnableImpl of DualCaseOwnableTrait { } fn transfer_ownership(self: @DualCaseOwnable, new_owner: ContractAddress) { - let snake_selector = selectors::transfer_ownership; - let camel_selector = selectors::transferOwnership; - let mut args = ArrayTrait::new(); args.append(new_owner.into()); try_selector_with_fallback( - *self.contract_address, snake_selector, camel_selector, args.span() + *self.contract_address, + selectors::transfer_ownership, + selectors::transferOwnership, + args.span() ) .unwrap_syscall(); } fn renounce_ownership(self: @DualCaseOwnable) { - let snake_selector = selectors::renounce_ownership; - let camel_selector = selectors::renounceOwnership; - let mut args = ArrayTrait::new(); try_selector_with_fallback( - *self.contract_address, snake_selector, camel_selector, args.span() + *self.contract_address, + selectors::renounce_ownership, + selectors::renounceOwnership, + args.span() ) .unwrap_syscall(); } From 07c7bbe4f6d61ba31f5eeb2583acabade6e067c3 Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 30 Jun 2023 14:02:58 -0400 Subject: [PATCH 39/42] fix comment --- src/openzeppelin/utils/selectors.cairo | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/openzeppelin/utils/selectors.cairo b/src/openzeppelin/utils/selectors.cairo index 1f5b39676..6da8b6871 100644 --- a/src/openzeppelin/utils/selectors.cairo +++ b/src/openzeppelin/utils/selectors.cairo @@ -1,5 +1,5 @@ // -// List of selectors with their starknet_keccak calculation +// Ownable // const owner: felt252 = 0x2016836a56b71f0d02689e69e326f4f4c1b9057164ef592671cf0d37c8040c0; From 06e4b8cc5f009551aaa7382777fa3c390fc2b3a4 Mon Sep 17 00:00:00 2001 From: Andrew Date: Sun, 2 Jul 2023 19:40:15 -0400 Subject: [PATCH 40/42] remove/fix comments --- src/openzeppelin/tests.cairo | 1 - src/openzeppelin/utils/selectors.cairo | 2 ++ 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/openzeppelin/tests.cairo b/src/openzeppelin/tests.cairo index 4c17cf09f..374ba6182 100644 --- a/src/openzeppelin/tests.cairo +++ b/src/openzeppelin/tests.cairo @@ -1,6 +1,5 @@ mod access; mod test_reentrancyguard; -//mod test_ownable; mod test_src5; mod test_account; mod test_erc20; diff --git a/src/openzeppelin/utils/selectors.cairo b/src/openzeppelin/utils/selectors.cairo index f55341be2..3ab48f7ce 100644 --- a/src/openzeppelin/utils/selectors.cairo +++ b/src/openzeppelin/utils/selectors.cairo @@ -9,6 +9,8 @@ const transferOwnership: felt252 = 0x14a390f291e2e1f29874769efdef47ddad94d76f77ff516fad206a385e8995f; const renounce_ownership: felt252 = 0x52580a92c73f4428f1a260c5d768ef462b25955307de00f99957df119865d; const renounceOwnership: felt252 = 0xd5d33d590e6660853069b37a2aea67c6fdaa0268626bc760350b590490feb5; + +// // ERC721 Selectors // From 967b06be9da5547ada27f1126878dc432562dd3a Mon Sep 17 00:00:00 2001 From: Andrew Date: Sun, 2 Jul 2023 19:53:06 -0400 Subject: [PATCH 41/42] fix conflicts --- src/openzeppelin/tests.cairo | 2 +- src/openzeppelin/token/erc721/interface.cairo | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/openzeppelin/tests.cairo b/src/openzeppelin/tests.cairo index 374ba6182..e50d9f319 100644 --- a/src/openzeppelin/tests.cairo +++ b/src/openzeppelin/tests.cairo @@ -4,7 +4,7 @@ mod test_src5; mod test_account; mod test_erc20; mod test_erc721; -//mod test_dual721; +mod test_dual721; mod test_initializable; mod test_pausable; mod mocks; diff --git a/src/openzeppelin/token/erc721/interface.cairo b/src/openzeppelin/token/erc721/interface.cairo index 78283dbde..53292dd94 100644 --- a/src/openzeppelin/token/erc721/interface.cairo +++ b/src/openzeppelin/token/erc721/interface.cairo @@ -8,6 +8,7 @@ const IERC721_METADATA_ID: felt252 = const IERC721_RECEIVER_ID: felt252 = 0x3a0dff5f70d80458ad14ae37bb182a728e3c8cdda0402a5daa86620bdf910bc; +#[abi] trait IERC721 { fn balance_of(account: ContractAddress) -> u256; fn owner_of(token_id: u256) -> ContractAddress; @@ -25,6 +26,7 @@ trait IERC721 { fn token_uri(token_id: u256) -> felt252; } +#[abi] trait IERC721Camel { fn balanceOf(account: ContractAddress) -> u256; fn ownerOf(tokenId: u256) -> ContractAddress; From 042311b86138c23af80484211e05b91b48e35b63 Mon Sep 17 00:00:00 2001 From: Andrew Fleming Date: Mon, 3 Jul 2023 10:22:49 -0400 Subject: [PATCH 42/42] Update src/openzeppelin/utils/selectors.cairo Co-authored-by: Eric Nordelo --- src/openzeppelin/utils/selectors.cairo | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/openzeppelin/utils/selectors.cairo b/src/openzeppelin/utils/selectors.cairo index 3ab48f7ce..8f56ff9d5 100644 --- a/src/openzeppelin/utils/selectors.cairo +++ b/src/openzeppelin/utils/selectors.cairo @@ -11,7 +11,7 @@ const renounce_ownership: felt252 = 0x52580a92c73f4428f1a260c5d768ef462b25955307 const renounceOwnership: felt252 = 0xd5d33d590e6660853069b37a2aea67c6fdaa0268626bc760350b590490feb5; // -// ERC721 Selectors +// ERC721 // const name: felt252 = 0x361458367e696363fbcc70777d07ebbd2394e89fd0adcaf147faccd1d294d60;