From d727f9be359311fada85a1e0fc11db66afc19f7f Mon Sep 17 00:00:00 2001 From: son trinh Date: Wed, 9 Oct 2024 12:18:05 +0700 Subject: [PATCH] refactor(genutil): Use sdk types genesis validator (#21678) Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: Matt Kocubinski --- crypto/codec/pubkey.go | 53 ++++++ crypto/keys/jsonkey.go | 41 +++++ server/types/app.go | 4 +- simapp/export.go | 19 +- simapp/v2/app_di.go | 13 +- simapp/v2/app_test.go | 5 +- simapp/v2/export.go | 35 +++- tests/e2e/genutil/export_test.go | 3 +- types/staking.go | 9 + x/genutil/client/cli/migrate.go | 23 ++- x/genutil/migration/v052/migrate.go | 173 ++++++++++++++++++ x/genutil/migration/v052/migrate_test.go | 51 ++++++ .../v052/testdata/old_app_genesis.json | 1 + x/genutil/types/genesis.go | 59 +++++- x/genutil/types/testdata/app_genesis.json | 2 +- x/genutil/utils.go | 4 +- x/genutil/v2/cli/export.go | 1 + x/genutil/v2/types.go | 4 + x/staking/genesis.go | 25 +-- 19 files changed, 459 insertions(+), 66 deletions(-) create mode 100644 crypto/codec/pubkey.go create mode 100644 crypto/keys/jsonkey.go create mode 100644 x/genutil/migration/v052/migrate.go create mode 100644 x/genutil/migration/v052/migrate_test.go create mode 100644 x/genutil/migration/v052/testdata/old_app_genesis.json diff --git a/crypto/codec/pubkey.go b/crypto/codec/pubkey.go new file mode 100644 index 000000000000..c45f258517b0 --- /dev/null +++ b/crypto/codec/pubkey.go @@ -0,0 +1,53 @@ +package codec + +import ( + "cosmossdk.io/errors" + + cryptokeys "github.com/cosmos/cosmos-sdk/crypto/keys" + bls12_381 "github.com/cosmos/cosmos-sdk/crypto/keys/bls12_381" + "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" + "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" + cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" +) + +func PubKeyToProto(pk cryptokeys.JSONPubkey) (cryptotypes.PubKey, error) { + switch pk.KeyType { + case ed25519.PubKeyName: + return &ed25519.PubKey{ + Key: pk.Value, + }, nil + case secp256k1.PubKeyName: + return &secp256k1.PubKey{ + Key: pk.Value, + }, nil + case bls12_381.PubKeyName: + return &bls12_381.PubKey{ + Key: pk.Value, + }, nil + default: + return nil, errors.Wrapf(sdkerrors.ErrInvalidType, "cannot convert %v to proto public key", pk) + } +} + +func PubKeyFromProto(pk cryptotypes.PubKey) (cryptokeys.JSONPubkey, error) { + switch pk := pk.(type) { + case *ed25519.PubKey: + return cryptokeys.JSONPubkey{ + KeyType: ed25519.PubKeyName, + Value: pk.Bytes(), + }, nil + case *secp256k1.PubKey: + return cryptokeys.JSONPubkey{ + KeyType: secp256k1.PubKeyName, + Value: pk.Bytes(), + }, nil + case *bls12_381.PubKey: + return cryptokeys.JSONPubkey{ + KeyType: bls12_381.PubKeyName, + Value: pk.Bytes(), + }, nil + default: + return cryptokeys.JSONPubkey{}, errors.Wrapf(sdkerrors.ErrInvalidType, "cannot convert %v from proto public key", pk) + } +} diff --git a/crypto/keys/jsonkey.go b/crypto/keys/jsonkey.go new file mode 100644 index 000000000000..8edbd91e7246 --- /dev/null +++ b/crypto/keys/jsonkey.go @@ -0,0 +1,41 @@ +package keys + +import ( + "github.com/cosmos/cosmos-sdk/crypto/keys/bls12_381" + "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" + "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" + "github.com/cosmos/cosmos-sdk/crypto/types" +) + +// JSONPubKey defines a public key that are parse from JSON file. +// convert PubKey to JSONPubKey needs a in between step +type JSONPubkey struct { + KeyType string `json:"type"` + Value []byte `json:"value"` +} + +func (pk JSONPubkey) Address() types.Address { + switch pk.KeyType { + case ed25519.PubKeyName: + ed25519 := ed25519.PubKey{ + Key: pk.Value, + } + return ed25519.Address() + case secp256k1.PubKeyName: + secp256k1 := secp256k1.PubKey{ + Key: pk.Value, + } + return secp256k1.Address() + case bls12_381.PubKeyName: + bls12_381 := bls12_381.PubKey{ + Key: pk.Value, + } + return bls12_381.Address() + default: + return nil + } +} + +func (pk JSONPubkey) Bytes() []byte { + return pk.Value +} diff --git a/server/types/app.go b/server/types/app.go index ca0a55ca1a0f..c1a67b2ceec3 100644 --- a/server/types/app.go +++ b/server/types/app.go @@ -6,7 +6,6 @@ import ( cmtproto "github.com/cometbft/cometbft/api/cometbft/types/v1" cmtcrypto "github.com/cometbft/cometbft/crypto" - cmttypes "github.com/cometbft/cometbft/types" "github.com/cosmos/gogoproto/grpc" "cosmossdk.io/core/server" @@ -18,6 +17,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 ( @@ -76,7 +76,7 @@ type ( // AppState is the application state as JSON. AppState json.RawMessage // Validators is the exported validator set. - Validators []cmttypes.GenesisValidator + Validators []sdk.GenesisValidator // Height is the app's latest block height. Height int64 // ConsensusParams are the exported consensus params for ABCI. diff --git a/simapp/export.go b/simapp/export.go index 05346741b99e..28d15f507a9b 100644 --- a/simapp/export.go +++ b/simapp/export.go @@ -5,7 +5,6 @@ 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" @@ -13,7 +12,6 @@ import ( "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" ) @@ -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 diff --git a/simapp/v2/app_di.go b/simapp/v2/app_di.go index d66b17ddb8ca..29f62defcde6 100644 --- a/simapp/v2/app_di.go +++ b/simapp/v2/app_di.go @@ -13,10 +13,12 @@ import ( "cosmossdk.io/log" "cosmossdk.io/runtime/v2" serverstore "cosmossdk.io/server/v2/store" + "cosmossdk.io/store/v2" "cosmossdk.io/store/v2/root" basedepinject "cosmossdk.io/x/accounts/defaults/base/depinject" lockupdepinject "cosmossdk.io/x/accounts/defaults/lockup/depinject" multisigdepinject "cosmossdk.io/x/accounts/defaults/multisig/depinject" + stakingkeeper "cosmossdk.io/x/staking/keeper" upgradekeeper "cosmossdk.io/x/upgrade/keeper" "github.com/cosmos/cosmos-sdk/client" @@ -38,10 +40,12 @@ type SimApp[T transaction.Tx] struct { appCodec codec.Codec txConfig client.TxConfig interfaceRegistry codectypes.InterfaceRegistry + store store.RootStore // required keepers during wiring // others keepers are all in the app UpgradeKeeper *upgradekeeper.Keeper + StakingKeeper *stakingkeeper.Keeper } func init() { @@ -161,6 +165,7 @@ func NewSimApp[T transaction.Tx]( &app.txConfig, &app.interfaceRegistry, &app.UpgradeKeeper, + &app.StakingKeeper, ); err != nil { panic(err) } @@ -170,7 +175,8 @@ func NewSimApp[T transaction.Tx]( if err != nil { panic(err) } - _, err = storeBuilder.Build(logger, storeConfig) + + app.store, err = storeBuilder.Build(logger, storeConfig) if err != nil { panic(err) } @@ -213,7 +219,6 @@ func (app *SimApp[T]) TxConfig() client.TxConfig { return app.txConfig } -// GetStore gets the app store. -func (app *SimApp[T]) GetStore() any { - return app.App.GetStore() +func (app *SimApp[T]) GetStore() store.RootStore { + return app.store } diff --git a/simapp/v2/app_test.go b/simapp/v2/app_test.go index 2b42c108ae8a..5d2b15811249 100644 --- a/simapp/v2/app_test.go +++ b/simapp/v2/app_test.go @@ -20,7 +20,6 @@ import ( sdkmath "cosmossdk.io/math" "cosmossdk.io/runtime/v2" serverv2 "cosmossdk.io/server/v2" - comettypes "cosmossdk.io/server/v2/cometbft/types" "cosmossdk.io/store/v2/db" banktypes "cosmossdk.io/x/bank/types" @@ -74,7 +73,7 @@ func NewTestApp(t *testing.T) (*SimApp[transaction.Tx], context.Context) { genesisBytes, err := json.Marshal(genesis) require.NoError(t, err) - st := app.GetStore().(comettypes.Store) + st := app.GetStore() ci, err := st.LastCommitID() require.NoError(t, err) @@ -110,7 +109,7 @@ func MoveNextBlock(t *testing.T, app *SimApp[transaction.Tx], ctx context.Contex bz := sha256.Sum256([]byte{}) - st := app.GetStore().(comettypes.Store) + st := app.GetStore() ci, err := st.LastCommitID() require.NoError(t, err) diff --git a/simapp/v2/export.go b/simapp/v2/export.go index 5a1757b16535..50f4a898bb37 100644 --- a/simapp/v2/export.go +++ b/simapp/v2/export.go @@ -3,27 +3,46 @@ package simapp import ( "context" + "cosmossdk.io/runtime/v2/services" + "cosmossdk.io/x/staking" + v2 "github.com/cosmos/cosmos-sdk/x/genutil/v2" ) // ExportAppStateAndValidators exports the state of the application for a genesis // file. -func (app *SimApp[T]) ExportAppStateAndValidators(jailAllowedAddrs []string) (v2.ExportedApp, error) { - // as if they could withdraw from the start of the next block +// This is a demonstation of how to export a genesis file. Export may need extended at +// the user discretion for cleaning the genesis state at the end provided with jailAllowedAddrs +func (app *SimApp[T]) ExportAppStateAndValidators( + jailAllowedAddrs []string, +) (v2.ExportedApp, error) { ctx := context.Background() + var exportedApp v2.ExportedApp latestHeight, err := app.LoadLatestHeight() if err != nil { - return v2.ExportedApp{}, err + return exportedApp, err } genesis, err := app.ExportGenesis(ctx, latestHeight) if err != nil { - return v2.ExportedApp{}, err + return exportedApp, err + } + + readerMap, err := app.GetStore().StateAt(latestHeight) + if err != nil { + return exportedApp, err + } + genesisCtx := services.NewGenesisContext(readerMap) + err = genesisCtx.Read(ctx, func(ctx context.Context) error { + exportedApp.Validators, err = staking.WriteValidators(ctx, app.StakingKeeper) + return err + }) + if err != nil { + return exportedApp, err } - return v2.ExportedApp{ - AppState: genesis, - Height: int64(latestHeight), - }, nil + exportedApp.AppState = genesis + exportedApp.Height = int64(latestHeight) + return exportedApp, nil } diff --git a/tests/e2e/genutil/export_test.go b/tests/e2e/genutil/export_test.go index 0a98ef6b4e85..798885c0900f 100644 --- a/tests/e2e/genutil/export_test.go +++ b/tests/e2e/genutil/export_test.go @@ -31,6 +31,7 @@ import ( "github.com/cosmos/cosmos-sdk/server/types" simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" gentestutil "github.com/cosmos/cosmos-sdk/testutil/x/genutil" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/genutil" genutilcli "github.com/cosmos/cosmos-sdk/x/genutil/client/cli" genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types" @@ -184,7 +185,7 @@ func setupApp(t *testing.T, tempDir string) (*simapp.SimApp, context.Context, ge ChainID: "theChainId", AppState: stateBytes, Consensus: &genutiltypes.ConsensusGenesis{ - Validators: nil, + Validators: []sdk.GenesisValidator{}, }, } diff --git a/types/staking.go b/types/staking.go index f8cdb325038c..b33e32ae9d04 100644 --- a/types/staking.go +++ b/types/staking.go @@ -3,6 +3,7 @@ package types import ( "cosmossdk.io/math" + cryptokeys "github.com/cosmos/cosmos-sdk/crypto/keys" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" ) @@ -98,3 +99,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 `json:"address"` + PubKey cryptokeys.JSONPubkey `json:"pub_key"` + Power int64 `json:"power"` + Name string `json:"name"` +} diff --git a/x/genutil/client/cli/migrate.go b/x/genutil/client/cli/migrate.go index 2b2a1c856b74..912aae4600f8 100644 --- a/x/genutil/client/cli/migrate.go +++ b/x/genutil/client/cli/migrate.go @@ -13,13 +13,19 @@ import ( "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/version" + v052 "github.com/cosmos/cosmos-sdk/x/genutil/migration/v052" "github.com/cosmos/cosmos-sdk/x/genutil/types" ) -const flagGenesisTime = "genesis-time" +const ( + flagGenesisTime = "genesis-time" + v52 = "v0.52" +) // MigrationMap is a map of SDK versions to their respective genesis migration functions. -var MigrationMap = types.MigrationMap{} +var MigrationMap = types.MigrationMap{ + v52: v052.Migrate, +} // MigrateGenesisCmd returns a command to execute genesis state migration. // Applications should pass their own migration map to this function. @@ -56,7 +62,17 @@ func MigrateHandler(cmd *cobra.Command, args []string, migrations types.Migratio } importGenesis := args[1] - appGenesis, err := types.AppGenesisFromFile(importGenesis) + outputDocument, _ := cmd.Flags().GetString(flags.FlagOutputDocument) + + // for v52 we need to migrate the consensus validator address from hex bytes to + // sdk consensus address. + var appGenesis *types.AppGenesis + var err error + if target == v52 { + appGenesis, err = v052.MigrateGenesisFile(importGenesis) + } else { + appGenesis, err = types.AppGenesisFromFile(importGenesis) + } if err != nil { return err } @@ -110,7 +126,6 @@ func MigrateHandler(cmd *cobra.Command, args []string, migrations types.Migratio return fmt.Errorf("failed to marshal app genesis: %w", err) } - outputDocument, _ := cmd.Flags().GetString(flags.FlagOutputDocument) if outputDocument == "" { cmd.Println(string(bz)) return nil diff --git a/x/genutil/migration/v052/migrate.go b/x/genutil/migration/v052/migrate.go new file mode 100644 index 000000000000..350240850cff --- /dev/null +++ b/x/genutil/migration/v052/migrate.go @@ -0,0 +1,173 @@ +package migrate + +import ( + "encoding/json" + "errors" + "fmt" + "io" + "os" + "path/filepath" + "time" + + cmtjson "github.com/cometbft/cometbft/libs/json" + cmttypes "github.com/cometbft/cometbft/types" + + "github.com/cosmos/cosmos-sdk/client" + cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/version" + "github.com/cosmos/cosmos-sdk/x/genutil/types" +) + +type legacyAppGenesis struct { + AppName string `json:"app_name"` + AppVersion string `json:"app_version"` + GenesisTime time.Time `json:"genesis_time"` + ChainID string `json:"chain_id"` + InitialHeight int64 `json:"initial_height"` + AppHash []byte `json:"app_hash"` + AppState json.RawMessage `json:"app_state,omitempty"` + Consensus *legacyConsensusGenesis `json:"consensus,omitempty"` +} + +type legacyConsensusGenesis struct { + Validators []cmttypes.GenesisValidator `json:"validators,omitempty"` + Params *cmttypes.ConsensusParams `json:"params,omitempty"` +} + +func MigrateGenesisFile(oldGenFile string) (*types.AppGenesis, error) { + file, err := os.Open(filepath.Clean(oldGenFile)) + if err != nil { + return nil, err + } + defer file.Close() + + appGenesis, err := migrateGenesisValidator(file) + if err != nil { + return nil, fmt.Errorf("failed to read genesis from file %s: %w", oldGenFile, err) + } + + return appGenesis, nil +} + +// migrateGenesisValidator migrate current genesis file genesis validator to match of the +// new genesis validator type. +func migrateGenesisValidator(r io.Reader) (*types.AppGenesis, error) { + var newAg types.AppGenesis + var ag legacyAppGenesis + var err error + + if rs, ok := r.(io.ReadSeeker); ok { + err = json.NewDecoder(rs).Decode(&ag) + if err == nil { + vals, err := convertValidators(ag.Consensus.Validators) + if err != nil { + return nil, err + } + newAg = types.AppGenesis{ + AppName: ag.AppName, + AppVersion: ag.AppVersion, + GenesisTime: ag.GenesisTime, + ChainID: ag.ChainID, + InitialHeight: ag.InitialHeight, + AppHash: ag.AppHash, + AppState: ag.AppState, + Consensus: &types.ConsensusGenesis{ + Validators: vals, + Params: ag.Consensus.Params, + }, + } + + return &newAg, nil + } + + err = fmt.Errorf("error unmarshalling legacy AppGenesis: %w", err) + if _, serr := rs.Seek(0, io.SeekStart); serr != nil { + err = errors.Join(err, fmt.Errorf("error seeking back to the front: %w", serr)) + return nil, err + } + } + + jsonBlob, ioerr := io.ReadAll(r) + if ioerr != nil { + err = errors.Join(err, fmt.Errorf("failed to read file completely: %w", ioerr)) + return nil, err + } + + // fallback to comet genesis parsing + var ctmGenesis cmttypes.GenesisDoc + if uerr := cmtjson.Unmarshal(jsonBlob, &ctmGenesis); uerr != nil { + err = errors.Join(err, fmt.Errorf("failed fallback to CometBFT GenDoc: %w", uerr)) + return nil, err + } + + vals, err := convertValidators(ctmGenesis.Validators) + if err != nil { + return nil, err + } + newAg = types.AppGenesis{ + AppName: version.AppName, + GenesisTime: ctmGenesis.GenesisTime, + ChainID: ctmGenesis.ChainID, + InitialHeight: ctmGenesis.InitialHeight, + AppHash: ctmGenesis.AppHash, + AppState: ctmGenesis.AppState, + Consensus: &types.ConsensusGenesis{ + Validators: vals, + Params: ctmGenesis.ConsensusParams, + }, + } + + return &newAg, nil +} + +func convertValidators(cmtVals []cmttypes.GenesisValidator) ([]sdk.GenesisValidator, error) { + vals := make([]sdk.GenesisValidator, len(cmtVals)) + for i, cmtVal := range cmtVals { + pk, err := cryptocodec.FromCmtPubKeyInterface(cmtVal.PubKey) + if err != nil { + return nil, err + } + jsonPk, err := cryptocodec.PubKeyFromProto(pk) + if err != nil { + return nil, err + } + vals[i] = sdk.GenesisValidator{ + Address: cmtVal.Address.Bytes(), + PubKey: jsonPk, + Power: cmtVal.Power, + Name: cmtVal.Name, + } + } + return vals, nil +} + +// CometBFT Genesis Handling for JSON, +// this is necessary for json unmarshaling of legacyConsensusGenesis +func (cs *legacyConsensusGenesis) MarshalJSON() ([]byte, error) { + type Alias legacyConsensusGenesis + return cmtjson.Marshal(&Alias{ + Validators: cs.Validators, + Params: cs.Params, + }) +} + +func (cs *legacyConsensusGenesis) UnmarshalJSON(b []byte) error { + type Alias legacyConsensusGenesis + + result := Alias{} + if err := cmtjson.Unmarshal(b, &result); err != nil { + return err + } + + cs.Params = result.Params + cs.Validators = result.Validators + + return nil +} + +// since we only need migrate the consensus validators content so there is no +// exported state migration. +func Migrate(appState types.AppMap, _ client.Context) (types.AppMap, error) { + return appState, nil +} diff --git a/x/genutil/migration/v052/migrate_test.go b/x/genutil/migration/v052/migrate_test.go new file mode 100644 index 000000000000..5c38bb3401fa --- /dev/null +++ b/x/genutil/migration/v052/migrate_test.go @@ -0,0 +1,51 @@ +package migrate + +import ( + "bytes" + "encoding/json" + "os" + "testing" + + "github.com/stretchr/testify/require" + + "github.com/cosmos/cosmos-sdk/x/genutil/types" +) + +var oldGenFilePath = "./testdata/old_app_genesis.json" + +func TestMigration(t *testing.T) { + tempDir := t.TempDir() + + // clean all content on this directory + err := os.RemoveAll(tempDir) + require.NoError(t, err) + + // should not be able to get app genesis from new genesis file + // since validators address are still in hex string and not cons address + _, err = types.AppGenesisFromFile(oldGenFilePath) + require.ErrorContains(t, err, "error unmarshalling AppGenesis: decoding bech32 failed") + + newAppGenesis, err := MigrateGenesisFile(oldGenFilePath) + require.NoError(t, err) + // save the new app genesis to new temp dir + err = newAppGenesis.SaveAs(tempDir) + require.NoError(t, err) + + // read the old app genesis to compare with the new app genesis + var oldAppGenesis legacyAppGenesis + r, err := os.Open(oldGenFilePath) + require.NoError(t, err) + err = json.NewDecoder(r).Decode(&oldAppGenesis) + require.NoError(t, err) + + // should be able to get app genesis from new genesis file + newAppGenesis, err = types.AppGenesisFromFile(tempDir) + require.NotNil(t, newAppGenesis) + require.NotNil(t, newAppGenesis.Consensus) + require.True(t, bytes.Equal(oldAppGenesis.AppHash, newAppGenesis.AppHash)) + require.True(t, bytes.Equal(oldAppGenesis.Consensus.Validators[0].Address.Bytes(), newAppGenesis.Consensus.Validators[0].Address.Bytes())) + require.True(t, bytes.Equal(oldAppGenesis.Consensus.Validators[0].PubKey.Bytes(), newAppGenesis.Consensus.Validators[0].PubKey.Bytes())) + require.Equal(t, len(oldAppGenesis.Consensus.Validators), len(newAppGenesis.Consensus.Validators), "Number of validators should remain the same after migration") + + require.NoError(t, err) +} diff --git a/x/genutil/migration/v052/testdata/old_app_genesis.json b/x/genutil/migration/v052/testdata/old_app_genesis.json new file mode 100644 index 000000000000..57ca37536b6d --- /dev/null +++ b/x/genutil/migration/v052/testdata/old_app_genesis.json @@ -0,0 +1 @@ +{"app_name":"\u003cappd\u003e","app_version":"","genesis_time":"2023-02-20T11:08:30.588307671Z","chain_id":"demo","initial_height":48,"app_hash":"","app_state":{"auth":{"accounts":[{"@type":"/cosmos.auth.v1beta1.BaseAccount","account_number":"1","address":"cosmos1qmkksxlxqdslq6kkca25m4jn344nx29lytq8f9","pub_key":null,"sequence":"0"},{"@type":"/cosmos.auth.v1beta1.BaseAccount","account_number":"8","address":"cosmos1pnt5523etwtzv6mj7haryfw6w8h5tkcuhd99m8","pub_key":null,"sequence":"0"},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"4","address":"cosmos1fl48vsnmsdzcv85q5d2q4z5ajdha8yu34mf0eh","pub_key":null,"sequence":"0"},"name":"bonded_tokens_pool","permissions":["burner","staking"]},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"5","address":"cosmos1tygms3xhhs3yv487phx3dw4a95jn7t7lpm470r","pub_key":null,"sequence":"0"},"name":"not_bonded_tokens_pool","permissions":["burner","staking"]},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"6","address":"cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn","pub_key":null,"sequence":"0"},"name":"gov","permissions":["burner"]},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"3","address":"cosmos1jv65s3grqf6v6jl3dp4t6c9t9rk99cd88lyufl","pub_key":null,"sequence":"0"},"name":"distribution","permissions":[]},{"@type":"/cosmos.auth.v1beta1.BaseAccount","account_number":"0","address":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","pub_key":{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"A3uyZdXedyvYx9VCL6xRjkxtcFpgxjhXFIz9b2mWz+aV"},"sequence":"4"},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"7","address":"cosmos1m3h30wlvsf8llruxtpukdvsy0km2kum8g38c8q","pub_key":null,"sequence":"0"},"name":"mint","permissions":["minter"]},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"2","address":"cosmos17xpfvakm2amg962yls6f84z3kell8c5lserqta","pub_key":null,"sequence":"0"},"name":"fee_collector","permissions":[]}],"params":{"max_memo_characters":"256","sig_verify_cost_ed25519":"590","sig_verify_cost_secp256k1":"1000","tx_sig_limit":"7","tx_size_cost_per_byte":"10"}},"authz":{"authorization":[]},"bank":{"balances":[{"address":"cosmos1qmkksxlxqdslq6kkca25m4jn344nx29lytq8f9","coins":[{"amount":"5000000000","denom":"stake"}]},{"address":"cosmos1pnt5523etwtzv6mj7haryfw6w8h5tkcuhd99m8","coins":[{"amount":"1000","denom":"stake"}]},{"address":"cosmos1fl48vsnmsdzcv85q5d2q4z5ajdha8yu34mf0eh","coins":[{"amount":"1000000","denom":"stake"}]},{"address":"cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn","coins":[{"amount":"10010000","denom":"stake"}]},{"address":"cosmos1jv65s3grqf6v6jl3dp4t6c9t9rk99cd88lyufl","coins":[{"amount":"9635","denom":"stake"}]},{"address":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","coins":[{"amount":"4988989000","denom":"stake"}]}],"denom_metadata":[],"params":{"default_send_enabled":true,"send_enabled":[]},"send_enabled":[],"supply":[{"amount":"10000009635","denom":"stake"}]},"consensus":null,"crisis":{"constant_fee":{"amount":"1000","denom":"stake"}},"distribution":{"delegator_starting_infos":[{"delegator_address":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","starting_info":{"height":"0","previous_period":"1","stake":"1000000.000000000000000000"},"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"delegator_withdraw_infos":[],"fee_pool":{"community_pool":[{"amount":"192.700000000000000000","denom":"stake"}]},"outstanding_rewards":[{"outstanding_rewards":[{"amount":"9442.300000000000000000","denom":"stake"}],"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"params":{"base_proposer_reward":"0.000000000000000000","bonus_proposer_reward":"0.000000000000000000","community_tax":"0.020000000000000000","withdraw_addr_enabled":true},"previous_proposer":"cosmosvalcons16vm0nx49eam4q0xasdnwdzsdl6ymgyjt757sgr","validator_accumulated_commissions":[{"accumulated":{"commission":[{"amount":"944.230000000000000000","denom":"stake"}]},"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"validator_current_rewards":[{"rewards":{"period":"2","rewards":[{"amount":"8498.070000000000000000","denom":"stake"}]},"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"validator_historical_rewards":[{"period":"1","rewards":{"cumulative_reward_ratio":[],"reference_count":2},"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"validator_slash_events":[]},"evidence":{"evidence":[]},"feegrant":{"allowances":[]},"genutil":{"gen_txs":[]},"gov":{"deposit_params":null,"deposits":[{"amount":[{"amount":"10010000","denom":"stake"}],"depositor":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","proposal_id":"1"}],"params":{"expedited_min_deposit":[{"amount":"50000000","denom":"stake"}],"expedited_threshold":"0.667000000000000000","expedited_voting_period":"86400s","max_deposit_period":"172800s","min_deposit":[{"amount":"10000000","denom":"stake"}],"min_initial_deposit_ratio":"0.000000000000000000","proposal_cancel_dest":"","proposal_cancel_ratio":"0.500000000000000000","quorum":"0.334000000000000000","threshold":"0.500000000000000000","veto_threshold":"0.334000000000000000","voting_period":"172800s"},"proposals":[{"deposit_end_time":"2023-02-22T11:11:52.776167376Z","expedited":false,"final_tally_result":{"abstain_count":"0","no_count":"0","no_with_veto_count":"0","yes_count":"0"},"id":"1","messages":[{"@type":"/cosmos.distribution.v1beta1.MsgCommunityPoolSpend","amount":[],"authority":"cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn","recipient":"cosmos1pnt5523etwtzv6mj7haryfw6w8h5tkcuhd99m8"}],"metadata":"ipfs://CID","proposer":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","status":"PROPOSAL_STATUS_VOTING_PERIOD","submit_time":"2023-02-20T11:11:52.776167376Z","summary":"test proposal","title":"test proposal","total_deposit":[{"amount":"10010000","denom":"stake"}],"voting_end_time":"2023-02-22T11:12:07.801161984Z","voting_start_time":"2023-02-20T11:12:07.801161984Z"}],"starting_proposal_id":"2","tally_params":null,"votes":[],"voting_params":null},"group":{"group_members":[],"group_policies":[],"group_policy_seq":"0","group_seq":"0","groups":[],"proposal_seq":"0","proposals":[],"votes":[]},"mint":{"minter":{"annual_provisions":"1300010905.175073197786747950","inflation":"0.130000967926594565"},"params":{"blocks_per_year":"6311520","goal_bonded":"0.670000000000000000","inflation_max":"0.200000000000000000","inflation_min":"0.070000000000000000","inflation_rate_change":"0.130000000000000000","mint_denom":"stake"}},"nft":{"classes":[],"entries":[]},"params":null,"slashing":{"missed_blocks":[{"address":"cosmosvalcons16vm0nx49eam4q0xasdnwdzsdl6ymgyjt757sgr","missed_blocks":[]}],"params":{"downtime_jail_duration":"600s","min_signed_per_window":"0.500000000000000000","signed_blocks_window":"100","slash_fraction_double_sign":"0.050000000000000000","slash_fraction_downtime":"0.010000000000000000"},"signing_infos":[{"address":"cosmosvalcons16vm0nx49eam4q0xasdnwdzsdl6ymgyjt757sgr","validator_signing_info":{"address":"cosmosvalcons16vm0nx49eam4q0xasdnwdzsdl6ymgyjt757sgr","index_offset":"46","jailed_until":"1970-01-01T00:00:00Z","missed_blocks_counter":"0","start_height":"0","tombstoned":false}}]},"staking":{"delegations":[{"delegator_address":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","shares":"1000000.000000000000000000","validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"exported":true,"last_total_power":"1","last_validator_powers":[{"address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp","power":"1"}],"params":{"bond_denom":"stake","historical_entries":10000,"max_entries":7,"max_validators":100,"min_commission_rate":"0.000000000000000000","unbonding_time":"1814400s"},"redelegations":[],"unbonding_delegations":[],"validators":[{"commission":{"commission_rates":{"max_change_rate":"0.010000000000000000","max_rate":"0.200000000000000000","rate":"0.100000000000000000"},"update_time":"2023-02-20T11:08:30.588307671Z"},"consensus_pubkey":{"@type":"/cosmos.crypto.ed25519.PubKey","key":"tMZonPQYoooG/xbFVhHg95pTLxx7aO43/qgHFxDagWM="},"delegator_shares":"1000000.000000000000000000","description":{"details":"","identity":"","moniker":"test","security_contact":"","website":""},"jailed":false,"min_self_delegation":"1","operator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp","status":"BOND_STATUS_BONDED","tokens":"1000000","unbonding_height":"0","unbonding_ids":[],"unbonding_on_hold_ref_count":"0","unbonding_time":"1970-01-01T00:00:00Z"}]},"upgrade":{},"vesting":{}},"consensus":{"validators":[{"address":"D336F99AA5CF77503CDD8366E68A0DFE89B4124B","pub_key":{"type":"tendermint/PubKeyEd25519","value":"tMZonPQYoooG/xbFVhHg95pTLxx7aO43/qgHFxDagWM="},"power":"1","name":"test"}],"params":{"block":{"max_bytes":"22020096","max_gas":"-1"},"evidence":{"max_age_num_blocks":"100000","max_age_duration":"172800000000000","max_bytes":"1048576"},"validator":{"pub_key_types":["ed25519"]},"version":{"app":"0"},"synchrony":{"precision":"0","message_delay":"0"},"feature":{"vote_extensions_enable_height":"0","pbts_enable_height":"0"}}}} \ No newline at end of file diff --git a/x/genutil/types/genesis.go b/x/genutil/types/genesis.go index 1566c0affaac..023abfba082c 100644 --- a/x/genutil/types/genesis.go +++ b/x/genutil/types/genesis.go @@ -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" ) @@ -118,6 +120,26 @@ func AppGenesisFromReader(reader io.Reader) (*AppGenesis, error) { return nil, err } + vals := []sdk.GenesisValidator{} + for _, cmtVal := range ctmGenesis.Validators { + pk, err := cryptocodec.FromCmtPubKeyInterface(cmtVal.PubKey) + if err != nil { + return nil, err + } + jsonPk, err := cryptocodec.PubKeyFromProto(pk) + if err != nil { + return nil, err + } + val := sdk.GenesisValidator{ + Address: cmtVal.Address.Bytes(), + PubKey: jsonPk, + 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 @@ -127,7 +149,7 @@ func AppGenesisFromReader(reader io.Reader) (*AppGenesis, error) { AppHash: ctmGenesis.AppHash, AppState: ctmGenesis.AppState, Consensus: &ConsensusGenesis{ - Validators: ctmGenesis.Validators, + Validators: vals, Params: ctmGenesis.ConsensusParams, }, } @@ -160,13 +182,36 @@ 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 { + pk, err := cryptocodec.PubKeyToProto(val.PubKey) + if err != nil { + return nil, err + } + cmtPk, err := cryptocodec.ToCmtPubKeyInterface(pk) + 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) + } + // assert nil value for empty validators set + if len(cmtValidators) == 0 { + cmtValidators = nil + } 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 } @@ -174,13 +219,13 @@ func (ag *AppGenesis) ToGenesisDoc() (*cmttypes.GenesisDoc, error) { // 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{ @@ -211,7 +256,7 @@ func (cs *ConsensusGenesis) MarshalJSON() ([]byte, error) { func (cs *ConsensusGenesis) UnmarshalJSON(b []byte) error { type Alias ConsensusGenesis - result := Alias{} + var result Alias if err := cmtjson.Unmarshal(b, &result); err != nil { return err } @@ -241,7 +286,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() } } diff --git a/x/genutil/types/testdata/app_genesis.json b/x/genutil/types/testdata/app_genesis.json index 57ca37536b6d..20bca1589f76 100644 --- a/x/genutil/types/testdata/app_genesis.json +++ b/x/genutil/types/testdata/app_genesis.json @@ -1 +1 @@ -{"app_name":"\u003cappd\u003e","app_version":"","genesis_time":"2023-02-20T11:08:30.588307671Z","chain_id":"demo","initial_height":48,"app_hash":"","app_state":{"auth":{"accounts":[{"@type":"/cosmos.auth.v1beta1.BaseAccount","account_number":"1","address":"cosmos1qmkksxlxqdslq6kkca25m4jn344nx29lytq8f9","pub_key":null,"sequence":"0"},{"@type":"/cosmos.auth.v1beta1.BaseAccount","account_number":"8","address":"cosmos1pnt5523etwtzv6mj7haryfw6w8h5tkcuhd99m8","pub_key":null,"sequence":"0"},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"4","address":"cosmos1fl48vsnmsdzcv85q5d2q4z5ajdha8yu34mf0eh","pub_key":null,"sequence":"0"},"name":"bonded_tokens_pool","permissions":["burner","staking"]},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"5","address":"cosmos1tygms3xhhs3yv487phx3dw4a95jn7t7lpm470r","pub_key":null,"sequence":"0"},"name":"not_bonded_tokens_pool","permissions":["burner","staking"]},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"6","address":"cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn","pub_key":null,"sequence":"0"},"name":"gov","permissions":["burner"]},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"3","address":"cosmos1jv65s3grqf6v6jl3dp4t6c9t9rk99cd88lyufl","pub_key":null,"sequence":"0"},"name":"distribution","permissions":[]},{"@type":"/cosmos.auth.v1beta1.BaseAccount","account_number":"0","address":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","pub_key":{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"A3uyZdXedyvYx9VCL6xRjkxtcFpgxjhXFIz9b2mWz+aV"},"sequence":"4"},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"7","address":"cosmos1m3h30wlvsf8llruxtpukdvsy0km2kum8g38c8q","pub_key":null,"sequence":"0"},"name":"mint","permissions":["minter"]},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"2","address":"cosmos17xpfvakm2amg962yls6f84z3kell8c5lserqta","pub_key":null,"sequence":"0"},"name":"fee_collector","permissions":[]}],"params":{"max_memo_characters":"256","sig_verify_cost_ed25519":"590","sig_verify_cost_secp256k1":"1000","tx_sig_limit":"7","tx_size_cost_per_byte":"10"}},"authz":{"authorization":[]},"bank":{"balances":[{"address":"cosmos1qmkksxlxqdslq6kkca25m4jn344nx29lytq8f9","coins":[{"amount":"5000000000","denom":"stake"}]},{"address":"cosmos1pnt5523etwtzv6mj7haryfw6w8h5tkcuhd99m8","coins":[{"amount":"1000","denom":"stake"}]},{"address":"cosmos1fl48vsnmsdzcv85q5d2q4z5ajdha8yu34mf0eh","coins":[{"amount":"1000000","denom":"stake"}]},{"address":"cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn","coins":[{"amount":"10010000","denom":"stake"}]},{"address":"cosmos1jv65s3grqf6v6jl3dp4t6c9t9rk99cd88lyufl","coins":[{"amount":"9635","denom":"stake"}]},{"address":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","coins":[{"amount":"4988989000","denom":"stake"}]}],"denom_metadata":[],"params":{"default_send_enabled":true,"send_enabled":[]},"send_enabled":[],"supply":[{"amount":"10000009635","denom":"stake"}]},"consensus":null,"crisis":{"constant_fee":{"amount":"1000","denom":"stake"}},"distribution":{"delegator_starting_infos":[{"delegator_address":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","starting_info":{"height":"0","previous_period":"1","stake":"1000000.000000000000000000"},"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"delegator_withdraw_infos":[],"fee_pool":{"community_pool":[{"amount":"192.700000000000000000","denom":"stake"}]},"outstanding_rewards":[{"outstanding_rewards":[{"amount":"9442.300000000000000000","denom":"stake"}],"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"params":{"base_proposer_reward":"0.000000000000000000","bonus_proposer_reward":"0.000000000000000000","community_tax":"0.020000000000000000","withdraw_addr_enabled":true},"previous_proposer":"cosmosvalcons16vm0nx49eam4q0xasdnwdzsdl6ymgyjt757sgr","validator_accumulated_commissions":[{"accumulated":{"commission":[{"amount":"944.230000000000000000","denom":"stake"}]},"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"validator_current_rewards":[{"rewards":{"period":"2","rewards":[{"amount":"8498.070000000000000000","denom":"stake"}]},"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"validator_historical_rewards":[{"period":"1","rewards":{"cumulative_reward_ratio":[],"reference_count":2},"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"validator_slash_events":[]},"evidence":{"evidence":[]},"feegrant":{"allowances":[]},"genutil":{"gen_txs":[]},"gov":{"deposit_params":null,"deposits":[{"amount":[{"amount":"10010000","denom":"stake"}],"depositor":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","proposal_id":"1"}],"params":{"expedited_min_deposit":[{"amount":"50000000","denom":"stake"}],"expedited_threshold":"0.667000000000000000","expedited_voting_period":"86400s","max_deposit_period":"172800s","min_deposit":[{"amount":"10000000","denom":"stake"}],"min_initial_deposit_ratio":"0.000000000000000000","proposal_cancel_dest":"","proposal_cancel_ratio":"0.500000000000000000","quorum":"0.334000000000000000","threshold":"0.500000000000000000","veto_threshold":"0.334000000000000000","voting_period":"172800s"},"proposals":[{"deposit_end_time":"2023-02-22T11:11:52.776167376Z","expedited":false,"final_tally_result":{"abstain_count":"0","no_count":"0","no_with_veto_count":"0","yes_count":"0"},"id":"1","messages":[{"@type":"/cosmos.distribution.v1beta1.MsgCommunityPoolSpend","amount":[],"authority":"cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn","recipient":"cosmos1pnt5523etwtzv6mj7haryfw6w8h5tkcuhd99m8"}],"metadata":"ipfs://CID","proposer":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","status":"PROPOSAL_STATUS_VOTING_PERIOD","submit_time":"2023-02-20T11:11:52.776167376Z","summary":"test proposal","title":"test proposal","total_deposit":[{"amount":"10010000","denom":"stake"}],"voting_end_time":"2023-02-22T11:12:07.801161984Z","voting_start_time":"2023-02-20T11:12:07.801161984Z"}],"starting_proposal_id":"2","tally_params":null,"votes":[],"voting_params":null},"group":{"group_members":[],"group_policies":[],"group_policy_seq":"0","group_seq":"0","groups":[],"proposal_seq":"0","proposals":[],"votes":[]},"mint":{"minter":{"annual_provisions":"1300010905.175073197786747950","inflation":"0.130000967926594565"},"params":{"blocks_per_year":"6311520","goal_bonded":"0.670000000000000000","inflation_max":"0.200000000000000000","inflation_min":"0.070000000000000000","inflation_rate_change":"0.130000000000000000","mint_denom":"stake"}},"nft":{"classes":[],"entries":[]},"params":null,"slashing":{"missed_blocks":[{"address":"cosmosvalcons16vm0nx49eam4q0xasdnwdzsdl6ymgyjt757sgr","missed_blocks":[]}],"params":{"downtime_jail_duration":"600s","min_signed_per_window":"0.500000000000000000","signed_blocks_window":"100","slash_fraction_double_sign":"0.050000000000000000","slash_fraction_downtime":"0.010000000000000000"},"signing_infos":[{"address":"cosmosvalcons16vm0nx49eam4q0xasdnwdzsdl6ymgyjt757sgr","validator_signing_info":{"address":"cosmosvalcons16vm0nx49eam4q0xasdnwdzsdl6ymgyjt757sgr","index_offset":"46","jailed_until":"1970-01-01T00:00:00Z","missed_blocks_counter":"0","start_height":"0","tombstoned":false}}]},"staking":{"delegations":[{"delegator_address":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","shares":"1000000.000000000000000000","validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"exported":true,"last_total_power":"1","last_validator_powers":[{"address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp","power":"1"}],"params":{"bond_denom":"stake","historical_entries":10000,"max_entries":7,"max_validators":100,"min_commission_rate":"0.000000000000000000","unbonding_time":"1814400s"},"redelegations":[],"unbonding_delegations":[],"validators":[{"commission":{"commission_rates":{"max_change_rate":"0.010000000000000000","max_rate":"0.200000000000000000","rate":"0.100000000000000000"},"update_time":"2023-02-20T11:08:30.588307671Z"},"consensus_pubkey":{"@type":"/cosmos.crypto.ed25519.PubKey","key":"tMZonPQYoooG/xbFVhHg95pTLxx7aO43/qgHFxDagWM="},"delegator_shares":"1000000.000000000000000000","description":{"details":"","identity":"","moniker":"test","security_contact":"","website":""},"jailed":false,"min_self_delegation":"1","operator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp","status":"BOND_STATUS_BONDED","tokens":"1000000","unbonding_height":"0","unbonding_ids":[],"unbonding_on_hold_ref_count":"0","unbonding_time":"1970-01-01T00:00:00Z"}]},"upgrade":{},"vesting":{}},"consensus":{"validators":[{"address":"D336F99AA5CF77503CDD8366E68A0DFE89B4124B","pub_key":{"type":"tendermint/PubKeyEd25519","value":"tMZonPQYoooG/xbFVhHg95pTLxx7aO43/qgHFxDagWM="},"power":"1","name":"test"}],"params":{"block":{"max_bytes":"22020096","max_gas":"-1"},"evidence":{"max_age_num_blocks":"100000","max_age_duration":"172800000000000","max_bytes":"1048576"},"validator":{"pub_key_types":["ed25519"]},"version":{"app":"0"},"synchrony":{"precision":"0","message_delay":"0"},"feature":{"vote_extensions_enable_height":"0","pbts_enable_height":"0"}}}} \ No newline at end of file +{"app_name":"\u003cappd\u003e","app_version":"","genesis_time":"2023-02-20T11:08:30.588307671Z","chain_id":"demo","initial_height":48,"app_hash":"","app_state":{"auth":{"accounts":[{"@type":"/cosmos.auth.v1beta1.BaseAccount","account_number":"1","address":"cosmos1qmkksxlxqdslq6kkca25m4jn344nx29lytq8f9","pub_key":null,"sequence":"0"},{"@type":"/cosmos.auth.v1beta1.BaseAccount","account_number":"8","address":"cosmos1pnt5523etwtzv6mj7haryfw6w8h5tkcuhd99m8","pub_key":null,"sequence":"0"},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"4","address":"cosmos1fl48vsnmsdzcv85q5d2q4z5ajdha8yu34mf0eh","pub_key":null,"sequence":"0"},"name":"bonded_tokens_pool","permissions":["burner","staking"]},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"5","address":"cosmos1tygms3xhhs3yv487phx3dw4a95jn7t7lpm470r","pub_key":null,"sequence":"0"},"name":"not_bonded_tokens_pool","permissions":["burner","staking"]},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"6","address":"cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn","pub_key":null,"sequence":"0"},"name":"gov","permissions":["burner"]},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"3","address":"cosmos1jv65s3grqf6v6jl3dp4t6c9t9rk99cd88lyufl","pub_key":null,"sequence":"0"},"name":"distribution","permissions":[]},{"@type":"/cosmos.auth.v1beta1.BaseAccount","account_number":"0","address":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","pub_key":{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"A3uyZdXedyvYx9VCL6xRjkxtcFpgxjhXFIz9b2mWz+aV"},"sequence":"4"},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"7","address":"cosmos1m3h30wlvsf8llruxtpukdvsy0km2kum8g38c8q","pub_key":null,"sequence":"0"},"name":"mint","permissions":["minter"]},{"@type":"/cosmos.auth.v1beta1.ModuleAccount","base_account":{"account_number":"2","address":"cosmos17xpfvakm2amg962yls6f84z3kell8c5lserqta","pub_key":null,"sequence":"0"},"name":"fee_collector","permissions":[]}],"params":{"max_memo_characters":"256","sig_verify_cost_ed25519":"590","sig_verify_cost_secp256k1":"1000","tx_sig_limit":"7","tx_size_cost_per_byte":"10"}},"authz":{"authorization":[]},"bank":{"balances":[{"address":"cosmos1qmkksxlxqdslq6kkca25m4jn344nx29lytq8f9","coins":[{"amount":"5000000000","denom":"stake"}]},{"address":"cosmos1pnt5523etwtzv6mj7haryfw6w8h5tkcuhd99m8","coins":[{"amount":"1000","denom":"stake"}]},{"address":"cosmos1fl48vsnmsdzcv85q5d2q4z5ajdha8yu34mf0eh","coins":[{"amount":"1000000","denom":"stake"}]},{"address":"cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn","coins":[{"amount":"10010000","denom":"stake"}]},{"address":"cosmos1jv65s3grqf6v6jl3dp4t6c9t9rk99cd88lyufl","coins":[{"amount":"9635","denom":"stake"}]},{"address":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","coins":[{"amount":"4988989000","denom":"stake"}]}],"denom_metadata":[],"params":{"default_send_enabled":true,"send_enabled":[]},"send_enabled":[],"supply":[{"amount":"10000009635","denom":"stake"}]},"consensus":null,"crisis":{"constant_fee":{"amount":"1000","denom":"stake"}},"distribution":{"delegator_starting_infos":[{"delegator_address":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","starting_info":{"height":"0","previous_period":"1","stake":"1000000.000000000000000000"},"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"delegator_withdraw_infos":[],"fee_pool":{"community_pool":[{"amount":"192.700000000000000000","denom":"stake"}]},"outstanding_rewards":[{"outstanding_rewards":[{"amount":"9442.300000000000000000","denom":"stake"}],"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"params":{"base_proposer_reward":"0.000000000000000000","bonus_proposer_reward":"0.000000000000000000","community_tax":"0.020000000000000000","withdraw_addr_enabled":true},"previous_proposer":"cosmosvalcons16vm0nx49eam4q0xasdnwdzsdl6ymgyjt757sgr","validator_accumulated_commissions":[{"accumulated":{"commission":[{"amount":"944.230000000000000000","denom":"stake"}]},"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"validator_current_rewards":[{"rewards":{"period":"2","rewards":[{"amount":"8498.070000000000000000","denom":"stake"}]},"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"validator_historical_rewards":[{"period":"1","rewards":{"cumulative_reward_ratio":[],"reference_count":2},"validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"validator_slash_events":[]},"evidence":{"evidence":[]},"feegrant":{"allowances":[]},"genutil":{"gen_txs":[]},"gov":{"deposit_params":null,"deposits":[{"amount":[{"amount":"10010000","denom":"stake"}],"depositor":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","proposal_id":"1"}],"params":{"expedited_min_deposit":[{"amount":"50000000","denom":"stake"}],"expedited_threshold":"0.667000000000000000","expedited_voting_period":"86400s","max_deposit_period":"172800s","min_deposit":[{"amount":"10000000","denom":"stake"}],"min_initial_deposit_ratio":"0.000000000000000000","proposal_cancel_dest":"","proposal_cancel_ratio":"0.500000000000000000","quorum":"0.334000000000000000","threshold":"0.500000000000000000","veto_threshold":"0.334000000000000000","voting_period":"172800s"},"proposals":[{"deposit_end_time":"2023-02-22T11:11:52.776167376Z","expedited":false,"final_tally_result":{"abstain_count":"0","no_count":"0","no_with_veto_count":"0","yes_count":"0"},"id":"1","messages":[{"@type":"/cosmos.distribution.v1beta1.MsgCommunityPoolSpend","amount":[],"authority":"cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn","recipient":"cosmos1pnt5523etwtzv6mj7haryfw6w8h5tkcuhd99m8"}],"metadata":"ipfs://CID","proposer":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","status":"PROPOSAL_STATUS_VOTING_PERIOD","submit_time":"2023-02-20T11:11:52.776167376Z","summary":"test proposal","title":"test proposal","total_deposit":[{"amount":"10010000","denom":"stake"}],"voting_end_time":"2023-02-22T11:12:07.801161984Z","voting_start_time":"2023-02-20T11:12:07.801161984Z"}],"starting_proposal_id":"2","tally_params":null,"votes":[],"voting_params":null},"group":{"group_members":[],"group_policies":[],"group_policy_seq":"0","group_seq":"0","groups":[],"proposal_seq":"0","proposals":[],"votes":[]},"mint":{"minter":{"annual_provisions":"1300010905.175073197786747950","inflation":"0.130000967926594565"},"params":{"blocks_per_year":"6311520","goal_bonded":"0.670000000000000000","inflation_max":"0.200000000000000000","inflation_min":"0.070000000000000000","inflation_rate_change":"0.130000000000000000","mint_denom":"stake"}},"nft":{"classes":[],"entries":[]},"params":null,"slashing":{"missed_blocks":[{"address":"cosmosvalcons16vm0nx49eam4q0xasdnwdzsdl6ymgyjt757sgr","missed_blocks":[]}],"params":{"downtime_jail_duration":"600s","min_signed_per_window":"0.500000000000000000","signed_blocks_window":"100","slash_fraction_double_sign":"0.050000000000000000","slash_fraction_downtime":"0.010000000000000000"},"signing_infos":[{"address":"cosmosvalcons16vm0nx49eam4q0xasdnwdzsdl6ymgyjt757sgr","validator_signing_info":{"address":"cosmosvalcons16vm0nx49eam4q0xasdnwdzsdl6ymgyjt757sgr","index_offset":"46","jailed_until":"1970-01-01T00:00:00Z","missed_blocks_counter":"0","start_height":"0","tombstoned":false}}]},"staking":{"delegations":[{"delegator_address":"cosmos15jenkldw6348lpgdev3vjzw90zzknxa9a3vg0j","shares":"1000000.000000000000000000","validator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp"}],"exported":true,"last_total_power":"1","last_validator_powers":[{"address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp","power":"1"}],"params":{"bond_denom":"stake","historical_entries":10000,"max_entries":7,"max_validators":100,"min_commission_rate":"0.000000000000000000","unbonding_time":"1814400s"},"redelegations":[],"unbonding_delegations":[],"validators":[{"commission":{"commission_rates":{"max_change_rate":"0.010000000000000000","max_rate":"0.200000000000000000","rate":"0.100000000000000000"},"update_time":"2023-02-20T11:08:30.588307671Z"},"consensus_pubkey":{"@type":"/cosmos.crypto.ed25519.PubKey","key":"tMZonPQYoooG/xbFVhHg95pTLxx7aO43/qgHFxDagWM="},"delegator_shares":"1000000.000000000000000000","description":{"details":"","identity":"","moniker":"test","security_contact":"","website":""},"jailed":false,"min_self_delegation":"1","operator_address":"cosmosvaloper15jenkldw6348lpgdev3vjzw90zzknxa9c9carp","status":"BOND_STATUS_BONDED","tokens":"1000000","unbonding_height":"0","unbonding_ids":[],"unbonding_on_hold_ref_count":"0","unbonding_time":"1970-01-01T00:00:00Z"}]},"upgrade":{},"vesting":{}},"consensus":{"validators":[{"address":"cosmosvalcons16vm0nx49eam4q0xasdnwdzsdl6ymgyjt757sgr","pub_key":{"type":"tendermint/PubKeyEd25519","value":"tMZonPQYoooG/xbFVhHg95pTLxx7aO43/qgHFxDagWM="},"power":"1","name":"test"}],"params":{"block":{"max_bytes":"22020096","max_gas":"-1"},"evidence":{"max_age_num_blocks":"100000","max_age_duration":"172800000000000","max_bytes":"1048576"},"validator":{"pub_key_types":["ed25519"]},"version":{"app":"0"},"synchrony":{"precision":"0","message_delay":"0"},"feature":{"vote_extensions_enable_height":"0","pbts_enable_height":"0"}}}} \ No newline at end of file diff --git a/x/genutil/utils.go b/x/genutil/utils.go index b20b7bfa6ec8..ee7d11e39e80 100644 --- a/x/genutil/utils.go +++ b/x/genutil/utils.go @@ -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" ) @@ -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 diff --git a/x/genutil/v2/cli/export.go b/x/genutil/v2/cli/export.go index 318ad58d8a47..9b0d992bad0a 100644 --- a/x/genutil/v2/cli/export.go +++ b/x/genutil/v2/cli/export.go @@ -76,6 +76,7 @@ func ExportCmd(appExporter v2.AppExporter) *cobra.Command { appGenesis.AppState = exported.AppState appGenesis.InitialHeight = exported.Height + appGenesis.Consensus.Validators = exported.Validators out, err := json.Marshal(appGenesis) if err != nil { diff --git a/x/genutil/v2/types.go b/x/genutil/v2/types.go index 3199a5a5afb4..fbf288365a57 100644 --- a/x/genutil/v2/types.go +++ b/x/genutil/v2/types.go @@ -3,6 +3,8 @@ package v2 import ( "context" "encoding/json" + + sdk "github.com/cosmos/cosmos-sdk/types" ) // AppExporter is a function that dumps all app state to @@ -20,4 +22,6 @@ type ExportedApp struct { AppState json.RawMessage // Height is the app's latest block height. Height int64 + // Validators is the exported validator set. + Validators []sdk.GenesisValidator } diff --git a/x/staking/genesis.go b/x/staking/genesis.go index d09ada842f44..938dc4666569 100644 --- a/x/staking/genesis.go +++ b/x/staking/genesis.go @@ -9,23 +9,12 @@ import ( "cosmossdk.io/x/staking/keeper" "cosmossdk.io/x/staking/types" - cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" + cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" 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 { @@ -37,10 +26,14 @@ func WriteValidators(ctx context.Context, keeper *keeper.Keeper) (vals []Genesis returnErr = err return true, err } + jsonPk, err := cryptocodec.PubKeyFromProto(pk) + if err != nil { + return true, err + } - vals = append(vals, GenesisValidator{ - Address: sdk.ConsAddress(pk.Address()), - PubKey: pk, + vals = append(vals, sdk.GenesisValidator{ + Address: pk.Address().Bytes(), + PubKey: jsonPk, Power: validator.GetConsensusPower(keeper.PowerReduction(ctx)), Name: validator.GetMoniker(), })