-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a wrapper blockstore to handle Cidv0v1 lookups.
License: MIT Signed-off-by: Kevin Atkinson <k@kevina.org>
- Loading branch information
Showing
4 changed files
with
78 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package cidv0v1 | ||
|
||
import ( | ||
blocks "gx/ipfs/QmVzK524a2VWLqyvtBeiHKsUAWYgeAk4DBeZoY7vpNPNRx/go-block-format" | ||
cid "gx/ipfs/QmYVNvtQkeZ6AKSwDrjQTs432QtL6umrrK41EBq3cu7iSP/go-cid" | ||
bs "gx/ipfs/QmadMhXJLHMFjpRmh85XjpmVDkEtQpNYEZNRpWRvYVLrvb/go-ipfs-blockstore" | ||
) | ||
|
||
type blockstore struct { | ||
bs.Blockstore | ||
} | ||
|
||
func NewBlockstore(b bs.Blockstore) bs.Blockstore { | ||
return &blockstore{b} | ||
} | ||
|
||
func (b *blockstore) Has(c *cid.Cid) (bool, error) { | ||
have, err := b.Blockstore.Has(c) | ||
if have || err != nil { | ||
return have, err | ||
} | ||
c1 := tryOtherCidVersion(c) | ||
if c1 == nil { | ||
return false, nil | ||
} | ||
return b.Blockstore.Has(c1) | ||
} | ||
|
||
func (b *blockstore) Get(c *cid.Cid) (blocks.Block, error) { | ||
block, err := b.Blockstore.Get(c) | ||
if err == nil { | ||
return block, nil | ||
} | ||
if err != bs.ErrNotFound { | ||
return nil, err | ||
} | ||
c1 := tryOtherCidVersion(c) | ||
if c1 == nil { | ||
return nil, bs.ErrNotFound | ||
} | ||
block, err = b.Blockstore.Get(c1) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// modify block so it has the original CID | ||
block, err = blocks.NewBlockWithCid(block.RawData(), c) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// insert the block with the original CID to avoid problems | ||
// with pinning | ||
err = b.Blockstore.Put(block) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return block, nil | ||
} | ||
|
||
func tryOtherCidVersion(c *cid.Cid) *cid.Cid { | ||
prefix := c.Prefix() | ||
if prefix.Codec != cid.DagProtobuf { | ||
return nil | ||
} | ||
var c1 *cid.Cid | ||
if prefix.Version == 0 { | ||
c1 = cid.NewCidV1(cid.DagProtobuf, c.Hash()) | ||
} else { | ||
c1 = cid.NewCidV0(c.Hash()) | ||
} | ||
return c1 | ||
} |