Skip to content

Commit b78c272

Browse files
committed
fix(x/slashing/keeper): hoist non-changing addresses parsing out of redelegation loop
This change hoists out, the parsing of non-changing validator address and delegator address which before were being re-parsed in every loop of the redelegation entries, parsing them once and reusing the already parsed addresses. Fixes #18032
1 parent 4001e2b commit b78c272

2 files changed

Lines changed: 12 additions & 10 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
5050

5151
### Improvements
5252

53+
* (x/staking/keeper) [#18035](https://github.com/cosmos/cosmos-sdk/pull/18035) Hoisted out of the redelegation loop, the non-changing validator and delegator addresses parsing.
5354
* (keyring) [#17913](https://github.com/cosmos/cosmos-sdk/pull/17913) Add `NewAutoCLIKeyring` for creating an AutoCLI keyring from a SDK keyring.
5455
* (codec) [#17913](https://github.com/cosmos/cosmos-sdk/pull/17913) `codectypes.NewAnyWithValue` supports proto v2 messages.
5556
* (client) [#17503](https://github.com/cosmos/cosmos-sdk/pull/17503) Add `client.Context{}.WithAddressCodec`, `WithValidatorAddressCodec`, `WithConsensusAddressCodec` to provide address codecs to the client context. See the [UPGRADING.md](./UPGRADING.md) for more details.

x/staking/keeper/slash.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,16 @@ func (k Keeper) SlashRedelegation(ctx context.Context, srcValidator types.Valida
287287
totalSlashAmount = math.ZeroInt()
288288
bondedBurnedAmount, notBondedBurnedAmount := math.ZeroInt(), math.ZeroInt()
289289

290+
valDstAddr, err := k.validatorAddressCodec.StringToBytes(redelegation.ValidatorDstAddress)
291+
if err != nil {
292+
return math.ZeroInt(), fmt.Errorf("SlashRedelegation: could not parse validator destination address: %w", err)
293+
}
294+
295+
delegatorAddress, err := k.authKeeper.AddressCodec().StringToBytes(redelegation.DelegatorAddress)
296+
if err != nil {
297+
return math.ZeroInt(), fmt.Errorf("SlashRedelegation: could not parse delegator address: %w", err)
298+
}
299+
290300
// perform slashing on all entries within the redelegation
291301
for _, entry := range redelegation.Entries {
292302
// If redelegation started before this height, stake didn't contribute to infraction
@@ -310,16 +320,7 @@ func (k Keeper) SlashRedelegation(ctx context.Context, srcValidator types.Valida
310320
continue
311321
}
312322

313-
valDstAddr, err := k.validatorAddressCodec.StringToBytes(redelegation.ValidatorDstAddress)
314-
if err != nil {
315-
panic(err)
316-
}
317-
318-
delegatorAddress, err := k.authKeeper.AddressCodec().StringToBytes(redelegation.DelegatorAddress)
319-
if err != nil {
320-
panic(err)
321-
}
322-
323+
// Delegations can be dynamic hence need to be looked up on every redelegation entry loop.
323324
delegation, err := k.Delegations.Get(ctx, collections.Join(sdk.AccAddress(delegatorAddress), sdk.ValAddress(valDstAddr)))
324325
if err != nil {
325326
// If deleted, delegation has zero shares, and we can't unbond any more

0 commit comments

Comments
 (0)