Skip to content

Commit

Permalink
feat: add periodic delegation balance poling icq (#1741)
Browse files Browse the repository at this point in the history
* add periodic poling icq

* revert redundant callback
  • Loading branch information
ajansari95 authored Nov 13, 2024
1 parent eadd9a9 commit 89e6e2b
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
19 changes: 19 additions & 0 deletions x/interchainstaking/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import (

"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

tmtypes "github.com/cosmos/ibc-go/v6/modules/light-clients/07-tendermint/types"

"github.com/quicksilver-zone/quicksilver/utils/addressutils"
"github.com/quicksilver-zone/quicksilver/x/interchainstaking/types"
)

Expand Down Expand Up @@ -47,6 +49,23 @@ func (k *Keeper) BeginBlocker(ctx sdk.Context) {
if err := k.GCCompletedUnbondings(ctx, zone); err != nil {
k.Logger(ctx).Error("error in GCCompletedUnbondings", "error", err.Error())
}

addressBytes, err := addressutils.AccAddressFromBech32(zone.DelegationAddress.Address, zone.AccountPrefix)
if err != nil {
k.Logger(ctx).Error("cannot decode bech32 delegation addr", "error", err.Error())
}
zone.DelegationAddress.IncrementBalanceWaitgroup()
k.ICQKeeper.MakeRequest(
ctx,
zone.ConnectionId,
zone.ChainId,
types.BankStoreKey,
append(banktypes.CreateAccountBalancesPrefix(addressBytes), []byte(zone.BaseDenom)...),
sdk.NewInt(-1),
types.ModuleName,
"accountbalance",
0,
)

Check warning on line 68 in x/interchainstaking/keeper/abci.go

View check run for this annotation

Codecov / codecov/patch

x/interchainstaking/keeper/abci.go#L53-L68

Added lines #L53 - L68 were not covered by tests
}

connection, found := k.IBCKeeper.ConnectionKeeper.GetConnection(ctx, zone.ConnectionId)
Expand Down
31 changes: 30 additions & 1 deletion x/interchainstaking/keeper/callbacks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1840,7 +1840,7 @@ func TestDepositIntervalCallback(t *testing.T) {
err = fmt.Errorf("pagination total exceeds int range: %d", res.Pagination.Total)
}
suite.NoError(err)
suite.Equal(txQueryCount, int(res.Pagination.Total)-3) //nolint:gosec
suite.Equal(int(res.Pagination.Total), txQueryCount) //nolint:gosec
}

func TestDepositIntervalCallbackWithExistingTxs(t *testing.T) {
Expand Down Expand Up @@ -2843,6 +2843,35 @@ func (suite *KeeperTestSuite) TestPerfBalanceCallbackUpdate() {
})
}

func (suite *KeeperTestSuite) TestPollDelegationAccountBalanceCallback() {
suite.Run("poll delegation account balance", func() {
suite.SetupTest()
suite.setupTestZones()

quicksilver := suite.GetQuicksilverApp(suite.chainA)
quicksilver.InterchainstakingKeeper.CallbackHandler().RegisterCallbacks()
ctx := suite.chainA.GetContext()

zone, _ := quicksilver.InterchainstakingKeeper.GetZone(ctx, suite.chainB.ChainID)
zone.DelegationAddress.Balance = sdk.NewCoins(sdk.NewCoin("uatom", sdkmath.NewInt(500)))
quicksilver.InterchainstakingKeeper.SetZone(ctx, &zone)
zone.DelegationAddress.IncrementBalanceWaitgroup()

response := sdk.NewCoin("uatom", sdk.NewInt(101))
respbz, err := quicksilver.AppCodec().Marshal(&response)
suite.NoError(err)

address := zone.DelegationAddress.Address
accAddr, err := sdk.AccAddressFromBech32(address)
suite.NoError(err)
data := append(banktypes.CreateAccountBalancesPrefix(accAddr), "uatom"...)
quicksilver.InterchainstakingKeeper.SetZone(ctx, &zone)

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

func TestRejectNonADR027(t *testing.T) {
registry := codectypes.NewInterfaceRegistry()
cdc := codec.NewProtoCodec(registry)
Expand Down
11 changes: 11 additions & 0 deletions x/interchainstaking/keeper/zones.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,17 @@ func (k *Keeper) SetAccountBalanceForDenom(ctx sdk.Context, zone *types.Zone, ad
return err
}
k.Logger(ctx).Info("Matched performance address", "address", address, "wg", zone.PerformanceAddress.BalanceWaitgroup, "balance", zone.PerformanceAddress.Balance)
case zone.DelegationAddress != nil && address == zone.DelegationAddress.Address:
existing := zone.DelegationAddress.Balance.AmountOf(coin.Denom)
err = zone.DelegationAddress.SetBalance(zone.DelegationAddress.Balance.Sub(sdk.NewCoins(sdk.NewCoin(coin.Denom, existing))...).Add(coin)) // reset this denom
if err != nil {
return err
}

Check warning on line 311 in x/interchainstaking/keeper/zones.go

View check run for this annotation

Codecov / codecov/patch

x/interchainstaking/keeper/zones.go#L310-L311

Added lines #L310 - L311 were not covered by tests
err = zone.DelegationAddress.DecrementBalanceWaitgroup()
if err != nil {
return err
}

Check warning on line 315 in x/interchainstaking/keeper/zones.go

View check run for this annotation

Codecov / codecov/patch

x/interchainstaking/keeper/zones.go#L314-L315

Added lines #L314 - L315 were not covered by tests
k.Logger(ctx).Info("Matched delegation address", "address", address, "wg", zone.DelegationAddress.BalanceWaitgroup, "balance", zone.DelegationAddress.Balance)
default:
panic("unexpected")
}
Expand Down

0 comments on commit 89e6e2b

Please sign in to comment.