Skip to content

Commit fd88ced

Browse files
authored
1 parent f800cc7 commit fd88ced

File tree

2 files changed

+14
-25
lines changed

2 files changed

+14
-25
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
5555
* [#507](https://github.com/crypto-org-chain/cosmos-sdk/pull/507) mempool respect gas wanted returned by ante handler
5656
* [#744](https://github.com/crypto-org-chain/cosmos-sdk/pull/744) Pass raw transactions to tx executor so it can do pre-estimations.
5757
* [#884](https://github.com/crypto-org-chain/cosmos-sdk/pull/884) Avoid decoding tx for in PrepareProposal if it's NopMempool.
58+
* (x/staking) [#24354](https://github.com/cosmos/cosmos-sdk/pull/24354) Optimize validator endblock by reducing bech32 conversions, resulting in significant performance improvement
5859

5960
## [Unreleased]
6061

x/staking/keeper/val_state_change.go

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import (
66
"fmt"
77
"sort"
88

9-
abci "github.com/cometbft/cometbft/abci/types"
109
gogotypes "github.com/cosmos/gogoproto/types"
1110

1211
"cosmossdk.io/core/address"
1312
"cosmossdk.io/math"
13+
abci "github.com/cometbft/cometbft/abci/types"
1414

1515
sdk "github.com/cosmos/cosmos-sdk/types"
1616
"github.com/cosmos/cosmos-sdk/x/staking/types"
@@ -188,17 +188,13 @@ func (k Keeper) ApplyAndReturnValidatorSetUpdates(ctx context.Context) (updates
188188
panic("unexpected validator status")
189189
}
190190

191+
valAddrStr := string(valAddr)
191192
// fetch the old power bytes
192-
valAddrStr, err := k.validatorAddressCodec.BytesToString(valAddr)
193-
if err != nil {
194-
return nil, err
195-
}
196-
oldPowerBytes, found := last[valAddrStr]
193+
oldPower, found := last[valAddrStr]
197194
newPower := validator.ConsensusPower(powerReduction)
198-
newPowerBytes := k.cdc.MustMarshal(&gogotypes.Int64Value{Value: newPower})
199195

200196
// update the validator set if power has changed
201-
if !found || !bytes.Equal(oldPowerBytes, newPowerBytes) {
197+
if !found || oldPower != newPower {
202198
updates = append(updates, validator.ABCIValidatorUpdate(powerReduction))
203199

204200
if err = k.SetLastValidatorPower(ctx, valAddr, newPower); err != nil {
@@ -209,7 +205,7 @@ func (k Keeper) ApplyAndReturnValidatorSetUpdates(ctx context.Context) (updates
209205
delete(last, valAddrStr)
210206
count++
211207

212-
totalPower = totalPower.Add(math.NewInt(newPower))
208+
totalPower = totalPower.AddRaw(newPower)
213209
}
214210

215211
noLongerBonded, err := sortNoLongerBonded(last, k.validatorAddressCodec)
@@ -454,9 +450,9 @@ func (k Keeper) completeUnbondingValidator(ctx context.Context, validator types.
454450
return validator, nil
455451
}
456452

457-
// map of operator bech32-addresses to serialized power
458-
// We use bech32 strings here, because we can't have slices as keys: map[[]byte][]byte
459-
type validatorsByAddr map[string][]byte
453+
// map of operator addresses to power
454+
// We use (non bech32) strings here, because we can't have slices as keys: map[[]byte][]byte
455+
type validatorsByAddr map[string]int64
460456

461457
// get the last validator set
462458
func (k Keeper) getLastValidatorsByAddr(ctx context.Context) (validatorsByAddr, error) {
@@ -468,17 +464,12 @@ func (k Keeper) getLastValidatorsByAddr(ctx context.Context) (validatorsByAddr,
468464
}
469465
defer iterator.Close()
470466

467+
var intVal gogotypes.Int64Value
471468
for ; iterator.Valid(); iterator.Next() {
472469
// extract the validator address from the key (prefix is 1-byte, addrLen is 1-byte)
473-
valAddr := types.AddressFromLastValidatorPowerKey(iterator.Key())
474-
valAddrStr, err := k.validatorAddressCodec.BytesToString(valAddr)
475-
if err != nil {
476-
return nil, err
477-
}
478-
479-
powerBytes := iterator.Value()
480-
last[valAddrStr] = make([]byte, len(powerBytes))
481-
copy(last[valAddrStr], powerBytes)
470+
valAddrStr := string(types.AddressFromLastValidatorPowerKey(iterator.Key()))
471+
k.cdc.MustUnmarshal(iterator.Value(), &intVal)
472+
last[valAddrStr] = intVal.GetValue()
482473
}
483474

484475
return last, nil
@@ -492,10 +483,7 @@ func sortNoLongerBonded(last validatorsByAddr, ac address.Codec) ([][]byte, erro
492483
index := 0
493484

494485
for valAddrStr := range last {
495-
valAddrBytes, err := ac.StringToBytes(valAddrStr)
496-
if err != nil {
497-
return nil, err
498-
}
486+
valAddrBytes := []byte(valAddrStr)
499487
noLongerBonded[index] = valAddrBytes
500488
index++
501489
}

0 commit comments

Comments
 (0)