Skip to content

Commit 79410ac

Browse files
committed
Create a wrapper blockstore to handle Cidv0v1 lookups.
License: MIT Signed-off-by: Kevin Atkinson <[email protected]>
1 parent 56a3baf commit 79410ac

File tree

4 files changed

+78
-29
lines changed

4 files changed

+78
-29
lines changed

blockservice/blockservice.go

+2-27
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ func getBlock(ctx context.Context, c *cid.Cid, bs blockstore.Blockstore, f excha
214214
return nil, err
215215
}
216216

217-
block, err := getBlockFromStore(c, bs)
217+
block, err := bs.Get(c)
218218
if err == nil {
219219
return block, nil
220220
}
@@ -242,31 +242,6 @@ func getBlock(ctx context.Context, c *cid.Cid, bs blockstore.Blockstore, f excha
242242
return nil, err
243243
}
244244

245-
func getBlockFromStore(c *cid.Cid, bs blockstore.Blockstore) (blocks.Block, error) {
246-
block, err0 := bs.Get(c)
247-
if err0 == nil {
248-
return block, nil
249-
}
250-
if err0 != blockstore.ErrNotFound {
251-
return nil, err0
252-
}
253-
prefix := c.Prefix()
254-
if prefix.Codec != cid.DagProtobuf {
255-
return nil, err0
256-
}
257-
var c1 *cid.Cid
258-
if prefix.Version == 0 {
259-
c1 = cid.NewCidV1(cid.DagProtobuf, c.Hash())
260-
} else {
261-
c1 = cid.NewCidV0(c.Hash())
262-
}
263-
block, err1 := bs.Get(c1)
264-
if err1 != nil {
265-
return nil, err0
266-
}
267-
return blocks.NewBlockWithCid(block.RawData(), c1)
268-
}
269-
270245
// GetBlocks gets a list of blocks asynchronously and returns through
271246
// the returned channel.
272247
// NB: No guarantees are made about order.
@@ -294,7 +269,7 @@ func getBlocks(ctx context.Context, ks []*cid.Cid, bs blockstore.Blockstore, f e
294269

295270
var misses []*cid.Cid
296271
for _, c := range ks {
297-
hit, err := getBlockFromStore(c, bs)
272+
hit, err := bs.Get(c)
298273
if err != nil {
299274
misses = append(misses, c)
300275
continue

core/builder.go

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
pin "github.com/ipfs/go-ipfs/pin"
1717
repo "github.com/ipfs/go-ipfs/repo"
1818
cfg "github.com/ipfs/go-ipfs/repo/config"
19+
cidv0v1 "github.com/ipfs/go-ipfs/thirdparty/cidv0v1"
1920
"github.com/ipfs/go-ipfs/thirdparty/verifbs"
2021
uio "github.com/ipfs/go-ipfs/unixfs/io"
2122

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

212213
wbs = bstore.NewIdStore(wbs)
213214

215+
wbs = cidv0v1.NewBlockstore(wbs)
216+
214217
n.BaseBlocks = wbs
215218
n.GCLocker = bstore.NewGCLocker()
216219
n.Blockstore = bstore.NewGCBlockstore(wbs, n.GCLocker)

test/sharness/t0276-cidv0v1.sh

100644100755
+2-2
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ test_expect_success "add afile using CIDv0 to node 0" '
107107
iptb run 0 ipfs add -q --cid-version=0 afile
108108
'
109109

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

119-
test_expect_failure "get bfile using CIDv0 via node 1" '
119+
test_expect_success "get bfile using CIDv0 via node 1" '
120120
BHASHv0=$(cid-fmt -v 0 %s $BHASHv1)
121121
iptb run 1 ipfs --timeout=2s cat $BHASHv0 > thefile &&
122122
test_cmp bfile thefile

thirdparty/cidv0v1/blockstore.go

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package cidv0v1
2+
3+
import (
4+
blocks "gx/ipfs/QmVzK524a2VWLqyvtBeiHKsUAWYgeAk4DBeZoY7vpNPNRx/go-block-format"
5+
cid "gx/ipfs/QmYVNvtQkeZ6AKSwDrjQTs432QtL6umrrK41EBq3cu7iSP/go-cid"
6+
bs "gx/ipfs/QmadMhXJLHMFjpRmh85XjpmVDkEtQpNYEZNRpWRvYVLrvb/go-ipfs-blockstore"
7+
)
8+
9+
type blockstore struct {
10+
bs.Blockstore
11+
}
12+
13+
func NewBlockstore(b bs.Blockstore) bs.Blockstore {
14+
return &blockstore{b}
15+
}
16+
17+
func (b *blockstore) Has(c *cid.Cid) (bool, error) {
18+
have, err := b.Blockstore.Has(c)
19+
if have || err != nil {
20+
return have, err
21+
}
22+
c1 := tryOtherCidVersion(c)
23+
if c1 == nil {
24+
return false, nil
25+
}
26+
return b.Blockstore.Has(c1)
27+
}
28+
29+
func (b *blockstore) Get(c *cid.Cid) (blocks.Block, error) {
30+
block, err := b.Blockstore.Get(c)
31+
if err == nil {
32+
return block, nil
33+
}
34+
if err != bs.ErrNotFound {
35+
return nil, err
36+
}
37+
c1 := tryOtherCidVersion(c)
38+
if c1 == nil {
39+
return nil, bs.ErrNotFound
40+
}
41+
block, err = b.Blockstore.Get(c1)
42+
if err != nil {
43+
return nil, err
44+
}
45+
// modify block so it has the original CID
46+
block, err = blocks.NewBlockWithCid(block.RawData(), c)
47+
if err != nil {
48+
return nil, err
49+
}
50+
// insert the block with the original CID to avoid problems
51+
// with pinning
52+
err = b.Blockstore.Put(block)
53+
if err != nil {
54+
return nil, err
55+
}
56+
return block, nil
57+
}
58+
59+
func tryOtherCidVersion(c *cid.Cid) *cid.Cid {
60+
prefix := c.Prefix()
61+
if prefix.Codec != cid.DagProtobuf {
62+
return nil
63+
}
64+
var c1 *cid.Cid
65+
if prefix.Version == 0 {
66+
c1 = cid.NewCidV1(cid.DagProtobuf, c.Hash())
67+
} else {
68+
c1 = cid.NewCidV0(c.Hash())
69+
}
70+
return c1
71+
}

0 commit comments

Comments
 (0)