Skip to content

Commit

Permalink
fix(network): move driver connect implem to berty host
Browse files Browse the repository at this point in the history
Signed-off-by: Godefroy Ponsinet <godefroy.ponsinet@outlook.com>
  • Loading branch information
90dy committed Mar 8, 2019
1 parent fa247b8 commit e1270f1
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 38 deletions.
78 changes: 78 additions & 0 deletions core/.scripts/update-libp2p.sh
@@ -0,0 +1,78 @@
#!/bin/bash

GO111MODULE=on

repos="
github.com/libp2p/go-addr-util
github.com/libp2p/go-buffer-pool
github.com/libp2p/go-conn-security
github.com/libp2p/go-conn-security-multistream
github.com/libp2p/go-flow-metrics
github.com/libp2p/go-libp2p
github.com/libp2p/go-libp2p-blankhost
github.com/libp2p/go-libp2p-circuit
github.com/libp2p/go-libp2p-connmgr
github.com/libp2p/go-libp2p-crypto
github.com/libp2p/go-libp2p-host
github.com/libp2p/go-libp2p-interface-connmgr
github.com/libp2p/go-libp2p-interface-pnet
github.com/libp2p/go-libp2p-kad-dht
github.com/libp2p/go-libp2p-kbucket
github.com/libp2p/go-libp2p-loggables
github.com/libp2p/go-libp2p-metrics
github.com/libp2p/go-libp2p-nat
github.com/libp2p/go-libp2p-net
github.com/libp2p/go-libp2p-netutil
github.com/libp2p/go-libp2p-peer
github.com/libp2p/go-libp2p-peerstore
github.com/libp2p/go-libp2p-pnet
github.com/libp2p/go-libp2p-protocol
github.com/libp2p/go-libp2p-pubsub
github.com/libp2p/go-libp2p-quic-transport
github.com/libp2p/go-libp2p-record
github.com/libp2p/go-libp2p-routing
github.com/libp2p/go-libp2p-secio
github.com/libp2p/go-libp2p-swarm
github.com/libp2p/go-libp2p-transport
github.com/libp2p/go-libp2p-transport-upgrader
github.com/libp2p/go-maddr-filter
github.com/libp2p/go-mplex
github.com/libp2p/go-msgio
github.com/libp2p/go-reuseport
github.com/libp2p/go-reuseport-transport
github.com/libp2p/go-sockaddr
github.com/libp2p/go-stream-muxer
github.com/libp2p/go-tcp-transport
github.com/libp2p/go-testutil
github.com/libp2p/go-ws-transport
github.com/multiformats/go-multiaddr
github.com/multiformats/go-multiaddr-dns
github.com/multiformats/go-multiaddr-net
github.com/multiformats/go-multibase
github.com/multiformats/go-multicodec
github.com/multiformats/go-multihash
github.com/multiformats/go-multistream
github.com/ipfs/go-cid
github.com/ipfs/go-datastore
github.com/ipfs/go-ipfs-addr
github.com/ipfs/go-ipfs-util
github.com/ipfs/go-log
"

# clean up
[ "clean" = "$1" ] && echo "cleaning..." && (rm -rf vendor || true) && go clean -modcache > /dev/null 2>&1

echo "upgrading..."

# update them all
for repo in $repos; do
version=$(git ls-remote --tags "git://$repo" | sed -E 's`refs/tags/(v[0-9]+\.[0-9]+\.[0-9]+).*`\1`g' | sort -V -k 2 | tail -1 | cut -f2)
if [ ! -z "$version" ]; then
echo "$repo -> $version"
go mod edit -require "$repo@$version"
fi
done

