Skip to content

Commit

Permalink
Add largest commitment address to castvote blob
Browse files Browse the repository at this point in the history
  • Loading branch information
thi4go committed Sep 30, 2021
1 parent b285b62 commit 93134e4
Show file tree
Hide file tree
Showing 5 changed files with 262 additions and 92 deletions.
1 change: 1 addition & 0 deletions politeiad/cmd/legacyimport/git.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package main
88 changes: 55 additions & 33 deletions politeiad/cmd/legacyimport/legacyimport.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/decred/politeia/politeiad/backendv2/tstorebe/tstore"
"github.com/decred/politeia/politeiad/plugins/pi"
"github.com/decred/politeia/politeiad/plugins/ticketvote"
"github.com/decred/politeia/politeiad/plugins/usermd"
pusermd "github.com/decred/politeia/politeiad/plugins/usermd"
"github.com/decred/politeia/util"
)
Expand Down Expand Up @@ -105,6 +106,8 @@ func newLegacyImport() (*legacyImport, error) {
}, nil
}

// preParsePaths builds an optimized traversal path for the git record
// repository.
func preParsePaths(path string) (map[string]string, error) {
// Pre-parse git records folder and get the path for each record's
// latest version.
Expand Down Expand Up @@ -156,17 +159,18 @@ func preParsePaths(path string) (map[string]string, error) {
// data for tlog.
func (l *legacyImport) parseRecordData(rootpath string) (*parsedData, error) {
var (
files []backend.File
metadata []backend.MetadataStream
proposalMd pi.ProposalMetadata
voteMd ticketvote.VoteMetadata
recordMd *backend.RecordMetadata
startVoteMd *ticketvote.Start
authDetailsMd *ticketvote.AuthDetails
voteDetailsMd *ticketvote.VoteDetails
commentsPath string
ballotPath string
parentToken string
files []backend.File
metadata []backend.MetadataStream
proposalMd pi.ProposalMetadata
voteMd ticketvote.VoteMetadata
recordMd *backend.RecordMetadata
statusChangeMd *usermd.StatusChangeMetadata
startVoteMd *ticketvote.Start
authDetailsMd *ticketvote.AuthDetails
voteDetailsMd *ticketvote.VoteDetails
commentsPath string
ballotPath string
parentToken string
)
err := filepath.Walk(rootpath,
func(path string, info os.FileInfo, err error) error {
Expand All @@ -176,7 +180,7 @@ func (l *legacyImport) parseRecordData(rootpath string) (*parsedData, error) {

// Build user metadata and get proposal name.
if info.Name() == "00.metadata.txt" {
usermd, name, err := l.convertProposalGeneral(path)
usermd, name, err := l.convertUserMetadata(path)
if err != nil {
return err
}
Expand All @@ -194,19 +198,10 @@ func (l *legacyImport) parseRecordData(rootpath string) (*parsedData, error) {

// Build status change metadata.
if info.Name() == "02.metadata.txt" {
statuschangemd, err := convertStatusChangeMetadata(path)
statusChangeMd, err = convertStatusChangeMetadata(path)
if err != nil {
return err
}
b, err := json.Marshal(statuschangemd)
if err != nil {
return err
}
metadata = append(metadata, backend.MetadataStream{
PluginID: pusermd.PluginID,
StreamID: pusermd.StreamIDStatusChanges,
Payload: string(b),
})
}

// Build authorize vote metadata.
Expand Down Expand Up @@ -353,20 +348,45 @@ func (l *legacyImport) parseRecordData(rootpath string) (*parsedData, error) {
// record path, and returns all data needed by tlog on the parsedData
// struct.
return &parsedData{
files: files,
metadata: metadata,
recordMd: recordMd,
authDetailsMd: authDetailsMd,
voteDetailsMd: voteDetailsMd,
commentsPath: commentsPath,
ballotPath: ballotPath,
legacyToken: proposalMd.LegacyToken,
files: files,
metadata: metadata,
recordMd: recordMd,
statusChangeMd: statusChangeMd,
authDetailsMd: authDetailsMd,
voteDetailsMd: voteDetailsMd,
commentsPath: commentsPath,
ballotPath: ballotPath,
legacyToken: proposalMd.LegacyToken,
parentToken: parentToken,
}, nil
}

// saveRecordData saves the parsed data onto tstore.
func (l *legacyImport) saveRecordData(data parsedData) ([]byte, error) {
newToken, err := l.recordSave(data.files, data.metadata, *data.recordMd)
// Create a new tlog tree for the legacy record.
newToken, err := l.tstore.RecordNew()
if err != nil {
return nil, err
}

// Save new token to record metadata.
data.recordMd.Token = hex.EncodeToString(newToken)
// Save new token to status change metadata.
data.statusChangeMd.Token = hex.EncodeToString(newToken)

// Add status change metadata to metadata stream.
b, err := json.Marshal(data.statusChangeMd)
if err != nil {
return nil, err
}
data.metadata = append(data.metadata, backend.MetadataStream{
PluginID: pusermd.PluginID,
StreamID: pusermd.StreamIDStatusChanges,
Payload: string(b),
})

// Save record to tstore.
err = l.recordSave(newToken, data.files, data.metadata, *data.recordMd)
if err != nil {
return nil, err
}
Expand All @@ -381,6 +401,9 @@ func (l *legacyImport) saveRecordData(data parsedData) ([]byte, error) {

// Save vote details blob, if any.
if data.voteDetailsMd != nil {
if data.parentToken != "" {
data.voteDetailsMd.Params.Parent = data.parentToken
}
err = l.blobSaveVoteDetails(*data.voteDetailsMd, newToken)
if err != nil {
return nil, err
Expand Down Expand Up @@ -467,7 +490,7 @@ func _main() error {
// Save legacy record on tstore.
newToken, err := l.saveRecordData(data)
if err != nil {
return err
panic(err)
}

fmt.Printf("legacyimport: Parsed record %v. new tlog token: %v\n",
Expand All @@ -476,7 +499,6 @@ func _main() error {
return nil
}(*pData)
}

wg.Wait()

fmt.Println("legacyimport: Done parsing first batch!")
Expand Down
38 changes: 15 additions & 23 deletions politeiad/cmd/legacyimport/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"bytes"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
Expand All @@ -21,24 +20,15 @@ import (
"github.com/subosito/gozaru"
)

func (l *legacyImport) recordSave(files []backend.File, metadata []backend.MetadataStream, recordmd backend.RecordMetadata) ([]byte, error) {
// Create a new tlog tree for the legacy record.
newToken, err := l.tstore.RecordNew()
if err != nil {
return nil, err
}

// Insert new token on record metadata.
recordmd.Token = hex.EncodeToString(newToken)

func (l *legacyImport) recordSave(newToken []byte, files []backend.File, metadata []backend.MetadataStream, recordmd backend.RecordMetadata) error {
// Verify data
err = metadataStreamsVerify(metadata)
err := metadataStreamsVerify(metadata)
if err != nil {
return nil, err
return err
}
err = filesVerify(files, nil)
if err != nil {
return nil, err
return err
}

// Check if record status is public. If so, we need to first save it as
Expand All @@ -53,7 +43,7 @@ func (l *legacyImport) recordSave(files []backend.File, metadata []backend.Metad
// Save record.
err = l.tstore.RecordSave(newToken, recordmd, metadata, files)
if err != nil {
return nil, err
return err
}

// If it's public, we need another save to ensure the public status
Expand All @@ -63,11 +53,11 @@ func (l *legacyImport) recordSave(files []backend.File, metadata []backend.Metad
recordmd.Iteration = 2
err = l.tstore.RecordSave(newToken, recordmd, metadata, files)
if err != nil {
return nil, err
return err
}
}

return newToken, nil
return nil
}

func (l *legacyImport) fetchUserByPubKey(pubkey string) (*user, error) {
Expand Down Expand Up @@ -146,8 +136,10 @@ func convertStatusChangeMetadata(path string) (*usermd.StatusChangeMetadata, err
if err != nil {
return nil, err
}
var rsc mdstream.RecordStatusChangeV2
var streams []mdstream.RecordStatusChangeV2
var (
rsc mdstream.RecordStatusChangeV2
streams []mdstream.RecordStatusChangeV2
)
err = json.Unmarshal(b, &rsc)
if err != nil {
// Record has 02.metadata.txt containing nested status changes.
Expand All @@ -156,7 +148,6 @@ func convertStatusChangeMetadata(path string) (*usermd.StatusChangeMetadata, err
if v == "" {
continue
}
var rsc mdstream.RecordStatusChangeV2
err = json.Unmarshal([]byte(v+"}"), &rsc)
if err != nil {
return nil, err
Expand All @@ -170,6 +161,7 @@ func convertStatusChangeMetadata(path string) (*usermd.StatusChangeMetadata, err
// Return most recent status change md.
latest := streams[len(streams)-1]
return &pusermd.StatusChangeMetadata{
// Token:
Version: uint32(latest.Version),
Status: uint32(latest.NewStatus),
Reason: latest.StatusChangeMessage,
Expand All @@ -179,9 +171,9 @@ func convertStatusChangeMetadata(path string) (*usermd.StatusChangeMetadata, err
}, nil
}

// convertProposalGeneral converts the ProposalGeneralV1 metadata structure
// previously used on legacy git records.
func (l *legacyImport) convertProposalGeneral(path string) (*usermd.UserMetadata, string, error) {
// convertUserMetadata converts the 00.metadata.txt file which contains the
// ProposalGeneralV1 metadata structure previously used on legacy git records.
func (l *legacyImport) convertUserMetadata(path string) (*usermd.UserMetadata, string, error) {
b, err := ioutil.ReadFile(path)
if err != nil {
return nil, "", err
Expand Down
Loading

0 comments on commit 93134e4

Please sign in to comment.