-
Notifications
You must be signed in to change notification settings - Fork 671
/
compatibility.go
81 lines (65 loc) · 1.92 KB
/
compatibility.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
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package version
import (
"errors"
"time"
"github.com/ava-labs/avalanchego/utils/timer/mockable"
)
var (
errIncompatible = errors.New("peers version is incompatible")
_ Compatibility = (*compatibility)(nil)
)
// Compatibility a utility for checking the compatibility of peer versions
type Compatibility interface {
// Returns the local version
Version() *Application
// Returns nil if the provided version is compatible with the local version.
// This means that the version is connectable and that consensus messages
// can be made to them.
Compatible(*Application) error
}
type compatibility struct {
version *Application
minCompatable *Application
minCompatableTime time.Time
prevMinCompatable *Application
clock mockable.Clock
}
// NewCompatibility returns a compatibility checker with the provided options
func NewCompatibility(
version *Application,
minCompatable *Application,
minCompatableTime time.Time,
prevMinCompatable *Application,
) Compatibility {
return &compatibility{
version: version,
minCompatable: minCompatable,
minCompatableTime: minCompatableTime,
prevMinCompatable: prevMinCompatable,
}
}
func (c *compatibility) Version() *Application {
return c.version
}
func (c *compatibility) Compatible(peer *Application) error {
if err := c.version.Compatible(peer); err != nil {
return err
}
if !peer.Before(c.minCompatable) {
// The peer is at least the minimum compatible version.
return nil
}
// The peer is going to be marked as incompatible at [c.minCompatableTime].
now := c.clock.Time()
if !now.Before(c.minCompatableTime) {
return errIncompatible
}
// The minCompatable check isn't being enforced yet.
if !peer.Before(c.prevMinCompatable) {
// The peer is at least the previous minimum compatible version.
return nil
}
return errIncompatible
}