-
Notifications
You must be signed in to change notification settings - Fork 205
/
supported_services.go
138 lines (127 loc) · 3.94 KB
/
supported_services.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
package types
type EndpointService struct {
ApiInterface string
Addon string
}
func (es *EndpointService) String() string {
st := es.ApiInterface
if es.Addon != "" {
st = st + " " + es.Addon
}
return st
}
func (endpoint *Endpoint) IsSupportedService(apiInterface string, addon string) bool {
for _, endpointApiInterface := range endpoint.ApiInterfaces {
// always support the base addons
if addon == "" && endpointApiInterface == apiInterface {
return true
}
if addon != "" {
// only chekc addons if the requested addon is not empty
for _, endpointAddon := range endpoint.Addons {
if addon == endpointAddon && endpointApiInterface == apiInterface {
return true
}
}
}
}
return false
}
// users are allowed to stake with an empty list of apiInterfaces, this code supports adding the default ones in that case
func (endpoint *Endpoint) SetDefaultApiInterfaces(requiredServices map[EndpointService]struct{}) {
if len(endpoint.ApiInterfaces) != 0 {
return
}
for endpointService := range requiredServices {
if endpointService.Addon != "" {
// only required ones are taken into account
continue
}
endpoint.ApiInterfaces = append(endpoint.ApiInterfaces, endpointService.ApiInterface)
}
}
// users are allowed to send apiInterfaces inside the addons list, this code supports that
func (endpoint *Endpoint) SetApiInterfacesFromAddons(allowedServices map[EndpointService]struct{}) {
newAddons := []string{}
existingApiInterfaces := map[string]struct{}{}
for _, apiInterface := range endpoint.ApiInterfaces {
existingApiInterfaces[apiInterface] = struct{}{}
}
for _, addon := range endpoint.Addons {
if _, ok := existingApiInterfaces[addon]; ok {
continue
}
service := EndpointService{
ApiInterface: addon, // we check if the addon is actually an ApiInterface
Addon: "", // intentionally don't set the addon here to check if it's an api interface
}
if _, ok := allowedServices[service]; ok {
// means this is an apiInterface and not an addon
endpoint.ApiInterfaces = append(endpoint.ApiInterfaces, addon)
} else {
newAddons = append(newAddons, addon)
}
}
endpoint.Addons = newAddons
}
func (endpoint *Endpoint) GetSupportedServices() (services []EndpointService) {
for _, apiInterface := range endpoint.ApiInterfaces {
// always support the base addons
service := EndpointService{
ApiInterface: apiInterface,
Addon: "",
}
services = append(services, service)
for _, addon := range endpoint.Addons {
if addon == apiInterface {
// will be used to remove an apiInterface from the base supported
continue
}
service := EndpointService{
ApiInterface: apiInterface,
Addon: addon,
}
services = append(services, service)
}
}
return services
}
func (stakeEntry *StakeEntry) GetEndpointsSupportingService(apiInterface string, addon string) (endpoints []*Endpoint) {
for idx, endpoint := range stakeEntry.Endpoints {
if endpoint.IsSupportedService(apiInterface, addon) {
endpoints = append(endpoints, &stakeEntry.Endpoints[idx])
}
}
return endpoints
}
func (stakeEntry *StakeEntry) GetSupportedServices() (services []EndpointService) {
existing := map[EndpointService]struct{}{}
for _, endpoint := range stakeEntry.Endpoints {
for _, apiInterface := range endpoint.ApiInterfaces {
// always support the base addons
service := EndpointService{
ApiInterface: apiInterface,
Addon: "",
}
if _, ok := existing[service]; !ok {
services = append(services, service)
}
existing[service] = struct{}{}
for _, addon := range endpoint.Addons {
if addon == apiInterface {
// will be used to remove an apiInterface from the base supported
continue
}
service := EndpointService{
ApiInterface: apiInterface,
Addon: addon,
}
if _, ok := existing[service]; !ok {
services = append(services, service)
}
existing[service] = struct{}{}
}
}
}
return services
}