-
Notifications
You must be signed in to change notification settings - Fork 105
/
types.go
71 lines (58 loc) · 1.77 KB
/
types.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
63
64
65
66
67
68
69
70
71
package types
import (
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/pkg/errors"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
"github.com/axelarnetwork/axelar-core/x/vote/exported"
)
//go:generate moq -out ./mock/types.go -pkg mock . VoteRouter
var _ codectypes.UnpackInterfacesMessage = TalliedVote{}
// UnpackInterfaces implements UnpackInterfacesMessage
func (m TalliedVote) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
var data codec.ProtoMarshaler
return unpacker.UnpackAny(m.Data, &data)
}
// NewTalliedVote is the constructor for TalliedVote
func NewTalliedVote(pollID exported.PollID, data codec.ProtoMarshaler) TalliedVote {
d, err := codectypes.NewAnyWithValue(data)
if err != nil {
panic(err)
}
return TalliedVote{
PollID: pollID,
Tally: sdk.ZeroUint(),
Data: d,
}
}
// TallyVote adds the given voting power to the tallied vote
func (m *TalliedVote) TallyVote(voter sdk.ValAddress, votingPower sdk.Uint, isLate bool) {
if voter == nil {
panic("voter cannot be nil")
}
if m.IsVoterLate == nil {
m.IsVoterLate = make(map[string]bool)
}
m.IsVoterLate[voter.String()] = isLate
m.Tally = m.Tally.Add(votingPower)
}
// ValidateBasic returns an error if the TalliedVote is not valid
func (m TalliedVote) ValidateBasic() error {
if m.Data == nil {
return errors.New("data is nil")
}
if m.Tally.IsZero() {
return errors.New("vote tally is zero")
}
addrs := maps.Keys(m.IsVoterLate)
slices.Sort(addrs)
for _, addr := range addrs {
if _, err := sdk.ValAddressFromBech32(addr); err != nil {
return sdkerrors.Wrapf(err, "voter %s is not a valid address", addr)
}
}
return nil
}