go mod vendor -v
go mod tidy
go mod verify
2 changes: 1 addition & 1 deletion core/cmd/berty/daemon.go
Expand Up @@ -150,7 +150,7 @@ func daemon(opts *daemonOptions) error {
MDNS: false,
DHT: opts.dhtServer,
BLE: opts.ble,
QUIC: false,
QUIC: true,
Metric: true,
Ping: true,
DefaultBootstrap: false,
Expand Down
16 changes: 10 additions & 6 deletions core/network/config/config.go
Expand Up @@ -103,6 +103,10 @@ func (cfg *Config) Apply(ctx context.Context, opts ...Option) error {
if len(cfg.Bind) > 0 {
libp2pOpts = append(libp2pOpts, libp2p.ListenAddrStrings(cfg.Bind...))
}

// transport
libp2pOpts = append(libp2pOpts, libp2p.DefaultTransports)

// add ble transport
if cfg.BLE {
libp2pOpts = append(libp2pOpts, libp2p.Transport(ble.NewTransport))
Expand All @@ -114,12 +118,12 @@ func (cfg *Config) Apply(ctx context.Context, opts ...Option) error {
}

// relay
// libp2pOpts = append(libp2pOpts, libp2p.EnableAutoRelay())
// if cfg.HOP {
// libp2pOpts = append(libp2pOpts, libp2p.EnableRelay(circuit.OptActive, circuit.OptHop))
// } else {
// libp2pOpts = append(libp2pOpts, libp2p.EnableRelay(circuit.OptActive))
// }
libp2pOpts = append(libp2pOpts, libp2p.EnableAutoRelay())
if cfg.HOP {
libp2pOpts = append(libp2pOpts, libp2p.EnableRelay(circuit.OptActive, circuit.OptHop))
} else {
libp2pOpts = append(libp2pOpts, libp2p.EnableRelay(circuit.OptActive))
}

// private network
if cfg.SwarmKey != "" {
Expand Down
23 changes: 0 additions & 23 deletions core/network/driver.go
Expand Up @@ -176,29 +176,6 @@ func (net *Network) Discover(ctx context.Context) {
// Connect ensures there is a connection between this host and the peer with
// given peer.ID.
func (net *Network) Connect(ctx context.Context, pi pstore.PeerInfo) error {
// first, check if we're already connectenet.
if net.host.Network().Connectedness(pi.ID) == inet.Connected {
return nil
}
// if we were given some addresses, keep + use them.
if len(pi.Addrs) > 0 {
net.host.Peerstore().AddAddrs(pi.ID, pi.Addrs, pstore.TempAddrTTL)
}

// Check if we have some addresses in our recent memory.
addrs := net.host.Peerstore().Addrs(pi.ID)
if len(addrs) < 1 {
// no addrs? find some with the routing system.
var err error
pi, err = net.host.Routing.FindPeer(ctx, pi.ID)
if err != nil {
return err
}
return net.host.Connect(ctx, pi)
}

// if we're here, we got some addrs. let's use our wrapped host to connect.
pi.Addrs = addrs
return net.host.Connect(ctx, pi)
}

Expand Down
2 changes: 1 addition & 1 deletion core/network/host/discovery.go
Expand Up @@ -59,7 +59,7 @@ func (d *BertyDiscovery) Advertise(ctx context.Context, ns string, opts ...disco
for i := range d.discoveries {
<-waitChans[i]
}

time.Sleep(time.Second)
return time.Now().Sub(t), nil
}

Expand Down
81 changes: 81 additions & 0 deletions core/network/host/host.go
Expand Up @@ -9,6 +9,7 @@ import (
host "github.com/libp2p/go-libp2p-host"
routing "github.com/libp2p/go-libp2p-routing"

circuit "github.com/libp2p/go-libp2p-circuit"
ifconnmgr "github.com/libp2p/go-libp2p-interface-connmgr"
inet "github.com/libp2p/go-libp2p-net"
peer "github.com/libp2p/go-libp2p-peer"
Expand Down Expand Up @@ -55,9 +56,80 @@ func NewBertyHost(ctx context.Context, host host.Host, opts *BertyHostOptions) (
// BertyHost's Connect differs in that if the host has no addresses for a
// given peer, it will use its routing system to try to find some.
func (bh *BertyHost) Connect(ctx context.Context, pi pstore.PeerInfo) error {

if bh.Network().Connectedness(pi.ID) == inet.Connected {
return nil
}
// if we were given some addresses, keep + use them.
if len(pi.Addrs) > 0 {
bh.Peerstore().AddAddrs(pi.ID, pi.Addrs, pstore.TempAddrTTL)
}

addrs := bh.Peerstore().Addrs(pi.ID)
if len(addrs) < 1 {
// no addrs? find some with the routing system.
var err error
addrs, err = bh.findPeerAddrs(ctx, pi.ID)
if err != nil {
return err
}
}
// Issue 448: if our address set includes routed specific relay addrs,
// we need to make sure the relay's addr itself is in the peerstore or else
// we wont be able to dial it.
for _, addr := range addrs {
_, err := addr.ValueForProtocol(circuit.P_CIRCUIT)
if err != nil {
// not a relay address
continue
}

if addr.Protocols()[0].Code != ma.P_P2P {
// not a routed relay specific address
continue
}

relay, _ := addr.ValueForProtocol(ma.P_P2P)

relayID, err := peer.IDFromString(relay)
if err != nil {
logger().Debug(fmt.Sprintf("failed to parse relay ID in address %s: %s", relay, err))
continue
}

if len(bh.Peerstore().Addrs(relayID)) > 0 {
// we already have addrs for this relay
continue
}

relayAddrs, err := bh.findPeerAddrs(ctx, relayID)
if err != nil {
logger().Debug(fmt.Sprintf("failed to find relay %s: %s", relay, err))
continue
}

bh.Peerstore().AddAddrs(relayID, relayAddrs, pstore.TempAddrTTL)
}

// if we're here, we got some addrs. let's use our wrapped host to connect.
pi.Addrs = addrs
return bh.Host.Connect(ctx, pi)
}

func (bh *BertyHost) findPeerAddrs(ctx context.Context, id peer.ID) ([]ma.Multiaddr, error) {
pi, err := bh.Routing.FindPeer(ctx, id)
if err != nil {
return nil, err // couldnt find any :(
}

if pi.ID != id {
err = fmt.Errorf("routing failure: provided addrs for different peer")
return nil, err
}

return pi.Addrs, nil
}

func (bh *BertyHost) ID() peer.ID {
return bh.Host.ID()
}
Expand Down Expand Up @@ -133,6 +205,15 @@ func (bh *BertyHost) newBestStream(ctx context.Context, p peer.ID) (inet.Stream,

func (bh *BertyHost) NewStream(ctx context.Context, p peer.ID, pids ...protocol.ID) (inet.Stream, error) {
logger().Debug("NEW_STREAM")

// Ensure we have a connection, with peer addresses resolved by the routing system (#207)
// It is not sufficient to let the underlying host connect, it will most likely not have
// any addresses for the peer without any prior connections.
err := bh.Connect(ctx, pstore.PeerInfo{ID: p})
if err != nil {
return nil, err
}

pref, err := bh.preferredProtocol(p, pids)
if err != nil {
return nil, err
Expand Down
5 changes: 1 addition & 4 deletions core/network/host/routing.go
Expand Up @@ -113,10 +113,7 @@ func (br *BertyRouting) SearchValue(ctx context.Context, ns string, opts ...ropt
}

func (br *BertyRouting) FindPeer(ctx context.Context, pid peer.ID) (pstore.PeerInfo, error) {
if err := br.isReady(ctx); err != nil {
return pstore.PeerInfo{}, nil
}

br.waitIsReady(ctx)
return br.dht.FindPeer(ctx, pid)

}
Expand Down
4 changes: 2 additions & 2 deletions core/network/options.go
Expand Up @@ -31,9 +31,9 @@ func WithDefaultOptions() config.Option {
EnablePing(),
// EnableMDNS(),
// EnableBLE(),
// EnableQUIC(),
EnableQUIC(),
EnablePrivateNetwork(config.DefaultSwarmKey),
EnableDHT(),
DisableDHT(),
EnableMetric(),
DisableHOP(),
)
Expand Down
1 change: 0 additions & 1 deletion core/network/test/network_test.go
Expand Up @@ -22,7 +22,6 @@ func Test(t *testing.T) {
net, err := network.New(ctx)
So(err, ShouldBeNil)
So(net, ShouldNotBeNil)

})

Convey("network with libp2p", t, FailureHalts, func() {
Expand Down

0 comments on commit e1270f1

Please sign in to comment.