Skip to content

Commit 8fc2000

Browse files
committed
minor improvement
1 parent 8c953dc commit 8fc2000

File tree

2 files changed

+29
-17
lines changed

2 files changed

+29
-17
lines changed

share/shwap/p2p/bitswap/block_registry.go

+20-10
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import (
1010

1111
// EmptyBlock constructs an empty Block with type in the given CID.
1212
func EmptyBlock(cid cid.Cid) (Block, error) {
13-
spec, ok := specRegistry[cid.Prefix().MhType]
14-
if !ok {
15-
return nil, fmt.Errorf("unsupported Block type: %v", cid.Prefix().MhType)
13+
spec, err := getSpec(cid)
14+
if err != nil {
15+
return nil, err
1616
}
1717

1818
blk, err := spec.builder(cid)
@@ -25,9 +25,9 @@ func EmptyBlock(cid cid.Cid) (Block, error) {
2525

2626
// maxBlockSize returns the maximum size of the Block type in the given CID.
2727
func maxBlockSize(cid cid.Cid) (int, error) {
28-
spec, ok := specRegistry[cid.Prefix().MhType]
29-
if !ok {
30-
return -1, fmt.Errorf("unsupported Block type: %v", cid.Prefix().MhType)
28+
spec, err := getSpec(cid)
29+
if err != nil {
30+
return 0, err
3131
}
3232

3333
return spec.maxSize, nil
@@ -38,24 +38,34 @@ func registerBlock(mhcode, codec uint64, maxSize, idSize int, bldrFn func(cid.Ci
3838
mh.Register(mhcode, func() hash.Hash {
3939
return &hasher{IDSize: idSize}
4040
})
41-
specRegistry[mhcode] = blockSpec{
41+
specRegistry[codec] = blockSpec{
4242
idSize: idSize,
4343
maxSize: maxSize,
44-
codec: codec,
44+
mhCode: mhcode,
4545
builder: bldrFn,
4646
}
4747
}
4848

49+
// getSpec returns the blockSpec for the given CID.
50+
func getSpec(cid cid.Cid) (blockSpec, error) {
51+
spec, ok := specRegistry[cid.Type()]
52+
if !ok {
53+
return blockSpec{}, fmt.Errorf("unsupported codec %d", cid.Type())
54+
}
55+
56+
return spec, nil
57+
}
58+
4959
// blockSpec holds constant metadata about particular Block types.
5060
type blockSpec struct {
5161
idSize int
5262
maxSize int
53-
codec uint64
63+
mhCode uint64
5464
builder func(cid.Cid) (Block, error)
5565
}
5666

5767
func (spec *blockSpec) String() string {
58-
return fmt.Sprintf("BlockSpec{IDSize: %d, Codec: %d}", spec.idSize, spec.codec)
68+
return fmt.Sprintf("BlockSpec{IDSize: %d, MHCode: %d}", spec.idSize, spec.mhCode)
5969
}
6070

6171
var specRegistry = make(map[uint64]blockSpec)

share/shwap/p2p/bitswap/cid.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,18 @@ func encodeToCID(bm encoding.BinaryMarshaler, mhcode, codec uint64) cid.Cid {
3535

3636
// validateCID checks correctness of the CID.
3737
func validateCID(cid cid.Cid) error {
38-
prefix := cid.Prefix()
39-
spec, ok := specRegistry[prefix.MhType]
40-
if !ok {
41-
return fmt.Errorf("unsupported multihash type %d", prefix.MhType)
38+
spec, err := getSpec(cid)
39+
if err != nil {
40+
return err
4241
}
4342

44-
if prefix.Codec != spec.codec {
45-
return fmt.Errorf("invalid CID codec %d", prefix.Codec)
43+
prefix := cid.Prefix()
44+
if prefix.Version != 1 {
45+
return fmt.Errorf("invalid cid version %d", prefix.Version)
46+
}
47+
if prefix.MhType != spec.mhCode {
48+
return fmt.Errorf("invalid multihash type %d", prefix.MhType)
4649
}
47-
4850
if prefix.MhLength != spec.idSize {
4951
return fmt.Errorf("invalid multihash length %d", prefix.MhLength)
5052
}

0 commit comments

Comments
 (0)