-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathserver.go
More file actions
338 lines (294 loc) · 11 KB
/
server.go
File metadata and controls
338 lines (294 loc) · 11 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package executable
import (
"context"
"encoding/hex"
"errors"
"fmt"
"sync"
"sync/atomic"
"time"
commoncap "github.com/smartcontractkit/chainlink-common/pkg/capabilities"
"github.com/smartcontractkit/chainlink-common/pkg/logger"
"github.com/smartcontractkit/chainlink-common/pkg/services"
"github.com/smartcontractkit/chainlink/v2/core/capabilities/remote"
"github.com/smartcontractkit/chainlink/v2/core/capabilities/remote/executable/request"
"github.com/smartcontractkit/chainlink/v2/core/capabilities/remote/types"
"github.com/smartcontractkit/chainlink/v2/core/capabilities/validation"
p2ptypes "github.com/smartcontractkit/chainlink/v2/core/services/p2p/types"
)
// server manages all external users of a local executable capability.
// Its responsibilities are:
// 1. Manage requests from external nodes executing the executable capability once sufficient requests are received.
// 2. Send out responses produced by an underlying capability to all requesters.
//
// server communicates with corresponding client on remote nodes.
type server struct {
services.StateMachine
capabilityID string
capMethodName string
peerID p2ptypes.PeerID
dispatcher types.Dispatcher
cfg atomic.Pointer[dynamicServerConfig]
lggr logger.Logger
requestIDToRequest map[string]requestAndMsgID
// Used to detect messages with the same message id but different payloads
messageIDToRequestIDsCount map[string]map[string]int
receiveLock sync.Mutex
stopCh services.StopChan
wg sync.WaitGroup
parallelExecutor *parallelExecutor
}
type dynamicServerConfig struct {
remoteExecutableConfig *commoncap.RemoteExecutableConfig
hasher types.MessageHasher
underlying commoncap.ExecutableCapability
capInfo commoncap.CapabilityInfo
localDonInfo commoncap.DON
workflowDONs map[uint32]commoncap.DON
}
type Server interface {
types.Receiver
services.Service
SetConfig(remoteExecutableConfig *commoncap.RemoteExecutableConfig, underlying commoncap.ExecutableCapability,
capInfo commoncap.CapabilityInfo, localDonInfo commoncap.DON, workflowDONs map[uint32]commoncap.DON,
messageHasher types.MessageHasher) error
}
var _ Server = &server{}
var _ types.Receiver = &server{}
var _ services.Service = &server{}
type requestAndMsgID struct {
request *request.ServerRequest
messageID string
}
func NewServer(capabilityID, methodName string, peerID p2ptypes.PeerID, dispatcher types.Dispatcher, lggr logger.Logger) *server {
return &server{
capabilityID: capabilityID,
capMethodName: methodName,
peerID: peerID,
dispatcher: dispatcher,
lggr: logger.With(logger.Named(lggr, "ExecutableCapabilityServer"), "capabilityID", capabilityID, "capMethodName", methodName),
requestIDToRequest: map[string]requestAndMsgID{},
messageIDToRequestIDsCount: map[string]map[string]int{},
stopCh: make(services.StopChan),
}
}
// SetConfig sets the remote server configuration dynamically
func (r *server) SetConfig(remoteExecutableConfig *commoncap.RemoteExecutableConfig, underlying commoncap.ExecutableCapability,
capInfo commoncap.CapabilityInfo, localDonInfo commoncap.DON, workflowDONs map[uint32]commoncap.DON, messageHasher types.MessageHasher) error {
currCfg := r.cfg.Load()
if remoteExecutableConfig == nil {
r.lggr.Info("no remote config provided, using default values")
remoteExecutableConfig = &commoncap.RemoteExecutableConfig{}
}
if messageHasher == nil {
r.lggr.Warn("no message hasher provided, using default V1 hasher")
messageHasher = NewV1Hasher(remoteExecutableConfig.RequestHashExcludedAttributes)
}
if capInfo.ID == "" || capInfo.ID != r.capabilityID {
return fmt.Errorf("capability info provided does not match the server's capabilityID: %s != %s", capInfo.ID, r.capabilityID)
}
if underlying == nil {
return errors.New("underlying capability cannot be nil")
}
if len(localDonInfo.Members) == 0 {
return errors.New("empty localDonInfo provided")
}
if len(workflowDONs) == 0 {
return errors.New("empty workflowDONs provided")
}
if remoteExecutableConfig.RequestTimeout <= 0 {
return errors.New("cfg.RequestTimeout must be positive")
}
if remoteExecutableConfig.ServerMaxParallelRequests <= 0 {
return errors.New("cfg.ServerMaxParallelRequests must be positive")
}
if currCfg != nil && currCfg.remoteExecutableConfig != nil &&
currCfg.remoteExecutableConfig.ServerMaxParallelRequests > 0 &&
remoteExecutableConfig.ServerMaxParallelRequests != currCfg.remoteExecutableConfig.ServerMaxParallelRequests {
r.lggr.Warn("ServerMaxParallelRequests changed but it won't be applied until node restart")
}
// always replace the whole dynamicServerConfig object to avoid inconsistent state
r.cfg.Store(&dynamicServerConfig{
remoteExecutableConfig: remoteExecutableConfig,
hasher: messageHasher,
underlying: underlying,
capInfo: capInfo,
localDonInfo: localDonInfo,
workflowDONs: workflowDONs,
})
return nil
}
func (r *server) Start(ctx context.Context) error {
return r.StartOnce(r.Name(), func() error {
cfg := r.cfg.Load()
// Validate that all required fields are set before starting
if cfg == nil {
return errors.New("config not set - call SetConfig() before Start()")
}
if cfg.remoteExecutableConfig == nil {
return errors.New("remote executable config not set - call SetConfig() before Start()")
}
if cfg.underlying == nil {
return errors.New("underlying capability not set - call SetConfig() before Start()")
}
if cfg.capInfo.ID == "" {
return errors.New("capability info not set - call SetConfig() before Start()")
}
if len(cfg.localDonInfo.Members) == 0 {
return errors.New("local DON info not set - call SetConfig() before Start()")
}
if cfg.remoteExecutableConfig.RequestTimeout <= 0 {
return errors.New("cfg.RequestTimeout not set - call SetConfig() before Start()")
}
if cfg.remoteExecutableConfig.ServerMaxParallelRequests <= 0 {
return errors.New("cfg.ServerMaxParallelRequests not set - call SetConfig() before Start()")
}
if r.dispatcher == nil {
return errors.New("dispatcher set to nil, cannot start server")
}
// Initialize parallel executor with the configured max parallel requests
r.parallelExecutor = newParallelExecutor(int(cfg.remoteExecutableConfig.ServerMaxParallelRequests))
r.wg.Add(1)
go func() {
defer r.wg.Done()
ticker := time.NewTicker(getServerTickerInterval(cfg))
defer ticker.Stop()
r.lggr.Info("ExecutableCapabilityServer started")
for {
select {
case <-r.stopCh:
return
case <-ticker.C:
ticker.Reset(getServerTickerInterval(cfg))
r.expireRequests()
}
}
}()
err := r.parallelExecutor.Start(ctx)
if err != nil {
return fmt.Errorf("failed to start parallel executor: %w", err)
}
return nil
})
}
func getServerTickerInterval(cfg *dynamicServerConfig) time.Duration {
if cfg.remoteExecutableConfig.RequestTimeout > 0 {
return cfg.remoteExecutableConfig.RequestTimeout
}
return defaultExpiryCheckInterval
}
func (r *server) Close() error {
return r.StopOnce(r.Name(), func() error {
close(r.stopCh)
r.wg.Wait()
if r.parallelExecutor != nil {
err := r.parallelExecutor.Close()
if err != nil {
return fmt.Errorf("failed to close parallel executor: %w", err)
}
}
r.lggr.Info("ExecutableCapabilityServer closed")
return nil
})
}
func (r *server) expireRequests() {
r.receiveLock.Lock()
defer r.receiveLock.Unlock()
for requestID, executeReq := range r.requestIDToRequest {
if executeReq.request.Expired() {
ctx, cancelFn := r.stopCh.NewCtx()
err := executeReq.request.Cancel(ctx, types.Error_TIMEOUT, "request expired by executable server")
cancelFn()
if err != nil {
r.lggr.Errorw("failed to cancel request", "request", executeReq, "err", err)
}
}
if executeReq.request.Evictable(commoncap.DefaultExecutableRequestTimeout) {
delete(r.requestIDToRequest, requestID)
delete(r.messageIDToRequestIDsCount, executeReq.messageID)
}
}
}
func (r *server) Receive(ctx context.Context, msg *types.MessageBody) {
cfg := r.cfg.Load()
if cfg == nil {
r.lggr.Errorw("config not set, cannot process request")
return
}
r.receiveLock.Lock()
defer r.receiveLock.Unlock()
switch msg.Method {
case types.MethodExecute:
default:
r.lggr.Errorw("received request for unsupported method type", "method", remote.SanitizeLogString(msg.Method))
return
}
messageID, err := GetMessageID(msg)
if err != nil {
r.lggr.Errorw("invalid message id", "err", err, "id", remote.SanitizeLogString(string(msg.MessageId)))
return
}
msgHash, err := cfg.hasher.Hash(msg)
if err != nil {
r.lggr.Errorw("failed to get message hash", "err", err)
return
}
// A request is uniquely identified by the message id and the hash of the payload to prevent a malicious
// actor from sending a different payload with the same message id
requestID := messageID + hex.EncodeToString(msgHash[:])
r.lggr.Debugw("received request", "msgId", msg.MessageId, "requestID", requestID)
if requestIDs, ok := r.messageIDToRequestIDsCount[messageID]; ok {
requestIDs[requestID]++
} else {
r.messageIDToRequestIDsCount[messageID] = map[string]int{requestID: 1}
}
requestIDs := r.messageIDToRequestIDsCount[messageID]
if len(requestIDs) > 1 {
// This is a potential attack vector as well as a situation that will occur if the client is sending non-deterministic payloads
// so a warning is logged
r.lggr.Warnw("received messages with the same id and different payloads", "messageID", messageID, "lenRequestIDs", len(requestIDs))
}
if _, ok := r.requestIDToRequest[requestID]; !ok {
callingDon, ok := cfg.workflowDONs[msg.CallerDonId]
if !ok {
r.lggr.Errorw("received request from unregistered don", "donId", msg.CallerDonId)
return
}
sr, ierr := request.NewServerRequest(cfg.underlying, msg.Method, cfg.capInfo.ID, cfg.localDonInfo.ID, r.peerID,
callingDon, messageID, r.dispatcher, cfg.remoteExecutableConfig.RequestTimeout, r.capMethodName, r.lggr)
if ierr != nil {
r.lggr.Errorw("failed to instantiate server request", "err", ierr)
return
}
r.requestIDToRequest[requestID] = requestAndMsgID{
request: sr,
messageID: messageID,
}
}
reqAndMsgID := r.requestIDToRequest[requestID]
if executeTaskErr := r.parallelExecutor.ExecuteTask(ctx,
func(ctx context.Context) {
err = reqAndMsgID.request.OnMessage(ctx, msg)
if err != nil {
r.lggr.Errorw("failed to execute on message", "messageID", reqAndMsgID.messageID, "err", err)
}
}); executeTaskErr != nil {
r.lggr.Errorw("failed to execute on message task", "messageID", messageID, "err", executeTaskErr)
}
}
func GetMessageID(msg *types.MessageBody) (string, error) {
idStr := string(msg.MessageId)
if !validation.IsValidID(idStr) {
return "", errors.New("invalid message id")
}
return idStr, nil
}
func (r *server) Ready() error {
return nil
}
func (r *server) HealthReport() map[string]error {
return nil
}
func (r *server) Name() string {
return r.lggr.Name()
}