From 241148d9c5dc3bd33cdf2c3feacf1efd5094081d Mon Sep 17 00:00:00 2001 From: David Florness Date: Mon, 15 Feb 2021 11:01:33 -0500 Subject: [PATCH] Calculate local voter's sum as soon as we have the final eval instead of using a needless WaitGroup in the main goroutine --- cmd/tallyard/main.go | 10 ++++++++-- election/msg.go | 13 ++++++++++++- election/voter.go | 30 +++++++++++++----------------- 3 files changed, 33 insertions(+), 20 deletions(-) diff --git a/cmd/tallyard/main.go b/cmd/tallyard/main.go index 3bd810c..3e40b02 100644 --- a/cmd/tallyard/main.go +++ b/cmd/tallyard/main.go @@ -3,6 +3,7 @@ package main import ( "fmt" "os" + "time" "github.com/kyoh86/xdg" log "github.com/sirupsen/logrus" @@ -150,8 +151,13 @@ func main() { } // send sum if needed - if el.LocalVoter != nil && el.LocalVoter.SumID == nil { - fmt.Println("waiting for evals...") + if el.LocalVoter != nil { + if el.LocalVoter.Sum == nil { + fmt.Println("waiting for evals...") + for el.LocalVoter.Sum == nil { + time.Sleep(time.Millisecond * 200) + } + } err = el.SendSum(client, elections.EventStore) if err != nil { panic(err) diff --git a/election/msg.go b/election/msg.go index f35d140..c134c48 100644 --- a/election/msg.go +++ b/election/msg.go @@ -410,6 +410,17 @@ func (elections *ElectionsMap) onEvalsMessage(evt *event.Event) (success bool) { } voter.Eval = new(big.Int).SetBytes(decryptedOutput) + for _, voterID := range *el.FinalJoinIDs { + if el.Joins[voterID].Eval == nil { + return true + } + } + sum := big.NewInt(0) + for _, voterID := range *el.FinalJoinIDs { + sum.Add(sum, el.Joins[voterID].Eval) + } + el.LocalVoter.Sum = sum + return true } @@ -489,7 +500,7 @@ func (elections *ElectionsMap) onSumMessage(evt *event.Event) (success bool) { return } - if voter.Sum != nil { + if voter.SumID != nil { warnf("voter submitted multiple sum events") return } diff --git a/election/voter.go b/election/voter.go index cad9221..6cab9ff 100644 --- a/election/voter.go +++ b/election/voter.go @@ -7,8 +7,6 @@ import ( "fmt" "io" "math/big" - "sync" - "time" "golang.org/x/crypto/nacl/box" "maunium.net/go/mautrix" @@ -147,6 +145,7 @@ func (el *Election) StartElection(client *mautrix.Client, eventStore *EventStore func (el *Election) SendEvals(client *mautrix.Client, eventStore *EventStore) error { el.RLock() if el.StartID == nil { + el.RUnlock() return errors.New("SendEvals called before election started") } @@ -183,28 +182,25 @@ func (el *Election) SendEvals(client *mautrix.Client, eventStore *EventStore) er } func (el *Election) SendSum(client *mautrix.Client, eventStore *EventStore) error { - sum := big.NewInt(0) + el.RLock() + if el.LocalVoter.Sum == nil { + el.RUnlock() + return errors.New("SendSum called before local sum was calculated") + } + var evalsIDs []id.EventID - var wg sync.WaitGroup - for _, voterJoinId := range *el.FinalJoinIDs { - wg.Add(1) - go func(voter *Voter) { - for voter.EvalsID == nil { - time.Sleep(time.Millisecond * 100) - } - evalsIDs = append(evalsIDs, *voter.EvalsID) - sum.Add(sum, voter.Eval) - wg.Done() - }(el.Joins[voterJoinId]) - } - wg.Wait() + for _, joinID := range *el.FinalJoinIDs { + voter := el.Joins[joinID] + evalsIDs = append(evalsIDs, *voter.EvalsID) + } resp, err := client.SendMessageEvent(el.RoomID, SumMessage, SumMessageContent{ Version: Version, EvalsIDs: evalsIDs, JoinID: el.LocalVoter.JoinEvt.ID, - Sum: base64.StdEncoding.EncodeToString(sum.Bytes()), + Sum: base64.StdEncoding.EncodeToString(el.LocalVoter.Sum.Bytes()), }) + el.RUnlock() if err != nil { return err } -- 2.38.4