-
Notifications
You must be signed in to change notification settings - Fork 211
/
version.go
62 lines (53 loc) · 1.79 KB
/
version.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Package version includes methods to compare versions between nodes.
package version
import (
"errors"
"fmt"
"github.com/spacemeshos/go-spacemesh/log"
"strconv"
"strings"
)
// CheckNodeVersion checks if a request version is more recent then the given min version. returns a bool and an error
func CheckNodeVersion(reqVersion string, minVersion string) (bool, error) {
// TODO : semantic versioning comparison is a pain, refine this or use a lib
splt := strings.Split(reqVersion, "/")
if len(splt) > 2 {
return false, errors.New("invalid client version")
}
if len(splt) == 2 {
reqVersion = splt[1]
}
// if same version string don't do anything
if reqVersion == minVersion {
return true, nil
}
// take each version number and convert to ints
reqsplit, minsplit := strings.Split(reqVersion, "."), strings.Split(minVersion, ".")
if len(reqsplit) != 3 || len(minsplit) != 3 {
return false, fmt.Errorf("one of provided versions isn't valid %v / %v", reqVersion, minVersion)
}
numa, numb := [3]int64{}, [3]int64{}
for i := 0; i < 3; i++ {
var err error
numa[i], err = strconv.ParseInt(reqsplit[i], 10, 8)
if err != nil {
return false, fmt.Errorf(" could not read version number %v", reqVersion)
}
numb[i], err = strconv.ParseInt(minsplit[i], 10, 8)
if err != nil {
return false, fmt.Errorf(" could not read version number %v", reqVersion)
}
}
for i := 0; i < 3; i++ {
if numa[i] > numb[i] {
return true, nil
}
if numa[i] != numb[i] {
return false, nil
}
}
// this should not happen, it means the versions perfectly match but we already tested that above
log.With().Error("error comparing node version strings",
log.String("reqVersion", reqVersion), log.String("minVersion", minVersion))
return false, errors.New("error comparing node version strings")
}