Skip to content
This repository was archived by the owner on Sep 9, 2022. It is now read-only.

Commit e65c36f

Browse files
authored
migrate to consolidated types (#78)
1 parent cd89397 commit e65c36f

10 files changed

+122
-135
lines changed

conn.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@ import (
55
"net"
66
"time"
77

8-
host "github.com/libp2p/go-libp2p-host"
9-
inet "github.com/libp2p/go-libp2p-net"
10-
pstore "github.com/libp2p/go-libp2p-peerstore"
8+
"github.com/libp2p/go-libp2p-core/host"
9+
"github.com/libp2p/go-libp2p-core/network"
10+
"github.com/libp2p/go-libp2p-core/peer"
11+
1112
ma "github.com/multiformats/go-multiaddr"
1213
manet "github.com/multiformats/go-multiaddr-net"
1314
)
1415

1516
type Conn struct {
16-
stream inet.Stream
17-
remote pstore.PeerInfo
17+
stream network.Stream
18+
remote peer.AddrInfo
1819
host host.Host
1920
}
2021

dial.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ import (
55
"fmt"
66
"math/rand"
77

8-
peer "github.com/libp2p/go-libp2p-peer"
9-
pstore "github.com/libp2p/go-libp2p-peerstore"
10-
tpt "github.com/libp2p/go-libp2p-transport"
8+
"github.com/libp2p/go-libp2p-core/peer"
9+
"github.com/libp2p/go-libp2p-core/transport"
1110
ma "github.com/multiformats/go-multiaddr"
1211
)
1312

14-
func (d *RelayTransport) Dial(ctx context.Context, a ma.Multiaddr, p peer.ID) (tpt.Conn, error) {
13+
func (d *RelayTransport) Dial(ctx context.Context, a ma.Multiaddr, p peer.ID) (transport.CapableConn, error) {
1514
c, err := d.Relay().Dial(ctx, a, p)
1615
if err != nil {
1716
return nil, err
@@ -37,7 +36,7 @@ func (r *Relay) Dial(ctx context.Context, a ma.Multiaddr, p peer.ID) (*Conn, err
3736
}
3837
}
3938

40-
dinfo := &pstore.PeerInfo{ID: p, Addrs: []ma.Multiaddr{}}
39+
dinfo := &peer.AddrInfo{ID: p, Addrs: []ma.Multiaddr{}}
4140
if len(destaddr.Bytes()) > 0 {
4241
dinfo.Addrs = append(dinfo.Addrs, destaddr)
4342
}
@@ -47,16 +46,16 @@ func (r *Relay) Dial(ctx context.Context, a ma.Multiaddr, p peer.ID) (*Conn, err
4746
return r.tryDialRelays(ctx, *dinfo)
4847
}
4948

50-
var rinfo *pstore.PeerInfo
51-
rinfo, err := pstore.InfoFromP2pAddr(relayaddr)
49+
var rinfo *peer.AddrInfo
50+
rinfo, err := peer.AddrInfoFromP2pAddr(relayaddr)
5251
if err != nil {
5352
return nil, fmt.Errorf("error parsing multiaddr '%s': %s", relayaddr.String(), err)
5453
}
5554

5655
return r.DialPeer(ctx, *rinfo, *dinfo)
5756
}
5857

59-
func (r *Relay) tryDialRelays(ctx context.Context, dinfo pstore.PeerInfo) (*Conn, error) {
58+
func (r *Relay) tryDialRelays(ctx context.Context, dinfo peer.AddrInfo) (*Conn, error) {
6059
var relays []peer.ID
6160
r.mx.Lock()
6261
for p := range r.relays {
@@ -76,7 +75,7 @@ func (r *Relay) tryDialRelays(ctx context.Context, dinfo pstore.PeerInfo) (*Conn
7675
}
7776

7877
rctx, cancel := context.WithTimeout(ctx, HopConnectTimeout)
79-
c, err := r.DialPeer(rctx, pstore.PeerInfo{ID: relay}, dinfo)
78+
c, err := r.DialPeer(rctx, peer.AddrInfo{ID: relay}, dinfo)
8079
cancel()
8180

8281
if err == nil {

go.mod

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,10 @@ require (
44
github.com/gogo/protobuf v1.2.1
55
github.com/ipfs/go-log v0.0.1
66
github.com/libp2p/go-buffer-pool v0.0.2
7-
github.com/libp2p/go-libp2p-blankhost v0.0.1
8-
github.com/libp2p/go-libp2p-host v0.0.3
9-
github.com/libp2p/go-libp2p-net v0.0.2
10-
github.com/libp2p/go-libp2p-peer v0.1.1
11-
github.com/libp2p/go-libp2p-peerstore v0.0.6
12-
github.com/libp2p/go-libp2p-swarm v0.0.6
13-
github.com/libp2p/go-libp2p-transport v0.0.5
14-
github.com/libp2p/go-libp2p-transport-upgrader v0.0.4
7+
github.com/libp2p/go-libp2p-blankhost v0.1.1
8+
github.com/libp2p/go-libp2p-core v0.0.1
9+
github.com/libp2p/go-libp2p-swarm v0.1.0
10+
github.com/libp2p/go-libp2p-transport-upgrader v0.1.1
1511
github.com/multiformats/go-multiaddr v0.0.4
1612
github.com/multiformats/go-multiaddr-net v0.0.1
1713
)

go.sum

Lines changed: 56 additions & 71 deletions
Large diffs are not rendered by default.

notify.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"context"
55
"time"
66

7-
inet "github.com/libp2p/go-libp2p-net"
8-
peer "github.com/libp2p/go-libp2p-peer"
7+
inet "github.com/libp2p/go-libp2p-core/network"
8+
peer "github.com/libp2p/go-libp2p-core/peer"
99
ma "github.com/multiformats/go-multiaddr"
1010
)
1111

relay.go

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@ import (
1010

1111
pb "github.com/libp2p/go-libp2p-circuit/pb"
1212

13-
logging "github.com/ipfs/go-log"
13+
"github.com/libp2p/go-libp2p-core/helpers"
14+
"github.com/libp2p/go-libp2p-core/host"
15+
"github.com/libp2p/go-libp2p-core/network"
16+
"github.com/libp2p/go-libp2p-core/peer"
17+
"github.com/libp2p/go-libp2p-core/peerstore"
18+
1419
pool "github.com/libp2p/go-buffer-pool"
15-
host "github.com/libp2p/go-libp2p-host"
16-
inet "github.com/libp2p/go-libp2p-net"
17-
peer "github.com/libp2p/go-libp2p-peer"
18-
pstore "github.com/libp2p/go-libp2p-peerstore"
1920
tptu "github.com/libp2p/go-libp2p-transport-upgrader"
21+
22+
logging "github.com/ipfs/go-log"
2023
ma "github.com/multiformats/go-multiaddr"
2124
)
2225

@@ -138,12 +141,12 @@ func (r *Relay) GetActiveHops() int32 {
138141
return atomic.LoadInt32(&r.liveHopCount)
139142
}
140143

141-
func (r *Relay) DialPeer(ctx context.Context, relay pstore.PeerInfo, dest pstore.PeerInfo) (*Conn, error) {
144+
func (r *Relay) DialPeer(ctx context.Context, relay peer.AddrInfo, dest peer.AddrInfo) (*Conn, error) {
142145

143146
log.Debugf("dialing peer %s through relay %s", dest.ID, relay.ID)
144147

145148
if len(relay.Addrs) > 0 {
146-
r.host.Peerstore().AddAddrs(relay.ID, relay.Addrs, pstore.TempAddrTTL)
149+
r.host.Peerstore().AddAddrs(relay.ID, relay.Addrs, peerstore.TempAddrTTL)
147150
}
148151

149152
s, err := r.host.NewStream(ctx, relay.ID, ProtoID)
@@ -219,7 +222,7 @@ func (r *Relay) CanHop(ctx context.Context, id peer.ID) (bool, error) {
219222
s.Reset()
220223
return false, err
221224
}
222-
if err := inet.FullClose(s); err != nil {
225+
if err := helpers.FullClose(s); err != nil {
223226
return false, err
224227
}
225228

@@ -230,7 +233,7 @@ func (r *Relay) CanHop(ctx context.Context, id peer.ID) (bool, error) {
230233
return msg.GetCode() == pb.CircuitRelay_SUCCESS, nil
231234
}
232235

233-
func (r *Relay) handleNewStream(s inet.Stream) {
236+
func (r *Relay) handleNewStream(s network.Stream) {
234237
log.Infof("new relay stream from: %s", s.Conn().RemotePeer())
235238

236239
rd := newDelimitedReader(s, maxMessageSize)
@@ -257,7 +260,7 @@ func (r *Relay) handleNewStream(s inet.Stream) {
257260
}
258261
}
259262

260-
func (r *Relay) handleHopStream(s inet.Stream, msg *pb.CircuitRelay) {
263+
func (r *Relay) handleHopStream(s network.Stream, msg *pb.CircuitRelay) {
261264
if !r.hop {
262265
r.handleError(s, pb.CircuitRelay_HOP_CANT_SPEAK_RELAY)
263266
return
@@ -300,15 +303,15 @@ func (r *Relay) handleHopStream(s inet.Stream, msg *pb.CircuitRelay) {
300303
defer cancel()
301304

302305
if !r.active {
303-
ctx = inet.WithNoDial(ctx, "relay hop")
306+
ctx = network.WithNoDial(ctx, "relay hop")
304307
} else if len(dst.Addrs) > 0 {
305-
r.host.Peerstore().AddAddrs(dst.ID, dst.Addrs, pstore.TempAddrTTL)
308+
r.host.Peerstore().AddAddrs(dst.ID, dst.Addrs, peerstore.TempAddrTTL)
306309
}
307310

308311
bs, err := r.host.NewStream(ctx, dst.ID, ProtoID)
309312
if err != nil {
310313
log.Debugf("error opening relay stream to %s: %s", dst.ID.Pretty(), err.Error())
311-
if err == inet.ErrNoConn {
314+
if err == network.ErrNoConn {
312315
r.handleError(s, pb.CircuitRelay_HOP_NO_CONN_TO_DST)
313316
} else {
314317
r.handleError(s, pb.CircuitRelay_HOP_CANT_DIAL_DST)
@@ -423,7 +426,7 @@ func (r *Relay) handleHopStream(s inet.Stream, msg *pb.CircuitRelay) {
423426
}()
424427
}
425428

426-
func (r *Relay) handleStopStream(s inet.Stream, msg *pb.CircuitRelay) {
429+
func (r *Relay) handleStopStream(s network.Stream, msg *pb.CircuitRelay) {
427430
src, err := peerToPeerInfo(msg.GetSrcPeer())
428431
if err != nil {
429432
r.handleError(s, pb.CircuitRelay_STOP_SRC_MULTIADDR_INVALID)
@@ -439,7 +442,7 @@ func (r *Relay) handleStopStream(s inet.Stream, msg *pb.CircuitRelay) {
439442
log.Infof("relay connection from: %s", src.ID)
440443

441444
if len(src.Addrs) > 0 {
442-
r.host.Peerstore().AddAddrs(src.ID, src.Addrs, pstore.TempAddrTTL)
445+
r.host.Peerstore().AddAddrs(src.ID, src.Addrs, peerstore.TempAddrTTL)
443446
}
444447

445448
select {
@@ -449,7 +452,7 @@ func (r *Relay) handleStopStream(s inet.Stream, msg *pb.CircuitRelay) {
449452
}
450453
}
451454

452-
func (r *Relay) handleCanHop(s inet.Stream, msg *pb.CircuitRelay) {
455+
func (r *Relay) handleCanHop(s network.Stream, msg *pb.CircuitRelay) {
453456
var err error
454457

455458
if r.hop {
@@ -462,22 +465,22 @@ func (r *Relay) handleCanHop(s inet.Stream, msg *pb.CircuitRelay) {
462465
s.Reset()
463466
log.Debugf("error writing relay response: %s", err.Error())
464467
} else {
465-
inet.FullClose(s)
468+
helpers.FullClose(s)
466469
}
467470
}
468471

469-
func (r *Relay) handleError(s inet.Stream, code pb.CircuitRelay_Status) {
472+
func (r *Relay) handleError(s network.Stream, code pb.CircuitRelay_Status) {
470473
log.Warningf("relay error: %s (%d)", pb.CircuitRelay_Status_name[int32(code)], code)
471474
err := r.writeResponse(s, code)
472475
if err != nil {
473476
s.Reset()
474477
log.Debugf("error writing relay response: %s", err.Error())
475478
} else {
476-
inet.FullClose(s)
479+
helpers.FullClose(s)
477480
}
478481
}
479482

480-
func (r *Relay) writeResponse(s inet.Stream, code pb.CircuitRelay_Status) error {
483+
func (r *Relay) writeResponse(s network.Stream, code pb.CircuitRelay_Status) error {
481484
wr := newDelimitedWriter(s)
482485

483486
var msg pb.CircuitRelay

relay_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import (
1414
pb "github.com/libp2p/go-libp2p-circuit/pb"
1515

1616
bhost "github.com/libp2p/go-libp2p-blankhost"
17-
host "github.com/libp2p/go-libp2p-host"
17+
"github.com/libp2p/go-libp2p-core/host"
18+
1819
swarm "github.com/libp2p/go-libp2p-swarm"
1920
swarmt "github.com/libp2p/go-libp2p-swarm/testing"
2021
ma "github.com/multiformats/go-multiaddr"

transport.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import (
44
"context"
55
"fmt"
66

7-
host "github.com/libp2p/go-libp2p-host"
8-
tpt "github.com/libp2p/go-libp2p-transport"
7+
"github.com/libp2p/go-libp2p-core/host"
8+
"github.com/libp2p/go-libp2p-core/transport"
9+
910
tptu "github.com/libp2p/go-libp2p-transport-upgrader"
1011
ma "github.com/multiformats/go-multiaddr"
1112
)
@@ -23,7 +24,7 @@ func init() {
2324
ma.AddProtocol(Protocol)
2425
}
2526

26-
var _ tpt.Transport = (*RelayTransport)(nil)
27+
var _ transport.Transport = (*RelayTransport)(nil)
2728

2829
type RelayTransport Relay
2930

@@ -35,7 +36,7 @@ func (r *Relay) Transport() *RelayTransport {
3536
return (*RelayTransport)(r)
3637
}
3738

38-
func (t *RelayTransport) Listen(laddr ma.Multiaddr) (tpt.Listener, error) {
39+
func (t *RelayTransport) Listen(laddr ma.Multiaddr) (transport.Listener, error) {
3940
// TODO: Ensure we have a connection to the relay, if specified. Also,
4041
// make sure the multiaddr makes sense.
4142
if !t.Relay().Matches(laddr) {
@@ -58,7 +59,7 @@ func (t *RelayTransport) Protocols() []int {
5859

5960
// AddRelayTransport constructs a relay and adds it as a transport to the host network.
6061
func AddRelayTransport(ctx context.Context, h host.Host, upgrader *tptu.Upgrader, opts ...RelayOpt) error {
61-
n, ok := h.Network().(tpt.Network)
62+
n, ok := h.Network().(transport.TransportNetwork)
6263
if !ok {
6364
return fmt.Errorf("%v is not a transport network", h.Network())
6465
}

transport_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ import (
1010

1111
. "github.com/libp2p/go-libp2p-circuit"
1212

13-
host "github.com/libp2p/go-libp2p-host"
14-
inet "github.com/libp2p/go-libp2p-net"
15-
pstore "github.com/libp2p/go-libp2p-peerstore"
13+
"github.com/libp2p/go-libp2p-core/host"
14+
"github.com/libp2p/go-libp2p-core/network"
15+
"github.com/libp2p/go-libp2p-core/peerstore"
16+
1617
swarm "github.com/libp2p/go-libp2p-swarm"
1718
swarmt "github.com/libp2p/go-libp2p-swarm/testing"
1819
ma "github.com/multiformats/go-multiaddr"
@@ -45,7 +46,7 @@ func testSetupRelay(t *testing.T, ctx context.Context) []host.Host {
4546

4647
time.Sleep(100 * time.Millisecond)
4748

48-
handler := func(s inet.Stream) {
49+
handler := func(s network.Stream) {
4950
_, err := s.Write(msg)
5051
if err != nil {
5152
t.Error(err)
@@ -72,7 +73,7 @@ func TestFullAddressTransportDial(t *testing.T) {
7273
rctx, rcancel := context.WithTimeout(ctx, time.Second)
7374
defer rcancel()
7475

75-
hosts[0].Peerstore().AddAddrs(hosts[2].ID(), []ma.Multiaddr{addr}, pstore.TempAddrTTL)
76+
hosts[0].Peerstore().AddAddrs(hosts[2].ID(), []ma.Multiaddr{addr}, peerstore.TempAddrTTL)
7677

7778
s, err := hosts[0].NewStream(rctx, hosts[2].ID(), TestProto)
7879
if err != nil {
@@ -103,7 +104,7 @@ func TestSpecificRelayTransportDial(t *testing.T) {
103104
rctx, rcancel := context.WithTimeout(ctx, time.Second)
104105
defer rcancel()
105106

106-
hosts[0].Peerstore().AddAddrs(hosts[2].ID(), []ma.Multiaddr{addr}, pstore.TempAddrTTL)
107+
hosts[0].Peerstore().AddAddrs(hosts[2].ID(), []ma.Multiaddr{addr}, peerstore.TempAddrTTL)
107108

108109
s, err := hosts[0].NewStream(rctx, hosts[2].ID(), TestProto)
109110
if err != nil {
@@ -134,7 +135,7 @@ func TestUnspecificRelayTransportDial(t *testing.T) {
134135
rctx, rcancel := context.WithTimeout(ctx, time.Second)
135136
defer rcancel()
136137

137-
hosts[0].Peerstore().AddAddrs(hosts[2].ID(), []ma.Multiaddr{addr}, pstore.TempAddrTTL)
138+
hosts[0].Peerstore().AddAddrs(hosts[2].ID(), []ma.Multiaddr{addr}, peerstore.TempAddrTTL)
138139

139140
s, err := hosts[0].NewStream(rctx, hosts[2].ID(), TestProto)
140141
if err != nil {

util.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,22 @@ import (
77

88
pb "github.com/libp2p/go-libp2p-circuit/pb"
99

10+
"github.com/libp2p/go-libp2p-core/peer"
11+
1012
ggio "github.com/gogo/protobuf/io"
1113
proto "github.com/gogo/protobuf/proto"
1214
pool "github.com/libp2p/go-buffer-pool"
13-
peer "github.com/libp2p/go-libp2p-peer"
14-
pstore "github.com/libp2p/go-libp2p-peerstore"
1515
ma "github.com/multiformats/go-multiaddr"
1616
)
1717

18-
func peerToPeerInfo(p *pb.CircuitRelay_Peer) (pstore.PeerInfo, error) {
18+
func peerToPeerInfo(p *pb.CircuitRelay_Peer) (peer.AddrInfo, error) {
1919
if p == nil {
20-
return pstore.PeerInfo{}, errors.New("nil peer")
20+
return peer.AddrInfo{}, errors.New("nil peer")
2121
}
2222

2323
id, err := peer.IDFromBytes(p.Id)
2424
if err != nil {
25-
return pstore.PeerInfo{}, err
25+
return peer.AddrInfo{}, err
2626
}
2727

2828
addrs := make([]ma.Multiaddr, 0, len(p.Addrs))
@@ -33,10 +33,10 @@ func peerToPeerInfo(p *pb.CircuitRelay_Peer) (pstore.PeerInfo, error) {
3333
}
3434
}
3535

36-
return pstore.PeerInfo{ID: id, Addrs: addrs}, nil
36+
return peer.AddrInfo{ID: id, Addrs: addrs}, nil
3737
}
3838

39-
func peerInfoToPeer(pi pstore.PeerInfo) *pb.CircuitRelay_Peer {
39+
func peerInfoToPeer(pi peer.AddrInfo) *pb.CircuitRelay_Peer {
4040
addrs := make([][]byte, len(pi.Addrs))
4141
for i, addr := range pi.Addrs {
4242
addrs[i] = addr.Bytes()

0 commit comments

Comments
 (0)