Skip to content

Commit

Permalink
minor improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
Wondertan committed Nov 14, 2024
1 parent 8c953dc commit 8fc2000
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
30 changes: 20 additions & 10 deletions share/shwap/p2p/bitswap/block_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (

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

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

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

return spec.maxSize, nil
Expand All @@ -38,24 +38,34 @@ func registerBlock(mhcode, codec uint64, maxSize, idSize int, bldrFn func(cid.Ci
mh.Register(mhcode, func() hash.Hash {
return &hasher{IDSize: idSize}
})
specRegistry[mhcode] = blockSpec{
specRegistry[codec] = blockSpec{
idSize: idSize,
maxSize: maxSize,
codec: codec,
mhCode: mhcode,
builder: bldrFn,
}
}

// getSpec returns the blockSpec for the given CID.
func getSpec(cid cid.Cid) (blockSpec, error) {
spec, ok := specRegistry[cid.Type()]
if !ok {
return blockSpec{}, fmt.Errorf("unsupported codec %d", cid.Type())
}

return spec, nil
}

// blockSpec holds constant metadata about particular Block types.
type blockSpec struct {
idSize int
maxSize int
codec uint64
mhCode uint64
builder func(cid.Cid) (Block, error)
}

func (spec *blockSpec) String() string {
return fmt.Sprintf("BlockSpec{IDSize: %d, Codec: %d}", spec.idSize, spec.codec)
return fmt.Sprintf("BlockSpec{IDSize: %d, MHCode: %d}", spec.idSize, spec.mhCode)
}

var specRegistry = make(map[uint64]blockSpec)
16 changes: 9 additions & 7 deletions share/shwap/p2p/bitswap/cid.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,18 @@ func encodeToCID(bm encoding.BinaryMarshaler, mhcode, codec uint64) cid.Cid {

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

if prefix.Codec != spec.codec {
return fmt.Errorf("invalid CID codec %d", prefix.Codec)
prefix := cid.Prefix()
if prefix.Version != 1 {
return fmt.Errorf("invalid cid version %d", prefix.Version)
}
if prefix.MhType != spec.mhCode {
return fmt.Errorf("invalid multihash type %d", prefix.MhType)
}

if prefix.MhLength != spec.idSize {
return fmt.Errorf("invalid multihash length %d", prefix.MhLength)
}
Expand Down

0 comments on commit 8fc2000

Please sign in to comment.