Skip to content

Commit

Permalink
feat: allow for the default consensus params to be set by the applica…
Browse files Browse the repository at this point in the history
…tion (#317)

* feat: allow for the default consensus params to be set by the application

* chore: add test

* fix: spelling

Co-authored-by: CHAMI Rachid <chamirachid1@gmail.com>

---------

Co-authored-by: CHAMI Rachid <chamirachid1@gmail.com>
  • Loading branch information
evan-forbes and rach-id committed May 12, 2023
1 parent 06c2fc2 commit 7d7c38d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
10 changes: 6 additions & 4 deletions server/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/cosmos/cosmos-sdk/store"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/version"
cmtproto "github.com/tendermint/tendermint/proto/tendermint/types"
)

// DONTCOVER
Expand All @@ -43,9 +44,10 @@ const ServerContextKey = sdk.ContextKey("server.context")

// server context
type Context struct {
Viper *viper.Viper
Config *tmcfg.Config
Logger tmlog.Logger
Viper *viper.Viper
Config *tmcfg.Config
Logger tmlog.Logger
DefaultConsensusParams *cmtproto.ConsensusParams
}

// ErrorCode contains the exit code for server exit.
Expand All @@ -66,7 +68,7 @@ func NewDefaultContext() *Context {
}

func NewContext(v *viper.Viper, config *tmcfg.Config, logger tmlog.Logger) *Context {
return &Context{v, config, logger}
return &Context{v, config, logger, nil}
}

func bindFlags(basename string, cmd *cobra.Command, v *viper.Viper) (err error) {
Expand Down
4 changes: 4 additions & 0 deletions x/genutil/client/cli/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ func InitCmd(mbm module.BasicManager, defaultNodeHome string) *cobra.Command {
genDoc.Validators = nil
genDoc.AppState = appState

if serverCtx.DefaultConsensusParams != nil {
genDoc.ConsensusParams = serverCtx.DefaultConsensusParams
}

if err = genutil.ExportGenesisFile(genDoc, genFile); err != nil {
return errors.Wrap(err, "Failed to export genesis file")
}
Expand Down
38 changes: 38 additions & 0 deletions x/genutil/client/cli/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
abci_server "github.com/tendermint/tendermint/abci/server"
"github.com/tendermint/tendermint/libs/cli"
"github.com/tendermint/tendermint/libs/log"
coretypes "github.com/tendermint/tendermint/types"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
Expand Down Expand Up @@ -283,6 +284,43 @@ func TestInitConfig(t *testing.T) {
require.Contains(t, out, "\"chain_id\": \"foo\"")
}

func TestInitWithConsensusParams(t *testing.T) {
home := t.TempDir()
logger := log.NewNopLogger()
cfg, err := genutiltest.CreateDefaultTendermintConfig(home)
require.NoError(t, err)

serverCtx := server.NewContext(viper.New(), cfg, logger)

// set new default consensus params
cps := coretypes.DefaultConsensusParams()
cps.Block.MaxBytes = 100000000
cps.Block.MaxGas = 420420420
serverCtx.DefaultConsensusParams = cps

interfaceRegistry := types.NewInterfaceRegistry()
marshaler := codec.NewProtoCodec(interfaceRegistry)
clientCtx := client.Context{}.
WithCodec(marshaler).
WithLegacyAmino(makeCodec()).
WithHomeDir(home)

ctx := context.Background()
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
ctx = context.WithValue(ctx, server.ServerContextKey, serverCtx)

cmd := genutilcli.InitCmd(testMbm, home)
cmd.SetArgs([]string{"testnode"})

err = cmd.ExecuteContext(ctx)
require.NoError(t, err)

genDoc, err := coretypes.GenesisDocFromFile(cfg.GenesisFile())
require.NoError(t, err)

require.Equal(t, genDoc.ConsensusParams, cps)
}

// custom tx codec
func makeCodec() *codec.LegacyAmino {
cdc := codec.NewLegacyAmino()
Expand Down

0 comments on commit 7d7c38d

Please sign in to comment.