Skip to content

Commit e348b59

Browse files
committed
refactor: block put --cid-codec
- 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
1 parent 4448de5 commit e348b59

File tree

5 files changed

+81
-48
lines changed

5 files changed

+81
-48
lines changed

core/commands/block.go

+34-24
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ var BlockCmd = &cmds.Command{
3131
Tagline: "Interact with raw IPFS blocks.",
3232
ShortDescription: `
3333
'ipfs block' is a plumbing command used to manipulate raw IPFS blocks.
34-
Reads from stdin or writes to stdout, and <key> is a base58 encoded
35-
multihash.
34+
Reads from stdin or writes to stdout. A block is identified by a Multihash
35+
passed with a valid CID.
3636
`,
3737
},
3838

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

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

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

115115
const (
116-
blockFormatOptionName = "format"
117-
blockStoreCodecOptionName = "store-codec"
118-
mhtypeOptionName = "mhtype"
119-
mhlenOptionName = "mhlen"
116+
blockFormatOptionName = "format"
117+
blockCidCodecOptionName = "cid-codec"
118+
mhtypeOptionName = "mhtype"
119+
mhlenOptionName = "mhlen"
120120
)
121121

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

134141
Arguments: []cmds.Argument{
135142
cmds.FileArg("data", true, true, "The data to be stored as an IPFS block.").EnableStdin(),
136143
},
137144
Options: []cmds.Option{
138-
cmds.StringOption(blockFormatOptionName, "f", "cid format for blocks to be created with."),
139-
cmds.StringOption(blockStoreCodecOptionName, "s", "multicodec name for blocks to be stored with"),
140-
cmds.StringOption(mhtypeOptionName, "multihash hash function").WithDefault("sha2-256"),
141-
cmds.IntOption(mhlenOptionName, "multihash hash length").WithDefault(-1),
142-
cmds.BoolOption(pinOptionName, "pin added blocks recursively").WithDefault(false),
145+
cmds.StringOption(blockCidCodecOptionName, "Multicodec to use in returned CID. Default: raw"),
146+
cmds.StringOption(mhtypeOptionName, "Multihash hash function").WithDefault("sha2-256"),
147+
cmds.IntOption(mhlenOptionName, "Multihash hash length").WithDefault(-1),
148+
cmds.BoolOption(pinOptionName, "Pin added blocks recursively").WithDefault(false),
143149
cmdutils.AllowBigBlockOption,
150+
cmds.StringOption(blockFormatOptionName, "f", "Use legacy format for returned CID (DEPRECATED)"),
144151
},
145152
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
146153
api, err := cmdenv.GetApi(env, req)
@@ -159,8 +166,11 @@ other than 'sha2-256' or format to anything other than 'v0' will result in CIDv1
159166
return errors.New("missing option \"mhlen\"")
160167
}
161168

162-
format, _ := req.Options[blockFormatOptionName].(string)
163-
storeCodec, _ := req.Options[blockStoreCodecOptionName].(string)
169+
cidCodec, _ := req.Options[blockCidCodecOptionName].(string)
170+
format, _ := req.Options[blockFormatOptionName].(string) // deprecated
171+
if format != "" && cidCodec != "" {
172+
return fmt.Errorf("unable to use %q (deprecated) with %q at the same time", blockFormatOptionName, blockCidCodecOptionName)
173+
}
164174

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

@@ -173,8 +183,8 @@ other than 'sha2-256' or format to anything other than 'v0' will result in CIDv1
173183

