Skip to content

Commit

Permalink
chore: jail validators with invalid balances (#1192)
Browse files Browse the repository at this point in the history
# Related Github tickets

- VolumeFi#1037

# Background

If validators have invalid (or missing) balances, we jail them.
Alternatively we could leave them out of the relayer pool, but this
makes it more visible when something has failed.

For this mechanism to work properly, we need
palomachain/pigeon#399 to be merged as well.

# Testing completed

- [x] test coverage exists or has been added/updated
- [x] tested in a private testnet

# Breaking changes

- [x] I have checked my code for breaking changes
- [x] If there are breaking changes, there is a supporting migration.
  • Loading branch information
maharifu authored Jun 12, 2024
1 parent 4a39be0 commit d4a0c49
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
15 changes: 13 additions & 2 deletions x/evm/keeper/attest_validator_balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func (k Keeper) processValidatorBalanceProof(
if err != nil {
k.Logger(ctx).Error("error while getting validator address", err)
}

hexAddr, balanceStr := common.HexToAddress(request.HexAddresses[i]), winner.Balances[i]
balance, ok := new(big.Int).SetString(balanceStr, 10)
if !ok {
Expand All @@ -63,8 +64,17 @@ func (k Keeper) processValidatorBalanceProof(
"val-addr", valAddr,
"eth-addr", hexAddr,
)
// WHAT TO DO NOW?!?!?! jail the poor fellow that has invalid balance format??
// blame the flock for reporting this??!?

reason := fmt.Sprintf(types.JailReasonInvalidBalance, chainReferenceID, balanceStr)
if err = k.Valset.Jail(ctx, valAddr, reason); err != nil {
k.Logger(ctx).Error(
"error jailing validator",
"err", err,
"val-addr", valAddr,
"eth-addr", hexAddr,
)
}

continue
}

Expand All @@ -76,6 +86,7 @@ func (k Keeper) processValidatorBalanceProof(
"eth-addr", hexAddr,
)
}

if balance.Cmp(minBalance) == -1 || balance.Cmp(big.NewInt(0)) == 0 {
isJailed, err := k.Valset.IsJailed(ctx, valAddr)
if err != nil {
Expand Down
47 changes: 47 additions & 0 deletions x/evm/keeper/attest_validator_balances_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper

import (
"fmt"
"math/big"
"time"

Expand Down Expand Up @@ -88,4 +89,50 @@ var _ = g.Describe("attest validator balance", func() {
Expect(err).To(BeNil())
})
})

g.Context("with invalid balances", func() {
g.BeforeEach(func() {
minBalance = big.NewInt(50)
validatorBalances := []string{"1000", "", "5000"}
req = &types.ValidatorBalancesAttestation{
FromBlockTime: time.Unix(999, 0),
HexAddresses: slice.Map(validatorBalances, func(b string) string {
return common.BytesToAddress([]byte("addr_" + b)).Hex()
}),
ValAddresses: slice.Map(validatorBalances, func(b string) sdk.ValAddress {
return sdk.ValAddress("addr_" + b)
}),
}
evidenceThatWon = &types.ValidatorBalancesAttestationRes{
BlockHeight: 123,
Balances: validatorBalances,
}

reason := fmt.Sprintf(types.JailReasonInvalidBalance, "chain-id", "")
v.On("Jail", mock.Anything, req.ValAddresses[1], reason).Return(nil)

for i, balance := range validatorBalances {
// The ones with invalid balance won't be updated, so we skip
if balance == "" {
continue
}

n, _ := new(big.Int).SetString(balance, 10)
v.On(
"SetValidatorBalance",
mock.Anything,
req.ValAddresses[i],
"evm",
"chain-id",
req.HexAddresses[i],
n,
).Return(nil)
}
})

g.It("updates the balances and jail those with invalid balances", func() {
err := subject()
Expect(err).To(BeNil())
})
})
})
1 change: 1 addition & 0 deletions x/evm/types/jail_reason.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ package types

const (
JailReasonNotEnoughFunds = "not enough funds in the target chain (%s). Has %s. Min required: %s."
JailReasonInvalidBalance = "invalid balance information in the target chain (%s): %s."
)

0 comments on commit d4a0c49

Please sign in to comment.