OSDN Git Service

new repo
[bytom/vapor.git] / version / version.go
1 // package version provide the version info for the node, and also provide
2 // support for version compatibility check and update notification.
3 //
4 // The version format should follow Semantic Versioning (https://semver.org/):
5 // MAJOR.MINOR.PATCH
6 //  1. MAJOR version when you make incompatible API changes,
7 //  2. MINOR version when you add functionality in a backwards-compatible manner, and
8 //      3. PATCH version when you make backwards-compatible bug fixes.
9 //
10 // A pre-release version MAY be denoted by appending a hyphen and a series of
11 // dot separated identifiers immediately following the patch version.
12 // Examples:
13 // 1.0.0-alpha, 1.0.0-alpha.1, 1.0.0-0.3.7, 1.0.0-x.7.z.92.
14 // Precedence:
15 // 1. Pre-release versions have a lower precedence than the associated normal version!
16 //    Numeric identifiers always have lower precedence than non-numeric identifiers.
17 // 2. A larger set of pre-release fields has a higher precedence than a smaller set,
18 //    if all of the preceding identifiers are equal.
19 // 3. Example:
20 //    1.0.0-alpha < 1.0.0-alpha.1 < 1.0.0-alpha.beta < 1.0.0-beta < 1.0.0-beta.2 < 1.0.0-beta.11 < 1.0.0-rc.1 < 1.0.0.
21 //
22 // Build metadata MAY be denoted by appending a plus sign and a series of dot
23 // separated identifiers immediately following the patch or pre-release version.
24 // Build metadata SHOULD be ignored when determining version precedence. Thus
25 // two versions that differ only in the build metadata, have the same precedence.
26
27 package version
28
29 import (
30         "sync"
31
32         gover "github.com/hashicorp/go-version"
33         log "github.com/sirupsen/logrus"
34         "gopkg.in/fatih/set.v0"
35 )
36
37 const (
38         // If needing to edit the iota, please ensure the following:
39         // noUpdate = 0
40         // hasUpdate = 1
41         // hasMUpdate = 2
42         noUpdate uint16 = iota
43         hasUpdate
44         hasMUpdate
45 )
46
47 var (
48         // The full version string
49         Version = "1.0.7"
50         // GitCommit is set with --ldflags "-X main.gitCommit=$(git rev-parse HEAD)"
51         GitCommit string
52         Status    *UpdateStatus
53 )
54
55 func init() {
56         if GitCommit != "" {
57                 Version += "+" + GitCommit[:8]
58         }
59         Status = &UpdateStatus{
60                 maxVerSeen:    Version,
61                 notified:      false,
62                 seedSet:       set.New(),
63                 versionStatus: noUpdate,
64         }
65 }
66
67 type UpdateStatus struct {
68         sync.RWMutex
69         maxVerSeen    string
70         notified      bool
71         seedSet       *set.Set
72         versionStatus uint16
73 }
74
75 func (s *UpdateStatus) AddSeed(seedAddr string) {
76         s.Lock()
77         defer s.Unlock()
78         s.seedSet.Add(seedAddr)
79 }
80
81 // CheckUpdate checks whether there is a newer version to update.
82 // If there is, it set the "Status" variable to a proper value.
83 //      params:
84 //              localVerStr: the version of the node itself
85 //              remoteVerStr: the version received from a seed node.
86 //              remoteAddr: the version received from a seed node.
87 // current rule:
88 //              1. small update: seed version is higher than the node itself
89 //              2. significant update: seed mojor version is higher than the node itself
90 func (s *UpdateStatus) CheckUpdate(localVerStr string, remoteVerStr string, remoteAddr string) error {
91         s.Lock()
92         defer s.Unlock()
93
94         if s.notified || !s.seedSet.Has(remoteAddr) {
95                 return nil
96         }
97
98         localVersion, err := gover.NewVersion(localVerStr)
99         if err != nil {
100                 return err
101         }
102         remoteVersion, err := gover.NewVersion(remoteVerStr)
103         if err != nil {
104                 return err
105         }
106         if remoteVersion.GreaterThan(localVersion) {
107                 s.versionStatus = hasUpdate
108                 s.maxVerSeen = remoteVerStr
109         }
110         if remoteVersion.Segments()[0] > localVersion.Segments()[0] {
111                 s.versionStatus = hasMUpdate
112         }
113         if s.versionStatus != noUpdate {
114                 log.WithFields(log.Fields{
115                         "Current version": localVerStr,
116                         "Newer version":   remoteVerStr,
117                         "seed":            remoteAddr,
118                 }).Warn("Please update your bytomd via https://github.com/Bytom/vapor/releases/ or http://bytom.io/wallet/")
119                 s.notified = true
120         }
121         return nil
122 }
123
124 func (s *UpdateStatus) MaxVerSeen() string {
125         s.RLock()
126         defer s.RUnlock()
127         return s.maxVerSeen
128 }
129
130 func (s *UpdateStatus) VersionStatus() uint16 {
131         s.RLock()
132         defer s.RUnlock()
133         return s.versionStatus
134 }
135
136 // CompatibleWith checks whether the remote peer version is compatible with the
137 // node itself.
138 // RULES:
139 // | local |           remote           |
140 // |   -   |             -              |
141 // | 1.0.3 | same major&moinor version. |
142 // | 1.0.4 |     same major version.    |
143 func CompatibleWith(remoteVerStr string) (bool, error) {
144         localVersion, err := gover.NewVersion(Version)
145         if err != nil {
146                 return false, err
147         }
148         remoteVersion, err := gover.NewVersion(remoteVerStr)
149         if err != nil {
150                 return false, err
151         }
152         return (localVersion.Segments()[0] == remoteVersion.Segments()[0]), nil
153 }