From 2503bfc35cc698b2d39aceb2ed11ef598b1181eb Mon Sep 17 00:00:00 2001 From: maria jose Date: Wed, 15 Nov 2023 06:29:15 -0400 Subject: [PATCH] end2endtest: validate that at the end of the election the half of the accounts don't have a valid SIK, due they are using temporal SIK --- cmd/end2endtest/helpers.go | 11 ++++++++ cmd/end2endtest/zkweighted.go | 53 +++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/cmd/end2endtest/helpers.go b/cmd/end2endtest/helpers.go index ea863a160..a13d71c08 100644 --- a/cmd/end2endtest/helpers.go +++ b/cmd/end2endtest/helpers.go @@ -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) } diff --git a/cmd/end2endtest/zkweighted.go b/cmd/end2endtest/zkweighted.go index f88e1d624..9354eccf3 100644 --- a/cmd/end2endtest/zkweighted.go +++ b/cmd/end2endtest/zkweighted.go @@ -4,6 +4,8 @@ import ( "fmt" "math/big" "os" + "strings" + "sync" "time" vapi "go.vocdoni.io/dvote/api" @@ -113,6 +115,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 { @@ -163,8 +166,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 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 int, err error) { + wg := &sync.WaitGroup{} + errorChan := make(chan error) + var siksRemoved int + + 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) + } + siksRemoved++ + 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 +}