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

cmd/block: add selection of multihash parameters to block put command #3483

Merged
merged 1 commit into from
Dec 8, 2016
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
22 changes: 19 additions & 3 deletions core/commands/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ It reads from stdin, and <key> is a base58 encoded multihash.
},
Options: []cmds.Option{
cmds.StringOption("format", "f", "cid format for blocks to be created with.").Default("v0"),
cmds.StringOption("mhtype", "multihash hash function").Default("sha2-256"),
cmds.IntOption("mhlen", "multihash hash length").Default(-1),
},
Run: func(req cmds.Request, res cmds.Response) {
n, err := req.InvocContext().GetNode()
Expand All @@ -143,11 +145,10 @@ It reads from stdin, and <key> is a base58 encoded multihash.
return
}

format, _, _ := req.Option("format").String()
var pref cid.Prefix
pref.MhType = mh.SHA2_256
pref.MhLength = -1
pref.Version = 1

format, _, _ := req.Option("format").String()
switch format {
case "cbor":
pref.Codec = cid.DagCBOR
Expand All @@ -163,6 +164,21 @@ It reads from stdin, and <key> is a base58 encoded multihash.
return
}

mhtype, _, _ := req.Option("mhtype").String()
mhtval, ok := mh.Names[mhtype]
if !ok {
res.SetError(fmt.Errorf("unrecognized multihash function: %s", mhtype), cmds.ErrNormal)
return
}
pref.MhType = mhtval

mhlen, _, err := req.Option("mhlen").Int()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
pref.MhLength = mhlen

bcid, err := pref.Sum(data)
if err != nil {
res.SetError(err, cmds.ErrNormal)
Expand Down
13 changes: 13 additions & 0 deletions test/sharness/t0050-block.sh
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,19 @@ test_expect_success "block get output looks right" '
test_cmp pb_block_out ../t0051-object-data/testPut.pb
'

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

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

test_expect_success "can read block with different hash" '
ipfs block get $HASH > blk_get_out &&
echo "foooo" > blk_get_exp &&
test_cmp blk_get_exp blk_get_out
'
#
# Misc tests
#
Expand Down