Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: deprecate CoreAPI.Dht, introduce CoreAPI.Routing #10329

Merged
merged 1 commit into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions client/rpc/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ func (api *HttpApi) Object() iface.ObjectAPI {
return (*ObjectAPI)(api)
}

// nolint deprecated
// Deprecated: use [HttpApi.Routing] instead.
func (api *HttpApi) Dht() iface.DhtAPI {
return (*DhtAPI)(api)
}
Expand Down
98 changes: 9 additions & 89 deletions client/rpc/dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,110 +2,30 @@ package rpc

import (
"context"
"encoding/json"

"github.com/ipfs/boxo/path"
caopts "github.com/ipfs/kubo/core/coreiface/options"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/routing"
)

type DhtAPI HttpApi

// nolint deprecated
// Deprecated: use [RoutingAPI.FindPeer] instead.
func (api *DhtAPI) FindPeer(ctx context.Context, p peer.ID) (peer.AddrInfo, error) {
var out struct {
Type routing.QueryEventType
Responses []peer.AddrInfo
}
resp, err := api.core().Request("dht/findpeer", p.String()).Send(ctx)
if err != nil {
return peer.AddrInfo{}, err
}
if resp.Error != nil {
return peer.AddrInfo{}, resp.Error
}
defer resp.Close()
dec := json.NewDecoder(resp.Output)
for {
if err := dec.Decode(&out); err != nil {
return peer.AddrInfo{}, err
}
if out.Type == routing.FinalPeer {
return out.Responses[0], nil
}
}
return api.core().Routing().FindPeer(ctx, p)
}

// nolint deprecated
// Deprecated: use [RoutingAPI.FindProviders] instead.
func (api *DhtAPI) FindProviders(ctx context.Context, p path.Path, opts ...caopts.DhtFindProvidersOption) (<-chan peer.AddrInfo, error) {
options, err := caopts.DhtFindProvidersOptions(opts...)
if err != nil {
return nil, err
}

rp, _, err := api.core().ResolvePath(ctx, p)
if err != nil {
return nil, err
}

resp, err := api.core().Request("dht/findprovs", rp.RootCid().String()).
Option("num-providers", options.NumProviders).
Send(ctx)
if err != nil {
return nil, err
}
if resp.Error != nil {
return nil, resp.Error
}
res := make(chan peer.AddrInfo)

go func() {
defer resp.Close()
defer close(res)
dec := json.NewDecoder(resp.Output)

for {
var out struct {
Extra string
Type routing.QueryEventType
Responses []peer.AddrInfo
}

if err := dec.Decode(&out); err != nil {
return // todo: handle this somehow
}
if out.Type == routing.QueryError {
return // usually a 'not found' error
// todo: handle other errors
}
if out.Type == routing.Provider {
for _, pi := range out.Responses {
select {
case res <- pi:
case <-ctx.Done():
return
}
}
}
}
}()

return res, nil
return api.core().Routing().FindProviders(ctx, p, opts...)
}

// nolint deprecated
// Deprecated: use [RoutingAPI.Provide] instead.
func (api *DhtAPI) Provide(ctx context.Context, p path.Path, opts ...caopts.DhtProvideOption) error {
options, err := caopts.DhtProvideOptions(opts...)
if err != nil {
return err
}

rp, _, err := api.core().ResolvePath(ctx, p)
if err != nil {
return err
}

return api.core().Request("dht/provide", rp.RootCid().String()).
Option("recursive", options.Recursive).
Exec(ctx, nil)
return api.core().Routing().Provide(ctx, p, opts...)
}

func (api *DhtAPI) core() *HttpApi {
Expand Down
98 changes: 98 additions & 0 deletions client/rpc/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"encoding/base64"
"encoding/json"

"github.com/ipfs/boxo/path"
"github.com/ipfs/kubo/core/coreiface/options"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/routing"
)

Expand Down Expand Up @@ -58,6 +60,102 @@ func (api *RoutingAPI) Put(ctx context.Context, key string, value []byte, opts .
return nil
}

func (api *RoutingAPI) FindPeer(ctx context.Context, p peer.ID) (peer.AddrInfo, error) {
var out struct {
Type routing.QueryEventType
Responses []peer.AddrInfo
}
resp, err := api.core().Request("routing/findpeer", p.String()).Send(ctx)
if err != nil {
return peer.AddrInfo{}, err
}
if resp.Error != nil {
return peer.AddrInfo{}, resp.Error
}
defer resp.Close()
dec := json.NewDecoder(resp.Output)
for {
if err := dec.Decode(&out); err != nil {
return peer.AddrInfo{}, err
}
if out.Type == routing.FinalPeer {
return out.Responses[0], nil
}
}
}

func (api *RoutingAPI) FindProviders(ctx context.Context, p path.Path, opts ...options.RoutingFindProvidersOption) (<-chan peer.AddrInfo, error) {
options, err := options.RoutingFindProvidersOptions(opts...)
if err != nil {
return nil, err
}

rp, _, err := api.core().ResolvePath(ctx, p)
if err != nil {
return nil, err
}

resp, err := api.core().Request("routing/findprovs", rp.RootCid().String()).
Option("num-providers", options.NumProviders).
Send(ctx)
if err != nil {
return nil, err
}
if resp.Error != nil {
return nil, resp.Error
}
res := make(chan peer.AddrInfo)

go func() {
defer resp.Close()
defer close(res)
dec := json.NewDecoder(resp.Output)

for {
var out struct {
Extra string
Type routing.QueryEventType
Responses []peer.AddrInfo
}

if err := dec.Decode(&out); err != nil {
return // todo: handle this somehow
}
if out.Type == routing.QueryError {
return // usually a 'not found' error
// todo: handle other errors
}
if out.Type == routing.Provider {
for _, pi := range out.Responses {
select {
case res <- pi:
case <-ctx.Done():
return
}
}
}
}
}()

return res, nil
}

func (api *RoutingAPI) Provide(ctx context.Context, p path.Path, opts ...options.RoutingProvideOption) error {
options, err := options.RoutingProvideOptions(opts...)
if err != nil {
return err
}

rp, _, err := api.core().ResolvePath(ctx, p)
if err != nil {
return err
}

return api.core().Request("routing/provide", rp.RootCid().String()).
Option("recursive", options.Recursive).
Exec(ctx, nil)
}

func (api *RoutingAPI) core() *HttpApi {
return (*HttpApi)(api)
}
3 changes: 2 additions & 1 deletion core/coreapi/coreapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ func (api *CoreAPI) Pin() coreiface.PinAPI {
return (*PinAPI)(api)
}

// Dht returns the DhtAPI interface implementation backed by the go-ipfs node
// nolint deprecated
// Deprecated: use [CoreAPI.Routing] instead.
func (api *CoreAPI) Dht() coreiface.DhtAPI {
return (*DhtAPI)(api)
}
Expand Down
Loading
Loading