From 561440a56dc9ca2a20eb0727f8f958372269b4a0 Mon Sep 17 00:00:00 2001 From: Callum Waters Date: Thu, 22 Sep 2022 10:49:31 +0200 Subject: [PATCH] config: add version to the config file (#9413) --- config/config.go | 19 ++++++++++++++++++- config/toml.go | 4 ++++ version/version.go | 2 +- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/config/config.go b/config/config.go index c60d37d13..01cf2c268 100644 --- a/config/config.go +++ b/config/config.go @@ -7,7 +7,10 @@ import ( "net/http" "os" "path/filepath" + "regexp" "time" + + "github.com/tendermint/tendermint/version" ) const ( @@ -60,6 +63,9 @@ var ( minSubscriptionBufferSize = 100 defaultSubscriptionBufferSize = 200 + + // taken from https://semver.org/ + semverRegexp = regexp.MustCompile(`^(?P0|[1-9]\d*)\.(?P0|[1-9]\d*)\.(?P0|[1-9]\d*)(?:-(?P(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$`) ) // Config defines the top level configuration for a Tendermint node @@ -165,6 +171,10 @@ type BaseConfig struct { //nolint: maligned // chainID is unexposed and immutable but here for convenience chainID string + // The version of the Tendermint binary that created + // or last modified the config file + Version string `mapstructure:"version"` + // The root directory for all data. // This should be set in viper so it can unmarshal into this struct RootDir string `mapstructure:"home"` @@ -238,6 +248,7 @@ type BaseConfig struct { //nolint: maligned // DefaultBaseConfig returns a default base configuration for a Tendermint node func DefaultBaseConfig() BaseConfig { return BaseConfig{ + Version: version.TMCoreSemVer, Genesis: defaultGenesisJSONPath, PrivValidatorKey: defaultPrivValKeyPath, PrivValidatorState: defaultPrivValStatePath, @@ -250,7 +261,7 @@ func DefaultBaseConfig() BaseConfig { BlockSyncMode: true, FilterPeers: false, DBBackend: "goleveldb", - DBPath: "data", + DBPath: defaultDataDir, } } @@ -296,6 +307,12 @@ func (cfg BaseConfig) DBDir() string { // ValidateBasic performs basic validation (checking param bounds, etc.) and // returns an error if any check fails. func (cfg BaseConfig) ValidateBasic() error { + // version on old config files aren't set so we can't expect it + // always to exist + if cfg.Version != "" && !semverRegexp.MatchString(cfg.Version) { + return fmt.Errorf("invalid version string: %s", cfg.Version) + } + switch cfg.LogFormat { case LogFormatPlain, LogFormatJSON: default: diff --git a/config/toml.go b/config/toml.go index a284e4358..f88611cc9 100644 --- a/config/toml.go +++ b/config/toml.go @@ -76,6 +76,10 @@ const defaultConfigTemplate = `# This is a TOML config file. # "$HOME/.tendermint" by default, but could be changed via $TMHOME env variable # or --home cmd flag. +# The version of the Tendermint binary that created or +# last modified the config file. Do not modify this. +version = "{{ .BaseConfig.Version }}" + ####################################################################### ### Main Base Config Options ### ####################################################################### diff --git a/version/version.go b/version/version.go index 951dd0bdf..272bd0326 100644 --- a/version/version.go +++ b/version/version.go @@ -5,7 +5,7 @@ var TMCoreSemVer = TMVersionDefault const ( // TMVersionDefault is the used as the fallback version of Tendermint Core // when not using git describe. It is formatted with semantic versioning. - TMVersionDefault = "v0.38.0-dev" + TMVersionDefault = "0.38.0-dev" // ABCISemVer is the semantic version of the ABCI protocol ABCISemVer = "1.0.0"