Skip to content

Commit 8e6e62e

Browse files
committed
Add countGroupMembership to operator contract statistics
1 parent 4563dd7 commit 8e6e62e

1 file changed

Lines changed: 23 additions & 10 deletions

File tree

solidity/contracts/statistics/KeepRandomBeaconOperatorStatistics.sol

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,37 @@ contract KeepRandomBeaconOperatorStatistics {
1414
operatorContract = KeepRandomBeaconOperator(_operatorContract);
1515
}
1616

17+
/// @notice Counts how many times the operator is present in a group.
18+
/// @param groupPubKey The public key of the group.
19+
/// @param operator The address of the operator.
20+
/// @return The number of members the operator has in the group.
21+
function countGroupMembership(
22+
bytes memory groupPubKey,
23+
address operator
24+
) public view returns (uint256) {
25+
address[] memory members = operatorContract.getGroupMembers(groupPubKey);
26+
uint256 counter;
27+
for (uint i = 0; i < members.length; i++) {
28+
if (members[i] == operator) {
29+
counter++;
30+
}
31+
}
32+
return counter;
33+
}
34+
35+
1736
/**
1837
* @dev Gets all indices in the provided group for a member.
1938
*/
2039
function getGroupMemberIndices(
2140
bytes memory groupPubKey,
2241
address member
2342
) public view returns (uint256[] memory indices) {
43+
uint256 count = countGroupMembership(groupPubKey, member);
2444
address[] memory members = operatorContract.getGroupMembers(groupPubKey);
2545

26-
uint256 counter;
27-
for (uint i = 0; i < members.length; i++) {
28-
if (members[i] == member) {
29-
counter++;
30-
}
31-
}
32-
33-
indices = new uint256[](counter);
34-
counter = 0;
46+
indices = new uint256[](count);
47+
uint256 counter = 0;
3548
for (uint i = 0; i < members.length; i++) {
3649
if (members[i] == member) {
3750
indices[counter] = i;
@@ -50,7 +63,7 @@ contract KeepRandomBeaconOperatorStatistics {
5063
bytes memory groupPubKey = operatorContract.getGroupPublicKey(groupIndex);
5164
uint256 memberRewards = operatorContract.getGroupMemberRewards(groupPubKey);
5265

53-
uint256 memberCount = getGroupMemberIndices(groupPubKey, operator).length;
66+
uint256 memberCount = countGroupMembership(groupPubKey, operator);
5467

5568
return memberRewards.mul(memberCount);
5669
}

0 commit comments

Comments
 (0)