Skip to content

Commit

Permalink
cmd/block: add selection of multihash parameters to block put command
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Jeromy <why@ipfs.io>
  • Loading branch information
whyrusleeping committed Dec 8, 2016
1 parent 24d35f3 commit cf6e321
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
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

0 comments on commit cf6e321

Please sign in to comment.