-
Notifications
You must be signed in to change notification settings - Fork 210
/
votes_calc.go
62 lines (51 loc) · 1.78 KB
/
votes_calc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package tortoisebeacon
import (
"context"
"fmt"
"math/big"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/log"
)
func (tb *TortoiseBeacon) calcVotes(ctx context.Context, epoch types.EpochID, round types.RoundID) (allVotes, []string, error) {
logger := tb.logger.WithContext(ctx).WithFields(epoch, round)
tb.mu.Lock()
defer tb.mu.Unlock()
logger.With().Debug("calculating votes", log.String("vote_margins", fmt.Sprint(tb.votesMargin)))
ownCurrentRoundVotes, undecided, err := tb.calcOwnCurrentRoundVotes()
if err != nil {
return allVotes{}, nil, fmt.Errorf("calc own current round votes: %w", err)
}
logger.With().Debug("calculated votes for one round",
log.String("for_votes", fmt.Sprint(ownCurrentRoundVotes.valid)),
log.String("against_votes", fmt.Sprint(ownCurrentRoundVotes.invalid)))
return ownCurrentRoundVotes, undecided, nil
}
func (tb *TortoiseBeacon) calcOwnCurrentRoundVotes() (allVotes, []string, error) {
ownCurrentRoundVotes := allVotes{
valid: make(proposalSet),
invalid: make(proposalSet),
}
positiveVotingThreshold := tb.votingThreshold(tb.epochWeight)
negativeThreshold := new(big.Int).Neg(positiveVotingThreshold)
var undecided []string
for vote, weightCount := range tb.votesMargin {
switch {
case weightCount.Cmp(positiveVotingThreshold) >= 0:
ownCurrentRoundVotes.valid[vote] = struct{}{}
case weightCount.Cmp(negativeThreshold) <= 0:
ownCurrentRoundVotes.invalid[vote] = struct{}{}
default:
undecided = append(undecided, vote)
}
}
return ownCurrentRoundVotes, undecided, nil
}
func tallyUndecided(votes *allVotes, undecided []string, coinFlip bool) {
for _, vote := range undecided {
if coinFlip {
votes.valid[vote] = struct{}{}
} else {
votes.invalid[vote] = struct{}{}
}
}
}