interface.go 1.75 KB
Newer Older
1 2 3 4
// Package mockrouting 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.
Brian Tiger Chow's avatar
Brian Tiger Chow committed
5 6 7
package mockrouting

import (
8 9
	"context"

10 11 12 13 14
	ds "github.com/ipfs/go-datastore"
	delay "github.com/ipfs/go-ipfs-delay"
	peer "github.com/libp2p/go-libp2p-peer"
	routing "github.com/libp2p/go-libp2p-routing"
	"github.com/libp2p/go-testutil"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
15 16
)

Steven Allen's avatar
Steven Allen committed
17 18 19 20 21 22
// MockValidator is a record validator that always returns success.
type MockValidator struct{}

func (MockValidator) Validate(_ string, _ []byte) error        { return nil }
func (MockValidator) Select(_ string, _ [][]byte) (int, error) { return 0, nil }

Brian Tiger Chow's avatar
Brian Tiger Chow committed
23 24
// Server provides mockrouting Clients
type Server interface {
25 26
	Client(p testutil.Identity) Client
	ClientWithDatastore(context.Context, testutil.Identity, ds.Datastore) Client
Brian Tiger Chow's avatar
Brian Tiger Chow committed
27 28 29 30 31 32 33 34 35
}

// Client implements IpfsRouting
type Client interface {
	routing.IpfsRouting
}

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

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

50 51
// DelayConfig can be used to configured the fake delays of a mock server.
// Use with NewServerWithDelay().
Brian Tiger Chow's avatar
Brian Tiger Chow committed
52 53 54 55 56 57 58 59
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
}