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

Redo network config #472

Merged
merged 11 commits into from
Apr 28, 2023
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ const (

// ChosenNetwork is a hacky solution to pass network config
// from cmd package to app.
var ChosenNetwork config.Network
var ChosenNetwork config.NetworkConfig

var (
// DefaultNodeHome default home directories for the application daemon.
Expand Down
2 changes: 1 addition & 1 deletion app/simulation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func BenchmarkSimulation(b *testing.B) {
})

encoding := config.NewEncodingConfig(app.ModuleBasics)
network, err := config.NetworkByChainID(constant.ChainIDDev)
network, err := config.NetworkConfigByChainID(constant.ChainIDDev)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion app/upgrade/v1/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
const Name = "v1"

// NewV1Upgrade makes an upgrade handler for v1 upgrade.
func NewV1Upgrade(mm *module.Manager, configurator module.Configurator, chosenNetwork config.Network, assetNFTKeeper assetnftkeeper.Keeper) upgrade.Upgrade {
func NewV1Upgrade(mm *module.Manager, configurator module.Configurator, chosenNetwork config.NetworkConfig, assetNFTKeeper assetnftkeeper.Keeper) upgrade.Upgrade {
return upgrade.Upgrade{
Name: Name,
StoreUpgrades: storetypes.StoreUpgrades{
Expand Down
10 changes: 5 additions & 5 deletions cmd/cored/cosmoscmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func OverwriteDefaultChainIDFlags(parentCmd *cobra.Command) {
}

// PreProcessFlags prepares the initial flags config for the cli.
func PreProcessFlags() (config.Network, error) {
func PreProcessFlags() (config.NetworkConfig, error) {
// define flags
const flagHelp = "help"
flagSet := pflag.NewFlagSet("pre-process", pflag.ExitOnError)
Expand All @@ -42,24 +42,24 @@ func PreProcessFlags() (config.Network, error) {
// we consider the issued command to be a help command if no args are provided.
// in that case we will not check the chain-id and will return
if len(os.Args) == 1 || *help {
return config.Network{}, nil
return config.NetworkConfig{}, nil
}

// overwrite home flag
if flagSet.Changed(flags.FlagHome) {
err := appendStringFlag(os.Args, flags.FlagHome, *chainID)
if err != nil {
return config.Network{}, err
return config.NetworkConfig{}, err
}
} else {
appendedHome := filepath.Join(app.DefaultNodeHome, *chainID)
os.Args = append(os.Args, fmt.Sprintf("--%s=%s", flags.FlagHome, appendedHome))
}

// get chain config
network, err := config.NetworkByChainID(constant.ChainID(*chainID))
network, err := config.NetworkConfigByChainID(constant.ChainID(*chainID))
if err != nil {
return config.Network{}, err
return config.NetworkConfig{}, err
}

return network, nil
Expand Down
7 changes: 3 additions & 4 deletions cmd/cored/cosmoscmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const (
)

// InitCmd returns the init cobra command.
func InitCmd(network config.Network, defaultNodeHome string) *cobra.Command {
func InitCmd(network config.NetworkConfig, defaultNodeHome string) *cobra.Command {
cmd := &cobra.Command{
Use: "init [moniker]",
Short: "Initialize configuration files for private validator, p2p, genesis, and application",
Expand Down Expand Up @@ -72,9 +72,8 @@ func InitCmd(network config.Network, defaultNodeHome string) *cobra.Command {
return err
}

networkNodeConfig := network.NodeConfig()
networkNodeConfig.Name = args[0]
cfg = network.NodeConfig().TendermintNodeConfig(cfg)
network.NodeConfig.Name = args[0]
cfg = network.NodeConfig.TendermintNodeConfig(cfg)

_, _, err = genutil.InitializeNodeValidatorFilesFromMnemonic(cfg, mnemonic)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions cmd/cored/cosmoscmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (s *rootOptions) apply(options ...Option) {
func NewRootCmd(
appName,
defaultNodeHome string,
network config.Network,
network config.NetworkConfig,
moduleBasics module.BasicManager,
buildApp AppBuilder,
options ...Option,
Expand Down Expand Up @@ -377,7 +377,7 @@ func (a appCreator) appExport(

// initAppConfig helps to override default appConfig template and configs.
// return "", nil if no custom configuration is required for the application.
func initAppConfig(network config.Network) (string, interface{}) {
func initAppConfig(network config.NetworkConfig) (string, interface{}) {
// Optionally allow the chain developer to overwrite the SDK's default
// server config.
srvCfg := serverconfig.DefaultConfig()
Expand Down
2 changes: 1 addition & 1 deletion genesis/coreum-mainnet-1.json
Original file line number Diff line number Diff line change
Expand Up @@ -838,4 +838,4 @@
}
}
}
}
}
39 changes: 17 additions & 22 deletions integration-tests/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"reflect"
"testing"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdkmultisig "github.com/cosmos/cosmos-sdk/crypto/keys/multisig"
Expand All @@ -13,26 +12,31 @@ import (
"github.com/google/uuid"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"

"github.com/CoreumFoundation/coreum/app"
"github.com/CoreumFoundation/coreum/pkg/client"
"github.com/CoreumFoundation/coreum/pkg/config"
"github.com/CoreumFoundation/coreum/x/deterministicgas"
feemodeltypes "github.com/CoreumFoundation/coreum/x/feemodel/types"
)

// ChainContext is a types used to store the components required for the test chains subcomponents.
type ChainContext struct {
ClientContext client.Context
NetworkConfig config.NetworkConfig
FeeModelParams feemodeltypes.Params
DeterministicGasConfig deterministicgas.Config
}

// NewChainContext returns a new instance if the ChainContext.
func NewChainContext(clientCtx client.Context, networkCfg config.NetworkConfig) ChainContext {
func NewChainContext(
clientCtx client.Context,
networkCfg config.NetworkConfig,
feeModelParams feemodeltypes.Params,
) ChainContext {
return ChainContext{
ClientContext: clientCtx,
NetworkConfig: networkCfg,
FeeModelParams: feeModelParams,
DeterministicGasConfig: deterministicgas.DefaultConfig(),
}
}
Expand Down Expand Up @@ -76,19 +80,19 @@ func (c ChainContext) ImportMnemonic(mnemonic string) sdk.AccAddress {
func (c ChainContext) TxFactory() client.Factory {
return client.Factory{}.
WithKeybase(c.ClientContext.Keyring()).
WithChainID(string(c.NetworkConfig.ChainID)).
WithChainID(string(c.NetworkConfig.ChainID())).
WithTxConfig(c.ClientContext.TxConfig()).
WithGasPrices(c.NewDecCoin(c.NetworkConfig.Fee.FeeModel.Params().InitialGasPrice).String())
WithGasPrices(c.NewDecCoin(c.FeeModelParams.Model.InitialGasPrice).String())
}

// NewCoin helper function to initialize sdk.Coin by passing just amount.
func (c ChainContext) NewCoin(amount sdk.Int) sdk.Coin {
return sdk.NewCoin(c.NetworkConfig.Denom, amount)
return sdk.NewCoin(c.NetworkConfig.Denom(), amount)
}

// NewDecCoin helper function to initialize sdk.DecCoin by passing just amount.
func (c ChainContext) NewDecCoin(amount sdk.Dec) sdk.DecCoin {
return sdk.NewDecCoinFromDec(c.NetworkConfig.Denom, amount)
return sdk.NewDecCoinFromDec(c.NetworkConfig.Denom(), amount)
}

// GasLimitByMsgs calculates sum of gas limits required for message types passed.
Expand Down Expand Up @@ -132,7 +136,7 @@ type BalancesOptions struct {
// ComputeNeededBalanceFromOptions computes the required balance based on the input options.
func (c ChainContext) ComputeNeededBalanceFromOptions(options BalancesOptions) sdk.Int {
if options.GasPrice.IsNil() {
options.GasPrice = c.NetworkConfig.Fee.FeeModel.Params().InitialGasPrice
options.GasPrice = c.FeeModelParams.Model.InitialGasPrice
}

if options.Amount.IsNil() {
Expand All @@ -156,8 +160,10 @@ func (c ChainContext) ComputeNeededBalanceFromOptions(options BalancesOptions) s

// ChainConfig defines the config arguments required for the test chain initialisation.
type ChainConfig struct {
ClientContext client.Context
GRPCAddress string
NetworkConfig config.NetworkConfig
FeeModelParams feemodeltypes.Params
FundingMnemonic string
StakerMnemonics []string
}
Expand All @@ -171,22 +177,11 @@ type Chain struct {

// NewChain creates an instance of the new Chain.
func NewChain(cfg ChainConfig) Chain {
clientCtx := client.NewContext(client.DefaultContextConfig(), app.ModuleBasics).
WithChainID(string(cfg.NetworkConfig.ChainID)).
WithKeyring(newConcurrentSafeKeyring(keyring.NewInMemory())).
WithBroadcastMode(flags.BroadcastBlock)

grpcClient, err := grpc.Dial(cfg.GRPCAddress, grpc.WithInsecure())
if err != nil {
panic(err)
}
clientCtx = clientCtx.WithGRPCClient(grpcClient)

chainCtx := NewChainContext(clientCtx, cfg.NetworkConfig)
chainCtx := NewChainContext(cfg.ClientContext, cfg.NetworkConfig, cfg.FeeModelParams)
governance := NewGovernance(chainCtx, cfg.StakerMnemonics)

faucetAddr := chainCtx.ImportMnemonic(cfg.FundingMnemonic)
faucet := NewFaucet(NewChainContext(clientCtx.WithFromAddress(faucetAddr), cfg.NetworkConfig))
faucet := NewFaucet(NewChainContext(cfg.ClientContext.WithFromAddress(faucetAddr), cfg.NetworkConfig, cfg.FeeModelParams))
return Chain{
ChainContext: chainCtx,
Governance: governance,
Expand Down
46 changes: 39 additions & 7 deletions integration-tests/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,17 @@ import (
"fmt"
"testing"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/pkg/errors"
"google.golang.org/grpc"

"github.com/CoreumFoundation/coreum-tools/pkg/logger"
"github.com/CoreumFoundation/coreum/app"
"github.com/CoreumFoundation/coreum/pkg/client"
"github.com/CoreumFoundation/coreum/pkg/config"
"github.com/CoreumFoundation/coreum/pkg/config/constant"
feemodeltypes "github.com/CoreumFoundation/coreum/x/feemodel/types"
)

// stringsFlag allows setting a value multiple times to collect a list, as in -I=val1 -I=val2.
Expand Down Expand Up @@ -37,6 +45,7 @@ type testingConfig struct {
}

var (
ctx context.Context
cfg testingConfig
chain Chain
)
Expand Down Expand Up @@ -84,24 +93,47 @@ func init() {
RunUnsafe: runUnsafe,
}

config.NewNetwork(cfg.NetworkConfig).SetSDKConfig()
loggerConfig := logger.Config{
Format: cfg.LogFormat,
Verbose: cfg.LogVerbose,
}
ctx = logger.WithLogger(context.Background(), logger.New(loggerConfig))

cfg.NetworkConfig.SetSDKConfig()

grpcClient, err := grpc.Dial(coredAddress, grpc.WithInsecure())
if err != nil {
panic(errors.WithStack(err))
}
clientCtx := client.NewContext(client.DefaultContextConfig(), app.ModuleBasics).
WithChainID(string(cfg.NetworkConfig.ChainID())).
WithKeyring(newConcurrentSafeKeyring(keyring.NewInMemory())).
WithBroadcastMode(flags.BroadcastBlock).
WithGRPCClient(grpcClient)

feemodelClient := feemodeltypes.NewQueryClient(clientCtx)

ctx, cancel := context.WithTimeout(ctx, client.DefaultContextConfig().TimeoutConfig.RequestTimeout)
defer cancel()

_, err = feemodelClient.Params(ctx, &feemodeltypes.QueryParamsRequest{})
if err != nil {
panic(errors.WithStack(err))
}

chain = NewChain(ChainConfig{
ClientContext: clientCtx,
GRPCAddress: cfg.GRPCAddress,
NetworkConfig: cfg.NetworkConfig,
FeeModelParams: feemodeltypes.DefaultParams(),
FundingMnemonic: cfg.FundingMnemonic,
StakerMnemonics: cfg.StakerMnemonics,
})
}

// NewTestingContext returns the configured chain and new context for the integration tests.
func NewTestingContext(t *testing.T) (context.Context, Chain) {
loggerConfig := logger.Config{
Format: cfg.LogFormat,
Verbose: cfg.LogVerbose,
}

ctx, cancel := context.WithCancel(logger.WithLogger(context.Background(), logger.New(loggerConfig)))
ctx, cancel := context.WithCancel(ctx)
t.Cleanup(cancel)

return ctx, chain
Expand Down
Loading