Skip to content

Commit

Permalink
refactor(genutil): Use sdk types genesis validator (#21678)
Browse files Browse the repository at this point in the history
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: Matt Kocubinski <mkocubinski@gmail.com>
  • Loading branch information
3 people authored and julienrbrt committed Oct 9, 2024
1 parent 1780d7d commit d727f9b
Show file tree
Hide file tree
Showing 19 changed files with 459 additions and 66 deletions.
53 changes: 53 additions & 0 deletions crypto/codec/pubkey.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
41 changes: 41 additions & 0 deletions crypto/keys/jsonkey.go
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 2 additions & 2 deletions server/types/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 (
Expand Down Expand Up @@ -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.
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
13 changes: 9 additions & 4 deletions simapp/v2/app_di.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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() {
Expand Down Expand Up @@ -161,6 +165,7 @@ func NewSimApp[T transaction.Tx](
&app.txConfig,
&app.interfaceRegistry,
&app.UpgradeKeeper,
&app.StakingKeeper,
); err != nil {
panic(err)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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
}
5 changes: 2 additions & 3 deletions simapp/v2/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down
35 changes: 27 additions & 8 deletions simapp/v2/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
3 changes: 2 additions & 1 deletion tests/e2e/genutil/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{},
},
}

Expand Down
9 changes: 9 additions & 0 deletions types/staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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"`
}
23 changes: 19 additions & 4 deletions x/genutil/client/cli/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit d727f9b

Please sign in to comment.