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

feat: add application genesis #15031

Merged
merged 35 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
f45336f
feat: application genesis
julienrbrt Feb 14, 2023
6f6a96d
updates
julienrbrt Feb 14, 2023
77688ce
wip
julienrbrt Feb 16, 2023
0c520dd
Merge branch 'main' into julien/app-genesis
julienrbrt Feb 16, 2023
95db1bc
updates
julienrbrt Feb 16, 2023
c598670
revert sdk genesis validator
julienrbrt Feb 16, 2023
96e1e3d
updates
julienrbrt Feb 16, 2023
5bdb70d
updates
julienrbrt Feb 16, 2023
3faf425
updates
julienrbrt Feb 16, 2023
39fcba8
updates
julienrbrt Feb 16, 2023
1ae07d5
more revert
julienrbrt Feb 17, 2023
f925b2f
Merge branch 'main' into julien/app-genesis
julienrbrt Feb 17, 2023
5bd8602
updates
julienrbrt Feb 17, 2023
0b66aab
updates
julienrbrt Feb 17, 2023
3360f15
updates
julienrbrt Feb 17, 2023
e188498
updates
julienrbrt Feb 17, 2023
bb242b9
Merge branch 'main' into julien/app-genesis
julienrbrt Feb 18, 2023
cb820c5
Merge branch 'main' into julien/app-genesis
julienrbrt Feb 19, 2023
5407f9d
Merge branch 'main' into julien/app-genesis
julienrbrt Feb 19, 2023
7d4cb65
feedback and fix
julienrbrt Feb 20, 2023
9f83600
better docs
julienrbrt Feb 20, 2023
eb83d6a
add tests
julienrbrt Feb 20, 2023
2f99ca0
Merge branch 'main' into julien/app-genesis
julienrbrt Feb 20, 2023
db55093
Merge branch 'main' into julien/app-genesis
julienrbrt Feb 20, 2023
7f20c2c
Merge branch 'main' into julien/app-genesis
julienrbrt Feb 21, 2023
84394a7
fix merge
julienrbrt Feb 21, 2023
eec5220
updates
julienrbrt Feb 21, 2023
618e845
Merge branch 'main' into julien/app-genesis
julienrbrt Feb 21, 2023
4f6d24f
fix test and have better split logic
julienrbrt Feb 21, 2023
2c72a0f
updates
julienrbrt Feb 21, 2023
1fe6189
remove e2e genutil
julienrbrt Feb 21, 2023
6a51882
Merge branch 'main' into julien/app-genesis
julienrbrt Feb 21, 2023
92c23e9
Merge branch 'main' into julien/app-genesis
julienrbrt Feb 22, 2023
2c767bf
typo
julienrbrt Feb 22, 2023
8957f0f
Merge branch 'main' into julien/app-genesis
julienrbrt Feb 22, 2023
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Features

