Skip to content

Commit

Permalink
Add Selector to retrieval params (#175)
Browse files Browse the repository at this point in the history
* Add Selector to retrieval params

* Funnel calls through NewParamsV0

* Update cbor-gen

* Obey the linter
  • Loading branch information
ingar authored Apr 7, 2020
1 parent d099de3 commit b8b9af5
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 30 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ require (
github.com/libp2p/go-libp2p-core v0.3.0
github.com/multiformats/go-multihash v0.0.13
github.com/stretchr/testify v1.4.0
github.com/whyrusleeping/cbor-gen v0.0.0-20200206220010-03c9665e2a66
github.com/whyrusleeping/cbor-gen v0.0.0-20200402171437-3d27c146c105
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,8 @@ github.com/whyrusleeping/cbor-gen v0.0.0-20200123233031-1cdf64d27158 h1:WXhVOwj2
github.com/whyrusleeping/cbor-gen v0.0.0-20200123233031-1cdf64d27158/go.mod h1:Xj/M2wWU+QdTdRbu/L/1dIZY8/Wb2K9pAhtroQuxJJI=
github.com/whyrusleeping/cbor-gen v0.0.0-20200206220010-03c9665e2a66 h1:LolR9FiEfQNn5U031bAhn/46po2JgWHKadYbcWFIJ+0=
github.com/whyrusleeping/cbor-gen v0.0.0-20200206220010-03c9665e2a66/go.mod h1:Xj/M2wWU+QdTdRbu/L/1dIZY8/Wb2K9pAhtroQuxJJI=
github.com/whyrusleeping/cbor-gen v0.0.0-20200402171437-3d27c146c105 h1:Sh6UG5dW5xW8Ek2CtRGq4ipdEvvx9hOyBJjEGyTYDl0=
github.com/whyrusleeping/cbor-gen v0.0.0-20200402171437-3d27c146c105/go.mod h1:Xj/M2wWU+QdTdRbu/L/1dIZY8/Wb2K9pAhtroQuxJJI=
github.com/whyrusleeping/chunker v0.0.0-20181014151217-fe64bd25879f h1:jQa4QT2UP9WYv2nzyawpKMOCl+Z/jW7djv2/J50lj9E=
github.com/whyrusleeping/chunker v0.0.0-20181014151217-fe64bd25879f/go.mod h1:p9UJB6dDgdPgMJZs7UjUOdulKyRr9fqkS+6JKAInPy8=
github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1 h1:EKhdznlJHPMoKr0XTrX+IlJs1LH3lyx2nfr1dOlZ79k=
Expand Down
7 changes: 2 additions & 5 deletions retrievalmarket/impl/clientstates/client_states_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,11 +501,8 @@ func makeDealState(status retrievalmarket.DealStatus) *retrievalmarket.ClientDea
FundsSpent: defaultFundsSpent,
PaymentRequested: defaultPaymentRequested,
DealProposal: retrievalmarket.DealProposal{
ID: retrievalmarket.DealID(10),
Params: retrievalmarket.Params{
PricePerByte: defaultPricePerByte,
PaymentIntervalIncrease: defaultIntervalIncrease,
},
ID: retrievalmarket.DealID(10),
Params: retrievalmarket.NewParamsV0(defaultPricePerByte, 0, defaultIntervalIncrease),
},
}
}
Expand Down
6 changes: 1 addition & 5 deletions retrievalmarket/impl/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,7 @@ CurrentInterval: %d
require.NoError(t, err)
require.Equal(t, retrievalmarket.QueryResponseAvailable, resp.Status)

rmParams := retrievalmarket.Params{
PricePerByte: pricePerByte,
PaymentInterval: paymentInterval,
PaymentIntervalIncrease: paymentIntervalIncrease,
}
rmParams := retrievalmarket.NewParamsV0(pricePerByte, paymentInterval, paymentIntervalIncrease)

// *** Retrieve the piece
did, err := client.Retrieve(bgCtx, payloadCID, rmParams, expectedTotal, retrievalPeer.ID, clientPaymentChannel, retrievalPeer.Address)
Expand Down
14 changes: 3 additions & 11 deletions retrievalmarket/impl/providerstates/provider_states_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,7 @@ func TestReceiveDeal(t *testing.T) {
proposal := retrievalmarket.DealProposal{
ID: retrievalmarket.DealID(10),
PayloadCID: expectedPiece,
Params: retrievalmarket.Params{
PricePerByte: defaultPricePerByte,
PaymentInterval: defaultCurrentInterval,
PaymentIntervalIncrease: defaultIntervalIncrease,
},
Params: retrievalmarket.NewParamsV0(defaultPricePerByte, defaultCurrentInterval, defaultIntervalIncrease),
}

blankDealState := func() *retrievalmarket.ProviderDealState {
Expand Down Expand Up @@ -465,12 +461,8 @@ func makeDealState(status retrievalmarket.DealStatus) *retrievalmarket.ProviderD
CurrentInterval: defaultCurrentInterval,
FundsReceived: defaultFundsReceived,
DealProposal: retrievalmarket.DealProposal{
ID: dealID,
Params: retrievalmarket.Params{
PricePerByte: defaultPricePerByte,
PaymentInterval: defaultCurrentInterval,
PaymentIntervalIncrease: defaultIntervalIncrease,
},
ID: dealID,
Params: retrievalmarket.NewParamsV0(defaultPricePerByte, defaultCurrentInterval, defaultIntervalIncrease),
},
}
}
Expand Down
22 changes: 21 additions & 1 deletion retrievalmarket/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package retrievalmarket

