Skip to content

Commit

Permalink
end2endtest: validate that at the end of the election the half of the…
Browse files Browse the repository at this point in the history
… accounts don't have a valid SIK, due they are using temporal SIK
  • Loading branch information
mariajdab committed Nov 15, 2023
1 parent 22f46ef commit e0edd17
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
11 changes: 11 additions & 0 deletions cmd/end2endtest/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,17 @@ func (t *e2eElection) setupElection(ed *vapi.ElectionDescription, nvAccts int) e
log.Errorf("gave up waiting for tx %x to be mined: %s", hash, err)
errorChan <- err
}

// check if the current account has a valid SIK
validSik, err := accountApi.ValidSIK()
if err != nil {
errorChan <- fmt.Errorf("unexpected error in account %s, when validate SIK, %s", acc.Address(), err)
}
if !validSik {
errorChan <- fmt.Errorf("unexpected invalid SIK for account %x", acc.Address())
}
log.Infof("valid SIK for the account %x", acc.Address())

}(i, acc)
}

Expand Down
54 changes: 54 additions & 0 deletions cmd/end2endtest/zkweighted.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"fmt"
"math/big"
"os"
"strings"
"sync"
"sync/atomic"
"time"

vapi "go.vocdoni.io/dvote/api"
Expand Down Expand Up @@ -113,6 +116,7 @@ func (t *E2EAnonElectionTempSIKs) Setup(api *apiclient.HTTPclient, c *config) er
}
ed.VoteType = vapi.VoteType{MaxVoteOverwrites: 1}
ed.Census = vapi.CensusTypeDescription{Type: vapi.CensusTypeZKWeighted}
// use temporal siks
ed.TempSIKs = true

if err := t.setupElection(ed, t.config.nvotes); err != nil {
Expand Down Expand Up @@ -163,8 +167,58 @@ func (t *E2EAnonElectionTempSIKs) Run() error {
return err
}

// the half of accounts should not have valid sik, due they are using temporal sik
halfAccts := len(votes) / 2
accs, err := removedSiks(t.api, votes)
if err != nil {
return err
}

if int(accs) != halfAccts {
return fmt.Errorf("unexpected number of accounts without valid SIK, got %d want %d", accs, halfAccts)
}

log.Infof("election %s status is RESULTS", t.election.ElectionID.String())
log.Infof("election results: %v", elres.Results)

return nil
}

// removedSiks get the number of accounts without a valid SIK
func removedSiks(api *apiclient.HTTPclient, votes []*apiclient.VoteData) (accts int32, err error) {
wg := &sync.WaitGroup{}
errorChan := make(chan error)
var siksRemoved int32

for _, v := range votes {
wg.Add(1)
go func(v *apiclient.VoteData) {
defer wg.Done()
privKey := v.VoterAccount.PrivateKey()
accountApi := api.Clone(privKey.String())
valid, err := accountApi.ValidSIK()

if err != nil {
if !strings.Contains(err.Error(), "SIK not found") {
errorChan <- fmt.Errorf("unexpected SIK validation error for account: %x, %s", v.VoterAccount.Address(), err)
}
atomic.AddInt32(&siksRemoved, 1)
log.Infof("SIK removed for account %x", v.VoterAccount.Address())

} else if valid {
log.Infof("valid SIK for account: %x", v.VoterAccount.Address())
}
}(v)
}
go func() { // avoid blocking the main goroutine
wg.Wait()
close(errorChan) // close the error channel after all goroutines have finished
}()

for err := range errorChan { // receive errors from the errorChan until it's closed.
if err != nil {
return 0, err
}
}
return siksRemoved, nil
}

0 comments on commit e0edd17

Please sign in to comment.