Skip to content

Commit

Permalink
refactor: block put --cid-codec
Browse files Browse the repository at this point in the history
- switches to ipfs/interface-go-ipfs-core#80 (comment)
- updates helptext of block commands to reflect the fact we now use CIDs
  everywhere and document the defaults
- add tests for new --cid-codec and old --format behavior
  • Loading branch information
lidel committed Apr 13, 2022
1 parent 4448de5 commit e348b59
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 48 deletions.
58 changes: 34 additions & 24 deletions core/commands/block.go
Expand Up @@ -31,8 +31,8 @@ var BlockCmd = &cmds.Command{
Tagline: "Interact with raw IPFS blocks.",
ShortDescription: `
'ipfs block' is a plumbing command used to manipulate raw IPFS blocks.
Reads from stdin or writes to stdout, and <key> is a base58 encoded
multihash.
Reads from stdin or writes to stdout. A block is identified by a Multihash
passed with a valid CID.
`,
},

Expand All @@ -51,14 +51,14 @@ var blockStatCmd = &cmds.Command{
'ipfs block stat' is a plumbing command for retrieving information
on raw IPFS blocks. It outputs the following to stdout:
Key - the base58 encoded multihash
Key - the CID of the block
Size - the size of the block in bytes
`,
},

Arguments: []cmds.Argument{
cmds.StringArg("key", true, false, "The base58 multihash of an existing block to stat.").EnableStdin(),
cmds.StringArg("cid", true, false, "The CID of an existing block to stat.").EnableStdin(),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env, req)
Expand Down Expand Up @@ -90,12 +90,12 @@ var blockGetCmd = &cmds.Command{
Tagline: "Get a raw IPFS block.",
ShortDescription: `
'ipfs block get' is a plumbing command for retrieving raw IPFS blocks.
It outputs to stdout, and <key> is a base58 encoded multihash.
It takes a <cid>, and outputs the block to stdout.
`,
},

Arguments: []cmds.Argument{
cmds.StringArg("key", true, false, "The base58 multihash of an existing block to get.").EnableStdin(),
cmds.StringArg("cid", true, false, "The CID of an existing block to get.").EnableStdin(),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env, req)
Expand All @@ -113,34 +113,41 @@ It outputs to stdout, and <key> is a base58 encoded multihash.
}

const (
blockFormatOptionName = "format"
blockStoreCodecOptionName = "store-codec"
mhtypeOptionName = "mhtype"
mhlenOptionName = "mhlen"
blockFormatOptionName = "format"
blockCidCodecOptionName = "cid-codec"
mhtypeOptionName = "mhtype"
mhlenOptionName = "mhlen"
)

var blockPutCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Store input as an IPFS block.",
ShortDescription: `
'ipfs block put' is a plumbing command for storing raw IPFS blocks.
It reads from stdin, and outputs the block's CID to stdout.
It reads data from stdin, and outputs the block's CID to stdout.
Unless specified, this command returns dag-pb CIDv0 CIDs. Setting 'mhtype' to anything
other than 'sha2-256' or format to anything other than 'v0' will result in CIDv1.
Unless cid-codec is specified, this command returns raw (0x55) CIDv1 CIDs.
Passing alternative --cid-codec does not modify imported data, nor run any
validation. It is provided solely for convenience for users who create blocks
in userland.
NOTE:
Do not use --format for any new code. It got superseded by --cid-codec and left
only for backward compatibility when a legacy CIDv0 is required (--format=v0).
`,
},

Arguments: []cmds.Argument{
cmds.FileArg("data", true, true, "The data to be stored as an IPFS block.").EnableStdin(),
},
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),
cmds.StringOption(blockCidCodecOptionName, "Multicodec to use in returned CID. Default: raw"),
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),
cmdutils.AllowBigBlockOption,
cmds.StringOption(blockFormatOptionName, "f", "Use legacy format for returned CID (DEPRECATED)"),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env, req)
Expand All @@ -159,8 +166,11 @@ other than 'sha2-256' or format to anything other than 'v0' will result in CIDv1
return errors.New("missing option \"mhlen\"")
}

format, _ := req.Options[blockFormatOptionName].(string)
storeCodec, _ := req.Options[blockStoreCodecOptionName].(string)
cidCodec, _ := req.Options[blockCidCodecOptionName].(string)
format, _ := req.Options[blockFormatOptionName].(string) // deprecated
if format != "" && cidCodec != "" {
return fmt.Errorf("unable to use %q (deprecated) with %q at the same time", blockFormatOptionName, blockCidCodecOptionName)
}

pin, _ := req.Options[pinOptionName].(bool)

Expand All @@ -173,8 +183,8 @@ 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.CidCodec(cidCodec),
options.Block.Format(format),
options.Block.StoreCodec(storeCodec),
options.Block.Pin(pin))
if err != nil {
return err
Expand Down Expand Up @@ -216,14 +226,14 @@ type removedBlock struct {

var blockRmCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Remove IPFS block(s).",
Tagline: "Remove IPFS block(s) from the local datastore.",
ShortDescription: `
'ipfs block rm' is a plumbing command for removing raw ipfs blocks.
It takes a list of base58 encoded multihashes to remove.
It takes a list of CIDs to remove from the local datastore..
`,
},
Arguments: []cmds.Argument{
cmds.StringArg("hash", true, true, "Bash58 encoded multihash of block(s) to remove."),
cmds.StringArg("cid", true, true, "CIDs of block(s) to remove."),
},
Options: []cmds.Option{
cmds.BoolOption(forceOptionName, "f", "Ignore nonexistent blocks."),
Expand Down
4 changes: 2 additions & 2 deletions core/coreapi/block.go
Expand Up @@ -31,7 +31,7 @@ func (api *BlockAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.Bloc
ctx, span := tracing.Span(ctx, "CoreAPI.BlockAPI", "Put")
defer span.End()

settings, pref, err := caopts.BlockPutOptions(opts...)
settings, err := caopts.BlockPutOptions(opts...)
if err != nil {
return nil, err
}
Expand All @@ -41,7 +41,7 @@ func (api *BlockAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.Bloc
return nil, err
}

bcid, err := pref.Sum(data)
bcid, err := settings.CidPrefix.Sum(data)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -57,7 +57,7 @@ require (
github.com/ipfs/go-unixfs v0.3.1
github.com/ipfs/go-unixfsnode v1.1.3
github.com/ipfs/go-verifcid v0.0.1
github.com/ipfs/interface-go-ipfs-core v0.6.2
github.com/ipfs/interface-go-ipfs-core v0.6.3-0.20220412233708-a6eb01527953
github.com/ipfs/tar-utils v0.0.2
github.com/ipld/go-car v0.3.2
github.com/ipld/go-car/v2 v2.1.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Expand Up @@ -605,8 +605,8 @@ github.com/ipfs/go-unixfsnode v1.1.3/go.mod h1:ZZxUM5wXBC+G0Co9FjrYTOm+UlhZTjxLf
github.com/ipfs/go-verifcid v0.0.1 h1:m2HI7zIuR5TFyQ1b79Da5N9dnnCP1vcu2QqawmWlK2E=
github.com/ipfs/go-verifcid v0.0.1/go.mod h1:5Hrva5KBeIog4A+UpqlaIU+DEstipcJYQQZc0g37pY0=
github.com/ipfs/interface-go-ipfs-core v0.4.0/go.mod h1:UJBcU6iNennuI05amq3FQ7g0JHUkibHFAfhfUIy927o=
github.com/ipfs/interface-go-ipfs-core v0.6.2 h1:nnkq9zhb5O8lPzkZeynEymc83RqkTRqfYH4x5JNUkT4=
github.com/ipfs/interface-go-ipfs-core v0.6.2/go.mod h1:h3NuO3wzv2KuKazt0zDF2/i8AFRqiKHusyh5DUQQdPA=
github.com/ipfs/interface-go-ipfs-core v0.6.3-0.20220412233708-a6eb01527953 h1:0jHj/vY8khomu6mCvmRlJT0RNMWM2f108uYoMGsqyX0=
github.com/ipfs/interface-go-ipfs-core v0.6.3-0.20220412233708-a6eb01527953/go.mod h1:lF27E/nnSPbylPqKVXGZghal2hzifs3MmjyiEjnc9FY=
github.com/ipfs/tar-utils v0.0.2 h1:UNgHB4x/PPzbMkmJi+7EqC9LNMPDztOVSnx1HAqSNg4=
github.com/ipfs/tar-utils v0.0.2/go.mod h1:4qlnRWgTVljIMhSG2SqRYn66NT+3wrv/kZt9V+eqxDM=
github.com/ipld/go-car v0.3.2 h1:V9wt/80FNfbMRWSD98W5br6fyjUAyVgI2lDOTZX16Lg=
Expand Down
61 changes: 42 additions & 19 deletions test/sharness/t0050-block.sh
Expand Up @@ -10,16 +10,18 @@ test_description="Test block command"

test_init_ipfs

HASH="QmRKqGMAM6EZngbpjSqrvYzq5Qd8b1bSWymjSUY9zQSNDk"
HASHB="QmdnpnsaEj69isdw5sNzp3h3HkaDz7xKq7BmvFFBzNr5e7"
HASH="bafkreibmlvvgdyihetgocpof6xk64kjjzdeq2e4c7hqs3krdheosk4tgj4"
HASHB="bafkreihfsphazrk2ilejpekyltjeh5k4yvwgjuwg26ueafohqioeo3sdca"

HASHV0="QmRKqGMAM6EZngbpjSqrvYzq5Qd8b1bSWymjSUY9zQSNDk"
HASHBV0="QmdnpnsaEj69isdw5sNzp3h3HkaDz7xKq7BmvFFBzNr5e7"

#
# "block put tests"
#

test_expect_success "'ipfs block put' succeeds" '
echo "Hello Mars!" >expected_in &&
ipfs block put <expected_in >actual_out
ipfs block put <expected_in | tee actual_out
'

test_expect_success "'ipfs block put' output looks good" '
Expand All @@ -30,7 +32,7 @@ test_expect_success "'ipfs block put' output looks good" '
test_expect_success "'ipfs block put' with 2 files succeeds" '
echo "Hello Mars!" > a &&
echo "Hello Venus!" > b &&
ipfs block put a b >actual_out
ipfs block put a b | tee actual_out
'

test_expect_success "'ipfs block put' output looks good" '
Expand All @@ -39,8 +41,8 @@ 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 "can set cid codec on block put" '
CODEC_HASH=$(ipfs block put --cid-codec=dag-pb ../t0051-object-data/testPut.pb)
'

test_expect_success "block get output looks right" '
Expand Down Expand Up @@ -205,7 +207,9 @@ test_expect_success "multi-block 'ipfs block rm -q' produces no output" '
test ! -s block_rm_out
'

test_expect_success "can set cid format on block put" '
# --format used 'protobuf' for 'dag-pb' which was invalid, but we keep
# for backward-compatibility
test_expect_success "can set deprecated --format=protobuf on block put" '
HASH=$(ipfs block put --format=protobuf ../t0051-object-data/testPut.pb)
'

Expand All @@ -220,7 +224,22 @@ 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" '
test_expect_success "can set --cid-codec=dag-pb on block put" '
HASH=$(ipfs block put --cid-codec=dag-pb ../t0051-object-data/testPut.pb)
'

test_expect_success "created an object correctly!" '
ipfs object get $HASH > obj_out &&
echo "{\"Links\":[],\"Data\":\"test json for sharness test\"}" > obj_exp &&
test_cmp obj_out obj_exp
'

test_expect_success "block get output looks right" '
ipfs block get $HASH > pb_block_out &&
test_cmp pb_block_out ../t0051-object-data/testPut.pb
'

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

Expand All @@ -245,17 +264,21 @@ test_expect_success "no panic in output" '
test_expect_code 1 grep "panic" stat_out
'

# 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 "can set multihash type and length on block put without format or cid-codec" '
HASH=$(echo "foooo" | ipfs block put --mhtype=sha3 --mhlen=20)
'

#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 "output looks good" '
test "bafkrifctrq4xazzixy2v4ezymjcvzpskqdwlxra" = "$HASH"
'

test_expect_success "can set multihash type and length on block put with cid-codec=dag-pb" '
HASH=$(echo "foooo" | ipfs block put --mhtype=sha3 --mhlen=20 --cid-codec=dag-pb)
'

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
Expand Down

0 comments on commit e348b59

Please sign in to comment.