diff --git a/config/config.go b/config/config.go index 8918efb82..16e89a457 100644 --- a/config/config.go +++ b/config/config.go @@ -282,10 +282,11 @@ func loadConfig() (*Config, []string, error) { } } - // Show the version and exit if the version flag was specified. appName := filepath.Base(os.Args[0]) appName = strings.TrimSuffix(appName, filepath.Ext(appName)) usageMessage := fmt.Sprintf("Use %s -h to show usage", appName) + + // Show the version and exit if the version flag was specified. if preCfg.ShowVersion { fmt.Println(appName, "version", version.Version()) os.Exit(0) diff --git a/peer/peer_test.go b/peer/peer_test.go index d742f72eb..1c50005cc 100644 --- a/peer/peer_test.go +++ b/peer/peer_test.go @@ -5,13 +5,14 @@ package peer_test import ( - "github.com/pkg/errors" "io" "net" "strconv" "testing" "time" + "github.com/pkg/errors" + "github.com/btcsuite/go-socks/socks" "github.com/kaspanet/kaspad/dagconfig" "github.com/kaspanet/kaspad/peer" @@ -241,8 +242,8 @@ func TestPeerConnection(t *testing.T) { wantLastPingNonce: uint64(0), wantLastPingMicros: int64(0), wantTimeOffset: int64(0), - wantBytesSent: 201, // 177 version + 24 verack - wantBytesReceived: 201, + wantBytesSent: 195, // 171 version + 24 verack + wantBytesReceived: 195, } wantStats2 := peerStats{ wantUserAgent: wire.DefaultUserAgent + "peer:1.0(comment)/", @@ -255,8 +256,8 @@ func TestPeerConnection(t *testing.T) { wantLastPingNonce: uint64(0), wantLastPingMicros: int64(0), wantTimeOffset: int64(0), - wantBytesSent: 201, // 177 version + 24 verack - wantBytesReceived: 201, + wantBytesSent: 195, // 171 version + 24 verack + wantBytesReceived: 195, } tests := []struct { diff --git a/rpcmodel/rpc_results.go b/rpcmodel/rpc_results.go index c273ade50..41e32835a 100644 --- a/rpcmodel/rpc_results.go +++ b/rpcmodel/rpc_results.go @@ -417,7 +417,7 @@ type GetWorkResult struct { // InfoDAGResult models the data returned by the kaspa rpc server getinfo command. type InfoDAGResult struct { - Version int32 `json:"version"` + Version string `json:"version"` ProtocolVersion int32 `json:"protocolVersion"` Blocks uint64 `json:"blocks"` TimeOffset int64 `json:"timeOffset"` diff --git a/server/p2p/p2p.go b/server/p2p/p2p.go index b6ba41438..931624604 100644 --- a/server/p2p/p2p.go +++ b/server/p2p/p2p.go @@ -65,7 +65,7 @@ var ( // userAgentVersion is the user agent version and is used to help // identify ourselves to other kaspa peers. - userAgentVersion = fmt.Sprintf("%d.%d.%d", version.AppMajor, version.AppMinor, version.AppPatch) + userAgentVersion = version.Version() ) // onionAddr implements the net.Addr interface and represents a tor address. diff --git a/server/rpc/handle_get_info.go b/server/rpc/handle_get_info.go index d24c93895..9140d31b0 100644 --- a/server/rpc/handle_get_info.go +++ b/server/rpc/handle_get_info.go @@ -10,7 +10,7 @@ import ( // that are not related to wallet functionality. func handleGetInfo(s *Server, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) { ret := &rpcmodel.InfoDAGResult{ - Version: int32(1000000*version.AppMajor + 10000*version.AppMinor + 100*version.AppPatch), + Version: version.Version(), ProtocolVersion: int32(maxProtocolVersion), Blocks: s.cfg.DAG.BlockCount(), TimeOffset: int64(s.cfg.TimeSource.Offset().Seconds()), diff --git a/version/version.go b/version/version.go index a9011cd9c..4b3a0d985 100644 --- a/version/version.go +++ b/version/version.go @@ -1,72 +1,50 @@ -// Copyright (c) 2013-2014 The btcsuite developers -// Use of this source code is governed by an ISC -// license that can be found in the LICENSE file. - package version import ( - "bytes" "fmt" "strings" ) -// semanticAlphabet -const semanticAlphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-" +// validCharacters is a list of characters valid in the appBuild string +const validCharacters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-" -// These constants define the application version and follow the semantic -// versioning 2.0.0 spec (http://semver.org/). const ( - AppMajor uint = 0 - AppMinor uint = 12 - AppPatch uint = 0 - - // appPreRelease MUST only contain characters from semanticAlphabet - // per the semantic versioning spec. - appPreRelease = "beta" + appMajor uint = 0 + appMinor uint = 1 + appPatch uint = 0 ) // appBuild is defined as a variable so it can be overridden during the build -// process with '-ldflags "-X main.appBuild foo' if needed. It MUST only -// contain characters from semanticAlphabet per the semantic versioning spec. +// process with '-ldflags "-X github.com/kaspanet/kaspad/version.appBuild=foo"' if needed. +// It MUST only contain characters from validCharacters. var appBuild string -// Version returns the application version as a properly formed string per the -// semantic versioning 2.0.0 spec (http://semver.org/). +var version = "" // string used for memoization of version + +// Version returns the application version as a properly formed string func Version() string { - // Start with the major, minor, and patch versions. - version := fmt.Sprintf("%d.%d.%d", AppMajor, AppMinor, AppPatch) + if version == "" { + // Start with the major, minor, and patch versions. + version = fmt.Sprintf("%d.%d.%d", appMajor, appMinor, appPatch) - // Append pre-release version if there is one. The hyphen called for - // by the semantic versioning spec is automatically appended and should - // not be contained in the pre-release string. The pre-release version - // is not appended if it contains invalid characters. - preRelease := normalizeVerString(appPreRelease) - if preRelease != "" { - version = fmt.Sprintf("%s-%s", version, preRelease) - } + // Append build metadata if there is any. The build metadata + // string is not appended if it contains invalid characters. + if appBuild != "" { + checkAppBuild(appBuild) - // Append build metadata if there is any. The plus called for - // by the semantic versioning spec is automatically appended and should - // not be contained in the build metadata string. The build metadata - // string is not appended if it contains invalid characters. - build := normalizeVerString(appBuild) - if build != "" { - version = fmt.Sprintf("%s+%s", version, build) + version = fmt.Sprintf("%s-%s", version, appBuild) + } } return version } -// normalizeVerString returns the passed string stripped of all characters which -// are not valid according to the semantic versioning guidelines for pre-release -// version and build metadata strings. In particular they MUST only contain -// characters in semanticAlphabet. -func normalizeVerString(str string) string { - var result bytes.Buffer - for _, r := range str { - if strings.ContainsRune(semanticAlphabet, r) { - result.WriteRune(r) +// checkAppBuild returns the passed string unless it contains any characters not in validCharacters +// If any invalid characters are encountered - an empty string is returned +func checkAppBuild(appBuild string) { + for _, r := range appBuild { + if !strings.ContainsRune(validCharacters, r) { + panic(fmt.Errorf("appBuild string (%s) contains forbidden characters. Only alphanumeric characters and dashes are allowed", appBuild)) } } - return result.String() } diff --git a/wire/message_test.go b/wire/message_test.go index 6cb5114d2..40f97d7c1 100644 --- a/wire/message_test.go +++ b/wire/message_test.go @@ -79,7 +79,7 @@ func TestMessage(t *testing.T) { kaspaNet KaspaNet // Network to use for wire encoding bytes int // Expected num bytes read/written }{ - {msgVersion, msgVersion, pver, MainNet, 159}, + {msgVersion, msgVersion, pver, MainNet, 153}, {msgVerack, msgVerack, pver, MainNet, 24}, {msgGetAddr, msgGetAddr, pver, MainNet, 26}, {msgAddr, msgAddr, pver, MainNet, 27},