@@ -26,6 +26,31 @@ func (nf *noFirewall) Validate(remotePeerPublicKey *operator.PublicKey) error {
26
26
return nil
27
27
}
28
28
29
+ // AllowList represents a list of operator public keys that are not checked
30
+ // against the firewall rules and are always valid peers.
31
+ type AllowList struct {
32
+ allowedPublicKeys map [string ]bool
33
+ }
34
+
35
+ // NewAllowList creates a new firewall's allowlist based on the given public
36
+ // key list.
37
+ func NewAllowList (operatorPublicKeys []* operator.PublicKey ) * AllowList {
38
+ allowedPublicKeys := make (map [string ]bool , len (operatorPublicKeys ))
39
+
40
+ for _ , operatorPublicKey := range operatorPublicKeys {
41
+ allowedPublicKeys [operatorPublicKey .String ()] = true
42
+ }
43
+
44
+ return & AllowList {allowedPublicKeys }
45
+ }
46
+
47
+ func (al * AllowList ) Contains (operatorPublicKey * operator.PublicKey ) bool {
48
+ return al .allowedPublicKeys [operatorPublicKey .String ()]
49
+ }
50
+
51
+ // EmptyAllowList represents an empty firewall allowlist.
52
+ var EmptyAllowList = NewAllowList ([]* operator.PublicKey {})
53
+
29
54
const (
30
55
// PositiveIsRecognizedCachePeriod is the time period the cache maintains
31
56
// the positive result of the last `IsRecognized` checks.
@@ -42,30 +67,38 @@ var errNotRecognized = fmt.Errorf(
42
67
"remote peer has not been recognized by any application" ,
43
68
)
44
69
45
- func AnyApplicationPolicy (applications []Application ) net.Firewall {
70
+ func AnyApplicationPolicy (
71
+ applications []Application ,
72
+ allowList * AllowList ,
73
+ ) net.Firewall {
46
74
return & anyApplicationPolicy {
47
75
applications : applications ,
76
+ allowList : allowList ,
48
77
positiveResultCache : cache .NewTimeCache (PositiveIsRecognizedCachePeriod ),
49
78
negativeResultCache : cache .NewTimeCache (NegativeIsRecognizedCachePeriod ),
50
79
}
51
80
}
52
81
53
82
type anyApplicationPolicy struct {
54
83
applications []Application
84
+ allowList * AllowList
55
85
positiveResultCache * cache.TimeCache
56
86
negativeResultCache * cache.TimeCache
57
87
}
58
88
59
89
// Validate checks whether the given operator meets the conditions to join
60
- // the network. Validate iterates over a list of applications and if any of them
61
- // recognizes the operator as eligible, it can join the network. Nil is returned
62
- // on a successful validation, error is returned if none of the applications
63
- // validates the operator successfully. Due to performance reasons the results
64
- // of validations are stored in a cache for a certain amount of time.
90
+ // the network. The operator can join the network if it is an allowlisted node
91
+ // or it is a non-allowlisted node but it is recognized as eligible by any of
92
+ // the applications. Nil is returned on a successful validation, error otherwise.
93
+ // Due to performance reasons, the results of validations for non-allowlisted
94
+ // nodes are stored in a cache for a certain amount of time.
65
95
func (aap * anyApplicationPolicy ) Validate (
66
96
remotePeerPublicKey * operator.PublicKey ,
67
97
) error {
68
- remotePeerPublicKeyHex := remotePeerPublicKey .String ()
98
+ // If the peer is on the allowlist, consider it validated.
99
+ if aap .allowList .Contains (remotePeerPublicKey ) {
100
+ return nil
101
+ }
69
102
70
103
// First, check in the in-memory time caches to minimize hits to the ETH client.
71
104
// If the Keep client with the given chain address is in the positive result
@@ -78,6 +111,8 @@ func (aap *anyApplicationPolicy) Validate(
78
111
aap .positiveResultCache .Sweep ()
79
112
aap .negativeResultCache .Sweep ()
80
113
114
+ remotePeerPublicKeyHex := remotePeerPublicKey .String ()
115
+
81
116
if aap .positiveResultCache .Has (remotePeerPublicKeyHex ) {
82
117
return nil
83
118
}
0 commit comments