import (
"bytes"
"context"
"errors"
"fmt"
Expand All @@ -11,7 +12,10 @@ import (
"github.com/filecoin-project/specs-actors/actors/abi/big"
"github.com/filecoin-project/specs-actors/actors/builtin/paych"
"github.com/ipfs/go-cid"
"github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/encoding/dagcbor"
"github.com/libp2p/go-libp2p-core/peer"
cbg "github.com/whyrusleeping/cbor-gen"

"github.com/filecoin-project/go-fil-markets/shared"
)
Expand Down Expand Up @@ -520,7 +524,7 @@ func IsTerminalStatus(status DealStatus) bool {

// Params are the parameters requested for a retrieval deal proposal
type Params struct {
//Selector ipld.Node // V1
Selector *cbg.Deferred // V1
PricePerByte abi.TokenAmount
PaymentInterval uint64 // when to request payment
PaymentIntervalIncrease uint64 //
Expand All @@ -535,6 +539,22 @@ func NewParamsV0(pricePerByte abi.TokenAmount, paymentInterval uint64, paymentIn
}
}

// NewParamsV1 generates parameters for a retrieval deal, including a selector
func NewParamsV1(pricePerByte abi.TokenAmount, paymentInterval uint64, paymentIntervalIncrease uint64, sel ipld.Node) Params {
var buffer bytes.Buffer
err := dagcbor.Encoder(sel, &buffer)
if err != nil {
return Params{}
}

return Params{
Selector: &cbg.Deferred{Raw: buffer.Bytes()},
PricePerByte: pricePerByte,
PaymentInterval: paymentInterval,
PaymentIntervalIncrease: paymentIntervalIncrease,
}
}

// DealID is an identifier for a retrieval deal (unique to a client)
type DealID uint64

Expand Down
30 changes: 28 additions & 2 deletions retrievalmarket/types_cbor_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions retrievalmarket/types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package retrievalmarket_test

import (
"bytes"
"testing"

"github.com/filecoin-project/specs-actors/actors/abi"
"github.com/ipld/go-ipld-prime/encoding/dagcbor"
ipldfree "github.com/ipld/go-ipld-prime/impl/free"
"github.com/ipld/go-ipld-prime/traversal/selector"
"github.com/ipld/go-ipld-prime/traversal/selector/builder"
"github.com/stretchr/testify/assert"

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

func TestParamsMarshalUnmarshal(t *testing.T) {
ssb := builder.NewSelectorSpecBuilder(ipldfree.NodeBuilder())
node := ssb.ExploreRecursive(selector.RecursionLimitNone(), ssb.ExploreAll(ssb.ExploreRecursiveEdge())).Node()
params := retrievalmarket.NewParamsV1(abi.NewTokenAmount(123), 456, 789, node)

buf := new(bytes.Buffer)
err := params.MarshalCBOR(buf)
assert.NoError(t, err)

unmarshalled := &retrievalmarket.Params{}
err = unmarshalled.UnmarshalCBOR(buf)
assert.NoError(t, err)

assert.Equal(t, params, *unmarshalled)

sel, err := dagcbor.Decoder(ipldfree.NodeBuilder(), bytes.NewBuffer(unmarshalled.Selector.Raw))
assert.NoError(t, err)
assert.Equal(t, sel, node)
}
6 changes: 1 addition & 5 deletions shared_testutil/test_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,7 @@ func MakeTestDealProposal() retrievalmarket.DealProposal {
return retrievalmarket.DealProposal{
PayloadCID: cid,
ID: retrievalmarket.DealID(rand.Uint64()),
Params: retrievalmarket.Params{
PricePerByte: MakeTestTokenAmount(),
PaymentInterval: rand.Uint64(),
PaymentIntervalIncrease: rand.Uint64(),
},
Params: retrievalmarket.NewParamsV0(MakeTestTokenAmount(), rand.Uint64(), rand.Uint64()),
}
}

Expand Down

0 comments on commit b8b9af5

Please sign in to comment.