-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
node_selector_total_difficulty.go
54 lines (45 loc) · 1.39 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
44
45
46
47
48
49
50
51
52
53
54
package client
import (
"math/big"
"github.com/smartcontractkit/chainlink/v2/common/types"
)
type totalDifficultyNodeSelector[
CHAIN_ID types.ID,
HEAD Head,
RPC NodeClient[CHAIN_ID, HEAD],
] []Node[CHAIN_ID, HEAD, RPC]
func NewTotalDifficultyNodeSelector[
CHAIN_ID types.ID,
HEAD Head,
RPC NodeClient[CHAIN_ID, HEAD],
](nodes []Node[CHAIN_ID, HEAD, RPC]) NodeSelector[CHAIN_ID, HEAD, RPC] {
return totalDifficultyNodeSelector[CHAIN_ID, HEAD, RPC](nodes)
}
func (s totalDifficultyNodeSelector[CHAIN_ID, HEAD, RPC]) Select() Node[CHAIN_ID, HEAD, RPC] {
// NodeNoNewHeadsThreshold may not be enabled, in this case all nodes have td == nil
var highestTD *big.Int
var nodes []Node[CHAIN_ID, HEAD, RPC]
var aliveNodes []Node[CHAIN_ID, HEAD, RPC]
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[CHAIN_ID, HEAD, RPC]) Name() string {
return NodeSelectionModeTotalDifficulty
}