Skip to content

Commit

Permalink
refactor(x/staking,genutil)!: use validatorUpdates (#19754)
Browse files Browse the repository at this point in the history
Co-authored-by: chixiaoxiao <[email protected]>
  • Loading branch information
chixiaowen and chixiaoxiao authored Mar 19, 2024
1 parent a1bfe5d commit bfa734c
Show file tree
Hide file tree
Showing 14 changed files with 121 additions and 147 deletions.
15 changes: 7 additions & 8 deletions tests/integration/staking/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
)

func bootstrapGenesisTest(t *testing.T, numAddrs int) (*fixture, []sdk.AccAddress) {
Expand Down Expand Up @@ -128,12 +129,11 @@ func TestInitGenesis(t *testing.T) {
assert.Equal(t, types.Bonded, resVal.Status)

abcivals := make([]abci.ValidatorUpdate, len(vals))

validatorUpdates := make([]module.ValidatorUpdate, len(abcivals))
for i, val := range validators {
abcivals[i] = val.ABCIValidatorUpdate((f.stakingKeeper.PowerReduction(f.sdkCtx)))
validatorUpdates[i] = val.ModuleValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx))
}

assert.DeepEqual(t, abcivals, vals)
assert.DeepEqual(t, validatorUpdates, vals)
}

func TestInitGenesis_PoolsBalanceMismatch(t *testing.T) {
Expand Down Expand Up @@ -229,12 +229,11 @@ func TestInitGenesisLargeValidatorSet(t *testing.T) {
vals, err := f.stakingKeeper.InitGenesis(f.sdkCtx, genesisState)
assert.NilError(t, err)

abcivals := make([]abci.ValidatorUpdate, 100)
validatorUpdates := make([]module.ValidatorUpdate, 100)
for i, val := range validators[:100] {
abcivals[i] = val.ABCIValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx))
validatorUpdates[i] = val.ModuleValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx))
}

// remove genesis validator
vals = vals[:100]
assert.DeepEqual(t, abcivals, vals)
assert.DeepEqual(t, validatorUpdates, vals)
}
40 changes: 20 additions & 20 deletions tests/integration/staking/keeper/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"testing"

abci "github.com/cometbft/cometbft/abci/types"
"gotest.tools/v3/assert"

"cosmossdk.io/math"
Expand All @@ -16,6 +15,7 @@ import (
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
)

