routed.go 3.51 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1
2
3
package routedhost

import (
4
	"context"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
5
6
7
	"fmt"
	"time"

Jeromy's avatar
Jeromy committed
8
	host "github.com/libp2p/go-libp2p-host"
Jeromy's avatar
Jeromy committed
9

Jeromy's avatar
Jeromy committed
10
	logging "github.com/ipfs/go-log"
Jeromy's avatar
Jeromy committed
11
	lgbl "github.com/libp2p/go-libp2p-loggables"
Jeromy's avatar
Jeromy committed
12
	inet "github.com/libp2p/go-libp2p-net"
Jeromy's avatar
Jeromy committed
13
14
	peer "github.com/libp2p/go-libp2p-peer"
	pstore "github.com/libp2p/go-libp2p-peerstore"
Jeromy's avatar
Jeromy committed
15
	protocol "github.com/libp2p/go-libp2p-protocol"
Jeromy's avatar
Jeromy committed
16
	ma "github.com/multiformats/go-multiaddr"
Jeromy's avatar
Jeromy committed
17
	msmux "github.com/whyrusleeping/go-multistream"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
18
19
)

Jeromy's avatar
Jeromy committed
20
var log = logging.Logger("routedhost")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
21
22
23
24
25
26
27
28
29
30

// AddressTTL is the expiry time for our addresses.
// We expire them quickly.
const AddressTTL = time.Second * 10

// RoutedHost is a p2p Host that includes a routing system.
// This allows the Host to find the addresses for peers when
// it does not have them.
type RoutedHost struct {
	host  host.Host // embedded other host.
Jeromy's avatar
Jeromy committed
31
32
33
34
	route Routing
}

type Routing interface {
Jeromy's avatar
Jeromy committed
35
	FindPeer(context.Context, peer.ID) (pstore.PeerInfo, error)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
36
37
}

Jeromy's avatar
Jeromy committed
38
func Wrap(h host.Host, r Routing) *RoutedHost {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
39
40
41
42
43
44
45
46
	return &RoutedHost{h, r}
}

// Connect ensures there is a connection between this host and the peer with
// given peer.ID. See (host.Host).Connect for more information.
//
// RoutedHost'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.
Jeromy's avatar
Jeromy committed
47
func (rh *RoutedHost) Connect(ctx context.Context, pi pstore.PeerInfo) error {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
48
49
50
51
52
53
54
	// first, check if we're already connected.
	if len(rh.Network().ConnsToPeer(pi.ID)) > 0 {
		return nil
	}

	// if we were given some addresses, keep + use them.
	if len(pi.Addrs) > 0 {
Jeromy's avatar
Jeromy committed
55
		rh.Peerstore().AddAddrs(pi.ID, pi.Addrs, pstore.TempAddrTTL)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
	}

	// Check if we have some addresses in our recent memory.
	addrs := rh.Peerstore().Addrs(pi.ID)
	if len(addrs) < 1 {

		// no addrs? find some with the routing system.
		pi2, err := rh.route.FindPeer(ctx, pi.ID)
		if err != nil {
			return err // couldnt find any :(
		}
		if pi2.ID != pi.ID {
			err = fmt.Errorf("routing failure: provided addrs for different peer")
			logRoutingErrDifferentPeers(ctx, pi.ID, pi2.ID, err)
			return err
		}
		addrs = pi2.Addrs
	}

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

func logRoutingErrDifferentPeers(ctx context.Context, wanted, got peer.ID, err error) {
	lm := make(lgbl.DeferredMap)
	lm["error"] = err
	lm["wantedPeer"] = func() interface{} { return wanted.Pretty() }
	lm["gotPeer"] = func() interface{} { return got.Pretty() }
	log.Event(ctx, "routingError", lm)
}

func (rh *RoutedHost) ID() peer.ID {
	return rh.host.ID()
}
Jeromy's avatar
Jeromy committed
91

Jeromy's avatar
Jeromy committed
92
func (rh *RoutedHost) Peerstore() pstore.Peerstore {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
93
94
	return rh.host.Peerstore()
}
Jeromy's avatar
Jeromy committed
95

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
96
97
98
func (rh *RoutedHost) Addrs() []ma.Multiaddr {
	return rh.host.Addrs()
}
Jeromy's avatar
Jeromy committed
99

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
100
101
102
func (rh *RoutedHost) Network() inet.Network {
	return rh.host.Network()
}
Jeromy's avatar
Jeromy committed
103

Jeromy's avatar
Jeromy committed
104
func (rh *RoutedHost) Mux() *msmux.MultistreamMuxer {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
105
106
	return rh.host.Mux()
}
Jeromy's avatar
Jeromy committed
107

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
108
109
110
func (rh *RoutedHost) SetStreamHandler(pid protocol.ID, handler inet.StreamHandler) {
	rh.host.SetStreamHandler(pid, handler)
}
Jeromy's avatar
Jeromy committed
111

112
113
114
115
func (rh *RoutedHost) SetStreamHandlerMatch(pid protocol.ID, m func(string) bool, handler inet.StreamHandler) {
	rh.host.SetStreamHandlerMatch(pid, m, handler)
}

Jeromy's avatar
Jeromy committed
116
117
118
119
func (rh *RoutedHost) RemoveStreamHandler(pid protocol.ID) {
	rh.host.RemoveStreamHandler(pid)
}

120
121
func (rh *RoutedHost) NewStream(ctx context.Context, p peer.ID, pids ...protocol.ID) (inet.Stream, error) {
	return rh.host.NewStream(ctx, p, pids...)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
122
123
124
125
126
}
func (rh *RoutedHost) Close() error {
	// no need to close IpfsRouting. we dont own it.
	return rh.host.Close()
}
Jeromy's avatar
Jeromy committed
127

128
var _ (host.Host) = (*RoutedHost)(nil)