Commit 6da57c67 authored by Brian Tiger Chow's avatar Brian Tiger Chow

feat(bitswap/testnet) impl a version of bitswap testnet that uses mockpeernet under the hood

License: MIT
Signed-off-by: default avatarBrian Tiger Chow <brian@perfmode.com>
parent 85ce94c5
......@@ -8,6 +8,7 @@ import (
"testing"
"time"
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-random"
blockservice "github.com/jbenet/go-ipfs/blockservice"
bitswap "github.com/jbenet/go-ipfs/exchange/bitswap"
......@@ -85,15 +86,16 @@ func RandomBytes(n int64) []byte {
}
func AddCatBytes(data []byte, conf Config) error {
sessionGenerator := bitswap.NewSessionGenerator(
tn.VirtualNetwork(
mockrouting.NewServerWithDelay(mockrouting.DelayConfig{
Query: delay.Fixed(conf.RoutingLatency),
ValueVisibility: delay.Fixed(conf.RoutingLatency),
}),
delay.Fixed(conf.NetworkLatency)), // TODO rename VirtualNetwork
)
ctx := context.Background()
rs := mockrouting.NewServerWithDelay(mockrouting.DelayConfig{
Query: delay.Fixed(conf.RoutingLatency),
ValueVisibility: delay.Fixed(conf.RoutingLatency),
})
net, err := tn.StreamNetWithDelay(ctx, rs, delay.Fixed(conf.NetworkLatency))
if err != nil {
return errors.Wrap(err)
}
sessionGenerator := bitswap.NewSessionGenerator(net)
defer sessionGenerator.Close()
adder := sessionGenerator.Next()
......
package bitswap
import (
"math"
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
bsnet "github.com/jbenet/go-ipfs/exchange/bitswap/network"
mockpeernet "github.com/jbenet/go-ipfs/net/mock"
peer "github.com/jbenet/go-ipfs/peer"
mockrouting "github.com/jbenet/go-ipfs/routing/mock"
delay "github.com/jbenet/go-ipfs/util/delay"
testutil "github.com/jbenet/go-ipfs/util/testutil"
)
type peernet struct {
mockpeernet.Mocknet
routingserver mockrouting.Server
}
func StreamNetWithDelay(
ctx context.Context,
rs mockrouting.Server,
d delay.D) (Network, error) {
net := mockpeernet.New(ctx)
net.SetLinkDefaults(mockpeernet.LinkOptions{
Latency: d.Get(),
Bandwidth: math.MaxInt32, // TODO inject
})
return &peernet{net, rs}, nil
}
func (pn *peernet) Adapter(p testutil.Peer) bsnet.BitSwapNetwork {
peers := pn.Mocknet.Peers()
client, err := pn.Mocknet.AddPeer(p.PrivateKey(), p.Address())
if err != nil {
panic(err.Error())
}
for _, other := range peers {
pn.Mocknet.LinkPeers(p.ID(), other)
}
routing := pn.routingserver.Client(peer.PeerInfo{ID: p.ID()})
return bsnet.NewFromIpfsNetwork(client, routing)
}
func (pn *peernet) HasPeer(p peer.ID) bool {
for _, member := range pn.Mocknet.Peers() {
if p == member {
return true
}
}
return false
}
var _ Network = &peernet{}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment