2019-08-16 20:03:11 +00:00
|
|
|
package p2p
|
|
|
|
|
|
|
|
import (
|
2019-08-29 16:32:52 +00:00
|
|
|
"context"
|
2019-08-16 20:03:11 +00:00
|
|
|
"sync"
|
|
|
|
|
2019-08-29 16:32:52 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/network"
|
2019-08-16 20:03:11 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
var handshakes = make(map[peer.ID]*pb.Hello)
|
|
|
|
var handshakeLock sync.Mutex
|
|
|
|
|
|
|
|
// AddHandshake to the local records for initial sync.
|
2019-09-01 22:29:58 +00:00
|
|
|
func (s *Service) AddHandshake(pid peer.ID, hello *pb.Hello) {
|
2019-08-16 20:03:11 +00:00
|
|
|
handshakeLock.Lock()
|
|
|
|
defer handshakeLock.Unlock()
|
|
|
|
handshakes[pid] = hello
|
|
|
|
}
|
2019-08-19 21:20:56 +00:00
|
|
|
|
|
|
|
// Handshakes has not been implemented yet and it may be moved to regular sync...
|
2019-09-01 22:29:58 +00:00
|
|
|
func (s *Service) Handshakes() map[peer.ID]*pb.Hello {
|
2019-08-19 21:20:56 +00:00
|
|
|
return nil
|
|
|
|
}
|
2019-08-29 16:32:52 +00:00
|
|
|
|
|
|
|
// AddConnectionHandler adds a callback function which handles the connection with a
|
|
|
|
// newly added peer. It performs a handshake with that peer by sending a hello request
|
|
|
|
// and validating the response from the peer.
|
2019-09-01 22:29:58 +00:00
|
|
|
func (s *Service) AddConnectionHandler(reqFunc func(ctx context.Context, id peer.ID) error) {
|
|
|
|
s.host.Network().Notify(&network.NotifyBundle{
|
2019-08-29 16:32:52 +00:00
|
|
|
ConnectedF: func(net network.Network, conn network.Conn) {
|
|
|
|
// Must be handled in a goroutine as this callback cannot be blocking.
|
|
|
|
go func() {
|
|
|
|
ctx := context.Background()
|
|
|
|
log.WithField("peer", conn.RemotePeer()).Debug(
|
2019-09-01 22:29:58 +00:00
|
|
|
"Performing handshake with peer",
|
2019-08-29 16:32:52 +00:00
|
|
|
)
|
|
|
|
if err := reqFunc(ctx, conn.RemotePeer()); err != nil {
|
|
|
|
log.WithError(err).Error("Could not send successful hello rpc request")
|
2019-09-01 22:29:58 +00:00
|
|
|
if err := s.Disconnect(conn.RemotePeer()); err != nil {
|
2019-08-29 16:32:52 +00:00
|
|
|
log.WithError(err).Errorf("Unable to close peer %s", conn.RemotePeer())
|
2019-08-30 20:15:40 +00:00
|
|
|
return
|
2019-08-29 16:32:52 +00:00
|
|
|
}
|
2019-09-02 13:29:59 +00:00
|
|
|
return
|
2019-08-29 16:32:52 +00:00
|
|
|
}
|
2019-09-02 13:29:59 +00:00
|
|
|
log.WithField("peer", conn.RemotePeer().Pretty()).Info("New peer connected.")
|
2019-08-29 16:32:52 +00:00
|
|
|
}()
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2019-09-01 22:29:58 +00:00
|
|
|
|
|
|
|
// addDisconnectionHandler ensures that previously disconnected peers aren't dialed again. Due
|
|
|
|
// to either their ports being closed, nodes are no longer active,etc.
|
|
|
|
func (s *Service) addDisconnectionHandler() {
|
|
|
|
s.host.Network().Notify(&network.NotifyBundle{
|
|
|
|
DisconnectedF: func(net network.Network, conn network.Conn) {
|
|
|
|
// Must be handled in a goroutine as this callback cannot be blocking.
|
|
|
|
go func() {
|
|
|
|
s.exclusionList.Set(conn.RemotePeer().String(), true, ttl)
|
|
|
|
log.WithField("peer", conn.RemotePeer()).Debug(
|
|
|
|
"Peer is added to exclusion list",
|
|
|
|
)
|
|
|
|
}()
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|