mirror of
https://github.com/ipfs/kubo.git
synced 2026-03-03 23:38:07 +08:00
Merge pull request #3107 from ipfs/feat/routing/interfaces
routing: rework interfaces to make separation easier
This commit is contained in:
commit
37cf360db5
@ -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,
|
||||
@ -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
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -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(),
|
||||
|
||||
@ -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")
|
||||
}
|
||||
@ -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()
|
||||
|
||||
@ -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 {
|
||||
@ -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()
|
||||
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -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
|
||||
}
|
||||
@ -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")
|
||||
}
|
||||
|
||||
@ -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 {
|
||||
FindProvidersAsync(context.Context, key.Key, int) <-chan pstore.PeerInfo
|
||||
// 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.
|
||||
@ -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
|
||||
@ -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))
|
||||
|
||||
Loading…
Reference in New Issue
Block a user