/
node_relevance.go
127 lines (107 loc) · 3.7 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright (C) 2017, 2018, 2019 EGAAS S.A.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or (at
// your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
package service
import (
"context"
"time"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/AplaProject/go-apla/packages/conf/syspar"
"github.com/AplaProject/go-apla/packages/consts"
"github.com/AplaProject/go-apla/packages/model"
"github.com/AplaProject/go-apla/packages/network/tcpclient"
)
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(ctx context.Context) {
go func() {
log.Info("Node relevance monitoring started")
for {
relevance, err := n.checkNodeRelevance(ctx)
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() {
if IsNodePaused() {
updatingEndWhilePaused <- struct{}{}
}
}
func (n *NodeRelevanceService) checkNodeRelevance(ctx context.Context) (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, err := GetNodesBanService().FilterBannedHosts(syspar.GetRemoteHosts())
if err != nil {
return false, err
}
// Node is single in blockchain network and it can't be irrelevant
if len(remoteHosts) == 0 {
return true, nil
}
_, maxBlockID, err := tcpclient.HostWithMaxBlock(ctx, remoteHosts)
if err != nil {
if err == tcpclient.ErrNodesUnavailable {
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()
}