Skip to content

Commit

Permalink
pieceCID is []byte everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
shannonwells committed Jan 15, 2020
1 parent efd596d commit 5324d30
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 30 deletions.
6 changes: 4 additions & 2 deletions pieceio/cario/cario.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package cario
import (
"context"
"fmt"
"github.com/filecoin-project/go-fil-markets/pieceio"
"io"

"github.com/ipfs/go-car"
"github.com/ipfs/go-cid"
"github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/traversal/selector"
"io"

"github.com/filecoin-project/go-fil-markets/pieceio"
)

type carIO struct {
Expand Down
5 changes: 3 additions & 2 deletions retrievalmarket/discovery/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ func (l *Local) AddPeer(cid cid.Cid, peer retrievalmarket.RetrievalPeer) error {
return l.ds.Put(dshelp.CidToDsKey(cid), entry)
}

func (l *Local) GetPeers(data cid.Cid) ([]retrievalmarket.RetrievalPeer, error) {
entry, err := l.ds.Get(dshelp.CidToDsKey(data))
func (l *Local) GetPeers(pieceCID []byte) ([]retrievalmarket.RetrievalPeer, error) {
key := string(pieceCID[:])
entry, err := l.ds.Get(datastore.NewKey(key))
if err == datastore.ErrNotFound {
return []retrievalmarket.RetrievalPeer{}, nil
}
Expand Down
15 changes: 5 additions & 10 deletions retrievalmarket/impl/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import (
"reflect"
"sync"

"github.com/filecoin-project/go-fil-markets/retrievalmarket/impl/clientstates"

"github.com/filecoin-project/go-address"
blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
blockstore "github.com/ipfs/go-ipfs-blockstore"
logging "github.com/ipfs/go-log"
"github.com/libp2p/go-libp2p-core/peer"
"golang.org/x/xerrors"

"github.com/filecoin-project/go-fil-markets/retrievalmarket"
"github.com/filecoin-project/go-fil-markets/retrievalmarket/impl/clientstates"
rmnet "github.com/filecoin-project/go-fil-markets/retrievalmarket/network"
"github.com/filecoin-project/go-fil-markets/shared/tokenamount"
)
Expand Down Expand Up @@ -53,13 +53,8 @@ func NewClient(

// TODO: Implement for retrieval provider V0 epic
// https://github.com/filecoin-project/go-retrieval-market-project/issues/12
func (c *client) FindProviders(pieceCIDBytes []byte) []retrievalmarket.RetrievalPeer {
_, pieceCid, err := cid.CidFromBytes(pieceCIDBytes)
if err != nil {
log.Error(err)
return []retrievalmarket.RetrievalPeer{}
}
peers, err := c.resolver.GetPeers(pieceCid)
func (c *client) FindProviders(pieceCID []byte) []retrievalmarket.RetrievalPeer {
peers, err := c.resolver.GetPeers(pieceCID)
if err != nil {
log.Error(err)
return []retrievalmarket.RetrievalPeer{}
Expand Down Expand Up @@ -153,7 +148,7 @@ func (c *client) handleDeal(ctx context.Context, dealState retrievalmarket.Clien
case retrievalmarket.DealStatusFundsNeeded, retrievalmarket.DealStatusFundsNeededLastPayment:
handler = clientstates.ProcessNextResponse
default:
c.failDeal(&dealState, errors.New("unexpected deal state"))
c.failDeal(&dealState, xerrors.New("unexpected deal state"))
return
}
dealModifier := handler(ctx, environment, dealState)
Expand Down
29 changes: 14 additions & 15 deletions retrievalmarket/impl/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package retrievalimpl_test

import (
"context"
"errors"
"testing"

"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-data-transfer/testutil"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-datastore"
dss "github.com/ipfs/go-datastore/sync"
bstore "github.com/ipfs/go-ipfs-blockstore"
Expand All @@ -20,6 +19,7 @@ import (
"github.com/filecoin-project/go-fil-markets/retrievalmarket/network"
rmnet "github.com/filecoin-project/go-fil-markets/retrievalmarket/network"
"github.com/filecoin-project/go-fil-markets/shared/tokenamount"
"github.com/filecoin-project/go-fil-markets/shared/types"
tut "github.com/filecoin-project/go-fil-markets/shared_testutil"
)

Expand Down Expand Up @@ -58,8 +58,7 @@ func TestClient_Query(t *testing.T) {
net := tut.NewTestRetrievalMarketNetwork(tut.TestNetworkParams{
QueryStreamBuilder: tut.ExpectPeerOnQueryStreamBuilder(t, expectedPeer, qsb, "Peers should match"),
})
c := retrievalimpl.NewClient(net, bs, testnodes.NewTestRetrievalClientNode(testnodes.TestRetrievalClientNodeParams{}))
c := retrievalimpl.NewClient(net, bs, &testRetrievalNode{}, &testPeerResolver{})
c := retrievalimpl.NewClient(net, bs, testnodes.NewTestRetrievalClientNode(testnodes.TestRetrievalClientNodeParams{}), &testPeerResolver{})

resp, err := c.Query(ctx, rpeer, pcid, retrievalmarket.QueryParams{})
require.NoError(t, err)
Expand All @@ -71,8 +70,8 @@ func TestClient_Query(t *testing.T) {
net := tut.NewTestRetrievalMarketNetwork(tut.TestNetworkParams{
QueryStreamBuilder: tut.FailNewQueryStream,
})
c := retrievalimpl.NewClient(net, bs, testnodes.NewTestRetrievalClientNode(testnodes.TestRetrievalClientNodeParams{}))
c := retrievalimpl.NewClient(net, bs, &testRetrievalNode{}, &testPeerResolver{})
c := retrievalimpl.NewClient(net, bs,
testnodes.NewTestRetrievalClientNode(testnodes.TestRetrievalClientNodeParams{}), &testPeerResolver{})

_, err := c.Query(ctx, rpeer, pcid, retrievalmarket.QueryParams{})
assert.EqualError(t, err, "new query stream failed")
Expand All @@ -91,8 +90,8 @@ func TestClient_Query(t *testing.T) {
net := tut.NewTestRetrievalMarketNetwork(tut.TestNetworkParams{
QueryStreamBuilder: qsbuilder,
})
c := retrievalimpl.NewClient(net, bs, testnodes.NewTestRetrievalClientNode(testnodes.TestRetrievalClientNodeParams{}))
c := retrievalimpl.NewClient(net, bs, &testRetrievalNode{}, &testPeerResolver{})
c := retrievalimpl.NewClient(net, bs,
testnodes.NewTestRetrievalClientNode(testnodes.TestRetrievalClientNodeParams{}), &testPeerResolver{})

statusCode, err := c.Query(ctx, rpeer, pcid, retrievalmarket.QueryParams{})
assert.EqualError(t, err, "write query failed")
Expand Down Expand Up @@ -141,22 +140,21 @@ func TestClient_FindProviders(t *testing.T) {
testResolver := testPeerResolver{peers: peers}

c := retrievalimpl.NewClient(net, bs, &testRetrievalNode{}, &testResolver)
testCid := testutil.GenerateCids(1)[0].Bytes()
testCid := []byte("somePieceCID")
assert.Len(t, c.FindProviders(testCid), 3)
})

t.Run("when there is an error, returns empty provider list", func(t *testing.T) {
peers := tut.RequireGenerateRetrievalPeers(t, 1)
testResolver := testPeerResolver{peers: peers}
testResolver := testPeerResolver{peers: []retrievalmarket.RetrievalPeer{}, resolverError: errors.New("boom")}
c := retrievalimpl.NewClient(net, bs, &testRetrievalNode{}, &testResolver)
badCid := []byte("badcid")
badCid := []byte("doesn't matter")
assert.Len(t, c.FindProviders(badCid), 0)
})

t.Run("when there are no providers", func(t *testing.T) {
testResolver := testPeerResolver{peers: []retrievalmarket.RetrievalPeer{}}
c := retrievalimpl.NewClient(net, bs, &testRetrievalNode{}, &testResolver)
testCid := testutil.GenerateCids(1)[0].Bytes()
testCid := []byte("unimportant")
assert.Len(t, c.FindProviders(testCid), 0)
})
}
Expand All @@ -178,10 +176,11 @@ func (t *testRetrievalNode) CreatePaymentVoucher(ctx context.Context, paymentCha

type testPeerResolver struct {
peers []retrievalmarket.RetrievalPeer
resolverError error
}

var _ retrievalmarket.PeerResolver = &testPeerResolver{}

func (t testPeerResolver) GetPeers(data cid.Cid) ([]retrievalmarket.RetrievalPeer, error) {
return t.peers, nil
func (tpr testPeerResolver) GetPeers( []byte) ([]retrievalmarket.RetrievalPeer, error) {
return tpr.peers, tpr.resolverError
}
2 changes: 1 addition & 1 deletion retrievalmarket/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ type RetrievalProviderNode interface {

// PeerResolver is an interface for looking up providers that may have a piece
type PeerResolver interface {
GetPeers(data cid.Cid) ([]RetrievalPeer, error) // TODO: channel
GetPeers(pieceCID []byte) ([]RetrievalPeer, error) // TODO: channel
}

// RetrievalPeer is a provider address/peer.ID pair (everything needed to make
Expand Down

0 comments on commit 5324d30

Please sign in to comment.