Skip to content

Commit

Permalink
Create separate host options for browser and native envs
Browse files Browse the repository at this point in the history
  • Loading branch information
albrow committed Aug 20, 2019
1 parent 873025b commit 405555a
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 69 deletions.
1 change: 0 additions & 1 deletion browser/main.go
Expand Up @@ -11,7 +11,6 @@ func main() {
appConfig := core.Config{
Verbosity: 4,
DataDir: "zeroex-mesh",
P2PListenPort: 0,
EthereumRPCURL: "https://mainnet.infura.io/v3/af2e590be00f463fbfd0b546784065ad",
EthereumNetworkID: 1,
UseBootstrapList: true,
Expand Down
3 changes: 1 addition & 2 deletions cmd/mesh-bootstrap/main.go
Expand Up @@ -12,8 +12,6 @@ import (
"strings"
"time"

"github.com/libp2p/go-libp2p-core/peer"

"github.com/0xProject/0x-mesh/keys"
"github.com/0xProject/0x-mesh/loghooks"
"github.com/0xProject/0x-mesh/p2p"
Expand All @@ -24,6 +22,7 @@ import (
p2pcrypto "github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/host"
p2pnet "github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/routing"
dht "github.com/libp2p/go-libp2p-kad-dht"
"github.com/libp2p/go-libp2p/p2p/host/relay"
Expand Down
11 changes: 8 additions & 3 deletions core/core.go
Expand Up @@ -53,8 +53,12 @@ type Config struct {
// DataDir is the directory to use for persisting all data, including the
// database and private key files.
DataDir string `envvar:"DATA_DIR" default:"0x_mesh"`
// P2PListenPort is the port on which to listen for new peer connections.
P2PListenPort int `envvar:"P2P_LISTEN_PORT"`
// P2PTCPPort is the port on which to listen for new TCP connections from
// peers in the network.
P2PTCPPort int `envvar:"P2P_TCP_PORT"`
// P2PWebSocketsPort is the port on which to listen for new WebSockets
// connections from peers in the network.
P2PWebSocketsPort int `envvar:"P2P_WEBSOCKETS_PORT"`
// EthereumRPCURL is the URL of an Etheruem node which supports the JSON RPC
// API.
EthereumRPCURL string `envvar:"ETHEREUM_RPC_URL" json:"-"`
Expand Down Expand Up @@ -348,7 +352,8 @@ func (app *App) Start(ctx context.Context) error {
// Initialize the p2p node.
nodeConfig := p2p.Config{
Topic: getPubSubTopic(app.config.EthereumNetworkID),
ListenPort: app.config.P2PListenPort,
TCPPort: app.config.P2PTCPPort,
WebSocketsPort: app.config.P2PWebSocketsPort,
Insecure: false,
PrivateKey: app.privKey,
MessageHandler: app,
Expand Down
82 changes: 82 additions & 0 deletions p2p/host_opts.go
@@ -0,0 +1,82 @@
// +build !js

package p2p

import (
"context"
"fmt"
"io/ioutil"
"net/http"

leveldbStore "github.com/ipfs/go-ds-leveldb"
libp2p "github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p-peerstore/pstoreds"
ma "github.com/multiformats/go-multiaddr"
)

func getOptionsForCurrentEnvironment(ctx context.Context, config Config) ([]libp2p.Option, error) {
// Note: 0.0.0.0 will use all available addresses.
tcpBindAddr, err := ma.NewMultiaddr(fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", config.TCPPort))
if err != nil {
return nil, err
}
wsBindAddr, err := ma.NewMultiaddr(fmt.Sprintf("/ip4/0.0.0.0/tcp/%d/ws", config.WebSocketsPort))
if err != nil {
return nil, err
}

// HACK(albrow): As a workaround for AutoNAT issues, ping ifconfig.me to
// determine our public IP address on boot. This will work for nodes that
// would be reachable via a public IP address but don't know what it is (e.g.
// because they are running in a Docker container).
publicIP, err := getPublicIP()
if err != nil {
return nil, fmt.Errorf("could not get public IP address: %s", err.Error())
}
tcpAdvertiseAddr, err := ma.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d", publicIP, config.TCPPort))
if err != nil {
return nil, err
}
wsAdvertiseAddr, err := ma.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d/ws", publicIP, config.WebSocketsPort))
if err != nil {
return nil, err
}
advertiseAddrs := []ma.Multiaddr{tcpAdvertiseAddr, wsAdvertiseAddr}

// Set up the peerstore to use LevelDB.
store, err := leveldbStore.NewDatastore(config.PeerstoreDir, nil)
if err != nil {
return nil, err
}
pstore, err := pstoreds.NewPeerstore(ctx, store, pstoreds.DefaultOpts())
if err != nil {
return nil, err
}
return []libp2p.Option{
libp2p.ListenAddrs(tcpBindAddr, wsBindAddr),
libp2p.AddrsFactory(newAddrsFactory(advertiseAddrs)),
libp2p.Peerstore(pstore),
}, nil
}

func newAddrsFactory(advertiseAddrs []ma.Multiaddr) func([]ma.Multiaddr) []ma.Multiaddr {
return func(addrs []ma.Multiaddr) []ma.Multiaddr {
// Note that we append the advertiseAddrs here just in case we are not
// actually reachable at our public IP address (and are reachable at one of
// the other addresses).
return append(addrs, advertiseAddrs...)
}
}

func getPublicIP() (string, error) {
res, err := http.Get("https://ifconfig.me/ip")
if err != nil {
return "", err
}
defer res.Body.Close()
ipBytes, err := ioutil.ReadAll(res.Body)
if err != nil {
return "", err
}
return string(ipBytes), nil
}
19 changes: 19 additions & 0 deletions p2p/host_opts_js.go
@@ -0,0 +1,19 @@
// +build js,wasm

package p2p

import (
"context"

libp2p "github.com/libp2p/go-libp2p"
ws "github.com/libp2p/go-ws-transport"
)

func getOptionsForCurrentEnvironment(ctx context.Context, config Config) ([]libp2p.Option, error) {
return []libp2p.Option{
libp2p.Transport(ws.New),
// Don't listen on any addresses by default. We can't accept incoming
// connections in the browser.
libp2p.ListenAddrs(),
}, nil
}
78 changes: 16 additions & 62 deletions p2p/node.go
Expand Up @@ -8,11 +8,9 @@ import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"time"

lru "github.com/hashicorp/golang-lru"
leveldbStore "github.com/ipfs/go-ds-leveldb"
libp2p "github.com/libp2p/go-libp2p"
connmgr "github.com/libp2p/go-libp2p-connmgr"
p2pcrypto "github.com/libp2p/go-libp2p-core/crypto"
Expand All @@ -21,7 +19,6 @@ import (
"github.com/libp2p/go-libp2p-core/routing"
discovery "github.com/libp2p/go-libp2p-discovery"
dht "github.com/libp2p/go-libp2p-kad-dht"
"github.com/libp2p/go-libp2p-peerstore/pstoreds"
pubsub "github.com/libp2p/go-libp2p-pubsub"
swarm "github.com/libp2p/go-libp2p-swarm"
ma "github.com/multiformats/go-multiaddr"
Expand Down Expand Up @@ -75,9 +72,13 @@ type Config struct {
// Topic is a unique string representing the pubsub topic. Only Nodes which
// have the same topic will share messages with one another.
Topic string
// ListenPort is the port on which to listen for new connections. It can be
// set to 0 to make the OS automatically choose any available port.
ListenPort int
// TCPPort is the port on which to listen for incoming TCP connections. It can
// be set to 0 to make the OS automatically choose any available port.
TCPPort int
// WebSocketsPort is the port on which to listen for incoming WebSockets
// connections. It can be set to 0 to make the OS automatically choose any
// available port.
WebSocketsPort int
// Insecure controls whether or not messages should be encrypted. It should
// always be set to false in production.
Insecure bool
Expand Down Expand Up @@ -106,12 +107,6 @@ func New(ctx context.Context, config Config) (*Node, error) {
return nil, errors.New("config.RendezvousString is required")
}

// Note: 0.0.0.0 will use all available addresses.
hostAddr, err := ma.NewMultiaddr(fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", config.ListenPort))
if err != nil {
return nil, err
}

// We need to declare the newDHT function ahead of time so we can use it in
// the libp2p.Routing option.
var kadDHT *dht.IpfsDHT
Expand All @@ -124,45 +119,26 @@ func New(ctx context.Context, config Config) (*Node, error) {
return kadDHT, err
}

// HACK(albrow): As a workaround for AutoNAT issues, ping ifconfig.me to
// determine our public IP address on boot. This will work for nodes that
// would be reachable via a public IP address but don't know what it is (e.g.
// because they are running in a Docker container).
publicIP, err := getPublicIP()
if err != nil {
return nil, fmt.Errorf("could not get public IP address: %s", err.Error())
}
maddrString := fmt.Sprintf("/ip4/%s/tcp/%d", publicIP, config.ListenPort)
advertiseAddr, err := ma.NewMultiaddr(maddrString)
if err != nil {
return nil, err
}

// Set up the peerstore to use LevelDB.
store, err := leveldbStore.NewDatastore(config.PeerstoreDir, nil)
if err != nil {
return nil, err
}
pstore, err := pstoreds.NewPeerstore(ctx, store, pstoreds.DefaultOpts())
// Get environment specific host options.
opts, err := getOptionsForCurrentEnvironment(ctx, config)
if err != nil {
return nil, err
}

// Set up the transport and the host.
// Set up and append environment agnostic host options.
connManager := connmgr.NewConnManager(peerCountLow, peerCountHigh, peerGraceDuration)
opts := []libp2p.Option{
libp2p.ListenAddrs(hostAddr),
libp2p.Identity(config.PrivateKey),
opts = append(opts, []libp2p.Option{
libp2p.Routing(newDHT),
libp2p.ConnectionManager(connManager),
libp2p.Identity(config.PrivateKey),
libp2p.EnableAutoRelay(),
libp2p.EnableRelay(),
libp2p.Routing(newDHT),
libp2p.AddrsFactory(newAddrsFactory([]ma.Multiaddr{advertiseAddr})),
libp2p.Peerstore(pstore),
}
}...)
if config.Insecure {
opts = append(opts, libp2p.NoSecurity)
}

// Initialize the host.
basicHost, err := libp2p.New(ctx, opts...)
if err != nil {
return nil, err
Expand Down Expand Up @@ -471,25 +447,3 @@ func (n *Node) receive(ctx context.Context) (*Message, error) {
}
return &Message{From: msg.GetFrom(), Data: msg.Data}, nil
}

func newAddrsFactory(advertiseAddrs []ma.Multiaddr) func([]ma.Multiaddr) []ma.Multiaddr {
return func(addrs []ma.Multiaddr) []ma.Multiaddr {
// Note that we append the advertiseAddrs here just in case we are not
// actually reachable at our public IP address (and are reachable at one of
// the other addresses).
return append(addrs, advertiseAddrs...)
}
}

func getPublicIP() (string, error) {
res, err := http.Get("https://ifconfig.me/ip")
if err != nil {
return "", err
}
defer res.Body.Close()
ipBytes, err := ioutil.ReadAll(res.Body)
if err != nil {
return "", err
}
return string(ipBytes), nil
}
1 change: 0 additions & 1 deletion p2p/node_test.go
Expand Up @@ -81,7 +81,6 @@ func newTestNode(t *testing.T, ctx context.Context, notifee p2pnet.Notifiee) *No
require.NoError(t, err)
config := Config{
Topic: testTopic,
ListenPort: 0, // Let OS randomly choose an open port.
PrivateKey: privKey,
MessageHandler: &dummyMessageHandler{},
RendezvousString: testRendezvousString,
Expand Down

0 comments on commit 405555a

Please sign in to comment.