Skip to content

Commit 93c4f19

Browse files
authored
Merge pull request #5281 from ipfs/kevina/inline-cids
Add support for inlinling via the id-hash
2 parents 4fb2666 + 7e3265a commit 93c4f19

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

core/commands/add.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
cmds "gx/ipfs/QmPTfgFTo9PFr1PvPKyKoeMgBvYPh6cX3aDP7DHKVbnCbi/go-ipfs-cmds"
2020
mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"
2121
pb "gx/ipfs/QmPtj12fdwuAqj9sBSTNUxBNu8kCGNp8b3o8yUzMm5GHpq/pb"
22+
cidutil "gx/ipfs/QmPyxJ2QS7L5FhGkNYkNcXHGjDhvGHueJ4auqAstFHYxy5/go-cidutil"
2223
cmdkit "gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
2324
files "gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit/files"
2425
offline "gx/ipfs/QmZxjqR9Qgompju73kakSoUj3rbVndAzky3oCDiBNCxPs1/go-ipfs-exchange-offline"
@@ -45,6 +46,8 @@ const (
4546
fstoreCacheOptionName = "fscache"
4647
cidVersionOptionName = "cid-version"
4748
hashOptionName = "hash"
49+
inlineOptionName = "inline"
50+
inlineLimitOptionName = "inline-limit"
4851
)
4952

5053
const adderOutChanSize = 8
@@ -121,6 +124,8 @@ You can now check what blocks have been created by:
121124
cmdkit.BoolOption(fstoreCacheOptionName, "Check the filestore for pre-existing blocks. (experimental)"),
122125
cmdkit.IntOption(cidVersionOptionName, "CID version. Defaults to 0 unless an option that depends on CIDv1 is passed. (experimental)"),
123126
cmdkit.StringOption(hashOptionName, "Hash function to use. Implies CIDv1 if not sha2-256. (experimental)").WithDefault("sha2-256"),
127+
cmdkit.BoolOption(inlineOptionName, "Inline small blocks into CIDs. (experimental)"),
128+
cmdkit.IntOption(inlineLimitOptionName, "Maximum block size to inline. (experimental)").WithDefault(32),
124129
},
125130
PreRun: func(req *cmds.Request, env cmds.Environment) error {
126131
quiet, _ := req.Options[quietOptionName].(bool)
@@ -174,6 +179,8 @@ You can now check what blocks have been created by:
174179
fscache, _ := req.Options[fstoreCacheOptionName].(bool)
175180
cidVer, cidVerSet := req.Options[cidVersionOptionName].(int)
176181
hashFunStr, _ := req.Options[hashOptionName].(string)
182+
inline, _ := req.Options[inlineOptionName].(bool)
183+
inlineLimit, _ := req.Options[inlineLimitOptionName].(int)
177184

178185
// The arguments are subject to the following constraints.
179186
//
@@ -280,7 +287,14 @@ You can now check what blocks have been created by:
280287
fileAdder.Silent = silent
281288
fileAdder.RawLeaves = rawblks
282289
fileAdder.NoCopy = nocopy
283-
fileAdder.CidBuilder = &prefix
290+
fileAdder.CidBuilder = prefix
291+
292+
if inline {
293+
fileAdder.CidBuilder = cidutil.InlineBuilder{
294+
Builder: fileAdder.CidBuilder,
295+
Limit: inlineLimit,
296+
}
297+
}
284298

285299
if hash {
286300
md := dagtest.Mock()

test/sharness/t0040-add-and-cat.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ test_add_cat_expensive "--cid-version=1" "zdj7WcatQrtuE4WMkS4XsfsMixuQN2po4irkYh
590590
# encoded with the blake2b-256 hash funtion
591591
test_add_cat_expensive '--hash=blake2b-256' "zDMZof1kwndounDzQCANUHjiE3zt1mPEgx7RE3JTHoZrRRa79xcv"
592592

593-
test_add_named_pipe " Post http://$API_ADDR/api/v0/add?chunker=size-262144&encoding=json&hash=sha2-256&pin=true&progress=true&recursive=true&stream-channels=true:"
593+
test_add_named_pipe " Post http://$API_ADDR/api/v0/add?chunker=size-262144&encoding=json&hash=sha2-256&inline-limit=32&pin=true&progress=true&recursive=true&stream-channels=true:"
594594

595595
test_add_pwd_is_symlink
596596

test/sharness/t0046-id-hash.sh

+35
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,41 @@ test_expect_success "can still fetch it" '
4343
test_cmp junk.txt actual
4444
'
4545

46+
test_expect_success "ipfs add --inline works as expected" '
47+
echo $ID_HASH0_CONTENTS > afile &&
48+
HASH=$(ipfs add -q --inline afile)
49+
'
50+
51+
test_expect_success "ipfs add --inline uses id multihash" '
52+
MHTYPE=`cid-fmt %h $HASH`
53+
echo "mhtype is $MHTYPE"
54+
test "$MHTYPE" = id
55+
'
56+
57+
test_expect_success "ipfs add --inline --raw-leaves works as expected" '
58+
echo $ID_HASH0_CONTENTS > afile &&
59+
HASH=$(ipfs add -q --inline --raw-leaves afile)
60+
'
61+
62+
test_expect_success "ipfs add --inline --raw-leaves outputs the correct hash" '
63+
echo "$ID_HASH0" = "$HASH" &&
64+
test "$ID_HASH0" = "$HASH"
65+
'
66+
67+
test_expect_success "create 1000 bytes file and get its hash" '
68+
random 1000 2 > 1000bytes &&
69+
HASH0=$(ipfs add -q --raw-leaves --only-hash 1000bytes)
70+
'
71+
72+
test_expect_success "ipfs add --inline --raw-leaves works as expected on large file" '
73+
HASH=$(ipfs add -q --inline --raw-leaves 1000bytes)
74+
'
75+
76+
test_expect_success "ipfs add --inline --raw-leaves outputs the correct hash on large file" '
77+
echo "$HASH0" = "$HASH" &&
78+
test "$HASH0" = "$HASH"
79+
'
80+
4681
test_expect_success "enable filestore" '
4782
ipfs config --json Experimental.FilestoreEnabled true
4883
'

0 commit comments

Comments
 (0)