forked from AplaProject/go-apla
/
node_relevance.go
112 lines (93 loc) · 3.05 KB
/
node_relevance.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
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
package service
import (
"context"
"time"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/GenesisCommunity/go-genesis/packages/conf/syspar"
"github.com/GenesisCommunity/go-genesis/packages/consts"
"github.com/GenesisCommunity/go-genesis/packages/model"
"github.com/GenesisCommunity/go-genesis/packages/utils"
)
var updatingEndWhilePaused = make(chan struct{})
type NodeRelevanceService struct {
availableBlockchainGap int64
checkingInterval time.Duration
}
func NewNodeRelevanceService(availableBlockchainGap int64, checkingInterval time.Duration) NodeRelevanceService {
return NodeRelevanceService{
availableBlockchainGap: availableBlockchainGap,
checkingInterval: checkingInterval,
}
}
// Run is starting node monitoring
func (n *NodeRelevanceService) Run() {
go func() {
log.Info("Node relevance monitoring started")
for {
relevance, err := n.checkNodeRelevance()
if err != nil {
log.WithFields(log.Fields{"type": consts.BCRelevanceError, "err": err}).Error("checking blockchain relevance")
return
}
if !relevance && !IsNodePaused() {
log.Info("Node Relevance Service is pausing node activity")
n.pauseNodeActivity()
}
if relevance && IsNodePaused() {
log.Info("Node Relevance Service is resuming node activity")
n.resumeNodeActivity()
}
select {
case <-time.After(n.checkingInterval):
case <-updatingEndWhilePaused:
}
}
}()
}
func NodeDoneUpdatingBlockchain() {
go func() {
if IsNodePaused() {
updatingEndWhilePaused <- struct{}{}
}
}()
}
func (n *NodeRelevanceService) checkNodeRelevance() (relevant bool, err error) {
curBlock := &model.InfoBlock{}
_, err = curBlock.Get()
if err != nil {
log.WithFields(log.Fields{"type": consts.DBError, "err": err}).Error("retrieving info block from db")
return false, errors.Wrapf(err, "retrieving info block from db")
}
remoteHosts := syspar.GetRemoteHosts()
// Node is single in blockchain network and it can't be irrelevant
if len(remoteHosts) == 0 {
return true, nil
}
ctx, _ := context.WithCancel(context.Background())
_, maxBlockID, err := utils.ChooseBestHost(ctx, remoteHosts, &log.Entry{Logger: &log.Logger{}})
if err != nil {
if err == utils.ErrNodesUnavailable {
log.WithFields(log.Fields{"hosts": remoteHosts}).Info("can't connect to others, stopping node relevance")
return false, nil
}
return false, errors.Wrapf(err, "choosing best host")
}
// Node can't connect to others
if maxBlockID == -1 {
log.WithFields(log.Fields{"hosts": remoteHosts}).Info("can't connect to others, stopping node relevance")
return false, nil
}
// Node blockchain is stale
if curBlock.BlockID+n.availableBlockchainGap < maxBlockID {
log.WithFields(log.Fields{"maxBlockID": maxBlockID, "curBlockID": curBlock.BlockID, "Gap": n.availableBlockchainGap}).Info("blockchain is stale, stopping node relevance")
return false, nil
}
return true, nil
}
func (n *NodeRelevanceService) pauseNodeActivity() {
np.Set(PauseTypeUpdatingBlockchain)
}
func (n *NodeRelevanceService) resumeNodeActivity() {
np.Unset()
}