Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref: Privatize and Rename State Fields #500

Open
wants to merge 31 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
e6dc3d5
ref: control
0xNeshi Jan 16, 2025
d9a23a6
ref: clippy now warns on pub with underscore
0xNeshi Jan 16, 2025
e88319e
ref: ownable_two_step
0xNeshi Jan 16, 2025
ae889ac
ref: ownable
0xNeshi Jan 16, 2025
8def8ae
ref: add pub(crate) to private fields
0xNeshi Jan 16, 2025
c3b94fd
ref: vesting wallet
0xNeshi Jan 16, 2025
f7f7719
ref: erc20 capped
0xNeshi Jan 16, 2025
18980a0
ref: erc20 flash_mint
0xNeshi Jan 16, 2025
cefbf1d
ref: erc20 metadata
0xNeshi Jan 16, 2025
69a6a7b
ref: erc20 permit
0xNeshi Jan 16, 2025
bc5ce13
ref: remove {} from empty struct in eip712::tests
0xNeshi Jan 16, 2025
d18264e
ref: remove {} from empty struct in safe_erc20
0xNeshi Jan 16, 2025
a1539d5
ref: make ownable field public in own2step
0xNeshi Jan 16, 2025
f81e25a
ref: erc20
0xNeshi Jan 16, 2025
3820914
ref: erc721 consecutive
0xNeshi Jan 17, 2025
c79174b
ref: erc721 enumerable
0xNeshi Jan 17, 2025
14cc318
ref: erc721 metadata
0xNeshi Jan 17, 2025
9af8e7b
ref: erc721 uri_storage
0xNeshi Jan 17, 2025
684edd1
ref: erc721
0xNeshi Jan 17, 2025
823fc99
ref: erc721 tests
0xNeshi Jan 17, 2025
33c1c9e
ref: erc1155 all
0xNeshi Jan 17, 2025
728f241
ref: checkpoints
0xNeshi Jan 17, 2025
9d8a488
ref: metadata
0xNeshi Jan 17, 2025
f7b432d
ref: nonces
0xNeshi Jan 17, 2025
7f99d9d
ref: pausable
0xNeshi Jan 17, 2025
cb17464
ref: bitmap
0xNeshi Jan 17, 2025
ed07d5b
ref: revert own2step test names
0xNeshi Jan 17, 2025
356f933
ref: make Ownable pub in VestingWallet
0xNeshi Jan 17, 2025
7730096
ref: revert underscores in int. fn names in consecutive
0xNeshi Jan 17, 2025
3100306
ref: make metadata pub(crate) in Erc721metadata
0xNeshi Jan 17, 2025
16fbe67
chore: update changelog
0xNeshi Jan 17, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed (Breaking)

