Skip to content

Commit

Permalink
store ValidatorUpdates in normal store (#12845)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpoke authored and sainoe committed Nov 2, 2022
1 parent fff2293 commit f40c644
Show file tree
Hide file tree
Showing 5 changed files with 1,127 additions and 804 deletions.
16 changes: 16 additions & 0 deletions docs/core/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@
- [UnbondingDelegationEntry](#cosmos.staking.v1beta1.UnbondingDelegationEntry)
- [ValAddresses](#cosmos.staking.v1beta1.ValAddresses)
- [Validator](#cosmos.staking.v1beta1.Validator)
- [ValidatorUpdates](#cosmos.staking.v1beta1.ValidatorUpdates)

- [BondStatus](#cosmos.staking.v1beta1.BondStatus)
- [InfractionType](#cosmos.staking.v1beta1.InfractionType)
Expand Down Expand Up @@ -6788,6 +6789,21 @@ multiplied by exchange rate.




<a name="cosmos.staking.v1beta1.ValidatorUpdates"></a>

### ValidatorUpdates
ValidatorUpdates defines an array of abci.ValidatorUpdate objects.


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `updates` | [tendermint.abci.ValidatorUpdate](#tendermint.abci.ValidatorUpdate) | repeated | |





<!-- end messages -->


Expand Down
6 changes: 6 additions & 0 deletions proto/cosmos/staking/v1beta1/staking.proto
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import "google/protobuf/timestamp.proto";
import "cosmos_proto/cosmos.proto";
import "cosmos/base/v1beta1/coin.proto";
import "tendermint/types/types.proto";
import "tendermint/abci/types.proto";

option go_package = "github.com/cosmos/cosmos-sdk/x/staking/types";

Expand Down Expand Up @@ -361,4 +362,9 @@ enum InfractionType {
INFRACTION_TYPE_DOUBLE_SIGN = 1 [(gogoproto.enumvalue_customname) = "DoubleSign"];
// DOWNTIME defines a validator that missed signing too many blocks.
INFRACTION_TYPE_DOWNTIME = 2 [(gogoproto.enumvalue_customname) = "Downtime"];
}

// ValidatorUpdates defines an array of abci.ValidatorUpdate objects.
message ValidatorUpdates {
repeated tendermint.abci.ValidatorUpdate updates = 1 [(gogoproto.nullable) = false];
}
53 changes: 11 additions & 42 deletions x/staking/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/tendermint/tendermint/libs/log"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/cosmos/cosmos-sdk/x/staking/types"
Expand Down Expand Up @@ -98,48 +97,18 @@ func (k Keeper) SetLastTotalPower(ctx sdk.Context, power sdk.Int) {
store.Set(types.LastTotalPowerKey, bz)
}

// GetValidatorUpdate returns the ABCI validator power update for the current block
// by the consensus address.
func (k Keeper) GetValidatorUpdate(ctx sdk.Context, consAddr sdk.ConsAddress) (abci.ValidatorUpdate, bool) {
store := prefix.NewStore(ctx.TransientStore(k.transientKey), types.ValidatorUpdatesKey)
bz := store.Get(consAddr.Bytes())
if len(bz) == 0 {
return abci.ValidatorUpdate{}, false
}

var valUpdate abci.ValidatorUpdate
k.cdc.MustUnmarshal(bz, &valUpdate)
return valUpdate, true
}

// HasValidatorUpdate returns true if there is a power update for the given validator
// within the last block.
func (k Keeper) HasValidatorUpdate(ctx sdk.Context, consAddr sdk.ConsAddress) bool {
store := prefix.NewStore(ctx.TransientStore(k.transientKey), types.ValidatorUpdatesKey)
return store.Has(consAddr.Bytes())
}

// SetValidatorUpdate sets the ABCI validator power update for the current block
// by the consensus address.
func (k Keeper) SetValidatorUpdate(ctx sdk.Context, consAddr sdk.ConsAddress, valUpdate abci.ValidatorUpdate) {
store := prefix.NewStore(ctx.TransientStore(k.transientKey), types.ValidatorUpdatesKey)
bz := k.cdc.MustMarshal(&valUpdate)
store.Set(consAddr.Bytes(), bz)
// SetValidatorUpdates sets the ABCI validator power updates for the current block.
func (k Keeper) SetValidatorUpdates(ctx sdk.Context, valUpdates []abci.ValidatorUpdate) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&types.ValidatorUpdates{Updates: valUpdates})
store.Set(types.ValidatorUpdatesKey, bz)
}

// GetValidatorUpdates returns all the ABCI validator power updates within the current block.
// GetValidatorUpdates returns the ABCI validator power updates within the current block.
func (k Keeper) GetValidatorUpdates(ctx sdk.Context) []abci.ValidatorUpdate {
store := ctx.TransientStore(k.transientKey)
iterator := sdk.KVStorePrefixIterator(store, types.ValidatorUpdatesKey)

var valsetUpdates []abci.ValidatorUpdate
defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
var valUpdate abci.ValidatorUpdate
k.cdc.MustUnmarshal(iterator.Value(), &valUpdate)
valsetUpdates = append(valsetUpdates, valUpdate)
}

return valsetUpdates
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.ValidatorUpdatesKey)
var valUpdates types.ValidatorUpdates
k.cdc.MustUnmarshal(bz, &valUpdates)
return valUpdates.Updates
}
17 changes: 3 additions & 14 deletions x/staking/keeper/val_state_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
abci "github.com/tendermint/tendermint/abci/types"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/staking/types"
)

Expand Down Expand Up @@ -130,10 +129,6 @@ func (k Keeper) ApplyAndReturnValidatorSetUpdates(ctx sdk.Context) (updates []ab
// part of the bonded validator set
valAddr := sdk.ValAddress(iterator.Value())
validator := k.mustGetValidator(ctx, valAddr)
consAddr, err := validator.GetConsAddr()
if err != nil {
return nil, sdkerrors.Wrapf(err, "invalid consensus address for validator %s", valAddr)
}

if validator.Jailed {
panic("should never retrieve a jailed validator from the power store")
Expand Down Expand Up @@ -180,7 +175,6 @@ func (k Keeper) ApplyAndReturnValidatorSetUpdates(ctx sdk.Context) (updates []ab
update := validator.ABCIValidatorUpdate(powerReduction)
updates = append(updates, update)
// set the validator update and power
k.SetValidatorUpdate(ctx, consAddr, update)
k.SetLastValidatorPower(ctx, valAddr, newPower)
}

Expand All @@ -203,18 +197,10 @@ func (k Keeper) ApplyAndReturnValidatorSetUpdates(ctx sdk.Context) (updates []ab
return nil, err
}

consAddr, err := validator.GetConsAddr()
if err != nil {
return nil, sdkerrors.Wrapf(err, "invalid consensus address for validator %s", valAddr)
}

amtFromBondedToNotBonded = amtFromBondedToNotBonded.Add(validator.GetTokens())
k.DeleteLastValidatorPower(ctx, validator.GetOperator())
update := validator.ABCIValidatorUpdateZero()
updates = append(updates, update)

// set the validator update to transient store
k.SetValidatorUpdate(ctx, consAddr, update)
}

// Update the pools based on the recent updates in the validator set:
Expand All @@ -237,6 +223,9 @@ func (k Keeper) ApplyAndReturnValidatorSetUpdates(ctx sdk.Context) (updates []ab
k.SetLastTotalPower(ctx, totalPower)
}

// set the list of validator updates
k.SetValidatorUpdates(ctx, updates)

return updates, nil
}

Expand Down
Loading

0 comments on commit f40c644

Please sign in to comment.