Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [Interchain Security] Remove dependency on transient store #12845

Merged
merged 2 commits into from
Aug 9, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/core/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,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 @@ -6619,6 +6620,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 @@ -358,4 +359,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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this dead code?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I couldn't find anything within SDK or IS calling it.

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 @@ -134,10 +133,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 @@ -184,7 +179,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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What was this doing and why is it no longer needed?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was setting the validator updates one at a time. I replace it with a single call that sets an array of validator updates. The original code was already collecting this array, i.e.,

updates = append(updates, update

k.SetLastValidatorPower(ctx, valAddr, newPower)
}

Expand All @@ -207,18 +201,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 @@ -241,6 +227,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