forked from maticnetwork/heimdall
-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.go
233 lines (193 loc) · 7.49 KB
/
handler.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package staking
import (
"bytes"
"fmt"
"math/big"
sdk "github.com/cosmos/cosmos-sdk/types"
hmCommon "github.com/maticnetwork/heimdall/common"
"github.com/maticnetwork/heimdall/helper"
"github.com/maticnetwork/heimdall/staking/types"
hmTypes "github.com/maticnetwork/heimdall/types"
)
// NewHandler new handler
func NewHandler(k Keeper, contractCaller helper.IContractCaller) sdk.Handler {
return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
ctx = ctx.WithEventManager(sdk.NewEventManager())
switch msg := msg.(type) {
case types.MsgValidatorJoin:
return HandleMsgValidatorJoin(ctx, msg, k, contractCaller)
case types.MsgValidatorExit:
return HandleMsgValidatorExit(ctx, msg, k, contractCaller)
case types.MsgSignerUpdate:
return HandleMsgSignerUpdate(ctx, msg, k, contractCaller)
case types.MsgStakeUpdate:
return HandleMsgStakeUpdate(ctx, msg, k, contractCaller)
default:
return sdk.ErrTxDecode("Invalid message in staking module").Result()
}
}
}
// HandleMsgValidatorJoin msg validator join
func HandleMsgValidatorJoin(ctx sdk.Context, msg types.MsgValidatorJoin, k Keeper, contractCaller helper.IContractCaller) sdk.Result {
k.Logger(ctx).Debug("✅ Validating validator join msg",
"validatorId", msg.ID,
"activationEpoch", msg.ActivationEpoch,
"amount", msg.Amount,
"SignerPubkey", msg.SignerPubKey.String(),
"txHash", msg.TxHash,
"logIndex", msg.LogIndex,
"blockNumber", msg.BlockNumber,
)
// Generate PubKey from Pubkey in message and signer
pubkey := msg.SignerPubKey
signer := pubkey.Address()
// Check if validator has been validator before
if _, ok := k.GetSignerFromValidatorID(ctx, msg.ID); ok {
k.Logger(ctx).Error("Validator has been validator before, cannot join with same ID", "validatorId", msg.ID)
return hmCommon.ErrValidatorAlreadyJoined(k.Codespace()).Result()
}
// get validator by signer
checkVal, err := k.GetValidatorInfo(ctx, signer.Bytes())
if err == nil || bytes.Equal(checkVal.Signer.Bytes(), signer.Bytes()) {
return hmCommon.ErrValidatorAlreadyJoined(k.Codespace()).Result()
}
// validate voting power
_, err = helper.GetPowerFromAmount(msg.Amount.BigInt())
if err != nil {
return hmCommon.ErrInvalidMsg(k.Codespace(), fmt.Sprintf("Invalid amount %v for validator %v", msg.Amount, msg.ID)).Result()
}
// sequence id
blockNumber := new(big.Int).SetUint64(msg.BlockNumber)
sequence := new(big.Int).Mul(blockNumber, big.NewInt(hmTypes.DefaultLogIndexUnit))
sequence.Add(sequence, new(big.Int).SetUint64(msg.LogIndex))
// check if incoming tx is older
if k.HasStakingSequence(ctx, sequence.String()) {
k.Logger(ctx).Error("Older invalid tx found")
return hmCommon.ErrOldTx(k.Codespace()).Result()
}
return sdk.Result{
Events: ctx.EventManager().Events(),
}
}
// HandleMsgStakeUpdate handles stake update message
func HandleMsgStakeUpdate(ctx sdk.Context, msg types.MsgStakeUpdate, k Keeper, contractCaller helper.IContractCaller) sdk.Result {
k.Logger(ctx).Debug("✅ Validating stake update msg",
"validatorID", msg.ID,
"newAmount", msg.NewAmount,
"txHash", msg.TxHash,
"logIndex", msg.LogIndex,
"blockNumber", msg.BlockNumber,
)
// pull validator from store
_, ok := k.GetValidatorFromValID(ctx, msg.ID)
if !ok {
k.Logger(ctx).Error("Fetching of validator from store failed", "validatorId", msg.ID)
return hmCommon.ErrNoValidator(k.Codespace()).Result()
}
// sequence id
blockNumber := new(big.Int).SetUint64(msg.BlockNumber)
sequence := new(big.Int).Mul(blockNumber, big.NewInt(hmTypes.DefaultLogIndexUnit))
sequence.Add(sequence, new(big.Int).SetUint64(msg.LogIndex))
// check if incoming tx is older
if k.HasStakingSequence(ctx, sequence.String()) {
k.Logger(ctx).Error("Older invalid tx found")
return hmCommon.ErrOldTx(k.Codespace()).Result()
}
// pull validator from store
validator, ok := k.GetValidatorFromValID(ctx, msg.ID)
if !ok {
k.Logger(ctx).Error("Fetching of validator from store failed", "validatorId", msg.ID)
return hmCommon.ErrNoValidator(k.Codespace()).Result()
}
if msg.Nonce != validator.Nonce+1 {
k.Logger(ctx).Error("Incorrect validator nonce")
return hmCommon.ErrNonce(k.Codespace()).Result()
}
// set validator amount
_, err := helper.GetPowerFromAmount(msg.NewAmount.BigInt())
if err != nil {
return hmCommon.ErrInvalidMsg(k.Codespace(), fmt.Sprintf("Invalid newamount %v for validator %v", msg.NewAmount, msg.ID)).Result()
}
return sdk.Result{
Events: ctx.EventManager().Events(),
}
}
// HandleMsgSignerUpdate handles signer update message
func HandleMsgSignerUpdate(ctx sdk.Context, msg types.MsgSignerUpdate, k Keeper, contractCaller helper.IContractCaller) sdk.Result {
k.Logger(ctx).Debug("✅ Validating signer update msg",
"validatorID", msg.ID,
"NewSignerPubkey", msg.NewSignerPubKey.String(),
"txHash", msg.TxHash,
"logIndex", msg.LogIndex,
"blockNumber", msg.BlockNumber,
)
newPubKey := msg.NewSignerPubKey
newSigner := newPubKey.Address()
// pull validator from store
validator, ok := k.GetValidatorFromValID(ctx, msg.ID)
if !ok {
k.Logger(ctx).Error("Fetching of validator from store failed", "validatorId", msg.ID)
return hmCommon.ErrNoValidator(k.Codespace()).Result()
}
// sequence id
blockNumber := new(big.Int).SetUint64(msg.BlockNumber)
sequence := new(big.Int).Mul(blockNumber, big.NewInt(hmTypes.DefaultLogIndexUnit))
sequence.Add(sequence, new(big.Int).SetUint64(msg.LogIndex))
// check if incoming tx is older
if k.HasStakingSequence(ctx, sequence.String()) {
k.Logger(ctx).Error("Older invalid tx found")
return hmCommon.ErrOldTx(k.Codespace()).Result()
}
// check if new signer address is same as existing signer
if bytes.Equal(newSigner.Bytes(), validator.Signer.Bytes()) {
// No signer change
k.Logger(ctx).Error("NewSigner same as OldSigner.")
return hmCommon.ErrNoSignerChange(k.Codespace()).Result()
}
// check nonce validity
if msg.Nonce != validator.Nonce+1 {
k.Logger(ctx).Error("Incorrect validator nonce")
return hmCommon.ErrNonce(k.Codespace()).Result()
}
return sdk.Result{
Events: ctx.EventManager().Events(),
}
}
// HandleMsgValidatorExit handle msg validator exit
func HandleMsgValidatorExit(ctx sdk.Context, msg types.MsgValidatorExit, k Keeper, contractCaller helper.IContractCaller) sdk.Result {
k.Logger(ctx).Debug("✅ Validating validator exit msg",
"validatorID", msg.ID,
"deactivatonEpoch", msg.DeactivationEpoch,
"txHash", msg.TxHash,
"logIndex", msg.LogIndex,
"blockNumber", msg.BlockNumber,
)
validator, ok := k.GetValidatorFromValID(ctx, msg.ID)
if !ok {
k.Logger(ctx).Error("Fetching of validator from store failed", "validatorID", msg.ID)
return hmCommon.ErrNoValidator(k.Codespace()).Result()
}
k.Logger(ctx).Debug("validator in store", "validator", validator)
// check if validator deactivation period is set
if validator.EndEpoch != 0 {
k.Logger(ctx).Error("Validator already unbonded")
return hmCommon.ErrValUnbonded(k.Codespace()).Result()
}
// sequence id
blockNumber := new(big.Int).SetUint64(msg.BlockNumber)
sequence := new(big.Int).Mul(blockNumber, big.NewInt(hmTypes.DefaultLogIndexUnit))
sequence.Add(sequence, new(big.Int).SetUint64(msg.LogIndex))
// check if incoming tx is older
if k.HasStakingSequence(ctx, sequence.String()) {
k.Logger(ctx).Error("Older invalid tx found")
return hmCommon.ErrOldTx(k.Codespace()).Result()
}
// check nonce validity
if msg.Nonce != validator.Nonce+1 {
k.Logger(ctx).Error("Incorrect validator nonce")
return hmCommon.ErrNonce(k.Codespace()).Result()
}
return sdk.Result{
Events: ctx.EventManager().Events(),
}
}