diff --git a/cmd/go-quai/start.go b/cmd/go-quai/start.go index fe9b78af66..ee7c7e9c4d 100644 --- a/cmd/go-quai/start.go +++ b/cmd/go-quai/start.go @@ -18,6 +18,7 @@ import ( "github.com/dominant-strategies/go-quai/log" "github.com/dominant-strategies/go-quai/metrics_config" "github.com/dominant-strategies/go-quai/p2p/node" + "github.com/dominant-strategies/go-quai/params" ) var startCmd = &cobra.Command{ @@ -62,12 +63,14 @@ func startCmdPreRun(cmd *cobra.Command, args []string) error { configDir := cmd.Flag(utils.ConfigDirFlag.Name).Value.String() viper.Set(utils.KeyFileFlag.Name, filepath.Join(configDir, "private.key")) } + // Initialize the version info + params.InitVersion() return nil } func runStart(cmd *cobra.Command, args []string) error { network := viper.GetString(utils.EnvironmentFlag.Name) - log.Global.Infof("Starting go-quai on the %s network", network) + log.Global.Infof("Starting %s on the %s network", params.VersionWithCommit(), network) ctx, cancel := context.WithCancel(context.Background()) defer cancel() diff --git a/cmd/utils/cmd.go b/cmd/utils/cmd.go index 394e1e4e5c..587badf487 100644 --- a/cmd/utils/cmd.go +++ b/cmd/utils/cmd.go @@ -111,7 +111,7 @@ func makeConfigNode(slicesRunning []common.Location, nodeLocation common.Locatio func defaultNodeConfig() node.Config { cfg := node.DefaultConfig cfg.Name = "" - cfg.Version = params.VersionWithCommit("", "") + cfg.Version = params.VersionWithCommit() cfg.HTTPModules = append(cfg.HTTPModules, "eth") cfg.WSModules = append(cfg.WSModules, "eth") return cfg diff --git a/p2p/node/node.go b/p2p/node/node.go index 1c4a06cb70..61ce6c936b 100644 --- a/p2p/node/node.go +++ b/p2p/node/node.go @@ -29,6 +29,7 @@ import ( "github.com/dominant-strategies/go-quai/p2p/node/requestManager" "github.com/dominant-strategies/go-quai/p2p/node/streamManager" "github.com/dominant-strategies/go-quai/p2p/protocol" + "github.com/dominant-strategies/go-quai/params" "github.com/dominant-strategies/go-quai/quai" ) @@ -155,8 +156,7 @@ func NewNode(ctx context.Context, quitCh chan struct{}) (*P2PNode, error) { } idOpts := []identify.Option{ - // TODO: Add version number + commit hash - identify.UserAgent("go-quai"), + identify.UserAgent("go-quai " + params.VersionWithCommit()), identify.ProtocolVersion(string(protocol.ProtocolVersion)), } diff --git a/params/version.go b/params/version.go index e5c1bea19d..47d954650a 100644 --- a/params/version.go +++ b/params/version.go @@ -20,6 +20,7 @@ import ( "bytes" "errors" "io/ioutil" + "os/exec" "strconv" "strings" "sync/atomic" @@ -38,6 +39,20 @@ type version struct { short string } +func InitVersion() { + Version = CachedVersion{} + Version.load() +} + +func GetGitHead() string { + cmd := exec.Command("git", "rev-parse", "HEAD") + head, err := cmd.CombinedOutput() + if err != nil { + panic(err) + } + return string(head) +} + func readVersionFile() (version, error) { raw, err := ioutil.ReadFile("VERSION") if err != nil { @@ -103,7 +118,7 @@ func (v *CachedVersion) load() { } v.major.Store(ver.major) v.minor.Store(ver.minor) - v.patch.Store(ver.patch) + v.patch.Store(GetGitHead()) v.meta.Store(ver.meta) v.full.Store(ver.full) v.short.Store(ver.short) @@ -164,14 +179,12 @@ func (v *CachedVersion) Short() string { return v.short.Load().(string) } -func VersionWithCommit(gitCommit, gitDate string) string { +func VersionWithCommit() string { vsn := Version.Full() + gitCommit := GetGitHead() if len(gitCommit) >= 8 { vsn += "-" + gitCommit[:8] } - if (Version.Meta() != "stable") && (gitDate != "") { - vsn += "-" + gitDate - } return vsn }