@cryptotaxi247 / kubo / commits / ea8683aca

routing: rework interfaces to make separation easier

License: MIT Signed-off-by: Jeromy <why@ipfs.io>

Jeromy committed Aug 20, 2016 at 11:30 UTC ea8683aca7a1e3aaa1c5739ed15b34ca6f546d55
7 files changed +42 -28
exchange/bitswap/network/ipfs_impl.go
+2 -2
@@ -20,7 +20,7 @@ import (
20 var log = logging.Logger("bitswap_network")
21
22 // NewFromIpfsHost returns a BitSwapNetwork supported by underlying IPFS host
23 -func NewFromIpfsHost(host host.Host, r routing.IpfsRouting) BitSwapNetwork {
23 +func NewFromIpfsHost(host host.Host, r routing.ContentRouting) BitSwapNetwork {
24 bitswapNetwork := impl{
25 host: host,
26 routing: r,
@@ -36,7 +36,7 @@ func NewFromIpfsHost(host host.Host, r routing.IpfsRouting) BitSwapNetwork {
36 // NetMessage objects, into the bitswap network interface.
37 type impl struct {
38 host host.Host
39 - routing routing.IpfsRouting
39 + routing routing.ContentRouting
40
41 // inbound messages from the network are forwarded to the receiver
42 receiver Receiver
exchange/reprovide/reprovide.go
+2 -2
@@ -15,13 +15,13 @@ var log = logging.Logger("reprovider")
15
16 type Reprovider struct {
17 // The routing system to provide values through
18 - rsys routing.IpfsRouting
18 + rsys routing.ContentRouting
19
20 // The backing store for blocks to be provided
21 bstore blocks.Blockstore
22 }
23
24 -func NewReprovider(rsys routing.IpfsRouting, bstore blocks.Blockstore) *Reprovider {
24 +func NewReprovider(rsys routing.ContentRouting, bstore blocks.Blockstore) *Reprovider {
25 return &Reprovider{
26 rsys: rsys,
27 bstore: bstore,
namesys/namesys.go
+1 -1
@@ -26,7 +26,7 @@ type mpns struct {
26 }
27
28 // NewNameSystem will construct the IPFS naming system based on Routing
29 -func NewNameSystem(r routing.IpfsRouting, ds ds.Datastore, cachesize int) NameSystem {
29 +func NewNameSystem(r routing.ValueStore, ds ds.Datastore, cachesize int) NameSystem {
30 return &mpns{
31 resolvers: map[string]resolver{
32 "dns": newDNSResolver(),
namesys/publisher.go
+5 -5
@@ -37,12 +37,12 @@ var PublishPutValTimeout = time.Minute
37 // ipnsPublisher is capable of publishing and resolving names to the IPFS
38 // routing system.
39 type ipnsPublisher struct {
40 - routing routing.IpfsRouting
40 + routing routing.ValueStore
41 ds ds.Datastore
42 }
43
44 // NewRoutingPublisher constructs a publisher for the IPFS Routing name system.
45 -func NewRoutingPublisher(route routing.IpfsRouting, ds ds.Datastore) *ipnsPublisher {
45 +func NewRoutingPublisher(route routing.ValueStore, ds ds.Datastore) *ipnsPublisher {
46 if ds == nil {
47 panic("nil datastore")
48 }
@@ -134,7 +134,7 @@ func checkCtxTTL(ctx context.Context) (time.Duration, bool) {
134 return d, ok
135 }
136
137 -func PutRecordToRouting(ctx context.Context, k ci.PrivKey, value path.Path, seqnum uint64, eol time.Time, r routing.IpfsRouting, id peer.ID) error {
137 +func PutRecordToRouting(ctx context.Context, k ci.PrivKey, value path.Path, seqnum uint64, eol time.Time, r routing.ValueStore, id peer.ID) error {
138 ctx, cancel := context.WithCancel(ctx)
139 defer cancel()
140
@@ -181,7 +181,7 @@ func waitOnErrChan(ctx context.Context, errs chan error) error {
181 }
182 }
183
184 -func PublishPublicKey(ctx context.Context, r routing.IpfsRouting, k key.Key, pubk ci.PubKey) error {
184 +func PublishPublicKey(ctx context.Context, r routing.ValueStore, k key.Key, pubk ci.PubKey) error {
185 log.Debugf("Storing pubkey at: %s", k)
186 pkbytes, err := pubk.Bytes()
187 if err != nil {
@@ -199,7 +199,7 @@ func PublishPublicKey(ctx context.Context, r routing.IpfsRouting, k key.Key, pub
199 return nil
200 }
201
202 -func PublishEntry(ctx context.Context, r routing.IpfsRouting, ipnskey key.Key, rec *pb.IpnsEntry) error {
202 +func PublishEntry(ctx context.Context, r routing.ValueStore, ipnskey key.Key, rec *pb.IpnsEntry) error {
203 timectx, cancel := context.WithTimeout(ctx, PublishPutValTimeout)
204 defer cancel()
205
namesys/republisher/repub.go
+2 -2
@@ -31,7 +31,7 @@ var DefaultRebroadcastInterval = time.Hour * 4
31 const DefaultRecordLifetime = time.Hour * 24
32
33 type Republisher struct {
34 - r routing.IpfsRouting
34 + r routing.ValueStore
35 ds ds.Datastore
36 ps pstore.Peerstore
37
@@ -44,7 +44,7 @@ type Republisher struct {
44 entries map[peer.ID]struct{}
45 }
46
47 -func NewRepublisher(r routing.IpfsRouting, ds ds.Datastore, ps pstore.Peerstore) *Republisher {
47 +func NewRepublisher(r routing.ValueStore, ds ds.Datastore, ps pstore.Peerstore) *Republisher {
48 return &Republisher{
49 r: r,
50 ps: ps,
namesys/routing.go
+2 -2
@@ -23,7 +23,7 @@ var log = logging.Logger("namesys")
23
24 // routingResolver implements NSResolver for the main IPFS SFS-like naming
25 type routingResolver struct {
26 - routing routing.IpfsRouting
26 + routing routing.ValueStore
27
28 cache *lru.Cache
29 }
@@ -88,7 +88,7 @@ type cacheEntry struct {
88 // to implement SFS-like naming on top.
89 // cachesize is the limit of the number of entries in the lru cache. Setting it
90 // to '0' will disable caching.
91 -func NewRoutingResolver(route routing.IpfsRouting, cachesize int) *routingResolver {
91 +func NewRoutingResolver(route routing.ValueStore, cachesize int) *routingResolver {
92 if route == nil {
93 panic("attempt to create resolver with nil routing system")
94 }
routing/routing.go
+28 -14
@@ -14,11 +14,27 @@ import (
14 // ErrNotFound is returned when a search fails to find anything
15 var ErrNotFound = errors.New("routing: not found")
16
17 -// IpfsRouting is the routing module interface
18 -// It is implemented by things like DHTs, etc.
19 -type IpfsRouting interface {
17 +// ContentRouting is a value provider layer of indirection. It is used to find
18 +// information about who has what content.
19 +type ContentRouting interface {
20 + // Announce that this node can provide value for given key
21 + Provide(context.Context, key.Key) error
22 +
23 + // Search for peers who are able to provide a given key
24 FindProvidersAsync(context.Context, key.Key, int) <-chan pstore.PeerInfo
25 +}
26 +
27 +// PeerRouting is a way to find information about certain peers.
28 +// This can be implemented by a simple lookup table, a tracking server,
29 +// or even a DHT.
30 +type PeerRouting interface {
31 + // Find specific Peer
32 + // FindPeer searches for a peer with given ID, returns a pstore.PeerInfo
33 + // with relevant addresses.
34 + FindPeer(context.Context, peer.ID) (pstore.PeerInfo, error)
35 +}
36
37 +type ValueStore interface {
38 // Basic Put/Get
39
40 // PutValue adds value corresponding to given Key.
@@ -38,17 +54,15 @@ type IpfsRouting interface {
54 // As a result, a value of '1' is mostly useful for cases where the record
55 // in question has only one valid value (such as public keys)
56 GetValues(c context.Context, k key.Key, count int) ([]RecvdVal, error)
57 +}
58
42 - // Value provider layer of indirection.
43 - // This is what DSHTs (Coral and MainlineDHT) do to store large values in a DHT.
44 -
45 - // Announce that this node can provide value for given key
46 - Provide(context.Context, key.Key) error
47 -
48 - // Find specific Peer
49 - // FindPeer searches for a peer with given ID, returns a pstore.PeerInfo
50 - // with relevant addresses.
51 - FindPeer(context.Context, peer.ID) (pstore.PeerInfo, error)
59 +// IpfsRouting is the combination of different routing types that ipfs
60 +// uses. It can be satisfied by a single item (such as a DHT) or multiple
61 +// different pieces that are more optimized to each task.
62 +type IpfsRouting interface {
63 + ContentRouting
64 + PeerRouting
65 + ValueStore
66
67 // Bootstrap allows callers to hint to the routing system to get into a
68 // Boostrapped state
@@ -74,7 +88,7 @@ func KeyForPublicKey(id peer.ID) key.Key {
88 return key.Key("/pk/" + string(id))
89 }
90
77 -func GetPublicKey(r IpfsRouting, ctx context.Context, pkhash []byte) (ci.PubKey, error) {
91 +func GetPublicKey(r ValueStore, ctx context.Context, pkhash []byte) (ci.PubKey, error) {
92 if dht, ok := r.(PubKeyFetcher); ok {
93 // If we have a DHT as our routing system, use optimized fetcher
94 return dht.GetPublicKey(ctx, peer.ID(pkhash))