Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
p2p
go-p2p-swarm
Commits
861161fc
Commit
861161fc
authored
May 31, 2016
by
Jeromy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
split peerstore from peer package
parent
d118f8ae
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
31 additions
and
25 deletions
+31
-25
dial_test.go
dial_test.go
+10
-9
peers_test.go
peers_test.go
+2
-1
simul_test.go
simul_test.go
+3
-2
swarm.go
swarm.go
+3
-2
swarm_addr_test.go
swarm_addr_test.go
+3
-3
swarm_net.go
swarm_net.go
+3
-2
swarm_test.go
swarm_test.go
+7
-6
No files found.
dial_test.go
View file @
861161fc
...
...
@@ -7,11 +7,12 @@ import (
"testing"
"time"
peer
"github.com/ipfs/go-libp2p-peer"
addrutil
"github.com/ipfs/go-libp2p/p2p/net/swarm/addr"
testutil
"github.com/ipfs/go-libp2p/testutil"
ci
"github.com/ipfs/go-libp2p/testutil/ci"
peer
"github.com/ipfs/go-libp2p-peer"
pstore
"github.com/ipfs/go-libp2p-peerstore"
ma
"github.com/jbenet/go-multiaddr"
manet
"github.com/jbenet/go-multiaddr-net"
context
"golang.org/x/net/context"
...
...
@@ -32,7 +33,7 @@ func TestBasicDial(t *testing.T) {
s1
:=
swarms
[
0
]
s2
:=
swarms
[
1
]
s1
.
peers
.
AddAddrs
(
s2
.
local
,
s2
.
ListenAddresses
(),
p
eer
.
PermanentAddrTTL
)
s1
.
peers
.
AddAddrs
(
s2
.
local
,
s2
.
ListenAddresses
(),
p
store
.
PermanentAddrTTL
)
c
,
err
:=
s1
.
Dial
(
ctx
,
s2
.
local
)
if
err
!=
nil
{
...
...
@@ -57,7 +58,7 @@ func TestDialWithNoListeners(t *testing.T) {
defer
closeSwarms
(
swarms
)
s2
:=
swarms
[
0
]
s1
.
peers
.
AddAddrs
(
s2
.
local
,
s2
.
ListenAddresses
(),
p
eer
.
PermanentAddrTTL
)
s1
.
peers
.
AddAddrs
(
s2
.
local
,
s2
.
ListenAddresses
(),
p
store
.
PermanentAddrTTL
)
c
,
err
:=
s1
.
Dial
(
ctx
,
s2
.
local
)
if
err
!=
nil
{
...
...
@@ -101,7 +102,7 @@ func TestSimultDials(t *testing.T) {
connect
:=
func
(
s
*
Swarm
,
dst
peer
.
ID
,
addr
ma
.
Multiaddr
)
{
// copy for other peer
log
.
Debugf
(
"TestSimultOpen: connecting: %s --> %s (%s)"
,
s
.
local
,
dst
,
addr
)
s
.
peers
.
AddAddr
(
dst
,
addr
,
p
eer
.
TempAddrTTL
)
s
.
peers
.
AddAddr
(
dst
,
addr
,
p
store
.
TempAddrTTL
)
if
_
,
err
:=
s
.
Dial
(
ctx
,
dst
);
err
!=
nil
{
t
.
Fatal
(
"error swarm dialing to peer"
,
err
)
}
...
...
@@ -178,7 +179,7 @@ func TestDialWait(t *testing.T) {
s2p
,
s2addr
,
s2l
:=
newSilentPeer
(
t
)
go
acceptAndHang
(
s2l
)
defer
s2l
.
Close
()
s1
.
peers
.
AddAddr
(
s2p
,
s2addr
,
p
eer
.
PermanentAddrTTL
)
s1
.
peers
.
AddAddr
(
s2p
,
s2addr
,
p
store
.
PermanentAddrTTL
)
before
:=
time
.
Now
()
if
c
,
err
:=
s1
.
Dial
(
ctx
,
s2p
);
err
==
nil
{
...
...
@@ -224,13 +225,13 @@ func TestDialBackoff(t *testing.T) {
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
s1
.
peers
.
AddAddrs
(
s2
.
local
,
s2addrs
,
p
eer
.
PermanentAddrTTL
)
s1
.
peers
.
AddAddrs
(
s2
.
local
,
s2addrs
,
p
store
.
PermanentAddrTTL
)
// dial to a non-existent peer.
s3p
,
s3addr
,
s3l
:=
newSilentPeer
(
t
)
go
acceptAndHang
(
s3l
)
defer
s3l
.
Close
()
s1
.
peers
.
AddAddr
(
s3p
,
s3addr
,
p
eer
.
PermanentAddrTTL
)
s1
.
peers
.
AddAddr
(
s3p
,
s3addr
,
p
store
.
PermanentAddrTTL
)
// in this test we will:
// 1) dial 10x to each node.
...
...
@@ -442,7 +443,7 @@ func TestDialBackoffClears(t *testing.T) {
defer
s2l
.
Close
()
// phase 1 -- dial to non-operational addresses
s1
.
peers
.
AddAddr
(
s2
.
local
,
s2bad
,
p
eer
.
PermanentAddrTTL
)
s1
.
peers
.
AddAddr
(
s2
.
local
,
s2bad
,
p
store
.
PermanentAddrTTL
)
before
:=
time
.
Now
()
if
c
,
err
:=
s1
.
Dial
(
ctx
,
s2
.
local
);
err
==
nil
{
...
...
@@ -472,7 +473,7 @@ func TestDialBackoffClears(t *testing.T) {
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
s1
.
peers
.
AddAddrs
(
s2
.
local
,
ifaceAddrs1
,
p
eer
.
PermanentAddrTTL
)
s1
.
peers
.
AddAddrs
(
s2
.
local
,
ifaceAddrs1
,
p
store
.
PermanentAddrTTL
)
if
_
,
err
:=
s1
.
Dial
(
ctx
,
s2
.
local
);
err
==
nil
{
t
.
Fatal
(
"should have failed to dial backed off peer"
)
...
...
peers_test.go
View file @
861161fc
...
...
@@ -4,6 +4,7 @@ import (
"testing"
peer
"github.com/ipfs/go-libp2p-peer"
pstore
"github.com/ipfs/go-libp2p-peerstore"
ma
"github.com/jbenet/go-multiaddr"
context
"golang.org/x/net/context"
)
...
...
@@ -17,7 +18,7 @@ func TestPeers(t *testing.T) {
connect
:=
func
(
s
*
Swarm
,
dst
peer
.
ID
,
addr
ma
.
Multiaddr
)
{
// TODO: make a DialAddr func.
s
.
peers
.
AddAddr
(
dst
,
addr
,
p
eer
.
PermanentAddrTTL
)
s
.
peers
.
AddAddr
(
dst
,
addr
,
p
store
.
PermanentAddrTTL
)
// t.Logf("connections from %s", s.LocalPeer())
// for _, c := range s.ConnectionsToPeer(dst) {
// t.Logf("connection from %s to %s: %v", s.LocalPeer(), dst, c)
...
...
simul_test.go
View file @
861161fc
...
...
@@ -6,9 +6,10 @@ import (
"testing"
"time"
peer
"github.com/ipfs/go-libp2p-peer"
ci
"github.com/ipfs/go-libp2p/testutil/ci"
peer
"github.com/ipfs/go-libp2p-peer"
pstore
"github.com/ipfs/go-libp2p-peerstore"
ma
"github.com/jbenet/go-multiaddr"
context
"golang.org/x/net/context"
)
...
...
@@ -26,7 +27,7 @@ func TestSimultOpen(t *testing.T) {
connect
:=
func
(
s
*
Swarm
,
dst
peer
.
ID
,
addr
ma
.
Multiaddr
)
{
// copy for other peer
log
.
Debugf
(
"TestSimultOpen: connecting: %s --> %s (%s)"
,
s
.
local
,
dst
,
addr
)
s
.
peers
.
AddAddr
(
dst
,
addr
,
p
eer
.
PermanentAddrTTL
)
s
.
peers
.
AddAddr
(
dst
,
addr
,
p
store
.
PermanentAddrTTL
)
if
_
,
err
:=
s
.
Dial
(
ctx
,
dst
);
err
!=
nil
{
t
.
Fatal
(
"error swarm dialing to peer"
,
err
)
}
...
...
swarm.go
View file @
861161fc
...
...
@@ -11,6 +11,7 @@ import (
"time"
peer
"github.com/ipfs/go-libp2p-peer"
pstore
"github.com/ipfs/go-libp2p-peerstore"
transport
"github.com/ipfs/go-libp2p-transport"
metrics
"github.com/ipfs/go-libp2p/p2p/metrics"
mconn
"github.com/ipfs/go-libp2p/p2p/metrics/conn"
...
...
@@ -67,7 +68,7 @@ func init() {
type
Swarm
struct
{
swarm
*
ps
.
Swarm
local
peer
.
ID
peers
p
eer
.
Peerstore
peers
p
store
.
Peerstore
connh
ConnHandler
dsync
dialsync
...
...
@@ -94,7 +95,7 @@ type Swarm struct {
// NewSwarm constructs a Swarm, with a Chan.
func
NewSwarm
(
ctx
context
.
Context
,
listenAddrs
[]
ma
.
Multiaddr
,
local
peer
.
ID
,
peers
p
eer
.
Peerstore
,
bwc
metrics
.
Reporter
)
(
*
Swarm
,
error
)
{
local
peer
.
ID
,
peers
p
store
.
Peerstore
,
bwc
metrics
.
Reporter
)
(
*
Swarm
,
error
)
{
listenAddrs
,
err
:=
filterAddrs
(
listenAddrs
)
if
err
!=
nil
{
...
...
swarm_addr_test.go
View file @
861161fc
...
...
@@ -3,11 +3,11 @@ package swarm
import
(
"testing"
peer
"github.com/ipfs/go-libp2p-peer"
metrics
"github.com/ipfs/go-libp2p/p2p/metrics"
addrutil
"github.com/ipfs/go-libp2p/p2p/net/swarm/addr"
testutil
"github.com/ipfs/go-libp2p/testutil"
pstore
"github.com/ipfs/go-libp2p-peerstore"
ma
"github.com/jbenet/go-multiaddr"
context
"golang.org/x/net/context"
)
...
...
@@ -63,7 +63,7 @@ func TestFilterAddrs(t *testing.T) {
t
.
Fatal
(
err
)
}
ps
:=
p
eer
.
NewPeerstore
()
ps
:=
p
store
.
NewPeerstore
()
ctx
:=
context
.
Background
()
if
_
,
err
:=
NewNetwork
(
ctx
,
bad
,
id
,
ps
,
metrics
.
NewBandwidthCounter
());
err
==
nil
{
...
...
@@ -111,7 +111,7 @@ func TestDialBadAddrs(t *testing.T) {
test
:=
func
(
a
ma
.
Multiaddr
)
{
p
:=
testutil
.
RandPeerIDFatal
(
t
)
s
.
peers
.
AddAddr
(
p
,
a
,
p
eer
.
PermanentAddrTTL
)
s
.
peers
.
AddAddr
(
p
,
a
,
p
store
.
PermanentAddrTTL
)
if
_
,
err
:=
s
.
Dial
(
ctx
,
p
);
err
==
nil
{
t
.
Error
(
"swarm should not dial: %s"
,
m
)
}
...
...
swarm_net.go
View file @
861161fc
...
...
@@ -4,6 +4,7 @@ import (
"fmt"
peer
"github.com/ipfs/go-libp2p-peer"
pstore
"github.com/ipfs/go-libp2p-peerstore"
metrics
"github.com/ipfs/go-libp2p/p2p/metrics"
inet
"github.com/ipfs/go-libp2p/p2p/net"
...
...
@@ -19,7 +20,7 @@ type Network Swarm
// NewNetwork constructs a new network and starts listening on given addresses.
func
NewNetwork
(
ctx
context
.
Context
,
listen
[]
ma
.
Multiaddr
,
local
peer
.
ID
,
peers
p
eer
.
Peerstore
,
bwc
metrics
.
Reporter
)
(
*
Network
,
error
)
{
peers
p
store
.
Peerstore
,
bwc
metrics
.
Reporter
)
(
*
Network
,
error
)
{
s
,
err
:=
NewSwarm
(
ctx
,
listen
,
local
,
peers
,
bwc
)
if
err
!=
nil
{
...
...
@@ -63,7 +64,7 @@ func (n *Network) Peers() []peer.ID {
}
// Peers returns the Peerstore, which tracks known peers
func
(
n
*
Network
)
Peerstore
()
p
eer
.
Peerstore
{
func
(
n
*
Network
)
Peerstore
()
p
store
.
Peerstore
{
return
n
.
Swarm
()
.
peers
}
...
...
swarm_test.go
View file @
861161fc
...
...
@@ -9,11 +9,12 @@ import (
"testing"
"time"
peer
"github.com/ipfs/go-libp2p-peer"
metrics
"github.com/ipfs/go-libp2p/p2p/metrics"
inet
"github.com/ipfs/go-libp2p/p2p/net"
testutil
"github.com/ipfs/go-libp2p/testutil"
peer
"github.com/ipfs/go-libp2p-peer"
pstore
"github.com/ipfs/go-libp2p-peerstore"
ma
"github.com/jbenet/go-multiaddr"
context
"golang.org/x/net/context"
)
...
...
@@ -52,7 +53,7 @@ func EchoStreamHandler(stream inet.Stream) {
func
makeDialOnlySwarm
(
ctx
context
.
Context
,
t
*
testing
.
T
)
*
Swarm
{
id
:=
testutil
.
RandIdentityOrFatal
(
t
)
peerstore
:=
p
eer
.
NewPeerstore
()
peerstore
:=
p
store
.
NewPeerstore
()
peerstore
.
AddPubKey
(
id
.
ID
(),
id
.
PublicKey
())
peerstore
.
AddPrivKey
(
id
.
ID
(),
id
.
PrivateKey
())
...
...
@@ -72,7 +73,7 @@ func makeSwarms(ctx context.Context, t *testing.T, num int) []*Swarm {
for
i
:=
0
;
i
<
num
;
i
++
{
localnp
:=
testutil
.
RandPeerNetParamsOrFatal
(
t
)
peerstore
:=
p
eer
.
NewPeerstore
()
peerstore
:=
p
store
.
NewPeerstore
()
peerstore
.
AddPubKey
(
localnp
.
ID
,
localnp
.
PubKey
)
peerstore
.
AddPrivKey
(
localnp
.
ID
,
localnp
.
PrivKey
)
...
...
@@ -94,7 +95,7 @@ func connectSwarms(t *testing.T, ctx context.Context, swarms []*Swarm) {
var
wg
sync
.
WaitGroup
connect
:=
func
(
s
*
Swarm
,
dst
peer
.
ID
,
addr
ma
.
Multiaddr
)
{
// TODO: make a DialAddr func.
s
.
peers
.
AddAddr
(
dst
,
addr
,
p
eer
.
PermanentAddrTTL
)
s
.
peers
.
AddAddr
(
dst
,
addr
,
p
store
.
PermanentAddrTTL
)
if
_
,
err
:=
s
.
Dial
(
ctx
,
dst
);
err
!=
nil
{
t
.
Fatal
(
"error swarm dialing to peer"
,
err
)
}
...
...
@@ -313,13 +314,13 @@ func TestAddrBlocking(t *testing.T) {
swarms
[
1
]
.
Filters
.
AddDialFilter
(
block
)
swarms
[
1
]
.
peers
.
AddAddr
(
swarms
[
0
]
.
LocalPeer
(),
swarms
[
0
]
.
ListenAddresses
()[
0
],
p
eer
.
PermanentAddrTTL
)
swarms
[
1
]
.
peers
.
AddAddr
(
swarms
[
0
]
.
LocalPeer
(),
swarms
[
0
]
.
ListenAddresses
()[
0
],
p
store
.
PermanentAddrTTL
)
_
,
err
=
swarms
[
1
]
.
Dial
(
ctx
,
swarms
[
0
]
.
LocalPeer
())
if
err
==
nil
{
t
.
Fatal
(
"dial should have failed"
)
}
swarms
[
0
]
.
peers
.
AddAddr
(
swarms
[
1
]
.
LocalPeer
(),
swarms
[
1
]
.
ListenAddresses
()[
0
],
p
eer
.
PermanentAddrTTL
)
swarms
[
0
]
.
peers
.
AddAddr
(
swarms
[
1
]
.
LocalPeer
(),
swarms
[
1
]
.
ListenAddresses
()[
0
],
p
store
.
PermanentAddrTTL
)
_
,
err
=
swarms
[
0
]
.
Dial
(
ctx
,
swarms
[
1
]
.
LocalPeer
())
if
err
==
nil
{
t
.
Fatal
(
"dial should have failed"
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment