-
Notifications
You must be signed in to change notification settings - Fork 0
/
peersRatingHandler.go
228 lines (187 loc) · 5.88 KB
/
peersRatingHandler.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package rating
import (
"fmt"
"sync"
"github.com/bhagyaraj1208117/andes-abc-1/core"
"github.com/bhagyaraj1208117/andes-abc-1/core/check"
"github.com/bhagyaraj1208117/andes-communication-go/p2p"
logger "github.com/bhagyaraj1208117/andes-logger-xyz"
"github.com/bhagyaraj1208117/andes-storage-go/types"
)
const (
topRatedTier = "top rated tier"
badRatedTier = "bad rated tier"
defaultRating = int32(0)
minRating = -100
maxRating = 100
increaseFactor = 2
decreaseFactor = -1
minNumOfPeers = 1
int32Size = 4
)
// ArgPeersRatingHandler is the DTO used to create a new peers rating handler
type ArgPeersRatingHandler struct {
TopRatedCache types.Cacher
BadRatedCache types.Cacher
Logger p2p.Logger
}
type peersRatingHandler struct {
topRatedCache types.Cacher
badRatedCache types.Cacher
mut sync.RWMutex
log p2p.Logger
}
// NewPeersRatingHandler returns a new peers rating handler
func NewPeersRatingHandler(args ArgPeersRatingHandler) (*peersRatingHandler, error) {
err := checkHandlerArgs(args)
if err != nil {
return nil, err
}
return &peersRatingHandler{
topRatedCache: args.TopRatedCache,
badRatedCache: args.BadRatedCache,
log: args.Logger,
}, nil
}
func checkHandlerArgs(args ArgPeersRatingHandler) error {
if check.IfNil(args.TopRatedCache) {
return fmt.Errorf("%w for TopRatedCache", p2p.ErrNilCacher)
}
if check.IfNil(args.BadRatedCache) {
return fmt.Errorf("%w for BadRatedCache", p2p.ErrNilCacher)
}
if check.IfNil(args.Logger) {
return p2p.ErrNilLogger
}
return nil
}
// IncreaseRating increases the rating of a peer with the increase factor
func (prh *peersRatingHandler) IncreaseRating(pid core.PeerID) {
// keep this section critical, as we do read + write
prh.mut.Lock()
defer prh.mut.Unlock()
prh.updateRating(pid, increaseFactor)
}
// DecreaseRating decreases the rating of a peer with the decrease factor
func (prh *peersRatingHandler) DecreaseRating(pid core.PeerID) {
// keep this section critical, as we do read + write
prh.mut.Lock()
defer prh.mut.Unlock()
prh.updateRating(pid, decreaseFactor)
}
func (prh *peersRatingHandler) getOldRating(pid []byte) (int32, bool) {
oldRating, found := prh.topRatedCache.Get(pid)
if found {
oldRatingInt, _ := oldRating.(int32)
return oldRatingInt, found
}
oldRating, found = prh.badRatedCache.Get(pid)
if found {
oldRatingInt, _ := oldRating.(int32)
return oldRatingInt, found
}
return defaultRating, found
}
func (prh *peersRatingHandler) updateRating(pid core.PeerID, updateFactor int32) {
oldRating, found := prh.getOldRating(pid.Bytes())
if !found {
// new pid, add it with default rating
prh.topRatedCache.Put(pid.Bytes(), defaultRating, int32Size)
return
}
newRating := oldRating + updateFactor
if newRating > maxRating {
newRating = maxRating
}
if newRating < minRating {
newRating = minRating
}
prh.updateRatingCacher(pid, oldRating, newRating)
}
func (prh *peersRatingHandler) updateRatingCacher(pid core.PeerID, oldRating, newRating int32) {
oldTier := computeRatingTier(oldRating)
newTier := computeRatingTier(newRating)
if newTier == oldTier {
if newTier == topRatedTier {
prh.topRatedCache.Put(pid.Bytes(), newRating, int32Size)
} else {
prh.badRatedCache.Put(pid.Bytes(), newRating, int32Size)
}
return
}
prh.movePeerToNewTier(newRating, newTier, pid)
}
func computeRatingTier(peerRating int32) string {
if peerRating >= defaultRating {
return topRatedTier
}
return badRatedTier
}
func (prh *peersRatingHandler) movePeerToNewTier(newRating int32, newTier string, pid core.PeerID) {
if newTier == topRatedTier {
prh.badRatedCache.Remove(pid.Bytes())
prh.topRatedCache.Put(pid.Bytes(), newRating, int32Size)
} else {
prh.topRatedCache.Remove(pid.Bytes())
prh.badRatedCache.Put(pid.Bytes(), newRating, int32Size)
}
}
// GetTopRatedPeersFromList returns a list of peers, searching them in the order of rating tiers
func (prh *peersRatingHandler) GetTopRatedPeersFromList(peers []core.PeerID, minNumOfPeersExpected int) []core.PeerID {
prh.mut.Lock()
defer prh.mut.Unlock()
peersTopRated := make([]core.PeerID, 0)
defer prh.displayPeersRating(&peersTopRated, minNumOfPeersExpected)
isListEmpty := len(peers) == 0
if minNumOfPeersExpected < minNumOfPeers || isListEmpty {
return make([]core.PeerID, 0)
}
peersTopRated, peersBadRated := prh.splitPeersByTiers(peers)
if len(peersTopRated) < minNumOfPeersExpected {
peersTopRated = append(peersTopRated, peersBadRated...)
}
return peersTopRated
}
func (prh *peersRatingHandler) displayPeersRating(peers *[]core.PeerID, minNumOfPeersExpected int) {
if prh.log.GetLevel() != logger.LogTrace {
return
}
strPeersRatings := ""
for _, peer := range *peers {
rating, ok := prh.topRatedCache.Get(peer.Bytes())
if !ok {
rating, _ = prh.badRatedCache.Get(peer.Bytes())
}
ratingInt, ok := rating.(int32)
if ok {
strPeersRatings += fmt.Sprintf("\n peerID: %s, rating: %d", peer.Pretty(), ratingInt)
} else {
strPeersRatings += fmt.Sprintf("\n peerID: %s, rating: invalid", peer.Pretty())
}
}
prh.log.Trace("Best peers to request from", "min requested", minNumOfPeersExpected, "peers ratings", strPeersRatings)
}
func (prh *peersRatingHandler) splitPeersByTiers(peers []core.PeerID) ([]core.PeerID, []core.PeerID) {
topRated := make([]core.PeerID, 0)
badRated := make([]core.PeerID, 0)
for _, peer := range peers {
isNewPeer := true
if prh.topRatedCache.Has(peer.Bytes()) {
topRated = append(topRated, peer)
isNewPeer = false
}
if prh.badRatedCache.Has(peer.Bytes()) {
badRated = append(badRated, peer)
isNewPeer = false
}
if isNewPeer {
prh.topRatedCache.Put(peer.Bytes(), defaultRating, int32Size)
topRated = append(topRated, peer)
}
}
return topRated, badRated
}
// IsInterfaceNil returns true if there is no value under the interface
func (prh *peersRatingHandler) IsInterfaceNil() bool {
return prh == nil
}