This repository has been archived by the owner on May 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
370 lines (316 loc) · 8.11 KB
/
main.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// main is main is main
package main
import (
"context"
"database/sql"
"encoding/json"
"io"
"log"
"net/http"
"os"
"sort"
"strings"
"time"
filaddr "github.com/filecoin-project/go-address"
"github.com/georgysavva/scany/sqlscan"
_ "github.com/mattn/go-sqlite3"
"golang.org/x/xerrors"
)
type ballot struct {
OptionID uint64
SignerAddress filaddr.Address
CreatedAt time.Time
}
const (
// ballotSource = `https://api.filpoll.io/api/polls/16/view-votes`
ballotSource = `https://w3s.link/ipfs/bafybeietprvjsf47sqs2gh7bfkanjbf3nig56jibqfgrijjqxiirgmg3we/fil_fip36_poll_ballots_obtained_morning_of_2022-09-29.json`
dbFn = `data/filstate_2162760.sqlite`
)
func main() {
ctx := context.Background()
if err := updateVotesInDB(ctx, dbFn, ballotSource); err != nil {
log.Fatalf("%+v", err)
}
}
func updateVotesInDB(ctx context.Context, dbFn string, ballotSrc string) error {
db, err := sql.Open(
"sqlite3", dbFn+"?"+strings.Join([]string{
"mode=rw",
"_foreign_keys=1",
"_defer_foreign_keys=1",
"_timeout=5000",
"_vacuum=none",
"_journal=memory",
"_sync=off",
}, "&"),
)
if err != nil {
return xerrors.Errorf("failed to open state database %s: %s", dbFn, err)
}
if _, err := db.Exec(
`
CREATE TABLE IF NOT EXISTS votes (
actor_id INTEGER NOT NULL UNIQUE,
does_accept BOOL NOT NULL,
vote_received DATETIME NULL
)
`,
); err != nil {
return err
}
acctIDs := make([]struct {
AccountID int
AccountAddress filaddr.Address
}, 0, 1<<12)
if err := sqlscan.Select(
ctx,
db,
&acctIDs,
`SELECT account_id, account_address FROM accounts`,
); err != nil {
return err
}
acctLookup := make(map[filaddr.Address]int, len(acctIDs))
for _, a := range acctIDs {
acctLookup[a.AccountAddress] = a.AccountID
}
var ballotRdr io.ReadCloser
defer func() {
if ballotRdr != nil {
ballotRdr.Close() //nolint:errcheck
}
}()
if strings.HasPrefix(ballotSrc, "http://") || strings.HasPrefix(ballotSrc, "https://") {
req, err := http.NewRequestWithContext(ctx, "GET", ballotSrc, nil)
if err != nil {
return err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return xerrors.Errorf("non-200 response: %d", resp.StatusCode)
}
ballotRdr = resp.Body
} else {
ballotRdr, err = os.Open(ballotSrc)
if err != nil {
return xerrors.Errorf("unable to open %s as plain file: %w", ballotSrc, err)
}
}
ballots := make([]ballot, 0, 1<<12)
if err := json.NewDecoder(ballotRdr).Decode(&ballots); err != nil {
return xerrors.Errorf("unexpected error parsing data json %s: %w", ballotSrc, err)
}
type vote struct {
doesAccept bool
received time.Time
}
votes := make(map[int]vote, 1<<12)
// process ordered by time, first(?) cast wins
sort.Slice(ballots, func(i, j int) bool {
return ballots[i].CreatedAt.Before(ballots[j].CreatedAt)
})
for _, b := range ballots {
acctID, found := acctLookup[b.SignerAddress]
if !found {
log.Printf("ignoring ballot %v: unknown robust address", b)
continue
}
var doesAccept bool
if b.OptionID == 49 {
doesAccept = true
} else if b.OptionID != 50 {
log.Printf("ignoring ballot %v: unknown vote option", b)
}
if v, exists := votes[acctID]; exists {
if v.doesAccept != doesAccept {
log.Printf(
"ignoring CONFLICTING ballot: ACCEPT:%t at %s but ACCEPT:%t at %s",
v.doesAccept,
v.received,
doesAccept,
b.CreatedAt,
)
}
continue
}
votes[acctID] = vote{
doesAccept: doesAccept,
received: b.CreatedAt,
}
}
insertVote, err := db.Prepare(
`
INSERT INTO votes
( actor_id, does_accept, vote_received )
VALUES ( $1, $2, $3 )
`,
)
if err != nil {
return err
}
if _, err = db.Exec(
`DELETE FROM votes`,
); err != nil {
return err
}
var acceptCount, rejectCount int
for a, v := range votes {
if v.doesAccept {
acceptCount++
} else {
rejectCount++
}
if _, err := insertVote.Exec(a, v.doesAccept, v.received); err != nil {
return err
}
}
// give all the msigs a "vote" as well, based on their having-voted parts
// do it recursively, because why not :)
for {
res, err := db.Exec(
`
INSERT INTO votes
( actor_id, does_accept )
SELECT ma.msig_id, v.does_accept
FROM votes v
JOIN msig_actors ma USING ( actor_id )
JOIN msigs m USING ( msig_id )
WHERE
ma.msig_id NOT IN ( SELECT actor_id FROM votes )
AND
ma.msig_id != 1858410 -- ignore the Fil+ LDN msig
GROUP BY ma.msig_id, m.threshold, v.does_accept
HAVING COUNT(*) >= m.threshold
`,
)
if err != nil {
return err
}
ra, err := res.RowsAffected()
if err != nil {
return err
}
if ra == 0 {
break
}
}
// now that we have all the signing actors vote: add the SPs as actors on their own too
// When there is a conflict, owner trumps worker
// https://filecoinproject.slack.com/archives/C01EU76LPCJ/p1663721692909119
if _, err := db.Exec(
`
WITH sp_votes AS (
SELECT
p.provider_id,
COALESCE(
( SELECT does_accept FROM votes v WHERE v.actor_id = p.owner_id ),
( SELECT does_accept FROM votes v WHERE v.actor_id = p.worker_id )
) AS does_accept
FROM providers p
)
INSERT INTO votes
( actor_id, does_accept )
SELECT provider_id, does_accept FROM sp_votes WHERE does_accept IS NOT NULL
`,
); err != nil {
return err
}
log.Printf("Processed %d ACCEPT and %d REJECT votes\n", acceptCount, rejectCount)
type prelimRes struct {
Type string
Weight float64
DoesAccept *bool
}
pr := make([]prelimRes, 0, 8)
log.Println("Calculating preliminary results ( takes about a minute )")
if err := sqlscan.Select(
ctx,
db,
&pr,
`
SELECT "BalancesNfil" type, SUM(bal) weight, does_accept FROM (
SELECT SUM( CAST( balance AS DOUBLE ) / 1000000000 ) bal, does_accept
FROM providers p
LEFT JOIN votes v ON p.provider_id = v.actor_id
GROUP BY does_accept
UNION ALL
SELECT SUM( CAST( balance AS DOUBLE ) / 1000000000 ) bal, does_accept
FROM accounts a
LEFT JOIN votes v ON a.account_id = v.actor_id
GROUP BY does_accept
UNION ALL
SELECT SUM( CAST( balance AS DOUBLE ) / 1000000000 ) bal, does_accept
FROM msigs m
LEFT JOIN votes v ON m.msig_id = v.actor_id
GROUP BY does_accept
) GROUP BY does_accept
UNION ALL
SELECT "DealBytesProvider" type, SUM( piece_size ) weight, does_accept
FROM deals d
LEFT JOIN votes v ON d.provider_id = v.actor_id
WHERE
d.sector_activation_epoch IS NOT NULL
AND
d.deal_slash_epoch IS NULL
AND
d.end_epoch > 2162760
GROUP BY does_accept
UNION ALL
SELECT "DealBytesClient" type, SUM( piece_size ) weight, does_accept
FROM deals d
LEFT JOIN votes v ON d.client_id = v.actor_id
WHERE
d.sector_activation_epoch IS NOT NULL
AND
d.deal_slash_epoch IS NULL
AND
d.end_epoch > 2162760
GROUP BY does_accept
UNION ALL
SELECT "SpRawBytesMiB" type, SUM( CAST( power_raw AS BIGINT ) >> 20 ) weight, does_accept
FROM providers p
LEFT JOIN votes v ON p.provider_id = v.actor_id
GROUP BY does_accept
`,
); err != nil {
return err
}
type tslice struct {
didVote bool
doesAccept bool
}
prelimTally := make(map[string]map[tslice]float64, 4)
for _, p := range pr {
t, seen := prelimTally[p.Type]
if !seen {
t = make(map[tslice]float64, 3)
prelimTally[p.Type] = t
}
ts := tslice{
didVote: (p.DoesAccept != nil),
}
if ts.didVote {
ts.doesAccept = *p.DoesAccept
}
t[ts] = p.Weight
}
for g, t := range prelimTally {
tot := t[tslice{didVote: false}] + t[tslice{didVote: true, doesAccept: false}] + t[tslice{didVote: true, doesAccept: true}]
totVoted := t[tslice{didVote: true, doesAccept: false}] + t[tslice{didVote: true, doesAccept: true}]
log.Printf(`
Group: %s
Abstain: % 3.1f%% % 20.0f
Yea: % 3.1f%% % 20.0f
Nay: % 3.1f%% % 20.0f
`,
g,
100*t[tslice{didVote: false}]/tot, t[tslice{didVote: false}],
100*t[tslice{didVote: true, doesAccept: true}]/totVoted, t[tslice{didVote: true, doesAccept: true}],
100*t[tslice{didVote: true, doesAccept: false}]/totVoted, t[tslice{didVote: true, doesAccept: false}],
)
}
return nil
}