Skip to content

Commit ea8683a

Browse files
committed
routing: rework interfaces to make separation easier
License: MIT Signed-off-by: Jeromy <[email protected]>
1 parent 7276fd8 commit ea8683a

File tree

7 files changed

+42
-28
lines changed

7 files changed

+42
-28
lines changed

exchange/bitswap/network/ipfs_impl.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
var log = logging.Logger("bitswap_network")
2121

2222
// 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 {
2424
bitswapNetwork := impl{
2525
host: host,
2626
routing: r,
@@ -36,7 +36,7 @@ func NewFromIpfsHost(host host.Host, r routing.IpfsRouting) BitSwapNetwork {
3636
// NetMessage objects, into the bitswap network interface.
3737
type impl struct {
3838
host host.Host
39-
routing routing.IpfsRouting
39+
routing routing.ContentRouting
4040

4141
// inbound messages from the network are forwarded to the receiver
4242
receiver Receiver

exchange/reprovide/reprovide.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ var log = logging.Logger("reprovider")
1515

1616
type Reprovider struct {
1717
// The routing system to provide values through
18-
rsys routing.IpfsRouting
18+
rsys routing.ContentRouting
1919

2020
// The backing store for blocks to be provided
2121
bstore blocks.Blockstore
2222
}
2323

24-
func NewReprovider(rsys routing.IpfsRouting, bstore blocks.Blockstore) *Reprovider {
24+
func NewReprovider(rsys routing.ContentRouting, bstore blocks.Blockstore) *Reprovider {
2525
return &Reprovider{
2626
rsys: rsys,
2727
bstore: bstore,

namesys/namesys.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type mpns struct {
2626
}
2727

2828
// 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 {
3030
return &mpns{
3131
resolvers: map[string]resolver{
3232
"dns": newDNSResolver(),

namesys/publisher.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ var PublishPutValTimeout = time.Minute
3737
// ipnsPublisher is capable of publishing and resolving names to the IPFS
3838
// routing system.
3939
type ipnsPublisher struct {
40-
routing routing.IpfsRouting
40+
routing routing.ValueStore
4141
ds ds.Datastore
4242
}
4343

4444
// 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 {
4646
if ds == nil {
4747
panic("nil datastore")
4848
}
@@ -134,7 +134,7 @@ func checkCtxTTL(ctx context.Context) (time.Duration, bool) {
134134
return d, ok
135135
}
136136

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 {
138138
ctx, cancel := context.WithCancel(ctx)
139139
defer cancel()
140140

@@ -181,7 +181,7 @@ func waitOnErrChan(ctx context.Context, errs chan error) error {
181181
}
182182
}
183183

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 {
185185
log.Debugf("Storing pubkey at: %s", k)
186186
pkbytes, err := pubk.Bytes()
187187
if err != nil {
@@ -199,7 +199,7 @@ func PublishPublicKey(ctx context.Context, r routing.IpfsRouting, k key.Key, pub
199199
return nil
200200
}
201201

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 {
203203
timectx, cancel := context.WithTimeout(ctx, PublishPutValTimeout)
204204
defer cancel()
205205

namesys/republisher/repub.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ var DefaultRebroadcastInterval = time.Hour * 4
3131
const DefaultRecordLifetime = time.Hour * 24
3232

3333
type Republisher struct {
34-
r routing.IpfsRouting
34+
r routing.ValueStore
3535
ds ds.Datastore
3636
ps pstore.Peerstore
3737

@@ -44,7 +44,7 @@ type Republisher struct {
4444
entries map[peer.ID]struct{}
4545
}
4646

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 {
4848
return &Republisher{
4949
r: r,
5050
ps: ps,

namesys/routing.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ var log = logging.Logger("namesys")
2323

2424
// routingResolver implements NSResolver for the main IPFS SFS-like naming
2525
type routingResolver struct {
26-
routing routing.IpfsRouting
26+
routing routing.ValueStore
2727

2828
cache *lru.Cache
2929
}
@@ -88,7 +88,7 @@ type cacheEntry struct {
8888
// to implement SFS-like naming on top.
8989
// cachesize is the limit of the number of entries in the lru cache. Setting it
9090
// to '0' will disable caching.
91-
func NewRoutingResolver(route routing.IpfsRouting, cachesize int) *routingResolver {
91+
func NewRoutingResolver(route routing.ValueStore, cachesize int) *routingResolver {
9292
if route == nil {
9393
panic("attempt to create resolver with nil routing system")
9494
}

routing/routing.go

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,27 @@ import (
1414
// ErrNotFound is returned when a search fails to find anything
1515
var ErrNotFound = errors.New("routing: not found")
1616

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
2024
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+
}
2136

37+
type ValueStore interface {
2238
// Basic Put/Get
2339

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

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
5266

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

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) {
7892
if dht, ok := r.(PubKeyFetcher); ok {
7993
// If we have a DHT as our routing system, use optimized fetcher
8094
return dht.GetPublicKey(ctx, peer.ID(pkhash))

0 commit comments

Comments
 (0)