-
Notifications
You must be signed in to change notification settings - Fork 1
/
hostWithConnectionManagement.go
92 lines (77 loc) · 2.77 KB
/
hostWithConnectionManagement.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
package discovery
import (
"context"
"fmt"
"strings"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multiaddr"
"github.com/multiversx/mx-chain-communication-go/p2p"
"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/core/check"
)
// ArgsHostWithConnectionManagement is the argument DTO used in the NewHostWithConnectionManagement function
type ArgsHostWithConnectionManagement struct {
ConnectableHost ConnectableHost
Sharder Sharder
ConnectionsWatcher p2p.ConnectionsWatcher
}
type hostWithConnectionManagement struct {
ConnectableHost
sharder Sharder
connectionsWatcher p2p.ConnectionsWatcher
}
// NewHostWithConnectionManagement returns a host wrapper able to decide if connection initiated to a peer
// will actually be kept or not
func NewHostWithConnectionManagement(args ArgsHostWithConnectionManagement) (*hostWithConnectionManagement, error) {
if check.IfNil(args.ConnectableHost) {
return nil, p2p.ErrNilHost
}
if check.IfNil(args.Sharder) {
return nil, p2p.ErrNilSharder
}
if check.IfNil(args.ConnectionsWatcher) {
return nil, p2p.ErrNilConnectionsWatcher
}
return &hostWithConnectionManagement{
ConnectableHost: args.ConnectableHost,
sharder: args.Sharder,
connectionsWatcher: args.ConnectionsWatcher,
}, nil
}
// Connect tries to connect to the provided address info if the sharder allows it
func (hwcm *hostWithConnectionManagement) Connect(ctx context.Context, pi peer.AddrInfo) error {
addresses := concatenateAddresses(pi.Addrs)
hwcm.connectionsWatcher.NewKnownConnection(core.PeerID(pi.ID), addresses)
err := hwcm.canConnectToPeer(pi.ID)
if err != nil {
return err
}
return hwcm.ConnectableHost.Connect(ctx, pi)
}
func concatenateAddresses(addresses []multiaddr.Multiaddr) string {
sb := strings.Builder{}
for _, ma := range addresses {
sb.WriteString(ma.String() + " ")
}
return sb.String()
}
func (hwcm *hostWithConnectionManagement) canConnectToPeer(pid peer.ID) error {
allPeers := hwcm.ConnectableHost.Network().Peers()
if !hwcm.sharder.Has(pid, allPeers) {
allPeers = append(allPeers, pid)
}
evicted := hwcm.sharder.ComputeEvictionList(allPeers)
if hwcm.sharder.Has(pid, evicted) {
return fmt.Errorf("%w, pid: %s", p2p.ErrUnwantedPeer, pid.String())
}
return nil
}
// IsConnected returns true if the current host is connected to the provided peer info
func (hwcm *hostWithConnectionManagement) IsConnected(pi peer.AddrInfo) bool {
return hwcm.Network().Connectedness(pi.ID) == network.Connected
}
// IsInterfaceNil returns true if there is no value under the interface
func (hwcm *hostWithConnectionManagement) IsInterfaceNil() bool {
return hwcm == nil || check.IfNil(hwcm.ConnectableHost)
}