From 10a186b995ec4ff4f558a3261b8ac9d5f584d450 Mon Sep 17 00:00:00 2001 From: wizeguyy Date: Tue, 18 Jun 2024 11:29:50 -0500 Subject: [PATCH 1/3] Init version cache at startup --- cmd/go-quai/start.go | 5 ++++- params/version.go | 5 +++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/cmd/go-quai/start.go b/cmd/go-quai/start.go index fe9b78af66..e2a32ef95d 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.Version.Full(), network) ctx, cancel := context.WithCancel(context.Background()) defer cancel() diff --git a/params/version.go b/params/version.go index e5c1bea19d..9d2fc0b617 100644 --- a/params/version.go +++ b/params/version.go @@ -38,6 +38,11 @@ type version struct { short string } +func InitVersion() { + Version = CachedVersion{} + Version.load() +} + func readVersionFile() (version, error) { raw, err := ioutil.ReadFile("VERSION") if err != nil { From ed29cc73b349284f61eda2a08f3b830baa6d2bd9 Mon Sep 17 00:00:00 2001 From: wizeguyy Date: Tue, 18 Jun 2024 12:02:39 -0500 Subject: [PATCH 2/3] Parse commit hash for version --- cmd/go-quai/start.go | 2 +- cmd/utils/cmd.go | 2 +- params/version.go | 18 +++++++++++++----- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/cmd/go-quai/start.go b/cmd/go-quai/start.go index e2a32ef95d..ee7c7e9c4d 100644 --- a/cmd/go-quai/start.go +++ b/cmd/go-quai/start.go @@ -70,7 +70,7 @@ func startCmdPreRun(cmd *cobra.Command, args []string) error { func runStart(cmd *cobra.Command, args []string) error { network := viper.GetString(utils.EnvironmentFlag.Name) - log.Global.Infof("Starting %s on the %s network", params.Version.Full(), 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/params/version.go b/params/version.go index 9d2fc0b617..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" @@ -43,6 +44,15 @@ func InitVersion() { 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 { @@ -108,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) @@ -169,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 } From 64fb08251c3739d50ac8f5de36f616be3bf5116a Mon Sep 17 00:00:00 2001 From: wizeguyy Date: Tue, 18 Jun 2024 12:05:48 -0500 Subject: [PATCH 3/3] Add version to useragent --- p2p/node/node.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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)), }