Skip to content

Commit

Permalink
Add pagination to obtain participants with/without transcripts.
Browse files Browse the repository at this point in the history
  • Loading branch information
derekpierre committed Jan 27, 2024
1 parent 63e6cd7 commit 6410e47
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
26 changes: 26 additions & 0 deletions contracts/contracts/coordination/Coordinator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,32 @@ contract Coordinator is Initializable, AccessControlDefaultAdminRulesUpgradeable
return (participant, index);
}

function getParticipants(
uint32 ritualId,
uint256 _startIndex,
uint256 _maxParticipants,
bool _includeTranscript
) external view returns (Participant[] memory) {
Ritual storage ritual = rituals[ritualId];
uint256 endIndex = ritual.participant.length;
require(_startIndex < endIndex, "Wrong start index");
if (_maxParticipants != 0 && _startIndex + _maxParticipants < endIndex) {
endIndex = _startIndex + _maxParticipants;
}
Participant[] memory ritualParticipants = new Participant[](endIndex - _startIndex);

uint256 resultIndex = 0;
for (uint256 i = _startIndex; i < endIndex; i++) {
Participant memory ritualParticipant = ritual.participant[i];
if (!_includeTranscript) {
ritualParticipant.transcript = "";
}
ritualParticipants[resultIndex++] = ritualParticipant;
}

return ritualParticipants;
}

function getProviders(uint32 ritualId) external view returns (address[] memory) {
Ritual storage ritual = rituals[ritualId];
address[] memory providers = new address[](ritual.participant.length);
Expand Down
46 changes: 46 additions & 0 deletions tests/test_coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,52 @@ def test_post_transcript_but_not_waiting_for_transcripts(
coordinator.postTranscript(0, transcript, sender=nodes[1])


def test_get_participants(coordinator, nodes, initiator, erc20, global_allow_list):
initiate_ritual(
coordinator=coordinator,
erc20=erc20,
authority=initiator,
nodes=nodes,
allow_logic=global_allow_list,
)
transcript = os.urandom(transcript_size(len(nodes), len(nodes)))

for node in nodes:
tx = coordinator.postTranscript(0, transcript, sender=node)

# get all participants
participants = coordinator.getParticipants(0, 0, len(nodes), False)
assert len(participants) == len(nodes)
for index, participant in enumerate(participants):
assert participant.provider == nodes[index].address
assert participant.aggregated is False
assert not participant.transcript

# max is higher than available
participants = coordinator.getParticipants(0, 0, len(nodes)*2, False)
assert len(participants) == len(nodes)
for index, participant in enumerate(participants):
assert participant.provider == nodes[index].address
assert participant.aggregated is False
assert not participant.transcript

# n at a time
for n_at_a_time in [2]:
index = 0
while index < len(nodes):
print(f">>>> Start Index {index}, End Index {index+n_at_a_time}")
participants_n_at_a_time = coordinator.getParticipants(0, index, index+n_at_a_time, True)
assert len(participants_n_at_a_time) <= n_at_a_time
for i, participant in enumerate(participants_n_at_a_time):
assert participant.provider == nodes[index+i].address
assert participant.aggregated is False
assert participant.transcript == transcript

index += n_at_a_time

assert index == len(nodes)


def test_post_aggregation(
coordinator, nodes, initiator, erc20, global_allow_list, treasury, deployer
):
Expand Down

0 comments on commit 6410e47

Please sign in to comment.