Skip to content

Commit

Permalink
Merge branch 'main' into test/DepositTxCallback
Browse files Browse the repository at this point in the history
  • Loading branch information
hoangdv2429 authored Oct 18, 2023
2 parents f6c7725 + 7521baa commit 283ff6b
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 26 deletions.
54 changes: 32 additions & 22 deletions x/interchainstaking/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,29 +48,39 @@ func (k *Keeper) BeginBlocker(ctx sdk.Context) {
}

connection, found := k.IBCKeeper.ConnectionKeeper.GetConnection(ctx, zone.ConnectionId)
if found {
consState, found := k.IBCKeeper.ClientKeeper.GetLatestClientConsensusState(ctx, connection.GetClientID())
if found {
tmConsState, ok := consState.(*tmtypes.ConsensusState)
if ok {
if len(zone.IbcNextValidatorsHash) == 0 || !bytes.Equal(zone.IbcNextValidatorsHash, tmConsState.NextValidatorsHash.Bytes()) {
k.Logger(ctx).Info("IBC ValSet has changed; requerying valset")
// trigger valset update.
period := int64(k.GetParam(ctx, types.KeyValidatorSetInterval))
query := stakingTypes.QueryValidatorsRequest{}
err := k.EmitValSetQuery(ctx, zone.ConnectionId, zone.ChainId, query, sdkmath.NewInt(period))
if err != nil {
k.Logger(ctx).Error("unable to trigger valset update query", "error", err.Error())
// failing to emit the valset update is not terminal but constitutes
// an error, as if this starts happening frequent it is something
// we should investigate.
}
zone.IbcNextValidatorsHash = tmConsState.NextValidatorsHash.Bytes()
k.SetZone(ctx, zone)
}
}
}
if !found {
return false
}

consState, found := k.IBCKeeper.ClientKeeper.GetLatestClientConsensusState(ctx, connection.GetClientID())
if !found {
return false
}

tmConsState, ok := consState.(*tmtypes.ConsensusState)
if !ok {
return false
}

changedValSet := len(zone.IbcNextValidatorsHash) == 0 || !bytes.Equal(zone.IbcNextValidatorsHash, tmConsState.NextValidatorsHash.Bytes())
if !changedValSet {
return false
}

k.Logger(ctx).Info("IBC ValSet has changed; requerying valset")
// trigger valset update.
period := int64(k.GetParam(ctx, types.KeyValidatorSetInterval))
query := stakingTypes.QueryValidatorsRequest{}
err := k.EmitValSetQuery(ctx, zone.ConnectionId, zone.ChainId, query, sdkmath.NewInt(period))
if err != nil {
k.Logger(ctx).Error("unable to trigger valset update query", "error", err.Error())
// failing to emit the valset update is not terminal but constitutes
// an error, as if this starts happening frequent it is something
// we should investigate.
}

zone.IbcNextValidatorsHash = tmConsState.NextValidatorsHash.Bytes()
k.SetZone(ctx, zone)
return false
})
}
3 changes: 2 additions & 1 deletion x/interchainstaking/keeper/address_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ func (k Keeper) IterateUserMappedAccounts(ctx sdk.Context, localAddress []byte,
defer iterator.Close()

i := int64(0)
remoteAddrPrefixLen := len(types.GetRemoteAddressPrefix(localAddress))
for ; iterator.Valid(); iterator.Next() {
value := iterator.Value()
key := iterator.Key()
chainIDBytes := key[len(types.GetRemoteAddressPrefix(localAddress)):]
chainIDBytes := key[remoteAddrPrefixLen:]
stop := fn(i, string(chainIDBytes), value)
if stop {
break
Expand Down
47 changes: 47 additions & 0 deletions x/interchainstaking/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1302,3 +1302,50 @@ func (suite *KeeperTestSuite) TestKeeper_MappedAccounts() {
})
}
}

func (suite *KeeperTestSuite) TestKeeper_Zone() {
testCases := []struct {
name string
malleate func()
req *types.QueryZoneRequest
wantErr bool
}{
{
name: "empty request",
malleate: func() {},
req: nil,
wantErr: true,
},
{
name: "zone not found",
malleate: func() {},
req: &types.QueryZoneRequest{ChainId: suite.chainB.ChainID},
wantErr: true,
},
{
name: "zone valid request",
malleate: func() {
suite.SetupTest()
suite.setupTestZones()
},
req: &types.QueryZoneRequest{ChainId: suite.chainB.ChainID},
wantErr: false,
},
}
for _, tc := range testCases {
suite.Run(tc.name, func() {
tc.malleate()
icsKeeper := suite.GetQuicksilverApp(suite.chainA).InterchainstakingKeeper
ctx := suite.chainA.GetContext()

resp, err := icsKeeper.Zone(ctx, tc.req)
if tc.wantErr {
suite.T().Logf("Error:\n%v\n", err)
suite.Error(err)
} else {
suite.NoError(err)
suite.NotNil(resp)
}
})
}
}
2 changes: 0 additions & 2 deletions x/interchainstaking/keeper/ibc_packet_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,12 +467,10 @@ func (k *Keeper) HandleWithdrawForUser(ctx sdk.Context, zone *types.Zone, msg *b

if len(dlist) > 0 {
newDist := make([]*types.Distribution, 0)
i := 0
for idx := range withdrawalRecord.Distribution {
if _, remove := dlist[idx]; !remove {
newDist = append(newDist, withdrawalRecord.Distribution[idx])
}
i++
}
k.Logger(ctx).Info("found matching withdrawal; awaiting additional messages")
withdrawalRecord.Distribution = newDist
Expand Down
11 changes: 10 additions & 1 deletion x/interchainstaking/keeper/zones.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,16 @@ func (k *Keeper) SetAccountBalanceForDenom(ctx sdk.Context, zone *types.Zone, ad
}
k.Logger(ctx).Info("Matched withdrawal address", "address", address, "wg", zone.WithdrawalAddress.BalanceWaitgroup, "balance", zone.WithdrawalAddress.Balance)
case zone.PerformanceAddress != nil && address == zone.PerformanceAddress.Address:
k.Logger(ctx).Info("Matched performance address")
existing := zone.PerformanceAddress.Balance.AmountOf(coin.Denom)
err = zone.PerformanceAddress.SetBalance(zone.PerformanceAddress.Balance.Sub(sdk.NewCoins(sdk.NewCoin(coin.Denom, existing))...).Add(coin)) // reset this denom
if err != nil {
return err
}
err = zone.PerformanceAddress.DecrementBalanceWaitgroup()
if err != nil {
return err
}
k.Logger(ctx).Info("Matched performance address", "address", address, "wg", zone.PerformanceAddress.BalanceWaitgroup, "balance", zone.PerformanceAddress.Balance)
default:
panic("unexpected")
}
Expand Down

0 comments on commit 283ff6b

Please sign in to comment.