Skip to content

Commit

Permalink
Hotfix/v1.2.3 (#308)
Browse files Browse the repository at this point in the history
* handle nil performance delegations

* handle v046 balance callbacks

* add v046 callback tests
  • Loading branch information
Joe Bowman authored Feb 20, 2023
1 parent d4a45f7 commit aa898c9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 26 deletions.
29 changes: 5 additions & 24 deletions x/interchainstaking/keeper/callbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/cosmos/cosmos-sdk/types/bech32"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/tx"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
Expand Down Expand Up @@ -423,38 +424,18 @@ func AccountBalanceCallback(k Keeper, ctx sdk.Context, args []byte, query icqtyp
return errors.New("account balance icq request must always have a length of at least 2 bytes")
}
balancesStore := query.Request[1:]
accAddr, _, err := banktypes.AddressAndDenomFromBalancesStore(balancesStore)
accAddr, denom, err := banktypes.AddressAndDenomFromBalancesStore(balancesStore)
if err != nil {
return err
}

coin := sdk.Coin{}
// this can legitimately be nil, so do not guard against nil byte slice here.
err = k.cdc.Unmarshal(args, &coin)
coin, err := bankkeeper.UnmarshalBalanceCompat(k.cdc, args, denom)
if err != nil {
k.Logger(ctx).Error("unable to unmarshal balance info for zone", "zone", zone.ChainId, "err", err)
return err
}

checkCoin, err := utils.CoinFromRequestKey(query.Request, accAddr)
if err != nil {
return err
}

if coin.IsNil() || coin.Denom == "" {
// if the balance returned is zero for a given denom, we just get a nil response.
// use the denom from the request so we can set a zero value coin for the correct denom.
coin = checkCoin
} else if coin.Denom != checkCoin.Denom {
return fmt.Errorf("received coin denom %s does not match requested denom %s", coin.Denom, checkCoin.Denom)
}

// By this point we've tried all means to retrieve the balance, so coin should not be nil.
// Please see https://github.com/ingenuity-build/quicksilver-incognito/issues/79#issuecomment-1340293800
if coin.IsNil() || coin.Amount.IsNil() {
err = fmt.Errorf("failed to retrieve Coin.Amount even after trying to look up from RequestKey: %q", query.Request)
k.Logger(ctx).Error("unable to retrieve balance info for zone", "zone", zone.ChainId, "err", err)
return err
if coin.Denom != denom {
return fmt.Errorf("received coin denom %s does not match requested denom %s", coin.Denom, denom)
}

// Ensure that the coin is valid.
Expand Down
34 changes: 32 additions & 2 deletions x/interchainstaking/keeper/callbacks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,36 @@ func (s *KeeperTestSuite) TestAccountBalanceCallbackMismatch() {
})
}

func (s *KeeperTestSuite) TestAccountBalance046Callback() {
s.Run("account balance", func() {
s.SetupTest()
s.setupTestZones()

app := s.GetQuicksilverApp(s.chainA)
app.InterchainstakingKeeper.CallbackHandler().RegisterCallbacks()
ctx := s.chainA.GetContext()

zone, _ := app.InterchainstakingKeeper.GetZone(ctx, s.chainB.ChainID)
zone.DepositAddress.IncrementBalanceWaitgroup()
zone.WithdrawalAddress.IncrementBalanceWaitgroup()
app.InterchainstakingKeeper.SetZone(ctx, &zone)

response := sdk.NewInt(10)

respbz, err := response.Marshal()
s.Require().NoError(err)

for _, addr := range []string{zone.DepositAddress.Address, zone.WithdrawalAddress.Address} {
accAddr, err := sdk.AccAddressFromBech32(addr)
s.Require().NoError(err)
data := append(banktypes.CreateAccountBalancesPrefix(accAddr), []byte("qck")...)

err = keeper.AccountBalanceCallback(app.InterchainstakingKeeper, ctx, respbz, icqtypes.Query{ChainId: s.chainB.ChainID, Request: data})
s.Require().NoError(err)
}
})
}

func (s *KeeperTestSuite) TestAccountBalanceCallbackNil() {
s.Run("account balance", func() {
s.SetupTest()
Expand All @@ -949,8 +979,8 @@ func (s *KeeperTestSuite) TestAccountBalanceCallbackNil() {
zone.WithdrawalAddress.BalanceWaitgroup++
app.InterchainstakingKeeper.SetZone(ctx, &zone)

response := sdk.Coin{}
respbz, err := app.AppCodec().Marshal(&response)
var response *sdk.Coin = nil
respbz, err := app.AppCodec().Marshal(response)
s.Require().NoError(err)

for _, addr := range []string{zone.DepositAddress.Address, zone.WithdrawalAddress.Address} {
Expand Down
5 changes: 5 additions & 0 deletions x/participationrewards/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ func (k Keeper) AfterEpochEnd(ctx sdk.Context, epochIdentifier string, epochNumb
return
}

if allocation == nil {
// if allocation is unset, then return early to avoid panic
return
}

if err := k.allocateZoneRewards(ctx, tvs, *allocation); err != nil {
k.Logger(ctx).Error(err.Error())
}
Expand Down

0 comments on commit aa898c9

Please sign in to comment.