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 all 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 a `getRoleMembers` method to return all accounts that have `role`.
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
12 changes: 7 additions & 5 deletions test/access/AccessControl.behavior.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,15 +239,17 @@ function shouldBehaveLikeAccessControlEnumerable() {
await this.mock.connect(this.defaultAdmin).grantRole(ROLE, this.otherAuthorized);
await this.mock.connect(this.defaultAdmin).revokeRole(ROLE, this.other);

const memberCount = await this.mock.getRoleMemberCount(ROLE);
expect(memberCount).to.equal(2);
const expectedMembers = [this.authorized.address, this.otherAuthorized.address];

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

expect(bearers).to.have.members([this.authorized.address, this.otherAuthorized.address]);
expect(memberCount).to.equal(expectedMembers.length);
expect(members).to.deep.equal(expectedMembers);
expect(await this.mock.getRoleMembers(ROLE)).to.deep.equal(expectedMembers);
});

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