-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
node_selector_total_difficulty.go
43 lines (35 loc) · 1.12 KB
/
node_selector_total_difficulty.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
package client
import "math/big"
type totalDifficultyNodeSelector []Node
// Deprecated: use [pkg/github.com/smartcontractkit/chainlink/v2/common/client.NewTotalDifficultyNodeSelector]
func NewTotalDifficultyNodeSelector(nodes []Node) NodeSelector {
return totalDifficultyNodeSelector(nodes)
}
func (s totalDifficultyNodeSelector) Select() Node {
// NodeNoNewHeadsThreshold may not be enabled, in this case all nodes have td == nil
var highestTD *big.Int
var nodes []Node
var aliveNodes []Node
for _, n := range s {
state, _, currentTD := n.StateAndLatest()
if state != NodeStateAlive {
continue
}
aliveNodes = append(aliveNodes, n)
if currentTD != nil && (highestTD == nil || currentTD.Cmp(highestTD) >= 0) {
if highestTD == nil || currentTD.Cmp(highestTD) > 0 {
highestTD = currentTD
nodes = nil
}
nodes = append(nodes, n)
}
}
//If all nodes have td == nil pick one from the nodes that are alive
if len(nodes) == 0 {
return firstOrHighestPriority(aliveNodes)
}
return firstOrHighestPriority(nodes)
}
func (s totalDifficultyNodeSelector) Name() string {
return NodeSelectionMode_TotalDifficulty
}