[NOD-549] Update version to 0.1.0 and allow injection of appBuild (#568)

* [NOD-549] Update version to 0.1.0 and allow injection of appBuild

* [NOD-549] Fixed peer tests

* [NOD-549] Fixed wire tests

* [NOD-549] Remove any mention of semVer.

* [NOD-549] Don't include appBuild at all if it includes invalid characters

* [NOD-549] Panic if appBuild contains invalid characters

* [NOD-549] Move checkAppBuild into
This commit is contained in:
Svarog 2020-01-06 15:30:00 +02:00 committed by Dan Aharoni
parent d8e3191469
commit 7cf15ac93b
7 changed files with 37 additions and 57 deletions

View File

@ -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)

View File

@ -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 {

View File

@ -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"`

View File

@ -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.

View File

@ -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()),

View File

@ -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()
}

View File

@ -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},