-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use concrete multicodec codes as indexing metadata protocol ID
Refactor metadata package to: 1) use concrete multicodec codes defined in the lookup table, and 2) simplify the datatransfer metadata implementation since dynamic protocol ID range no longer makes sense when using concrete multicodec codes. Relates to: - multiformats/multicodec#260
- Loading branch information
Showing
14 changed files
with
168 additions
and
291 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
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
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 |
---|---|---|
@@ -1,8 +1,9 @@ | ||
// Package metadata captures the metadata types known by the index-provider. | ||
// | ||
// The metadata is used to provide information about how to retrieve data blocks associated to | ||
// multihashes from a provider. | ||
// It is represented as an array of bytes in the indexer protocol, starting with a varint ProtocolID | ||
// that defines how to decode the remaining bytes. | ||
// multihashes advertised by a provider. It is represented as an array of bytes in the indexer | ||
// protocol, starting with a varint ProtocolID that defines how to decode the remaining bytes. | ||
// | ||
// Two metadata types are currently represented here: BitswapMetadata and DataTransferMetadata. | ||
// Two metadata types are currently represented here: BitswapMetadata and | ||
// GraphsyncFilecoinV1Metadata. | ||
package metadata |
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,87 @@ | ||
package metadata | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
|
||
stiapi "github.com/filecoin-project/storetheindex/api/v0" | ||
"github.com/ipfs/go-cid" | ||
"github.com/ipld/go-ipld-prime/codec/dagcbor" | ||
"github.com/ipld/go-ipld-prime/node/bindnode" | ||
"github.com/ipld/go-ipld-prime/schema" | ||
"github.com/multiformats/go-multicodec" | ||
) | ||
|
||
// GraphsyncFilecoinV1Metadata represents the indexing metadata for multicodec.TransportGraphsyncFilecoinv1. | ||
type GraphsyncFilecoinV1Metadata struct { | ||
// PieceCID identifies the piece this data can be found in | ||
PieceCID cid.Cid | ||
// VerifiedDeal indicates if the deal is verified | ||
VerifiedDeal bool | ||
// FastRetrieval indicates whether the provider claims there is an unsealed copy | ||
FastRetrieval bool | ||
} | ||
|
||
var graphSyncfilecoinV1SchemaType schema.Type | ||
|
||
func init() { | ||
ts := schema.TypeSystem{} | ||
ts.Init() | ||
ts.Accumulate(schema.SpawnLink("Link")) | ||
ts.Accumulate(schema.SpawnBool("Bool")) | ||
ts.Accumulate(schema.SpawnStruct("GraphsyncFilecoinV1Metadata", | ||
[]schema.StructField{ | ||
schema.SpawnStructField("PieceCID", "Link", false, false), | ||
schema.SpawnStructField("VerifiedDeal", "Bool", false, false), | ||
schema.SpawnStructField("FastRetrieval", "Bool", false, false), | ||
}, | ||
schema.SpawnStructRepresentationMap(nil), | ||
)) | ||
graphSyncfilecoinV1SchemaType = ts.TypeByName("GraphsyncFilecoinV1Metadata") | ||
} | ||
|
||
// ToIndexerMetadata converts GraphsyncFilecoinV1Metadata into indexer Metadata format. | ||
func (dtm *GraphsyncFilecoinV1Metadata) ToIndexerMetadata() (stiapi.Metadata, error) { | ||
nd := bindnode.Wrap(dtm, graphSyncfilecoinV1SchemaType) | ||
buf := new(bytes.Buffer) | ||
err := dagcbor.Encode(nd, buf) | ||
if err != nil { | ||
return stiapi.Metadata{}, err | ||
} | ||
return stiapi.Metadata{ | ||
ProtocolID: multicodec.TransportGraphsyncFilecoinv1, | ||
Data: buf.Bytes(), | ||
}, nil | ||
} | ||
|
||
// FromIndexerMetadata decodes the indexer metadata format into GraphsyncFilecoinV1Metadata | ||
// if the protocol ID of the given metadata matches multicodec.TransportGraphsyncFilecoinv1. | ||
// Otherwise, ErrNotGraphsyncFilecoinV1 error is returned. | ||
func (dtm *GraphsyncFilecoinV1Metadata) FromIndexerMetadata(m stiapi.Metadata) error { | ||
if m.ProtocolID != multicodec.TransportGraphsyncFilecoinv1 { | ||
return ErrNotGraphsyncFilecoinV1{m.ProtocolID} | ||
} | ||
r := bytes.NewBuffer(m.Data) | ||
proto := bindnode.Prototype((*GraphsyncFilecoinV1Metadata)(nil), graphSyncfilecoinV1SchemaType) | ||
nb := proto.NewBuilder() | ||
err := dagcbor.Decode(nb, r) | ||
if err != nil { | ||
return err | ||
} | ||
nd := nb.Build() | ||
gm := bindnode.Unwrap(nd).(*GraphsyncFilecoinV1Metadata) | ||
dtm.VerifiedDeal = gm.VerifiedDeal | ||
dtm.FastRetrieval = gm.FastRetrieval | ||
dtm.PieceCID = gm.PieceCID | ||
return nil | ||
} | ||
|
||
// ErrNotGraphsyncFilecoinV1 indicates a protocol does not use graphsync filecoin v1 transport. | ||
// See: multicodec.TransportGraphsyncFilecoinv1. | ||
type ErrNotGraphsyncFilecoinV1 struct { | ||
incorrectProtocol multicodec.Code | ||
} | ||
|
||
func (e ErrNotGraphsyncFilecoinV1) Error() string { | ||
return fmt.Sprintf("protocol 0x%X does not use the FilecoinV1 exchange format", uint64(e.incorrectProtocol)) | ||
} |
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,57 @@ | ||
package metadata_test | ||
|
||
import ( | ||
stiapi "github.com/filecoin-project/storetheindex/api/v0" | ||
"testing" | ||
|
||
"github.com/filecoin-project/index-provider/metadata" | ||
"github.com/filecoin-project/index-provider/testutil" | ||
"github.com/multiformats/go-multicodec" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestRoundTripDataTransferFilecoin(t *testing.T) { | ||
cids := testutil.GenerateCids(4) | ||
filecoinV1Datas := []*metadata.GraphsyncFilecoinV1Metadata{ | ||
{ | ||
PieceCID: cids[0], | ||
VerifiedDeal: false, | ||
FastRetrieval: false, | ||
}, | ||
{ | ||
PieceCID: cids[1], | ||
VerifiedDeal: false, | ||
FastRetrieval: true, | ||
}, | ||
{ | ||
PieceCID: cids[2], | ||
VerifiedDeal: true, | ||
FastRetrieval: true, | ||
}, | ||
{ | ||
PieceCID: cids[3], | ||
VerifiedDeal: true, | ||
FastRetrieval: true, | ||
}, | ||
} | ||
for _, src := range filecoinV1Datas { | ||
imd, err := src.ToIndexerMetadata() | ||
require.NoError(t, err) | ||
require.Equal(t, multicodec.TransportGraphsyncFilecoinv1, imd.ProtocolID) | ||
|
||
dst := &metadata.GraphsyncFilecoinV1Metadata{} | ||
err = dst.FromIndexerMetadata(imd) | ||
require.NoError(t, err) | ||
require.Equal(t, src, dst) | ||
} | ||
} | ||
|
||
func TestGraphsyncFilecoinV1Metadata_FromIndexerMetadataErr(t *testing.T) { | ||
imd := stiapi.Metadata{ | ||
ProtocolID: multicodec.TransportBitswap, | ||
} | ||
dst := &metadata.GraphsyncFilecoinV1Metadata{} | ||
err := dst.FromIndexerMetadata(imd) | ||
require.IsType(t, metadata.ErrNotGraphsyncFilecoinV1{}, err) | ||
require.Equal(t, "protocol 0x900 does not use the FilecoinV1 exchange format", err.Error()) | ||
} |
Oops, something went wrong.