Skip to content
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

commands/block: use CIDv1 with custom mhtype #4563

Merged
merged 3 commits into from
Mar 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 29 additions & 12 deletions core/commands/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package commands
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -121,14 +122,17 @@ var blockPutCmd = &cmds.Command{
ShortDescription: `
'ipfs block put' is a plumbing command for storing raw IPFS blocks.
It reads from stdin, and <key> is a base58 encoded multihash.

By default CIDv0 is going to be generated. Setting 'mhtype' to anything other
than 'sha2-256' or format to anything other than 'v0' will result in CIDv1.
`,
},

Arguments: []cmdkit.Argument{
cmdkit.FileArg("data", true, false, "The data to be stored as an IPFS block.").EnableStdin(),
},
Options: []cmdkit.Option{
cmdkit.StringOption("format", "f", "cid format for blocks to be created with.").WithDefault("v0"),
cmdkit.StringOption("format", "f", "cid format for blocks to be created with."),
cmdkit.StringOption("mhtype", "multihash hash function").WithDefault("sha2-256"),
cmdkit.IntOption("mhlen", "multihash hash length").WithDefault(-1),
},
Expand Down Expand Up @@ -157,27 +161,40 @@ It reads from stdin, and <key> is a base58 encoded multihash.
return
}

mhtype, _ := req.Options["mhtype"].(string)
mhtval, ok := mh.Names[mhtype]
if !ok {
err := fmt.Errorf("unrecognized multihash function: %s", mhtype)
res.SetError(err, cmdkit.ErrNormal)
return
}

var pref cid.Prefix
pref.Version = 1

format, _ := req.Options["format"].(string)
formatval, ok := cid.Codecs[format]
if !ok {
res.SetError(fmt.Errorf("unrecognized format: %s", format), cmdkit.ErrNormal)
return
format, formatSet := req.Options["format"].(string)
if !formatSet {
if mhtval == mh.SHA2_256 {
format = "v0"
} else {
format = "protobuf"
}
}

if format == "v0" {
pref.Version = 0
}
pref.Codec = formatval

mhtype, _ := req.Options["mhtype"].(string)
mhtval, ok := mh.Names[mhtype]
formatval, ok := cid.Codecs[format]
if !ok {
err := fmt.Errorf("unrecognized multihash function: %s", mhtype)
res.SetError(err, cmdkit.ErrNormal)
res.SetError(fmt.Errorf("unrecognized format: '%s'", format), cmdkit.ErrNormal)
return
}
if mhtval != mh.SHA2_256 && pref.Version == 0 {
res.SetError(errors.New("cannot generate CIDv0 with non-sha256 hash function"), cmdkit.ErrNormal)
return
}

pref.Codec = formatval
pref.MhType = mhtval

mhlen, ok := req.Options["mhlen"].(int)
Expand Down
16 changes: 14 additions & 2 deletions test/sharness/t0050-block.sh
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,11 @@ test_expect_success "block get output looks right" '
'

test_expect_success "can set multihash type and length on block put" '
HASH=$(echo "foooo" | ipfs block put --format=raw --mhtype=sha3 --mhlen=16)
HASH=$(echo "foooo" | ipfs block put --format=raw --mhtype=sha3 --mhlen=20)
'

test_expect_success "output looks good" '
test "z25ScPysKoxJBcPxczn9NvuHiZU5" = "$HASH"
test "z83bYcqyBkbx5fuNAcvbdv4pr5RYQiEpK" = "$HASH"
'

test_expect_success "can read block with different hash" '
Expand All @@ -209,4 +209,16 @@ test_expect_success "no panic in output" '
test_expect_code 1 grep "panic" stat_out
'

test_expect_success "can set multihash type and length on block put without format" '
HASH=$(echo "foooo" | ipfs block put --mhtype=sha3 --mhlen=20)
'

test_expect_success "output looks good" '
test "z8bwYCvQPhyDY7VUTsUdGdE8Evm1ktSPV" = "$HASH"
'

test_expect_success "put with sha3 and cidv0 fails" '
echo "foooo" | test_must_fail ipfs block put --mhtype=sha3 --mhlen=20 --format=v0
'

test_done