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(genutil): Use sdk types genesis validator #21678

Merged
merged 54 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
fddadd3
move use sdk types genesis validator
sontrinh16 Sep 11, 2024
41c5520
debug
sontrinh16 Sep 13, 2024
219213b
debug
sontrinh16 Sep 13, 2024
794ecbc
debug
sontrinh16 Sep 13, 2024
252f50f
Merge branch 'main' into son/use_sdk_genesis_validator
sontrinh16 Sep 16, 2024
0033996
fix conflict
sontrinh16 Sep 16, 2024
ed34222
debug
sontrinh16 Sep 17, 2024
17a42a3
assert nil for empty val set
sontrinh16 Sep 17, 2024
6bb526b
minor
sontrinh16 Sep 17, 2024
f9aa48b
fix test
sontrinh16 Sep 17, 2024
389b7b8
lint
sontrinh16 Sep 17, 2024
5775c9f
fix e2e test
sontrinh16 Sep 17, 2024
85a753e
Merge branch 'main' into son/use_sdk_genesis_validator
sontrinh16 Sep 17, 2024
5fe570c
add migration and genesis validator in genutil v2
sontrinh16 Sep 18, 2024
5b31f47
Merge branch 'son/use_sdk_genesis_validator' of https://github.com/co…
sontrinh16 Sep 18, 2024
dd2a634
cleanup
sontrinh16 Sep 18, 2024
fba3f54
lint
sontrinh16 Sep 18, 2024
05fe687
more lint
sontrinh16 Sep 18, 2024
e4f1972
resolve conflict
sontrinh16 Sep 19, 2024
80a92ae
minor
sontrinh16 Sep 19, 2024
fb41419
Merge branch 'main' into son/use_sdk_genesis_validator
sontrinh16 Sep 23, 2024
d0f9f1c
make work with migrate cmd
sontrinh16 Sep 23, 2024
7a7c42a
Merge branch 'son/use_sdk_genesis_validator' of https://github.com/co…
sontrinh16 Sep 23, 2024
ddf3149
remove migrator
sontrinh16 Sep 23, 2024
7c0ce57
rename
sontrinh16 Sep 23, 2024
641164f
remove duplicated code
sontrinh16 Sep 23, 2024
bbb3b01
Update x/genutil/migration/v052/migrate_test.go
sontrinh16 Sep 23, 2024
5d02a84
Update x/genutil/migration/v052/migrate.go
sontrinh16 Sep 23, 2024
e9e27f6
address comment
sontrinh16 Sep 23, 2024
cebc09d
Merge branch 'son/use_sdk_genesis_validator' of https://github.com/co…
sontrinh16 Sep 23, 2024
a0d41c9
populate simapp v2 export
sontrinh16 Sep 23, 2024
7075a3d
lint
sontrinh16 Sep 23, 2024
d1f59d2
minor
sontrinh16 Sep 24, 2024
528606f
Merge branch 'main' into son/use_sdk_genesis_validator
sontrinh16 Sep 24, 2024
b5abb09
address comment
sontrinh16 Sep 24, 2024
29156ed
remove encoding
sontrinh16 Sep 26, 2024
b1924fd
Merge branch 'son/use_sdk_genesis_validator' of https://github.com/co…
sontrinh16 Sep 26, 2024
bb2d7f1
fix test
sontrinh16 Sep 30, 2024
8937cb7
fix conflict
sontrinh16 Sep 30, 2024
b4014f3
fix test
sontrinh16 Sep 30, 2024
bae84ba
minor
sontrinh16 Sep 30, 2024
e1a6b0c
Merge branch 'main' into son/use_sdk_genesis_validator
sontrinh16 Sep 30, 2024
76ab968
address comments
sontrinh16 Sep 30, 2024
33e0432
Merge branch 'son/use_sdk_genesis_validator' of https://github.com/co…
sontrinh16 Sep 30, 2024
6a81119
Merge branch 'main' into son/use_sdk_genesis_validator
sontrinh16 Sep 30, 2024
496627d
Merge branch 'main' into son/use_sdk_genesis_validator
kocubinski Oct 1, 2024
f0c5509
place holder
sontrinh16 Oct 2, 2024
e7600cd
Merge branch 'main' into son/use_sdk_genesis_validator
sontrinh16 Oct 3, 2024
2d40cef
Merge branch 'main' into son/use_sdk_genesis_validator
kocubinski Oct 8, 2024
7879361
refactor(genutil): export genesis with val set implementation (#22178)
kocubinski Oct 8, 2024
ebbf3e2
Merge branch 'main' into son/use_sdk_genesis_validator
sontrinh16 Oct 8, 2024
9ddc128
resolve conflict
sontrinh16 Oct 8, 2024
687866d
lint
sontrinh16 Oct 8, 2024
9b397f0
minor
sontrinh16 Oct 8, 2024
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
4 changes: 2 additions & 2 deletions server/types/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"io"

cmtproto "github.com/cometbft/cometbft/api/cometbft/types/v1"
cmttypes "github.com/cometbft/cometbft/types"
"github.com/cosmos/gogoproto/grpc"

corestore "cosmossdk.io/core/store"
Expand All @@ -16,6 +15,7 @@ import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/server/api"
"github.com/cosmos/cosmos-sdk/server/config"
sdk "github.com/cosmos/cosmos-sdk/types"
)