- All contract state fields are no longer public. #500
- Bump cargo-stylus to v0.5.8. #493
- Constants `TYPE_HASH`, `FIELDS`, `SALT` and `TYPED_DATA_PREFIX`, and type `DomainSeparatorTuple` are no longer exported from `utils::cryptography::eip712`. #478
- Bump Stylus SDK to v0.7.0. #433
Expand Down
29 changes: 12 additions & 17 deletions contracts/src/access/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ pub struct RoleData {
#[storage]
pub struct AccessControl {
/// Role identifier -> Role information.
#[allow(clippy::used_underscore_binding)]
pub _roles: StorageMap<FixedBytes<32>, RoleData>,
pub(crate) roles: StorageMap<FixedBytes<32>, RoleData>,
}

#[public]
Expand All @@ -133,7 +132,7 @@ impl AccessControl {
/// * `account` - The account to check for membership.
#[must_use]
pub fn has_role(&self, role: B256, account: Address) -> bool {
self._roles.getter(role).has_role.get(account)
self.roles.getter(role).has_role.get(account)
}

/// Checks if [`msg::sender`] has been granted `role`.
Expand Down Expand Up @@ -162,7 +161,7 @@ impl AccessControl {
/// * `role` - The role identifier.
#[must_use]
pub fn get_role_admin(&self, role: B256) -> B256 {
*self._roles.getter(role).admin_role
*self.roles.getter(role).admin_role
}

/// Grants `role` to `account`.
Expand Down Expand Up @@ -291,7 +290,7 @@ impl AccessControl {
/// Emits a [`RoleAdminChanged`] event.
pub fn _set_role_admin(&mut self, role: B256, new_admin_role: B256) {
let previous_admin_role = self.get_role_admin(role);
self._roles.setter(role).admin_role.set(new_admin_role);
self.roles.setter(role).admin_role.set(new_admin_role);
evm::log(RoleAdminChanged {
role,
previous_admin_role,
Expand Down Expand Up @@ -343,7 +342,7 @@ impl AccessControl {
if self.has_role(role, account) {
false
} else {
self._roles.setter(role).has_role.insert(account, true);
self.roles.setter(role).has_role.insert(account, true);
evm::log(RoleGranted { role, account, sender: msg::sender() });
true
}
Expand All @@ -365,7 +364,7 @@ impl AccessControl {
/// May emit a [`RoleRevoked`] event.
pub fn _revoke_role(&mut self, role: B256, account: Address) -> bool {
if self.has_role(role, account) {
self._roles.setter(role).has_role.insert(account, false);
self.roles.setter(role).has_role.insert(account, false);
evm::log(RoleRevoked { role, account, sender: msg::sender() });
true
} else {
Expand Down Expand Up @@ -404,11 +403,7 @@ mod tests {
// NOTE: Once we have support for setting `msg::sender` and constructor,
// this function shouldn't be needed.
fn _grant_role_to_msg_sender(contract: &mut AccessControl, role: [u8; 32]) {
contract
._roles
.setter(role.into())
.has_role
.insert(msg::sender(), true);
contract.roles.setter(role.into()).has_role.insert(msg::sender(), true);
}

#[motsu::test]
Expand Down Expand Up @@ -454,7 +449,7 @@ mod tests {
#[motsu::test]
fn admin_can_revoke_role(contract: AccessControl) {
_grant_role_to_msg_sender(contract, AccessControl::DEFAULT_ADMIN_ROLE);
contract._roles.setter(ROLE.into()).has_role.insert(ALICE, true);
contract.roles.setter(ROLE.into()).has_role.insert(ALICE, true);

let has_role = contract.has_role(ROLE.into(), ALICE);
assert!(has_role);
Expand All @@ -465,7 +460,7 @@ mod tests {

#[motsu::test]
fn non_admin_cannot_revoke_role(contract: AccessControl) {
contract._roles.setter(ROLE.into()).has_role.insert(ALICE, true);
contract.roles.setter(ROLE.into()).has_role.insert(ALICE, true);

let has_role = contract.has_role(ROLE.into(), ALICE);
assert!(has_role);
Expand Down Expand Up @@ -536,7 +531,7 @@ mod tests {
contract._set_role_admin(ROLE.into(), OTHER_ROLE.into());
_grant_role_to_msg_sender(contract, OTHER_ROLE);

contract._roles.setter(ROLE.into()).has_role.insert(ALICE, true);
contract.roles.setter(ROLE.into()).has_role.insert(ALICE, true);
contract.revoke_role(ROLE.into(), ALICE).unwrap();
let has_role = contract.has_role(ROLE.into(), ALICE);
assert!(!has_role);
Expand Down Expand Up @@ -583,14 +578,14 @@ mod tests {

#[motsu::test]
fn internal_grant_role_false_if_role(contract: AccessControl) {
contract._roles.setter(ROLE.into()).has_role.insert(ALICE, true);
contract.roles.setter(ROLE.into()).has_role.insert(ALICE, true);
let role_granted = contract._grant_role(ROLE.into(), ALICE);
assert!(!role_granted);
}

#[motsu::test]
fn internal_revoke_role_true_if_role(contract: AccessControl) {
contract._roles.setter(ROLE.into()).has_role.insert(ALICE, true);
contract.roles.setter(ROLE.into()).has_role.insert(ALICE, true);
let role_revoked = contract._revoke_role(ROLE.into(), ALICE);
assert!(role_revoked);
}
Expand Down
29 changes: 14 additions & 15 deletions contracts/src/access/ownable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ impl MethodError for Error {
#[storage]
pub struct Ownable {
/// The current owner of this contract.
#[allow(clippy::used_underscore_binding)]
pub _owner: StorageAddress,
pub(crate) owner: StorageAddress,
}

/// Interface for an [`Ownable`] contract.
Expand Down Expand Up @@ -134,7 +133,7 @@ impl IOwnable for Ownable {
type Error = Error;

fn owner(&self) -> Address {
self._owner.get()
self.owner.get()
}

fn transfer_ownership(
Expand Down Expand Up @@ -195,8 +194,8 @@ impl Ownable {
///
/// Emits a [`OwnershipTransferred`] event.
pub fn _transfer_ownership(&mut self, new_owner: Address) {
let previous_owner = self._owner.get();
self._owner.set(new_owner);
let previous_owner = self.owner.get();
self.owner.set(new_owner);
evm::log(OwnershipTransferred { previous_owner, new_owner });
}
}
Expand All @@ -212,25 +211,25 @@ mod tests {

#[motsu::test]
fn reads_owner(contract: Ownable) {
contract._owner.set(msg::sender());
contract.owner.set(msg::sender());
let owner = contract.owner();
assert_eq!(owner, msg::sender());
}

#[motsu::test]
fn transfers_ownership(contract: Ownable) {
contract._owner.set(msg::sender());
contract.owner.set(msg::sender());

contract.transfer_ownership(ALICE).expect("should transfer ownership");
let owner = contract._owner.get();
let owner = contract.owner.get();
assert_eq!(owner, ALICE);
}

#[motsu::test]
fn prevents_non_onwers_from_transferring(contract: Ownable) {
// Alice must be set as owner, because we can't set the
// `msg::sender` yet.
contract._owner.set(ALICE);
contract.owner.set(ALICE);

let bob = address!("B0B0cB49ec2e96DF5F5fFB081acaE66A2cBBc2e2");
let err = contract.transfer_ownership(bob).unwrap_err();
Expand All @@ -239,37 +238,37 @@ mod tests {

#[motsu::test]
fn prevents_reaching_stuck_state(contract: Ownable) {
contract._owner.set(msg::sender());
contract.owner.set(msg::sender());

let err = contract.transfer_ownership(Address::ZERO).unwrap_err();
assert!(matches!(err, Error::InvalidOwner(_)));
}

#[motsu::test]
fn loses_ownership_after_renouncing(contract: Ownable) {
contract._owner.set(msg::sender());
contract.owner.set(msg::sender());

let _ = contract.renounce_ownership();
let owner = contract._owner.get();
let owner = contract.owner.get();
assert_eq!(owner, Address::ZERO);
}

#[motsu::test]
fn prevents_non_owners_from_renouncing(contract: Ownable) {
// Alice must be set as owner, because we can't set the
// `msg::sender` yet.
contract._owner.set(ALICE);
contract.owner.set(ALICE);

let err = contract.renounce_ownership().unwrap_err();
assert!(matches!(err, Error::UnauthorizedAccount(_)));
}

#[motsu::test]
fn recovers_access_using_internal_transfer(contract: Ownable) {
contract._owner.set(ALICE);
contract.owner.set(ALICE);

contract._transfer_ownership(ALICE);
let owner = contract._owner.get();
let owner = contract.owner.get();
assert_eq!(owner, ALICE);
}
}
Loading
Loading