-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Upgrade Query Protocol to Spec V0 (#25)
* feat(retrieval): add network abstraction skeleton Create a network to wrap cbor read/writes and introduce some typechecking, and provide a mockable abstraction * feat(retrievalmarket): upgrade query protocol to spec v0 Implements spec v0 of retrieval query protocol. Also defines mocks for network * fix(retrievalmarket): fix lint errors * updates after rebase * some updates after rebase with master * fix tests/breakages * fix test * updates after repo rename & rebase with master Co-authored-by: Shannon Wells <shannonwells@users.noreply.github.com>
- Loading branch information
1 parent
7ac5a8a
commit 65eccea
Showing
13 changed files
with
869 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package retrievalimpl_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/filecoin-project/go-address" | ||
"github.com/ipfs/go-datastore" | ||
dss "github.com/ipfs/go-datastore/sync" | ||
bstore "github.com/ipfs/go-ipfs-blockstore" | ||
"github.com/libp2p/go-libp2p-core/peer" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/filecoin-project/go-fil-markets/retrievalmarket" | ||
retrievalimpl "github.com/filecoin-project/go-fil-markets/retrievalmarket/impl" | ||
"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" | ||
) | ||
|
||
func TestClient_Query(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
bs := bstore.NewBlockstore(dss.MutexWrap(datastore.NewMapDatastore())) | ||
|
||
pcid := []byte(string("applesauce")) | ||
expectedPeer := peer.ID("somevalue") | ||
rpeer := retrievalmarket.RetrievalPeer{ | ||
Address: address.TestAddress2, | ||
ID: expectedPeer, | ||
} | ||
|
||
expectedQuery := retrievalmarket.Query{ | ||
PieceCID: pcid, | ||
} | ||
|
||
expectedQueryResponse := retrievalmarket.QueryResponse{ | ||
Status: retrievalmarket.QueryResponseAvailable, | ||
Size: 1234, | ||
PaymentAddress: address.TestAddress, | ||
MinPricePerByte: tokenamount.FromInt(5678), | ||
MaxPaymentInterval: 4321, | ||
MaxPaymentIntervalIncrease: 0, | ||
} | ||
|
||
t.Run("it works", func(t *testing.T) { | ||
var qsb tut.QueryStreamBuilder = func(p peer.ID) (rmnet.RetrievalQueryStream, error) { | ||
return tut.NewTestRetrievalQueryStream(tut.TestQueryStreamParams{ | ||
Writer: tut.ExpectQueryWriter(t, expectedQuery, "queries should match"), | ||
RespReader: tut.StubbedQueryResponseReader(expectedQueryResponse), | ||
}), nil | ||
} | ||
net := tut.NewTestRetrievalMarketNetwork(tut.TestNetworkParams{ | ||
QueryStreamBuilder: tut.ExpectPeerOnQueryStreamBuilder(t, expectedPeer, qsb, "Peers should match"), | ||
}) | ||
c := retrievalimpl.NewClient(net, bs, &testRetrievalNode{}) | ||
|
||
resp, err := c.Query(ctx, rpeer, pcid, retrievalmarket.QueryParams{}) | ||
require.NoError(t, err) | ||
assert.NotNil(t, resp) | ||
assert.Equal(t, expectedQueryResponse, resp) | ||
}) | ||
|
||
t.Run("when the stream returns error, returns error", func(t *testing.T) { | ||
net := tut.NewTestRetrievalMarketNetwork(tut.TestNetworkParams{ | ||
QueryStreamBuilder: tut.FailNewQueryStream, | ||
}) | ||
c := retrievalimpl.NewClient(net, bs, &testRetrievalNode{}) | ||
|
||
_, err := c.Query(ctx, rpeer, pcid, retrievalmarket.QueryParams{}) | ||
assert.EqualError(t, err, "new query stream failed") | ||
}) | ||
|
||
t.Run("when WriteQuery fails, returns error", func(t *testing.T) { | ||
|
||
qsbuilder := func(p peer.ID) (network.RetrievalQueryStream, error) { | ||
newStream := tut.NewTestRetrievalQueryStream(tut.TestQueryStreamParams{ | ||
PeerID: p, | ||
Writer: tut.FailQueryWriter, | ||
}) | ||
return newStream, nil | ||
} | ||
|
||
net := tut.NewTestRetrievalMarketNetwork(tut.TestNetworkParams{ | ||
QueryStreamBuilder: qsbuilder, | ||
}) | ||
c := retrievalimpl.NewClient(net, bs, &testRetrievalNode{}) | ||
|
||
statusCode, err := c.Query(ctx, rpeer, pcid, retrievalmarket.QueryParams{}) | ||
assert.EqualError(t, err, "write query failed") | ||
assert.Equal(t, retrievalmarket.QueryResponseUndefined, statusCode) | ||
}) | ||
|
||
t.Run("when ReadQueryResponse fails, returns error", func(t *testing.T) { | ||
qsbuilder := func(p peer.ID) (network.RetrievalQueryStream, error) { | ||
newStream := tut.NewTestRetrievalQueryStream(tut.TestQueryStreamParams{ | ||
PeerID: p, | ||
RespReader: tut.FailResponseReader, | ||
}) | ||
return newStream, nil | ||
} | ||
net := tut.NewTestRetrievalMarketNetwork(tut.TestNetworkParams{ | ||
QueryStreamBuilder: qsbuilder, | ||
}) | ||
c := retrievalimpl.NewClient(net, bs, &testRetrievalNode{}) | ||
|
||
statusCode, err := c.Query(ctx, rpeer, pcid, retrievalmarket.QueryParams{}) | ||
assert.EqualError(t, err, "query response failed") | ||
assert.Equal(t, retrievalmarket.QueryResponseUndefined, statusCode) | ||
}) | ||
} | ||
|
||
type testRetrievalNode struct { | ||
} | ||
|
||
func (t *testRetrievalNode) GetOrCreatePaymentChannel(ctx context.Context, clientAddress address.Address, minerAddress address.Address, clientFundsAvailable tokenamount.TokenAmount) (address.Address, error) { | ||
return address.Address{}, nil | ||
} | ||
|
||
func (t *testRetrievalNode) AllocateLane(paymentChannel address.Address) (uint64, error) { | ||
return 0, nil | ||
} | ||
|
||
func (t *testRetrievalNode) CreatePaymentVoucher(ctx context.Context, paymentChannel address.Address, amount tokenamount.TokenAmount, lane uint64) (*types.SignedVoucher, error) { | ||
return nil, nil | ||
} |
Oops, something went wrong.