type (
Expand Down Expand Up @@ -73,7 +73,7 @@ type (
// AppState is the application state as JSON.
AppState json.RawMessage
// Validators is the exported validator set.
Validators []cmttypes.GenesisValidator
Validators []sdk.GenesisValidator
Copy link
Contributor

Choose a reason for hiding this comment

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

Tip

Codebase Verification

Update remaining cmttypes.GenesisValidator usages for consistency

The verification process has identified several instances where cmttypes.GenesisValidator is still being used in the codebase. These occurrences need to be addressed to maintain consistency with the change to sdk.GenesisValidator in the ExportedApp struct:

  • x/genutil/types/genesis.go: Two instances of cmttypes.GenesisValidator usage
  • x/genutil/migration/migration.go: One instance in a struct field definition

Action items:

  • Update these instances to use sdk.GenesisValidator instead of cmttypes.GenesisValidator.
  • Carefully review the affected files to ensure proper migration and maintain functionality.
  • Test thoroughly after making these changes to verify that validator-related operations still work as expected.
Analysis chain

Verify compatibility with existing code.

The change from cmttypes.GenesisValidator to sdk.GenesisValidator aligns with the shift towards using Cosmos SDK types for the Validators structure. This modification is likely part of a larger refactor to integrate more closely with the Cosmos SDK.

However, it's important to ensure that existing code relying on the cmttypes.GenesisValidator type is updated to handle the new sdk.GenesisValidator type to maintain compatibility and functionality related to validator management.

Run the following script to identify instances of cmttypes.GenesisValidator usage in the codebase:

If the script returns instances of cmttypes.GenesisValidator usage outside of test files or comments, update the code to handle the new sdk.GenesisValidator type accordingly.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Search for usage of `cmttypes.GenesisValidator` in Go files.

# Test: Search for the type usage. Expect: Only occurrences in test files or comments.
rg --type go $'cmttypes\.GenesisValidator'

Length of output: 287

// Height is the app's latest block height.
Height int64
// ConsensusParams are the exported consensus params for ABCI.
Expand Down
19 changes: 1 addition & 18 deletions simapp/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ import (
"fmt"

cmtproto "github.com/cometbft/cometbft/api/cometbft/types/v1"
cmttypes "github.com/cometbft/cometbft/types"

"cosmossdk.io/collections"
storetypes "cosmossdk.io/store/types"
slashingtypes "cosmossdk.io/x/slashing/types"
"cosmossdk.io/x/staking"
stakingtypes "cosmossdk.io/x/staking/types"

cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
Expand Down Expand Up @@ -43,25 +41,10 @@ func (app *SimApp) ExportAppStateAndValidators(forZeroHeight bool, jailAllowedAd
}

validators, err := staking.WriteValidators(ctx, app.StakingKeeper)
cmtValidators := []cmttypes.GenesisValidator{}
for _, val := range validators {
cmtPk, err := cryptocodec.ToCmtPubKeyInterface(val.PubKey)
if err != nil {
return servertypes.ExportedApp{}, err
}
cmtVal := cmttypes.GenesisValidator{
Address: val.Address.Bytes(),
PubKey: cmtPk,
Power: val.Power,
Name: val.Name,
}

cmtValidators = append(cmtValidators, cmtVal)
}

return servertypes.ExportedApp{
AppState: appState,
Validators: cmtValidators,
Validators: validators,
Height: height,
ConsensusParams: app.BaseApp.GetConsensusParams(ctx),
}, err
Expand Down
10 changes: 0 additions & 10 deletions types/genesis.go

This file was deleted.

8 changes: 8 additions & 0 deletions types/staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,11 @@ type ValidatorI interface {
SharesFromTokens(amt math.Int) (math.LegacyDec, error) // shares worth of delegator's bond
SharesFromTokensTruncated(amt math.Int) (math.LegacyDec, error) // truncated shares worth of delegator's bond
}

// GenesisValidator is an initial validator.
type GenesisValidator struct {
Address ConsAddress
PubKey cryptotypes.PubKey
Power int64
Name string
}
45 changes: 39 additions & 6 deletions x/genutil/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
cmtjson "github.com/cometbft/cometbft/libs/json"
cmttypes "github.com/cometbft/cometbft/types"

cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/version"
)

Expand Down Expand Up @@ -118,6 +120,22 @@ func AppGenesisFromReader(reader io.Reader) (*AppGenesis, error) {
return nil, err
}

vals := []sdk.GenesisValidator{}
for _, cmtVal := range ctmGenesis.Validators {
cmtPk, err := cryptocodec.FromCmtPubKeyInterface(cmtVal.PubKey)
if err != nil {
return nil, err
}
val := sdk.GenesisValidator{
Address: cmtVal.Address.Bytes(),
PubKey: cmtPk,
Power: cmtVal.Power,
Name: cmtVal.Name,
}

vals = append(vals, val)
}

ag = AppGenesis{
AppName: version.AppName,
// AppVersion is not filled as we do not know it from a CometBFT genesis
Expand All @@ -127,7 +145,7 @@ func AppGenesisFromReader(reader io.Reader) (*AppGenesis, error) {
AppHash: ctmGenesis.AppHash,
AppState: ctmGenesis.AppState,
Consensus: &ConsensusGenesis{
Validators: ctmGenesis.Validators,
Validators: vals,
Params: ctmGenesis.ConsensusParams,
},
}
Expand Down Expand Up @@ -160,27 +178,42 @@ func AppGenesisFromFile(genFile string) (*AppGenesis, error) {

// ToGenesisDoc converts the AppGenesis to a CometBFT GenesisDoc.
func (ag *AppGenesis) ToGenesisDoc() (*cmttypes.GenesisDoc, error) {
cmtValidators := []cmttypes.GenesisValidator{}
for _, val := range ag.Consensus.Validators {
cmtPk, err := cryptocodec.ToCmtPubKeyInterface(val.PubKey)
if err != nil {
return nil, err
}
cmtVal := cmttypes.GenesisValidator{
Address: val.Address.Bytes(),
PubKey: cmtPk,
Power: val.Power,
Name: val.Name,
}

cmtValidators = append(cmtValidators, cmtVal)
}
return &cmttypes.GenesisDoc{
GenesisTime: ag.GenesisTime,
ChainID: ag.ChainID,
InitialHeight: ag.InitialHeight,
AppHash: ag.AppHash,
AppState: ag.AppState,
Validators: ag.Consensus.Validators,
Validators: cmtValidators,
ConsensusParams: ag.Consensus.Params,
}, nil
}

// ConsensusGenesis defines the consensus layer's genesis.
// TODO(@julienrbrt) eventually abstract from CometBFT types
type ConsensusGenesis struct {
Validators []cmttypes.GenesisValidator `json:"validators,omitempty"`
Params *cmttypes.ConsensusParams `json:"params,omitempty"`
Validators []sdk.GenesisValidator `json:"validators,omitempty"`
Params *cmttypes.ConsensusParams `json:"params,omitempty"`
}

// NewConsensusGenesis returns a ConsensusGenesis with given values.
// It takes a proto consensus params so it can called from server export command.
func NewConsensusGenesis(params cmtproto.ConsensusParams, validators []cmttypes.GenesisValidator) *ConsensusGenesis {
func NewConsensusGenesis(params cmtproto.ConsensusParams, validators []sdk.GenesisValidator) *ConsensusGenesis {
return &ConsensusGenesis{
Params: &cmttypes.ConsensusParams{
Block: cmttypes.BlockParams{
Expand Down Expand Up @@ -241,7 +274,7 @@ func (cs *ConsensusGenesis) ValidateAndComplete() error {
return fmt.Errorf("incorrect address for validator %v in the genesis file, should be %v", v, v.PubKey.Address())
}
if len(v.Address) == 0 {
cs.Validators[i].Address = v.PubKey.Address()
cs.Validators[i].Address = v.PubKey.Address().Bytes()
}
}

Expand Down
4 changes: 2 additions & 2 deletions x/genutil/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import (
tmed25519 "github.com/cometbft/cometbft/crypto/ed25519"
"github.com/cometbft/cometbft/p2p"
"github.com/cometbft/cometbft/privval"
cmttypes "github.com/cometbft/cometbft/types"
"github.com/cosmos/go-bip39"

cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/genutil/types"
)

Expand All @@ -35,7 +35,7 @@ func ExportGenesisFile(genesis *types.AppGenesis, genFile string) error {
// ExportGenesisFileWithTime creates and writes the genesis configuration to disk.
// An error is returned if building or writing the configuration to file fails.
func ExportGenesisFileWithTime(
genFile, chainID string, validators []cmttypes.GenesisValidator, appState json.RawMessage, genTime time.Time,
genFile, chainID string, validators []sdk.GenesisValidator, appState json.RawMessage, genTime time.Time,
) error {
appGenesis := types.NewAppGenesisWithVersion(chainID, appState)
appGenesis.GenesisTime = genTime
Expand Down
16 changes: 2 additions & 14 deletions x/staking/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,11 @@ import (
"cosmossdk.io/x/staking/keeper"
"cosmossdk.io/x/staking/types"

cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// TODO: move this to sdk types and use this instead of comet types GenesisValidator
// then we can do pubkey conversion in ToGenesisDoc
//
// this is a temporary work around to avoid import comet directly in staking
type GenesisValidator struct {
Address sdk.ConsAddress
PubKey cryptotypes.PubKey
Power int64
Name string
}

// WriteValidators returns a slice of bonded genesis validators.
func WriteValidators(ctx context.Context, keeper *keeper.Keeper) (vals []GenesisValidator, returnErr error) {
func WriteValidators(ctx context.Context, keeper *keeper.Keeper) (vals []sdk.GenesisValidator, returnErr error) {
err := keeper.LastValidatorPower.Walk(ctx, nil, func(key []byte, _ gogotypes.Int64Value) (bool, error) {
validator, err := keeper.GetValidator(ctx, key)
if err != nil {
Expand All @@ -38,7 +26,7 @@ func WriteValidators(ctx context.Context, keeper *keeper.Keeper) (vals []Genesis
return true, err
}

vals = append(vals, GenesisValidator{
vals = append(vals, sdk.GenesisValidator{
Address: sdk.ConsAddress(pk.Address()),
PubKey: pk,
Power: validator.GetConsensusPower(keeper.PowerReduction(ctx)),
Expand Down
Loading