-
Notifications
You must be signed in to change notification settings - Fork 19
/
version.go
142 lines (116 loc) · 4.39 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package version
import (
"errors"
"sort"
"strings"
vgversion "code.vegaprotocol.io/vega/libs/version"
coreversion "code.vegaprotocol.io/vega/version"
"code.vegaprotocol.io/vega/wallet/network"
)
var ErrCouldNotListNetworks = errors.New("couldn't list the networks")
// RequestVersionFn is the function in charge of retrieving the network version
// ran by the host lists.
type RequestVersionFn func(hosts []string) (string, error)
//go:generate go run github.com/golang/mock/mockgen -destination mocks/store_mock.go -package mocks code.vegaprotocol.io/vega/wallet/version NetworkStore
type NetworkStore interface {
ListNetworks() ([]string, error)
GetNetwork(string) (*network.Network, error)
}
func IsUnreleased() bool {
return vgversion.IsUnreleased(coreversion.Get())
}
type GetSoftwareVersionResponse struct {
Version string `json:"version"`
GitHash string `json:"gitHash"`
}
func GetSoftwareVersionInfo() *GetSoftwareVersionResponse {
response := &GetSoftwareVersionResponse{
Version: coreversion.Get(),
GitHash: coreversion.GetCommitHash(),
}
return response
}
type CheckSoftwareCompatibilityResponse struct {
NetworksCompatibility []NetworkCompatibility `json:"networksCompatibility"`
}
type NetworkCompatibility struct {
Network string `json:"network"`
IsCompatible bool `json:"isCompatible"`
RetrievedVersion string `json:"retrievedVersion"`
Error error `json:"error"`
}
func CheckSoftwareCompatibility(netStore NetworkStore, requestVersionFn RequestVersionFn) (*CheckSoftwareCompatibilityResponse, error) {
networks, err := netStore.ListNetworks()
// If there's an error we don't fail the command as the compatibility matrix
// is just a nice to have.
if err != nil {
// Best-effort, so we don't fail.
return nil, ErrCouldNotListNetworks
}
networksCompatibility := make([]NetworkCompatibility, 0, len(networks))
coreVersion := coreversion.Get()
coreVersionForComparison := onlyMajorAndMinor(coreVersion)
for _, net := range networks {
networkCompatibility := NetworkCompatibility{
Network: net,
}
netConfig, err := netStore.GetNetwork(net)
if err != nil {
// Best-effort, so we don't fail.
networkCompatibility.Error = err
networksCompatibility = append(networksCompatibility, networkCompatibility)
continue
}
if err := netConfig.EnsureCanConnectGRPCNode(); err != nil {
// Best-effort, so we don't fail.
networkCompatibility.Error = err
networksCompatibility = append(networksCompatibility, networkCompatibility)
continue
}
networkVersion, err := requestVersionFn(netConfig.API.GRPC.Hosts)
if err != nil {
// Best-effort, so we don't fail.
networkCompatibility.Error = err
networksCompatibility = append(networksCompatibility, networkCompatibility)
continue
}
networkCompatibility.RetrievedVersion = networkVersion
networkVersionForComparison := onlyMajorAndMinor(networkVersion)
if networkVersionForComparison != coreVersionForComparison {
networkCompatibility.IsCompatible = false
networksCompatibility = append(networksCompatibility, networkCompatibility)
continue
}
networkCompatibility.IsCompatible = true
networksCompatibility = append(networksCompatibility, networkCompatibility)
}
// Ensure the output is determinist.
sort.Slice(networksCompatibility, func(a, b int) bool {
return networksCompatibility[a].Network < networksCompatibility[b].Network
})
return &CheckSoftwareCompatibilityResponse{
NetworksCompatibility: networksCompatibility,
}, nil
}
func onlyMajorAndMinor(version string) string {
segments := strings.Split(version, ".")
if len(segments) < 2 {
// It doesn't seem to be a valid semantic version.
return version
}
return segments[0] + "." + segments[1]
}