core.go 2.02 KB
Newer Older
Brian Tiger Chow's avatar
Brian Tiger Chow committed
1 2 3 4 5 6
package epictest

import (
	context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
	datastore "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
	sync "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
7

Brian Tiger Chow's avatar
Brian Tiger Chow committed
8 9
	blockstore "github.com/jbenet/go-ipfs/blocks/blockstore"
	blockservice "github.com/jbenet/go-ipfs/blockservice"
10
	core "github.com/jbenet/go-ipfs/core"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
11 12 13
	bitswap "github.com/jbenet/go-ipfs/exchange/bitswap"
	bsnet "github.com/jbenet/go-ipfs/exchange/bitswap/network"
	merkledag "github.com/jbenet/go-ipfs/merkledag"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
14
	host "github.com/jbenet/go-ipfs/p2p/host"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
15
	peer "github.com/jbenet/go-ipfs/p2p/peer"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
16 17 18
	dht "github.com/jbenet/go-ipfs/routing/dht"
	"github.com/jbenet/go-ipfs/util/datastore2"
	delay "github.com/jbenet/go-ipfs/util/delay"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
19
	eventlog "github.com/jbenet/go-ipfs/util/eventlog"
20
	testutil "github.com/jbenet/go-ipfs/util/testutil"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
21 22
)

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
23 24
var log = eventlog.Logger("epictest")

25
func MocknetTestRepo(p peer.ID, h host.Host, conf testutil.LatencyConfig) core.ConfigOption {
26
	return func(ctx context.Context) (*core.IpfsNode, error) {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
27 28 29
		const kWriteCacheElems = 100
		const alwaysSendToPeer = true
		dsDelay := delay.Fixed(conf.BlockstoreLatency)
30
		ds := datastore2.CloserWrap(sync.MutexWrap(datastore2.WithDelay(datastore.NewMapDatastore(), dsDelay)))
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
31

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
32 33 34
		log.Debugf("MocknetTestRepo: %s %s %s", p, h.ID(), h)
		dhtt := dht.NewDHT(ctx, h, ds)
		bsn := bsnet.NewFromIpfsHost(h, dhtt)
Brian Tiger Chow's avatar
Brian Tiger Chow committed
35 36 37 38 39
		bstore, err := blockstore.WriteCached(blockstore.NewBlockstore(ds), kWriteCacheElems)
		if err != nil {
			return nil, err
		}
		exch := bitswap.New(ctx, p, bsn, bstore, alwaysSendToPeer)
40 41 42 43
		blockservice, err := blockservice.New(bstore, exch)
		if err != nil {
			return nil, err
		}
44 45 46 47 48 49
		return &core.IpfsNode{
			Peerstore:  h.Peerstore(),
			Blockstore: bstore,
			Exchange:   exch,
			Datastore:  ds,
			PeerHost:   h,
50 51
			DAG:        merkledag.NewDAGService(blockservice),
			Blocks:     blockservice,
52 53 54
			Routing:    dhtt,
			Identity:   p,
			DHT:        dhtt,
Brian Tiger Chow's avatar
Brian Tiger Chow committed
55 56 57
		}, nil
	}
}