forked from althea-net/cosmos-gravity-bridge
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathattestation_handler.go
343 lines (317 loc) · 14.1 KB
/
attestation_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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package keeper
import (
"fmt"
distrkeeper "github.com/cosmos/cosmos-sdk/x/distribution/keeper"
"math/big"
"strconv"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
distypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
"github.com/onomyprotocol/arc/module/eth/x/gravity/types"
)
// Check that distKeeper implements the expected type
var _ types.DistributionKeeper = (*distrkeeper.Keeper)(nil)
// AttestationHandler processes `observed` Attestations
type AttestationHandler struct {
// NOTE: If you add anything to this struct, add a nil check to ValidateMembers below!
keeper *Keeper
bankKeeper *bankkeeper.BaseKeeper
distKeeper *distrkeeper.Keeper
}
// Check for nil members
func (a AttestationHandler) ValidateMembers() {
if a.keeper == nil {
panic("Nil keeper!")
}
if a.bankKeeper == nil {
panic("Nil bankKeeper!")
}
if a.distKeeper == nil {
panic("Nil distKeeper!")
}
}
// SendToCommunityPool handles sending incorrect deposits to the community pool, since the deposits
// have already been made on Ethereum there's nothing we can do to reverse them, and we should at least
// make use of the tokens which would otherwise be lost
func (a AttestationHandler) SendToCommunityPool(ctx sdk.Context, coins sdk.Coins) error {
if err := a.bankKeeper.SendCoinsFromModuleToModule(ctx, types.ModuleName, distypes.ModuleName, coins); err != nil {
return sdkerrors.Wrap(err, "transfer to community pool failed")
}
feePool := (*a.distKeeper).GetFeePool(ctx)
feePool.CommunityPool = feePool.CommunityPool.Add(sdk.NewDecCoinsFromCoins(coins...)...)
(*a.distKeeper).SetFeePool(ctx, feePool)
return nil
}
// Handle is the entry point for Attestation processing.
func (a AttestationHandler) Handle(ctx sdk.Context, att types.Attestation, claim types.EthereumClaim) error {
switch claim := claim.(type) {
// deposit in this context means a deposit into the Ethereum side of the bridge
case *types.MsgSendToCosmosClaim:
invalidAddress := false
receiverAddress, addressErr := types.IBCAddressFromBech32(claim.CosmosReceiver)
if addressErr != nil {
invalidAddress = true
}
tokenAddress, errTokenAddress := types.NewEthAddress(claim.TokenContract)
ethereumSender, errEthereumSender := types.NewEthAddress(claim.EthereumSender)
// these are not possible unless the validators get together and submit
// a bogus event, this would create lost tokens stuck in the bridge
// and not accessible to anyone
if errTokenAddress != nil {
hash, _ := claim.ClaimHash()
a.keeper.logger(ctx).Error("Invalid token contract",
"cause", errTokenAddress.Error(),
"claim type", claim.GetType(),
"id", types.GetAttestationKey(claim.GetEventNonce(), hash),
"nonce", fmt.Sprint(claim.GetEventNonce()),
)
return sdkerrors.Wrap(errTokenAddress, "invalid token contract on claim")
}
if errEthereumSender != nil {
hash, _ := claim.ClaimHash()
a.keeper.logger(ctx).Error("Invalid ethereum sender",
"cause", errEthereumSender.Error(),
"claim type", claim.GetType(),
"id", types.GetAttestationKey(claim.GetEventNonce(), hash),
"nonce", fmt.Sprint(claim.GetEventNonce()),
)
return sdkerrors.Wrap(errTokenAddress, "invalid ethereum sender on claim")
}
// While not strictly necessary, explicitly making the receiver a native address
// insulates us from the implicit address conversion done in x/bank's account store iterator
nativeReceiver, err := types.GetNativePrefixedAccAddress(receiverAddress)
if err != nil {
invalidAddress = true
}
// Checks the address if it's inside the blacklisted address list and marks
// if it's inside the list.
if a.keeper.IsOnBlacklist(ctx, *ethereumSender) {
invalidAddress = true
}
// Check if coin is Cosmos-originated asset and get denom
isCosmosOriginated, denom := a.keeper.ERC20ToDenomLookup(ctx, *tokenAddress)
coins := sdk.Coins{sdk.NewCoin(denom, claim.Amount)}
if !isCosmosOriginated {
swapPair := a.keeper.GetParams(ctx).Erc20ToDenomPermanentSwap
if swapPair.Erc20 != "" && swapPair.Denom != "" && denom == types.GravityDenomPrefix+swapPair.Erc20 {
denom = swapPair.Denom
coins[0].Denom = swapPair.Denom
}
// We need to mint eth-originated coins (aka vouchers)
// Make sure that users are not bridging an impossible amount
prevSupply := a.bankKeeper.GetSupply(ctx, denom)
newSupply := new(big.Int).Add(prevSupply.Amount.BigInt(), claim.Amount.BigInt())
if newSupply.BitLen() > 256 { // new supply overflows uint256
a.keeper.logger(ctx).Error("Deposit Overflow",
"claim type", claim.GetType(),
"nonce", fmt.Sprint(claim.GetEventNonce()),
)
return sdkerrors.Wrap(types.ErrIntOverflowAttestation, "invalid supply after SendToCosmos attestation")
}
if err := a.bankKeeper.MintCoins(ctx, types.ModuleName, coins); err != nil {
// in this case we have lost tokens! They are in the bridge, but not
// in the community pool our out in some users balance, every instance of this
// error needs to be detected and resolved
hash, _ := claim.ClaimHash()
a.keeper.logger(ctx).Error("Failed minting",
"cause", err.Error(),
"claim type", claim.GetType(),
"id", types.GetAttestationKey(claim.GetEventNonce(), hash),
"nonce", fmt.Sprint(claim.GetEventNonce()),
)
return sdkerrors.Wrapf(err, "mint vouchers coins: %s", coins)
}
}
if !invalidAddress { // valid address so far, try to lock up the coins in the requested cosmos address
if err := a.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, nativeReceiver, coins); err != nil {
// someone attempted to send tokens to a blacklisted user from Ethereum, log and send to Community pool
hash, _ := claim.ClaimHash()
a.keeper.logger(ctx).Error("Blacklisted deposit",
"cause", err.Error(),
"claim type", claim.GetType(),
"id", types.GetAttestationKey(claim.GetEventNonce(), hash),
"nonce", fmt.Sprint(claim.GetEventNonce()),
)
invalidAddress = true
}
}
// for whatever reason above, blacklisted, invalid string, etc this deposit is not valid
// we can't send the tokens back on the Ethereum side, and if we don't put them somewhere on
// the cosmos side they will be lost an inaccessible even though they are locked in the bridge.
// so we deposit the tokens into the community pool for later use
if invalidAddress {
if err = a.SendToCommunityPool(ctx, coins); err != nil {
hash, _ := claim.ClaimHash()
a.keeper.logger(ctx).Error("Failed community pool send",
"cause", err.Error(),
"claim type", claim.GetType(),
"id", types.GetAttestationKey(claim.GetEventNonce(), hash),
"nonce", fmt.Sprint(claim.GetEventNonce()),
)
return sdkerrors.Wrap(err, "failed to send to Community pool")
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeInvalidSendToCosmosReceiver,
sdk.NewAttribute("MsgSendToCosmosAmount", claim.Amount.String()),
sdk.NewAttribute("MsgSendToCosmosNonce", strconv.Itoa(int(claim.GetEventNonce()))),
sdk.NewAttribute("MsgSendToCosmosToken", tokenAddress.GetAddress()),
sdk.NewAttribute("MsgSendToCosmosSender", claim.EthereumSender),
),
)
} else {
ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute("MsgSendToCosmosAmount", claim.Amount.String()),
sdk.NewAttribute("MsgSendToCosmosNonce", strconv.Itoa(int(claim.GetEventNonce()))),
sdk.NewAttribute("MsgSendToCosmosToken", tokenAddress.GetAddress()),
),
)
}
// withdraw in this context means a withdraw from the Ethereum side of the bridge
case *types.MsgBatchSendToEthClaim:
contract, err := types.NewEthAddress(claim.TokenContract)
if err != nil {
return sdkerrors.Wrap(err, "invalid token contract on batch")
}
a.keeper.OutgoingTxBatchExecuted(ctx, *contract, claim.BatchNonce)
ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute("MsgBatchSendToEthClaim", strconv.Itoa(int(claim.BatchNonce))),
),
)
return nil
case *types.MsgERC20DeployedClaim:
tokenAddress, err := types.NewEthAddress(claim.TokenContract)
if err != nil {
return sdkerrors.Wrap(err, "invalid token contract on claim")
}
// Check if it already exists
existingERC20, exists := a.keeper.GetCosmosOriginatedERC20(ctx, claim.CosmosDenom)
if exists && existingERC20 != nil && tokenAddress != nil && existingERC20.GetAddress() != tokenAddress.GetAddress() {
return sdkerrors.Wrap(
types.ErrInvalid,
fmt.Sprintf("ERC20 %s already exists for denom %s", existingERC20, claim.CosmosDenom))
}
// Check if denom exists
metadata, ok := a.keeper.bankKeeper.GetDenomMetaData(ctx, claim.CosmosDenom)
if !ok || metadata.Base == "" {
return sdkerrors.Wrap(types.ErrUnknown, fmt.Sprintf("denom not found %s", claim.CosmosDenom))
}
// Check if attributes of ERC20 match Cosmos denom
if claim.Name != metadata.Name {
return sdkerrors.Wrap(
types.ErrInvalid,
fmt.Sprintf("ERC20 name %s does not match denom name %s", claim.Name, metadata.Description))
}
if claim.Symbol != metadata.Symbol {
return sdkerrors.Wrap(
types.ErrInvalid,
fmt.Sprintf("ERC20 symbol %s does not match denom symbol %s", claim.Symbol, metadata.Display))
}
// ERC20 tokens use a very simple mechanism to tell you where to display the decimal point.
// The "decimals" field simply tells you how many decimal places there will be.
// Cosmos denoms have a system that is much more full featured, with enterprise-ready token denominations.
// There is a DenomUnits array that tells you what the name of each denomination of the
// token is.
// To correlate this with an ERC20 "decimals" field, we have to search through the DenomUnits array
// to find the DenomUnit which matches up to the main token "display" value. Then we take the
// "exponent" from this DenomUnit.
// If the correct DenomUnit is not found, it will default to 0. This will result in there being no decimal places
// in the token's ERC20 on Ethereum. So, for example, if this happened with Atom, 1 Atom would appear on Ethereum
// as 1 million Atoms, having 6 extra places before the decimal point.
// This will only happen with a Denom Metadata which is for all intents and purposes invalid, but I am not sure
// this is checked for at any other point.
decimals := uint32(0)
for _, denomUnit := range metadata.DenomUnits {
if denomUnit.Denom == metadata.Display {
decimals = denomUnit.Exponent
break
}
}
if decimals != uint32(claim.Decimals) {
return sdkerrors.Wrap(
types.ErrInvalid,
fmt.Sprintf("ERC20 decimals %d does not match denom decimals %d", claim.Decimals, decimals))
}
// Add to denom-erc20 mapping
a.keeper.setCosmosOriginatedDenomToERC20(ctx, claim.CosmosDenom, *tokenAddress)
ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute("MsgERC20DeployedClaimToken", tokenAddress.GetAddress()),
sdk.NewAttribute("MsgERC20DeployedClaim", strconv.Itoa(int(claim.GetEventNonce()))),
),
)
case *types.MsgValsetUpdatedClaim:
rewardAddress, err := types.NewEthAddress(claim.RewardToken)
if err != nil {
return sdkerrors.Wrap(err, "invalid reward token on claim")
}
// TODO here we should check the contents of the validator set against
// the store, if they differ we should take some action to indicate to the
// user that bridge highjacking has occurred
a.keeper.SetLastObservedValset(ctx, types.Valset{
Nonce: claim.ValsetNonce,
Members: claim.Members,
Height: 0,
RewardAmount: claim.RewardAmount,
RewardToken: claim.RewardToken,
})
// if the reward is greater than zero and the reward token
// is valid then some reward was issued by this validator set
// and we need to either add to the total tokens for a Cosmos native
// token, or burn non cosmos native tokens
if claim.RewardAmount.GT(sdk.ZeroInt()) && claim.RewardToken != types.ZeroAddressString {
// Check if coin is Cosmos-originated asset and get denom
isCosmosOriginated, denom := a.keeper.ERC20ToDenomLookup(ctx, *rewardAddress)
if isCosmosOriginated {
// If it is cosmos originated, mint some coins to account
// for coins that now exist on Ethereum and may eventually come
// back to Cosmos.
//
// Note the flow is
// user relays valset and gets reward -> event relayed to cosmos mints tokens to module
// -> user sends tokens to cosmos and gets the minted tokens from the module
//
// it is not possible for this to be a race condition thanks to the event nonces
// no matter how long it takes to relay the valset updated event the deposit event
// for the user will always come after.
//
// Note we are minting based on the claim! This is important as the reward value
// could change between when this event occurred and the present
coins := sdk.Coins{sdk.NewCoin(denom, claim.RewardAmount)}
if err := a.bankKeeper.MintCoins(ctx, types.ModuleName, coins); err != nil {
ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute("MsgValsetUpdatedClaim", strconv.Itoa(int(claim.GetEventNonce()))),
),
)
return sdkerrors.Wrapf(err, "unable to mint cosmos originated coins %v", coins)
}
} else {
// // If it is not cosmos originated, burn the coins (aka Vouchers)
// // so that we don't think we have more in the bridge than we actually do
// coins := sdk.Coins{sdk.NewCoin(denom, claim.RewardAmount)}
// a.bankKeeper.BurnCoins(ctx, types.ModuleName, coins)
// if you want to issue Ethereum originated tokens remove this panic and uncomment
// the above code but note that you will have to constantly replenish the tokens in the
// module or your chain will eventually halt.
panic("Can not use Ethereum originated token as reward!")
}
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute("MsgValsetUpdatedClaim", strconv.Itoa(int(claim.GetEventNonce()))),
),
)
default:
panic(fmt.Sprintf("Invalid event type for attestations %s", claim.GetType()))
}
return nil
}