-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnode.go
More file actions
363 lines (317 loc) · 10.6 KB
/
node.go
File metadata and controls
363 lines (317 loc) · 10.6 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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package multinode
import (
"context"
"errors"
"fmt"
"net/url"
"sync"
"time"
"github.com/smartcontractkit/chainlink-common/pkg/logger"
"github.com/smartcontractkit/chainlink-common/pkg/services"
)
const QueryTimeout = 10 * time.Second
var errInvalidChainID = errors.New("invalid chain id")
type NodeConfig interface {
PollFailureThreshold() uint32
PollSuccessThreshold() uint32
PollInterval() time.Duration
SelectionMode() string
SyncThreshold() uint32
NodeIsSyncingEnabled() bool
FinalizedBlockPollInterval() time.Duration
EnforceRepeatableRead() bool
DeathDeclarationDelay() time.Duration
NewHeadsPollInterval() time.Duration
VerifyChainID() bool
}
type ChainConfig interface {
NodeNoNewHeadsThreshold() time.Duration
NoNewFinalizedHeadsThreshold() time.Duration
FinalityDepth() uint32
FinalityTagEnabled() bool
FinalizedBlockOffset() uint32
}
// FinalizedStateCheckConfig is an optional interface for enabling finalized state availability checking.
type FinalizedStateCheckConfig interface {
FinalizedStateCheckFailureThreshold() uint32
}
// FinalizedStateChecker is an optional interface for RPCClients that support finalized state checks.
type FinalizedStateChecker interface {
CheckFinalizedStateAvailability(ctx context.Context) error
}
type nodeMetrics interface {
IncrementNodeVerifies(ctx context.Context, nodeName string)
IncrementNodeVerifiesFailed(ctx context.Context, nodeName string)
IncrementNodeVerifiesSuccess(ctx context.Context, nodeName string)
IncrementNodeTransitionsToAlive(ctx context.Context, nodeName string)
IncrementNodeTransitionsToInSync(ctx context.Context, nodeName string)
IncrementNodeTransitionsToOutOfSync(ctx context.Context, nodeName string)
IncrementNodeTransitionsToUnreachable(ctx context.Context, nodeName string)
IncrementNodeTransitionsToInvalidChainID(ctx context.Context, nodeName string)
IncrementNodeTransitionsToUnusable(ctx context.Context, nodeName string)
IncrementNodeTransitionsToSyncing(ctx context.Context, nodeName string)
IncrementNodeTransitionsToFinalizedStateNotAvailable(ctx context.Context, nodeName string)
RecordNodeClientVersion(ctx context.Context, nodeName string, version string)
SetHighestSeenBlock(ctx context.Context, nodeName string, blockNumber int64)
SetHighestFinalizedBlock(ctx context.Context, nodeName string, blockNumber int64)
IncrementSeenBlocks(ctx context.Context, nodeName string)
IncrementPolls(ctx context.Context, nodeName string)
IncrementPollsFailed(ctx context.Context, nodeName string)
IncrementPollsSuccess(ctx context.Context, nodeName string)
IncrementFinalizedStateFailed(ctx context.Context, nodeName string)
}
type Node[
CHAIN_ID ID,
RPC any,
] interface {
// State returns most accurate state of the Node on the moment of call.
// While some of the checks may be performed in the background and State may return cached value, critical, like
// `FinalizedBlockOutOfSync`, must be executed upon every call.
State() nodeState
// StateAndLatest returns nodeState with the latest ChainInfo observed by Node during current lifecycle.
StateAndLatest() (nodeState, ChainInfo)
// HighestUserObservations - returns highest ChainInfo ever observed by underlying RPC excluding results of health check requests
HighestUserObservations() ChainInfo
SetPoolChainInfoProvider(PoolChainInfoProvider)
// Name is a unique identifier for this node.
Name() string
// String - returns string representation of the node, useful for debugging (name + URLS used to connect to the RPC)
String() string
RPC() RPC
// UnsubscribeAllExceptAliveLoop - closes all subscriptions except the aliveLoop subscription
UnsubscribeAllExceptAliveLoop()
ConfiguredChainID() CHAIN_ID
// Order - returns priority order configured for the RPC
Order() int32
// Start - starts health checks
Start(context.Context) error
Close() error
}
type node[
CHAIN_ID ID,
HEAD Head,
RPC RPCClient[CHAIN_ID, HEAD],
] struct {
services.StateMachine
lfcLog logger.Logger
name string
id int
chainID CHAIN_ID
nodePoolCfg NodeConfig
chainCfg ChainConfig
order int32
chainFamily string
metrics nodeMetrics
ws *url.URL
http *url.URL
rpc RPC
isLoadBalancedRPC bool
stateMu sync.RWMutex // protects state* fields
state nodeState
poolInfoProvider PoolChainInfoProvider
stopCh services.StopChan
// wg waits for subsidiary goroutines
wg sync.WaitGroup
healthCheckSubs []Subscription
}
func NewNode[
CHAIN_ID ID,
HEAD Head,
RPC RPCClient[CHAIN_ID, HEAD],
](
nodeCfg NodeConfig,
chainCfg ChainConfig,
lggr logger.Logger,
metrics nodeMetrics,
wsuri *url.URL,
httpuri *url.URL,
name string,
id int,
chainID CHAIN_ID,
nodeOrder int32,
rpc RPC,
chainFamily string,
isLoadBalancedRPC bool,
) Node[CHAIN_ID, RPC] {
n := new(node[CHAIN_ID, HEAD, RPC])
n.name = name
n.id = id
n.chainID = chainID
n.nodePoolCfg = nodeCfg
n.chainCfg = chainCfg
n.order = nodeOrder
n.metrics = metrics
if wsuri != nil {
n.ws = wsuri
}
if httpuri != nil {
n.http = httpuri
}
n.stopCh = make(services.StopChan)
lggr = logger.Named(lggr, "Node")
lggr = logger.With(lggr,
"nodeTier", Primary.String(),
"nodeName", name,
"node", n.String(),
"chainID", chainID,
"nodeOrder", n.order,
)
n.lfcLog = logger.Named(lggr, "Lifecycle")
n.rpc = rpc
n.isLoadBalancedRPC = isLoadBalancedRPC
n.chainFamily = chainFamily
return n
}
func (n *node[CHAIN_ID, HEAD, RPC]) String() string {
s := fmt.Sprintf("(%s)%s", Primary.String(), n.name)
if n.ws != nil {
s += fmt.Sprintf(":%s", n.ws.String())
}
if n.http != nil {
s += fmt.Sprintf(":%s", n.http.String())
}
return s
}
func (n *node[CHAIN_ID, HEAD, RPC]) ConfiguredChainID() (chainID CHAIN_ID) {
return n.chainID
}
func (n *node[CHAIN_ID, HEAD, RPC]) Name() string {
return n.name
}
func (n *node[CHAIN_ID, HEAD, RPC]) RPC() RPC {
return n.rpc
}
// unsubscribeAllExceptAliveLoop is not thread-safe; it should only be called
// while holding the stateMu lock.
func (n *node[CHAIN_ID, HEAD, RPC]) unsubscribeAllExceptAliveLoop() {
n.rpc.UnsubscribeAllExcept(n.healthCheckSubs...)
}
func (n *node[CHAIN_ID, HEAD, RPC]) UnsubscribeAllExceptAliveLoop() {
n.stateMu.Lock()
defer n.stateMu.Unlock()
n.unsubscribeAllExceptAliveLoop()
}
func (n *node[CHAIN_ID, HEAD, RPC]) Close() error {
return n.StopOnce(n.name, n.close)
}
func (n *node[CHAIN_ID, HEAD, RPC]) close() error {
defer func() {
n.wg.Wait()
n.rpc.Close()
}()
n.stateMu.Lock()
defer n.stateMu.Unlock()
close(n.stopCh)
n.state = nodeStateClosed
return nil
}
// Start dials and verifies the node
// Should only be called once in a node's lifecycle
// Return value is necessary to conform to interface but this will never
// actually return an error.
func (n *node[CHAIN_ID, HEAD, RPC]) Start(startCtx context.Context) error {
return n.StartOnce(n.name, func() error {
n.wg.Add(1)
go n.start()
return nil
})
}
// start initially dials the node and verifies chain ID
// This spins off lifecycle goroutines.
// Not thread-safe.
// Node lifecycle is synchronous: only one goroutine should be running at a
// time.
func (n *node[CHAIN_ID, HEAD, RPC]) start() {
defer n.wg.Done()
if n.State() != nodeStateUndialed {
panic(fmt.Sprintf("cannot dial node with state %v", n.state))
}
ctx, cancel := n.stopCh.NewCtx()
defer cancel()
if err := n.rpc.Dial(ctx); err != nil {
n.lfcLog.Errorw("Dial failed: Node is unreachable", "err", err)
n.declareUnreachable()
return
}
n.setState(nodeStateDialed)
state := n.verifyConn(ctx, n.lfcLog)
n.declareState(state)
}
// verifyChainID checks that connection to the node matches the given chain ID
// Not thread-safe
// Pure verifyChainID: does not mutate node "state" field.
func (n *node[CHAIN_ID, HEAD, RPC]) verifyChainID(callerCtx context.Context, lggr logger.Logger) nodeState {
n.metrics.IncrementNodeVerifies(callerCtx, n.name)
promFailed := func() {
n.metrics.IncrementNodeVerifiesFailed(callerCtx, n.name)
}
st := n.getCachedState()
switch st {
case nodeStateClosed:
// The node is already closed, and any subsequent transition is invalid.
// To make spotting such transitions a bit easier, return the invalid node state.
return nodeStateLen
case nodeStateDialed, nodeStateOutOfSync, nodeStateInvalidChainID, nodeStateSyncing, nodeStateFinalizedStateNotAvailable:
default:
panic(fmt.Sprintf("cannot verify node in state %v", st))
}
var chainID CHAIN_ID
var err error
if chainID, err = n.rpc.ChainID(callerCtx); err != nil {
promFailed()
lggr.Errorw("Failed to verify chain ID for node", "err", err, "nodeState", n.getCachedState())
return nodeStateUnreachable
} else if chainID.String() != n.chainID.String() {
promFailed()
err = fmt.Errorf(
"rpc ChainID doesn't match local chain ID: RPC ID=%s, local ID=%s, node name=%s: %w",
chainID.String(),
n.chainID.String(),
n.name,
errInvalidChainID,
)
lggr.Errorw("Failed to verify RPC node; remote endpoint returned the wrong chain ID", "err", err, "nodeState", n.getCachedState())
return nodeStateInvalidChainID
}
n.metrics.IncrementNodeVerifiesSuccess(callerCtx, n.name)
return nodeStateAlive
}
// createVerifiedConn - establishes new connection with the RPC and verifies that it's valid: chainID matches, and it's not syncing.
// Returns desired state if one of the verifications fails. Otherwise, returns nodeStateAlive.
func (n *node[CHAIN_ID, HEAD, RPC]) createVerifiedConn(ctx context.Context, lggr logger.Logger) nodeState {
if err := n.rpc.Dial(ctx); err != nil {
n.lfcLog.Errorw("Dial failed: Node is unreachable", "err", err, "nodeState", n.getCachedState())
return nodeStateUnreachable
}
return n.verifyConn(ctx, lggr)
}
// verifyConn - verifies that current connection is valid: chainID matches, and it's not syncing.
// Returns desired state if one of the verifications fails. Otherwise, returns nodeStateAlive.
func (n *node[CHAIN_ID, HEAD, RPC]) verifyConn(ctx context.Context, lggr logger.Logger) nodeState {
if n.nodePoolCfg.VerifyChainID() {
state := n.verifyChainID(ctx, lggr)
if state != nodeStateAlive {
return state
}
}
if n.nodePoolCfg.NodeIsSyncingEnabled() {
isSyncing, err := n.rpc.IsSyncing(ctx)
if err != nil {
lggr.Errorw("Unexpected error while verifying RPC node synchronization status", "err", err, "nodeState", n.getCachedState())
return nodeStateUnreachable
}
if isSyncing {
lggr.Errorw("Verification failed: Node is syncing", "nodeState", n.getCachedState())
return nodeStateSyncing
}
}
return nodeStateAlive
}
func (n *node[CHAIN_ID, HEAD, RPC]) Order() int32 {
return n.order
}
func (n *node[CHAIN_ID, HEAD, RPC]) newCtx() (context.Context, context.CancelFunc) {
ctx, cancel := n.stopCh.NewCtx()
ctx = CtxAddHealthCheckFlag(ctx)
return ctx, cancel
}