From df12c5348c0d867eb6ba0b9064a0e40ac49e5f01 Mon Sep 17 00:00:00 2001 From: David Florness Date: Sat, 14 Nov 2020 13:39:37 -0500 Subject: [PATCH] Remove election data JSON file I'm going to switch to protocol buffers. --- config.go | 192 ------------------------------------------------- config_test.go | 65 ----------------- election.go | 21 +----- main.go | 5 -- 4 files changed, 2 insertions(+), 281 deletions(-) delete mode 100644 config.go delete mode 100644 config_test.go diff --git a/config.go b/config.go deleted file mode 100644 index 8915292..0000000 --- a/config.go +++ /dev/null @@ -1,192 +0,0 @@ -package main - -import ( - "encoding/json" - "fmt" - "io" - "os" - "path" - - "github.com/kyoh86/xdg" - "github.com/libp2p/go-libp2p-core/crypto" - "github.com/mr-tron/base58/base58" -) - -// where cached election info is stored -var ( - tallyardConfigPath = path.Join(xdg.DataHome(), "tallyard") - electionInfoFilePath = path.Join(tallyardConfigPath, "elections.json") -) - -// maps an election's key to its cached info -type ElectionInfos map[string]*ElectionInfo - -// an entry of an election's cached info -type ElectionInfo struct{ - Candidates []Candidate - PrivKey crypto.PrivKey - PubKey crypto.PubKey - RendezvousNonce Nonce -} - -// used for JSON {en,de}coding -type RawElectionInfos map[string]RawElectionInfo - -type RawElectionInfo struct{ - Candidates []string - // Our private key in base58. TODO: encrypt - PrivKey string - // Our public key in base58. Kept in addition to the private key to - // check against when the private key is later encrypted - PubKey string - RendezvousNonce string -} - -// Saves an election's info into a file that can later be easily retrieved, even -// on separate invocations of tallyard. Returns an error if there was one. -func (e *Election) saveInfo() error { - err := os.MkdirAll(tallyardConfigPath, 0700) - if err != nil { - return err - } - infos, err := loadPastElectionInfos() - if err != nil { - return fmt.Errorf("unable to load election info while saving election info: %s", err) - } - file, err := os.OpenFile(electionInfoFilePath, - os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) - if err != nil { - return fmt.Errorf("unable to open election info file: %s", err) - } - defer file.Close() - localVoter := e.localVoter - infos[e.electionKey] = &ElectionInfo{ - e.Candidates, - localVoter.Peerstore().PrivKey(localVoter.ID()), - localVoter.Peerstore().PubKey(localVoter.ID()), - e.rendezvousNonce, - } - raw, err := marshalElectionInfos(infos) - if err != nil { - return fmt.Errorf("unable to marshal election info for cache file: %s", err) - } - err = encodeElectionsInfos(file, raw) - if err != nil { - return fmt.Errorf("unable to encode election info for cache file: %s", err) - } - return nil -} - -// Checks whether we've joined this election before and attempts to retrieve its -// info we cached for it. Useful if a connection was suddently lost and -// tallyard had to restart. Returns the election's info (nil if no such info -// exists) and an error -func getElectionInfo(electionKey string) (*ElectionInfo, error) { - infos, err := loadPastElectionInfos() - if err != nil { - return nil, fmt.Errorf("unable to get election info: %s", err) - } - if info, hasElectionKey := infos[electionKey]; hasElectionKey { - return info, nil - } - return nil, nil -} - -func loadPastElectionInfos() (ElectionInfos, error) { - err := os.MkdirAll(tallyardConfigPath, 0700) - if err != nil { - return nil, err - } - file, err := os.OpenFile(electionInfoFilePath, - os.O_RDONLY|os.O_CREATE, 0600) - if err != nil { - return nil, fmt.Errorf("unable to open election info file: %s", err) - } - defer file.Close() - raw, err := decodeElectionInfos(file) - if err != nil { - return nil, fmt.Errorf("unable to decode election info for cache file: %s", err) - } - infos, err := unmarshalRawElectionInfos(raw) - if err != nil { - return nil, fmt.Errorf("unable to unmarshal election info for cache file: %s", err) - } - return infos, nil -} - -func encodeElectionsInfos(writer io.Writer, rawElections RawElectionInfos) error { - encoder := json.NewEncoder(writer) - return encoder.Encode(rawElections) -} - -func decodeElectionInfos(reader io.Reader) (rawElections RawElectionInfos, err error) { - decoder := json.NewDecoder(reader) - err = decoder.Decode(&rawElections) - if err == io.EOF { - return make(RawElectionInfos), nil - } - if err != nil { - return nil, err - } - return rawElections, nil -} - -func marshalElectionInfos(electionConfigs ElectionInfos) (rawElections RawElectionInfos, err error) { - rawElections = make(RawElectionInfos) - for electionKey, electionConfig := range electionConfigs { - candidates := make([]string, 0, len(electionConfig.Candidates)) - for _, c := range electionConfig.Candidates { - candidates = append(candidates, string(c)) - } - privKeyBytes, err := crypto.MarshalPrivateKey(electionConfig.PrivKey) - if err != nil { - return nil, err - } - privKeyStr := base58.Encode(privKeyBytes) - pubKeyBytes, err := crypto.MarshalPublicKey(electionConfig.PubKey) - if err != nil { - return nil, err - } - pubKeyStr := base58.Encode(pubKeyBytes) - rawElections[electionKey] = RawElectionInfo{ - candidates, - privKeyStr, - pubKeyStr, - string(electionConfig.RendezvousNonce), - } - } - return rawElections, nil -} - -func unmarshalRawElectionInfos(rawElections RawElectionInfos) (electionConfigs ElectionInfos, err error) { - electionConfigs = make(ElectionInfos) - for electionKey, info := range rawElections { - candidates := make([]Candidate, 0, len(info.Candidates)) - for _, s := range info.Candidates { - candidates = append(candidates, Candidate(s)) - } - privKeyBytes, err := base58.Decode(info.PrivKey) - if err != nil { - return nil, err - } - privKey, err := crypto.UnmarshalPrivateKey(privKeyBytes) - if err != nil { - return nil, err - } - pubKeyBytes, err := base58.Decode(info.PubKey) - if err != nil { - return nil, err - } - pubKey, err := crypto.UnmarshalPublicKey(pubKeyBytes) - if err != nil { - return nil, err - } - electionConfigs[electionKey] = &ElectionInfo{ - candidates, - privKey, - pubKey, - Nonce(info.RendezvousNonce), - } - } - return electionConfigs, nil -} diff --git a/config_test.go b/config_test.go deleted file mode 100644 index e7a74e9..0000000 --- a/config_test.go +++ /dev/null @@ -1,65 +0,0 @@ -package main - -import ( - "fmt" - "strings" - "testing" -) - -const ( - electionKey0 = "DpgZ3EG8cyNAxrbGejTTUuJB6KkvKVGK9WEpQWDh6RLd0QmVitsctABm8xbadtkqU46WMLCjuAadUR2RAkjaPf7xz7X" - electionKey1 = "Dmfo6renA9YKuTFdGL4iSP5fcHbxHG4Qc9XTHkBzdHPa0QmVjm2QACdwMYguMtMooDoLsiagyVfe7qwxpuzUmX1stTM" -) - -var rawElectionInfosRef = RawElectionInfos{ - electionKey0: - { - PrivKey: "7r1r8iBFRYi5hLWbNS5vx53fAvnyFFGrgnDEwu8DSGRVdik5TVuPhCBpqummjaMMXgogcoz6h88rEWvM1af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49pFEtVeGv1at6SFNKYzyPCWYnegfvs2f54rUKCchWDoNZwYw7fja32d87qEUzWouorLXc4ATkykaKzHthoiwEMjweQKGgGgUXbNbP56x1jZYgLPSCVHHgGaLNKgqW6tSXEKThEEg42kzDNiymr7soWwGrZPsNyE5vpfoxd7ebeKy2LJ4H9AwqAbUFhuFAFwzcTRLsiWGJKg9ysi8fpcGBcg7NbrrkHwsR1jvvpV7f855pRTPMXCUQpYiGWVPtJ1UZAsmX5RqVmgfdyJHYok8aL2XML9GeHTHZZNg7iD2ST8DSdKNhaYs7oxyTMvWsWnqPezUq5kaQxRa8zTkFqUJYUAgiZrKDFrgsXC4EgboyZM3CK", - PubKey: "7ySHQR7YZJApfZRKds9hzzFypZo8s6WQyZqXCYVHRjug84TSqhdKDnnZpz69WtL8kvTgtrjEF3GHmsfjgMSt2t7vYgU26qHZfCXPUhA9u93vkCXSG6ZLBWw8K2HxMWPR69LoWzCsfXnLnw9mbr64uHmEeP64PSidtyshApUS73ikt7KGMdk3twJLr7SZ8uaqw6DCvBd9po5tVtPZ9gS1NPG3vuavLfm2P9YT2LMzCf68bJmqzgiVpUxRemvpwppVP675C5m6ctDELBTvqwQ9dc8baG4ztZPehEeUhULG5JpgtMKayCYRNhQVKdENyWXfFYzpMJ27iMH3LT2dixjgXRkqh8PKFt1srJA4we6vCLx1yjDjJV7MELo9P9nkXbojjQLXnqa41bYYGMN4ChK1C78U", - }, -} - -func TestRawElectionInfosDecoding(t *testing.T) { - jsonStream := fmt.Sprintf(` - {"%s": {"PrivKey": "%s", - "PubKey": "%s"}, - "%s": {"PrivKey": "%s", - "PubKey": "%s"}}`, - electionKey0, - rawElectionInfosRef[electionKey0].PrivKey, - rawElectionInfosRef[electionKey0].PubKey, - electionKey1, - rawElectionInfosRef[electionKey1].PrivKey, - rawElectionInfosRef[electionKey1].PubKey, - ) - rawElections, err := decodeElectionInfos(strings.NewReader(jsonStream)) - if err != nil { - panic(err) - } - if len(rawElections) != 2 { - t.FailNow() - } - if _, exists := rawElections[electionKey0]; !exists { - t.FailNow() - } - if _, exists := rawElections[electionKey1]; !exists { - t.FailNow() - } - if rawElections[electionKey0].PrivKey != rawElectionInfosRef[electionKey0].PrivKey { - t.FailNow() - } - if rawElections[electionKey1].PrivKey != rawElectionInfosRef[electionKey1].PrivKey { - t.FailNow() - } - if rawElections[electionKey0].PubKey != rawElectionInfosRef[electionKey0].PubKey { - t.FailNow() - } - if rawElections[electionKey1].PubKey != rawElectionInfosRef[electionKey1].PubKey { - t.FailNow() - } -} diff --git a/election.go b/election.go index 4d2d8b1..7b7cdec 100644 --- a/election.go +++ b/election.go @@ -61,25 +61,7 @@ func NewElectionWithElectionKey(electionKey string) *Election { localVoter *LocalVoter rendezvousNonce Nonce ) - // try to recover info from elections file to see if we've - // joined/created this election before - electionInfo, err := getElectionInfo(electionKey) - if err != nil { - panic(err) - } - if electionInfo != nil { // we've joined this election before - // ensure public key from config is same as one derived from - // private key - candidates = electionInfo.Candidates - if !electionInfo.PrivKey.GetPublic().Equals(electionInfo.PubKey) { - // TODO: handle properly - panic("loaded public key does not match derived one from private key") - } - localVoter = NewLocalVoter(libp2p.Identity(electionInfo.PrivKey)) - rendezvousNonce = electionInfo.RendezvousNonce - } else { // we've never joined this election before - localVoter = NewLocalVoter(libp2p.RandomIdentity) - } + localVoter = NewLocalVoter(libp2p.RandomIdentity) e := &Election{ Candidates: candidates, electionKey: electionKey, @@ -92,6 +74,7 @@ func NewElectionWithElectionKey(electionKey string) *Election { panic("invalid election key") } logger.Info("merkle root:", electionKey[:zeroi]) + var err error e.merkleRoot, err = base58.Decode(electionKey[:zeroi]) if err != nil { panic(err) diff --git a/main.go b/main.go index d80ff0f..9f80cdc 100644 --- a/main.go +++ b/main.go @@ -212,11 +212,6 @@ func main() { } } - // saves identity - if err := election.saveInfo(); err != nil { - panic(err) - } - election.localVoter.Bootstrap() if election.masterID == election.localVoter.ID() { -- 2.38.4