Skip to content

Commit

Permalink
apiclient: VoteData now has Election (instead of just ElectionID)
Browse files Browse the repository at this point in the history
this avoids refetching the election on every Vote
  • Loading branch information
altergui authored and p4u committed Oct 23, 2023
1 parent 4c8ca16 commit 9bdef4d
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 37 deletions.
24 changes: 11 additions & 13 deletions apiclient/vote.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import (
// (deprecated).
type VoteData struct {
Choices []int
ElectionID types.HexBytes
Election *api.Election
VoteWeight *big.Int

ProofMkTree *CensusProof
Expand All @@ -60,27 +60,25 @@ func (cl *HTTPclient) Vote(v *VoteData) (types.HexBytes, error) {
if v.VoterAccount != nil {
c = cl.Clone(hex.EncodeToString(v.VoterAccount.PrivateKey()))
}
election, err := c.Election(v.ElectionID)
if err != nil {
return nil, err
}

var vote *models.VoteEnvelope
var err error

if v.Keys != nil {
vote, err = c.voteEnvelopeWithKeys(v.Choices, v.Keys, election)
vote, err = c.voteEnvelopeWithKeys(v.Choices, v.Keys, v.Election)
} else {
vote, err = c.prepareVoteEnvelope(v.Choices, election)
vote, err = c.prepareVoteEnvelope(v.Choices, v.Election)
}
if err != nil {
return nil, err
}

log.Debugw("generating a new vote", "electionId", v.ElectionID, "voter", c.account.AddressString())
log.Debugw("generating a new vote", "electionId", v.Election.ElectionID, "voter", c.account.AddressString())
voteAPI := &api.Vote{}
censusOriginCSP := models.CensusOrigin_name[int32(models.CensusOrigin_OFF_CHAIN_CA)]
censusOriginWeighted := models.CensusOrigin_name[int32(models.CensusOrigin_OFF_CHAIN_TREE_WEIGHTED)]
switch {
case election.VoteMode.Anonymous:
case v.Election.VoteMode.Anonymous:
// support no vote weight provided
if v.VoteWeight == nil {
v.VoteWeight = v.ProofMkTree.LeafWeight
Expand All @@ -89,8 +87,8 @@ func (cl *HTTPclient) Vote(v *VoteData) (types.HexBytes, error) {
// information and encode it into a json
rawInputs, err := circuit.GenerateCircuitInput(circuit.CircuitInputsParameters{
Account: c.account,
ElectionId: election.ElectionID,
CensusRoot: election.Census.CensusRoot,
ElectionId: v.Election.ElectionID,
CensusRoot: v.Election.Census.CensusRoot,
SIKRoot: v.ProofSIKTree.Root,
CensusSiblings: v.ProofMkTree.Siblings,
SIKSiblings: v.ProofSIKTree.Siblings,
Expand Down Expand Up @@ -136,7 +134,7 @@ func (cl *HTTPclient) Vote(v *VoteData) (types.HexBytes, error) {
if err != nil {
return nil, fmt.Errorf("could not prepare vote transaction: %w", err)
}
case election.Census.CensusOrigin == censusOriginWeighted:
case v.Election.Census.CensusOrigin == censusOriginWeighted:
// support custom vote weight
var voteWeight []byte
if v.VoteWeight != nil {
Expand All @@ -160,7 +158,7 @@ func (cl *HTTPclient) Vote(v *VoteData) (types.HexBytes, error) {
if err != nil {
return nil, err
}
case election.Census.CensusOrigin == censusOriginCSP:
case v.Election.Census.CensusOrigin == censusOriginCSP:
// decode the CSP proof and include in a VoteEnvelope
p := models.ProofCA{}
if err := proto.Unmarshal(v.ProofCSP, &p); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/end2endtest/ballot.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func sendAndValidateVotes(e e2eElection, choices [][]int, expectedResults [][]*t
e.voters.Range(func(key, value any) bool {
if acctp, ok := value.(acctProof); ok {
votes = append(votes, &apiclient.VoteData{
ElectionID: e.election.ElectionID,
Election: e.election,
ProofMkTree: acctp.proof,
Choices: choices[vcount],
VoterAccount: acctp.account,
Expand Down
2 changes: 1 addition & 1 deletion cmd/end2endtest/censusize.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (t *E2EMaxCensusSizeElection) Run() error {
t.voters.Range(func(key, value any) bool {
if acctp, ok := value.(acctProof); ok {
votes = append(votes, &apiclient.VoteData{
ElectionID: t.election.ElectionID,
Election: t.election,
ProofMkTree: acctp.proof,
Choices: []int{0},
VoterAccount: acctp.account,
Expand Down
2 changes: 1 addition & 1 deletion cmd/end2endtest/csp.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (t *E2ECSPElection) Run() error {
t.voters.Range(func(key, value any) bool {
if acctp, ok := value.(acctProof); ok {
votes = append(votes, &apiclient.VoteData{
ElectionID: t.election.ElectionID,
Election: t.election,
ProofCSP: acctp.proof.Proof,
Choices: []int{0},
VoterAccount: acctp.account,
Expand Down
14 changes: 7 additions & 7 deletions cmd/end2endtest/dynamicensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (t *E2EDynamicensusElection) Run() error {
}

v := apiclient.VoteData{
ElectionID: election.election.ElectionID,
Election: election.election,
ProofMkTree: proof,
VoterAccount: vAccts[0],
Choices: []int{1},
Expand Down Expand Up @@ -116,7 +116,7 @@ func (t *E2EDynamicensusElection) Run() error {
t.elections[0].voters.Range(func(key, value any) bool {
if acctp, ok := value.(acctProof); ok {
votes = append(votes, &apiclient.VoteData{
ElectionID: t.elections[0].election.ElectionID,
Election: t.elections[0].election,
ProofMkTree: acctp.proof,
Choices: []int{0},
VoterAccount: acctp.account,
Expand Down Expand Up @@ -225,7 +225,7 @@ func (t *E2EDynamicensusElection) Run() error {
defer wg.Done()

api := t.elections[1].api
electionID := t.elections[1].election.ElectionID
election := t.elections[1].election

// Send the votes (parallelized)
startTime := time.Now()
Expand All @@ -236,7 +236,7 @@ func (t *E2EDynamicensusElection) Run() error {
t.elections[1].voters.Range(func(key, value any) bool {
if acctp, ok := value.(acctProof); ok {
votes = append(votes, &apiclient.VoteData{
ElectionID: electionID,
Election: election,
ProofMkTree: acctp.proof,
Choices: []int{0},
VoterAccount: acctp.account,
Expand All @@ -247,7 +247,7 @@ func (t *E2EDynamicensusElection) Run() error {

errs := t.elections[1].sendVotes(votes[1:])
if len(errs) > 0 {
errCh <- fmt.Errorf("error from electionID: %s, %+v", electionID, errs)
errCh <- fmt.Errorf("error from electionID: %s, %+v", election.ElectionID, errs)
return
}

Expand All @@ -259,13 +259,13 @@ func (t *E2EDynamicensusElection) Run() error {
var err error

if censusRoot2, _, err = setupNewCensusAndVote(t.elections[1]); err != nil {
errCh <- fmt.Errorf("unexpected error from electionID: %s, error: %s", electionID, err)
errCh <- fmt.Errorf("unexpected error from electionID: %s, error: %s", election.ElectionID, err)
return
}

log.Debugf("election details before: %s %s %x", t.elections[1].election.Census.CensusOrigin, t.elections[1].election.Census.CensusURL, t.elections[1].election.Census.CensusRoot)

if _, err := api.TransactionSetCensus(electionID, vapi.ElectionCensus{
if _, err := api.TransactionSetCensus(election.ElectionID, vapi.ElectionCensus{
CensusOrigin: "OFF_CHAIN_TREE_WEIGHTED",
CensusRoot: censusRoot2,
CensusURL: "http://test/census",
Expand Down
2 changes: 1 addition & 1 deletion cmd/end2endtest/encrypted.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (t *E2EEncryptedElection) Run() error {
t.voters.Range(func(key, value any) bool {
if acctp, ok := value.(acctProof); ok {
votes = append(votes, &apiclient.VoteData{
ElectionID: t.election.ElectionID,
Election: t.election,
ProofMkTree: acctp.proof,
Choices: []int{vcount % 2},
VoterAccount: acctp.account,
Expand Down
4 changes: 2 additions & 2 deletions cmd/end2endtest/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,8 +504,8 @@ func (t *e2eElection) sendVotes(votes []*apiclient.VoteData) map[int]error {
queues = append(queues, make(map[int]*apiclient.VoteData, len(votes)))
}
for i, v := range votes {
if v.ElectionID == nil {
v.ElectionID = t.election.ElectionID
if v.Election == nil {
v.Election = t.election
}
queues[i%t.config.parallelCount][i] = v
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/end2endtest/overwrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (t *E2EOverwriteElection) Run() error {
t.voters.Range(func(key, value any) bool {
if acctp, ok := value.(acctProof); ok {
votes = append(votes, &apiclient.VoteData{
ElectionID: t.election.ElectionID,
Election: t.election,
ProofMkTree: acctp.proof,
Choices: []int{0},
VoterAccount: acctp.account,
Expand Down
2 changes: 1 addition & 1 deletion cmd/end2endtest/plaintext.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (t *E2EPlaintextElection) Run() error {
t.voters.Range(func(key, value any) bool {
if acctp, ok := value.(acctProof); ok {
votes = append(votes, &apiclient.VoteData{
ElectionID: t.election.ElectionID,
Election: t.election,
ProofMkTree: acctp.proof,
Choices: []int{vcount % 2},
VoterAccount: acctp.account,
Expand Down
2 changes: 1 addition & 1 deletion cmd/end2endtest/race.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (t *E2ERaceDuringCommit) Run() error {
t.voters.Range(func(key, value any) bool {
if acctp, ok := value.(acctProof); ok {
votes = append(votes, &apiclient.VoteData{
ElectionID: t.election.ElectionID,
Election: t.election,
ProofMkTree: acctp.proof,
Choices: []int{vcount % 2},
VoterAccount: acctp.account,
Expand Down
4 changes: 2 additions & 2 deletions cmd/end2endtest/zkweighted.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (t *E2EAnonElection) Run() error {
t.voters.Range(func(key, value any) bool {
if acctp, ok := value.(acctProof); ok {
votes = append(votes, &apiclient.VoteData{
ElectionID: t.election.ElectionID,
Election: t.election,
ProofMkTree: acctp.proof,
ProofSIKTree: acctp.proofSIK,
Choices: []int{vcount % 2},
Expand Down Expand Up @@ -137,7 +137,7 @@ func (t *E2EAnonElectionTempSIKs) Run() error {
t.voters.Range(func(key, value any) bool {
if acctp, ok := value.(acctProof); ok {
votes = append(votes, &apiclient.VoteData{
ElectionID: t.election.ElectionID,
Election: t.election,
ProofMkTree: acctp.proof,
ProofSIKTree: acctp.proofSIK,
Choices: []int{vcount % 2},
Expand Down
13 changes: 7 additions & 6 deletions vocone/vocone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,19 +115,20 @@ func testCSPvote(cli *apiclient.HTTPclient) error {
}

// Wait until the process is ready
info, err := cli.ChainInfo()
ctx1, cancel1 := context.WithTimeout(context.Background(), time.Second*30)
defer cancel1()
election, err := cli.WaitUntilElectionStatus(ctx1, processID, "READY")
if err != nil {
return err
}
cli.WaitUntilHeight(context.Background(), info.Height+2)

// Send the votes
for i, k := range voterKeys {
c := cli.Clone(fmt.Sprintf("%x", k.PrivateKey()))
c.Vote(&apiclient.VoteData{
Choices: []int{1},
ElectionID: processID,
ProofCSP: proofs[i],
Choices: []int{1},
Election: election,
ProofCSP: proofs[i],
})
}

Expand All @@ -136,7 +137,7 @@ func testCSPvote(cli *apiclient.HTTPclient) error {
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
election, err := cli.WaitUntilElectionStatus(ctx, processID, "RESULTS")
election, err = cli.WaitUntilElectionStatus(ctx, processID, "RESULTS")
if err != nil {
return err
}
Expand Down

0 comments on commit 9bdef4d

Please sign in to comment.