/
node_ban.go
202 lines (169 loc) · 5.18 KB
/
node_ban.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
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
// 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 (
"sync"
"time"
"github.com/AplaProject/go-apla/packages/conf"
"github.com/AplaProject/go-apla/packages/conf/syspar"
"github.com/AplaProject/go-apla/packages/consts"
"github.com/AplaProject/go-apla/packages/script"
"github.com/AplaProject/go-apla/packages/smart"
"github.com/AplaProject/go-apla/packages/utils"
"github.com/AplaProject/go-apla/packages/utils/tx"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
type localBannedNode struct {
FullNode *syspar.FullNode
LocalUnBanTime time.Time
}
type NodesBanService struct {
localBannedNodes map[int64]localBannedNode
fullNodes []syspar.FullNode
m *sync.Mutex
}
var nbs *NodesBanService
// GetNodesBanService is returning nodes ban service
func GetNodesBanService() *NodesBanService {
return nbs
}
// InitNodesBanService initializing nodes ban storage
func InitNodesBanService() error {
nbs = &NodesBanService{
localBannedNodes: make(map[int64]localBannedNode),
m: &sync.Mutex{},
}
nbs.refreshNodes()
return nil
}
// RegisterBadBlock is set node to local ban and saving bad block to global registry
func (nbs *NodesBanService) RegisterBadBlock(node syspar.FullNode, badBlockId, blockTime int64, reason string) error {
if nbs.IsBanned(node) {
return nil
}
nbs.localBan(node)
err := nbs.newBadBlock(node, badBlockId, blockTime, reason)
if err != nil {
return err
}
return nil
}
// IsBanned is allows to check node ban (local or global)
func (nbs *NodesBanService) IsBanned(node syspar.FullNode) bool {
nbs.refreshNodes()
nbs.m.Lock()
defer nbs.m.Unlock()
// Searching for local ban
now := time.Now()
if fn, ok := nbs.localBannedNodes[node.KeyID]; ok {
if now.Equal(fn.LocalUnBanTime) || now.After(fn.LocalUnBanTime) {
delete(nbs.localBannedNodes, node.KeyID)
return false
}
return true
}
// Searching for global ban.
// Here we don't estimating global ban expiration. If ban time doesn't equal zero - we assuming
// that node is still banned (even if `unban` time has already passed)
for _, fn := range nbs.fullNodes {
if fn.KeyID == node.KeyID {
if !fn.UnbanTime.Equal(time.Unix(0, 0)) {
return true
} else {
break
}
}
}
return false
}
func (nbs *NodesBanService) refreshNodes() {
nbs.m.Lock()
nbs.fullNodes = syspar.GetNodes()
nbs.m.Unlock()
}
func (nbs *NodesBanService) localBan(node syspar.FullNode) {
nbs.m.Lock()
defer nbs.m.Unlock()
nbs.localBannedNodes[node.KeyID] = localBannedNode{
FullNode: &node,
LocalUnBanTime: time.Now().Add(syspar.GetLocalNodeBanTime()),
}
}
func (nbs *NodesBanService) newBadBlock(producer syspar.FullNode, blockId, blockTime int64, reason string) error {
nodePrivateKey, err := utils.GetNodePrivateKey()
if err != nil || len(nodePrivateKey) < 1 {
if err == nil {
log.WithFields(log.Fields{"type": consts.EmptyObject}).Error("node private key is empty")
}
return err
}
var currentNode syspar.FullNode
nbs.m.Lock()
for _, fn := range nbs.fullNodes {
if fn.KeyID == conf.Config.KeyID {
currentNode = fn
break
}
}
nbs.m.Unlock()
if currentNode.KeyID == 0 {
return errors.New("cant find current node in full nodes list")
}
vm := smart.GetVM()
contract := smart.VMGetContract(vm, "NewBadBlock", 1)
info := contract.Block.Info.(*script.ContractInfo)
sc := tx.SmartContract{
Header: tx.Header{
ID: int(info.ID),
Time: time.Now().Unix(),
EcosystemID: 1,
KeyID: conf.Config.KeyID,
},
Params: map[string]interface{}{
"ProducerNodeID": producer.KeyID,
"ConsumerNodeID": currentNode.KeyID,
"BlockID": blockId,
"Timestamp": blockTime,
"Reason": reason,
},
}
txData, txHash, err := tx.NewInternalTransaction(sc, nodePrivateKey)
if err != nil {
return err
}
return tx.CreateTransaction(txData, txHash, conf.Config.KeyID)
}
func (nbs *NodesBanService) FilterHosts(hosts []string) ([]string, []string, error) {
var goodHosts, banHosts []string
for _, h := range hosts {
n, err := syspar.GetNodeByHost(h)
if err != nil {
log.WithFields(log.Fields{"error": err, "host": h}).Error("getting node by host")
return nil, nil, err
}
if nbs.IsBanned(n) {
banHosts = append(banHosts, n.TCPAddress)
} else {
goodHosts = append(goodHosts, n.TCPAddress)
}
}
return goodHosts, banHosts, nil
}
func (nbs *NodesBanService) FilterBannedHosts(hosts []string) (goodHosts []string, err error) {
goodHosts, _, err = nbs.FilterHosts(hosts)
return
}