Skip to content

Commit

Permalink
e2etest: refactor generateProofs, use buffered chan
Browse files Browse the repository at this point in the history
  • Loading branch information
altergui committed Sep 19, 2023
1 parent b17322a commit 28db33c
Showing 1 changed file with 11 additions and 17 deletions.
28 changes: 11 additions & 17 deletions cmd/end2endtest/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,21 +242,11 @@ func (t *e2eElection) generateProofs(root types.HexBytes, isAnonymousVoting bool
proof *apiclient.CensusProof
address string
}
proofs := make(map[string]*apiclient.CensusProof, len(t.voterAccounts))
proofCh := make(chan *voterProof)
stopProofs := make(chan bool)
go func() {
for {
select {
case p := <-proofCh:
proofs[p.address] = p.proof
case <-stopProofs:
return
}
}
}()
proofCh := make(chan *voterProof, len(t.voterAccounts))

addNaccounts := func(accounts []*ethereum.SignKeys, wg *sync.WaitGroup) {
var wg sync.WaitGroup

addNaccounts := func(accounts []*ethereum.SignKeys) {
defer wg.Done()
log.Infof("generating %d census proofs", len(accounts))
for _, acc := range accounts {
Expand Down Expand Up @@ -285,18 +275,22 @@ func (t *e2eElection) generateProofs(root types.HexBytes, isAnonymousVoting bool
}

pcount := t.config.nvotes / t.config.parallelCount
var wg sync.WaitGroup
for i := 0; i < len(t.voterAccounts); i += pcount {
end := i + pcount
if end > len(t.voterAccounts) {
end = len(t.voterAccounts)
}
wg.Add(1)
go addNaccounts(t.voterAccounts[i:end], &wg)
go addNaccounts(t.voterAccounts[i:end])
}

wg.Wait()
stopProofs <- true
close(proofCh)

proofs := make(map[string]*apiclient.CensusProof, len(t.voterAccounts))
for p := range proofCh {
proofs[p.address] = p.proof
}

log.Debugf("%d/%d census proofs generated successfully", len(proofs), len(t.voterAccounts))

Expand Down

0 comments on commit 28db33c

Please sign in to comment.