prysm-pulse/shared/p2p/options.go
Preston Van Loon 0559d01261
Add support for connecting via relay nodes (#827)
* Preliminary support for relay nodes

* lint

* Add comment, remove TODO

* work on relay address factory

* dial relay node, if available

* forgot new files

* fix service registry breakage

* added logging

* Added a peer count with prometheus

* always start mDNS

* fix lint
2018-11-25 11:55:02 -05:00

44 lines
1.1 KiB
Go

package p2p
import (
"fmt"
"math/rand"
"time"
libp2p "github.com/libp2p/go-libp2p"
crypto "github.com/libp2p/go-libp2p-crypto"
ma "github.com/multiformats/go-multiaddr"
"github.com/prysmaticlabs/prysm/shared/iputils"
)
var port int32 = 9000
var portRange int32 = 100
// buildOptions for the libp2p host.
// TODO(287): Expand on these options and provide the option configuration via flags.
// Currently, this is a random port and a (seemingly) consistent private key
// identity.
func buildOptions() []libp2p.Option {
rand.Seed(int64(time.Now().Nanosecond()))
priv, _, err := crypto.GenerateKeyPair(crypto.Secp256k1, 512)
if err != nil {
log.Errorf("Failed to generate crypto key pair: %v", err)
}
ip, err := iputils.ExternalIPv4()
if err != nil {
log.Errorf("Could not get IPv4 address: %v", err)
}
listen, err := ma.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d", ip, port+(rand.Int31n(portRange))))
if err != nil {
log.Errorf("Failed to p2p listen: %v", err)
}
return []libp2p.Option{
libp2p.ListenAddrs(listen),
libp2p.Identity(priv),
libp2p.EnableRelay(), // Allows dialing to peers via relay.
}
}