Skip to content

Commit

Permalink
feat(clientutils): encode cid directly (#398)
Browse files Browse the repository at this point in the history
encode CIDs directly in the label field in either Base58 (V0) or Base64 (V1)
  • Loading branch information
hannahhoward authored Sep 9, 2020
1 parent d155db9 commit 5eee772
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 37 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ require (
github.com/libp2p/go-libp2p v0.10.0
github.com/libp2p/go-libp2p-core v0.6.0
github.com/multiformats/go-multiaddr v0.2.2
github.com/multiformats/go-multibase v0.0.3
github.com/prometheus/common v0.0.0-20180801064454-c7de2306084e
github.com/stretchr/testify v1.6.1
github.com/whyrusleeping/cbor-gen v0.0.0-20200814224545-656e08ce49ee
Expand Down
2 changes: 1 addition & 1 deletion storagemarket/impl/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ func (c *Client) ProposeStorageDeal(ctx context.Context, params storagemarket.Pr
PieceSize: pieceSize.Padded(),
Client: params.Addr,
Provider: params.Info.Address,
Label: string(label),
Label: label,
StartEpoch: params.StartEpoch,
EndEpoch: params.EndEpoch,
StoragePricePerEpoch: params.Price,
Expand Down
29 changes: 7 additions & 22 deletions storagemarket/impl/clientutils/clientutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@
package clientutils

import (
"bytes"
"context"

"github.com/ipfs/go-cid"
"github.com/ipld/go-ipld-prime/codec/dagcbor"
"github.com/ipld/go-ipld-prime/fluent"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
basicnode "github.com/ipld/go-ipld-prime/node/basic"
"github.com/multiformats/go-multibase"
"golang.org/x/xerrors"

"github.com/filecoin-project/go-address"
Expand Down Expand Up @@ -64,23 +60,12 @@ func VerifyResponse(ctx context.Context, resp network.SignedResponse, minerAddr
return nil
}

// LabelField makes a label field for a deal proposal as a CBOR encoded struct
// with the following structure:
// LabelField makes a label field for a deal proposal as a multibase encoding
// of the payload CID (B58BTC for V0, B64 for V1)
//
// {
// "pcids": [payloadCID]
// }
//
func LabelField(payloadCID cid.Cid) ([]byte, error) {
nd := fluent.MustBuildMap(basicnode.Style.Any, 1, func(ma fluent.MapAssembler) {
ma.AssembleEntry("pcids").CreateList(1, func(la fluent.ListAssembler) {
la.AssembleValue().AssignLink(cidlink.Link{Cid: payloadCID})
})
})
buf := new(bytes.Buffer)
err := dagcbor.Encoder(nd, buf)
if err != nil {
return nil, err
func LabelField(payloadCID cid.Cid) (string, error) {
if payloadCID.Version() == 0 {
return payloadCID.StringOfBase(multibase.Base58BTC)
}
return buf.Bytes(), nil
return payloadCID.StringOfBase(multibase.Base64)
}
15 changes: 1 addition & 14 deletions storagemarket/impl/clientutils/clientutils_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package clientutils_test

import (
"bytes"
"context"
"errors"
"fmt"
Expand All @@ -11,9 +10,6 @@ import (

"github.com/ipfs/go-cid"
"github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/codec/dagcbor"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
basicnode "github.com/ipld/go-ipld-prime/node/basic"
"github.com/stretchr/testify/require"

"github.com/filecoin-project/go-address"
Expand Down Expand Up @@ -153,16 +149,7 @@ func TestLabelField(t *testing.T) {
payloadCID := shared_testutil.GenerateCids(1)[0]
label, err := clientutils.LabelField(payloadCID)
require.NoError(t, err)
nb := basicnode.Style.Any.NewBuilder()
err = dagcbor.Decoder(nb, bytes.NewReader(label))
resultCid, err := cid.Decode(label)
require.NoError(t, err)
nd := nb.Build()
pcidsNd, err := nd.LookupString("pcids")
require.NoError(t, err)
linkNd, err := pcidsNd.LookupIndex(0)
require.NoError(t, err)
link, err := linkNd.AsLink()
require.NoError(t, err)
resultCid := link.(cidlink.Link).Cid
require.True(t, payloadCID.Equals(resultCid))
}

0 comments on commit 5eee772

Please sign in to comment.