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: pass config to app when creating new app #16

Merged
merged 2 commits into from
Jan 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion app/ante/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ func NewApp(options ...func(baseApp *baseapp.BaseApp)) (*app.App, params.Encodin
encCfg := app.MakeEncodingConfig()

nApp := app.New(
logger, db, nil, true, map[int64]bool{}, app.DefaultNodeHome, 0, encCfg, simapp.EmptyAppOptions{}, options...)
logger, db, nil, true, map[int64]bool{}, app.DefaultNodeHome, 0, encCfg, app.NewDefaultAppConfig(), simapp.EmptyAppOptions{}, options...)

genesisState := app.NewDefaultGenesisState(encCfg.Marshaler)
genesisState, _ = genesisStateWithValSet(nApp, genesisState, valSet, []authtypes.GenesisAccount{acc}, balance)
Expand Down
1 change: 1 addition & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ func New(
homePath string,
invCheckPeriod uint,
encodingConfig appparams.EncodingConfig,
customAppConfig *AppConfig,
appOpts servertypes.AppOptions,
baseAppOptions ...func(*baseapp.BaseApp),
) *App {
Expand Down
51 changes: 51 additions & 0 deletions app/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package app

import serverconfig "github.com/cosmos/cosmos-sdk/server/config"

type AppConfig struct {
serverconfig.Config

CrossChain CrossChainConfig `mapstructure:"cross_chain"`
}

type CrossChainConfig struct {
SrcChainId uint32 `mapstructure:"src_chain_id"`

DestChainId uint32 `mapstructure:"dest_chain_id"`
}

var CustomAppTemplate = serverconfig.DefaultConfigTemplate + `
###############################################################################
### CrossChain Config ###
###############################################################################
[cross_chain]
# chain-id for current chain
src_chain_id = {{ .CrossChain.SrcChainId }}
# chain-id for destination chain(bsc)
dest_chain_id = {{ .CrossChain.DestChainId }}
`

func NewDefaultAppConfig() *AppConfig {
srvCfg := serverconfig.DefaultConfig()
// The SDK's default minimum gas price is set to "" (empty value) inside
// app.toml. If left empty by validators, the node will halt on startup.
// However, the chain developer can set a default app.toml value for their
// validators here.
//
// In summary:
// - if you leave srvCfg.MinGasPrices = "", all validators MUST tweak their
// own app.toml config,
// - if you set srvCfg.MinGasPrices non-empty, validators CAN tweak their
// own app.toml to override, or use this default value.
//
// In simapp, we set the min gas prices to 0.
srvCfg.MinGasPrices = "0stake"

return &AppConfig{
Config: *srvCfg,
CrossChain: CrossChainConfig{
SrcChainId: 1,
DestChainId: 2,
},
}
}
1 change: 1 addition & 0 deletions app/simulation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func BenchmarkSimulation(b *testing.B) {
app.DefaultNodeHome,
0,
encoding,
app.NewDefaultAppConfig(),
simapp.EmptyAppOptions{},
)

Expand Down
96 changes: 36 additions & 60 deletions cmd/bfsd/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/client/rpc"
"github.com/cosmos/cosmos-sdk/server"
serverconfig "github.com/cosmos/cosmos-sdk/server/config"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
"github.com/cosmos/cosmos-sdk/snapshots"
snapshottypes "github.com/cosmos/cosmos-sdk/snapshots/types"
Expand All @@ -30,6 +29,7 @@ import (
"github.com/spf13/cast"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
tmcfg "github.com/tendermint/tendermint/config"
tmcli "github.com/tendermint/tendermint/libs/cli"
"github.com/tendermint/tendermint/libs/log"
Expand All @@ -41,6 +41,10 @@ import (
"github.com/bnb-chain/bfs/version"
)

var (
appConfig *app.AppConfig = app.NewDefaultAppConfig()
)

// NewRootCmd creates a new root command for a Cosmos SDK application
func NewRootCmd() (*cobra.Command, appparams.EncodingConfig) {
encodingConfig := app.MakeEncodingConfig()
Expand Down Expand Up @@ -75,11 +79,15 @@ func NewRootCmd() (*cobra.Command, appparams.EncodingConfig) {
return err
}

customAppTemplate, customAppConfig := initAppConfig()
customTMConfig := initTendermintConfig()
return server.InterceptConfigsPreRunHandler(
cmd, customAppTemplate, customAppConfig, customTMConfig,
err = server.InterceptConfigsPreRunHandler(
cmd, app.CustomAppTemplate, app.NewDefaultAppConfig(), customTMConfig,
)
if err != nil {
return err
}

return ParseAppConfigInPlace()
},
}

Expand All @@ -93,6 +101,28 @@ func NewRootCmd() (*cobra.Command, appparams.EncodingConfig) {
return rootCmd, encodingConfig
}

func ParseAppConfigInPlace() error {
homeDir := viper.GetString(flags.FlagHome)

tmpViper := viper.New()
tmpViper.SetConfigName("app")
tmpViper.SetConfigType("toml")
tmpViper.AddConfigPath(homeDir)
tmpViper.AddConfigPath(filepath.Join(homeDir, "config"))

// If a config file is found, read it in.
if err := tmpViper.ReadInConfig(); err != nil {
return err
}

appConfig = app.NewDefaultAppConfig()
err := tmpViper.Unmarshal(appConfig)
if err != nil {
return err
}
return nil
}

// initTendermintConfig helps to override default Tendermint Config values.
// return tmcfg.DefaultConfig if no custom configuration is required for the application.
func initTendermintConfig() *tmcfg.Config {
Expand Down Expand Up @@ -323,6 +353,7 @@ func (a appCreator) newApp(
cast.ToString(appOpts.Get(flags.FlagHome)),
cast.ToUint(appOpts.Get(server.FlagInvCheckPeriod)),
a.encodingConfig,
appConfig,
appOpts,
baseapp.SetPruning(pruningOpts),
baseapp.SetMinGasPrices(cast.ToString(appOpts.Get(server.FlagMinGasPrices))),
Expand Down Expand Up @@ -362,6 +393,7 @@ func (a appCreator) appExport(
homePath,
uint(1),
a.encodingConfig,
appConfig,
appOpts,
)

Expand All @@ -373,59 +405,3 @@ func (a appCreator) appExport(

return app.ExportAppStateAndValidators(forZeroHeight, jailAllowedAddrs)
}

// initAppConfig helps to override default appConfig template and configs.
// return "", nil if no custom configuration is required for the application.
func initAppConfig() (string, interface{}) {
// The following code snippet is just for reference.

// WASMConfig defines configuration for the wasm module.
type WASMConfig struct {
// This is the maximum sdk gas (wasm and storage) that we allow for any x/wasm "smart" queries
QueryGasLimit uint64 `mapstructure:"query_gas_limit"`

// Address defines the gRPC-web server to listen on
LruSize uint64 `mapstructure:"lru_size"`
}

type CustomAppConfig struct {
serverconfig.Config

WASM WASMConfig `mapstructure:"wasm"`
}

// Optionally allow the chain developer to overwrite the SDK's default
// server config.
srvCfg := serverconfig.DefaultConfig()
// The SDK's default minimum gas price is set to "" (empty value) inside
// app.toml. If left empty by validators, the node will halt on startup.
// However, the chain developer can set a default app.toml value for their
// validators here.
//
// In summary:
// - if you leave srvCfg.MinGasPrices = "", all validators MUST tweak their
// own app.toml config,
// - if you set srvCfg.MinGasPrices non-empty, validators CAN tweak their
// own app.toml to override, or use this default value.
//
// In simapp, we set the min gas prices to 0.
srvCfg.MinGasPrices = "0stake"

customAppConfig := CustomAppConfig{
Config: *srvCfg,
WASM: WASMConfig{
LruSize: 1,
QueryGasLimit: 300000,
},
}

customAppTemplate := serverconfig.DefaultConfigTemplate + `
[wasm]
# This is the maximum sdk gas (wasm and storage) that we allow for any x/wasm "smart" queries
query_gas_limit = 300000
# This is the number of wasm vm instances we keep cached in memory for speed-up
# Warning: this is currently unstable and may lead to crashes, best to keep for 0 unless testing locally
lru_size = 0`

return customAppTemplate, customAppConfig
}
33 changes: 18 additions & 15 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,26 @@ require (
github.com/spf13/cast v1.5.0
github.com/spf13/cobra v1.5.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.0
github.com/stretchr/testify v1.8.1
github.com/tendermint/tendermint v0.34.22
github.com/tendermint/tm-db v0.6.7
google.golang.org/genproto v0.0.0-20221114212237-e4508ebdbee1
google.golang.org/grpc v1.50.1
google.golang.org/genproto v0.0.0-20221207170731-23e4bf6bdc37
google.golang.org/grpc v1.51.0
gopkg.in/yaml.v2 v2.4.0
)

require golang.org/x/text v0.4.0 // indirect
require golang.org/x/text v0.5.0 // indirect

require github.com/prysmaticlabs/prysm v0.0.0-20220124113610-e26cde5e091b
require (
github.com/prysmaticlabs/prysm v0.0.0-20220124113610-e26cde5e091b
github.com/spf13/viper v1.13.0
)

require (
cloud.google.com/go v0.105.0 // indirect
cloud.google.com/go/compute v1.12.1 // indirect
cloud.google.com/go/compute v1.13.0 // indirect
cloud.google.com/go/compute/metadata v0.2.1 // indirect
cloud.google.com/go/iam v0.7.0 // indirect
cloud.google.com/go/iam v0.8.0 // indirect
cloud.google.com/go/storage v1.27.0 // indirect
filippo.io/edwards25519 v1.0.0-rc.1 // indirect
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect
Expand Down Expand Up @@ -115,11 +118,12 @@ require (
github.com/google/orderedcode v0.0.1 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.0 // indirect
github.com/googleapis/gax-go/v2 v2.6.0 // indirect
github.com/googleapis/gax-go/v2 v2.7.0 // indirect
github.com/gorilla/handlers v1.5.1 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.0 // indirect
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c // indirect
github.com/gtank/merlin v0.1.1 // indirect
github.com/gtank/ristretto255 v0.1.2 // indirect
Expand Down Expand Up @@ -198,7 +202,6 @@ require (
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/spf13/afero v1.8.2 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/viper v1.13.0 // indirect
github.com/subosito/gotenv v1.4.1 // indirect
github.com/supranational/blst v0.3.8-0.20220526154634-513d2456b344 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 // indirect
Expand All @@ -220,18 +223,18 @@ require (
github.com/xanzy/ssh-agent v0.3.2 // indirect
github.com/zondax/hid v0.9.1-0.20220302062450-5552068d2266 // indirect
go.etcd.io/bbolt v1.3.6 // indirect
go.opencensus.io v0.23.0 // indirect
go.opencensus.io v0.24.0 // indirect
golang.org/x/crypto v0.0.0-20220924013350-4ba4fb4dd9e7 // indirect
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e // indirect
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
golang.org/x/net v0.2.0 // indirect
golang.org/x/oauth2 v0.2.0 // indirect
golang.org/x/net v0.3.0 // indirect
golang.org/x/oauth2 v0.3.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.2.0 // indirect
golang.org/x/term v0.2.0 // indirect
golang.org/x/sys v0.3.0 // indirect
golang.org/x/term v0.3.0 // indirect
golang.org/x/tools v0.1.13-0.20220803210227-8b9a1fbdf5c3 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/api v0.102.0 // indirect
google.golang.org/api v0.103.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
Expand Down
Loading