Skip to content

Commit

Permalink
ticketvote: Cast vote error format.
Browse files Browse the repository at this point in the history
This commit changes the ErrorCode field in the CastVoteReply structs
to a pointer, in order to fix a bug where default error code value
ErrorCodeInvalid was returned on success.

After this, in case of success the error code pointer will be nil.
  • Loading branch information
amass01 authored Nov 22, 2021
1 parent 657a575 commit bc7da5b
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 50 deletions.
40 changes: 20 additions & 20 deletions politeiad/backendv2/tstorebe/plugins/ticketvote/cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -1315,7 +1315,7 @@ func (p *ticketVotePlugin) ballot(token []byte, votes []ticketvote.CastVote, br
"found %v", t)
e := ticketvote.VoteErrorInternalError
cvr.Ticket = v.Ticket
cvr.ErrorCode = e
cvr.ErrorCode = &e
cvr.ErrorContext = fmt.Sprintf("%v: %v",
ticketvote.VoteErrors[e], t)
goto saveReply
Expand Down Expand Up @@ -1347,7 +1347,7 @@ func (p *ticketVotePlugin) ballot(token []byte, votes []ticketvote.CastVote, br
"%v", t, err)
e := ticketvote.VoteErrorInternalError
cvr.Ticket = v.Ticket
cvr.ErrorCode = e
cvr.ErrorCode = &e
cvr.ErrorContext = fmt.Sprintf("%v: %v",
ticketvote.VoteErrors[e], t)
goto saveReply
Expand All @@ -1364,7 +1364,7 @@ func (p *ticketVotePlugin) ballot(token []byte, votes []ticketvote.CastVote, br
log.Errorf("cmdCastBallot: voteColliderSave %v: %v", t, err)
e := ticketvote.VoteErrorInternalError
cvr.Ticket = v.Ticket
cvr.ErrorCode = e
cvr.ErrorCode = &e
cvr.ErrorContext = fmt.Sprintf("%v: %v",
ticketvote.VoteErrors[e], t)
goto saveReply
Expand Down Expand Up @@ -1429,7 +1429,7 @@ func (p *ticketVotePlugin) cmdCastBallot(token []byte, payload string) (string,
if err != nil {
e := ticketvote.VoteErrorTokenInvalid
receipts[k].Ticket = v.Ticket
receipts[k].ErrorCode = e
receipts[k].ErrorCode = &e
receipts[k].ErrorContext = fmt.Sprintf("%v: not hex",
ticketvote.VoteErrors[e])
continue
Expand All @@ -1439,7 +1439,7 @@ func (p *ticketVotePlugin) cmdCastBallot(token []byte, payload string) (string,
if !bytes.Equal(t, token) {
e := ticketvote.VoteErrorMultipleRecordVotes
receipts[k].Ticket = v.Ticket
receipts[k].ErrorCode = e
receipts[k].ErrorCode = &e
receipts[k].ErrorContext = ticketvote.VoteErrors[e]
continue
}
Expand All @@ -1448,15 +1448,15 @@ func (p *ticketVotePlugin) cmdCastBallot(token []byte, payload string) (string,
if voteDetails == nil {
e := ticketvote.VoteErrorVoteStatusInvalid
receipts[k].Ticket = v.Ticket
receipts[k].ErrorCode = e
receipts[k].ErrorCode = &e
receipts[k].ErrorContext = fmt.Sprintf("%v: vote is "+
"not active", ticketvote.VoteErrors[e])
continue
}
if voteHasEnded(bestBlock, voteDetails.EndBlockHeight) {
e := ticketvote.VoteErrorVoteStatusInvalid
receipts[k].Ticket = v.Ticket
receipts[k].ErrorCode = e
receipts[k].ErrorCode = &e
receipts[k].ErrorContext = fmt.Sprintf("%v: vote has "+
"ended", ticketvote.VoteErrors[e])
continue
Expand All @@ -1467,7 +1467,7 @@ func (p *ticketVotePlugin) cmdCastBallot(token []byte, payload string) (string,
if err != nil {
e := ticketvote.VoteErrorVoteBitInvalid
receipts[k].Ticket = v.Ticket
receipts[k].ErrorCode = e
receipts[k].ErrorCode = &e
receipts[k].ErrorContext = ticketvote.VoteErrors[e]
continue
}
Expand All @@ -1476,7 +1476,7 @@ func (p *ticketVotePlugin) cmdCastBallot(token []byte, payload string) (string,
if err != nil {
e := ticketvote.VoteErrorVoteBitInvalid
receipts[k].Ticket = v.Ticket
receipts[k].ErrorCode = e
receipts[k].ErrorCode = &e
receipts[k].ErrorContext = fmt.Sprintf("%v: %v",
ticketvote.VoteErrors[e], err)
continue
Expand All @@ -1487,7 +1487,7 @@ func (p *ticketVotePlugin) cmdCastBallot(token []byte, payload string) (string,
if !ok {
e := ticketvote.VoteErrorTicketNotEligible
receipts[k].Ticket = v.Ticket
receipts[k].ErrorCode = e
receipts[k].ErrorCode = &e
receipts[k].ErrorContext = ticketvote.VoteErrors[e]
continue
}
Expand All @@ -1497,14 +1497,14 @@ func (p *ticketVotePlugin) cmdCastBallot(token []byte, payload string) (string,
if !isActive {
e := ticketvote.VoteErrorVoteStatusInvalid
receipts[k].Ticket = v.Ticket
receipts[k].ErrorCode = e
receipts[k].ErrorCode = &e
receipts[k].ErrorContext = fmt.Sprintf("%v: vote is "+
"not active", ticketvote.VoteErrors[e])
}
if isDup {
e := ticketvote.VoteErrorTicketAlreadyVoted
receipts[k].Ticket = v.Ticket
receipts[k].ErrorCode = e
receipts[k].ErrorCode = &e
receipts[k].ErrorContext = ticketvote.VoteErrors[e]
continue
}
Expand All @@ -1521,7 +1521,7 @@ func (p *ticketVotePlugin) cmdCastBallot(token []byte, payload string) (string,
// that are not found in the cache are fetched manually.
tickets := make([]string, 0, len(cb.Ballot))
for k, v := range votes {
if receipts[k].ErrorCode != ticketvote.VoteErrorInvalid {
if receipts[k].ErrorCode != nil {
// Vote has an error. Skip it.
continue
}
Expand Down Expand Up @@ -1554,7 +1554,7 @@ func (p *ticketVotePlugin) cmdCastBallot(token []byte, payload string) (string,

// Verify the signatures
for k, v := range votes {
if receipts[k].ErrorCode != ticketvote.VoteErrorInvalid {
if receipts[k].ErrorCode != nil {
// Vote has an error. Skip it.
continue
}
Expand All @@ -1567,7 +1567,7 @@ func (p *ticketVotePlugin) cmdCastBallot(token []byte, payload string) (string,
"%v: %v", t, v.Ticket)
e := ticketvote.VoteErrorInternalError
receipts[k].Ticket = v.Ticket
receipts[k].ErrorCode = e
receipts[k].ErrorCode = &e
receipts[k].ErrorContext = fmt.Sprintf("%v: %v",
ticketvote.VoteErrors[e], t)
continue
Expand All @@ -1578,7 +1578,7 @@ func (p *ticketVotePlugin) cmdCastBallot(token []byte, payload string) (string,
"%v %v", t, v.Ticket, commitmentAddr.err)
e := ticketvote.VoteErrorInternalError
receipts[k].Ticket = v.Ticket
receipts[k].ErrorCode = e
receipts[k].ErrorCode = &e
receipts[k].ErrorContext = fmt.Sprintf("%v: %v",
ticketvote.VoteErrors[e], t)
continue
Expand All @@ -1587,7 +1587,7 @@ func (p *ticketVotePlugin) cmdCastBallot(token []byte, payload string) (string,
if err != nil {
e := ticketvote.VoteErrorSignatureInvalid
receipts[k].Ticket = v.Ticket
receipts[k].ErrorCode = e
receipts[k].ErrorCode = &e
receipts[k].ErrorContext = fmt.Sprintf("%v: %v",
ticketvote.VoteErrors[e], err)
continue
Expand Down Expand Up @@ -1641,7 +1641,7 @@ func (p *ticketVotePlugin) cmdCastBallot(token []byte, payload string) (string,
ballotCount int
)
for k, v := range votes {
if receipts[k].ErrorCode != ticketvote.VoteErrorInvalid {
if receipts[k].ErrorCode != nil {
// Vote has an error. Skip it.
continue
}
Expand Down Expand Up @@ -1679,7 +1679,7 @@ func (p *ticketVotePlugin) cmdCastBallot(token []byte, payload string) (string,

// Fill in the receipts
for k, v := range votes {
if receipts[k].ErrorCode != ticketvote.VoteErrorInvalid {
if receipts[k].ErrorCode != nil {
// Vote has an error. Skip it.
continue
}
Expand All @@ -1690,7 +1690,7 @@ func (p *ticketVotePlugin) cmdCastBallot(token []byte, payload string) (string,
"%v", t, v.Ticket)
e := ticketvote.VoteErrorInternalError
receipts[k].Ticket = v.Ticket
receipts[k].ErrorCode = e
receipts[k].ErrorCode = &e
receipts[k].ErrorContext = fmt.Sprintf("%v: %v",
ticketvote.VoteErrors[e], t)
continue
Expand Down
4 changes: 2 additions & 2 deletions politeiad/plugins/ticketvote/ticketvote.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,8 +497,8 @@ type CastVoteReply struct {

// The follwing fields will only be present if an error occurred
// while attempting to cast the vote.
ErrorCode VoteErrorT `json:"errorcode,omitempty"`
ErrorContext string `json:"errorcontext,omitempty"`
ErrorCode *VoteErrorT `json:"errorcode,omitempty"`
ErrorContext string `json:"errorcontext,omitempty"`
}

// CastBallot casts a ballot of votes. A ballot can only contain votes for a
Expand Down
4 changes: 2 additions & 2 deletions politeiawww/api/ticketvote/v1/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,8 +446,8 @@ type CastVoteReply struct {

// The follwing fields will only be present if an error occurred
// while attempting to cast the vote.
ErrorCode VoteErrorT `json:"errorcode,omitempty"`
ErrorContext string `json:"errorcontext,omitempty"`
ErrorCode *VoteErrorT `json:"errorcode,omitempty"`
ErrorContext string `json:"errorcontext,omitempty"`
}

// CastBallot casts a ballot of votes. A ballot can only contain the votes for
Expand Down
2 changes: 1 addition & 1 deletion politeiawww/cmd/pictl/cmdcastballot.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func (c *cmdCastBallot) Execute(args []string) error {
h := eligibleTickets[i]

// Check for vote error
if v.ErrorContext != "" {
if v.ErrorCode != nil {
failedReceipts = append(failedReceipts, v)
failedTickets = append(failedTickets, h)
continue
Expand Down
18 changes: 5 additions & 13 deletions politeiawww/cmd/politeiavoter/trickle.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,11 @@ func waitRandom(min, max byte) time.Duration {

func (p *piv) voteTicket(ectx context.Context, bunchID, voteID, of int, va voteAlarm) error {
voteID++ // make human readable
//fmt.Printf("bunchID: %v voterID: %v at: %v\n", bunchID, voteID, va.At)

// Wait
err := WaitUntil(ectx, va.At)
if err != nil {
return fmt.Errorf("%v bunch %v vote %v failed: %v\n",
return fmt.Errorf("%v bunch %v vote %v failed: %v",
time.Now(), bunchID, voteID, err)
}

Expand Down Expand Up @@ -186,16 +185,9 @@ func (p *piv) voteTicket(ectx context.Context, bunchID, voteID, of int, va voteA
// Unrecoverable error
return fmt.Errorf("unrecoverable error: %v",
err)
} else {
// XXX TODO(lukebp) please make this a pointer and only
// evaluate these errors when it is set. For now we
// have to treat VoteErrorInvalid as valid because of
// this.
switch vr.ErrorCode {
// Success
case tkv1.VoteErrorInvalid:
// XXX treat as success for now

} else if vr.ErrorCode != nil {
// Evaluate errors when ErrorCode is set
switch *vr.ErrorCode {
// Silently ignore.
case tkv1.VoteErrorTicketAlreadyVoted:
// This happens during network errors. Since
Expand Down Expand Up @@ -257,7 +249,7 @@ func (p *piv) voteTicket(ectx context.Context, bunchID, voteID, of int, va voteA

return nil
}

} else {
// Success, log it and exit
err = p.jsonLog(successJournal, va.Vote.Token, vr)
if err != nil {
Expand Down
7 changes: 5 additions & 2 deletions politeiawww/legacy/proposals.go
Original file line number Diff line number Diff line change
Expand Up @@ -832,8 +832,11 @@ func convertVoteTypeToWWW(t tkplugin.VoteT) www.VoteT {
}
}

func convertVoteErrorCodeToWWW(e tkplugin.VoteErrorT) decredplugin.ErrorStatusT {
switch e {
func convertVoteErrorCodeToWWW(e *tkplugin.VoteErrorT) decredplugin.ErrorStatusT {
if e == nil {
return decredplugin.ErrorStatusInvalid
}
switch *e {
case tkplugin.VoteErrorInvalid:
return decredplugin.ErrorStatusInvalid
case tkplugin.VoteErrorInternalError:
Expand Down
27 changes: 17 additions & 10 deletions politeiawww/legacy/ticketvote/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,25 +391,32 @@ func convertVoteParamsToV1(v ticketvote.VoteParams) v1.VoteParams {
return vp
}

func convertVoteErrorToV1(e ticketvote.VoteErrorT) v1.VoteErrorT {
switch e {
func convertVoteErrorToV1(e *ticketvote.VoteErrorT) *v1.VoteErrorT {
if e == nil {
return nil
}

var ve v1.VoteErrorT
switch *e {
case ticketvote.VoteErrorInvalid:
return v1.VoteErrorInvalid
ve = v1.VoteErrorInvalid
case ticketvote.VoteErrorInternalError:
return v1.VoteErrorInternalError
ve = v1.VoteErrorInternalError
case ticketvote.VoteErrorRecordNotFound:
return v1.VoteErrorRecordNotFound
ve = v1.VoteErrorRecordNotFound
case ticketvote.VoteErrorVoteBitInvalid:
return v1.VoteErrorVoteBitInvalid
ve = v1.VoteErrorVoteBitInvalid
case ticketvote.VoteErrorVoteStatusInvalid:
return v1.VoteErrorVoteStatusInvalid
ve = v1.VoteErrorVoteStatusInvalid
case ticketvote.VoteErrorTicketAlreadyVoted:
return v1.VoteErrorTicketAlreadyVoted
ve = v1.VoteErrorTicketAlreadyVoted
case ticketvote.VoteErrorTicketNotEligible:
return v1.VoteErrorTicketNotEligible
ve = v1.VoteErrorTicketNotEligible
default:
return v1.VoteErrorInternalError
ve = v1.VoteErrorInternalError
}

return &ve
}

func convertCastVoteRepliesToV1(replies []ticketvote.CastVoteReply) []v1.CastVoteReply {
Expand Down

0 comments on commit bc7da5b

Please sign in to comment.