Skip to content

Commit 99f548e

Browse files
committed
wip
1 parent aca9d51 commit 99f548e

File tree

8 files changed

+180
-89
lines changed

8 files changed

+180
-89
lines changed

cosmwasm/cw-manager/src/contract.rs

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,12 @@ fn only_authorized(ctx: &mut ExecCtx) -> Result<(), ContractError> {
3636
_check_authorized(ctx)
3737
}
3838

39-
// =============================================== ROLE MANAGEMENT ===============================================
39+
// ======================================= ROLE MANAGEMENT =======================================
4040

4141
/// See [`ExecuteMsg::LabelRole`].
4242
pub(crate) fn label_role(ctx: &mut ExecCtx, role_id: RoleId, label: &str) {
43-
// TODO: figure out how we want to label roles; events like the original solidity implementation or in storage?
43+
// TODO: figure out how we want to label roles; events like the original solidity implementation
44+
// or in storage?
4445
ctx.emit(RoleLabel { role_id, label })
4546
}
4647

@@ -125,7 +126,8 @@ pub(crate) fn set_grant_delay(
125126
_set_grant_delay(ctx, role_id, new_delay)
126127
}
127128

128-
/// Internal version of [`grant_role`] without access control. Returns true if the role was newly granted.
129+
/// Internal version of [`grant_role`] without access control. Returns true if the role was newly
130+
/// granted.
129131
///
130132
/// Emits a [`RoleGranted`] event.
131133
///
@@ -188,7 +190,8 @@ pub(crate) fn _grant_role(
188190
Ok(new_member)
189191
}
190192

191-
// Internal version of [`revoke_role`] without access control. This logic is also used by {renounceRole}. Returns true if the role was previously granted.
193+
// Internal version of [`revoke_role`] without access control. This logic is also used by
194+
// {renounceRole}. Returns true if the role was previously granted.
192195
///
193196
/// Emits a [`RoleRevoked`] event if the account had the role.
194197
///
@@ -218,7 +221,7 @@ fn _revoke_role(ctx: &mut ExecCtx, role_id: RoleId, account: &Addr) -> Result<bo
218221
///
219222
/// Emits a [`RoleAdminChanged`] event.
220223
///
221-
/// NOTE: Setting the admin role as the `PUBLIC_ROLE` is allowed, but it will effectively allow
224+
/// NOTE: Setting the admin role as the [`RoleId::PUBLIC_ROLE`] is allowed, but it will effectively allow
222225
/// anyone to set grant or revoke such role.
223226
///
224227
/// ```solidity
@@ -248,7 +251,7 @@ fn _set_role_admin(ctx: &mut ExecCtx, role_id: RoleId, admin: RoleId) -> Result<
248251
///
249252
/// Emits a [`RoleGuardianChanged`] event.
250253
///
251-
/// NOTE: Setting the guardian role as the `PUBLIC_ROLE` is allowed, but it will effectively allow
254+
/// NOTE: Setting the guardian role as the [`RoleId::PUBLIC_ROLE`] is allowed, but it will effectively allow
252255
/// anyone to cancel any scheduled operation for such role.
253256
///
254257
/// ```solidity
@@ -278,9 +281,9 @@ fn _set_role_guardian(
278281
Ok(())
279282
}
280283

281-
/// Internal version of {setGrantDelay} without access control.
284+
/// Internal version of [`set_grant_delay`] without access control.
282285
///
283-
/// Emits a {RoleGrantDelayChanged} event.
286+
/// Emits a [`RoleGrantDelayChanged`] event.
284287
///
285288
/// ```solidity
286289
/// function _setGrantDelay(uint64 roleId, uint32 newDelay) internal virtual
@@ -324,7 +327,7 @@ fn _set_grant_delay(
324327
Ok(())
325328
}
326329

327-
// ============================================= FUNCTION MANAGEMENT ==============================================
330+
// ===================================== FUNCTION MANAGEMENT ======================================
328331

329332
/// See [`ExecuteMsg::SetTargetFunctionRole`].
330333
pub(crate) fn set_target_function_role<'a>(
@@ -416,7 +419,7 @@ fn _set_target_admin_delay(
416419
Ok(())
417420
}
418421

