-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
version.go
96 lines (78 loc) · 2.78 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
package types
// GetCompatibleVersions returns an ordered set of compatible IBC versions for the
// caller chain's connection end.
func GetCompatibleVersions() []string {
return []string{"1.0.0"}
}
// LatestVersion gets the latest version of a connection protocol
//
// CONTRACT: version array MUST be already sorted.
func LatestVersion(versions []string) string {
if len(versions) == 0 {
return ""
}
return versions[len(versions)-1]
}
// PickVersion picks the counterparty latest version that is matches the list
// of compatible versions for the connection.
func PickVersion(counterpartyVersions, compatibleVersions []string) string {
n := len(counterpartyVersions)
m := len(compatibleVersions)
// aux hash maps to lookup already seen versions
counterpartyVerLookup := make(map[string]bool)
compatibleVerLookup := make(map[string]bool)
// versions suported
var supportedVersions []string
switch {
case n == 0 || m == 0:
return ""
case n == m:
for i := n - 1; i >= 0; i-- {
counterpartyVerLookup[counterpartyVersions[i]] = true
compatibleVerLookup[compatibleVersions[i]] = true
// check if we've seen any of the versions
if _, ok := compatibleVerLookup[counterpartyVersions[i]]; ok {
supportedVersions = append(supportedVersions, counterpartyVersions[i])
}
if _, ok := counterpartyVerLookup[compatibleVersions[i]]; ok {
// TODO: check if the version is already in the array
supportedVersions = append(supportedVersions, compatibleVersions[i])
}
}
case n > m:
for i := n - 1; i >= m; i-- {
counterpartyVerLookup[compatibleVersions[i]] = true
}
for i := m - 1; i >= 0; i-- {
counterpartyVerLookup[counterpartyVersions[i]] = true
compatibleVerLookup[compatibleVersions[i]] = true
// check if we've seen any of the versions
if _, ok := compatibleVerLookup[counterpartyVersions[i]]; ok {
supportedVersions = append(supportedVersions, counterpartyVersions[i])
}
if _, ok := counterpartyVerLookup[compatibleVersions[i]]; ok {
supportedVersions = append(supportedVersions, compatibleVersions[i])
}
}
case n < m:
for i := m - 1; i >= n; i-- {
compatibleVerLookup[compatibleVersions[i]] = true
}
for i := n - 1; i >= 0; i-- {
counterpartyVerLookup[counterpartyVersions[i]] = true
compatibleVerLookup[compatibleVersions[i]] = true
// check if we've seen any of the versions
if _, ok := compatibleVerLookup[counterpartyVersions[i]]; ok {
supportedVersions = append(supportedVersions, counterpartyVersions[i])
}
if _, ok := counterpartyVerLookup[compatibleVersions[i]]; ok {
supportedVersions = append(supportedVersions, compatibleVersions[i])
}
}
}
if len(supportedVersions) == 0 {
return ""
}
// TODO: compare latest version before appending
return supportedVersions[len(supportedVersions)-1]
}