/
msgs.go
152 lines (122 loc) · 3.92 KB
/
msgs.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package types
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
var (
_ sdk.Msg = &MsgAllocationPrecommit{}
_ sdk.Msg = &MsgAllocationCommit{}
)
// var _ codectypes.UnpackInterfacesMessage = &MsgAllocationCommit{}
const (
TypeMsgAllocationPrecommit = "allocation_precommit"
TypeMsgAllocationCommit = "allocation_commit"
)
//////////////////////////
// MsgAllocationPrecommit //
//////////////////////////
// NewMsgAllocationPrecommit return a new MsgAllocationPrecommit
func NewMsgAllocationPrecommit(vote RebalanceVote, salt string, signer sdk.AccAddress, val sdk.ValAddress) (*MsgAllocationPrecommit, error) {
if signer == nil {
return nil, fmt.Errorf("no signer provided")
}
hash, err := vote.Hash(salt, val)
if err != nil {
return nil, err
}
return &MsgAllocationPrecommit{
Precommit: []*AllocationPrecommit{{Hash: hash, CellarId: vote.Cellar.Id}},
Signer: signer.String(),
}, nil
}
// Route implements sdk.Msg
func (m *MsgAllocationPrecommit) Route() string { return ModuleName }
// Type implements sdk.Msg
func (m *MsgAllocationPrecommit) Type() string { return TypeMsgAllocationPrecommit }
// ValidateBasic implements sdk.Msg
func (m *MsgAllocationPrecommit) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(m.Signer); err != nil {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, err.Error())
}
if m.Precommit == nil {
return fmt.Errorf("empty precommit")
}
for _, pc := range m.Precommit {
if len(pc.Hash) == 0 {
return fmt.Errorf("empty precommit hash")
} else if pc.CellarId == "" {
return fmt.Errorf("empty precommit cellar id")
}
}
return nil
}
// GetSignBytes implements sdk.Msg
func (m *MsgAllocationPrecommit) GetSignBytes() []byte {
panic("amino support disabled")
}
// GetSigners implements sdk.Msg
func (m *MsgAllocationPrecommit) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{m.MustGetSigner()}
}
// MustGetSigner returns the signer address
func (m *MsgAllocationPrecommit) MustGetSigner() sdk.AccAddress {
addr, err := sdk.AccAddressFromBech32(m.Signer)
if err != nil {
panic(err)
}
return addr
}
///////////////////////
// MsgAllocationCommit //
///////////////////////
// NewMsgAllocationCommit return a new MsgAllocationPrecommit
func NewMsgAllocationCommit(commits []*Allocation, signer sdk.AccAddress) *MsgAllocationCommit {
if signer == nil {
return nil
}
return &MsgAllocationCommit{
Commit: commits,
Signer: signer.String(),
}
}
// Route implements sdk.Msg
func (m *MsgAllocationCommit) Route() string { return ModuleName }
// Type implements sdk.Msg
func (m *MsgAllocationCommit) Type() string { return TypeMsgAllocationCommit }
// ValidateBasic implements sdk.Msg
func (m *MsgAllocationCommit) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(m.Signer); err != nil {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, err.Error())
}
for _, commit := range m.Commit {
if err := commit.ValidateBasic(); err != nil {
return err
}
}
return nil
}
// GetSignBytes implements sdk.Msg
func (m *MsgAllocationCommit) GetSignBytes() []byte {
panic("amino support disabled")
}
// GetSigners implements sdk.Msg
func (m *MsgAllocationCommit) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{m.MustGetSigner()}
}
// MustGetSigner returns the signer address
func (m *MsgAllocationCommit) MustGetSigner() sdk.AccAddress {
addr, err := sdk.AccAddressFromBech32(m.Signer)
if err != nil {
panic(err)
}
return addr
}
// // UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
// func (m *MsgAllocationCommit) UnpackInterfaces(unpacker codectypes.AnyUnpacker) (err error) {
// return m.Vote.UnpackInterfaces(unpacker)
// }
// // UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
// func (m *QueryOracleDataResponse) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
// return unpacker.UnpackAny(m.OracleData, new(OracleData))
// }