Skip to content

Commit

Permalink
use go routines for verify vote count and endElectionAndFetchResults
Browse files Browse the repository at this point in the history
  • Loading branch information
mariajdab authored and p4u committed Nov 10, 2023
1 parent fe24e1a commit 8f7962f
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 47 deletions.
8 changes: 2 additions & 6 deletions cmd/end2endtest/ballot.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,13 +246,9 @@ func sendAndValidateVotes(e e2eElection, choices [][]int, expectedResults [][]*t
"n", e.config.nvotes, "time", time.Since(startTime),
"vps", float64(e.config.nvotes)/time.Since(startTime).Seconds())

if err := e.verifyVoteCount(nvotes); err != nil {
return fmt.Errorf("error in verifyVoteCount: %s", err)
}

elres, err := e.endElectionAndFetchResults()
elres, err := e.verifyAndEndElection(nvotes)
if err != nil {
return fmt.Errorf("error in electionAndFetchResults: %s", err)
return err
}

if !matchResults(elres.Results, expectedResults) {
Expand Down
6 changes: 1 addition & 5 deletions cmd/end2endtest/censusize.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,7 @@ func (t *E2EMaxCensusSizeElection) Run() error {
}

// one vote is not valid
if err := t.verifyVoteCount(t.config.nvotes - 1); err != nil {
return err
}

elres, err := t.endElectionAndFetchResults()
elres, err := t.verifyAndEndElection(t.config.nvotes - 1)
if err != nil {
return err
}
Expand Down
6 changes: 1 addition & 5 deletions cmd/end2endtest/csp.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,7 @@ func (t *E2ECSPElection) Run() error {
"n", c.nvotes, "time", time.Since(startTime),
"vps", int(float64(c.nvotes)/time.Since(startTime).Seconds()))

if err := t.verifyVoteCount(t.config.nvotes); err != nil {
return err
}

elres, err := t.endElectionAndFetchResults()
elres, err := t.verifyAndEndElection(t.config.nvotes)
if err != nil {
return err
}
Expand Down
10 changes: 2 additions & 8 deletions cmd/end2endtest/dynamicensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,15 +190,9 @@ func (t *E2EDynamicensusElection) Run() error {
log.Debugw("error expected when try to vote,", "error:", err)
}

if err := t.elections[0].verifyVoteCount(nvotes - 1); err != nil {
errCh <- fmt.Errorf("error in verifyVoteCount: %s", err)
return
}

elres, err := t.elections[0].endElectionAndFetchResults()
elres, err := t.elections[0].verifyAndEndElection(nvotes - 1)
if err != nil {
errCh <- fmt.Errorf("error in electionAndFetchResults: %s", err)
return
errCh <- err
}

expectedResults := [][]*types.BigInt{votesToBigInt(uint64(nvotes-2)*10, 10, 0)}
Expand Down
6 changes: 1 addition & 5 deletions cmd/end2endtest/encrypted.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,7 @@ func (t *E2EEncryptedElection) Run() error {
"n", t.config.nvotes, "time", time.Since(startTime),
"vps", int(float64(t.config.nvotes)/time.Since(startTime).Seconds()))

if err := t.verifyVoteCount(t.config.nvotes); err != nil {
return err
}

elres, err := t.endElectionAndFetchResults()
elres, err := t.verifyAndEndElection(t.config.nvotes)
if err != nil {
return err
}
Expand Down
42 changes: 39 additions & 3 deletions cmd/end2endtest/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -625,24 +625,25 @@ func cspGenProof(pid, voterKey []byte, csp *ethereum.SignKeys) (*apiclient.Censu

func (t *e2eElection) verifyVoteCount(nvotesExpected int) error {
startTime := time.Now()
api := t.api.Clone(t.config.accountPrivKeys[0])

// Wait for all the votes to be verified
for {
count, err := t.api.ElectionVoteCount(t.election.ElectionID)
count, err := api.ElectionVoteCount(t.election.ElectionID)
if err != nil {
log.Warn(err)
}
if count == uint32(nvotesExpected) {
break
}

if err := t.api.WaitUntilNextBlock(); err != nil {
if err := api.WaitUntilNextBlock(); err != nil {
return fmt.Errorf("timeout waiting for next block")
}

log.Infof("verified %d/%d votes", count, nvotesExpected)
if time.Since(startTime) > t.config.timeout {
log.Fatalf("timeout waiting for votes to be registered")
return fmt.Errorf("timeout waiting for votes to be registered")
}
}

Expand Down Expand Up @@ -673,6 +674,41 @@ func (t *e2eElection) endElectionAndFetchResults() (*vapi.ElectionResults, error
return results, nil
}

func (t *e2eElection) verifyAndEndElection(nvotesExpected int) (*vapi.ElectionResults, error) {
var wg sync.WaitGroup
var results *vapi.ElectionResults
errCh := make(chan error, 2)

wg.Add(1)
go func() {
defer wg.Done()
if err := t.verifyVoteCount(nvotesExpected); err != nil {
errCh <- fmt.Errorf("error in verifyVoteCount %w", err)
}
}()

wg.Add(1)
go func() {
defer wg.Done()
elres, err := t.endElectionAndFetchResults()
if err != nil {
errCh <- fmt.Errorf("error in endElectionAndFetchResults %w", err)
}
results = elres
}()

wg.Wait()
close(errCh)

for err := range errCh {
if err != nil {
return &vapi.ElectionResults{}, err
}
}

return results, nil
}

func (t *e2eElection) registerAnonAccts(voterAccounts []*ethereum.SignKeys) error {
errorChan := make(chan error)
wg := &sync.WaitGroup{}
Expand Down
6 changes: 1 addition & 5 deletions cmd/end2endtest/overwrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,7 @@ func (t *E2EOverwriteElection) Run() error {
}
log.Infof("the account %v send an overwrite vote", votes[0].VoterAccount.Address())

if err := t.verifyVoteCount(t.config.nvotes); err != nil {
return err
}

elres, err := t.endElectionAndFetchResults()
elres, err := t.verifyAndEndElection(t.config.nvotes)
if err != nil {
return err
}
Expand Down
6 changes: 1 addition & 5 deletions cmd/end2endtest/plaintext.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,7 @@ func (t *E2EPlaintextElection) Run() error {
"n", c.nvotes, "time", time.Since(startTime),
"vps", int(float64(c.nvotes)/time.Since(startTime).Seconds()))

if err := t.verifyVoteCount(t.config.nvotes); err != nil {
return err
}

elres, err := t.endElectionAndFetchResults()
elres, err := t.verifyAndEndElection(t.config.nvotes)
if err != nil {
return err
}
Expand Down
6 changes: 1 addition & 5 deletions cmd/end2endtest/zkweighted.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,7 @@ func (t *E2EAnonElectionTempSIKs) Run() error {
"n", t.config.nvotes, "time", time.Since(startTime),
"vps", int(float64(t.config.nvotes)/time.Since(startTime).Seconds()))

if err := t.verifyVoteCount(t.config.nvotes); err != nil {
return err
}

elres, err := t.endElectionAndFetchResults()
elres, err := t.verifyAndEndElection(t.config.nvotes)
if err != nil {
return err
}
Expand Down

0 comments on commit 8f7962f

Please sign in to comment.