* (cli) [#14659](https://github.com/cosmos/cosmos-sdk/pull/14659) Added ability to query blocks by events with queries directly passed to Tendermint, which will allow for full query operator support, e.g. `>`.
* (x/genutil) [#15301](https://github.com/cosmos/cosmos-sdk/pull/15031) Add application genesis. The genesis is now entirely managed by the application and passed to CometBFT at note instantiation. Functions that were taking a `cmttypes.GenesisDoc{}` now takes a `genutiltypes.AppGenesis{}`.
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
* (cli) [#14659](https://github.com/cosmos/cosmos-sdk/pull/14659) Added ability to query blocks by events with queries directly passed to Tendermint, which will allow for full query operator support, e.g. `>`.
* [#14897](https://github.com/cosmos/cosmos-sdk/pull/14897) Migrate the Cosmos SDK to CometBFT.
* (x/gov) [#14720](https://github.com/cosmos/cosmos-sdk/pull/14720) Upstream expedited proposals from Osmosis.
* (x/auth) [#14650](https://github.com/cosmos/cosmos-sdk/pull/14650) Add Textual SignModeHandler. It is however **NOT** enabled by default, and should only be used for **TESTING** purposes until `SIGN_MODE_TEXTUAL` is fully released.
Expand Down
2 changes: 1 addition & 1 deletion proto/cosmos/genutil/v1beta1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ message GenesisState {
(amino.field_name) = "gentxs",
(amino.dont_omitempty) = true
];
}
}
45 changes: 13 additions & 32 deletions server/export.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
package server

import (
"encoding/json"
"fmt"
"os"

cmtjson "github.com/cometbft/cometbft/libs/json"
cmttypes "github.com/cometbft/cometbft/types"
"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/server/types"
sdk "github.com/cosmos/cosmos-sdk/types"
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
)

const (
FlagHeight = "height"
FlagForZeroHeight = "for-zero-height"
FlagJailAllowedAddrs = "jail-allowed-addrs"
FlagModulesToExport = "modules-to-export"
FlagOutputDocument = "output-document"
)

// ExportCmd dumps app state to JSON.
Expand Down Expand Up @@ -66,58 +64,41 @@ func ExportCmd(appExporter types.AppExporter, defaultNodeHome string) *cobra.Com
forZeroHeight, _ := cmd.Flags().GetBool(FlagForZeroHeight)
jailAllowedAddrs, _ := cmd.Flags().GetStringSlice(FlagJailAllowedAddrs)
modulesToExport, _ := cmd.Flags().GetStringSlice(FlagModulesToExport)
outputDocument, _ := cmd.Flags().GetString(FlagOutputDocument)
outputDocument, _ := cmd.Flags().GetString(flags.FlagOutputDocument)

exported, err := appExporter(serverCtx.Logger, db, traceWriter, height, forZeroHeight, jailAllowedAddrs, serverCtx.Viper, modulesToExport)
if err != nil {
return fmt.Errorf("error exporting state: %v", err)
}

doc, err := cmttypes.GenesisDocFromFile(serverCtx.Config.GenesisFile())
appGenesis, err := genutiltypes.AppGenesisFromFile(serverCtx.Config.GenesisFile())
if err != nil {
return err
}

doc.AppState = exported.AppState
doc.Validators = exported.Validators
doc.InitialHeight = exported.Height
doc.ConsensusParams = &cmttypes.ConsensusParams{
Block: cmttypes.BlockParams{
MaxBytes: exported.ConsensusParams.Block.MaxBytes,
MaxGas: exported.ConsensusParams.Block.MaxGas,
},
Evidence: cmttypes.EvidenceParams{
MaxAgeNumBlocks: exported.ConsensusParams.Evidence.MaxAgeNumBlocks,
MaxAgeDuration: exported.ConsensusParams.Evidence.MaxAgeDuration,
MaxBytes: exported.ConsensusParams.Evidence.MaxBytes,
},
Validator: cmttypes.ValidatorParams{
PubKeyTypes: exported.ConsensusParams.Validator.PubKeyTypes,
},
}
appGenesis.AppState = exported.AppState
appGenesis.Validators = exported.Validators
appGenesis.InitialHeight = exported.Height
appGenesis.ConsensusParams = exported.ConsensusParams

// NOTE: CometBFT uses a custom JSON decoder for GenesisDoc
// (except for stuff inside AppState). Inside AppState, we're free
// to encode as protobuf or amino.
encoded, err := cmtjson.Marshal(doc)
out, err := json.Marshal(appGenesis)
if err != nil {
return err
}

cmd.SetOut(cmd.OutOrStdout())
cmd.SetErr(cmd.OutOrStderr())
out := sdk.MustSortJSON(encoded)

if outputDocument == "" {
cmd.Println(string(out))
return nil
}

var exportedGenDoc cmttypes.GenesisDoc
if err = cmtjson.Unmarshal(out, &exportedGenDoc); err != nil {
var exportedAppGenesis genutiltypes.AppGenesis
if err = json.Unmarshal(out, &exportedAppGenesis); err != nil {
return err
}
if err = exportedGenDoc.SaveAs(outputDocument); err != nil {
if err = exportedAppGenesis.SaveAs(outputDocument); err != nil {
return err
}

Expand All @@ -130,7 +111,7 @@ func ExportCmd(appExporter types.AppExporter, defaultNodeHome string) *cobra.Com
cmd.Flags().Bool(FlagForZeroHeight, false, "Export state to start at height zero (perform preproccessing)")
cmd.Flags().StringSlice(FlagJailAllowedAddrs, []string{}, "Comma-separated list of operator addresses of jailed validators to unjail")
cmd.Flags().StringSlice(FlagModulesToExport, []string{}, "Comma-separated list of modules to export. If empty, will export all modules")
cmd.Flags().String(FlagOutputDocument, "", "Exported state is written to the given file instead of STDOUT")
cmd.Flags().String(flags.FlagOutputDocument, "", "Exported state is written to the given file instead of STDOUT")

return cmd
}
6 changes: 3 additions & 3 deletions server/mock/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

abci "github.com/cometbft/cometbft/abci/types"
"github.com/cometbft/cometbft/libs/log"
"github.com/cometbft/cometbft/types"
db "github.com/cosmos/cosmos-db"
"google.golang.org/grpc"

Expand All @@ -19,6 +18,7 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
)

// NewApp creates a simple mock kvstore app for testing. It should work
Expand Down Expand Up @@ -116,7 +116,7 @@ func InitChainer(key storetypes.StoreKey) func(sdk.Context, abci.RequestInitChai

// AppGenState can be passed into InitCmd, returns a static string of a few
// key-values that can be parsed by InitChainer
func AppGenState(_ *codec.LegacyAmino, _ types.GenesisDoc, _ []json.RawMessage) (appState json.RawMessage, err error) {
func AppGenState(_ *codec.LegacyAmino, _ genutiltypes.AppGenesis, _ []json.RawMessage) (appState json.RawMessage, err error) {
appState = json.RawMessage(`{
"values": [
{
Expand All @@ -133,7 +133,7 @@ func AppGenState(_ *codec.LegacyAmino, _ types.GenesisDoc, _ []json.RawMessage)
}

// AppGenStateEmpty returns an empty transaction state for mocking.
func AppGenStateEmpty(_ *codec.LegacyAmino, _ types.GenesisDoc, _ []json.RawMessage) (appState json.RawMessage, err error) {
func AppGenStateEmpty(_ *codec.LegacyAmino, _ genutiltypes.AppGenesis, _ []json.RawMessage) (appState json.RawMessage, err error) {
appState = json.RawMessage(``)
return
}
Expand Down
4 changes: 2 additions & 2 deletions server/mock/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (

abci "github.com/cometbft/cometbft/abci/types"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/cometbft/cometbft/types"
"github.com/stretchr/testify/require"

simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
)

func TestInitApp(t *testing.T) {
Expand All @@ -21,7 +21,7 @@ func TestInitApp(t *testing.T) {
}
require.NoError(t, err)

appState, err := AppGenState(nil, types.GenesisDoc{}, nil)
appState, err := AppGenState(nil, genutiltypes.AppGenesis{}, nil)
require.NoError(t, err)

req := abci.RequestInitChain{
Expand Down
15 changes: 12 additions & 3 deletions server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import (
"time"

"github.com/cometbft/cometbft/abci/server"
tcmd "github.com/cometbft/cometbft/cmd/cometbft/commands"
cmtcmd "github.com/cometbft/cometbft/cmd/cometbft/commands"
"github.com/cometbft/cometbft/node"
"github.com/cometbft/cometbft/p2p"
pvm "github.com/cometbft/cometbft/privval"
"github.com/cometbft/cometbft/proxy"
"github.com/cometbft/cometbft/rpc/client/local"
cmttypes "github.com/cometbft/cometbft/types"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"google.golang.org/grpc"
Expand All @@ -30,6 +31,7 @@ import (
"github.com/cosmos/cosmos-sdk/server/types"
"github.com/cosmos/cosmos-sdk/telemetry"
"github.com/cosmos/cosmos-sdk/types/mempool"
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
)

const (
Expand Down Expand Up @@ -204,7 +206,7 @@ is performed. Note, when enabled, gRPC will also be automatically enabled.
})

// add support for all CometBFT-specific command line options
tcmd.AddNodeFlags(cmd)
cmtcmd.AddNodeFlags(cmd)
return cmd
}

Expand Down Expand Up @@ -301,7 +303,14 @@ func startInProcess(ctx *Context, clientCtx client.Context, appCreator types.App
if err != nil {
return err
}
genDocProvider := node.DefaultGenesisDocProviderFunc(cfg)
genDocProvider := func() (*cmttypes.GenesisDoc, error) {
appGenesis, err := genutiltypes.AppGenesisFromFile(cfg.GenesisFile())
if err != nil {
return nil, err
}

return appGenesis.ToCometBFTGenesisDoc()
}

var (
tmNode *node.Node
Expand Down
19 changes: 7 additions & 12 deletions simapp/simd/cmd/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import (
"os"
"path/filepath"

"cosmossdk.io/math"
"cosmossdk.io/math/unsafe"
cmtconfig "github.com/cometbft/cometbft/config"
"github.com/cometbft/cometbft/types"
cmttime "github.com/cometbft/cometbft/types/time"
"github.com/spf13/cobra"
"github.com/spf13/pflag"

"cosmossdk.io/math"
"cosmossdk.io/math/unsafe"
"cosmossdk.io/simapp"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
Expand Down Expand Up @@ -391,15 +391,10 @@ func initGenFiles(
return err
}

genDoc := types.GenesisDoc{
ChainID: chainID,
AppState: appGenStateJSON,
Validators: nil,
}

appGenesis := genutiltypes.NewAppGenesisWithVersion(chainID, appGenStateJSON)
// generate empty genesis files for each validator and save
for i := 0; i < numValidators; i++ {
if err := genDoc.SaveAs(genFiles[i]); err != nil {
if err := appGenesis.SaveAs(genFiles[i]); err != nil {
return err
}
}
Expand All @@ -425,12 +420,12 @@ func collectGenFiles(
nodeID, valPubKey := nodeIDs[i], valPubKeys[i]
initCfg := genutiltypes.NewInitConfig(chainID, gentxsDir, nodeID, valPubKey)

genDoc, err := types.GenesisDocFromFile(nodeConfig.GenesisFile())
appGenesis, err := genutiltypes.AppGenesisFromFile(nodeConfig.GenesisFile())
if err != nil {
return err
}

nodeAppState, err := genutil.GenAppStateFromConfig(clientCtx.Codec, clientCtx.TxConfig, nodeConfig, initCfg, *genDoc, genBalIterator, genutiltypes.DefaultMessageValidator)
nodeAppState, err := genutil.GenAppStateFromConfig(clientCtx.Codec, clientCtx.TxConfig, nodeConfig, initCfg, appGenesis, genBalIterator, genutiltypes.DefaultMessageValidator)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/staking/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func TestInitGenesis(t *testing.T) {
assert.NilError(t, err)

for _, val := range vals2 {
assert.Assert(t, val.Address.String() != "")
assert.Assert(t, val.Address != "")
}

// now make sure the validators are bonded and intra-tx counters are correct
Expand Down
21 changes: 14 additions & 7 deletions testutil/network/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
pvm "github.com/cometbft/cometbft/privval"
"github.com/cometbft/cometbft/proxy"
"github.com/cometbft/cometbft/rpc/client/local"
"github.com/cometbft/cometbft/types"
cmttypes "github.com/cometbft/cometbft/types"
cmttime "github.com/cometbft/cometbft/types/time"

"github.com/cosmos/cosmos-sdk/server/api"
Expand All @@ -40,14 +40,21 @@ func startInProcess(cfg Config, val *Validator) error {
}

app := cfg.AppConstructor(*val)
genDocProvider := node.DefaultGenesisDocProviderFunc(cmtCfg)
appGenesisProvider := func() (*cmttypes.GenesisDoc, error) {
appGenesis, err := genutiltypes.AppGenesisFromFile(cmtCfg.GenesisFile())
if err != nil {
return nil, err
}

return appGenesis.ToCometBFTGenesisDoc()
}

tmNode, err := node.NewNode( //resleak:notresource
cmtCfg,
pvm.LoadOrGenFilePV(cmtCfg.PrivValidatorKeyFile(), cmtCfg.PrivValidatorStateFile()),
nodeKey,
proxy.NewLocalClientCreator(app),
genDocProvider,
appGenesisProvider,
node.DefaultDBProvider,
node.DefaultMetricsProvider(cmtCfg.Instrumentation),
logger.With("module", val.Moniker),
Expand Down Expand Up @@ -123,13 +130,13 @@ func collectGenFiles(cfg Config, vals []*Validator, outputDir string) error {
initCfg := genutiltypes.NewInitConfig(cfg.ChainID, gentxsDir, vals[i].NodeID, vals[i].PubKey)

genFile := cmtCfg.GenesisFile()
genDoc, err := types.GenesisDocFromFile(genFile)
appGenesis, err := genutiltypes.AppGenesisFromFile(genFile)
if err != nil {
return err
}

appState, err := genutil.GenAppStateFromConfig(cfg.Codec, cfg.TxConfig,
cmtCfg, initCfg, *genDoc, banktypes.GenesisBalancesIterator{}, genutiltypes.DefaultMessageValidator)
cmtCfg, initCfg, appGenesis, banktypes.GenesisBalancesIterator{}, genutiltypes.DefaultMessageValidator)
if err != nil {
return err
}
Expand Down Expand Up @@ -168,15 +175,15 @@ func initGenFiles(cfg Config, genAccounts []authtypes.GenesisAccount, genBalance
return err
}

genDoc := types.GenesisDoc{
appGenesis := genutiltypes.AppGenesis{
ChainID: cfg.ChainID,
AppState: appGenStateJSON,
Validators: nil,
}

// generate empty genesis files for each validator and save
for i := 0; i < cfg.NumValidators; i++ {
if err := genDoc.SaveAs(genFiles[i]); err != nil {
if err := appGenesis.SaveAs(genFiles[i]); err != nil {
return err
}
}
Expand Down
4 changes: 2 additions & 2 deletions testutil/sims/address_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/errors"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
)
Expand Down Expand Up @@ -156,7 +156,7 @@ func NewPubKeyFromHex(pk string) (res cryptotypes.PubKey) {
panic(err)
}
if len(pkBytes) != ed25519.PubKeySize {
panic(errorsmod.Wrap(errors.ErrInvalidPubKey, "invalid pubkey size"))
panic(errorsmod.Wrap(sdkerrors.ErrInvalidPubKey, "invalid pubkey size"))
}
return &ed25519.PubKey{Key: pkBytes}
}
Loading