2018-07-17 18:39:04 +00:00
|
|
|
package p2p
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2019-05-09 21:02:24 +00:00
|
|
|
"github.com/libp2p/go-libp2p"
|
2018-07-17 18:39:04 +00:00
|
|
|
ma "github.com/multiformats/go-multiaddr"
|
2019-05-09 21:02:24 +00:00
|
|
|
|
2018-08-14 16:16:21 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/iputils"
|
2018-07-17 18:39:04 +00:00
|
|
|
)
|
|
|
|
|
2019-05-17 17:29:25 +00:00
|
|
|
// Using package global for test. Swarm testing does not allow testing with
|
|
|
|
// NoSecurity option enabled. If this is resolved upstream, we can set up swarm
|
|
|
|
// with security disabled in the pubsub tests and remove this package global.
|
|
|
|
// https://github.com/libp2p/go-libp2p-swarm/issues/124
|
|
|
|
var disableSecurity = true
|
|
|
|
|
2018-07-17 18:39:04 +00:00
|
|
|
// buildOptions for the libp2p host.
|
2018-09-20 04:14:31 +00:00
|
|
|
// TODO(287): Expand on these options and provide the option configuration via flags.
|
2018-07-17 18:39:04 +00:00
|
|
|
// Currently, this is a random port and a (seemingly) consistent private key
|
|
|
|
// identity.
|
2019-05-09 21:02:24 +00:00
|
|
|
func buildOptions(port, maxPeers int) []libp2p.Option {
|
2018-08-14 16:16:21 +00:00
|
|
|
ip, err := iputils.ExternalIPv4()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Could not get IPv4 address: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-11-26 02:54:02 +00:00
|
|
|
listen, err := ma.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d", ip, port))
|
2018-07-29 00:44:24 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to p2p listen: %v", err)
|
|
|
|
}
|
2018-07-17 18:39:04 +00:00
|
|
|
|
2019-05-17 17:29:25 +00:00
|
|
|
opts := []libp2p.Option{
|
2018-07-17 18:39:04 +00:00
|
|
|
libp2p.ListenAddrs(listen),
|
2018-11-25 16:55:02 +00:00
|
|
|
libp2p.EnableRelay(), // Allows dialing to peers via relay.
|
2019-05-09 21:02:24 +00:00
|
|
|
optionConnectionManager(maxPeers),
|
2018-07-17 18:39:04 +00:00
|
|
|
}
|
2019-05-17 17:29:25 +00:00
|
|
|
|
|
|
|
if disableSecurity {
|
|
|
|
opts = append(opts, libp2p.NoSecurity)
|
|
|
|
}
|
|
|
|
|
|
|
|
return opts
|
2018-07-17 18:39:04 +00:00
|
|
|
}
|