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 4 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
774 changes: 742 additions & 32 deletions api/cosmos/genutil/v1beta1/genesis.pulsar.go

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions proto/cosmos/genutil/v1beta1/genesis.proto
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
syntax = "proto3";
package cosmos.genutil.v1beta1;

import "google/protobuf/any.proto";
import "gogoproto/gogo.proto";
import "amino/amino.proto";
import "cosmos_proto/cosmos.proto";

option go_package = "github.com/cosmos/cosmos-sdk/x/genutil/types";

Expand All @@ -16,3 +18,18 @@ message GenesisState {
(amino.dont_omitempty) = true
];
}

// GenesisValidator defines an initial validator.
message GenesisValidator {
option (gogoproto.equal) = false;
option (gogoproto.goproto_getters) = false;

// address defines the validator address.
string address = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];
// pub_key defines the validator public key.
google.protobuf.Any consensus_pubkey = 2 [(cosmos_proto.accepts_interface) = "cosmos.crypto.PubKey"];
// power defines the validator voting power.
int64 voting_power = 3;
// name defines the validator name.
string name = 4;
}
38 changes: 11 additions & 27 deletions server/export.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
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 (
Expand Down Expand Up @@ -73,33 +73,17 @@ func ExportCmd(appExporter types.AppExporter, defaultNodeHome string) *cobra.Com
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 = appGenesis.ConsensusParams
Fixed Show fixed Hide fixed

// 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)
encoded, err := json.Marshal(appGenesis)
if err != nil {
return err
}
Expand All @@ -113,11 +97,11 @@ func ExportCmd(appExporter types.AppExporter, defaultNodeHome string) *cobra.Com
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 Down
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
3 changes: 2 additions & 1 deletion server/mock/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/cosmos/cosmos-sdk/x/auth/signing"

"cosmossdk.io/errors"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand Down Expand Up @@ -126,7 +127,7 @@ func decodeTx(txBytes []byte) (sdk.Tx, error) {
k, v := split[0], split[1]
tx = &KVStoreTx{k, v, txBytes, nil}
} else {
return nil, sdkerrors.Wrap(sdkerrors.ErrTxDecode, "too many '='")
return nil, errors.Wrap(sdkerrors.ErrTxDecode, "too many '='")
}

return tx, nil
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
9 changes: 4 additions & 5 deletions simapp/simd/cmd/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

cmtconfig "github.com/cometbft/cometbft/config"
cmtrand "github.com/cometbft/cometbft/libs/rand"
"github.com/cometbft/cometbft/types"
cmttime "github.com/cometbft/cometbft/types/time"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -392,15 +391,15 @@ func initGenFiles(
return err
}

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

// 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 @@ -426,12 +425,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
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
5 changes: 3 additions & 2 deletions testutil/sims/address_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import (
"fmt"
"strconv"

"cosmossdk.io/errors"
"cosmossdk.io/math"
"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 @@ -154,7 +155,7 @@ func NewPubKeyFromHex(pk string) (res cryptotypes.PubKey) {
panic(err)
}
if len(pkBytes) != ed25519.PubKeySize {
panic(errors.Wrap(errors.ErrInvalidPubKey, "invalid pubkey size"))
panic(errors.Wrap(sdkerrors.ErrInvalidPubKey, "invalid pubkey size"))
}
return &ed25519.PubKey{Key: pkBytes}
}
10 changes: 4 additions & 6 deletions testutil/sims/state_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"time"

"cosmossdk.io/math"
cmtjson "github.com/cometbft/cometbft/libs/json"
cmttypes "github.com/cometbft/cometbft/types"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
Expand All @@ -19,6 +17,7 @@ import (
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
simcli "github.com/cosmos/cosmos-sdk/x/simulation/client/cli"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)
Expand Down Expand Up @@ -211,15 +210,14 @@ func AppStateRandomizedFn(

// AppStateFromGenesisFileFn util function to generate the genesis AppState
// from a genesis.json file.
func AppStateFromGenesisFileFn(r io.Reader, cdc codec.JSONCodec, genesisFile string) (cmttypes.GenesisDoc, []simtypes.Account, error) {
func AppStateFromGenesisFileFn(r io.Reader, cdc codec.JSONCodec, genesisFile string) (genutiltypes.AppGenesis, []simtypes.Account, error) {
bytes, err := os.ReadFile(genesisFile)
if err != nil {
panic(err)
}

var genesis cmttypes.GenesisDoc
// NOTE: CometBFT uses a custom JSON decoder for GenesisDoc
if err = cmtjson.Unmarshal(bytes, &genesis); err != nil {
var genesis genutiltypes.AppGenesis
if err = json.Unmarshal(bytes, &genesis); err != nil {
return genesis, nil, err
}

Expand Down
Loading