2019-08-16 17:13:04 +00:00
|
|
|
package p2p
|
|
|
|
|
|
|
|
import (
|
2019-08-19 21:20:56 +00:00
|
|
|
"context"
|
|
|
|
|
2019-08-16 17:13:04 +00:00
|
|
|
"github.com/gogo/protobuf/proto"
|
|
|
|
"github.com/libp2p/go-libp2p-core/network"
|
2019-08-16 20:03:11 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
2019-08-16 17:13:04 +00:00
|
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/p2p/encoder"
|
2019-08-16 20:03:11 +00:00
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
2019-08-16 17:13:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// P2P represents the full p2p interface composed of all of the sub-interfaces.
|
|
|
|
type P2P interface {
|
|
|
|
Broadcaster
|
|
|
|
SetStreamHandler
|
|
|
|
EncodingProvider
|
|
|
|
PubSubProvider
|
2019-08-16 20:03:11 +00:00
|
|
|
PeerManager
|
|
|
|
HandshakeManager
|
2019-08-19 21:20:56 +00:00
|
|
|
Sender
|
2019-08-29 16:32:52 +00:00
|
|
|
ConnectionHandler
|
2019-08-16 17:13:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Broadcaster broadcasts messages to peers over the p2p pubsub protocol.
|
|
|
|
type Broadcaster interface {
|
2019-08-19 21:20:56 +00:00
|
|
|
Broadcast(context.Context, proto.Message) error
|
2019-08-16 17:13:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetStreamHandler configures p2p to handle streams of a certain topic ID.
|
|
|
|
type SetStreamHandler interface {
|
|
|
|
SetStreamHandler(topic string, handler network.StreamHandler)
|
|
|
|
}
|
|
|
|
|
2019-08-29 16:32:52 +00:00
|
|
|
// ConnectionHandler configures p2p to handle connections with a peer.
|
|
|
|
type ConnectionHandler interface {
|
|
|
|
AddConnectionHandler(f func(ctx context.Context, id peer.ID) error)
|
|
|
|
}
|
|
|
|
|
2019-08-16 17:13:04 +00:00
|
|
|
// EncodingProvider provides p2p network encoding.
|
|
|
|
type EncodingProvider interface {
|
|
|
|
Encoding() encoder.NetworkEncoding
|
|
|
|
}
|
|
|
|
|
|
|
|
// PubSubProvider provides the p2p pubsub protocol.
|
|
|
|
type PubSubProvider interface {
|
|
|
|
PubSub() *pubsub.PubSub
|
|
|
|
}
|
2019-08-16 20:03:11 +00:00
|
|
|
|
|
|
|
// PeerManager abstracts some peer management methods from libp2p.
|
|
|
|
type PeerManager interface {
|
|
|
|
Disconnect(peer.ID) error
|
2019-08-24 18:41:24 +00:00
|
|
|
PeerID() peer.ID
|
2019-08-16 20:03:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HandshakeManager abstracts certain methods regarding handshake records.
|
|
|
|
type HandshakeManager interface {
|
|
|
|
AddHandshake(peer.ID, *pb.Hello)
|
|
|
|
}
|
2019-08-19 21:20:56 +00:00
|
|
|
|
|
|
|
// Sender abstracts the sending functionality from libp2p.
|
|
|
|
type Sender interface {
|
2019-08-22 23:02:46 +00:00
|
|
|
Send(context.Context, proto.Message, peer.ID) (network.Stream, error)
|
2019-08-19 21:20:56 +00:00
|
|
|
}
|