Skip to content

Commit

Permalink
commands/block: use CIDv1 with custom mhtype
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Łukasz Magiera <[email protected]>
  • Loading branch information
magik6k committed Jan 8, 2018
1 parent c1a488f commit 4c180b8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
22 changes: 11 additions & 11 deletions core/commands/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (

util "github.com/ipfs/go-ipfs/blocks/blockstore/util"
e "github.com/ipfs/go-ipfs/core/commands/e"
"gx/ipfs/QmP9vZfc5WSjfGTXmwX2EcicMFzmZ6fXn7HTdKYat6ccmH/go-ipfs-cmds"
"gx/ipfs/QmQp2a2Hhb7F6eK2A5hN8f9aJy4mtkEikL9Zj4cgB7d1dD/go-ipfs-cmdkit"

cmds "gx/ipfs/QmP9vZfc5WSjfGTXmwX2EcicMFzmZ6fXn7HTdKYat6ccmH/go-ipfs-cmds"
cmdkit "gx/ipfs/QmQp2a2Hhb7F6eK2A5hN8f9aJy4mtkEikL9Zj4cgB7d1dD/go-ipfs-cmdkit"
mh "gx/ipfs/QmYeKnKpubCMRiq3PGZcTREErthbb5Q9cXsCoSkD9bjEBd/go-multihash"
blocks "gx/ipfs/QmYsEQydGrsxNZfAiskvQ76N2xE9hDQtSAkRSynwMiUK3c/go-block-format"
cid "gx/ipfs/QmeSrf6pzut73u6zLQkRFQ3ygt3k6XFT2kjdYP8Tnkwwyg/go-cid"
Expand Down Expand Up @@ -156,6 +156,14 @@ It reads from stdin, and <key> is a base58 encoded multihash.
return
}

mhtype, _, _ := req.Option("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

Expand All @@ -165,18 +173,10 @@ It reads from stdin, and <key> is a base58 encoded multihash.
res.SetError(fmt.Errorf("unrecognized format: %s", format), cmdkit.ErrNormal)
return
}
if format == "v0" {
if format == "v0" && mhtval == mh.SHA2_256 {
pref.Version = 0
}
pref.Codec = formatval

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

mhlen, _, err := req.Option("mhlen").Int()
Expand Down
8 changes: 8 additions & 0 deletions test/sharness/t0050-block.sh
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,12 @@ 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=16)
'

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

test_done

0 comments on commit 4c180b8

Please sign in to comment.