419-
// =============================================== MODE MANAGEMENT ================================================
422+
// ======================================= MODE MANAGEMENT ========================================
420423

421424
/// See [`ExecuteMsg::SetTargetClosed`].
422425
pub(crate) fn set_target_closed(
@@ -454,7 +457,7 @@ fn _set_target_closed(ctx: &mut ExecCtx, target: &Addr, closed: bool) -> Result<
454457
Ok(())
455458
}
456459

457-
// ============================================== DELAYED OPERATIONS ==============================================
460+
// ====================================== DELAYED OPERATIONS ======================================
458461

459462
/// See [`QueryMsg::GetSchedule`].
460463
pub(crate) fn get_schedule(ctx: QueryCtx, id: H256) -> Result<u64, ContractError> {
@@ -612,7 +615,8 @@ pub(crate) fn cancel(
612615
if schedule.timepoint == 0 {
613616
return Err(ContractError::AccessManagerNotScheduled(operation_id));
614617
} else if caller != msgsender {
615-
// calls can only be canceled by the account that scheduled them, a global admin, or by a guardian of the required role.
618+
// calls can only be canceled by the account that scheduled them, a global admin, or by a
619+
// guardian of the required role.
616620
let is_admin = has_role(ctx.query_ctx(), RoleId::ADMIN_ROLE, msgsender)?.is_member;
617621
let is_guardian = has_role(
618622
ctx.query_ctx(),
@@ -716,7 +720,7 @@ pub(crate) fn hash_operation(caller: &Addr, target: &Addr, data: &str) -> H256 {
716720
Sha256::digest(format!("{caller}/{target}/{data}",)).into()
717721
}
718722

719-
// ==================================================== OTHERS ====================================================
723+
// ============================================ OTHERS ============================================
720724

721725
/// @inheritdoc IAccessManager
722726
pub(crate) fn update_authority(
@@ -735,11 +739,12 @@ pub(crate) fn update_authority(
735739
)?))
736740
}
737741

738-
// ================================================= ADMIN LOGIC ==================================================
742+
// ========================================= ADMIN LOGIC ==========================================
739743

740744
/// Check if the current call is authorized according to admin and roles logic.
741745
///
742-
/// WARNING: Carefully review the considerations of {AccessManaged-restricted} since they apply to this modifier.
746+
/// WARNING: Carefully review the considerations of {AccessManaged-restricted} since they apply to
747+
/// this modifier.
743748
///
744749
/// ```solidity
745750
/// function _checkAuthorized() private
@@ -770,12 +775,14 @@ fn _check_authorized(ctx: &mut ExecCtx) -> Result<(), ContractError> {
770775
Ok(())
771776
}
772777

773-
/// Get the admin restrictions of a given function call based on the function and arguments involved.
778+
/// Get the admin restrictions of a given function call based on the function and arguments
779+
/// involved.
774780
///
775781
/// Returns:
776782
/// - bool restricted: does this data match a restricted operation
777783
/// - uint64: which role is this operation restricted to
778-
/// - uint32: minimum delay to enforce for that operation (max between operation's delay and admin's execution delay)
784+
/// - uint32: minimum delay to enforce for that operation (max between operation's delay and
785+
/// admin's execution delay)
779786
///
780787
/// ```solidity
781788
/// function _getAdminRestrictions(
@@ -810,7 +817,8 @@ fn _get_admin_restrictions(
810817
Ok((true, RoleId::ADMIN_ROLE, delay))
811818
}
812819

813-
// Restricted to that role's admin with no delay beside any execution delay the caller may have.
820+
// Restricted to that role's admin with no delay beside any execution delay the caller may
821+
// have.
814822
Ok(GrantRole { role_id, .. } | RevokeRole { role_id, .. }) => {
815823
Ok((true, get_role_admin(ctx.query_ctx(), role_id)?, 0))
816824
}
@@ -823,7 +831,8 @@ fn _get_admin_restrictions(
823831
}
824832
}
825833

826-
/// Extracts the selector from calldata. Returns an error if there is no selector in the provided JSON data.
834+
/// Extracts the selector from calldata. Returns an error if there is no selector in the provided
835+
/// JSON data.
827836
///
828837
/// ```solidity
829838
/// function _checkSelector(bytes calldata data) private pure returns (bytes4)
@@ -836,7 +845,7 @@ fn _check_selector(data: &str) -> Result<Selector, ContractError> {
836845
.selector())
837846
}
838847

