-
Notifications
You must be signed in to change notification settings - Fork 74
/
cmd_import.go
1334 lines (1178 loc) · 38.3 KB
/
cmd_import.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2022 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"bytes"
"encoding/base64"
"encoding/binary"
"encoding/hex"
"encoding/json"
"errors"
"flag"
"fmt"
"io/fs"
"net/http"
"os"
"path/filepath"
"sort"
"strings"
"sync"
"time"
"github.com/decred/dcrd/chaincfg/v3"
"github.com/decred/dcrd/dcrutil/v3"
"github.com/decred/politeia/politeiad/api/v1/identity"
"github.com/decred/politeia/politeiad/api/v1/mime"
backend "github.com/decred/politeia/politeiad/backendv2"
"github.com/decred/politeia/politeiad/backendv2/tstorebe/store"
"github.com/decred/politeia/politeiad/backendv2/tstorebe/store/mysql"
"github.com/decred/politeia/politeiad/backendv2/tstorebe/tlog"
"github.com/decred/politeia/politeiad/backendv2/tstorebe/tstore"
"github.com/decred/politeia/politeiad/plugins/comments"
"github.com/decred/politeia/politeiad/plugins/pi"
"github.com/decred/politeia/politeiad/plugins/ticketvote"
"github.com/decred/politeia/politeiad/plugins/usermd"
"github.com/decred/politeia/politeiawww/config"
"github.com/decred/politeia/politeiawww/legacy/user"
userdb "github.com/decred/politeia/politeiawww/legacy/user/mysql"
"github.com/decred/politeia/util"
"github.com/google/trillian"
"github.com/google/uuid"
"google.golang.org/grpc/codes"
)
const (
// tstore settings
defaultTlogHost = "localhost:8090"
defaultDBHost = "localhost:3306"
defaultDBPass = "politeiadpass"
// User database settings
userDBPass = "politeiawwwpass"
)
var (
// CLI flags for the import command. We print a custom usage message,
// see usage.go, so the individual flag usage messages are left blank.
importFlags = flag.NewFlagSet(importCmdName, flag.ExitOnError)
testnet = importFlags.Bool("testnet", false, "")
tlogHost = importFlags.String("tloghost", defaultTlogHost, "")
dbHost = importFlags.String("dbhost", defaultDBHost, "")
dbPass = importFlags.String("dbpass", defaultDBPass, "")
importToken = importFlags.String("token", "", "")
stubUsers = importFlags.Bool("stubusers", false, "")
// tstore settings
politeiadHomeDir = dcrutil.AppDataDir("politeiad", false)
politeiadDataDir = filepath.Join(politeiadHomeDir, "data")
// dcrtimeHost = "" // Not needed for import
// dcrtimeCert = "" // Not needed for import
// User database settings
userDBEncryptionKey = filepath.Join(config.DefaultHomeDir, "sbox.key")
)
// execImportCmd executes the import command.
func execImportCmd(args []string) error {
// Verify the legacy directory exists
if len(args) == 0 {
return fmt.Errorf("legacy dir argument not provided")
}
legacyDir := util.CleanAndExpandPath(args[0])
if _, err := os.Stat(legacyDir); err != nil {
return fmt.Errorf("legacy directory not found: %v", legacyDir)
}
// Parse the CLI flags
err := importFlags.Parse(args[1:])
if err != nil {
return err
}
// Testnet or mainnet
params := config.MainNetParams.Params
if *testnet {
params = config.TestNet3Params.Params
}
fmt.Printf("\n")
fmt.Printf("Command parameters\n")
fmt.Printf("Network : %v\n", params.Name)
fmt.Printf("Tlog host: %v\n", *tlogHost)
fmt.Printf("DB host : %v\n", *dbHost)
fmt.Printf("\n")
// Print the total elapsed time on exit
t := time.Now()
defer func() {
fmt.Printf("Import elapsed time: %v\n", time.Since(t))
}()
// Setup the import command context
c, err := newImportCmd(legacyDir, *tlogHost, *dbHost, *dbPass,
*importToken, *stubUsers, params)
if err != nil {
return err
}
// Import the legacy proposals
return c.importLegacyProposals()
}
// importCmd implements the legacypoliteia import command. The import command
// reads the output of the convert command from disk and imports it into the
// politeiad tstore backend.
//
// The performance bottleneck for this command is the trillian log server (tlog
// server). ~50 leaves/sec can be appended onto a tlog tree. This means that
// importing 10,000 proposal votes will take ~200 seconds (3 minutes, 20
// seconds). The vast majority of the execution time of this command is spent
// importing proposal votes.
//
// The command is relatively light weight. It's memory footprint should stay
// under 100 MiB and CPU usage should be minimal.
type importCmd struct {
sync.Mutex
legacyDir string
tlogHost string
token string // Optional
stubUsers bool
tstore *tstore.Tstore
// The following are used to import the proposal votes into tstore manually
// in order to increase performance to an acceptable speed.
kv store.BlobKV
tlogClient tlog.Client
// The following fields will only be populated when the caller provides
// the stub users flag.
userDB user.Database
http *http.Client
}
// newImportCmd returns a new importCmd.
func newImportCmd(legacyDir, tlogHost, dbHost, dbPass, importToken string, stubUsers bool, params *chaincfg.Params) (*importCmd, error) {
// Setup the tstore connection
ts, err := tstore.New(politeiadHomeDir, politeiadDataDir,
params, tlogHost, dbHost, dbPass, "", "")
if err != nil {
return nil, err
}
// Setup key-value store
var (
dbUser = "politeiad"
dbName = fmt.Sprintf("%v_kv", params.Name)
)
kv, err := mysql.New(dbHost, dbUser, dbPass, dbName)
if err != nil {
return nil, err
}
// Setup trillian client
tlogClient, err := tlog.NewClient(tlogHost)
if err != nil {
return nil, err
}
// Setup the user database connection
var (
userDB user.Database
httpC *http.Client
)
if stubUsers {
userDB, err = userdb.New(dbHost, userDBPass,
params.Name, userDBEncryptionKey)
if err != nil {
return nil, err
}
httpC, err = util.NewHTTPClient(false, "")
if err != nil {
return nil, err
}
}
return &importCmd{
legacyDir: legacyDir,
token: importToken,
tlogHost: tlogHost,
stubUsers: stubUsers,
tstore: ts,
kv: kv,
tlogClient: tlogClient,
userDB: userDB,
http: httpC,
}, nil
}
// importProposals walks the legacy directory and imports the legacy proposals
// into tstore. It accomplishes this using the following steps:
//
// 1. Inventory all legacy proposals being imported.
//
// 2. Retrieve the tstore token inventory.
//
// 3. Iterate through each record in the existing tstore inventory and check
// if the record corresponds to one of the legacy proposals.
//
// 4. Perform an fsck on all legacy proposals that already exist in tstore to
// verify that the full legacy proposal has been imported. Any missing
// legacy proposal content is added to tstore during this step. A partial
// import can happen if the import command was being run and was stopped
// prior to completion or if it encountered an unexpected error.
//
// 5. Add the legacy RFP proposals to tstore. This must be done first so that
// the RFP submissions can link to the tstore RFP proposal token.
//
// 6. Add the remaining legacy proposals to tstore.
//
// 7. Add a startRunoffRecord for each RFP proposal vote. The record is added
// to the RFP parent's tlog tree. This is required in order to mimic what
// would happen under normal operating conditions.
func (c *importCmd) importLegacyProposals() error {
// 1. Inventory all legacy proposals being imported
legacyInv, err := parseLegacyTokens(c.legacyDir)
if err != nil {
return err
}
legacyInvM := make(map[string]struct{}, len(legacyInv))
for _, token := range legacyInv {
legacyInvM[token] = struct{}{}
}
fmt.Printf("%v legacy proposals found for import\n", len(legacyInv))
// 2. Retrieve the tstore token inventory
inv, err := c.tstore.Inventory()
if err != nil {
return err
}
fmt.Printf("%v existing proposals found in tstore\n", len(inv))
// imported contains the legacy tokens of all legacy proposals
// that have already been imported into tstore. This list does
// not differentiate between partially imported or fully
// imported proposals. The fsck function checks for and handles
// partially imported proposals.
//
// map[legacyToken]tstoreToken
imported := make(map[string][]byte, len(legacyInv))
// startRunoffRecords is used to aggregate the data for runoff
// votes. This is done during runtime because the tstore tokens
// for all of the RFP submissions must be compiled before the
// startRunoffRecord can be saved to the parent RFP tree.
//
// map[tstoreTokenForParentRFP]startRunoffRecord
startRunoffRecords := make(map[string]startRunoffRecord, len(legacyInv))
// 3. Iterate through each record in the existing tstore
// inventory and check if the record corresponds to one
// of the legacy proposals.
for _, tstoreToken := range inv {
// Get the record metadata from tstore
filenames := []string{pi.FileNameProposalMetadata}
r, err := c.tstore.RecordPartial(tstoreToken, 0, filenames, false)
if err != nil {
return err
}
switch r.RecordMetadata.Status {
case backend.StatusPublic, backend.StatusArchived:
// These statuses are expected
default:
// This is not a record that we're interested in.
// The legacy proposals are all going to be either
// public or archived.
continue
}
// Check if this is a legacy proposal
pm, err := decodeProposalMetadata(r.Files)
if err != nil {
return err
}
if pm.LegacyToken == "" {
// This is not a legacy proposal
continue
}
// This is a legacy proposal. Add it to the imported list.
imported[pm.LegacyToken] = tstoreToken
}
fmt.Printf("%v legacy proposals were found in tstore\n", len(imported))
// 4. Perform an fsck on all legacy proposals that already exist
// in tstore to verify that the full legacy proposal has been
// imported. Any missing legacy proposal content is added to
// tstore during this step. A partial import can happen if
// the import command was being run and was stopped prior to
// completion or if it encountered an unexpected error.
for legacyToken, tstoreToken := range imported {
err := c.fsckProposal(legacyToken, tstoreToken)
if err != nil {
return err
}
}
// 5. Add the legacy RFP proposals to tstore. This must be done
// first so that the RFP submissions can link to the tstore
// RFP proposal token.
for _, legacyToken := range legacyInv {
if c.token != "" && c.token != legacyToken {
// The caller wants to import a specific
// proposal and this is not it.
continue
}
if _, ok := imported[legacyToken]; ok {
// This proposal has already been imported
continue
}
p, err := readProposal(c.legacyDir, legacyToken)
if err != nil {
return err
}
if !p.isRFP() {
// This is not an RFP. Skip it for now.
continue
}
fmt.Printf("Importing proposal %v/%v\n", len(imported)+1, len(legacyInv))
tstoreToken, err := c.importProposal(p, nil)
if err != nil {
return err
}
imported[legacyToken] = tstoreToken
}
// 6. Add the remaining legacy proposals to tstore
for _, legacyToken := range legacyInv {
if c.token != "" && c.token != legacyToken {
// The caller wants to import a specific
// proposal and this is not it.
continue
}
if _, ok := imported[legacyToken]; ok {
// This proposal has already been imported
continue
}
fmt.Printf("Importing proposal %v/%v\n", len(imported)+1, len(legacyInv))
// Read the proposal from disk
p, err := readProposal(c.legacyDir, legacyToken)
if err != nil {
return err
}
// Lookup th RFP parent tstore token if this is an RFP submission.
// The RFP submissions must reference the parent RFP tstore token,
// not the parent RFP legacy token.
var parentTstoreToken []byte
if p.isRFPSubmission() {
parentTstoreToken = imported[p.VoteMetadata.LinkTo]
if parentTstoreToken == nil {
// Should not happen
return fmt.Errorf("rpf parent tstore token not found")
}
}
// Import the proposal
tstoreToken, err := c.importProposal(p, parentTstoreToken)
if err != nil {
return err
}
imported[legacyToken] = tstoreToken
// Aggregate the runoff vote data needed for the startRunoffRecord.
// This is only necessary if this proposal in an RFP submission.
if parentTstoreToken != nil {
parentToken := hex.EncodeToString(parentTstoreToken)
srr, ok := startRunoffRecords[parentToken]
if !ok {
srr = startRunoffRecord{
Submissions: []string{},
Mask: p.VoteDetails.Params.Mask,
Duration: p.VoteDetails.Params.Duration,
QuorumPercentage: p.VoteDetails.Params.QuorumPercentage,
PassPercentage: p.VoteDetails.Params.PassPercentage,
StartBlockHeight: p.VoteDetails.StartBlockHeight,
StartBlockHash: p.VoteDetails.StartBlockHash,
EndBlockHeight: p.VoteDetails.EndBlockHeight,
EligibleTickets: p.VoteDetails.EligibleTickets,
}
}
submissionToken := hex.EncodeToString(tstoreToken)
srr.Submissions = append(srr.Submissions, submissionToken)
startRunoffRecords[parentToken] = srr
}
}
// 7. Add a startRunoffRecord for each RFP proposal vote. The
// record is added to the RFP parent's tlog tree. This is
// required in order to mimic what would happen under normal
// operating conditions.
for parentTstoreToken, srr := range startRunoffRecords {
fmt.Printf("Importing start runoff record to %v\n", parentTstoreToken)
parent, err := hex.DecodeString(parentTstoreToken)
if err != nil {
return err
}
err = c.saveStartRunoffRecord(parent, srr)
if err != nil {
return err
}
}
return nil
}
// fsckProposal verifies that a legacy proposal has been fully imported into
// tstore. If a partial import is found, this function will pick up where the
// previous invocation left off and finish the import.
func (c *importCmd) fsckProposal(legacyToken string, tstoreToken []byte) error {
fmt.Printf("Fsck proposal %x %v\n", tstoreToken, legacyToken)
// This is non-trivial to implement and will only be needed
// if an error occurs during the import process. We'll leave
// this unimplemented for now and only implement it if
// something goes wrong during the production import process
// and we actually need it.
return nil
}
// importProposal imports the specified legacy proposal into tstore and returns
// the tstore token that is created during import.
//
// parentTstoreToken is an optional argument that will be populated for RFP
// submissions. The parentTstoreToken is the parent RFP tstore token that the
// RFP submissions will need to reference. This argument will be nil for all
// proposals that are not RFP submissions.
//
// This function assumes that the proposal does not yet exist in tstore.
// Handling proposals that have been partially added is done by the
// fsckProposal function.
func (c *importCmd) importProposal(p *proposal, parentTstoreToken []byte) ([]byte, error) {
fmt.Printf(" Legacy token: %v\n", p.RecordMetadata.Token)
// Create a new tstore record entry
tstoreToken, err := c.tstore.RecordNew()
if err != nil {
return nil, err
}
fmt.Printf(" Tstore token: %x\n", tstoreToken)
// Perform proposal data changes
err = overwriteProposalFields(p, tstoreToken, parentTstoreToken)
if err != nil {
return nil, err
}
// Import the proposal contents
fmt.Printf(" Importing record data...\n")
err = c.importRecord(*p, tstoreToken)
if err != nil {
return nil, err
}
fmt.Printf(" Importing comment plugin data...\n")
err = c.importCommentPluginData(*p, tstoreToken)
if err != nil {
return nil, err
}
fmt.Printf(" Importing ticketvote plugin data...\n")
err = c.importTicketvotePluginData(*p, tstoreToken)
if err != nil {
return nil, err
}
// Stub the user in the politeiawww user database
if c.stubUsers {
err := c.stubProposalUsers(*p)
if err != nil {
return nil, err
}
}
return tstoreToken, nil
}
// importRecord imports the backend record portion of a proposal into tstore
// using the same steps that would occur under if the proposal was saved under
// normal conditions and not being imported by this tool. This is required
// because there are certain steps that the tstore backend must complete, ex.
// re-saving encrypted blobs as plain text when a proposal is made public, in
// order for the proposal to be imported correctly.
func (c *importCmd) importRecord(p proposal, tstoreToken []byte) error {
// Convert user generated metadata into backend files.
//
// User generated metadata includes:
// - pi plugin ProposalMetadata
// - ticketvote plugin VoteMetadata (may not exist)
f, err := convertProposalMetadataToFile(p.ProposalMetadata)
if err != nil {
return err
}
p.Files = append(p.Files, *f)
if p.VoteMetadata != nil {
f, err := convertVoteMetadataToFile(*p.VoteMetadata)
if err != nil {
return err
}
p.Files = append(p.Files, *f)
}
// Convert server generated metadata into backed metadata streams.
//
// Server generated metadata includes:
// - user plugin StatusChangeMetadata
// - user plugin UserMetadata
//
// Public proposals will only have one status change. Abandoned
// proposals will have two status changes, the public status change
// and the archived status change. The status changes are handled
// individually and not automatically added to the same metadata
// stream so that we can mimick how status change data is saved
// under normal operation.
userStream, err := convertUserMetadataToMetadataStream(p.UserMetadata)
if err != nil {
return err
}
var (
publicStatus = p.StatusChanges[0]
abandonedStatus *usermd.StatusChangeMetadata
)
if len(p.StatusChanges) > 1 {
abandonedStatus = &p.StatusChanges[1]
}
// Cache the record status that we will end up at. We
// must go through the normal status iterations in order
// to import the proposal correctly.
//
// Ex: unreviewed -> public -> abandoned
status := p.RecordMetadata.Status
// Save the proposal as unvetted
p.RecordMetadata.State = backend.StateUnvetted
p.RecordMetadata.Status = backend.StatusUnreviewed
metadataStreams := []backend.MetadataStream{
*userStream,
}
err = c.tstore.RecordSave(tstoreToken, p.RecordMetadata,
metadataStreams, p.Files)
if err != nil {
return err
}
// Save the proposal as vetted. The public status change
// is added to the status change metadata stream during
// this step. The timestamp is incremented by 1 second
// so it's not the same timestamp as the unvetted version.
p.RecordMetadata.State = backend.StateVetted
p.RecordMetadata.Status = backend.StatusPublic
p.RecordMetadata.Timestamp += 1
statusChangeStream, err := convertStatusChangeToMetadataStream(publicStatus)
if err != nil {
return err
}
metadataStreams = []backend.MetadataStream{
*userStream,
*statusChangeStream,
}
err = c.tstore.RecordSave(tstoreToken, p.RecordMetadata,
metadataStreams, p.Files)
if err != nil {
return err
}
switch status {
case backend.StatusPublic:
// This is a public proposal. There is nothing else
// that needs to be done.
return nil
case backend.StatusArchived:
// This is an abandoned proposal. Continue so that the
// status is updated below.
default:
// This should not happen. There should only be public
// and abandoned proposals.
return fmt.Errorf("invalid record status %v", status)
}
// This is an abandoned proposal. Update the record metadata,
// add the abandoned status to the status changes metadata
// stream, and freeze the tstore record. This is what would
// happen under regular operating conditions. The timestamp
// is incremented by 1 second so that it is unique.
p.RecordMetadata.Status = backend.StatusArchived
p.RecordMetadata.Iteration += 1
p.RecordMetadata.Timestamp += 1
abandonedStream, err := convertStatusChangeToMetadataStream(*abandonedStatus)
if err != nil {
return err
}
metadataStreams = []backend.MetadataStream{
*userStream,
appendMetadataStream(*statusChangeStream, *abandonedStream),
}
return c.tstore.RecordFreeze(tstoreToken, p.RecordMetadata,
metadataStreams, p.Files)
}
// importCommentPluginData imports the comment plugin data into tstore for
// the provided proposal.
func (c *importCmd) importCommentPluginData(p proposal, tstoreToken []byte) error {
for i, v := range p.CommentAdds {
s := fmt.Sprintf(" Comment add %v/%v", i+1, len(p.CommentAdds))
printInPlace(s)
err := c.saveCommentAdd(tstoreToken, v)
if err != nil {
return err
}
if i == len(p.CommentAdds)-1 {
fmt.Printf("\n")
}
}
for i, v := range p.CommentDels {
s := fmt.Sprintf(" Comment del %v/%v", i+1, len(p.CommentDels))
printInPlace(s)
err := c.saveCommentDel(tstoreToken, v)
if err != nil {
return err
}
if i == len(p.CommentDels)-1 {
fmt.Printf("\n")
}
}
for i, v := range p.CommentVotes {
s := fmt.Sprintf(" Comment vote %v/%v", i+1, len(p.CommentVotes))
printInPlace(s)
err := c.saveCommentVote(tstoreToken, v)
if err != nil {
return err
}
if i == len(p.CommentVotes)-1 {
fmt.Printf("\n")
}
}
return nil
}
// importTicketvotePluginData imports the ticketvote plugin data into tstore
// for the provided proposal.
//
// Some proposals we're never voted on and therefor do not have any ticketvote
// plugin data that needs to be imported.
func (c *importCmd) importTicketvotePluginData(p proposal, tstoreToken []byte) error {
// Save the auth details
if p.AuthDetails == nil {
return nil
}
fmt.Printf(" Auth details\n")
err := c.saveAuthDetails(tstoreToken, *p.AuthDetails)
if err != nil {
return err
}
// Save the vote details
if p.VoteDetails == nil {
return nil
}
fmt.Printf(" Vote details\n")
err = c.saveVoteDetails(tstoreToken, *p.VoteDetails)
if err != nil {
return err
}
// Save the cast votes. These are saved concurrently in batches
// to get around the tlog signer performance bottleneck. The tlog
// signer will only append queued leaves onto a tlog tree every
// xxx interval, where xxx is a config setting that is currently
// configured to 200ms for politeia. If we did not submit the
// votes concurrently, each vote would take at least 200ms to
// be appended, which is unacceptably slow when you have tens of
// thousands of votes to import.
//
// tlog is incredibly finicky. I think there is a deadlock bug
// somewhere in the trillian log server that gets hit when a large
// number of leaves are being appended. A batch size of 50 was
// found during testing to be a good balance between performance
// and errors. Increasing the batch size speeds up the importing,
// but also results in more deadlocks.
var (
batchSize = 50
startIdx = 0
t = time.Now()
)
for startIdx < len(p.CastVotes) {
endIdx := startIdx + batchSize
if endIdx > len(p.CastVotes) {
endIdx = len(p.CastVotes)
}
s := fmt.Sprintf(" Cast vote %v/%v", endIdx, len(p.CastVotes))
printInPlace(s)
c.saveVoteBatch(tstoreToken, p.CastVotes[startIdx:endIdx])
startIdx += batchSize
}
fmt.Printf("\n")
fmt.Printf(" Elapsed vote import time: %v\n", time.Since(t))
return nil
}
// SavePluginBlobEntry is a light weight version of the TstoreClient BlobSave
// method that is used during normal operation of politeiad when saving plugin
// data. This light weight function is necessary to increase performance of
// a plugin data blob to an acceptable speed for this command.
func (c *importCmd) savePluginBlobEntry(token []byte, be store.BlobEntry) error {
// Prepare key-value store blob
digest, err := hex.DecodeString(be.Digest)
if err != nil {
return err
}
blob, err := store.Blobify(be)
if err != nil {
return err
}
key := uuid.New().String()
kv := map[string][]byte{key: blob}
// Save the blob to store
err = c.kv.Put(kv, false)
if err != nil {
return err
}
// Setup the tlog leaf extra data
type extraData struct {
Key string `json:"k"`
Desc string `json:"d"`
State backend.StateT `json:"s,omitempty"`
}
b, err := base64.StdEncoding.DecodeString(be.DataHint)
if err != nil {
return err
}
var dd store.DataDescriptor
err = json.Unmarshal(b, &dd)
if err != nil {
return err
}
ed := extraData{
Key: key,
Desc: dd.Descriptor,
State: backend.StateVetted,
}
extraDataB, err := json.Marshal(ed)
if err != nil {
return err
}
// Append log leaf to trillian tree
var (
treeID = int64(binary.LittleEndian.Uint64(token))
leaves = []*trillian.LogLeaf{
tlog.NewLogLeaf(digest, extraDataB),
}
)
queued, _, err := c.tlogClient.LeavesAppend(treeID, leaves)
if err != nil {
return err
}
if len(queued) != 1 {
return fmt.Errorf("got %v queued leaves, want 1", len(queued))
}
code := codes.Code(queued[0].QueuedLeaf.GetStatus().GetCode())
switch code {
case codes.OK:
// This is ok; continue
case codes.AlreadyExists:
return backend.ErrDuplicatePayload
default:
return fmt.Errorf("queued leaf error: %v", c)
}
return nil
}
// saveVoteBatch saves a batch of cast votes to tstore. This includes appending
// leaves onto the tlog tree and saving the data blobs to the key-value store.
//
// tlog is incredibly finicky. I think there is a deadlock bug somewhere in the
// trillian log server that gets hit when a large number of leaves are being
// appended. The tlog server will periodically freeze up without throwing any
// errors and will require a hard restart. This function was written in a way
// that mitigates this issue as much as possible. If the trillian log server
// freezes up, this function will be stuck in a rety loop until the trillian
// lop server is reset.
func (c *importCmd) saveVoteBatch(tstoreToken []byte, votes []ticketvote.CastVoteDetails) {
var wg sync.WaitGroup
for _, v := range votes {
// Increment the wait group
wg.Add(1)
go func(cvd ticketvote.CastVoteDetails) {
// Decrement the wait group on successful completion
defer func() {
wg.Done()
}()
var voteSaved bool
for !voteSaved {
err := c.saveCastVoteDetails(tstoreToken, cvd)
switch {
case err == nil:
voteSaved = true
case strings.Contains(err.Error(), "duplicate payload"):
fmt.Printf("\n")
fmt.Printf("%v: %v\n", cvd.Ticket, err)
fmt.Printf("Vote %v already saved; skipping\n", cvd.Ticket)
voteSaved = true
default:
fmt.Printf("\n")
fmt.Printf("Failed to save cast vote %v: %v\n", cvd.Ticket, err)
fmt.Printf("Retrying cast vote %v\n", cvd.Ticket)
time.Sleep(50 * time.Millisecond)
}
}
// Not exactly sure why, but this reduces the number of failed
// tlog appends.
time.Sleep(50 * time.Millisecond)
var colliderSaved bool
for !colliderSaved {
vc := voteCollider{
Token: cvd.Token,
Ticket: cvd.Ticket,
}
err := c.saveVoteCollider(tstoreToken, vc)
switch {
case err == nil:
colliderSaved = true
case strings.Contains(err.Error(), "duplicate payload"):
fmt.Printf("\n")
fmt.Printf("%v: %v\n", cvd.Ticket, err)
fmt.Printf("Vote collider %v already saved; skipping\n", cvd.Ticket)
colliderSaved = true
default:
fmt.Printf("\n")
fmt.Printf("Failed to save vote collider %v: %v\n", cvd.Ticket, err)
fmt.Printf("Retrying vote collider %v\n", cvd.Ticket)
time.Sleep(50 * time.Millisecond)
}
}
}(v)
}
// Wait for all votes to be successfully saved
wg.Wait()
}
const (
// The following data descriptors were pulled from the plugins. They're not
// exported from the plugins and under normal circumstances there's no reason
// to have them as exported variables, so we duplicate them here.
// comments plugin data descriptors
dataDescriptorCommentAdd = comments.PluginID + "-add-v1"
dataDescriptorCommentDel = comments.PluginID + "-del-v1"
dataDescriptorCommentVote = comments.PluginID + "-vote-v1"
// ticketvote plugin data descriptors
dataDescriptorAuthDetails = ticketvote.PluginID + "-auth-v1"
dataDescriptorVoteDetails = ticketvote.PluginID + "-vote-v1"
dataDescriptorCastVoteDetails = ticketvote.PluginID + "-castvote-v1"
dataDescriptorVoteCollider = ticketvote.PluginID + "-vcollider-v1"
dataDescriptorStartRunoff = ticketvote.PluginID + "-startrunoff-v1"
)
// saveCommentAdd saves a CommentAdd to tstore as a plugin data blob.
func (c *importCmd) saveCommentAdd(tstoreToken []byte, ca comments.CommentAdd) error {
data, err := json.Marshal(ca)
if err != nil {
return err
}
hint, err := json.Marshal(
store.DataDescriptor{
Type: store.DataTypeStructure,
Descriptor: dataDescriptorCommentAdd,
})
if err != nil {
return err
}
be := store.NewBlobEntry(hint, data)
return c.savePluginBlobEntry(tstoreToken, be)
}
// saveCommentDel saves a CommentDel to tstore as a plugin data blob.
func (c *importCmd) saveCommentDel(tstoreToken []byte, cd comments.CommentDel) error {
data, err := json.Marshal(cd)
if err != nil {
return err
}
hint, err := json.Marshal(
store.DataDescriptor{
Type: store.DataTypeStructure,
Descriptor: dataDescriptorCommentDel,
})
if err != nil {
return err
}
be := store.NewBlobEntry(hint, data)
return c.savePluginBlobEntry(tstoreToken, be)
}
// saveCommentVote saves a CommentVote to tstore as a plugin data blob.
func (c *importCmd) saveCommentVote(tstoreToken []byte, cv comments.CommentVote) error {
data, err := json.Marshal(cv)
if err != nil {
return err
}
hint, err := json.Marshal(
store.DataDescriptor{
Type: store.DataTypeStructure,
Descriptor: dataDescriptorCommentVote,
})
if err != nil {
return err
}
be := store.NewBlobEntry(hint, data)
return c.savePluginBlobEntry(tstoreToken, be)
}
// saveAuthDetails saves a AuthDetails to tstore as a plugin data blob.
func (c *importCmd) saveAuthDetails(tstoreToken []byte, ad ticketvote.AuthDetails) error {
data, err := json.Marshal(ad)
if err != nil {
return err
}
hint, err := json.Marshal(
store.DataDescriptor{
Type: store.DataTypeStructure,