Skip to content

Commit

Permalink
coreiface: deprecate DhtAPI in favor of RoutingAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorropo committed Sep 5, 2023
1 parent 574df96 commit 9f2683a
Show file tree
Hide file tree
Showing 8 changed files with 242 additions and 226 deletions.
2 changes: 2 additions & 0 deletions coreiface/coreapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ type CoreAPI interface {
Object() ObjectAPI

// Dht returns an implementation of Dht API
//
// Deprecated: use [CoreAPI.Routing] instead.
Dht() DhtAPI

// Swarm returns an implementation of Swarm API
Expand Down
7 changes: 3 additions & 4 deletions coreiface/dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ package iface
import (
"context"

"github.com/ipfs/boxo/coreiface/path"

"github.com/ipfs/boxo/coreiface/options"
"github.com/ipfs/boxo/coreiface/path"

"github.com/libp2p/go-libp2p/core/peer"
)

// DhtAPI specifies the interface to the DHT
// Note: This API will likely get deprecated in near future, see
// https://github.com/ipfs/interface-ipfs-core/issues/249 for more context.
//
// Deprecated: Use [RoutingAPI] instead, see https://github.com/ipfs/interface-ipfs-core/issues/249 for more context.
type DhtAPI interface {
// FindPeer queries the DHT for all of the multiaddresses associated with a
// Peer ID
Expand Down
68 changes: 15 additions & 53 deletions coreiface/options/dht.go
Original file line number Diff line number Diff line change
@@ -1,64 +1,26 @@
package options

type DhtProvideSettings struct {
Recursive bool
}
// Deprecated: use [RoutingProvideSettings] instead
type DhtProvideSettings = RoutingProvideSettings

type DhtFindProvidersSettings struct {
NumProviders int
}
// Deprecated: use [RoutingFindProvidersSettings] instead
type DhtFindProvidersSettings = RoutingFindProvidersSettings

type (
DhtProvideOption func(*DhtProvideSettings) error
DhtFindProvidersOption func(*DhtFindProvidersSettings) error
)
// Deprecated: use [RoutingProvideOption] instead
type DhtProvideOption = RoutingProvideOption

func DhtProvideOptions(opts ...DhtProvideOption) (*DhtProvideSettings, error) {
options := &DhtProvideSettings{
Recursive: false,
}
// Deprecated: use [RoutingFindProvidersOption] instead
type DhtFindProvidersOption = RoutingFindProvidersOption

for _, opt := range opts {
err := opt(options)
if err != nil {
return nil, err
}
}
return options, nil
// Deprecated: use [RoutingProvideOptions] instead
func DhtProvideOptions(opts ...DhtProvideOption) (*DhtProvideSettings, error) {
return RoutingProvideOptions(opts...)
}

// Deprecated: use [RoutingFindProvidersOptions] instead
func DhtFindProvidersOptions(opts ...DhtFindProvidersOption) (*DhtFindProvidersSettings, error) {
options := &DhtFindProvidersSettings{
NumProviders: 20,
}

for _, opt := range opts {
err := opt(options)
if err != nil {
return nil, err
}
}
return options, nil
return RoutingFindProvidersOptions(opts...)
}

type dhtOpts struct{}

var Dht dhtOpts

// Recursive is an option for Dht.Provide which specifies whether to provide
// the given path recursively
func (dhtOpts) Recursive(recursive bool) DhtProvideOption {
return func(settings *DhtProvideSettings) error {
settings.Recursive = recursive
return nil
}
}

// NumProviders is an option for Dht.FindProviders which specifies the
// number of peers to look for. Default is 20
func (dhtOpts) NumProviders(numProviders int) DhtFindProvidersOption {
return func(settings *DhtFindProvidersSettings) error {
settings.NumProviders = numProviders
return nil
}
}
// Deprecated: use [Routing] instead
var Dht routingOpts
62 changes: 62 additions & 0 deletions coreiface/options/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ type RoutingPutSettings struct {

type RoutingPutOption func(*RoutingPutSettings) error

type RoutingProvideSettings struct {
Recursive bool
}

type RoutingProvideOption func(*DhtProvideSettings) error

type RoutingFindProvidersSettings struct {
NumProviders int
}

type RoutingFindProvidersOption func(*DhtFindProvidersSettings) error

func RoutingPutOptions(opts ...RoutingPutOption) (*RoutingPutSettings, error) {
options := &RoutingPutSettings{
AllowOffline: false,
Expand Down Expand Up @@ -33,3 +45,53 @@ func (putOpts) AllowOffline(allow bool) RoutingPutOption {
return nil
}
}

func RoutingProvideOptions(opts ...RoutingProvideOption) (*RoutingProvideSettings, error) {
options := &RoutingProvideSettings{
Recursive: false,
}

for _, opt := range opts {
err := opt(options)
if err != nil {
return nil, err
}
}
return options, nil
}

func RoutingFindProvidersOptions(opts ...RoutingFindProvidersOption) (*RoutingFindProvidersSettings, error) {
options := &RoutingFindProvidersSettings{
NumProviders: 20,
}

for _, opt := range opts {
err := opt(options)
if err != nil {
return nil, err
}
}
return options, nil
}

type routingOpts struct{}

var Routing routingOpts

// Recursive is an option for Dht.Provide which specifies whether to provide
// the given path recursively
func (routingOpts) Recursive(recursive bool) DhtProvideOption {
return func(settings *DhtProvideSettings) error {
settings.Recursive = recursive
return nil
}
}

// NumProviders is an option for Dht.FindProviders which specifies the
// number of peers to look for. Default is 20
func (routingOpts) NumProviders(numProviders int) DhtFindProvidersOption {
return func(settings *DhtFindProvidersSettings) error {
settings.NumProviders = numProviders
return nil
}
}
16 changes: 14 additions & 2 deletions coreiface/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,25 @@ import (
"context"

"github.com/ipfs/boxo/coreiface/options"
"github.com/ipfs/boxo/coreiface/path"

"github.com/libp2p/go-libp2p/core/peer"
)

// RoutingAPI specifies the interface to the routing layer.
type RoutingAPI interface {
// Get retrieves the best value for a given key
// Get retrieves the best value for a given key.
Get(context.Context, string) ([]byte, error)

// Put sets a value for a given key
// Put sets a value for a given key.
Put(ctx context.Context, key string, value []byte, opts ...options.RoutingPutOption) error

// FindPeer queries the DHT for all of the multiaddresses associated with a Peer ID.
FindPeer(context.Context, peer.ID) (peer.AddrInfo, error)

// FindProviders finds peers in the DHT who can provide a specific value given a key.
FindProviders(context.Context, path.Path, ...options.DhtFindProvidersOption) (<-chan peer.AddrInfo, error)

// Provide announces to the network that you are providing given values.
Provide(context.Context, path.Path, ...options.DhtProvideOption) error
}
1 change: 0 additions & 1 deletion coreiface/tests/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ func TestApi(p Provider) func(t *testing.T) {
return func(t *testing.T) {
t.Run("Block", tp.TestBlock)
t.Run("Dag", tp.TestDag)
t.Run("Dht", tp.TestDht)
t.Run("Key", tp.TestKey)
t.Run("Name", tp.TestName)
t.Run("Object", tp.TestObject)
Expand Down
166 changes: 0 additions & 166 deletions coreiface/tests/dht.go

This file was deleted.

Loading

0 comments on commit 9f2683a

Please sign in to comment.