174184
p, err := api.Block().Put(req.Context, file,
175185
options.Block.Hash(mhtval, mhlen),
186+
options.Block.CidCodec(cidCodec),
176187
options.Block.Format(format),
177-
options.Block.StoreCodec(storeCodec),
178188
options.Block.Pin(pin))
179189
if err != nil {
180190
return err
@@ -216,14 +226,14 @@ type removedBlock struct {
216226

217227
var blockRmCmd = &cmds.Command{
218228
Helptext: cmds.HelpText{
219-
Tagline: "Remove IPFS block(s).",
229+
Tagline: "Remove IPFS block(s) from the local datastore.",
220230
ShortDescription: `
221231
'ipfs block rm' is a plumbing command for removing raw ipfs blocks.
222-
It takes a list of base58 encoded multihashes to remove.
232+
It takes a list of CIDs to remove from the local datastore..
223233
`,
224234
},
225235
Arguments: []cmds.Argument{
226-
cmds.StringArg("hash", true, true, "Bash58 encoded multihash of block(s) to remove."),
236+
cmds.StringArg("cid", true, true, "CIDs of block(s) to remove."),
227237
},
228238
Options: []cmds.Option{
229239
cmds.BoolOption(forceOptionName, "f", "Ignore nonexistent blocks."),

core/coreapi/block.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func (api *BlockAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.Bloc
3131
ctx, span := tracing.Span(ctx, "CoreAPI.BlockAPI", "Put")
3232
defer span.End()
3333

34-
settings, pref, err := caopts.BlockPutOptions(opts...)
34+
settings, err := caopts.BlockPutOptions(opts...)
3535
if err != nil {
3636
return nil, err
3737
}
@@ -41,7 +41,7 @@ func (api *BlockAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.Bloc
4141
return nil, err
4242
}
4343

44-
bcid, err := pref.Sum(data)
44+
bcid, err := settings.CidPrefix.Sum(data)
4545
if err != nil {
4646
return nil, err
4747
}

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ require (
5757
github.com/ipfs/go-unixfs v0.3.1
5858
github.com/ipfs/go-unixfsnode v1.1.3
5959
github.com/ipfs/go-verifcid v0.0.1
60-
github.com/ipfs/interface-go-ipfs-core v0.6.2
60+
github.com/ipfs/interface-go-ipfs-core v0.6.3-0.20220412233708-a6eb01527953
6161
github.com/ipfs/tar-utils v0.0.2
6262
github.com/ipld/go-car v0.3.2
6363
github.com/ipld/go-car/v2 v2.1.1

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -605,8 +605,8 @@ github.com/ipfs/go-unixfsnode v1.1.3/go.mod h1:ZZxUM5wXBC+G0Co9FjrYTOm+UlhZTjxLf
605605
github.com/ipfs/go-verifcid v0.0.1 h1:m2HI7zIuR5TFyQ1b79Da5N9dnnCP1vcu2QqawmWlK2E=
606606
github.com/ipfs/go-verifcid v0.0.1/go.mod h1:5Hrva5KBeIog4A+UpqlaIU+DEstipcJYQQZc0g37pY0=
607607
github.com/ipfs/interface-go-ipfs-core v0.4.0/go.mod h1:UJBcU6iNennuI05amq3FQ7g0JHUkibHFAfhfUIy927o=
608-
github.com/ipfs/interface-go-ipfs-core v0.6.2 h1:nnkq9zhb5O8lPzkZeynEymc83RqkTRqfYH4x5JNUkT4=
609-
github.com/ipfs/interface-go-ipfs-core v0.6.2/go.mod h1:h3NuO3wzv2KuKazt0zDF2/i8AFRqiKHusyh5DUQQdPA=
608+
github.com/ipfs/interface-go-ipfs-core v0.6.3-0.20220412233708-a6eb01527953 h1:0jHj/vY8khomu6mCvmRlJT0RNMWM2f108uYoMGsqyX0=
609+
github.com/ipfs/interface-go-ipfs-core v0.6.3-0.20220412233708-a6eb01527953/go.mod h1:lF27E/nnSPbylPqKVXGZghal2hzifs3MmjyiEjnc9FY=
610610
github.com/ipfs/tar-utils v0.0.2 h1:UNgHB4x/PPzbMkmJi+7EqC9LNMPDztOVSnx1HAqSNg4=
611611
github.com/ipfs/tar-utils v0.0.2/go.mod h1:4qlnRWgTVljIMhSG2SqRYn66NT+3wrv/kZt9V+eqxDM=
612612
github.com/ipld/go-car v0.3.2 h1:V9wt/80FNfbMRWSD98W5br6fyjUAyVgI2lDOTZX16Lg=

test/sharness/t0050-block.sh

+42-19
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,18 @@ test_description="Test block command"
1010

1111
test_init_ipfs
1212

13-
HASH="QmRKqGMAM6EZngbpjSqrvYzq5Qd8b1bSWymjSUY9zQSNDk"
14-
HASHB="QmdnpnsaEj69isdw5sNzp3h3HkaDz7xKq7BmvFFBzNr5e7"
13+
HASH="bafkreibmlvvgdyihetgocpof6xk64kjjzdeq2e4c7hqs3krdheosk4tgj4"
14+
HASHB="bafkreihfsphazrk2ilejpekyltjeh5k4yvwgjuwg26ueafohqioeo3sdca"
15+
16+
HASHV0="QmRKqGMAM6EZngbpjSqrvYzq5Qd8b1bSWymjSUY9zQSNDk"
17+
HASHBV0="QmdnpnsaEj69isdw5sNzp3h3HkaDz7xKq7BmvFFBzNr5e7"
1518

16-
#
1719
# "block put tests"
1820
#
1921

2022
test_expect_success "'ipfs block put' succeeds" '
2123
echo "Hello Mars!" >expected_in &&
22-
ipfs block put <expected_in >actual_out
24+
ipfs block put <expected_in | tee actual_out
2325
'
2426

2527
test_expect_success "'ipfs block put' output looks good" '
@@ -30,7 +32,7 @@ test_expect_success "'ipfs block put' output looks good" '
3032
test_expect_success "'ipfs block put' with 2 files succeeds" '
3133
echo "Hello Mars!" > a &&
3234
echo "Hello Venus!" > b &&
33-
ipfs block put a b >actual_out
35+
ipfs block put a b | tee actual_out
3436
'
3537

3638
test_expect_success "'ipfs block put' output looks good" '
@@ -39,8 +41,8 @@ test_expect_success "'ipfs block put' output looks good" '
3941
test_cmp expected_out actual_out
4042
'
4143

42-
test_expect_success "can set cid store codec on block put" '
43-
CODEC_HASH=$(ipfs block put --store-codec=dag-pb ../t0051-object-data/testPut.pb)
44+
test_expect_success "can set cid codec on block put" '
45+
CODEC_HASH=$(ipfs block put --cid-codec=dag-pb ../t0051-object-data/testPut.pb)
4446
'
4547

4648
test_expect_success "block get output looks right" '
@@ -205,7 +207,9 @@ test_expect_success "multi-block 'ipfs block rm -q' produces no output" '
205207
test ! -s block_rm_out
206208
'
207209

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

@@ -220,7 +224,22 @@ test_expect_success "block get output looks right" '
220224
test_cmp pb_block_out ../t0051-object-data/testPut.pb
221225
'
222226

223-
test_expect_success "can set multihash type and length on block put" '
227+
test_expect_success "can set --cid-codec=dag-pb on block put" '
228+
HASH=$(ipfs block put --cid-codec=dag-pb ../t0051-object-data/testPut.pb)
229+
'
230+
231+
test_expect_success "created an object correctly!" '
232+
ipfs object get $HASH > obj_out &&
233+
echo "{\"Links\":[],\"Data\":\"test json for sharness test\"}" > obj_exp &&
234+
test_cmp obj_out obj_exp
235+
'
236+
237+
test_expect_success "block get output looks right" '
238+
ipfs block get $HASH > pb_block_out &&
239+
test_cmp pb_block_out ../t0051-object-data/testPut.pb
240+
'
241+
242+
test_expect_success "can set multihash type and length on block put with --format=raw (deprecated)" '
224243
HASH=$(echo "foooo" | ipfs block put --format=raw --mhtype=sha3 --mhlen=20)
225244
'
226245

@@ -245,17 +264,21 @@ test_expect_success "no panic in output" '
245264
test_expect_code 1 grep "panic" stat_out
246265
'
247266

248-
# FIXME: FAILING. This should work once 'protobuf' is added back in
249-
# the default in https://github.com/ipfs/interface-go-ipfs-core/pull/80
250-
# (after the first round of review passes).
267+
test_expect_success "can set multihash type and length on block put without format or cid-codec" '
268+
HASH=$(echo "foooo" | ipfs block put --mhtype=sha3 --mhlen=20)
269+
'
251270

252-
#test_expect_success "can set multihash type and length on block put without format" '
253-
# HASH=$(echo "foooo" | ipfs block put --mhtype=sha3 --mhlen=20)
254-
#'
255-
#
256-
#test_expect_success "output looks good" '
257-
# test "bafybifctrq4xazzixy2v4ezymjcvzpskqdwlxra" = "$HASH"
258-
#'
271+
test_expect_success "output looks good" '
272+
test "bafkrifctrq4xazzixy2v4ezymjcvzpskqdwlxra" = "$HASH"
273+
'
274+
275+
test_expect_success "can set multihash type and length on block put with cid-codec=dag-pb" '
276+
HASH=$(echo "foooo" | ipfs block put --mhtype=sha3 --mhlen=20 --cid-codec=dag-pb)
277+
'
278+
279+
test_expect_success "output looks good" '
280+
test "bafybifctrq4xazzixy2v4ezymjcvzpskqdwlxra" = "$HASH"
281+
'
259282

260283
test_expect_success "put with sha3 and cidv0 fails" '
261284
echo "foooo" | test_must_fail ipfs block put --mhtype=sha3 --mhlen=20 --format=v0

0 commit comments

Comments
 (0)