-
Notifications
You must be signed in to change notification settings - Fork 170
/
msg_server.go
180 lines (145 loc) · 5.08 KB
/
msg_server.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package keeper
import (
"context"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/umee-network/umee/v2/x/oracle/types"
)
type msgServer struct {
Keeper
}
// NewMsgServerImpl returns an implementation of the oracle MsgServer interface
// for the provided Keeper.
func NewMsgServerImpl(keeper Keeper) types.MsgServer {
return &msgServer{Keeper: keeper}
}
func (ms msgServer) AggregateExchangeRatePrevote(
goCtx context.Context,
msg *types.MsgAggregateExchangeRatePrevote,
) (*types.MsgAggregateExchangeRatePrevoteResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
valAddr, err := sdk.ValAddressFromBech32(msg.Validator)
if err != nil {
return nil, err
}
feederAddr, err := sdk.AccAddressFromBech32(msg.Feeder)
if err != nil {
return nil, err
}
if err := ms.ValidateFeeder(ctx, feederAddr, valAddr); err != nil {
return nil, err
}
// Ensure prevote wasn't already submitted
if ms.HasAggregateExchangeRatePrevote(ctx, valAddr) {
return nil, types.ErrExistingPrevote
}
// Convert hex string to votehash
voteHash, err := types.AggregateVoteHashFromHexString(msg.Hash)
if err != nil {
return nil, sdkerrors.Wrap(types.ErrInvalidHash, err.Error())
}
aggregatePrevote := types.NewAggregateExchangeRatePrevote(voteHash, valAddr, uint64(ctx.BlockHeight()))
ms.SetAggregateExchangeRatePrevote(ctx, valAddr, aggregatePrevote)
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeAggregatePrevote,
sdk.NewAttribute(types.EventAttrKeyVoter, msg.Validator),
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.EventAttrValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, msg.Feeder),
),
})
return &types.MsgAggregateExchangeRatePrevoteResponse{}, nil
}
func (ms msgServer) AggregateExchangeRateVote(
goCtx context.Context,
msg *types.MsgAggregateExchangeRateVote,
) (*types.MsgAggregateExchangeRateVoteResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
valAddr, err := sdk.ValAddressFromBech32(msg.Validator)
if err != nil {
return nil, err
}
feederAddr, err := sdk.AccAddressFromBech32(msg.Feeder)
if err != nil {
return nil, err
}
if err := ms.ValidateFeeder(ctx, feederAddr, valAddr); err != nil {
return nil, err
}
params := ms.GetParams(ctx)
aggregatePrevote, err := ms.GetAggregateExchangeRatePrevote(ctx, valAddr)
if err != nil {
return nil, sdkerrors.Wrap(types.ErrNoAggregatePrevote, msg.Validator)
}
// Check a msg is submitted proper period
if (uint64(ctx.BlockHeight())/params.VotePeriod)-(aggregatePrevote.SubmitBlock/params.VotePeriod) != 1 {
return nil, types.ErrRevealPeriodMissMatch
}
exchangeRateTuples, err := types.ParseExchangeRateTuples(msg.ExchangeRates)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, err.Error())
}
// Verify a exchange rate with aggregate prevote hash
hash := types.GetAggregateVoteHash(msg.Salt, msg.ExchangeRates, valAddr)
if aggregatePrevote.Hash != hash.String() {
return nil, sdkerrors.Wrapf(types.ErrVerificationFailed, "must be given %s not %s", aggregatePrevote.Hash, hash)
}
// Filter out rates which aren't included in the AcceptList
filteredTuples := types.ExchangeRateTuples{}
for _, tuple := range exchangeRateTuples {
if params.AcceptList.Contains(tuple.Denom) {
filteredTuples = append(filteredTuples, tuple)
}
}
// Move aggregate prevote to aggregate vote with given exchange rates
ms.SetAggregateExchangeRateVote(ctx, valAddr, types.NewAggregateExchangeRateVote(filteredTuples, valAddr))
ms.DeleteAggregateExchangeRatePrevote(ctx, valAddr)
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeAggregateVote,
sdk.NewAttribute(types.EventAttrKeyVoter, msg.Validator),
sdk.NewAttribute(types.EventAttrKeyExchangeRates, msg.ExchangeRates),
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.EventAttrValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, msg.Feeder),
),
})
return &types.MsgAggregateExchangeRateVoteResponse{}, nil
}
func (ms msgServer) DelegateFeedConsent(
goCtx context.Context,
msg *types.MsgDelegateFeedConsent,
) (*types.MsgDelegateFeedConsentResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
operatorAddr, err := sdk.ValAddressFromBech32(msg.Operator)
if err != nil {
return nil, err
}
delegateAddr, err := sdk.AccAddressFromBech32(msg.Delegate)
if err != nil {
return nil, err
}
val := ms.StakingKeeper.Validator(ctx, operatorAddr)
if val == nil {
return nil, sdkerrors.Wrap(stakingtypes.ErrNoValidatorFound, msg.Operator)
}
ms.SetFeederDelegation(ctx, operatorAddr, delegateAddr)
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeFeedDelegate,
sdk.NewAttribute(types.EventAttrKeyFeeder, msg.Delegate),
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.EventAttrValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, msg.Operator),
),
})
return &types.MsgDelegateFeedConsentResponse{}, nil
}