interface.go 1.73 KB
Newer Older
Brian Tiger Chow's avatar
Brian Tiger Chow committed
1 2 3 4 5 6 7
// Package mock provides a virtual routing server. To use it, create a virtual
// routing server and use the Client() method to get a routing client
// (IpfsRouting). The server quacks like a DHT but is really a local in-memory
// hash table.
package mockrouting

import (
8 9
	"context"

10
	delay "github.com/ipfs/go-ipfs/thirdparty/delay"
11
	"github.com/ipfs/go-ipfs/thirdparty/testutil"
12

13
	routing "gx/ipfs/QmUrCwTDvJgmBbJVHu1HGEyqDaod3dR6sEkZkpxZk4u47c/go-libp2p-routing"
14
	pstore "gx/ipfs/QmXXCcQ7CLg5a81Ui9TTR35QcR4y7ZyihxwfjqaHfUVcVo/go-libp2p-peerstore"
George Antoniadis's avatar
George Antoniadis committed
15
	ds "gx/ipfs/QmbzuUusHqaLLoNTDEVLcSF6vZDHZDLPC7p4bztRvvkXxU/go-datastore"
16
	cid "gx/ipfs/QmcEcrBAMrwMyhSjXt4yfyPpzgSuV8HLHavnfmiKCSRqZU/go-cid"
17
	peer "gx/ipfs/QmfMmLGoKzCHDN7cGgk64PJr4iipzidDRME8HABSJqvmhC/go-libp2p-peer"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
18 19 20 21
)

// Server provides mockrouting Clients
type Server interface {
22 23
	Client(p testutil.Identity) Client
	ClientWithDatastore(context.Context, testutil.Identity, ds.Datastore) Client
Brian Tiger Chow's avatar
Brian Tiger Chow committed
24 25 26 27
}

// Client implements IpfsRouting
type Client interface {
28
	FindProviders(context.Context, *cid.Cid) ([]pstore.PeerInfo, error)
Brian Tiger Chow's avatar
Brian Tiger Chow committed
29 30 31 32 33
	routing.IpfsRouting
}

// NewServer returns a mockrouting Server
func NewServer() Server {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
34 35 36 37
	return NewServerWithDelay(DelayConfig{
		ValueVisibility: delay.Fixed(0),
		Query:           delay.Fixed(0),
	})
Brian Tiger Chow's avatar
Brian Tiger Chow committed
38 39 40
}

// NewServerWithDelay returns a mockrouting Server with a delay!
Brian Tiger Chow's avatar
Brian Tiger Chow committed
41
func NewServerWithDelay(conf DelayConfig) Server {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
42
	return &s{
43
		providers: make(map[string]map[peer.ID]providerRecord),
Brian Tiger Chow's avatar
Brian Tiger Chow committed
44
		delayConf: conf,
Brian Tiger Chow's avatar
Brian Tiger Chow committed
45 46
	}
}
Brian Tiger Chow's avatar
Brian Tiger Chow committed
47 48 49 50 51 52 53 54 55

type DelayConfig struct {
	// ValueVisibility is the time it takes for a value to be visible in the network
	// FIXME there _must_ be a better term for this
	ValueVisibility delay.D

	// Query is the time it takes to receive a response from a routing query
	Query delay.D
}