Skip to content

Commit e28f769

Browse files
Jerónimo AlbivinbruceluilgoozAlex Johnson
authored
feat: migration system for the config file (ignite#2792)
* Separate the struct definitions of the chainconfig from the parser * Move the struct Config into v0 package * Add and implement the interface Config * Parse the yaml based on the version * Move the filed Build into the common struct * Create the interface for validator * Change the parameter for parse * Add the conversion * Replace v0 with v1 after parsing * Fix the getInit for v1 * Update the parser and the validator * Replace v0 with v1 in env * Change the template * Fixes the issue with the GetHost interface * Add the method to increment the port number * Move FaucetHost to the common package * Remove the default() in config * Type the Version * Refactor the function Parse * Add the test to verify the Parse with migration * Clean up the interface of Config * Update the subcommands for ignite chain * Add tests on the ConfigYaml an some comments * Add the struct defining Gentx * Remove the getters in BaseConfig * Fix the function ConvertLatest * Remove all getters * refactor createValidatorFromConfig * Refactor the common pkg * Change FillValidatorsDefaults and add tests * a * chore: remove empty file * refactor: simplify file names * refactor: improve tests layout * refactor: add `config` package to `chainconfig` This change resolves a cyclic import issue * refactor: rename `Config` interface to `Converter` * chore: change testdata to give BaseConfig precedence * refactor: rename `Config.ConfigVersion` to `Config.Version` The interface's `Version()` method was also renamed to `GetVersion()`. * chore: code cleanup for consistency * chore: remove unused `BaseConfig.AccountByName` method * refactor: move `ListAccounts()` into `BaseConfig` * refactor: rename `ErrCouldntLocateConfig` to `ErrConfigNotFound` * chore: fix docstrings and broken test * refactor: improve v0 to v1 conversion * feat: add `xnet` package * refactor: improve `v1` config implementation - Simplify `Validator` to remove many getter methods - Removed some global variables - Improve unit tests - Add more unit tests - Remove unit tests that were not useful * refactor: remove global and rename var `Migration` to `Versions` * refactor: add default config function to `config` Also added `SetDefaults` to be able to initialize configs during conversion to default values. * refactor: change `ConvertLatest` to return `*v1.Config` This function must always return the latest config. * refactor: move `ErrConfigNotFound` to `errors.go` Also changed `UnsupportedVersionError` to contain the version number instead of an error message. * fix: change v1 `ConvertNext` to assign 1 to the version * test: add `TestConvertV0ToV1` to `v0` convert tests Also removed the `TestConvertNext`. * test: change `v0` testdata to only return a v0 config with values * fix: various fixes related to the refactor * fix: parsing and migration now work using interfaces * refactor: change migration cmd verifier The verifier now stops the command when the config migration is not made. * fix: change config validation to validate "validators" * chore: change `Seek` calls to use a constant argument * refactor: change to use standard reader/writers instead of seeker * test: add test for `MigrateLatest` * test: move testdata config files inside its corresponding version The config files and `GetConfig()` functions for each version not live inside the `v0` and `v1` package. * refactor: rewrite `Parse()` tests * fix: changed `v1` set defaults for don't ovewrite existing values * chore: add more values to testdata config files * test: add `TestParseWithUnknownVersion` test * chore: remove `GetConfigV0` from `v0` testdata Test must use `v0testdata.GetConfig()` instead. * test: rewrite `v0` clone test * fix: change imports from "ignite-hq" to "ignite" * fix: change integration tests to work with the new config * chore: update changelog * chore: fix integration test argument for `IsAppServed` * test: fix broken integration tests * chore(pkg/xnet): add `MustIncreasePortBy` to simplify tests * refactor: use struct to manage config's server addresses Map type causes a lot of trouble and extra casting and checks when the Cosmos SDK or Tendermint config values for the server addresses are read or modified so instead of that this changeset uses a struct. We can't use Cosmos SDK ot Tendermint config structs because if any of there configs gets updated it could break the migrations. The changeset also fixes the default address values that were encoded to the YAML config. These values no longer appear if they are the default ones. * chore: minor corrections * chore: remove unneeded `SetDefaults` call * fix: change init chain to support merging YAML generated maps The contributed implementation was removing some fields which was not correct; Now those config fields are saved during init. It seems that the map fields were being removed to avoid an issue with maps containing interface{} key types instead of string. This change fixes it by saving all the config values and by adding support for YAML generated maps thought a transformer implementation. * refactor: remove merge transformer in favor of a custom map type The mergo transformer didn't work properly so it was replaced with a custom map type that implements the yaml.Unmarshaller interface to convert the map keys to strings right after they are decoded. * refactor: change chain's init to update app config files only once The blockchain app config file were updated twice, once by the stargate plugin and then again by the chain init. This changeset updated the app's config only once. The stargate plugin is the one that updates them. The changeset also solves the issue where come of the addresses that must have the protocol prefix were ovewritten with an address without prefix because the host config value was removed (ignite#2470), so now addresses are defined in the app and config sections. * test: fix broken integration test * refactor: trigger config migration verifier from a pre run handler * refactor: change config migration cmd handler to use `cliui` package * refactor: add a type alias for the latest config version * ci: fix typo * chore: rename variables to follow standard * refactor: change `addGitChangesVerifier` to a cmd pre run handler * refactor: add confirmation before migrating config in unclean state A confirmation is shown now before migrating the confir file to the latest version if the app repository has uncommitted changes. * fix: change config clonning to copy using mergo package * chore: replace deprecated call to ReadFile * chore: fix formatting * tests: change `IsAppServed` to receive the API address * fix: change `pkg/yaml` to convert keys in slice of map This is required to make sure the values read from YAML files can be merged and serialized to other formats like JSON. * tests: fix broken network publish integration test * chore: add `gitChangesConfirmPreRunHandler` to `ts-client` cmd * test: fix broken TS client bank module integration test Co-authored-by: vinbrucelu <houshengbo@gmail.com> Co-authored-by: İlker G. Öztürk <ilkergoktugozturk@gmail.com> Co-authored-by: Alex Johnson <alex@shmeeload.xyz>
1 parent c1fb80f commit e28f769

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+2423
-708
lines changed

changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
- Add `--skip-proto` flag to `build`, `init` and `serve` commands to build the chain without building proto files
3434
- Add `node query tx` command to query a transaction in any chain.
3535
- Add `node query bank` command to query an account's bank balance in any chain.
36+
- Add `node tx bank send` command to send funds from one account to an other in any chain.
37+
- Add migration system for the config file to allow config versioning
3638
- Add `node tx bank send` command to send funds from one account to another in any chain.
3739
- Implement `network profile` command
3840
- Add `generate ts-client` command to generate a stand-alone modular TypeScript client.

ignite/chainconfig/chainconfig.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package chainconfig
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
"strings"
8+
9+
"github.com/ignite/cli/ignite/chainconfig/config"
10+
v0 "github.com/ignite/cli/ignite/chainconfig/v0"
11+
v1 "github.com/ignite/cli/ignite/chainconfig/v1"
12+
"github.com/ignite/cli/ignite/pkg/xfilepath"
13+
)
14+
15+
var (
16+
// ConfigDirPath returns the path of configuration directory of Ignite.
17+
ConfigDirPath = xfilepath.JoinFromHome(xfilepath.Path(".ignite"))
18+
19+
// ConfigFileNames is a list of recognized names as for Ignite's config file.
20+
ConfigFileNames = []string{"config.yml", "config.yaml"}
21+
22+
// DefaultTSClientPath defines the default relative path to use when generating the TS client.
23+
// The path is relative to the app's directory.
24+
DefaultTSClientPath = "ts-client"
25+
26+
// LatestVersion defines the latest version of the config.
27+
LatestVersion config.Version = 1
28+
29+
// Versions holds config types for the supported versions.
30+
Versions = map[config.Version]config.Converter{
31+
0: &v0.Config{},
32+
1: &v1.Config{},
33+
}
34+
)
35+
36+
// Config defines the latest config.
37+
type Config = v1.Config
38+
39+
// DefaultConfig returns a config for the latest version initialized with default values.
40+
func DefaultConfig() *Config {
41+
return v1.DefaultConfig()
42+
}
43+
44+
// FaucetHost returns the faucet host to use.
45+
func FaucetHost(cfg *Config) string {
46+
// We keep supporting Port option for backward compatibility
47+
// TODO: drop this option in the future
48+
host := cfg.Faucet.Host
49+
if cfg.Faucet.Port != 0 {
50+
host = fmt.Sprintf(":%d", cfg.Faucet.Port)
51+
}
52+
53+
return host
54+
}
55+
56+
// TSClientPath returns the relative path to the Typescript client directory.
57+
// Path is relative to the app's directory.
58+
func TSClientPath(conf *Config) string {
59+
if path := strings.TrimSpace(conf.Client.Typescript.Path); path != "" {
60+
return filepath.Clean(path)
61+
}
62+
63+
return DefaultTSClientPath
64+
}
65+
66+
// CreateConfigDir creates config directory if it is not created yet.
67+
func CreateConfigDir() error {
68+
path, err := ConfigDirPath()
69+
if err != nil {
70+
return err
71+
}
72+
73+
return os.MkdirAll(path, 0o755)
74+
}
75+
76+
// LocateDefault locates the default path for the config file.
77+
// Returns ErrConfigNotFound when no config file found.
78+
func LocateDefault(root string) (path string, err error) {
79+
for _, name := range ConfigFileNames {
80+
path = filepath.Join(root, name)
81+
if _, err := os.Stat(path); err == nil {
82+
return path, nil
83+
} else if !os.IsNotExist(err) {
84+
return "", err
85+
}
86+
}
87+
88+
return "", ErrConfigNotFound
89+
}

ignite/chainconfig/config.go

Lines changed: 0 additions & 293 deletions
This file was deleted.

0 commit comments

Comments
 (0)