/
selection.go
49 lines (44 loc) · 1.38 KB
/
selection.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
package autopeering
import (
"github.com/iotaledger/hive.go/core/autopeering/discover"
"github.com/iotaledger/hive.go/core/autopeering/peer"
"github.com/iotaledger/hive.go/core/autopeering/peer/service"
"github.com/iotaledger/hive.go/core/autopeering/selection"
"github.com/iotaledger/hive.go/core/identity"
"github.com/iotaledger/hive.go/core/logger"
)
func createPeerSel(localID *peer.Local, nbrDiscover *discover.Protocol) *selection.Protocol {
// assure that the logger is available
log := logger.NewLogger(PluginName).Named("sel")
return selection.New(localID, nbrDiscover,
selection.Logger(log),
selection.NeighborValidator(selection.ValidatorFunc(isValidNeighbor)),
selection.UseMana(Parameters.Mana),
selection.ManaFunc(evalMana),
selection.R(Parameters.R),
selection.Ro(Parameters.Ro),
)
}
// isValidNeighbor checks whether a peer is a valid neighbor.
func isValidNeighbor(p *peer.Peer) bool {
// gossip must be supported
gossipService := p.Services().Get(service.P2PKey)
if gossipService == nil {
return false
}
// gossip service must be valid
if gossipService.Network() != "tcp" || gossipService.Port() < 0 || gossipService.Port() > 65535 {
return false
}
return true
}
func evalMana(nodeIdentity *identity.Identity) uint64 {
if deps.ManaFunc == nil {
return 0
}
m, _, err := deps.ManaFunc(nodeIdentity.ID())
if err != nil {
return 0
}
return uint64(m)
}