[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 := filepath.Base(os.Args[0])
appName = strings.TrimSuffix(appName, filepath.Ext(appName)) appName = strings.TrimSuffix(appName, filepath.Ext(appName))
usageMessage := fmt.Sprintf("Use %s -h to show usage", 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 { if preCfg.ShowVersion {
fmt.Println(appName, "version", version.Version()) fmt.Println(appName, "version", version.Version())
os.Exit(0) os.Exit(0)

View File

@ -5,13 +5,14 @@
package peer_test package peer_test
import ( import (
"github.com/pkg/errors"
"io" "io"
"net" "net"
"strconv" "strconv"
"testing" "testing"
"time" "time"
"github.com/pkg/errors"
"github.com/btcsuite/go-socks/socks" "github.com/btcsuite/go-socks/socks"
"github.com/kaspanet/kaspad/dagconfig" "github.com/kaspanet/kaspad/dagconfig"
"github.com/kaspanet/kaspad/peer" "github.com/kaspanet/kaspad/peer"
@ -241,8 +242,8 @@ func TestPeerConnection(t *testing.T) {
wantLastPingNonce: uint64(0), wantLastPingNonce: uint64(0),
wantLastPingMicros: int64(0), wantLastPingMicros: int64(0),
wantTimeOffset: int64(0), wantTimeOffset: int64(0),
wantBytesSent: 201, // 177 version + 24 verack wantBytesSent: 195, // 171 version + 24 verack
wantBytesReceived: 201, wantBytesReceived: 195,
} }
wantStats2 := peerStats{ wantStats2 := peerStats{
wantUserAgent: wire.DefaultUserAgent + "peer:1.0(comment)/", wantUserAgent: wire.DefaultUserAgent + "peer:1.0(comment)/",
@ -255,8 +256,8 @@ func TestPeerConnection(t *testing.T) {
wantLastPingNonce: uint64(0), wantLastPingNonce: uint64(0),
wantLastPingMicros: int64(0), wantLastPingMicros: int64(0),
wantTimeOffset: int64(0), wantTimeOffset: int64(0),
wantBytesSent: 201, // 177 version + 24 verack wantBytesSent: 195, // 171 version + 24 verack
wantBytesReceived: 201, wantBytesReceived: 195,
} }
tests := []struct { tests := []struct {

View File

@ -417,7 +417,7 @@ type GetWorkResult struct {
// InfoDAGResult models the data returned by the kaspa rpc server getinfo command. // InfoDAGResult models the data returned by the kaspa rpc server getinfo command.
type InfoDAGResult struct { type InfoDAGResult struct {
Version int32 `json:"version"` Version string `json:"version"`
ProtocolVersion int32 `json:"protocolVersion"` ProtocolVersion int32 `json:"protocolVersion"`
Blocks uint64 `json:"blocks"` Blocks uint64 `json:"blocks"`
TimeOffset int64 `json:"timeOffset"` TimeOffset int64 `json:"timeOffset"`

View File

@ -65,7 +65,7 @@ var (
// userAgentVersion is the user agent version and is used to help // userAgentVersion is the user agent version and is used to help
// identify ourselves to other kaspa peers. // 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. // 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. // that are not related to wallet functionality.
func handleGetInfo(s *Server, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) { func handleGetInfo(s *Server, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) {
ret := &rpcmodel.InfoDAGResult{ ret := &rpcmodel.InfoDAGResult{
Version: int32(1000000*version.AppMajor + 10000*version.AppMinor + 100*version.AppPatch), Version: version.Version(),
ProtocolVersion: int32(maxProtocolVersion), ProtocolVersion: int32(maxProtocolVersion),
Blocks: s.cfg.DAG.BlockCount(), Blocks: s.cfg.DAG.BlockCount(),
TimeOffset: int64(s.cfg.TimeSource.Offset().Seconds()), 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 package version
import ( import (
"bytes"
"fmt" "fmt"
"strings" "strings"
) )
// semanticAlphabet // validCharacters is a list of characters valid in the appBuild string
const semanticAlphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-" const validCharacters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-"
// These constants define the application version and follow the semantic
// versioning 2.0.0 spec (http://semver.org/).
const ( const (
AppMajor uint = 0 appMajor uint = 0
AppMinor uint = 12 appMinor uint = 1
AppPatch uint = 0 appPatch uint = 0
// appPreRelease MUST only contain characters from semanticAlphabet
// per the semantic versioning spec.
appPreRelease = "beta"
) )
// appBuild is defined as a variable so it can be overridden during the build // 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 // process with '-ldflags "-X github.com/kaspanet/kaspad/version.appBuild=foo"' if needed.
// contain characters from semanticAlphabet per the semantic versioning spec. // It MUST only contain characters from validCharacters.
var appBuild string var appBuild string
// Version returns the application version as a properly formed string per the var version = "" // string used for memoization of version
// semantic versioning 2.0.0 spec (http://semver.org/).
// Version returns the application version as a properly formed string
func Version() string { func Version() string {
if version == "" {
// Start with the major, minor, and patch versions. // Start with the major, minor, and patch versions.
version := fmt.Sprintf("%d.%d.%d", AppMajor, AppMinor, AppPatch) version = fmt.Sprintf("%d.%d.%d", appMajor, appMinor, appPatch)
// Append pre-release version if there is one. The hyphen called for // Append build metadata if there is any. The build metadata
// 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 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. // string is not appended if it contains invalid characters.
build := normalizeVerString(appBuild) if appBuild != "" {
if build != "" { checkAppBuild(appBuild)
version = fmt.Sprintf("%s+%s", version, build)
version = fmt.Sprintf("%s-%s", version, appBuild)
}
} }
return version return version
} }
// normalizeVerString returns the passed string stripped of all characters which // checkAppBuild returns the passed string unless it contains any characters not in validCharacters
// are not valid according to the semantic versioning guidelines for pre-release // If any invalid characters are encountered - an empty string is returned
// version and build metadata strings. In particular they MUST only contain func checkAppBuild(appBuild string) {
// characters in semanticAlphabet. for _, r := range appBuild {
func normalizeVerString(str string) string { if !strings.ContainsRune(validCharacters, r) {
var result bytes.Buffer panic(fmt.Errorf("appBuild string (%s) contains forbidden characters. Only alphanumeric characters and dashes are allowed", appBuild))
for _, r := range str {
if strings.ContainsRune(semanticAlphabet, r) {
result.WriteRune(r)
} }
} }
return result.String()
} }

View File

@ -79,7 +79,7 @@ func TestMessage(t *testing.T) {
kaspaNet KaspaNet // Network to use for wire encoding kaspaNet KaspaNet // Network to use for wire encoding
bytes int // Expected num bytes read/written bytes int // Expected num bytes read/written
}{ }{
{msgVersion, msgVersion, pver, MainNet, 159}, {msgVersion, msgVersion, pver, MainNet, 153},
{msgVerack, msgVerack, pver, MainNet, 24}, {msgVerack, msgVerack, pver, MainNet, 24},
{msgGetAddr, msgGetAddr, pver, MainNet, 26}, {msgGetAddr, msgGetAddr, pver, MainNet, 26},
{msgAddr, msgAddr, pver, MainNet, 27}, {msgAddr, msgAddr, pver, MainNet, 27},