forked from lavanet/lava
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspec_validator.go
195 lines (171 loc) · 5.91 KB
/
spec_validator.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package rpcprovider
import (
"context"
"sync"
"time"
"github.com/lavanet/lava/protocol/chainlib"
"github.com/lavanet/lava/protocol/lavasession"
"github.com/lavanet/lava/utils"
spectypes "github.com/lavanet/lava/x/spec/types"
)
const (
SpecValidationIntervalFlagName = "spec-validation-interval"
SpecValidationIntervalDisabledChainsFlagName = "spec-validation-interval-disabled-chains"
)
var (
SpecValidationInterval = 3 * time.Hour
SpecValidationIntervalDisabledChains = 3 * time.Minute
)
type SpecValidator struct {
lock sync.RWMutex
chainFetchers map[string][]*chainlib.ChainFetcherIf // key is chainId
providerListeners map[string]*ProviderListener // key is address
skipValidations map[string]struct{} // key is a validation name to skip
}
func NewSpecValidator() *SpecValidator {
return &SpecValidator{
lock: sync.RWMutex{},
chainFetchers: make(map[string][]*chainlib.ChainFetcherIf),
providerListeners: make(map[string]*ProviderListener),
skipValidations: make(map[string]struct{}),
}
}
func (sv *SpecValidator) GetUniqueName() string {
return "spec_validator"
}
func (sv *SpecValidator) Start(ctx context.Context) {
go sv.validateAllChainsLoop(ctx)
}
func (sv *SpecValidator) validateAllChainsLoop(ctx context.Context) {
validationTicker := time.NewTicker(SpecValidationInterval)
validationTickerForDisabled := time.NewTicker(SpecValidationIntervalDisabledChains)
for {
select {
case <-validationTicker.C:
func() {
sv.lock.Lock()
defer sv.lock.Unlock()
sv.validateAllChains(ctx)
// we just ran validate on all chains no reason to do disabled chains in the next interval
validationTickerForDisabled.Reset(SpecValidationIntervalDisabledChains)
}()
case <-validationTickerForDisabled.C:
func() {
sv.lock.Lock()
defer sv.lock.Unlock()
sv.validateAllDisabledChains(ctx)
}()
case <-ctx.Done():
validationTicker.Stop()
return
}
}
}
func (sv *SpecValidator) AddChainFetcher(ctx context.Context, chainFetcher *chainlib.ChainFetcherIf, chainId string) error {
sv.lock.Lock()
defer sv.lock.Unlock()
err := (*chainFetcher).Validate(ctx)
if err != nil {
return err
}
if _, found := sv.chainFetchers[chainId]; !found {
sv.chainFetchers[chainId] = []*chainlib.ChainFetcherIf{}
}
sv.chainFetchers[chainId] = append(sv.chainFetchers[chainId], chainFetcher)
return nil
}
func (sv *SpecValidator) AddRPCProviderListener(address string, providerListener *ProviderListener) {
sv.lock.Lock()
defer sv.lock.Unlock()
sv.providerListeners[address] = providerListener
}
func (sv *SpecValidator) VerifySpec(spec spectypes.Spec) {
sv.lock.Lock()
defer sv.lock.Unlock()
chainId := spec.Index
if _, found := sv.chainFetchers[chainId]; !found {
utils.LavaFormatError("Could not find chainFetchers with given chainId", nil, utils.Attribute{Key: "chainId", Value: chainId})
return
}
utils.LavaFormatDebug("Running spec verification for chainId", utils.LogAttr("chainId", chainId))
sv.validateChain(context.Background(), chainId)
}
func (sv *SpecValidator) Active() bool {
return true
}
func (sv *SpecValidator) getRpcProviderEndpointFromChainFetcher(chainFetcher *chainlib.ChainFetcherIf) *lavasession.RPCEndpoint {
endpoint := (*chainFetcher).FetchEndpoint()
return &lavasession.RPCEndpoint{
NetworkAddress: endpoint.NetworkAddress.Address,
ChainID: endpoint.ChainID,
ApiInterface: endpoint.ApiInterface,
Geolocation: endpoint.Geolocation,
}
}
func (sv *SpecValidator) validateAllChains(ctx context.Context) {
for chainId := range sv.chainFetchers {
sv.validateChain(ctx, chainId)
}
}
func (sv *SpecValidator) validateAllDisabledChains(ctx context.Context) {
for chainId := range sv.getDisabledChains(ctx) {
sv.validateChain(ctx, chainId)
}
}
func (sv *SpecValidator) getDisabledChains(ctx context.Context) map[string]struct{} {
disabledChains := map[string]struct{}{}
for _, chainFetchersList := range sv.chainFetchers {
for _, chainFetcher := range chainFetchersList {
rpcEndpoint := sv.getRpcProviderEndpointFromChainFetcher(chainFetcher)
providerListener, found := sv.providerListeners[rpcEndpoint.NetworkAddress]
if !found {
continue
}
relayReceiver, found := providerListener.relayServer.relayReceivers[rpcEndpoint.Key()]
if !found {
continue
}
if !relayReceiver.enabled {
disabledChains[rpcEndpoint.ChainID] = struct{}{}
}
}
}
return disabledChains
}
func (sv *SpecValidator) validateChain(ctx context.Context, chainId string) {
errors := []error{}
for _, chainFetcher := range sv.chainFetchers[chainId] {
err := (*chainFetcher).Validate(ctx)
rpcEndpoint := sv.getRpcProviderEndpointFromChainFetcher(chainFetcher)
providerListener, found := sv.providerListeners[rpcEndpoint.NetworkAddress]
if !found {
if err != nil {
utils.LavaFormatWarning("Verification failed for endpoint", nil, utils.Attribute{Key: "endpoint", Value: rpcEndpoint})
errors = append(errors, err)
}
continue
}
relayReceiver, found := providerListener.relayServer.relayReceivers[rpcEndpoint.Key()]
if !found {
if err != nil {
utils.LavaFormatWarning("Verification failed for endpoint", nil, utils.Attribute{Key: "endpoint", Value: rpcEndpoint})
errors = append(errors, err)
}
continue
}
if err != nil {
relayReceiver.enabled = false
utils.LavaFormatError("[-] Verification failed for endpoint. Disabling endpoint.", nil, utils.Attribute{Key: "endpoint", Value: rpcEndpoint})
errors = append(errors, err)
} else if !relayReceiver.enabled {
relayReceiver.enabled = true
utils.LavaFormatError("[+] Verification passed for disabled endpoint. Enabling endpoint.", nil, utils.Attribute{Key: "endpoint", Value: rpcEndpoint})
}
}
if len(errors) > 0 {
utils.LavaFormatError("Validation chainId failed with errors", nil,
utils.Attribute{Key: "chainId", Value: chainId},
utils.Attribute{Key: "errors", Value: errors},
)
}
}