M cmd/tallyard/main.go => cmd/tallyard/main.go +8 -2
@@ 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)
M election/msg.go => election/msg.go +12 -1
@@ 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
}
M election/voter.go => election/voter.go +13 -17
@@ 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
}