Skip to content

Commit

Permalink
e2etest: drop redundant struct electionDescpInfo and var anonymousVoting
Browse files Browse the repository at this point in the history
  • Loading branch information
altergui committed Apr 19, 2023
1 parent 75faaa2 commit 92c9a5a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 67 deletions.
19 changes: 10 additions & 9 deletions cmd/end2endtest/encrypted.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
52 changes: 11 additions & 41 deletions cmd/end2endtest/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"},
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
}
Expand All @@ -288,22 +267,17 @@ 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) {
fmt.Println("error is size valid 2")
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
Expand All @@ -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
}
Expand Down
17 changes: 9 additions & 8 deletions cmd/end2endtest/plaintext.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
19 changes: 10 additions & 9 deletions cmd/end2endtest/zkweighted.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 92c9a5a

Please sign in to comment.