2022-10-23 09:13:43 -04:00
|
|
|
package peer_exchange
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/libp2p/go-libp2p/core/host"
|
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
2023-08-10 18:28:22 +05:30
|
|
|
"github.com/waku-org/go-waku/waku/v2/peermanager"
|
2022-10-23 09:13:43 -04:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type PeerExchangeParameters struct {
|
2023-10-16 22:12:01 +05:30
|
|
|
host host.Host
|
|
|
|
selectedPeer peer.ID
|
|
|
|
peerSelectionType peermanager.PeerSelection
|
|
|
|
preferredPeers peer.IDSlice
|
|
|
|
pm *peermanager.PeerManager
|
|
|
|
log *zap.Logger
|
2022-10-23 09:13:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type PeerExchangeOption func(*PeerExchangeParameters)
|
|
|
|
|
|
|
|
// WithPeer is an option used to specify the peerID to push a waku message to
|
|
|
|
func WithPeer(p peer.ID) PeerExchangeOption {
|
|
|
|
return func(params *PeerExchangeParameters) {
|
|
|
|
params.selectedPeer = p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-16 22:12:01 +05:30
|
|
|
// WithAutomaticPeerSelection is an option used to randomly select a peer from the Waku peer store
|
2022-11-24 17:50:43 -04:00
|
|
|
// to obtains peers from. If a list of specific peers is passed, the peer will be chosen
|
|
|
|
// from that list assuming it supports the chosen protocol, otherwise it will chose a peer
|
|
|
|
// from the node peerstore
|
2023-10-16 22:12:01 +05:30
|
|
|
// Note: this option can only be used if WakuNode is initialized which internally intializes the peerManager
|
2022-11-24 17:50:43 -04:00
|
|
|
func WithAutomaticPeerSelection(fromThesePeers ...peer.ID) PeerExchangeOption {
|
2022-10-23 09:13:43 -04:00
|
|
|
return func(params *PeerExchangeParameters) {
|
2023-10-16 22:12:01 +05:30
|
|
|
params.peerSelectionType = peermanager.Automatic
|
|
|
|
params.preferredPeers = fromThesePeers
|
2022-10-23 09:13:43 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithFastestPeerSelection is an option used to select a peer from the peer store
|
2022-11-24 17:50:43 -04:00
|
|
|
// with the lowest ping. If a list of specific peers is passed, the peer will be chosen
|
|
|
|
// from that list assuming it supports the chosen protocol, otherwise it will chose a peer
|
|
|
|
// from the node peerstore
|
2023-10-16 22:12:01 +05:30
|
|
|
func WithFastestPeerSelection(fromThesePeers ...peer.ID) PeerExchangeOption {
|
2022-10-23 09:13:43 -04:00
|
|
|
return func(params *PeerExchangeParameters) {
|
2023-10-16 22:12:01 +05:30
|
|
|
params.peerSelectionType = peermanager.LowestRTT
|
|
|
|
params.preferredPeers = fromThesePeers
|
2022-10-23 09:13:43 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultOptions are the default options to be used when using the lightpush protocol
|
|
|
|
func DefaultOptions(host host.Host) []PeerExchangeOption {
|
|
|
|
return []PeerExchangeOption{
|
|
|
|
WithAutomaticPeerSelection(),
|
|
|
|
}
|
|
|
|
}
|