Skip to content

Commit

Permalink
Merge pull request #3107 from ipfs/feat/routing/interfaces
Browse files Browse the repository at this point in the history
routing: rework interfaces to make separation easier
  • Loading branch information
whyrusleeping authored Aug 21, 2016
2 parents eac1c63 + ea8683a commit 37cf360
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 28 deletions.
4 changes: 2 additions & 2 deletions exchange/bitswap/network/ipfs_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
var log = logging.Logger("bitswap_network")

// NewFromIpfsHost returns a BitSwapNetwork supported by underlying IPFS host
func NewFromIpfsHost(host host.Host, r routing.IpfsRouting) BitSwapNetwork {
func NewFromIpfsHost(host host.Host, r routing.ContentRouting) BitSwapNetwork {
bitswapNetwork := impl{
host: host,
routing: r,
Expand All @@ -36,7 +36,7 @@ func NewFromIpfsHost(host host.Host, r routing.IpfsRouting) BitSwapNetwork {
// NetMessage objects, into the bitswap network interface.
type impl struct {
host host.Host
routing routing.IpfsRouting
routing routing.ContentRouting

// inbound messages from the network are forwarded to the receiver
receiver Receiver
Expand Down
4 changes: 2 additions & 2 deletions exchange/reprovide/reprovide.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ var log = logging.Logger("reprovider")

type Reprovider struct {
// The routing system to provide values through
rsys routing.IpfsRouting
rsys routing.ContentRouting

// The backing store for blocks to be provided
bstore blocks.Blockstore
}

func NewReprovider(rsys routing.IpfsRouting, bstore blocks.Blockstore) *Reprovider {
func NewReprovider(rsys routing.ContentRouting, bstore blocks.Blockstore) *Reprovider {
return &Reprovider{
rsys: rsys,
bstore: bstore,
Expand Down
2 changes: 1 addition & 1 deletion namesys/namesys.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type mpns struct {
}

// NewNameSystem will construct the IPFS naming system based on Routing
func NewNameSystem(r routing.IpfsRouting, ds ds.Datastore, cachesize int) NameSystem {
func NewNameSystem(r routing.ValueStore, ds ds.Datastore, cachesize int) NameSystem {
return &mpns{
resolvers: map[string]resolver{
"dns": newDNSResolver(),
Expand Down
10 changes: 5 additions & 5 deletions namesys/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ var PublishPutValTimeout = time.Minute
// ipnsPublisher is capable of publishing and resolving names to the IPFS
// routing system.
type ipnsPublisher struct {
routing routing.IpfsRouting
routing routing.ValueStore
ds ds.Datastore
}

// NewRoutingPublisher constructs a publisher for the IPFS Routing name system.
func NewRoutingPublisher(route routing.IpfsRouting, ds ds.Datastore) *ipnsPublisher {
func NewRoutingPublisher(route routing.ValueStore, ds ds.Datastore) *ipnsPublisher {
if ds == nil {
panic("nil datastore")
}
Expand Down Expand Up @@ -134,7 +134,7 @@ func checkCtxTTL(ctx context.Context) (time.Duration, bool) {
return d, ok
}

func PutRecordToRouting(ctx context.Context, k ci.PrivKey, value path.Path, seqnum uint64, eol time.Time, r routing.IpfsRouting, id peer.ID) error {
func PutRecordToRouting(ctx context.Context, k ci.PrivKey, value path.Path, seqnum uint64, eol time.Time, r routing.ValueStore, id peer.ID) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()

Expand Down Expand Up @@ -181,7 +181,7 @@ func waitOnErrChan(ctx context.Context, errs chan error) error {
}
}

func PublishPublicKey(ctx context.Context, r routing.IpfsRouting, k key.Key, pubk ci.PubKey) error {
func PublishPublicKey(ctx context.Context, r routing.ValueStore, k key.Key, pubk ci.PubKey) error {
log.Debugf("Storing pubkey at: %s", k)
pkbytes, err := pubk.Bytes()
if err != nil {
Expand All @@ -199,7 +199,7 @@ func PublishPublicKey(ctx context.Context, r routing.IpfsRouting, k key.Key, pub
return nil
}

func PublishEntry(ctx context.Context, r routing.IpfsRouting, ipnskey key.Key, rec *pb.IpnsEntry) error {
func PublishEntry(ctx context.Context, r routing.ValueStore, ipnskey key.Key, rec *pb.IpnsEntry) error {
timectx, cancel := context.WithTimeout(ctx, PublishPutValTimeout)
defer cancel()

Expand Down
4 changes: 2 additions & 2 deletions namesys/republisher/repub.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var DefaultRebroadcastInterval = time.Hour * 4
const DefaultRecordLifetime = time.Hour * 24

type Republisher struct {
r routing.IpfsRouting
r routing.ValueStore
ds ds.Datastore
ps pstore.Peerstore

Expand All @@ -44,7 +44,7 @@ type Republisher struct {
entries map[peer.ID]struct{}
}

func NewRepublisher(r routing.IpfsRouting, ds ds.Datastore, ps pstore.Peerstore) *Republisher {
func NewRepublisher(r routing.ValueStore, ds ds.Datastore, ps pstore.Peerstore) *Republisher {
return &Republisher{
r: r,
ps: ps,
Expand Down
4 changes: 2 additions & 2 deletions namesys/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var log = logging.Logger("namesys")

// routingResolver implements NSResolver for the main IPFS SFS-like naming
type routingResolver struct {
routing routing.IpfsRouting
routing routing.ValueStore

cache *lru.Cache
}
Expand Down Expand Up @@ -88,7 +88,7 @@ type cacheEntry struct {
// to implement SFS-like naming on top.
// cachesize is the limit of the number of entries in the lru cache. Setting it
// to '0' will disable caching.
func NewRoutingResolver(route routing.IpfsRouting, cachesize int) *routingResolver {
func NewRoutingResolver(route routing.ValueStore, cachesize int) *routingResolver {
if route == nil {
panic("attempt to create resolver with nil routing system")
}
Expand Down
42 changes: 28 additions & 14 deletions routing/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,27 @@ import (
// ErrNotFound is returned when a search fails to find anything
var ErrNotFound = errors.New("routing: not found")

// IpfsRouting is the routing module interface
// It is implemented by things like DHTs, etc.
type IpfsRouting interface {
// ContentRouting is a value provider layer of indirection. It is used to find
// information about who has what content.
type ContentRouting interface {
// Announce that this node can provide value for given key
Provide(context.Context, key.Key) error

// Search for peers who are able to provide a given key
FindProvidersAsync(context.Context, key.Key, int) <-chan pstore.PeerInfo
}

// PeerRouting is a way to find information about certain peers.
// This can be implemented by a simple lookup table, a tracking server,
// or even a DHT.
type PeerRouting interface {
// Find specific Peer
// FindPeer searches for a peer with given ID, returns a pstore.PeerInfo
// with relevant addresses.
FindPeer(context.Context, peer.ID) (pstore.PeerInfo, error)
}

type ValueStore interface {
// Basic Put/Get

// PutValue adds value corresponding to given Key.
Expand All @@ -38,17 +54,15 @@ type IpfsRouting interface {
// As a result, a value of '1' is mostly useful for cases where the record
// in question has only one valid value (such as public keys)
GetValues(c context.Context, k key.Key, count int) ([]RecvdVal, error)
}

// Value provider layer of indirection.
// This is what DSHTs (Coral and MainlineDHT) do to store large values in a DHT.

// Announce that this node can provide value for given key
Provide(context.Context, key.Key) error

// Find specific Peer
// FindPeer searches for a peer with given ID, returns a pstore.PeerInfo
// with relevant addresses.
FindPeer(context.Context, peer.ID) (pstore.PeerInfo, error)
// IpfsRouting is the combination of different routing types that ipfs
// uses. It can be satisfied by a single item (such as a DHT) or multiple
// different pieces that are more optimized to each task.
type IpfsRouting interface {
ContentRouting
PeerRouting
ValueStore

// Bootstrap allows callers to hint to the routing system to get into a
// Boostrapped state
Expand All @@ -74,7 +88,7 @@ func KeyForPublicKey(id peer.ID) key.Key {
return key.Key("/pk/" + string(id))
}

func GetPublicKey(r IpfsRouting, ctx context.Context, pkhash []byte) (ci.PubKey, error) {
func GetPublicKey(r ValueStore, ctx context.Context, pkhash []byte) (ci.PubKey, error) {
if dht, ok := r.(PubKeyFetcher); ok {
// If we have a DHT as our routing system, use optimized fetcher
return dht.GetPublicKey(ctx, peer.ID(pkhash))
Expand Down

0 comments on commit 37cf360

Please sign in to comment.