Skip to content
This repository has been archived by the owner on Mar 28, 2023. It is now read-only.

Commit

Permalink
Update provide to take an array of keys, per spec (#45)
Browse files Browse the repository at this point in the history
* Update provide to take an array of keys, per spec
  • Loading branch information
willscott authored Sep 5, 2022
1 parent 9e5b6f6 commit 0c2a9b6
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 46 deletions.
21 changes: 20 additions & 1 deletion client/contentrouting.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/ipfs/go-cid"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/routing"
"github.com/multiformats/go-multihash"
)

type ContentRoutingClient struct {
Expand All @@ -27,10 +28,28 @@ func (c *ContentRoutingClient) Provide(ctx context.Context, key cid.Cid, announc
return nil
}

_, err := c.client.Provide(ctx, key, 24*time.Hour)
_, err := c.client.Provide(ctx, []cid.Cid{key}, 24*time.Hour)
return err
}

func (c *ContentRoutingClient) ProvideMany(ctx context.Context, keys []multihash.Multihash) error {
keysAsCids := make([]cid.Cid, 0, len(keys))
for _, m := range keys {
keysAsCids = append(keysAsCids, cid.NewCidV1(cid.Raw, m))
}
_, err := c.client.Provide(ctx, keysAsCids, 24*time.Hour)
return err
}

// Ready is part of the existing `ProvideMany` interface, but can be used more generally to determine if the routing client
// has a working connection.
func (c *ContentRoutingClient) Ready() bool {
// TODO: currently codegen does not expose a way to access the state of the connection
// Once either that is exposed, or the `Identify` portion of the reframe spec is implemented,
// a more nuanced response for this method will be possible.
return true
}

func (c *ContentRoutingClient) FindProvidersAsync(ctx context.Context, key cid.Cid, numResults int) <-chan peer.AddrInfo {
addrInfoCh := make(chan peer.AddrInfo)
resultCh, err := c.client.FindProvidersAsync(ctx, key)
Expand Down
4 changes: 2 additions & 2 deletions client/contentrouting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ func (t TestDelegatedRoutingClient) PutIPNSAsync(ctx context.Context, id []byte,
panic("not supported")
}

func (t TestDelegatedRoutingClient) ProvideAsync(ctx context.Context, key cid.Cid, ttl time.Duration) (<-chan time.Duration, error) {
func (t TestDelegatedRoutingClient) ProvideAsync(ctx context.Context, key []cid.Cid, ttl time.Duration) (<-chan time.Duration, error) {
panic("not supported")
}

func (t TestDelegatedRoutingClient) Provide(ctx context.Context, key cid.Cid, tl time.Duration) (time.Duration, error) {
func (t TestDelegatedRoutingClient) Provide(ctx context.Context, key []cid.Cid, tl time.Duration) (time.Duration, error) {
panic("not supported")
}

Expand Down
6 changes: 3 additions & 3 deletions client/findproviders.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ type DelegatedRoutingClient interface {
GetIPNSAsync(ctx context.Context, id []byte) (<-chan GetIPNSAsyncResult, error)
PutIPNS(ctx context.Context, id []byte, record []byte) error
PutIPNSAsync(ctx context.Context, id []byte, record []byte) (<-chan PutIPNSAsyncResult, error)
Provide(ctx context.Context, key cid.Cid, ttl time.Duration) (time.Duration, error)
ProvideAsync(ctx context.Context, key cid.Cid, ttl time.Duration) (<-chan time.Duration, error)
Provide(ctx context.Context, key []cid.Cid, ttl time.Duration) (time.Duration, error)
ProvideAsync(ctx context.Context, key []cid.Cid, ttl time.Duration) (<-chan time.Duration, error)
}

type Client struct {
Expand Down Expand Up @@ -174,7 +174,7 @@ func ParseNodeAddresses(n *proto.Peer) []peer.AddrInfo {
func ToProtoPeer(ai peer.AddrInfo) *proto.Peer {
p := proto.Peer{
ID: values.Bytes(ai.ID),
Multiaddresses: make(proto.AnonList20, 0),
Multiaddresses: make(proto.AnonList21, 0),
}

for _, addr := range ai.Addrs {
Expand Down
24 changes: 16 additions & 8 deletions client/provide.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func parseProtocol(tp *proto.TransferProtocol) (TransferProtocol, error) {

// ProvideRequest is a message indicating a provider can provide a Key for a given TTL
type ProvideRequest struct {
Key cid.Cid
Key []cid.Cid
*Provider
Timestamp int64
AdvisoryTTL time.Duration
Expand All @@ -109,7 +109,7 @@ type ProvideRequest struct {

var provideSchema, provideSchemaErr = ipld.LoadSchemaBytes([]byte(`
type ProvideRequest struct {
Key &Any
Key [&Any]
Provider Provider
Timestamp Int
AdvisoryTTL Int
Expand Down Expand Up @@ -234,8 +234,12 @@ func ParseProvideRequest(req *proto.ProvideRequest) (*ProvideRequest, error) {
if err != nil {
return nil, err
}
keys := make([]cid.Cid, 0, len(req.Key))
for _, c := range req.Key {
keys = append(keys, cid.Cid(c))
}
pr := ProvideRequest{
Key: cid.Cid(req.Key),
Key: keys,
Provider: prov,
AdvisoryTTL: time.Duration(req.AdvisoryTTL),
Timestamp: int64(req.Timestamp),
Expand Down Expand Up @@ -268,9 +272,9 @@ type ProvideAsyncResult struct {
Err error
}

func (fp *Client) Provide(ctx context.Context, key cid.Cid, ttl time.Duration) (time.Duration, error) {
func (fp *Client) Provide(ctx context.Context, keys []cid.Cid, ttl time.Duration) (time.Duration, error) {
req := ProvideRequest{
Key: key,
Key: keys,
Provider: fp.provider,
AdvisoryTTL: ttl,
Timestamp: time.Now().Unix(),
Expand Down Expand Up @@ -308,9 +312,9 @@ func (fp *Client) Provide(ctx context.Context, key cid.Cid, ttl time.Duration) (
return 0, err
}

func (fp *Client) ProvideAsync(ctx context.Context, key cid.Cid, ttl time.Duration) (<-chan time.Duration, error) {
func (fp *Client) ProvideAsync(ctx context.Context, keys []cid.Cid, ttl time.Duration) (<-chan time.Duration, error) {
req := ProvideRequest{
Key: key,
Key: keys,
Provider: fp.provider,
AdvisoryTTL: ttl,
Timestamp: time.Now().Unix(),
Expand Down Expand Up @@ -352,8 +356,12 @@ func (fp *Client) ProvideSignedRecord(ctx context.Context, req *ProvideRequest)
if req.Provider != nil {
providerProto = *req.Provider.ToProto()
}
keys := make(proto.AnonList14, 0, len(req.Key))
for _, c := range req.Key {
keys = append(keys, proto.LinkToAny(c))
}
ch0, err := fp.client.Provide_Async(ctx, &proto.ProvideRequest{
Key: proto.LinkToAny(req.Key),
Key: keys,
Provider: providerProto,
Timestamp: values.Int(req.Timestamp),
AdvisoryTTL: values.Int(req.AdvisoryTTL),
Expand Down
Loading

0 comments on commit 0c2a9b6

Please sign in to comment.