Skip to content

Commit

Permalink
Create a wrapper blockstore to handle Cidv0v1 lookups.
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Kevin Atkinson <k@kevina.org>
  • Loading branch information
kevina committed Jul 29, 2018
1 parent 56a3baf commit 79410ac
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 29 deletions.
29 changes: 2 additions & 27 deletions blockservice/blockservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func getBlock(ctx context.Context, c *cid.Cid, bs blockstore.Blockstore, f excha
return nil, err
}

block, err := getBlockFromStore(c, bs)
block, err := bs.Get(c)
if err == nil {
return block, nil
}
Expand Down Expand Up @@ -242,31 +242,6 @@ func getBlock(ctx context.Context, c *cid.Cid, bs blockstore.Blockstore, f excha
return nil, err
}

func getBlockFromStore(c *cid.Cid, bs blockstore.Blockstore) (blocks.Block, error) {
block, err0 := bs.Get(c)
if err0 == nil {
return block, nil
}
if err0 != blockstore.ErrNotFound {
return nil, err0
}
prefix := c.Prefix()
if prefix.Codec != cid.DagProtobuf {
return nil, err0
}
var c1 *cid.Cid
if prefix.Version == 0 {
c1 = cid.NewCidV1(cid.DagProtobuf, c.Hash())
} else {
c1 = cid.NewCidV0(c.Hash())
}
block, err1 := bs.Get(c1)
if err1 != nil {
return nil, err0
}
return blocks.NewBlockWithCid(block.RawData(), c1)
}

// GetBlocks gets a list of blocks asynchronously and returns through
// the returned channel.
// NB: No guarantees are made about order.
Expand Down Expand Up @@ -294,7 +269,7 @@ func getBlocks(ctx context.Context, ks []*cid.Cid, bs blockstore.Blockstore, f e

var misses []*cid.Cid
for _, c := range ks {
hit, err := getBlockFromStore(c, bs)
hit, err := bs.Get(c)
if err != nil {
misses = append(misses, c)
continue
Expand Down
3 changes: 3 additions & 0 deletions core/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
pin "github.com/ipfs/go-ipfs/pin"
repo "github.com/ipfs/go-ipfs/repo"
cfg "github.com/ipfs/go-ipfs/repo/config"
cidv0v1 "github.com/ipfs/go-ipfs/thirdparty/cidv0v1"
"github.com/ipfs/go-ipfs/thirdparty/verifbs"
uio "github.com/ipfs/go-ipfs/unixfs/io"

Expand Down Expand Up @@ -211,6 +212,8 @@ func setupNode(ctx context.Context, n *IpfsNode, cfg *BuildCfg) error {

wbs = bstore.NewIdStore(wbs)

wbs = cidv0v1.NewBlockstore(wbs)

n.BaseBlocks = wbs
n.GCLocker = bstore.NewGCLocker()
n.Blockstore = bstore.NewGCBlockstore(wbs, n.GCLocker)
Expand Down
4 changes: 2 additions & 2 deletions test/sharness/t0276-cidv0v1.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ test_expect_success "add afile using CIDv0 to node 0" '
iptb run 0 ipfs add -q --cid-version=0 afile
'

test_expect_failure "get afile using CIDv1 via node 1" '
test_expect_success "get afile using CIDv1 via node 1" '
iptb run 1 ipfs --timeout=2s cat $AHASHv1 > thefile &&
test_cmp afile thefile
'
Expand All @@ -116,7 +116,7 @@ test_expect_success "add bfile using CIDv1 to node 0" '
BHASHv1=$(iptb run 0 ipfs add -q --cid-version=1 --raw-leaves=false bfile)
'

test_expect_failure "get bfile using CIDv0 via node 1" '
test_expect_success "get bfile using CIDv0 via node 1" '
BHASHv0=$(cid-fmt -v 0 %s $BHASHv1)
iptb run 1 ipfs --timeout=2s cat $BHASHv0 > thefile &&
test_cmp bfile thefile
Expand Down
71 changes: 71 additions & 0 deletions thirdparty/cidv0v1/blockstore.go
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
}

0 comments on commit 79410ac

Please sign in to comment.