-
Notifications
You must be signed in to change notification settings - Fork 1
/
results.go
276 lines (244 loc) · 9.86 KB
/
results.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package main
import (
"encoding/hex"
"fmt"
"math"
"math/big"
"net/http"
"strings"
"sync"
"time"
"github.com/vocdoni/vote-frame/airstack"
"github.com/vocdoni/vote-frame/communityhub"
"github.com/vocdoni/vote-frame/helpers"
"github.com/vocdoni/vote-frame/imageframe"
"github.com/vocdoni/vote-frame/mongo"
"go.vocdoni.io/dvote/api"
"go.vocdoni.io/dvote/httprouter"
"go.vocdoni.io/dvote/httprouter/apirest"
"go.vocdoni.io/dvote/log"
"go.vocdoni.io/dvote/types"
)
var resultsPNGgenerationMutex = sync.Mutex{}
// checkIfElectionFinishedAndHandle checks if the election is finished and if so, sends the final results.
// Returns true if the election is finished and the response was sent, false otherwise.
// The caller should return immediately after this function returns true.
func (v *vocdoniHandler) checkIfElectionFinishedAndHandle(electionID types.HexBytes, ctx *httprouter.HTTPContext) bool {
pngResults := v.db.FinalResultsPNG(electionID)
if pngResults == nil {
return false
}
response := strings.ReplaceAll(frame(frameFinalResults), "{image}", imageLink(imageframe.AddImageToCache(pngResults)))
response = strings.ReplaceAll(response, "{processID}", electionID.String())
response = strings.ReplaceAll(response, "{title}", "Final results")
ctx.SetResponseContentType("text/html; charset=utf-8")
if err := ctx.Send([]byte(response), http.StatusOK); err != nil {
log.Warnw("failed to send response", "error", err)
return true
}
return true
}
func (v *vocdoniHandler) results(msg *apirest.APIdata, ctx *httprouter.HTTPContext) error {
electionID := ctx.URLParam("electionID")
if len(electionID) == 0 {
return errorImageResponse(ctx, fmt.Errorf("invalid electionID"))
}
log.Infow("received results request", "electionID", electionID)
// validate the frame package to airstack
if v.airstack != nil {
airstack.ValidateFrameMessage(msg.Data, v.airstack.ApiKey())
}
electionIDbytes, err := hex.DecodeString(electionID)
if err != nil {
return errorImageResponse(ctx, fmt.Errorf("failed to decode electionID: %w", err))
}
// check if the election is finished and if so, send the final results as a static PNG
if v.checkIfElectionFinishedAndHandle(electionIDbytes, ctx) {
return nil
}
// get the election from the vochain and create a PNG image with the results
election, err := v.cli.Election(electionIDbytes)
if err != nil {
return errorImageResponse(ctx, fmt.Errorf("failed to fetch election: %w", err))
}
metadata := helpers.UnpackMetadata(election.Metadata)
if len(election.Results) == 0 {
return errorImageResponse(ctx, fmt.Errorf("election results not ready"))
}
electiondb, err := v.db.Election(electionIDbytes)
if err != nil {
log.Warnw("failed to fetch election from database", "error", err)
}
// if final results, create the static PNG image with the results
if election.FinalResults {
id, err := v.finalizeElectionResults(election, electiondb)
if err != nil {
return errorImageResponse(ctx, fmt.Errorf("failed to create final results: %w", err))
}
response := strings.ReplaceAll(frame(frameFinalResults), "{image}", imageLink(id))
response = strings.ReplaceAll(response, "{processID}", electionID)
response = strings.ReplaceAll(response, "{title}", "Final results")
ctx.SetResponseContentType("text/html; charset=utf-8")
return ctx.Send([]byte(response), http.StatusOK)
} else {
_, err := v.updateAndFetchResultsFromDatabase(electionIDbytes, election)
if err != nil {
return fmt.Errorf("failed to update/fetch results: %w", err)
}
}
totalWeightStr := ""
census, err := v.db.CensusFromElection(electionIDbytes)
if err == nil {
totalWeightStr = census.TotalWeight
}
// if not final results, create the dynamic PNG image with the results
response := strings.ReplaceAll(frame(frameResults), "{image}", resultsPNGfile(election, electiondb, totalWeightStr))
response = strings.ReplaceAll(response, "{title}", metadata.Title["default"])
response = strings.ReplaceAll(response, "{processID}", electionID)
ctx.SetResponseContentType("text/html; charset=utf-8")
return ctx.Send([]byte(response), http.StatusOK)
}
// finalizeElectionResults creates the final results image and stores it in the database.
// election is mandatory but electiondb is optional. Some features may not work if electiondb is nil.
// Returns the imageID of the final results image.
func (v *vocdoniHandler) finalizeElectionResults(election *api.Election, electiondb *mongo.Election) (imageID string, err error) {
if election == nil || election.Metadata == nil {
return "", fmt.Errorf("nil election or missing parameters")
}
if !election.FinalResults {
return "", fmt.Errorf("election not finalized")
}
totalWeightStr := ""
census, err := v.db.CensusFromElection(election.ElectionID)
if err == nil {
totalWeightStr = census.TotalWeight
}
id, err := imageframe.ResultsImage(election, electiondb, totalWeightStr)
if err != nil {
return "", fmt.Errorf("failed to create image: %w", err)
}
go func() {
choices, votes := helpers.ExtractResults(election, 0)
if err := v.db.AddFinalResults(election.ElectionID, imageframe.FromCache(id), choices, helpers.BigIntsToStrings(votes)); err != nil {
log.Errorw(err, "failed to add final results to database")
return
}
if electiondb != nil {
if err := v.settleResultsIntoCommunityHub(electiondb, choices, votes); err != nil {
log.Errorw(err, "failed to settle results into community hub")
}
}
}()
return id, nil
}
func (v *vocdoniHandler) settleResultsIntoCommunityHub(electiondb *mongo.Election, choices []string, votes []*big.Int) error {
if len(votes) == 0 || len(choices) == 0 {
return fmt.Errorf("invalid votes/choices")
}
if electiondb == nil {
return fmt.Errorf("nil electiondb")
}
// check if the election is from a community, else return silently
if electiondb.Community == nil {
return nil
}
log.Debugw("settling results into community hub", "electionID", electiondb.ElectionID, "communityID", electiondb.Community.ID)
// load the community hub contract for the community
contract, err := v.comhub.CommunityContract(electiondb.Community.ID)
if err != nil || contract == nil {
return fmt.Errorf("failed to fetch community contract: %w", err)
}
// load the community from the community hub contract
comm, err := contract.Community(electiondb.Community.ID)
if err != nil || comm == nil {
return fmt.Errorf("failed to fetch community from the community hub: %w", err)
}
electionID, err := hex.DecodeString(electiondb.ElectionID)
if err != nil {
return fmt.Errorf("failed to decode electionID: %w", err)
}
// Extract the list of participants from the database
voters, err := v.db.VotersOfElection(electionID)
if err != nil {
return fmt.Errorf("failed to fetch voters from the database: %w", err)
}
participants := []*big.Int{}
for _, voter := range voters {
participants = append(participants, new(big.Int).SetUint64(voter.UserID))
}
// Transform the results into a format suitable for the community hub
tally := [][]*big.Int{votes}
// We need the census to calculate the turnout
census, err := v.db.CensusFromElection(electionID)
if err != nil {
return fmt.Errorf("failed to fetch census from the database for election %x: %w", electionID, err)
}
root, err := hex.DecodeString(census.Root)
if err != nil {
return fmt.Errorf("failed to decode census root: %w", err)
}
// Create a new big.Int from the truncated float
turnout := big.NewInt(int64(math.Trunc(float64(helpers.CalculateTurnout(census.TotalWeight, electiondb.CastedWeight)))))
// Extract the choices from the election results
totalVotingPower, _ := new(big.Int).SetString(census.TotalWeight, 10)
hubResults := &communityhub.HubResults{
Question: electiondb.Question,
Options: choices,
Date: time.Now().String(),
Tally: tally,
Turnout: turnout,
TotalVotingPower: totalVotingPower,
Participants: participants,
CensusRoot: root,
CensusURI: census.URL,
VoteCount: new(big.Int).SetUint64(electiondb.CastedVotes),
}
log.Infow("sending results transaction to community hub smart contract",
"electionID", electiondb.ElectionID,
"communityID", electiondb.Community.ID,
"hubResults", hubResults)
if err := contract.SetResults(comm, hubResults); err != nil {
return fmt.Errorf("failed to set results on the community hub: %w", err)
}
return nil
}
func resultsPNGfile(election *api.Election, electiondb *mongo.Election, totalWeightStr string) string {
resultsPNGgenerationMutex.Lock()
defer resultsPNGgenerationMutex.Unlock()
id, err := imageframe.ResultsImage(election, electiondb, totalWeightStr)
if err != nil {
log.Warnw("failed to create results image", "error", err)
return imageLink(imageframe.NotFoundImage())
}
return imageLink(id)
}
// updateAndFetchResultsFromDatabase updates the results on the database and returns them updated.
// It also updates the LRU cached election.
// If election is nil, it fetches it from the vochain API.
func (v *vocdoniHandler) updateAndFetchResultsFromDatabase(
electionID types.HexBytes,
election *api.Election,
) (*mongo.Results, error) {
if election == nil {
var err error
election, err = v.cli.Election(electionID)
if err != nil {
return nil, fmt.Errorf("failed to fetch election: %w", err)
}
}
// Update LRU cached election
_ = v.electionLRU.Add(fmt.Sprintf("%x", electionID), election)
// Update the results on the database
choices, votes := helpers.ExtractResults(election, 0)
votesString := helpers.BigIntsToStrings(votes)
log.Infow("updating partial results", "electionID", electionID.String(), "choices", choices, "votes", votesString)
if err := v.db.SetPartialResults(electionID, choices, votesString); err != nil {
return nil, fmt.Errorf("failed to update results: %w", err)
}
// Fetch results from the database to return them in the response
results, err := v.db.Results(electionID)
if err != nil {
return nil, fmt.Errorf("failed to fetch results: %w", err)
}
return results, nil
}