Skip to content

Commit

Permalink
x/gravity: add events for jailing and slashing (#187)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderbez authored Sep 9, 2021
1 parent e71fcef commit 51e1ad5
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 21 deletions.
66 changes: 53 additions & 13 deletions module/x/gravity/abci.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gravity

import (
"fmt"
"sort"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -212,12 +213,17 @@ func outgoingTxSlashing(ctx sdk.Context, k keeper.Keeper) {
cons sdk.ConsAddress
}

var valInfos []valInfo
bondedVals := k.StakingKeeper.GetBondedValidatorsByPower(ctx)
valInfos := make([]valInfo, len(bondedVals))

for i, val := range bondedVals {
consAddr, err := val.GetConsAddr()
if err != nil {
panic(fmt.Sprintf("failed to get consensus address: %s", err))
}

for _, val := range k.StakingKeeper.GetBondedValidatorsByPower(ctx) {
consAddr, _ := val.GetConsAddr()
sigs, exist := k.SlashingKeeper.GetValidatorSigningInfo(ctx, consAddr)
valInfos = append(valInfos, valInfo{val, exist, sigs, consAddr})
valInfos[i] = valInfo{val, exist, sigs, consAddr}
}

var unbondingValInfos []valInfo
Expand All @@ -231,9 +237,18 @@ func outgoingTxSlashing(ctx sdk.Context, k keeper.Keeper) {
for ; unbondingValIterator.Valid(); unbondingValIterator.Next() {
unbondingValidators := k.GetUnbondingvalidators(unbondingValIterator.Value())
for _, valAddr := range unbondingValidators.Addresses {
addr, _ := sdk.ValAddressFromBech32(valAddr)
validator, _ := k.StakingKeeper.GetValidator(ctx, sdk.ValAddress(addr))
valConsAddr, _ := validator.GetConsAddr()
addr, err := sdk.ValAddressFromBech32(valAddr)
if err != nil {
panic(fmt.Sprintf("failed to bech32 decode validator address: %s", err))
}

validator, _ := k.StakingKeeper.GetValidator(ctx, addr)

valConsAddr, err := validator.GetConsAddr()
if err != nil {
panic(fmt.Sprintf("failed to get validator consensus address: %s", err))
}

valSigningInfo, exist := k.SlashingKeeper.GetValidatorSigningInfo(ctx, valConsAddr)
unbondingValInfos = append(unbondingValInfos, valInfo{validator, exist, valSigningInfo, valConsAddr})
}
Expand All @@ -247,35 +262,60 @@ func outgoingTxSlashing(ctx sdk.Context, k keeper.Keeper) {
if valInfo.exist && valInfo.sigs.StartHeight < int64(otx.GetCosmosHeight()) {
if _, ok := signatures[valInfo.val.GetOperator().String()]; !ok {
if !valInfo.val.IsJailed() {
power := valInfo.val.ConsensusPower(k.PowerReduction)
k.StakingKeeper.Slash(
ctx,
valInfo.cons,
ctx.BlockHeight(),
valInfo.val.ConsensusPower(k.PowerReduction),
power,
params.SlashFractionBatch,
)
k.StakingKeeper.Jail(ctx, valInfo.cons)

ctx.EventManager().EmitEvent(
sdk.NewEvent(
slashingtypes.EventTypeSlash,
sdk.NewAttribute(slashingtypes.AttributeKeyAddress, valInfo.cons.String()),
sdk.NewAttribute(slashingtypes.AttributeKeyJailed, valInfo.cons.String()),
sdk.NewAttribute(slashingtypes.AttributeKeyReason, types.AttributeMissingBridgeBatchSig),
sdk.NewAttribute(slashingtypes.AttributeKeyPower, fmt.Sprintf("%d", power)),
),
)
}
}
}
}

if sstx, ok := otx.(*types.SignerSetTx); ok {
for _, valInfo := range unbondingValInfos {
// Only slash validators who joined after valset is created and they are unbonding and UNBOND_SLASHING_WINDOW didn't pass
if valInfo.exist && valInfo.sigs.StartHeight < int64(sstx.Nonce) && valInfo.val.IsUnbonding() && sstx.Height < uint64(valInfo.val.UnbondingHeight)+params.UnbondSlashingSignerSetTxsWindow {
// Check if validator has confirmed valset or not
// Only slash validators who joined after valset is created and they are
// unbonding and UNBOND_SLASHING_WINDOW didn't pass.
if valInfo.exist && valInfo.sigs.StartHeight < int64(sstx.Nonce) &&
valInfo.val.IsUnbonding() &&
sstx.Height < uint64(valInfo.val.UnbondingHeight)+params.UnbondSlashingSignerSetTxsWindow {
// check if validator has confirmed valset or not
if _, found := signatures[valInfo.val.GetOperator().String()]; !found {
if !valInfo.val.IsJailed() {
// TODO: do we want to slash jailed validators
// TODO: Do we want to slash jailed validators?
power := valInfo.val.ConsensusPower(k.PowerReduction)
k.StakingKeeper.Slash(
ctx,
valInfo.cons,
ctx.BlockHeight(),
valInfo.val.ConsensusPower(k.PowerReduction),
power,
params.SlashFractionSignerSetTx,
)
k.StakingKeeper.Jail(ctx, valInfo.cons)

ctx.EventManager().EmitEvent(
sdk.NewEvent(
slashingtypes.EventTypeSlash,
sdk.NewAttribute(slashingtypes.AttributeKeyAddress, valInfo.cons.String()),
sdk.NewAttribute(slashingtypes.AttributeKeyJailed, valInfo.cons.String()),
sdk.NewAttribute(slashingtypes.AttributeKeyReason, types.AttributeMissingBridgeBatchSig),
sdk.NewAttribute(slashingtypes.AttributeKeyPower, fmt.Sprintf("%d", power)),
),
)
}
}
}
Expand Down
17 changes: 9 additions & 8 deletions module/x/gravity/types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ const (
EventTypeBridgeDepositReceived = "deposit_received"
EventTypeBridgeWithdrawCanceled = "withdraw_canceled"

AttributeKeyEthereumEventVoteRecordID = "ethereum_event_vote_record_id"
AttributeKeyBatchConfirmKey = "batch_confirm_key"
AttributeKeyEthereumSignatureKey = "ethereum_signature_key"
AttributeKeyOutgoingBatchID = "batch_id"
AttributeKeyEthereumEventVoteRecordID = "ethereum_event_vote_record_id"
AttributeKeyBatchConfirmKey = "batch_confirm_key"
AttributeKeyEthereumSignatureKey = "ethereum_signature_key"
AttributeKeyOutgoingBatchID = "batch_id"
AttributeKeyOutgoingTXID = "outgoing_tx_id"
AttributeKeyEthereumEventType = "ethereum_event_type"
AttributeKeyContract = "bridge_contract"
Expand All @@ -26,8 +26,9 @@ const (
AttributeKeyValidatorAddr = "validator_address"
AttributeKeyContractCallInvalidationScope = "contract_call_invalidation_scope"
AttributeKeyContractCallInvalidationNonce = "contract_call_invalidation_nonce"
AttributeKeyContractCallPayload = "contract_call_payload"
AttributeKeyContractCallTokens = "contract_call_tokens"
AttributeKeyContractCallFees = "contract_call_fees"
AttributeKeyEthTxTimeout = "eth_tx_timeout"
AttributeKeyContractCallPayload = "contract_call_payload"
AttributeKeyContractCallTokens = "contract_call_tokens"
AttributeKeyContractCallFees = "contract_call_fees"
AttributeKeyEthTxTimeout = "eth_tx_timeout"
AttributeMissingBridgeBatchSig = "missing_bridge_batch_signature"
)

0 comments on commit 51e1ad5

Please sign in to comment.