/
msg_server.go
305 lines (249 loc) · 9.85 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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package keeper
import (
"context"
"encoding/hex"
"fmt"
"strconv"
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/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/cosmos/gravity-bridge/module/x/gravity/types"
)
type msgServer struct {
Keeper
}
// NewMsgServerImpl returns an implementation of the gov MsgServer interface
// for the provided Keeper.
func NewMsgServerImpl(keeper Keeper) types.MsgServer {
return &msgServer{Keeper: keeper}
}
var _ types.MsgServer = msgServer{}
func (k msgServer) SetDelegateKeys(c context.Context, msg *types.MsgDelegateKeys) (*types.MsgDelegateKeysResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
valAddr, err := sdk.ValAddressFromBech32(msg.ValidatorAddress)
if err != nil {
return nil, err
}
orchAddr, err := sdk.AccAddressFromBech32(msg.OrchestratorAddress)
if err != nil {
return nil, err
}
ethAddr := common.HexToAddress(msg.EthereumAddress)
// ensure that the validator exists
if k.Keeper.StakingKeeper.Validator(ctx, valAddr) == nil {
return nil, sdkerrors.Wrap(stakingtypes.ErrNoValidatorFound, valAddr.String())
}
// check if the Ethereum address is currently not used
validators := k.getValidatorsByEthereumAddress(ctx, ethAddr)
if len(validators) > 0 {
return nil, sdkerrors.Wrapf(types.ErrDelegateKeys, "ethereum address %s in use", ethAddr)
}
// check if the orchestrator address is currently not used
ethAddrs := k.getEthereumAddressesByOrchestrator(ctx, orchAddr)
if len(ethAddrs) > 0 {
return nil, sdkerrors.Wrapf(types.ErrDelegateKeys, "orchestrator address %s in use", orchAddr)
}
valAccAddr := sdk.AccAddress(valAddr)
valAccSeq, err := k.accountKeeper.GetSequence(ctx, valAccAddr)
if err != nil {
return nil, sdkerrors.Wrapf(types.ErrDelegateKeys, "failed to get sequence for validator account %s", valAccAddr)
}
var nonce uint64
if valAccSeq > 0 {
nonce = valAccSeq - 1
}
signMsgBz := k.cdc.MustMarshal(&types.DelegateKeysSignMsg{
ValidatorAddress: valAddr.String(),
// We decrement since we process the message after the ante-handler which
// increments the nonce.
Nonce: nonce,
})
hash := crypto.Keccak256Hash(signMsgBz).Bytes()
if err = types.ValidateEthereumSignature(hash, msg.EthSignature, ethAddr); err != nil {
return nil, sdkerrors.Wrapf(
types.ErrDelegateKeys,
"failed to validate delegate keys signature for Ethereum address %X; %s",
ethAddr, err,
)
}
k.SetOrchestratorValidatorAddress(ctx, valAddr, orchAddr)
k.setValidatorEthereumAddress(ctx, valAddr, ethAddr)
k.setEthereumOrchestratorAddress(ctx, ethAddr, orchAddr)
ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, msg.Type()),
sdk.NewAttribute(types.AttributeKeySetOrchestratorAddr, orchAddr.String()),
sdk.NewAttribute(types.AttributeKeySetEthereumAddr, ethAddr.Hex()),
sdk.NewAttribute(types.AttributeKeyValidatorAddr, valAddr.String()),
),
)
return &types.MsgDelegateKeysResponse{}, nil
}
// SubmitEthereumTxConfirmation handles MsgSubmitEthereumTxConfirmation
func (k msgServer) SubmitEthereumTxConfirmation(c context.Context, msg *types.MsgSubmitEthereumTxConfirmation) (*types.MsgSubmitEthereumTxConfirmationResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
confirmation, err := types.UnpackConfirmation(msg.Confirmation)
if err != nil {
return nil, err
}
val, err := k.getSignerValidator(ctx, msg.Signer)
if err != nil {
return nil, err
}
otx := k.GetOutgoingTx(ctx, confirmation.GetStoreIndex())
if otx == nil {
return nil, sdkerrors.Wrap(types.ErrInvalid, "couldn't find outgoing tx")
}
gravityID := k.getGravityID(ctx)
checkpoint := otx.GetCheckpoint([]byte(gravityID))
ethAddress := k.GetValidatorEthereumAddress(ctx, val)
if ethAddress != confirmation.GetSigner() {
return nil, sdkerrors.Wrap(types.ErrInvalid, "eth address does not match signer eth address")
}
if err = types.ValidateEthereumSignature(checkpoint, confirmation.GetSignature(), ethAddress); err != nil {
return nil, sdkerrors.Wrap(types.ErrInvalid, fmt.Sprintf(
"signature verification failed ethAddress %s gravityID %s checkpoint %s typeURL %s signature %s err %s",
ethAddress.Hex(),
gravityID,
hex.EncodeToString(checkpoint),
msg.Confirmation.TypeUrl,
hex.EncodeToString(confirmation.GetSignature()),
err,
))
}
// TODO: should validators be able to overwrite their signatures?
if k.getEthereumSignature(ctx, confirmation.GetStoreIndex(), val) != nil {
return nil, sdkerrors.Wrap(types.ErrInvalid, "signature duplicate")
}
key := k.SetEthereumSignature(ctx, confirmation, val)
ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, msg.Type()),
sdk.NewAttribute(types.AttributeKeyEthereumSignatureKey, string(key)),
),
)
return &types.MsgSubmitEthereumTxConfirmationResponse{}, nil
}
// func (k Keeper) ValidateEthereumSignature
// SubmitEthereumEvent handles MsgSubmitEthereumEvent
func (k msgServer) SubmitEthereumEvent(c context.Context, msg *types.MsgSubmitEthereumEvent) (*types.MsgSubmitEthereumEventResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
event, err := types.UnpackEvent(msg.Event)
if err != nil {
return nil, err
}
// return an error if the validator isn't in the active set
val, err := k.getSignerValidator(ctx, msg.Signer)
if err != nil {
return nil, err
}
// Add the claim to the store
_, err = k.recordEventVote(ctx, event, val)
if err != nil {
return nil, sdkerrors.Wrap(err, "create event vote record")
}
// Emit the handle message event
ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, fmt.Sprintf("%T", event)),
// TODO: maybe return something better here? is this the right string representation?
sdk.NewAttribute(types.AttributeKeyEthereumEventVoteRecordID, string(types.MakeEthereumEventVoteRecordKey(event.GetEventNonce(), event.Hash()))),
),
)
return &types.MsgSubmitEthereumEventResponse{}, nil
}
// SendToEthereum handles MsgSendToEthereum
func (k msgServer) SendToEthereum(c context.Context, msg *types.MsgSendToEthereum) (*types.MsgSendToEthereumResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
sender, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
return nil, err
}
txID, err := k.createSendToEthereum(ctx, sender, msg.EthereumRecipient, msg.Amount, msg.BridgeFee)
if err != nil {
return nil, err
}
ctx.EventManager().EmitEvents([]sdk.Event{
sdk.NewEvent(
types.EventTypeBridgeWithdrawalReceived,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
sdk.NewAttribute(types.AttributeKeyContract, k.getBridgeContractAddress(ctx)),
sdk.NewAttribute(types.AttributeKeyBridgeChainID, strconv.Itoa(int(k.getBridgeChainID(ctx)))),
sdk.NewAttribute(types.AttributeKeyOutgoingTXID, strconv.Itoa(int(txID))),
sdk.NewAttribute(types.AttributeKeyNonce, fmt.Sprint(txID)),
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, msg.Type()),
sdk.NewAttribute(types.AttributeKeyOutgoingTXID, fmt.Sprint(txID)),
),
})
return &types.MsgSendToEthereumResponse{Id: txID}, nil
}
// RequestBatchTx handles MsgRequestBatchTx
func (k msgServer) RequestBatchTx(c context.Context, msg *types.MsgRequestBatchTx) (*types.MsgRequestBatchTxResponse, error) {
// TODO: limit this to only orchestrators and validators?
ctx := sdk.UnwrapSDKContext(c)
// Check if the denom is a gravity coin, if not, check if there is a deployed ERC20 representing it.
// If not, error out
_, tokenContract, err := k.DenomToERC20Lookup(ctx, msg.Denom)
if err != nil {
return nil, err
}
batchID := k.BuildBatchTx(ctx, tokenContract, BatchTxSize)
ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, msg.Type()),
sdk.NewAttribute(types.AttributeKeyContract, tokenContract.Hex()),
sdk.NewAttribute(types.AttributeKeyBatchNonce, fmt.Sprint(batchID.BatchNonce)),
),
)
return &types.MsgRequestBatchTxResponse{}, nil
}
func (k msgServer) CancelSendToEthereum(c context.Context, msg *types.MsgCancelSendToEthereum) (*types.MsgCancelSendToEthereumResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
err := k.Keeper.cancelSendToEthereum(ctx, msg.Id, msg.Sender)
if err != nil {
return nil, err
}
ctx.EventManager().EmitEvents([]sdk.Event{
sdk.NewEvent(
types.EventTypeBridgeWithdrawCanceled,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
sdk.NewAttribute(types.AttributeKeyContract, k.getBridgeContractAddress(ctx)),
sdk.NewAttribute(types.AttributeKeyBridgeChainID, strconv.Itoa(int(k.getBridgeChainID(ctx)))),
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, msg.Type()),
sdk.NewAttribute(types.AttributeKeyOutgoingTXID, fmt.Sprint(msg.Id)),
),
})
return &types.MsgCancelSendToEthereumResponse{}, nil
}
// getSignerValidator takes an sdk.AccAddress that represents either a validator or orchestrator address and returns
// the assoicated validator address
func (k Keeper) getSignerValidator(ctx sdk.Context, signerString string) (sdk.ValAddress, error) {
signer, err := sdk.AccAddressFromBech32(signerString)
if err != nil {
return nil, sdkerrors.Wrap(types.ErrInvalid, "signer address")
}
var validatorI stakingtypes.ValidatorI
if validator := k.GetOrchestratorValidatorAddress(ctx, signer); validator == nil {
validatorI = k.StakingKeeper.Validator(ctx, sdk.ValAddress(signer))
} else {
validatorI = k.StakingKeeper.Validator(ctx, validator)
}
if validatorI == nil {
return nil, sdkerrors.Wrap(types.ErrInvalid, "not orchestrator or validator")
} else if !validatorI.IsBonded() {
return nil, sdkerrors.Wrap(types.ErrInvalid, fmt.Sprintf("validator is not bonded: %s", validatorI.GetOperator()))
}
return validatorI.GetOperator(), nil
}