839-
// =================================================== HELPERS ====================================================
848+
// =========================================== HELPERS ============================================
840849

841850
/// An extended version of [`can_call`] for internal usage that checks [`_can_call_self`]
842851
/// when the target is this contract.
@@ -876,8 +885,9 @@ fn _can_call_extended(
876885
/// <https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v5.4.0/contracts/access/manager/AccessManager.sol#L685>
877886
fn _can_call_self(ctx: &mut ExecCtx, caller: &Addr, data: &str) -> Result<CanCall, ContractError> {
878887
if caller == ctx.address_this() {
879-
// Caller is AccessManager, this means the call was sent through {execute} and it already checked
880-
// permissions. We verify that the call "identifier", which is set during {execute}, is correct.
888+
// Caller is AccessManager, this means the call was sent through {execute} and it already
889+
// checked permissions. We verify that the call "identifier", which is set during
890+
// `execute`, is correct.
881891
return Ok(CanCall {
882892
allowed: _is_executing(ctx.query_ctx(), ctx.address_this(), &_check_selector(data)?)?,
883893
delay: 0,
@@ -958,8 +968,9 @@ pub(crate) fn can_call(
958968
delay: 0,
959969
})
960970
} else if caller == ctx.address_this() {
961-
// Caller is AccessManager, this means the call was sent through {execute} and it already checked
962-
// permissions. We verify that the call "identifier", which is set during {execute}, is correct.
971+
// Caller is AccessManager, this means the call was sent through `execute` and it already
972+
// checked permissions. We verify that the call "identifier", which is set during
973+
// `execute`, is correct.
963974
Ok(CanCall {
964975
allowed: _is_executing(ctx, target, selector)?,
965976
delay: 0,
@@ -1139,6 +1150,7 @@ pub struct FullAccess {
11391150
current_delay: u32,
11401151
/// Pending execution delay for the account.
11411152
pending_delay: u32,
1142-
/// Timestamp at which the pending execution delay will become active. 0 means no delay update is scheduled.
1153+
/// Timestamp at which the pending execution delay will become active. 0 means no delay update
1154+
/// is scheduled.
11431155
effect: u64,
11441156
}

cosmwasm/cw-manager/src/event.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ impl Into<Event> for RoleLabel<'_> {
101101
/// Emitted when `account` is granted `roleId`.
102102
///
103103
/// NOTE: The meaning of the `since` argument depends on the `newMember` argument.
104-
/// If the role is granted to a new member, the `since` argument indicates when the account becomes a member of the role, otherwise it indicates the execution delay for this account and roleId is updated.
104+
/// If the role is granted to a new member, the `since` argument indicates when the account becomes
105+
/// a member of the role, otherwise it indicates the execution delay for this account and roleId is
106+
/// updated.
105107
///
106108
/// ```solidity
107109
/// event RoleGranted(uint64 indexed roleId, address indexed account, uint32 delay, uint48 since, bool newMember);
@@ -127,7 +129,8 @@ impl Into<Event> for RoleGranted<'_> {
127129
}
128130
}
129131

130-
/// Emitted when `account` membership or `roleId` is revoked. Unlike granting, revoking is instantaneous.
132+
/// Emitted when `account` membership or `roleId` is revoked. Unlike granting, revoking is
133+
/// instantaneous.
131134
///
132135
/// ```solidity
133136
/// event RoleRevoked(uint64 indexed roleId, address indexed account);

cosmwasm/cw-manager/src/lib.rs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,41 @@
22
//!
33
//! AccessManager is a central contract to store the permissions of a system.
44
//!
5-
//! A smart contract under the control of an AccessManager instance is known as a target, and will inherit from the
6-
//! {AccessManaged} contract, be connected to this contract as its manager and implement the {AccessManaged-restricted}
7-
//! modifier on a set of functions selected to be permissioned. Note that any function without this setup won't be
8-
//! effectively restricted.
5+
//! A smart contract under the control of an AccessManager instance is known as a target, and will
6+
//! inherit from the {AccessManaged} contract, be connected to this contract as its manager and
7+
//! implement the {AccessManaged-restricted} modifier on a set of functions selected to be
8+
//! permissioned. Note that any function without this setup won't be effectively restricted.
99
//!
10-
//! The restriction rules for such functions are defined in terms of "roles" identified by a [`RoleId`] and scoped
11-
//! by target ([`Addr`][cosmwasm_std::Addr]) and function selectors ([`Selector`][crate::types::Selector]). These roles are stored in this contract and can be
12-
//! configured by admins ([`RoleId::ADMIN_ROLE`] members) after a delay (see [`QueryMsg::GetTargetAdminDelay`]).
10+
//! The restriction rules for such functions are defined in terms of "roles" identified by a
11+
//! [`RoleId`] and scoped by target ([`Addr`][cosmwasm_std::Addr]) and function selectors
12+
//! ([`Selector`][crate::types::Selector]). These roles are stored in this contract and can be
13+
//! configured by admins ([`RoleId::ADMIN_ROLE`] members) after a delay (see
14+
//! [`QueryMsg::GetTargetAdminDelay`]).
1315
//!
1416
//! For each target contract, admins can configure the following without any delay:
1517
//!
1618
//! * The target's {AccessManaged-authority} via [`ExecuteMsg::UpdateAuthority`].
1719
//! * Close or open a target via [`ExecuteMsg::SetTargetClosed`] keeping the permissions intact.
18-
//! * The roles that are allowed (or disallowed) to call a given function (identified by its selector) through [`ExecuteMsg::SetTargetAdminDelay`].
20+
//! * The roles that are allowed (or disallowed) to call a given function (identified by its
21+
//! selector) through [`ExecuteMsg::SetTargetAdminDelay`].
1922
//!
20-
//! By default every address is member of the [`RoleId::PUBLIC_ROLE`] and every target function is restricted to the [`RoleId::ADMIN_ROLE`] until configured otherwise.
21-
//! Additionally, each role has the following configuration options restricted to this manager's admins:
23+
//! By default every address is member of the [`RoleId::PUBLIC_ROLE`] and every target function is
24+
//! restricted to the [`RoleId::ADMIN_ROLE`] until configured otherwise. Additionally, each role has
25+
//! the following configuration options restricted to this manager's admins:
2226
//!
2327
//! * A role's admin role via [`ExecuteMsg::SetRoleAdmin`] who can grant or revoke roles.
2428
//! * A role's guardian role via [`ExecuteMsg::SetRoleGuardian`] who's allowed to cancel operations.
25-
//! * A delay in which a role takes effect after being granted through [`ExecuteMsg::SetGrantDelay`].
29+
//! * A delay in which a role takes effect after being granted through
30+
//! [`ExecuteMsg::SetGrantDelay`].
2631
//! * A delay of any target's admin action via [`ExecuteMsg::SetTargetAdminDelay`].
2732
//! * A role label for discoverability purposes with [`ExecuteMsg::LabelRole`].
2833
//!
29-
//! Any account can be added and removed into any number of these roles by using the [`ExecuteMsg::GrantRole`] and [`ExecuteMsg::RevokeRole`] functions
30-
//! restricted to each role's admin (see [`QueryMsg::GetRoleAdmin`]).
34+
//! Any account can be added and removed into any number of these roles by using the
35+
//! [`ExecuteMsg::GrantRole`] and [`ExecuteMsg::RevokeRole`] functions restricted to each role's
36+
//! admin (see [`QueryMsg::GetRoleAdmin`]).
3137
//!
32-
//! Since all the permissions of the managed system can be modified by the admins of this instance, it is expected that
33-
//! they will be highly secured (e.g., a multisig or a well-configured DAO).
38+
//! Since all the permissions of the managed system can be modified by the admins of this instance,
39+
//! it is expected that they will be highly secured (e.g., a multisig or a well-configured DAO).
3440
//!
3541
//! [am]: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v5.4.0/contracts/access/manager/AccessManager.sol
3642

0 commit comments

Comments
 (0)