Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version stuff #1893

Merged
merged 3 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion cmd/go-quai/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion cmd/utils/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions p2p/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)),
}

Expand Down
23 changes: 18 additions & 5 deletions params/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"bytes"
"errors"
"io/ioutil"
"os/exec"
"strconv"
"strings"
"sync/atomic"
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

Expand Down
Loading