Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

politeiad: Consistent record entries ordering. #1607

Merged
merged 7 commits into from Dec 28, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 21 additions & 1 deletion politeiad/backendv2/tstorebe/tstore/record.go
Expand Up @@ -10,6 +10,7 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"sort"

backend "github.com/decred/politeia/politeiad/backendv2"
"github.com/decred/politeia/politeiad/backendv2/tstorebe/store"
Expand Down Expand Up @@ -702,7 +703,12 @@ func (t *Tstore) record(treeID int64, version uint32, filenames []string, omitAl

// Decode blobs
entries := make([]store.BlobEntry, 0, len(keys))
for _, v := range blobs {
// To ensure that the ordering of the record's files and metadata streams
// is always consistent, we need iterate over the blobs map in a
// deterministic manner, which requires sorting the map keys.
sortedKeys := getSortedKeys(blobs)
for _, key := range sortedKeys {
v := blobs[key]
be, err := store.Deblob(v)
if err != nil {
return nil, err
Expand Down Expand Up @@ -779,6 +785,20 @@ func (t *Tstore) record(treeID int64, version uint32, filenames []string, omitAl
}, nil
}

// getSortedKeys accepts a map of record blobs indexed by string keys,
// and it returns the keys in a sorted slice.
func getSortedKeys(blobs map[string][]byte) []string {
keys := make([]string, 0, len(blobs))
for k := range blobs {
keys = append(keys, k)
}

// Sort keys
sort.Strings(keys)

return keys
}

// Record returns the specified version of the record.
func (t *Tstore) Record(token []byte, version uint32) (*backend.Record, error) {
log.Tracef("Record: %x %v", token, version)
Expand Down