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

Fix: Handle correctly keys on Get/Put Value and Get/Put IPNS. #31

Merged
merged 2 commits into from
Jul 7, 2022
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
6 changes: 4 additions & 2 deletions client/getipns.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"

"github.com/ipfs/go-delegated-routing/gen/proto"
ipns "github.com/ipfs/go-ipns"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/routing"
)

Expand All @@ -21,7 +23,7 @@ func (fp *Client) GetIPNS(ctx context.Context, id []byte) ([]byte, error) {
if len(records) == 0 {
return nil, routing.ErrNotFound
}
best, err := fp.validator.Select(string(id), records)
best, err := fp.validator.Select(ipns.RecordKey(peer.ID(id)), records)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -66,7 +68,7 @@ func (fp *Client) GetIPNSAsync(ctx context.Context, id []byte) (<-chan GetIPNSAs
continue
}

if err = fp.validator.Validate(string(id), r0.Resp.Record); err != nil {
if err = fp.validator.Validate(ipns.RecordKey(peer.ID(id)), r0.Resp.Record); err != nil {
r1.Err = err
select {
case <-ctx.Done():
Expand Down
14 changes: 13 additions & 1 deletion client/putipns.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@ package client

import (
"context"
"fmt"

"github.com/ipfs/go-delegated-routing/gen/proto"
"github.com/libp2p/go-libp2p-core/peer"
)

func (fp *Client) PutIPNS(ctx context.Context, id []byte, record []byte) error {
_, err := fp.client.PutIPNS(ctx, &proto.PutIPNSRequest{ID: id, Record: record})
_, err := peer.IDFromBytes(id)
if err != nil {
return fmt.Errorf("invalid peer ID: %w", err)
}

_, err = fp.client.PutIPNS(ctx, &proto.PutIPNSRequest{ID: id, Record: record})
if err != nil {
return err
}
Expand All @@ -19,6 +26,11 @@ type PutIPNSAsyncResult struct {
}

func (fp *Client) PutIPNSAsync(ctx context.Context, id []byte, record []byte) (<-chan PutIPNSAsyncResult, error) {
_, err := peer.IDFromBytes(id)
if err != nil {
return nil, fmt.Errorf("invalid peer ID: %w", err)
}

ch0, err := fp.client.PutIPNS_Async(ctx, &proto.PutIPNSRequest{ID: id, Record: record})
if err != nil {
return nil, err
Expand Down
36 changes: 33 additions & 3 deletions client/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,41 @@ package client

import (
"context"
"fmt"

"github.com/ipfs/go-ipns"
"github.com/libp2p/go-libp2p-core/routing"
record "github.com/libp2p/go-libp2p-record"
)

var _ routing.ValueStore = &Client{}

// PutValue adds value corresponding to given Key.
func (c *Client) PutValue(ctx context.Context, key string, val []byte, opts ...routing.Option) error {
return c.PutIPNS(ctx, []byte(key), val)
ns, path, err := record.SplitKey(key)
if err != nil {
return fmt.Errorf("invalid key: %w", err)
}

if ns != "ipns" {
return ipns.ErrKeyFormat
}

return c.PutIPNS(ctx, []byte(path), val)
ajnavarro marked this conversation as resolved.
Show resolved Hide resolved
}

// GetValue searches for the value corresponding to given Key.
func (c *Client) GetValue(ctx context.Context, key string, opts ...routing.Option) ([]byte, error) {
return c.GetIPNS(ctx, []byte(key))
ns, path, err := record.SplitKey(key)
if err != nil {
return nil, fmt.Errorf("invalid key: %w", err)
}

if ns != "ipns" {
return nil, ipns.ErrKeyFormat
}

return c.GetIPNS(ctx, []byte(path))
}

// SearchValue searches for better and better values from this value
Expand All @@ -29,7 +50,16 @@ func (c *Client) GetValue(ctx context.Context, key string, opts ...routing.Optio
// Implementations of this methods won't return ErrNotFound. When a value
// couldn't be found, the channel will get closed without passing any results
func (c *Client) SearchValue(ctx context.Context, key string, opts ...routing.Option) (<-chan []byte, error) {
resChan, err := c.GetIPNSAsync(ctx, []byte(key))
ns, path, err := record.SplitKey(key)
if err != nil {
return nil, fmt.Errorf("invalid key: %w", err)
}

if ns != "ipns" {
return nil, ipns.ErrKeyFormat
}

resChan, err := c.GetIPNSAsync(ctx, []byte(path))
if err != nil {
return nil, err
}
Expand Down
14 changes: 8 additions & 6 deletions test/clientserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func testClientServer(t *testing.T, numIter int) (avgLatency time.Duration, delt
}

// exercise GetIPNS
record, err := c.GetIPNS(context.Background(), testIPNSID)
record, err := c.GetIPNS(context.Background(), []byte(testPeerIDFromIPNS))
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -102,7 +102,7 @@ func testClientServer(t *testing.T, numIter int) (avgLatency time.Duration, delt
}

// exercise PutIPNS
err = c.PutIPNS(context.Background(), testIPNSID, testIPNSRecord)
err = c.PutIPNS(context.Background(), []byte(testPeerIDFromIPNS), testIPNSRecord)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -189,7 +189,7 @@ func TestCancelContext(t *testing.T) {

ctx, cancel := context.WithCancel(context.Background())

gir, err := c.GetIPNSAsync(ctx, testIPNSID)
gir, err := c.GetIPNSAsync(ctx, []byte(testPeerIDFromIPNS))
if err != nil {
t.Fatal(err)
}
Expand All @@ -203,7 +203,7 @@ func TestCancelContext(t *testing.T) {

ctx, cancel = context.WithCancel(context.Background())

pir, err := c.PutIPNSAsync(ctx, testIPNSID, testIPNSRecord)
pir, err := c.PutIPNSAsync(ctx, []byte(testPeerIDFromIPNS), testIPNSRecord)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -294,8 +294,9 @@ var (
Addrs: []multiaddr.Multiaddr{testMultiaddr},
}
// IPNS
testIPNSID []byte
testIPNSRecord []byte
testPeerIDFromIPNS peer.ID
testIPNSID []byte
testIPNSRecord []byte
)

// TestMain generates a valid IPNS key and record for testing purposes.
Expand All @@ -308,6 +309,7 @@ func TestMain(m *testing.M) {
if err != nil {
panic(err)
}
testPeerIDFromIPNS = peerID
testIPNSID = []byte(ipns.RecordKey(peerID))
entry, err := ipns.Create(privateKey, testIPNSID, 0, time.Now().Add(time.Hour), time.Hour)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"version": "v0.2.2"
"version": "v0.3.0"
}