Skip to content

Commit

Permalink
core: deprecate CoreAPI.Dht, introduce CoreAPI.Routing
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Feb 1, 2024
1 parent 1a3b8d7 commit 3357e5f
Show file tree
Hide file tree
Showing 18 changed files with 541 additions and 470 deletions.
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)

Check warning on line 16 in client/rpc/dht.go

View check run for this annotation

Codecov / codecov/patch

client/rpc/dht.go#L16

Added line #L16 was not covered by tests
}

// 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...)

Check warning on line 22 in client/rpc/dht.go

View check run for this annotation

Codecov / codecov/patch

client/rpc/dht.go#L22

Added line #L22 was not covered by tests
}

// 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...)

Check warning on line 28 in client/rpc/dht.go

View check run for this annotation

Codecov / codecov/patch

client/rpc/dht.go#L28

Added line #L28 was not covered by tests
}

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
}

Check warning on line 71 in client/rpc/routing.go

View check run for this annotation

Codecov / codecov/patch

client/rpc/routing.go#L70-L71

Added lines #L70 - L71 were not covered by tests
if resp.Error != nil {
return peer.AddrInfo{}, resp.Error
}

Check warning on line 74 in client/rpc/routing.go

View check run for this annotation

Codecov / codecov/patch

client/rpc/routing.go#L73-L74

Added lines #L73 - L74 were not covered by tests
defer resp.Close()
dec := json.NewDecoder(resp.Output)
for {
if err := dec.Decode(&out); err != nil {
return peer.AddrInfo{}, err
}

Check warning on line 80 in client/rpc/routing.go

View check run for this annotation

Codecov / codecov/patch

client/rpc/routing.go#L79-L80

Added lines #L79 - L80 were not covered by tests
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
}

Check warning on line 91 in client/rpc/routing.go

View check run for this annotation

Codecov / codecov/patch

client/rpc/routing.go#L90-L91

Added lines #L90 - L91 were not covered by tests

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

Check warning on line 96 in client/rpc/routing.go

View check run for this annotation

Codecov / codecov/patch

client/rpc/routing.go#L95-L96

Added lines #L95 - L96 were not covered by tests

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

Check warning on line 103 in client/rpc/routing.go

View check run for this annotation

Codecov / codecov/patch

client/rpc/routing.go#L102-L103

Added lines #L102 - L103 were not covered by tests
if resp.Error != nil {
return nil, resp.Error
}

Check warning on line 106 in client/rpc/routing.go

View check run for this annotation

Codecov / codecov/patch

client/rpc/routing.go#L105-L106

Added lines #L105 - L106 were not covered by tests
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
}

Check warning on line 127 in client/rpc/routing.go

View check run for this annotation

Codecov / codecov/patch

client/rpc/routing.go#L125-L127

Added lines #L125 - L127 were not covered by tests
if out.Type == routing.Provider {
for _, pi := range out.Responses {
select {
case res <- pi:
case <-ctx.Done():
return

Check warning on line 133 in client/rpc/routing.go

View check run for this annotation

Codecov / codecov/patch

client/rpc/routing.go#L132-L133

Added lines #L132 - L133 were not covered by tests
}
}
}
}
}()

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
}

Check warning on line 147 in client/rpc/routing.go

View check run for this annotation

Codecov / codecov/patch

client/rpc/routing.go#L146-L147

Added lines #L146 - L147 were not covered by tests

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

Check warning on line 152 in client/rpc/routing.go

View check run for this annotation

Codecov / codecov/patch

client/rpc/routing.go#L151-L152

Added lines #L151 - L152 were not covered by tests

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

0 comments on commit 3357e5f

Please sign in to comment.