Skip to content

Commit

Permalink
ensure testnet peers get evenly connected mesh
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Jeromy <jeromyj@gmail.com>
  • Loading branch information
whyrusleeping committed Jul 16, 2017
1 parent a93b44b commit 39e9328
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
4 changes: 2 additions & 2 deletions exchange/bitswap/testnet/peernet.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package bitswap

import (
context "context"
"context"
bsnet "github.com/ipfs/go-ipfs/exchange/bitswap/network"
mockrouting "github.com/ipfs/go-ipfs/routing/mock"
testutil "github.com/ipfs/go-ipfs/thirdparty/testutil"
Expand Down Expand Up @@ -37,4 +37,4 @@ func (pn *peernet) HasPeer(p peer.ID) bool {
return false
}

var _ Network = &peernet{}
var _ Network = (*peernet)(nil)
21 changes: 21 additions & 0 deletions exchange/bitswap/testnet/virtual.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,29 @@ import (
mockrouting "github.com/ipfs/go-ipfs/routing/mock"
delay "github.com/ipfs/go-ipfs/thirdparty/delay"
testutil "github.com/ipfs/go-ipfs/thirdparty/testutil"

routing "gx/ipfs/QmPjTrrSfE6TzLv6ya6VWhGcCgPrUAdcgrDcQyRDX2VyW1/go-libp2p-routing"
logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log"
cid "gx/ipfs/QmTprEaAA2A9bst5XH7exuyi5KzNMK3SEDNN8rBDnKWcUS/go-cid"
peer "gx/ipfs/QmXYjuNuxVzXKJCfWasQk1RqkhVLDM9jtUKhqc2WPQmFSB/go-libp2p-peer"
)

var log = logging.Logger("bstestnet")

func VirtualNetwork(rs mockrouting.Server, d delay.D) Network {
return &network{
clients: make(map[peer.ID]bsnet.Receiver),
delay: d,
routingserver: rs,
conns: make(map[string]struct{}),
}
}

type network struct {
clients map[peer.ID]bsnet.Receiver
routingserver mockrouting.Server
delay delay.D
conns map[string]struct{}
}

func (n *network) Adapter(p testutil.Identity) bsnet.BitSwapNetwork {
Expand Down Expand Up @@ -149,7 +155,22 @@ func (nc *networkClient) ConnectTo(_ context.Context, p peer.ID) error {
if !nc.network.HasPeer(p) {
return errors.New("no such peer in network")
}
tag := tagForPeers(nc.local, p)
if _, ok := nc.network.conns[tag]; ok {
log.Warning("ALREADY CONNECTED TO PEER (is this a reconnect? test lib needs fixing)")
return nil
}
nc.network.conns[tag] = struct{}{}
// TODO: add handling for disconnects

nc.network.clients[p].PeerConnected(nc.local)
nc.Receiver.PeerConnected(p)
return nil
}

func tagForPeers(a, b peer.ID) string {
if a < b {
return string(a + b)
}
return string(b + a)
}
2 changes: 1 addition & 1 deletion exchange/bitswap/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (g *SessionGenerator) Instances(n int) []Instance {
for i, inst := range instances {
for j := i + 1; j < len(instances); j++ {
oinst := instances[j]
inst.Exchange.PeerConnected(oinst.Peer)
inst.Exchange.network.ConnectTo(context.Background(), oinst.Peer)
}
}
return instances
Expand Down

0 comments on commit 39e9328

Please sign in to comment.