Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mocknet: use peer ID in peer address #476

Merged
merged 1 commit into from
Nov 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions p2p/net/mock/mock_net.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mocknet
import (
"context"
"fmt"
"net"
"sort"
"sync"

Expand All @@ -17,10 +18,13 @@ import (
peer "github.com/libp2p/go-libp2p-peer"
pstore "github.com/libp2p/go-libp2p-peerstore"
pstoremem "github.com/libp2p/go-libp2p-peerstore/pstoremem"
testutil "github.com/libp2p/go-testutil"
ma "github.com/multiformats/go-multiaddr"
)

// IP6 range that gets blackholed (in case our traffic ever makes it out onto
// the internet).
var blackholeIP6 = net.ParseIP("100::")

// mocknet implements mocknet.Mocknet
type mocknet struct {
nets map[peer.ID]*peernet
Expand Down Expand Up @@ -54,8 +58,20 @@ func (mn *mocknet) GenPeer() (host.Host, error) {
if err != nil {
return nil, err
}

a := testutil.RandLocalTCPAddress()
id, err := peer.IDFromPrivateKey(sk)
if err != nil {
return nil, err
}
suffix := id
if len(id) > 8 {
suffix = id[len(id)-8:]
}
ip := append(net.IP{}, blackholeIP6...)
copy(ip[net.IPv6len-len(suffix):], suffix)
a, err := ma.NewMultiaddr(fmt.Sprintf("/ip6/%s/tcp/4242", ip))
if err != nil {
return nil, fmt.Errorf("failed to create test multiaddr: %s", err)
}

h, err := mn.AddPeer(sk, a)
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions p2p/net/mock/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,14 @@ func TestLimitedStreams(t *testing.T) {
t.Fatal("Expected 2ish seconds but got ", time.Since(before))
}
}
func TestFuzzManyPeers(t *testing.T) {
for i := 0; i < 50000; i++ {
_, err := FullMeshConnected(context.Background(), 2)
if err != nil {
t.Fatal(err)
}
}
}

func TestStreamsWithLatency(t *testing.T) {
latency := time.Millisecond * 500
Expand Down