From 22e0ea4579038c62bca2b5eb017cc087c8f13e04 Mon Sep 17 00:00:00 2001 From: Rod Vagg Date: Fri, 10 Jun 2022 20:18:17 +1000 Subject: [PATCH] feat(ipld): use new bindnode registry in go-ipld-prime Ref: https://github.com/ipld/go-ipld-prime/pull/437 --- bindnodeutils/bindnodeutils.go | 119 ------------------ go.mod | 4 +- go.sum | 7 +- retrievalmarket/impl/client.go | 5 +- retrievalmarket/impl/dtutils/dtutils_test.go | 9 +- retrievalmarket/impl/ipld_compat_test.go | 13 +- .../impl/providerstates/provider_states.go | 5 +- .../providerstates/provider_states_test.go | 7 +- .../requestvalidation/requestvalidation.go | 7 +- .../requestvalidation_test.go | 9 +- retrievalmarket/types.go | 20 +-- .../impl/clientstates/client_states.go | 3 +- storagemarket/impl/dtutils/dtutils.go | 7 +- storagemarket/impl/dtutils/dtutils_test.go | 3 +- storagemarket/impl/ipld_compat_test.go | 5 +- .../impl/requestvalidation/common.go | 5 +- .../request_validation_test.go | 18 +-- storagemarket/impl/requestvalidation/types.go | 6 +- 18 files changed, 63 insertions(+), 189 deletions(-) delete mode 100644 bindnodeutils/bindnodeutils.go diff --git a/bindnodeutils/bindnodeutils.go b/bindnodeutils/bindnodeutils.go deleted file mode 100644 index a185d1b2..00000000 --- a/bindnodeutils/bindnodeutils.go +++ /dev/null @@ -1,119 +0,0 @@ -package bindnodeutils - -import ( - "fmt" - "io" - "reflect" - - "github.com/ipld/go-ipld-prime" - "github.com/ipld/go-ipld-prime/codec" - "github.com/ipld/go-ipld-prime/datamodel" - "github.com/ipld/go-ipld-prime/node/bindnode" - "github.com/ipld/go-ipld-prime/schema" -) - -// We use the prototype map to store TypedPrototype and bindnode options mapped -// against the Go type so we only have to run the schema parse once and we -// can be sure to use the right options (converters) whenever operating on -// this type. - -type prototypeData struct { - proto schema.TypedPrototype - options []bindnode.Option -} - -var prototype map[reflect.Type]prototypeData = make(map[reflect.Type]prototypeData) - -func typeOf(ptrValue interface{}) reflect.Type { - val := reflect.ValueOf(ptrValue).Type() - for val.Kind() == reflect.Ptr { - val = val.Elem() - } - return val -} - -// lookup of cached TypedPrototype (and therefore Type) for a Go type, if not -// found, initial parse and setup and caching of the TypedPrototype will happen -func prototypeDataFor(ptrType interface{}) prototypeData { - typ := typeOf(ptrType) - proto, ok := prototype[typ] - if !ok { - panic(fmt.Sprintf("bindnode utils: type has not been registered: %s", typ.Name())) - } - return proto -} - -// RegisterType registers ptrType with schema such that it can be wrapped and -// unwrapped without needing the schema, Type, or TypedPrototype. -// Typically the typeName will match the Go type name, but it can be whatever -// is defined in the schema for the type being registered. -// -// May panic if the schema is invalid or the type doesn't match the schema. -func RegisterType(ptrType interface{}, schema string, typeName string, options ...bindnode.Option) { - typ := typeOf(ptrType) - if _, ok := prototype[typ]; ok { - panic(fmt.Sprintf("bindnode utils: type already registered: %s", typ.Name())) - } - typeSystem, err := ipld.LoadSchemaBytes([]byte(schema)) - if err != nil { - panic(fmt.Sprintf("bindnode utils: failed to load schema: %s", err.Error())) - } - schemaType := typeSystem.TypeByName(typeName) - if schemaType == nil { - panic(fmt.Sprintf("bindnode utils: schema for [%T] does not contain that named type [%s]", ptrType, typ.Name())) - } - prototype[typ] = prototypeData{ - bindnode.Prototype(ptrType, schemaType, options...), - options, - } -} - -// IsRegistered can be used to determine if the type has already been registered -// within this current application instance. -// Using RegisterType on an already registered type will cause a panic, so where -// this may be the case, IsRegistered can be used to check. -func IsRegistered(ptrType interface{}) bool { - _, ok := prototype[typeOf(ptrType)] - return ok -} - -// TypeFromReader deserializes DAG-CBOR from a Reader and instantiates the Go -// type that's provided as a pointer via the ptrValue argument. -func TypeFromReader(r io.Reader, ptrValue interface{}, decoder codec.Decoder) (interface{}, error) { - protoData := prototypeDataFor(ptrValue) - node, err := ipld.DecodeStreamingUsingPrototype(r, decoder, protoData.proto) - if err != nil { - return nil, err - } - typ := bindnode.Unwrap(node) - return typ, nil -} - -// TypeFromNode converts an datamodel.Node into an appropriate Go type that's -// provided as a pointer via the ptrValue argument -func TypeFromNode(node datamodel.Node, ptrValue interface{}) (interface{}, error) { - protoData := prototypeDataFor(ptrValue) - if tn, ok := node.(schema.TypedNode); ok { - node = tn.Representation() - } - builder := protoData.proto.Representation().NewBuilder() - err := builder.AssignNode(node) - if err != nil { - return nil, err - } - typ := bindnode.Unwrap(builder.Build()) - return typ, nil -} - -// TypeToNode converts a Go type that's provided as a pointer via the ptrValue -// argument to an schema.TypedNode. -func TypeToNode(ptrValue interface{}) schema.TypedNode { - protoData := prototypeDataFor(ptrValue) - return bindnode.Wrap(ptrValue, protoData.proto.Type(), protoData.options...) -} - -// TypeToWriter is a utility method that serializes a Go type that's provided as a -// pointer via the ptrValue argument as DAG-CBOR to a Writer -func TypeToWriter(ptrValue interface{}, w io.Writer, encoder codec.Encoder) error { - return ipld.EncodeStreaming(w, TypeToNode(ptrValue), encoder) -} diff --git a/go.mod b/go.mod index 1c692344..09f81d3c 100644 --- a/go.mod +++ b/go.mod @@ -45,7 +45,7 @@ require ( github.com/ipfs/go-unixfs v0.3.1 github.com/ipld/go-car v0.3.3 github.com/ipld/go-car/v2 v2.1.1 - github.com/ipld/go-ipld-prime v0.16.1-0.20220524010812-a2c7491b1229 + github.com/ipld/go-ipld-prime v0.16.1-0.20220610101408-9bfac86e6256 github.com/jbenet/go-random v0.0.0-20190219211222-123a90aedc0c github.com/jpillora/backoff v1.0.0 github.com/libp2p/go-libp2p v0.20.0 @@ -53,7 +53,7 @@ require ( github.com/libp2p/go-libp2p-mplex v0.6.0 // indirect github.com/multiformats/go-multiaddr v0.5.0 github.com/multiformats/go-multibase v0.0.3 - github.com/multiformats/go-multicodec v0.4.1 + github.com/multiformats/go-multicodec v0.5.0 github.com/multiformats/go-multihash v0.1.0 github.com/multiformats/go-varint v0.0.6 github.com/petar/GoLLRB v0.0.0-20210522233825-ae3b015fd3e9 diff --git a/go.sum b/go.sum index 4b7d3d5d..ee02da48 100644 --- a/go.sum +++ b/go.sum @@ -725,8 +725,8 @@ github.com/ipld/go-ipld-prime v0.14.3-0.20211207234443-319145880958/go.mod h1:Qc github.com/ipld/go-ipld-prime v0.14.4/go.mod h1:QcE4Y9n/ZZr8Ijg5bGPT0GqYWgZ1704nH0RDcQtgTP0= github.com/ipld/go-ipld-prime v0.16.0/go.mod h1:axSCuOCBPqrH+gvXr2w9uAOulJqBPhHPT2PjoiiU1qA= github.com/ipld/go-ipld-prime v0.16.1-0.20220519105356-1f1151b69dba/go.mod h1:/bZAYlzT7SJS4UV0al4q67xgKvenm5hKrPCa2wNGN1U= -github.com/ipld/go-ipld-prime v0.16.1-0.20220524010812-a2c7491b1229 h1:A83oV+ZFzTNYLS1/XAyfOPTmtzEM0sMSK18GA0dEorU= -github.com/ipld/go-ipld-prime v0.16.1-0.20220524010812-a2c7491b1229/go.mod h1:IInaTjkNLKAZoliYXvjlB3CFQa7IPReVwxOb0oke/RA= +github.com/ipld/go-ipld-prime v0.16.1-0.20220610101408-9bfac86e6256 h1:LynWK6MPm4frKNcOoRs0XQciKSWBVcouJMmp0I9ukKg= +github.com/ipld/go-ipld-prime v0.16.1-0.20220610101408-9bfac86e6256/go.mod h1:aYcKm5TIvGfY8P3QBKz/2gKcLxzJ1zDaD+o0bOowhgs= github.com/ipld/go-ipld-prime-proto v0.0.0-20191113031812-e32bd156a1e5/go.mod h1:gcvzoEDBjwycpXt3LBE061wT9f46szXGHAmj9uoP6fU= github.com/ipld/go-ipld-prime/storage/bsadapter v0.0.0-20211210234204-ce2a1c70cd73 h1:TsyATB2ZRRQGTwafJdgEUQkmjOExRV0DNokcihZxbnQ= github.com/ipld/go-ipld-prime/storage/bsadapter v0.0.0-20211210234204-ce2a1c70cd73/go.mod h1:2PJ0JgxyB08t0b2WKrcuqI3di0V+5n6RS/LTUJhkoxY= @@ -1302,8 +1302,9 @@ github.com/multiformats/go-multicodec v0.3.0/go.mod h1:qGGaQmioCDh+TeFOnxrbU0DaI github.com/multiformats/go-multicodec v0.3.1-0.20210902112759-1539a079fd61/go.mod h1:1Hj/eHRaVWSXiSNNfcEPcwZleTmdNP81xlxDLnWU9GQ= github.com/multiformats/go-multicodec v0.3.1-0.20211210143421-a526f306ed2c/go.mod h1:1Hj/eHRaVWSXiSNNfcEPcwZleTmdNP81xlxDLnWU9GQ= github.com/multiformats/go-multicodec v0.4.0/go.mod h1:1Hj/eHRaVWSXiSNNfcEPcwZleTmdNP81xlxDLnWU9GQ= -github.com/multiformats/go-multicodec v0.4.1 h1:BSJbf+zpghcZMZrwTYBGwy0CPcVZGWiC72Cp8bBd4R4= github.com/multiformats/go-multicodec v0.4.1/go.mod h1:1Hj/eHRaVWSXiSNNfcEPcwZleTmdNP81xlxDLnWU9GQ= +github.com/multiformats/go-multicodec v0.5.0 h1:EgU6cBe/D7WRwQb1KmnBvU7lrcFGMggZVTPtOW9dDHs= +github.com/multiformats/go-multicodec v0.5.0/go.mod h1:DiY2HFaEp5EhEXb/iYzVAunmyX/aSFMxq2KMKfWEues= github.com/multiformats/go-multihash v0.0.1/go.mod h1:w/5tugSrLEbWqlcgJabL3oHFKTwfvkofsjW2Qa1ct4U= github.com/multiformats/go-multihash v0.0.5/go.mod h1:lt/HCbqlQwlPBz7lv0sQCdtfcMtlJvakRUn/0Ual8po= github.com/multiformats/go-multihash v0.0.8/go.mod h1:YSLudS+Pi8NHE7o6tb3D8vrpKa63epEDmG8nTduyAew= diff --git a/retrievalmarket/impl/client.go b/retrievalmarket/impl/client.go index a15b7cb4..8b23fbae 100644 --- a/retrievalmarket/impl/client.go +++ b/retrievalmarket/impl/client.go @@ -23,7 +23,6 @@ import ( "github.com/filecoin-project/go-state-types/big" "github.com/filecoin-project/go-statemachine/fsm" - "github.com/filecoin-project/go-fil-markets/bindnodeutils" "github.com/filecoin-project/go-fil-markets/discovery" "github.com/filecoin-project/go-fil-markets/retrievalmarket" "github.com/filecoin-project/go-fil-markets/retrievalmarket/impl/clientstates" @@ -405,12 +404,12 @@ func (c *clientDealEnvironment) OpenDataTransfer(ctx context.Context, to peer.ID if proposal.SelectorSpecified() { sel = proposal.Selector.Node } - vouch := bindnodeutils.TypeToNode(proposal) + vouch := retrievalmarket.BindnodeRegistry.TypeToNode(proposal) return c.c.dataTransfer.OpenPullDataChannel(ctx, to, datatransfer.TypedVoucher{Voucher: vouch, Type: retrievalmarket.DealProposalType}, proposal.PayloadCID, sel) } func (c *clientDealEnvironment) SendDataTransferVoucher(ctx context.Context, channelID datatransfer.ChannelID, payment *retrievalmarket.DealPayment) error { - vouch := bindnodeutils.TypeToNode(payment) + vouch := retrievalmarket.BindnodeRegistry.TypeToNode(payment) return c.c.dataTransfer.SendVoucher(ctx, channelID, datatransfer.TypedVoucher{Voucher: vouch, Type: retrievalmarket.DealPaymentType}) } diff --git a/retrievalmarket/impl/dtutils/dtutils_test.go b/retrievalmarket/impl/dtutils/dtutils_test.go index 8aea3ae8..2e6e1389 100644 --- a/retrievalmarket/impl/dtutils/dtutils_test.go +++ b/retrievalmarket/impl/dtutils/dtutils_test.go @@ -16,7 +16,6 @@ import ( datatransfer "github.com/filecoin-project/go-data-transfer/v2" "github.com/filecoin-project/go-statemachine/fsm" - "github.com/filecoin-project/go-fil-markets/bindnodeutils" "github.com/filecoin-project/go-fil-markets/retrievalmarket" rm "github.com/filecoin-project/go-fil-markets/retrievalmarket" "github.com/filecoin-project/go-fil-markets/retrievalmarket/impl/dtutils" @@ -25,7 +24,7 @@ import ( func TestProviderDataTransferSubscriber(t *testing.T) { dealProposal := shared_testutil.MakeTestDealProposal() - node := bindnodeutils.TypeToNode(dealProposal) + node := rm.BindnodeRegistry.TypeToNode(dealProposal) dealProposalVoucher := datatransfer.TypedVoucher{Voucher: node, Type: rm.DealProposalType} testPeers := shared_testutil.GeneratePeers(2) transferID := datatransfer.TransferID(rand.Uint64()) @@ -112,10 +111,10 @@ func TestProviderDataTransferSubscriber(t *testing.T) { } func TestClientDataTransferSubscriber(t *testing.T) { dealProposal := shared_testutil.MakeTestDealProposal() - node := bindnodeutils.TypeToNode(dealProposal) + node := rm.BindnodeRegistry.TypeToNode(dealProposal) dealProposalVoucher := datatransfer.TypedVoucher{Voucher: node, Type: retrievalmarket.DealProposalType} dealResponseVoucher := func(dealResponse retrievalmarket.DealResponse) datatransfer.TypedVoucher { - node := bindnodeutils.TypeToNode(&dealResponse) + node := rm.BindnodeRegistry.TypeToNode(&dealResponse) return datatransfer.TypedVoucher{Voucher: node, Type: retrievalmarket.DealResponseType} } paymentOwed := shared_testutil.MakeTestTokenAmount() @@ -322,7 +321,7 @@ func TestTransportConfigurer(t *testing.T) { thisPeer := expectedChannelID.Initiator expectedPeer := expectedChannelID.Responder dealProposalVoucher := func(proposal rm.DealProposal) datatransfer.TypedVoucher { - node := bindnodeutils.TypeToNode(&proposal) + node := rm.BindnodeRegistry.TypeToNode(&proposal) return datatransfer.TypedVoucher{Voucher: node, Type: rm.DealProposalType} } diff --git a/retrievalmarket/impl/ipld_compat_test.go b/retrievalmarket/impl/ipld_compat_test.go index 74cd7f3f..d486a226 100644 --- a/retrievalmarket/impl/ipld_compat_test.go +++ b/retrievalmarket/impl/ipld_compat_test.go @@ -22,7 +22,6 @@ import ( "github.com/filecoin-project/go-state-types/crypto" "github.com/filecoin-project/specs-actors/actors/builtin/paych" - "github.com/filecoin-project/go-fil-markets/bindnodeutils" "github.com/filecoin-project/go-fil-markets/retrievalmarket" ) @@ -62,7 +61,7 @@ func TestIpldCompat_DealResponse(t *testing.T) { nb := basicnode.Prototype.Any.NewBuilder() assert.Nil(t, dagcbor.Decode(nb, &originalBuf)) node := nb.Build() - drBindnodeIface, err := bindnodeutils.TypeFromNode(node, &retrievalmarket.DealResponse{}) + drBindnodeIface, err := retrievalmarket.BindnodeRegistry.TypeFromNode(node, &retrievalmarket.DealResponse{}) assert.Nil(t, err) drBindnode, ok := drBindnodeIface.(*retrievalmarket.DealResponse) assert.True(t, ok) @@ -71,7 +70,7 @@ func TestIpldCompat_DealResponse(t *testing.T) { compareDealResponse(t, testCase.dr, *drBindnode) // encode the new DealResponse with bindnode to bytes - node = bindnodeutils.TypeToNode(drBindnode) + node = retrievalmarket.BindnodeRegistry.TypeToNode(drBindnode) var bindnodeBuf bytes.Buffer dagcbor.Encode(node.(schema.TypedNode).Representation(), &bindnodeBuf) bindnodeBytes := bindnodeBuf.Bytes() @@ -153,7 +152,7 @@ func TestIpldCompat_DealProposal(t *testing.T) { nb := basicnode.Prototype.Any.NewBuilder() assert.Nil(t, dagcbor.Decode(nb, &originalBuf)) node := nb.Build() - dpBindnodeIface, err := bindnodeutils.TypeFromNode(node, &retrievalmarket.DealProposal{}) + dpBindnodeIface, err := retrievalmarket.BindnodeRegistry.TypeFromNode(node, &retrievalmarket.DealProposal{}) assert.Nil(t, err) dpBindnode, ok := dpBindnodeIface.(*retrievalmarket.DealProposal) assert.True(t, ok) @@ -162,7 +161,7 @@ func TestIpldCompat_DealProposal(t *testing.T) { compareDealProposal(t, testCase.dp, *dpBindnode) // encode the new DealProposal with bindnode to bytes - node = bindnodeutils.TypeToNode(dpBindnode) + node = retrievalmarket.BindnodeRegistry.TypeToNode(dpBindnode) var bindnodeBuf bytes.Buffer dagcbor.Encode(node.(schema.TypedNode).Representation(), &bindnodeBuf) bindnodeBytes := bindnodeBuf.Bytes() @@ -273,7 +272,7 @@ func TestIpldCompat_DealPayment(t *testing.T) { nb := basicnode.Prototype.Any.NewBuilder() assert.Nil(t, dagcbor.Decode(nb, &originalBuf)) node := nb.Build() - dpBindnodeIface, err := bindnodeutils.TypeFromNode(node, &retrievalmarket.DealPayment{}) + dpBindnodeIface, err := retrievalmarket.BindnodeRegistry.TypeFromNode(node, &retrievalmarket.DealPayment{}) assert.Nil(t, err) dpBindnode, ok := dpBindnodeIface.(*retrievalmarket.DealPayment) assert.True(t, ok) @@ -282,7 +281,7 @@ func TestIpldCompat_DealPayment(t *testing.T) { compareDealPayment(t, testCase.dp, *dpBindnode) // encode the new DealPayment with bindnode to bytes - node = bindnodeutils.TypeToNode(dpBindnode) + node = retrievalmarket.BindnodeRegistry.TypeToNode(dpBindnode) var bindnodeBuf bytes.Buffer dagcbor.Encode(node.(schema.TypedNode).Representation(), &bindnodeBuf) bindnodeBytes := bindnodeBuf.Bytes() diff --git a/retrievalmarket/impl/providerstates/provider_states.go b/retrievalmarket/impl/providerstates/provider_states.go index ddaa83f1..4ad9d430 100644 --- a/retrievalmarket/impl/providerstates/provider_states.go +++ b/retrievalmarket/impl/providerstates/provider_states.go @@ -13,7 +13,6 @@ import ( "github.com/filecoin-project/go-statemachine" "github.com/filecoin-project/go-statemachine/fsm" - "github.com/filecoin-project/go-fil-markets/bindnodeutils" rm "github.com/filecoin-project/go-fil-markets/retrievalmarket" ) @@ -138,7 +137,7 @@ func updateFunding(ctx fsm.Context, DataLimit: deal.Params.NextInterval(totalPaid), } if voucherResult != nil { - node := bindnodeutils.TypeToNode(voucherResult) + node := rm.BindnodeRegistry.TypeToNode(voucherResult) vr.VoucherResult = &datatransfer.TypedVoucher{Voucher: node, Type: rm.DealResponseType} } return vr @@ -183,7 +182,7 @@ func errorDealResponse(dealID rm.ProviderDealIdentifier, errMsg error) datatrans Message: errMsg.Error(), Status: rm.DealStatusErrored, } - node := bindnodeutils.TypeToNode(&dr) + node := rm.BindnodeRegistry.TypeToNode(&dr) return datatransfer.ValidationResult{ Accepted: false, VoucherResult: &datatransfer.TypedVoucher{Voucher: node, Type: rm.DealResponseType}, diff --git a/retrievalmarket/impl/providerstates/provider_states_test.go b/retrievalmarket/impl/providerstates/provider_states_test.go index 143aec38..dbca900c 100644 --- a/retrievalmarket/impl/providerstates/provider_states_test.go +++ b/retrievalmarket/impl/providerstates/provider_states_test.go @@ -19,7 +19,6 @@ import ( fsmtest "github.com/filecoin-project/go-statemachine/fsm/testutil" "github.com/filecoin-project/specs-actors/actors/builtin/paych" - "github.com/filecoin-project/go-fil-markets/bindnodeutils" "github.com/filecoin-project/go-fil-markets/piecestore" "github.com/filecoin-project/go-fil-markets/retrievalmarket" rm "github.com/filecoin-project/go-fil-markets/retrievalmarket" @@ -151,13 +150,13 @@ func TestUnpauseDeal(t *testing.T) { func TestUpdateFunding(t *testing.T) { ctx := context.Background() emptyDealPayment := rm.DealPayment{} - emptyDealPaymentNode := bindnodeutils.TypeToNode(&emptyDealPayment) + emptyDealPaymentNode := rm.BindnodeRegistry.TypeToNode(&emptyDealPayment) emptyDealPaymentVoucher := datatransfer.TypedVoucher{Voucher: emptyDealPaymentNode, Type: rm.DealPaymentType} emptyDealProposal := rm.DealProposal{} - emptyDealProposalNode := bindnodeutils.TypeToNode(&emptyDealProposal) + emptyDealProposalNode := rm.BindnodeRegistry.TypeToNode(&emptyDealProposal) emptyDealProposalVoucher := datatransfer.TypedVoucher{Voucher: emptyDealProposalNode, Type: rm.DealProposalType} dealResponseVoucher := func(resp rm.DealResponse) *datatransfer.TypedVoucher { - node := bindnodeutils.TypeToNode(&resp) + node := rm.BindnodeRegistry.TypeToNode(&resp) return &datatransfer.TypedVoucher{Voucher: node, Type: rm.DealResponseType} } eventMachine, err := fsm.NewEventProcessor(rm.ProviderDealState{}, "Status", providerstates.ProviderEvents) diff --git a/retrievalmarket/impl/requestvalidation/requestvalidation.go b/retrievalmarket/impl/requestvalidation/requestvalidation.go index 99507daa..5c4f6194 100644 --- a/retrievalmarket/impl/requestvalidation/requestvalidation.go +++ b/retrievalmarket/impl/requestvalidation/requestvalidation.go @@ -15,7 +15,6 @@ import ( "github.com/filecoin-project/go-state-types/abi" "github.com/filecoin-project/go-state-types/big" - "github.com/filecoin-project/go-fil-markets/bindnodeutils" "github.com/filecoin-project/go-fil-markets/piecestore" rm "github.com/filecoin-project/go-fil-markets/retrievalmarket" ) @@ -70,7 +69,7 @@ func rejectProposal(proposal *rm.DealProposal, status rm.DealStatus, reason stri Status: status, Message: reason, } - node := bindnodeutils.TypeToNode(&dr) + node := rm.BindnodeRegistry.TypeToNode(&dr) return datatransfer.ValidationResult{ Accepted: false, VoucherResult: &datatransfer.TypedVoucher{Voucher: node, Type: rm.DealResponseType}, @@ -153,7 +152,7 @@ func (rv *ProviderRequestValidator) validatePull(receiver peer.ID, proposal *rm. Status: status, PaymentOwed: deal.Params.OutstandingBalance(big.Zero(), 0, false), } - node := bindnodeutils.TypeToNode(&dr) + node := rm.BindnodeRegistry.TypeToNode(&dr) result := datatransfer.ValidationResult{ Accepted: true, VoucherResult: &datatransfer.TypedVoucher{Voucher: node, Type: rm.DealResponseType}, @@ -207,7 +206,7 @@ func errorDealResponse(dealID rm.ProviderDealIdentifier, err error) (datatransfe Message: err.Error(), Status: rm.DealStatusErrored, } - node := bindnodeutils.TypeToNode(&dr) + node := rm.BindnodeRegistry.TypeToNode(&dr) return datatransfer.ValidationResult{ Accepted: false, VoucherResult: &datatransfer.TypedVoucher{Voucher: node, Type: rm.DealResponseType}, diff --git a/retrievalmarket/impl/requestvalidation/requestvalidation_test.go b/retrievalmarket/impl/requestvalidation/requestvalidation_test.go index effaf7ab..c708b232 100644 --- a/retrievalmarket/impl/requestvalidation/requestvalidation_test.go +++ b/retrievalmarket/impl/requestvalidation/requestvalidation_test.go @@ -17,7 +17,6 @@ import ( "github.com/filecoin-project/go-state-types/abi" "github.com/filecoin-project/go-state-types/big" - "github.com/filecoin-project/go-fil-markets/bindnodeutils" "github.com/filecoin-project/go-fil-markets/piecestore" rm "github.com/filecoin-project/go-fil-markets/retrievalmarket" "github.com/filecoin-project/go-fil-markets/retrievalmarket/impl/requestvalidation" @@ -28,7 +27,7 @@ func TestValidatePush(t *testing.T) { fve := &fakeValidationEnvironment{} sender := shared_testutil.GeneratePeers(1)[0] testDp := shared_testutil.MakeTestDealProposal() - voucher := bindnodeutils.TypeToNode(testDp) + voucher := rm.BindnodeRegistry.TypeToNode(testDp) requestValidator := requestvalidation.NewProviderRequestValidator(fve) validationResult, err := requestValidator.ValidatePush(datatransfer.ChannelID{}, sender, voucher, testDp.PayloadCID, selectorparse.CommonSelector_ExploreAllRecursively) require.Nil(t, validationResult.VoucherResult) @@ -44,13 +43,13 @@ func dealResponseToVoucher(t *testing.T, status rm.DealStatus, id rm.DealID, mes if owed != nil { dr.PaymentOwed = *owed } - node := bindnodeutils.TypeToNode(&dr) + node := rm.BindnodeRegistry.TypeToNode(&dr) return &datatransfer.TypedVoucher{Voucher: node, Type: rm.DealResponseType} } func TestValidatePull(t *testing.T) { proposal := shared_testutil.MakeTestDealProposal() - node := bindnodeutils.TypeToNode(proposal) + node := rm.BindnodeRegistry.TypeToNode(proposal) proposalVoucher := datatransfer.TypedVoucher{Voucher: node, Type: rm.DealProposalType} zero := big.Zero() @@ -196,7 +195,7 @@ func TestValidateRestart(t *testing.T) { ID: dealID, Params: params, } - node := bindnodeutils.TypeToNode(&proposal) + node := rm.BindnodeRegistry.TypeToNode(&proposal) proposalVoucher := datatransfer.TypedVoucher{Voucher: node, Type: rm.DealProposalType} testCases := map[string]struct { diff --git a/retrievalmarket/types.go b/retrievalmarket/types.go index ede3d99c..870e5604 100644 --- a/retrievalmarket/types.go +++ b/retrievalmarket/types.go @@ -8,6 +8,7 @@ import ( "github.com/ipfs/go-cid" "github.com/ipld/go-ipld-prime/datamodel" "github.com/ipld/go-ipld-prime/node/bindnode" + bindnoderegistry "github.com/ipld/go-ipld-prime/node/bindnode/registry" "github.com/libp2p/go-libp2p-core/peer" "github.com/libp2p/go-libp2p-core/protocol" "golang.org/x/xerrors" @@ -18,7 +19,6 @@ import ( "github.com/filecoin-project/go-state-types/big" "github.com/filecoin-project/specs-actors/actors/builtin/paych" - "github.com/filecoin-project/go-fil-markets/bindnodeutils" "github.com/filecoin-project/go-fil-markets/piecestore" ) @@ -241,7 +241,7 @@ type Params struct { // paramsSchema is the IPLD Schema for a serialized Params const paramsSchema = ` type Params struct { - Selector nullable Any # can be nullable, but bindnodeutils.SerializedNode takes care of that + Selector nullable Any # can be nullable, but CborGenCompatibleNode takes care of that PieceCID nullable &Any PricePerByte Bytes # abi.TokenAmount PaymentInterval Int @@ -400,7 +400,7 @@ func DealProposalFromNode(node datamodel.Node) (*DealProposal, error) { if node == nil { return nil, fmt.Errorf("empty voucher") } - dpIface, err := bindnodeutils.TypeFromNode(node, &DealProposal{}) + dpIface, err := BindnodeRegistry.TypeFromNode(node, &DealProposal{}) if err != nil { return nil, xerrors.Errorf("invalid DealProposal: %w", err) } @@ -446,7 +446,7 @@ func DealResponseFromNode(node datamodel.Node) (*DealResponse, error) { if node == nil { return nil, fmt.Errorf("empty voucher") } - dpIface, err := bindnodeutils.TypeFromNode(node, &DealResponse{}) + dpIface, err := BindnodeRegistry.TypeFromNode(node, &DealResponse{}) if err != nil { return nil, xerrors.Errorf("invalid DealResponse: %w", err) } @@ -514,7 +514,7 @@ func DealPaymentFromNode(node datamodel.Node) (*DealPayment, error) { if node == nil { return nil, fmt.Errorf("empty voucher") } - dpIface, err := bindnodeutils.TypeFromNode(node, &DealPayment{}) + dpIface, err := BindnodeRegistry.TypeFromNode(node, &DealPayment{}) if err != nil { return nil, xerrors.Errorf("invalid DealPayment: %w", err) } @@ -590,9 +590,11 @@ type PricingInput struct { CurrentAsk Ask } +var BindnodeRegistry = bindnoderegistry.NewRegistry() + func init() { - bindnodeutils.RegisterType((*Params)(nil), paramsSchema, "Params", paramsBindnodeOptions...) - bindnodeutils.RegisterType((*DealProposal)(nil), dealProposalSchema, "DealProposal", dealProposalBindnodeOptions...) - bindnodeutils.RegisterType((*DealResponse)(nil), dealResponseSchema, "DealResponse", dealResponseBindnodeOptions...) - bindnodeutils.RegisterType((*DealPayment)(nil), dealPaymentSchema, "DealPayment", dealPaymentBindnodeOptions...) + BindnodeRegistry.RegisterType((*Params)(nil), paramsSchema, "Params", paramsBindnodeOptions...) + BindnodeRegistry.RegisterType((*DealProposal)(nil), dealProposalSchema, "DealProposal", dealProposalBindnodeOptions...) + BindnodeRegistry.RegisterType((*DealResponse)(nil), dealResponseSchema, "DealResponse", dealResponseBindnodeOptions...) + BindnodeRegistry.RegisterType((*DealPayment)(nil), dealPaymentSchema, "DealPayment", dealPaymentBindnodeOptions...) } diff --git a/storagemarket/impl/clientstates/client_states.go b/storagemarket/impl/clientstates/client_states.go index 36f21c4b..5494f718 100644 --- a/storagemarket/impl/clientstates/client_states.go +++ b/storagemarket/impl/clientstates/client_states.go @@ -16,7 +16,6 @@ import ( "github.com/filecoin-project/go-state-types/exitcode" "github.com/filecoin-project/go-statemachine/fsm" - "github.com/filecoin-project/go-fil-markets/bindnodeutils" "github.com/filecoin-project/go-fil-markets/storagemarket" "github.com/filecoin-project/go-fil-markets/storagemarket/impl/requestvalidation" "github.com/filecoin-project/go-fil-markets/storagemarket/network" @@ -161,7 +160,7 @@ func InitiateDataTransfer(ctx fsm.Context, environment ClientDealEnvironment, de log.Infof("sending data for a deal %s", deal.ProposalCid) voucher := requestvalidation.StorageDataTransferVoucher{Proposal: deal.ProposalCid} - node := bindnodeutils.TypeToNode(&voucher) + node := requestvalidation.BindnodeRegistry.TypeToNode(&voucher) // initiate a push data transfer. This will complete asynchronously and the // completion of the data transfer will trigger a change in deal state diff --git a/storagemarket/impl/dtutils/dtutils.go b/storagemarket/impl/dtutils/dtutils.go index 8e0dc947..e53a6a84 100644 --- a/storagemarket/impl/dtutils/dtutils.go +++ b/storagemarket/impl/dtutils/dtutils.go @@ -14,7 +14,6 @@ import ( datatransfer "github.com/filecoin-project/go-data-transfer/v2" "github.com/filecoin-project/go-statemachine/fsm" - "github.com/filecoin-project/go-fil-markets/bindnodeutils" "github.com/filecoin-project/go-fil-markets/storagemarket" "github.com/filecoin-project/go-fil-markets/storagemarket/impl/requestvalidation" ) @@ -44,7 +43,7 @@ func ProviderDataTransferSubscriber(deals EventReceiver) datatransfer.Subscriber channelState.ChannelID()) return } - voucherIface, err := bindnodeutils.TypeFromNode(node.Voucher, &requestvalidation.StorageDataTransferVoucher{}) + voucherIface, err := requestvalidation.BindnodeRegistry.TypeFromNode(node.Voucher, &requestvalidation.StorageDataTransferVoucher{}) // if this event is for a transfer not related to storage, ignore if err != nil { log.Debugw("ignoring data-transfer event as it's not storage related", "event", datatransfer.Events[event.Code], "channelID", @@ -107,7 +106,7 @@ func ClientDataTransferSubscriber(deals EventReceiver) datatransfer.Subscriber { channelState.ChannelID()) return } - voucherIface, err := bindnodeutils.TypeFromNode(node.Voucher, &requestvalidation.StorageDataTransferVoucher{}) + voucherIface, err := requestvalidation.BindnodeRegistry.TypeFromNode(node.Voucher, &requestvalidation.StorageDataTransferVoucher{}) // if this event is for a transfer not related to storage, ignore if err != nil { log.Debugw("ignoring data-transfer event as it's not storage related", "event", datatransfer.Events[event.Code], "channelID", @@ -171,7 +170,7 @@ func TransportConfigurer(storeGetter StoreGetter) datatransfer.TransportConfigur log.Errorf("attempting to configure data store, empty voucher") return } - voucherIface, err := bindnodeutils.TypeFromNode(voucher.Voucher, &requestvalidation.StorageDataTransferVoucher{}) + voucherIface, err := requestvalidation.BindnodeRegistry.TypeFromNode(voucher.Voucher, &requestvalidation.StorageDataTransferVoucher{}) // if this event is for a transfer not related to storage, ignore if err != nil { log.Errorf("attempting to configure data store, bad voucher: %s", err) diff --git a/storagemarket/impl/dtutils/dtutils_test.go b/storagemarket/impl/dtutils/dtutils_test.go index 839421ae..fa784725 100644 --- a/storagemarket/impl/dtutils/dtutils_test.go +++ b/storagemarket/impl/dtutils/dtutils_test.go @@ -17,7 +17,6 @@ import ( datatransfer "github.com/filecoin-project/go-data-transfer/v2" "github.com/filecoin-project/go-statemachine/fsm" - "github.com/filecoin-project/go-fil-markets/bindnodeutils" "github.com/filecoin-project/go-fil-markets/shared_testutil" "github.com/filecoin-project/go-fil-markets/storagemarket" "github.com/filecoin-project/go-fil-markets/storagemarket/impl/dtutils" @@ -28,7 +27,7 @@ func storageDataTransferVoucher(t *testing.T, proposalCid cid.Cid) datatransfer. sdtv := requestvalidation.StorageDataTransferVoucher{ Proposal: proposalCid, } - node := bindnodeutils.TypeToNode(&sdtv) + node := requestvalidation.BindnodeRegistry.TypeToNode(&sdtv) return datatransfer.TypedVoucher{Voucher: node, Type: requestvalidation.StorageDataTransferVoucherType} } diff --git a/storagemarket/impl/ipld_compat_test.go b/storagemarket/impl/ipld_compat_test.go index fcd99847..182f70b1 100644 --- a/storagemarket/impl/ipld_compat_test.go +++ b/storagemarket/impl/ipld_compat_test.go @@ -14,7 +14,6 @@ import ( "github.com/ipld/go-ipld-prime/schema" "github.com/stretchr/testify/assert" - "github.com/filecoin-project/go-fil-markets/bindnodeutils" "github.com/filecoin-project/go-fil-markets/storagemarket/impl/requestvalidation" ) @@ -36,7 +35,7 @@ func TestIpldCompat_StorageDataTransferVoucher(t *testing.T) { nb := basicnode.Prototype.Any.NewBuilder() dagcbor.Decode(nb, &originalBuf) node := nb.Build() - sdtvBindnodeIface, err := bindnodeutils.TypeFromNode(node, &requestvalidation.StorageDataTransferVoucher{}) + sdtvBindnodeIface, err := requestvalidation.BindnodeRegistry.TypeFromNode(node, &requestvalidation.StorageDataTransferVoucher{}) assert.Nil(t, err) sdtvBindnode, ok := sdtvBindnodeIface.(*requestvalidation.StorageDataTransferVoucher) assert.True(t, ok) @@ -45,7 +44,7 @@ func TestIpldCompat_StorageDataTransferVoucher(t *testing.T) { assert.Equal(t, sdtv.Proposal, sdtvBindnode.Proposal) // encode the new StorageDataTransferVoucher with bindnode to bytes - node = bindnodeutils.TypeToNode(sdtvBindnode) + node = requestvalidation.BindnodeRegistry.TypeToNode(sdtvBindnode) var bindnodeBuf bytes.Buffer dagcbor.Encode(node.(schema.TypedNode).Representation(), &bindnodeBuf) bindnodeBytes := bindnodeBuf.Bytes() diff --git a/storagemarket/impl/requestvalidation/common.go b/storagemarket/impl/requestvalidation/common.go index 2b168f44..63bfae28 100644 --- a/storagemarket/impl/requestvalidation/common.go +++ b/storagemarket/impl/requestvalidation/common.go @@ -6,7 +6,6 @@ import ( "github.com/libp2p/go-libp2p-core/peer" "golang.org/x/xerrors" - "github.com/filecoin-project/go-fil-markets/bindnodeutils" "github.com/filecoin-project/go-fil-markets/storagemarket" ) @@ -23,7 +22,7 @@ func ValidatePush( baseCid cid.Cid, Selector datamodel.Node) error { - dealVoucherIface, err := bindnodeutils.TypeFromNode(voucher, &StorageDataTransferVoucher{}) + dealVoucherIface, err := BindnodeRegistry.TypeFromNode(voucher, &StorageDataTransferVoucher{}) if err != nil { return xerrors.Errorf("could not decode StorageDataTransferVoucher: %w", err) } @@ -58,7 +57,7 @@ func ValidatePull( baseCid cid.Cid, Selector datamodel.Node) error { - dealVoucherIface, err := bindnodeutils.TypeFromNode(voucher, &StorageDataTransferVoucher{}) + dealVoucherIface, err := BindnodeRegistry.TypeFromNode(voucher, &StorageDataTransferVoucher{}) if err != nil { return xerrors.Errorf("could not decode StorageDataTransferVoucher: %w", err) } diff --git a/storagemarket/impl/requestvalidation/request_validation_test.go b/storagemarket/impl/requestvalidation/request_validation_test.go index 387c026c..42d3bd51 100644 --- a/storagemarket/impl/requestvalidation/request_validation_test.go +++ b/storagemarket/impl/requestvalidation/request_validation_test.go @@ -21,9 +21,9 @@ import ( "github.com/filecoin-project/go-statestore" "github.com/filecoin-project/specs-actors/v8/actors/builtin/market" - "github.com/filecoin-project/go-fil-markets/bindnodeutils" tut "github.com/filecoin-project/go-fil-markets/shared_testutil" "github.com/filecoin-project/go-fil-markets/storagemarket" + "github.com/filecoin-project/go-fil-markets/storagemarket/impl/requestvalidation" rv "github.com/filecoin-project/go-fil-markets/storagemarket/impl/requestvalidation" ) @@ -168,7 +168,7 @@ func AssertPushValidator(t *testing.T, validator datatransfer.RequestValidator, t.Fatal("error serializing proposal") } sdtv := rv.StorageDataTransferVoucher{proposalNd.Cid()} - voucher := bindnodeutils.TypeToNode(&sdtv) + voucher := requestvalidation.BindnodeRegistry.TypeToNode(&sdtv) checkValidateAndRevalidatePush(t, validator, datatransfer.ChannelID{}, sender, datatransfer.TypedVoucher{Voucher: voucher, Type: rv.StorageDataTransferVoucherType}, proposal.Proposal.PieceCID, nil, func(t *testing.T, result datatransfer.ValidationResult, err error) { if err != nil { @@ -188,7 +188,7 @@ func AssertPushValidator(t *testing.T, validator datatransfer.RequestValidator, t.Fatal("deal tracking failed") } sdtv := rv.StorageDataTransferVoucher{minerDeal.ProposalCid} - voucher := bindnodeutils.TypeToNode(&sdtv) + voucher := requestvalidation.BindnodeRegistry.TypeToNode(&sdtv) checkValidateAndRevalidatePush(t, validator, datatransfer.ChannelID{}, sender, datatransfer.TypedVoucher{Voucher: voucher, Type: rv.StorageDataTransferVoucherType}, blockGenerator.Next().Cid(), nil, func(t *testing.T, result datatransfer.ValidationResult, err error) { if err != nil { @@ -209,7 +209,7 @@ func AssertPushValidator(t *testing.T, validator datatransfer.RequestValidator, } ref := minerDeal.Ref sdtv := rv.StorageDataTransferVoucher{minerDeal.ProposalCid} - voucher := bindnodeutils.TypeToNode(&sdtv) + voucher := requestvalidation.BindnodeRegistry.TypeToNode(&sdtv) checkValidateAndRevalidatePush(t, validator, datatransfer.ChannelID{}, sender, datatransfer.TypedVoucher{Voucher: voucher, Type: rv.StorageDataTransferVoucherType}, ref.Root, nil, func(t *testing.T, result datatransfer.ValidationResult, err error) { if err != nil { @@ -230,7 +230,7 @@ func AssertPushValidator(t *testing.T, validator datatransfer.RequestValidator, } ref := minerDeal.Ref sdtv := rv.StorageDataTransferVoucher{minerDeal.ProposalCid} - voucher := bindnodeutils.TypeToNode(&sdtv) + voucher := requestvalidation.BindnodeRegistry.TypeToNode(&sdtv) checkValidateAndRevalidatePush(t, validator, datatransfer.ChannelID{}, sender, datatransfer.TypedVoucher{Voucher: voucher, Type: rv.StorageDataTransferVoucherType}, ref.Root, nil, func(t *testing.T, result datatransfer.ValidationResult, err error) { if err != nil { @@ -254,7 +254,7 @@ func AssertValidatesPulls(t *testing.T, validator datatransfer.RequestValidator, t.Fatal("error serializing proposal") } sdtv := rv.StorageDataTransferVoucher{proposalNd.Cid()} - voucher := bindnodeutils.TypeToNode(&sdtv) + voucher := requestvalidation.BindnodeRegistry.TypeToNode(&sdtv) checkValidateAndRevalidatePull(t, validator, datatransfer.ChannelID{}, receiver, datatransfer.TypedVoucher{Voucher: voucher, Type: rv.StorageDataTransferVoucherType}, proposal.Proposal.PieceCID, nil, func(t *testing.T, result datatransfer.ValidationResult, err error) { if err != nil { @@ -274,7 +274,7 @@ func AssertValidatesPulls(t *testing.T, validator datatransfer.RequestValidator, t.Fatal("deal tracking failed") } sdtv := rv.StorageDataTransferVoucher{clientDeal.ProposalCid} - voucher := bindnodeutils.TypeToNode(&sdtv) + voucher := requestvalidation.BindnodeRegistry.TypeToNode(&sdtv) checkValidateAndRevalidatePull(t, validator, datatransfer.ChannelID{}, receiver, datatransfer.TypedVoucher{Voucher: voucher, Type: rv.StorageDataTransferVoucherType}, blockGenerator.Next().Cid(), nil, func(t *testing.T, result datatransfer.ValidationResult, err error) { if err != nil { @@ -295,7 +295,7 @@ func AssertValidatesPulls(t *testing.T, validator datatransfer.RequestValidator, } payloadCid := clientDeal.DataRef.Root sdtv := rv.StorageDataTransferVoucher{clientDeal.ProposalCid} - voucher := bindnodeutils.TypeToNode(&sdtv) + voucher := requestvalidation.BindnodeRegistry.TypeToNode(&sdtv) checkValidateAndRevalidatePull(t, validator, datatransfer.ChannelID{}, receiver, datatransfer.TypedVoucher{Voucher: voucher, Type: rv.StorageDataTransferVoucherType}, payloadCid, nil, func(t *testing.T, result datatransfer.ValidationResult, err error) { if err != nil { @@ -316,7 +316,7 @@ func AssertValidatesPulls(t *testing.T, validator datatransfer.RequestValidator, } payloadCid := clientDeal.DataRef.Root sdtv := rv.StorageDataTransferVoucher{clientDeal.ProposalCid} - voucher := bindnodeutils.TypeToNode(&sdtv) + voucher := requestvalidation.BindnodeRegistry.TypeToNode(&sdtv) checkValidateAndRevalidatePull(t, validator, datatransfer.ChannelID{}, receiver, datatransfer.TypedVoucher{Voucher: voucher, Type: rv.StorageDataTransferVoucherType}, payloadCid, nil, func(t *testing.T, result datatransfer.ValidationResult, err error) { if err != nil { diff --git a/storagemarket/impl/requestvalidation/types.go b/storagemarket/impl/requestvalidation/types.go index 9cbb95b2..ed62f458 100644 --- a/storagemarket/impl/requestvalidation/types.go +++ b/storagemarket/impl/requestvalidation/types.go @@ -4,10 +4,10 @@ import ( "errors" "github.com/ipfs/go-cid" + bindnoderegistry "github.com/ipld/go-ipld-prime/node/bindnode/registry" datatransfer "github.com/filecoin-project/go-data-transfer/v2" - "github.com/filecoin-project/go-fil-markets/bindnodeutils" "github.com/filecoin-project/go-fil-markets/storagemarket" ) @@ -60,6 +60,8 @@ const StorageDataTransferVoucherSchema = ` } representation tuple ` +var BindnodeRegistry = bindnoderegistry.NewRegistry() + func init() { - bindnodeutils.RegisterType((*StorageDataTransferVoucher)(nil), StorageDataTransferVoucherSchema, "StorageDataTransferVoucher") + BindnodeRegistry.RegisterType((*StorageDataTransferVoucher)(nil), StorageDataTransferVoucherSchema, "StorageDataTransferVoucher") }