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

refactor(x/staking,genutil)!: use validatorUpdates #19754

Merged
merged 7 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
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
36 changes: 31 additions & 5 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,24 @@ 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)))
if ed25519 := abcivals[i].PubKey.GetEd25519(); len(ed25519) > 0 {
validatorUpdates[i] = module.ValidatorUpdate{
PubKey: ed25519,
PubKeyType: "ed25519",
Power: abcivals[i].Power,
}
} else if secp256k1 := abcivals[i].PubKey.GetSecp256K1(); len(secp256k1) > 0 {
validatorUpdates[i] = module.ValidatorUpdate{
PubKey: secp256k1,
PubKeyType: "secp256k1",
Power: abcivals[i].Power,
}
}
}

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

func TestInitGenesis_PoolsBalanceMismatch(t *testing.T) {
Expand Down Expand Up @@ -230,11 +243,24 @@ func TestInitGenesisLargeValidatorSet(t *testing.T) {
assert.NilError(t, err)

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

Choose a reason for hiding this comment

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

Do we have to go via the abci struct in PowerReduction?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done! use ModuleValidatorUpdate

if ed25519 := abcivals[i].PubKey.GetEd25519(); len(ed25519) > 0 {
validatorUpdates[i] = module.ValidatorUpdate{
PubKey: ed25519,
PubKeyType: "ed25519",
Power: abcivals[i].Power,
}
} else if secp256k1 := abcivals[i].PubKey.GetSecp256K1(); len(secp256k1) > 0 {
validatorUpdates[i] = module.ValidatorUpdate{
PubKey: secp256k1,
PubKeyType: "secp256k1",
Power: abcivals[i].Power,
}
}
}

// 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)
Copy link
Member

Choose a reason for hiding this comment

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

Can we get a changelog under x/staking for that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done!

}

// AccountKeeper defines the expected account keeper (noalias)
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"
likhita-809 marked this conversation as resolved.
Show resolved Hide resolved
"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
Loading