diff --git a/core/commands/block.go b/core/commands/block.go index 945dbf64f80..402ab7c9f93 100644 --- a/core/commands/block.go +++ b/core/commands/block.go @@ -114,6 +114,7 @@ It outputs to stdout, and is a base58 encoded multihash. const ( blockFormatOptionName = "format" + blockStoreCodecOptionName = "store-codec" mhtypeOptionName = "mhtype" mhlenOptionName = "mhlen" ) @@ -135,6 +136,7 @@ other than 'sha2-256' or format to anything other than 'v0' will result in CIDv1 }, Options: []cmds.Option{ cmds.StringOption(blockFormatOptionName, "f", "cid format for blocks to be created with."), + cmds.StringOption(blockStoreCodecOptionName, "s", "multicodec name for blocks to be stored with"), cmds.StringOption(mhtypeOptionName, "multihash hash function").WithDefault("sha2-256"), cmds.IntOption(mhlenOptionName, "multihash hash length").WithDefault(-1), cmds.BoolOption(pinOptionName, "pin added blocks recursively").WithDefault(false), @@ -157,14 +159,8 @@ other than 'sha2-256' or format to anything other than 'v0' will result in CIDv1 return errors.New("missing option \"mhlen\"") } - format, formatSet := req.Options[blockFormatOptionName].(string) - if !formatSet { - if mhtval != mh.SHA2_256 || (mhlen != -1 && mhlen != 32) { - format = "protobuf" - } else { - format = "v0" - } - } + format, _ := req.Options[blockFormatOptionName].(string) + storeCodec, _ := req.Options[blockStoreCodecOptionName].(string) pin, _ := req.Options[pinOptionName].(bool) @@ -178,6 +174,7 @@ other than 'sha2-256' or format to anything other than 'v0' will result in CIDv1 p, err := api.Block().Put(req.Context, file, options.Block.Hash(mhtval, mhlen), options.Block.Format(format), + options.Block.StoreCodec(storeCodec), options.Block.Pin(pin)) if err != nil { return err diff --git a/core/commands/cid.go b/core/commands/cid.go index 13c3e83a918..2531cd31733 100644 --- a/core/commands/cid.go +++ b/core/commands/cid.go @@ -11,7 +11,9 @@ import ( cidutil "github.com/ipfs/go-cidutil" cmds "github.com/ipfs/go-ipfs-cmds" verifcid "github.com/ipfs/go-verifcid" + "github.com/ipld/go-ipld-prime/multicodec" mbase "github.com/multiformats/go-multibase" + mc "github.com/multiformats/go-multicodec" mhash "github.com/multiformats/go-multihash" ) @@ -296,7 +298,8 @@ var basesCmd = &cmds.Command{ } const ( - codecsNumericOptionName = "numeric" + codecsNumericOptionName = "numeric" + codecsSupportedOptionName = "supported" ) var codecsCmd = &cmds.Command{ @@ -304,13 +307,31 @@ var codecsCmd = &cmds.Command{ Tagline: "List available CID codecs.", }, Options: []cmds.Option{ - cmds.BoolOption(codecsNumericOptionName, "also include numeric codes"), + cmds.BoolOption(codecsNumericOptionName, "n", "also include numeric codes"), + cmds.BoolOption(codecsSupportedOptionName, "s", "list only codecs supported by go-ipfs commands"), }, Run: func(req *cmds.Request, resp cmds.ResponseEmitter, env cmds.Environment) error { + listSupported, _ := req.Options[codecsSupportedOptionName].(bool) + supportedCodecs := make(map[uint64]struct{}) + if listSupported { + for _, code := range multicodec.ListEncoders() { + supportedCodecs[code] = struct{}{} + } + for _, code := range multicodec.ListDecoders() { + supportedCodecs[code] = struct{}{} + } + } + var res []CodeAndName - // use CodecToStr as there are multiple names for a given code - for code, name := range cid.CodecToStr { - res = append(res, CodeAndName{int(code), name}) + for _, code := range mc.KnownCodes() { + if code.Tag() == "ipld" { + if listSupported { + if _, ok := supportedCodecs[uint64(code)]; !ok { + continue + } + } + res = append(res, CodeAndName{int(code), mc.Code(code).String()}) + } } return cmds.EmitOnce(resp, res) }, diff --git a/test/sharness/t0050-block.sh b/test/sharness/t0050-block.sh index 2b58c7efe1a..3b6ee8ccf24 100755 --- a/test/sharness/t0050-block.sh +++ b/test/sharness/t0050-block.sh @@ -39,6 +39,15 @@ test_expect_success "'ipfs block put' output looks good" ' test_cmp expected_out actual_out ' +test_expect_success "can set cid store codec on block put" ' + CODEC_HASH=$(ipfs block put --store-codec=dag-pb ../t0051-object-data/testPut.pb) +' + +test_expect_success "block get output looks right" ' + ipfs block get $CODEC_HASH > pb_block_out && + test_cmp pb_block_out ../t0051-object-data/testPut.pb +' + # # "block get" tests # @@ -236,13 +245,17 @@ 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) -' +# FIXME: FAILING. This should work once 'protobuf' is added back in +# the default in https://github.com/ipfs/interface-go-ipfs-core/pull/80 +# (after the first round of review passes). -test_expect_success "output looks good" ' - test "bafybifctrq4xazzixy2v4ezymjcvzpskqdwlxra" = "$HASH" -' +#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 "bafybifctrq4xazzixy2v4ezymjcvzpskqdwlxra" = "$HASH" +#' test_expect_success "put with sha3 and cidv0 fails" ' echo "foooo" | test_must_fail ipfs block put --mhtype=sha3 --mhlen=20 --format=v0 diff --git a/test/sharness/t0290-cid.sh b/test/sharness/t0290-cid.sh index 680e5308d91..8d4dc62b693 100755 --- a/test/sharness/t0290-cid.sh +++ b/test/sharness/t0290-cid.sh @@ -103,11 +103,19 @@ Z 90 base58flickr EOF cat < codecs_expect + 81 cbor 85 raw - 112 protobuf - 113 cbor + 112 dag-pb + 113 dag-cbor + 114 libp2p-key 120 git-raw + 123 torrent-info + 124 torrent-file + 129 leofcoin-block + 130 leofcoin-tx + 131 leofcoin-pr 133 dag-jose + 134 dag-cose 144 eth-block 145 eth-block-list 146 eth-tx-trie @@ -117,16 +125,34 @@ cat < codecs_expect 150 eth-state-trie 151 eth-account-snapshot 152 eth-storage-trie + 153 eth-receipt-log-trie + 154 eth-reciept-log 176 bitcoin-block 177 bitcoin-tx + 178 bitcoin-witness-commitment 192 zcash-block 193 zcash-tx + 208 stellar-block + 209 stellar-tx 224 decred-block 225 decred-tx 240 dash-block 241 dash-tx -61697 fil-commitment-unsealed -61698 fil-commitment-sealed + 250 swarm-manifest + 251 swarm-feed + 297 dag-json + 496 swhid-1-snp + 512 json +EOF + +cat < supported_codecs_expect + 81 cbor + 85 raw + 112 dag-pb + 113 dag-cbor + 120 git-raw + 297 dag-json + 512 json EOF cat < hashes_expect @@ -232,6 +258,17 @@ test_expect_success "cid codecs --numeric" ' test_cmp codecs_expect actual ' +test_expect_success "cid codecs --supported" ' + cut -c 8- supported_codecs_expect > expect && + ipfs cid codecs --supported > actual + test_cmp expect actual +' + +test_expect_success "cid codecs --supported --numeric" ' + ipfs cid codecs --supported --numeric > actual && + test_cmp supported_codecs_expect actual +' + test_expect_success "cid hashes" ' cut -c 8- hashes_expect > expect && ipfs cid hashes > actual