func newMonikerValidator(tb testing.TB, operator sdk.ValAddress, pubKey cryptotypes.PubKey, moniker string) types.Validator {
Expand Down Expand Up @@ -557,8 +557,8 @@ func TestApplyAndReturnValidatorSetUpdatesAllNone(t *testing.T) {
assert.NilError(t, err)
validators[0], _ = f.stakingKeeper.GetValidator(f.sdkCtx, val0bz)
validators[1], _ = f.stakingKeeper.GetValidator(f.sdkCtx, val1bz)
assert.DeepEqual(t, validators[0].ABCIValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[1])
assert.DeepEqual(t, validators[1].ABCIValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[0])
assert.DeepEqual(t, validators[0].ModuleValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[1])
assert.DeepEqual(t, validators[1].ModuleValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[0])
}

func TestApplyAndReturnValidatorSetUpdatesIdentical(t *testing.T) {
Expand Down Expand Up @@ -607,7 +607,7 @@ func TestApplyAndReturnValidatorSetUpdatesSingleValueChange(t *testing.T) {
validators[0] = keeper.TestingUpdateValidator(f.stakingKeeper, f.sdkCtx, validators[0], false)

updates := applyValidatorSetUpdates(t, f.sdkCtx, f.stakingKeeper, 1)
assert.DeepEqual(t, validators[0].ABCIValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[0])
assert.DeepEqual(t, validators[0].ModuleValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[0])
}

func TestApplyAndReturnValidatorSetUpdatesMultipleValueChange(t *testing.T) {
Expand All @@ -629,8 +629,8 @@ func TestApplyAndReturnValidatorSetUpdatesMultipleValueChange(t *testing.T) {
validators[1] = keeper.TestingUpdateValidator(f.stakingKeeper, f.sdkCtx, validators[1], false)

updates := applyValidatorSetUpdates(t, f.sdkCtx, f.stakingKeeper, 2)
assert.DeepEqual(t, validators[0].ABCIValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[0])
assert.DeepEqual(t, validators[1].ABCIValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[1])
assert.DeepEqual(t, validators[0].ModuleValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[0])
assert.DeepEqual(t, validators[1].ModuleValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[1])
}

func TestApplyAndReturnValidatorSetUpdatesInserted(t *testing.T) {
Expand All @@ -649,7 +649,7 @@ func TestApplyAndReturnValidatorSetUpdatesInserted(t *testing.T) {
val2bz, err := f.stakingKeeper.ValidatorAddressCodec().StringToBytes(validators[2].GetOperator())
assert.NilError(t, err)
validators[2], _ = f.stakingKeeper.GetValidator(f.sdkCtx, val2bz)
assert.DeepEqual(t, validators[2].ABCIValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[0])
assert.DeepEqual(t, validators[2].ModuleValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[0])

// test validtor added at the beginning
// tendermintUpdate set: {} -> {c0}
Expand All @@ -659,7 +659,7 @@ func TestApplyAndReturnValidatorSetUpdatesInserted(t *testing.T) {
val3bz, err := f.stakingKeeper.ValidatorAddressCodec().StringToBytes(validators[3].GetOperator())
assert.NilError(t, err)
validators[3], _ = f.stakingKeeper.GetValidator(f.sdkCtx, val3bz)
assert.DeepEqual(t, validators[3].ABCIValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[0])
assert.DeepEqual(t, validators[3].ModuleValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[0])

// test validtor added at the end
// tendermintUpdate set: {} -> {c0}
Expand All @@ -669,7 +669,7 @@ func TestApplyAndReturnValidatorSetUpdatesInserted(t *testing.T) {
val4bz, err := f.stakingKeeper.ValidatorAddressCodec().StringToBytes(validators[4].GetOperator())
assert.NilError(t, err)
validators[4], _ = f.stakingKeeper.GetValidator(f.sdkCtx, val4bz)
assert.DeepEqual(t, validators[4].ABCIValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[0])
assert.DeepEqual(t, validators[4].ModuleValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[0])
}

func TestApplyAndReturnValidatorSetUpdatesWithCliffValidator(t *testing.T) {
Expand Down Expand Up @@ -706,8 +706,8 @@ func TestApplyAndReturnValidatorSetUpdatesWithCliffValidator(t *testing.T) {
val2bz, err := f.stakingKeeper.ValidatorAddressCodec().StringToBytes(validators[2].GetOperator())
assert.NilError(t, err)
validators[2], _ = f.stakingKeeper.GetValidator(f.sdkCtx, val2bz)
assert.DeepEqual(t, validators[0].ABCIValidatorUpdateZero(), updates[1])
assert.DeepEqual(t, validators[2].ABCIValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[0])
assert.DeepEqual(t, validators[0].ModuleValidatorUpdateZero(), updates[1])
assert.DeepEqual(t, validators[2].ModuleValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[0])
}

func TestApplyAndReturnValidatorSetUpdatesNewValidator(t *testing.T) {
Expand Down Expand Up @@ -743,8 +743,8 @@ func TestApplyAndReturnValidatorSetUpdatesNewValidator(t *testing.T) {
assert.NilError(t, err)
validators[0], _ = f.stakingKeeper.GetValidator(f.sdkCtx, val0bz)
validators[1], _ = f.stakingKeeper.GetValidator(f.sdkCtx, val1bz)
assert.DeepEqual(t, validators[0].ABCIValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[0])
assert.DeepEqual(t, validators[1].ABCIValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[1])
assert.DeepEqual(t, validators[0].ModuleValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[0])
assert.DeepEqual(t, validators[1].ModuleValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[1])

applyValidatorSetUpdates(t, f.sdkCtx, f.stakingKeeper, 0)

Expand Down Expand Up @@ -791,9 +791,9 @@ func TestApplyAndReturnValidatorSetUpdatesNewValidator(t *testing.T) {
validator, _ = f.stakingKeeper.GetValidator(f.sdkCtx, valbz)
validators[0], _ = f.stakingKeeper.GetValidator(f.sdkCtx, val0bz)
validators[1], _ = f.stakingKeeper.GetValidator(f.sdkCtx, val1bz)
assert.DeepEqual(t, validator.ABCIValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[0])
assert.DeepEqual(t, validators[0].ABCIValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[1])
assert.DeepEqual(t, validators[1].ABCIValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[2])
assert.DeepEqual(t, validator.ModuleValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[0])
assert.DeepEqual(t, validators[0].ModuleValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[1])
assert.DeepEqual(t, validators[1].ModuleValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[2])
}

func TestApplyAndReturnValidatorSetUpdatesBondTransition(t *testing.T) {
Expand Down Expand Up @@ -828,8 +828,8 @@ func TestApplyAndReturnValidatorSetUpdatesBondTransition(t *testing.T) {
assert.NilError(t, err)
validators[2], _ = f.stakingKeeper.GetValidator(f.sdkCtx, val2bz)
validators[1], _ = f.stakingKeeper.GetValidator(f.sdkCtx, val1bz)
assert.DeepEqual(t, validators[2].ABCIValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[0])
assert.DeepEqual(t, validators[1].ABCIValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[1])
assert.DeepEqual(t, validators[2].ModuleValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[0])
assert.DeepEqual(t, validators[1].ModuleValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[1])

applyValidatorSetUpdates(t, f.sdkCtx, f.stakingKeeper, 0)

Expand Down Expand Up @@ -871,12 +871,12 @@ func TestApplyAndReturnValidatorSetUpdatesBondTransition(t *testing.T) {

// verify initial CometBFT updates are correct
updates = applyValidatorSetUpdates(t, f.sdkCtx, f.stakingKeeper, 1)
assert.DeepEqual(t, validators[1].ABCIValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[0])
assert.DeepEqual(t, validators[1].ModuleValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[0])

applyValidatorSetUpdates(t, f.sdkCtx, f.stakingKeeper, 0)
}

func applyValidatorSetUpdates(t *testing.T, ctx sdk.Context, k *keeper.Keeper, expectedUpdatesLen int) []abci.ValidatorUpdate {
func applyValidatorSetUpdates(t *testing.T, ctx sdk.Context, k *keeper.Keeper, expectedUpdatesLen int) []module.ValidatorUpdate {
t.Helper()
updates, err := k.ApplyAndReturnValidatorSetUpdates(ctx)
assert.NilError(t, err)
Expand Down
11 changes: 7 additions & 4 deletions x/genutil/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ package genutil
import (
"context"

abci "github.com/cometbft/cometbft/abci/types"

"cosmossdk.io/core/genesis"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/genutil/types"
)

Expand All @@ -16,9 +15,13 @@ func InitGenesis(
ctx context.Context, stakingKeeper types.StakingKeeper,
deliverTx genesis.TxHandler, genesisState types.GenesisState,
txEncodingConfig client.TxEncodingConfig,
) (validators []abci.ValidatorUpdate, err error) {
) (validatorUpdates []module.ValidatorUpdate, err error) {
if len(genesisState.GenTxs) > 0 {
validators, err = DeliverGenTxs(ctx, genesisState.GenTxs, stakingKeeper, deliverTx, txEncodingConfig)
moduleValidatorUpdates, err := DeliverGenTxs(ctx, genesisState.GenTxs, stakingKeeper, deliverTx, txEncodingConfig)
if err != nil {
return nil, err
}
return moduleValidatorUpdates, nil
}
return
}
5 changes: 2 additions & 3 deletions x/genutil/gentx.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ import (
"fmt"
"strings"

abci "github.com/cometbft/cometbft/abci/types"

"cosmossdk.io/core/genesis"
bankexported "cosmossdk.io/x/bank/exported"
stakingtypes "cosmossdk.io/x/staking/types"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/genutil/types"
)

Expand Down Expand Up @@ -94,7 +93,7 @@ func DeliverGenTxs(
ctx context.Context, genTxs []json.RawMessage,
stakingKeeper types.StakingKeeper, deliverTx genesis.TxHandler,
txEncodingConfig client.TxEncodingConfig,
) ([]abci.ValidatorUpdate, error) {
) ([]module.ValidatorUpdate, error) {
for _, genTx := range genTxs {
tx, err := txEncodingConfig.TxJSONDecoder()(genTx)
if err != nil {
Expand Down
26 changes: 1 addition & 25 deletions x/genutil/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,31 +78,7 @@ func (am AppModule) ValidateGenesis(bz json.RawMessage) error {
func (am AppModule) InitGenesis(ctx context.Context, data json.RawMessage) ([]module.ValidatorUpdate, error) {
var genesisState types.GenesisState
am.cdc.MustUnmarshalJSON(data, &genesisState)
cometValidatorUpdates, err := InitGenesis(ctx, am.stakingKeeper, am.deliverTx, genesisState, am.txEncodingConfig)
if err != nil {
return nil, err
}

validatorUpdates := make([]module.ValidatorUpdate, len(cometValidatorUpdates))
for i, v := range cometValidatorUpdates {
if ed25519 := v.PubKey.GetEd25519(); len(ed25519) > 0 {
validatorUpdates[i] = module.ValidatorUpdate{
PubKey: ed25519,
PubKeyType: "ed25519",
Power: v.Power,
}
} else if secp256k1 := v.PubKey.GetSecp256K1(); len(secp256k1) > 0 {
validatorUpdates[i] = module.ValidatorUpdate{
PubKey: secp256k1,
PubKeyType: "secp256k1",
Power: v.Power,
}
} else {
return nil, fmt.Errorf("unexpected validator pubkey type: %T", v.PubKey)
}
}

return validatorUpdates, nil
return InitGenesis(ctx, am.stakingKeeper, am.deliverTx, genesisState, am.txEncodingConfig)
}

// ExportGenesis returns the exported genesis state as raw bytes for the genutil module.
Expand Down
16 changes: 8 additions & 8 deletions x/genutil/testutil/expected_keepers_mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions x/genutil/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ import (
"context"
"encoding/json"

abci "github.com/cometbft/cometbft/abci/types"

bankexported "cosmossdk.io/x/bank/exported"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
)

// StakingKeeper defines the expected staking keeper (noalias)
type StakingKeeper interface {
ApplyAndReturnValidatorSetUpdates(context.Context) (updates []abci.ValidatorUpdate, err error)
ApplyAndReturnValidatorSetUpdates(context.Context) (updates []module.ValidatorUpdate, err error)
}

// AccountKeeper defines the expected account keeper (noalias)
Expand Down
1 change: 1 addition & 0 deletions x/staking/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* [#17335](https://github.com/cosmos/cosmos-sdk/pull/17335) Remove usage of `"cosmossdk.io/x/staking/types".Infraction_*` in favour of `"cosmossdk.io/api/cosmos/staking/v1beta1".Infraction_` in order to remove dependency between modules on staking
* [#17655](https://github.com/cosmos/cosmos-sdk/pull/17655) `QueryHistoricalInfo` was adjusted to return `HistoricalRecord` and marked `Hist` as deprecated.
* [#19414](https://github.com/cosmos/cosmos-sdk/pull/19414) Staking module takes an environment variable in `NewStakingKeeper` instead of individual services.
* [#19754](https://github.com/cosmos/cosmos-sdk/pull/19754) update to use `[]module.ValidatorUpdate` as return for `ApplyAndReturnValidatorSetUpdates`.

### State Breaking changes

Expand Down
5 changes: 2 additions & 3 deletions x/staking/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import (
"context"
"time"

abci "github.com/cometbft/cometbft/abci/types"

"cosmossdk.io/x/staking/types"

"github.com/cosmos/cosmos-sdk/telemetry"
"github.com/cosmos/cosmos-sdk/types/module"
)

// BeginBlocker will persist the current header and validator set as a historical entry
Expand All @@ -19,7 +18,7 @@ func (k *Keeper) BeginBlocker(ctx context.Context) error {
}

// EndBlocker called at every block, update validator set
func (k *Keeper) EndBlocker(ctx context.Context) ([]abci.ValidatorUpdate, error) {
func (k *Keeper) EndBlocker(ctx context.Context) ([]module.ValidatorUpdate, error) {
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyEndBlocker)
return k.BlockValidatorUpdates(ctx)
}
Loading

0 comments on commit bfa734c

Please sign in to comment.