Skip to content

Commit

Permalink
Redo network config (#472)
Browse files Browse the repository at this point in the history
* Redesign network config

* Adjust everything

* Bring back denom parameter to the genesis template

* Merge master into wojtek/network-config

* fixes

* Merge remote-tracking branch 'origin/wojtek/network-config' into wojtek/network-config

* fixes

* Merge master into wojtek/network-config

* fixes

* Merge master into wojtek/network-config

* Merge master into wojtek/network-config
  • Loading branch information
wojtek-coreum authored Apr 28, 2023
1 parent 974d106 commit 599bd6e
Show file tree
Hide file tree
Showing 31 changed files with 673 additions and 738 deletions.
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
19 changes: 14 additions & 5 deletions cmd/cored/cosmoscmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cosmoscmd

import (
"bufio"
"os"
"path/filepath"

"github.com/cosmos/cosmos-sdk/client"
Expand Down Expand Up @@ -29,7 +30,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 @@ -67,14 +68,22 @@ func InitCmd(network config.Network, defaultNodeHome string) *cobra.Command {
return errors.Errorf("genesis.json file already exists: %v", genFile)
}

err = network.SaveGenesis(clientCtx.HomeDir)
genDocBytes, err := network.EncodeGenesis()
if err != nil {
return err
}

networkNodeConfig := network.NodeConfig()
networkNodeConfig.Name = args[0]
cfg = network.NodeConfig().TendermintNodeConfig(cfg)
configDir := filepath.Join(clientCtx.HomeDir, "config")
if err := os.MkdirAll(configDir, 0o700); err != nil {
return errors.Wrap(err, "unable to make config directory")
}

if err := os.WriteFile(filepath.Join(configDir, "genesis.json"), genDocBytes, 0644); err != nil {
return errors.Wrap(err, "unable to write genesis bytes to file")
}

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 @@
}
}
}
}
}
38 changes: 16 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,9 +12,7 @@ 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"
Expand All @@ -25,14 +22,20 @@ import (
type ChainContext struct {
ClientContext client.Context
NetworkConfig config.NetworkConfig
InitialGasPrice sdk.Dec
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,
initialGasPrice sdk.Dec,
) ChainContext {
return ChainContext{
ClientContext: clientCtx,
NetworkConfig: networkCfg,
InitialGasPrice: initialGasPrice,
DeterministicGasConfig: deterministicgas.DefaultConfig(),
}
}
Expand Down Expand Up @@ -76,19 +79,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.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 +135,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.InitialGasPrice
}

if options.Amount.IsNil() {
Expand All @@ -156,8 +159,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
InitialGasPrice sdk.Dec
FundingMnemonic string
StakerMnemonics []string
}
Expand All @@ -171,22 +176,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.InitialGasPrice)
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.InitialGasPrice))
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()

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

chain = NewChain(ChainConfig{
ClientContext: clientCtx,
GRPCAddress: cfg.GRPCAddress,
NetworkConfig: cfg.NetworkConfig,
InitialGasPrice: resp.Params.Model.InitialGasPrice,
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

0 comments on commit 599bd6e

Please sign in to comment.