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

Add getRoleMembers method to return all accounts that have role #4546

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
5 changes: 5 additions & 0 deletions .changeset/violet-moons-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'openzeppelin-solidity': minor
---

`AccessControlEnumerable`: add `getRoleMembers` method to return all accounts that have `role`
ernestognw marked this conversation as resolved.
Show resolved Hide resolved
12 changes: 12 additions & 0 deletions contracts/access/extensions/AccessControlEnumerable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ abstract contract AccessControlEnumerable is IAccessControlEnumerable, AccessCon
return _roleMembers[role].length();
}

/**
* @dev Return all accounts that have `role`
*
* WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
* to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
* this function has an unbounded cost, and using it as part of a state-changing function may render the function
* uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
*/
function getRoleMembers(bytes32 role) public view virtual returns (address[] memory) {
return _roleMembers[role].values();
}

/**
* @dev Overload {AccessControl-_grantRole} to track enumerable memberships
*/
Expand Down
7 changes: 5 additions & 2 deletions test/access/AccessControl.behavior.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,15 +238,18 @@ function shouldBehaveLikeAccessControlEnumerable() {
await this.mock.connect(this.defaultAdmin).grantRole(ROLE, this.otherAuthorized);
await this.mock.connect(this.defaultAdmin).revokeRole(ROLE, this.other);

const expectedMembers = [this.authorized.address, this.otherAuthorized.address];

const memberCount = await this.mock.getRoleMemberCount(ROLE);
expect(memberCount).to.equal(2);
expect(memberCount).to.equal(expectedMembers.length);

const bearers = [];
for (let i = 0; i < memberCount; ++i) {
bearers.push(await this.mock.getRoleMember(ROLE, i));
}

expect(bearers).to.have.members([this.authorized.address, this.otherAuthorized.address]);
expect(bearers).to.have.members(expectedMembers);
expect(await this.accessControl.getRoleMembers(ROLE)).to.have.members(expectedMembers);
});

it('role enumeration should be in sync after renounceRole call', async function () {
Expand Down
Loading