From 5adcf20dac7a0fd4157c3de49f50eae717a8809f Mon Sep 17 00:00:00 2001 From: Tien Dung Date: Tue, 17 Oct 2023 17:16:20 +0700 Subject: [PATCH 1/3] Hoist code out of the loop (#707) --- x/interchainstaking/keeper/address_map.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/x/interchainstaking/keeper/address_map.go b/x/interchainstaking/keeper/address_map.go index a264445ad..f81b2081d 100644 --- a/x/interchainstaking/keeper/address_map.go +++ b/x/interchainstaking/keeper/address_map.go @@ -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 From 6595291d702aa11be4250a45f4544430da9d1b8b Mon Sep 17 00:00:00 2001 From: Tien Dung Date: Tue, 17 Oct 2023 17:55:21 +0700 Subject: [PATCH 2/3] Remove unused variable (#706) Co-authored-by: Joe Bowman --- x/interchainstaking/keeper/ibc_packet_handlers.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/x/interchainstaking/keeper/ibc_packet_handlers.go b/x/interchainstaking/keeper/ibc_packet_handlers.go index 0472782b4..a1220fde9 100644 --- a/x/interchainstaking/keeper/ibc_packet_handlers.go +++ b/x/interchainstaking/keeper/ibc_packet_handlers.go @@ -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 From 7521baa6038b325e3e40287170c3d6aeccd6c579 Mon Sep 17 00:00:00 2001 From: Tien Dung Date: Tue, 17 Oct 2023 18:01:15 +0700 Subject: [PATCH 3/3] Early return function BeginBlocker (#705) Co-authored-by: Nguyen Thanh Nhan Co-authored-by: Joe Bowman --- x/interchainstaking/keeper/abci.go | 54 ++++++++++++++++++------------ 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/x/interchainstaking/keeper/abci.go b/x/interchainstaking/keeper/abci.go index 4539bb625..49b79d0a2 100644 --- a/x/interchainstaking/keeper/abci.go +++ b/x/interchainstaking/keeper/abci.go @@ -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 }) }