-
-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ability to retrieve blocks even if given using a different CID version #5285
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
#!/usr/bin/env bash | ||
# | ||
# Copyright (c) 2017 Jakub Sztandera | ||
# MIT Licensed; see the LICENSE file in this repository. | ||
# | ||
|
||
test_description="CID Version 0/1 Duality" | ||
|
||
. lib/test-lib.sh | ||
|
||
test_init_ipfs | ||
|
||
# | ||
# | ||
# | ||
|
||
test_expect_success "create two small files" ' | ||
random 1000 7 > afile | ||
random 1000 9 > bfile | ||
' | ||
|
||
test_expect_success "add file using CIDv1 but don't pin" ' | ||
AHASHv1=$(ipfs add -q --cid-version=1 --raw-leaves=false --pin=false afile) | ||
' | ||
|
||
test_expect_success "add file using CIDv0" ' | ||
AHASHv0=$(ipfs add -q --cid-version=0 afile) | ||
' | ||
|
||
test_expect_success "check hashes" ' | ||
test "$(cid-fmt %v-%c $AHASHv0)" = "cidv0-protobuf" && | ||
test "$(cid-fmt %v-%c $AHASHv1)" = "cidv1-protobuf" && | ||
test "$(cid-fmt -v 0 %s $AHASHv1)" = "$AHASHv0" | ||
' | ||
|
||
test_expect_success "make sure CIDv1 hash really is in the repo" ' | ||
ipfs refs local | grep -q $AHASHv1 | ||
' | ||
|
||
test_expect_success "make sure CIDv0 hash really is in the repo" ' | ||
ipfs refs local | grep -q $AHASHv0 | ||
' | ||
|
||
test_expect_success "run gc" ' | ||
ipfs repo gc | ||
' | ||
|
||
test_expect_success "make sure the CIDv0 hash is in the repo" ' | ||
ipfs refs local | grep -q $AHASHv0 | ||
' | ||
|
||
test_expect_success "make sure we can get CIDv0 added file" ' | ||
ipfs cat $AHASHv0 > thefile && | ||
test_cmp afile thefile | ||
' | ||
|
||
test_expect_success "make sure the CIDv1 hash is not in the repo" ' | ||
! ipfs refs local | grep -q $AHASHv1 | ||
' | ||
|
||
test_expect_success "clean up" ' | ||
ipfs pin rm $AHASHv0 && | ||
ipfs repo gc && | ||
! ipfs refs local | grep -q $AHASHv0 | ||
' | ||
|
||
# | ||
# | ||
# | ||
|
||
test_expect_success "add file using CIDv1 but don't pin" ' | ||
ipfs add -q --cid-version=1 --raw-leaves=false --pin=false afile | ||
' | ||
|
||
test_expect_success "check that we can access the file when converted to CIDv0" ' | ||
ipfs cat $AHASHv0 > thefile && | ||
test_cmp afile thefile | ||
' | ||
|
||
test_expect_success "clean up" ' | ||
ipfs repo gc | ||
' | ||
|
||
test_expect_success "add file using CIDv0 but don't pin" ' | ||
ipfs add -q --cid-version=0 --raw-leaves=false --pin=false afile | ||
' | ||
|
||
test_expect_success "check that we can access the file when converted to CIDv1" ' | ||
ipfs cat $AHASHv1 > thefile && | ||
test_cmp afile thefile | ||
' | ||
|
||
# | ||
# | ||
# | ||
|
||
test_expect_success "set up iptb testbed" ' | ||
iptb init -n 2 -p 0 -f --bootstrap=none | ||
' | ||
|
||
test_expect_success "start nodes" ' | ||
iptb start && | ||
iptb connect 0 1 | ||
' | ||
|
||
test_expect_success "add afile using CIDv0 to node 0" ' | ||
iptb run 0 ipfs add -q --cid-version=0 afile | ||
' | ||
|
||
test_expect_success "get afile using CIDv1 via node 1" ' | ||
iptb run 1 ipfs --timeout=2s cat $AHASHv1 > thefile && | ||
test_cmp afile thefile | ||
' | ||
|
||
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_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 | ||
' | ||
|
||
test_expect_success "stop testbed" ' | ||
iptb stop | ||
' | ||
|
||
test_done |
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 | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should check hash alg too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.