From 92c9a5aa756a2e8f2405536d057756cdb34b7027 Mon Sep 17 00:00:00 2001 From: Gui Iribarren Date: Wed, 19 Apr 2023 09:12:09 -0300 Subject: [PATCH] e2etest: drop redundant struct electionDescpInfo and var anonymousVoting --- cmd/end2endtest/encrypted.go | 19 +++++++------ cmd/end2endtest/helpers.go | 52 ++++++++--------------------------- cmd/end2endtest/plaintext.go | 17 ++++++------ cmd/end2endtest/zkweighted.go | 19 +++++++------ 4 files changed, 40 insertions(+), 67 deletions(-) diff --git a/cmd/end2endtest/encrypted.go b/cmd/end2endtest/encrypted.go index 1bf32a06d..33d0a8ff3 100644 --- a/cmd/end2endtest/encrypted.go +++ b/cmd/end2endtest/encrypted.go @@ -34,15 +34,16 @@ func (t *E2EEncryptedElection) Setup(api *apiclient.HTTPclient, c *config) error t.api = api t.config = c - if err := t.setupElection(electionDescpInfo{ - electType: vapi.ElectionType{ - Autostart: true, - Interruptible: true, - SecretUntilTheEnd: true, - }, - voteType: vapi.VoteType{MaxVoteOverwrites: 1}, - censusType: vapi.CensusTypeWeighted, - }); err != nil { + ed := newTestElectionDescription() + ed.ElectionType = vapi.ElectionType{ + Autostart: true, + Interruptible: true, + SecretUntilTheEnd: true, + } + ed.VoteType = vapi.VoteType{MaxVoteOverwrites: 1} + ed.Census = vapi.CensusTypeDescription{Type: vapi.CensusTypeWeighted} + + if err := t.setupElection(ed); err != nil { return err } diff --git a/cmd/end2endtest/helpers.go b/cmd/end2endtest/helpers.go index 8ea9e188e..e66dca83e 100644 --- a/cmd/end2endtest/helpers.go +++ b/cmd/end2endtest/helpers.go @@ -4,11 +4,12 @@ import ( "context" "encoding/hex" "fmt" - "go.vocdoni.io/dvote/util" "math/big" "sync" "time" + "go.vocdoni.io/dvote/util" + vapi "go.vocdoni.io/dvote/api" "go.vocdoni.io/dvote/apiclient" "go.vocdoni.io/dvote/crypto/ethereum" @@ -18,32 +19,12 @@ import ( "go.vocdoni.io/proto/build/go/models" ) -type electionDescpInfo struct { - electType vapi.ElectionType - voteType vapi.VoteType - censusURI string - censusRoot types.HexBytes - censusType string - nvotes int -} - -func newElectionDescription(ed electionDescpInfo) *vapi.ElectionDescription { +func newTestElectionDescription() *vapi.ElectionDescription { return &vapi.ElectionDescription{ Title: map[string]string{"default": fmt.Sprintf("Test election %s", util.RandomHex(8))}, Description: map[string]string{"default": "Test election description"}, EndDate: time.Now().Add(time.Minute * 20), - VoteType: ed.voteType, - - ElectionType: ed.electType, - - Census: vapi.CensusTypeDescription{ - RootHash: ed.censusRoot, - URL: ed.censusURI, - Type: ed.censusType, - Size: uint64(ed.nvotes), - }, - Questions: []vapi.Question{ { Title: map[string]string{"default": "Test question 1"}, @@ -238,9 +219,7 @@ func (t e2eElection) generateProofs(root types.HexBytes, isAnonymousVoting bool) return proofs } -func (t *e2eElection) setupElection(ed electionDescpInfo) error { - anonymousVoting := false - +func (t *e2eElection) setupElection(ed *vapi.ElectionDescription) error { // Set the account in the API client, so we can sign transactions if err := t.api.SetAccount(hex.EncodeToString(t.config.accountKeys[0].PrivateKey())); err != nil { return err @@ -259,7 +238,7 @@ func (t *e2eElection) setupElection(ed electionDescpInfo) error { log.Infof("account %s balance is %d", t.api.MyAddress().Hex(), acc.Balance) // Create a new census - censusID, err := t.api.NewCensus(ed.censusType) + censusID, err := t.api.NewCensus(ed.Census.Type) if err != nil { fmt.Println("error new census") return err @@ -270,7 +249,7 @@ func (t *e2eElection) setupElection(ed electionDescpInfo) error { t.voterAccounts = ethereum.NewSignKeysBatch(t.config.nvotes) // Add the accounts to the census by batches - if err := t.addParticipantsCensus(ed.censusType, censusID); err != nil { + if err := t.addParticipantsCensus(ed.Census.Type, censusID); err != nil { fmt.Println("error addding participants") return err } @@ -288,6 +267,9 @@ func (t *e2eElection) setupElection(ed electionDescpInfo) error { return err } log.Infof("census published with root %s", root.String()) + ed.Census.RootHash = root + ed.Census.URL = censusURI + ed.Census.Size = uint64(t.config.nvotes) // Check census size (of the published census) if !t.isCensusSizeValid(root) { @@ -295,15 +277,7 @@ func (t *e2eElection) setupElection(ed electionDescpInfo) error { return err } - // add useful information to be used in electionDescription - ed.censusRoot = root - ed.censusURI = censusURI - ed.nvotes = t.config.nvotes - - // Create a new Election - description := newElectionDescription(ed) - - t.election, err = t.createElection(description) + t.election, err = t.createElection(ed) if err != nil { fmt.Println("error create election") return err @@ -320,11 +294,7 @@ func (t *e2eElection) setupElection(ed electionDescpInfo) error { return err } - if ed.electType.Anonymous { - anonymousVoting = true - } - - t.proofs = t.generateProofs(root, anonymousVoting) + t.proofs = t.generateProofs(root, ed.ElectionType.Anonymous) return nil } diff --git a/cmd/end2endtest/plaintext.go b/cmd/end2endtest/plaintext.go index 354897f38..f60372647 100644 --- a/cmd/end2endtest/plaintext.go +++ b/cmd/end2endtest/plaintext.go @@ -34,14 +34,15 @@ func (t *E2EPlaintextElection) Setup(api *apiclient.HTTPclient, c *config) error t.api = api t.config = c - if err := t.setupElection(electionDescpInfo{ - electType: vapi.ElectionType{ - Autostart: true, - Interruptible: true, - }, - voteType: vapi.VoteType{MaxVoteOverwrites: 1}, - censusType: vapi.CensusTypeWeighted, - }); err != nil { + ed := newTestElectionDescription() + ed.ElectionType = vapi.ElectionType{ + Autostart: true, + Interruptible: true, + } + ed.VoteType = vapi.VoteType{MaxVoteOverwrites: 1} + ed.Census = vapi.CensusTypeDescription{Type: vapi.CensusTypeWeighted} + + if err := t.setupElection(ed); err != nil { return err } diff --git a/cmd/end2endtest/zkweighted.go b/cmd/end2endtest/zkweighted.go index fc28cc2a3..6c882a859 100644 --- a/cmd/end2endtest/zkweighted.go +++ b/cmd/end2endtest/zkweighted.go @@ -33,15 +33,16 @@ func (t *E2EAnonElection) Setup(api *apiclient.HTTPclient, c *config) error { t.api = api t.config = c - if err := t.setupElection(electionDescpInfo{ - electType: vapi.ElectionType{ - Autostart: true, - Interruptible: true, - Anonymous: true, - }, - voteType: vapi.VoteType{MaxVoteOverwrites: 1}, - censusType: vapi.CensusTypeZKWeighted, - }); err != nil { + ed := newTestElectionDescription() + ed.ElectionType = vapi.ElectionType{ + Autostart: true, + Interruptible: true, + Anonymous: true, + } + ed.VoteType = vapi.VoteType{MaxVoteOverwrites: 1} + ed.Census = vapi.CensusTypeDescription{Type: vapi.CensusTypeZKWeighted} + + if err := t.setupElection(ed); err != nil { return err } log.Debugf("election details: %+v", *t.election)