@cryptotaxi247 / kubo / commits / 4bbd00185

adder: add support for specifying the hash function

License: MIT Signed-off-by: Kevin Atkinson <k@kevina.org>

Kevin Atkinson committed May 13, 2017 at 01:51 UTC 4bbd00185c6c7cb42e63a1b3f04dcc0c52d6ccea
2 files changed +36
core/commands/add.go
+18
@@ -4,6 +4,7 @@ import (
4 "errors"
5 "fmt"
6 "io"
7 + "strings"
8
9 bstore "github.com/ipfs/go-ipfs/blocks/blockstore"
10 blockservice "github.com/ipfs/go-ipfs/blockservice"
@@ -17,6 +18,7 @@ import (
18 mfs "github.com/ipfs/go-ipfs/mfs"
19 ft "github.com/ipfs/go-ipfs/unixfs"
20
21 + mh "gx/ipfs/QmVGtdTZdTFaLsaj2RwdVG8jcjNNcp1DE914DKZ2kHmXHw/go-multihash"
22 u "gx/ipfs/QmWbjfz3u6HkAdPh34dgPchGbQjob6LXLhAeCGii2TX69n/go-ipfs-util"
23 "gx/ipfs/QmeWjRodbcZFKe5tMN7poEx3izym6osrLSnTLf9UjJZBbs/pb"
24 )
@@ -39,6 +41,7 @@ const (
41 noCopyOptionName = "nocopy"
42 fstoreCacheOptionName = "fscache"
43 cidVersionOptionName = "cid-version"
44 + hashOptionName = "hash"
45 )
46
47 const adderOutChanSize = 8
@@ -90,6 +93,7 @@ You can now refer to the added file in a gateway, like so:
93 cmds.BoolOption(noCopyOptionName, "Add the file using filestore. (experimental)"),
94 cmds.BoolOption(fstoreCacheOptionName, "Check the filestore for pre-existing blocks. (experimental)"),
95 cmds.IntOption(cidVersionOptionName, "Cid version. Non-zero value will change default of 'raw-leaves' to true. (experimental)").Default(0),
96 + cmds.StringOption(hashOptionName, "Hash function to use. Will set Cid version to 1 if used. (experimental)").Default("sha2-256"),
97 },
98 PreRun: func(req cmds.Request) error {
99 quiet, _, _ := req.Option(quietOptionName).Bool()
@@ -164,6 +168,7 @@ You can now refer to the added file in a gateway, like so:
168 nocopy, _, _ := req.Option(noCopyOptionName).Bool()
169 fscache, _, _ := req.Option(fstoreCacheOptionName).Bool()
170 cidVer, _, _ := req.Option(cidVersionOptionName).Int()
171 + hashFunStr, hfset, _ := req.Option(hashOptionName).String()
172
173 if nocopy && !cfg.Experimental.FilestoreEnabled {
174 res.SetError(errors.New("filestore is not enabled, see https://git.io/vy4XN"),
@@ -180,6 +185,10 @@ You can now refer to the added file in a gateway, like so:
185 return
186 }
187
188 + if hfset && cidVer == 0 {
189 + cidVer = 1
190 + }
191 +
192 if cidVer >= 1 && !rbset {
193 rawblks = true
194 }
@@ -190,6 +199,15 @@ You can now refer to the added file in a gateway, like so:
199 return
200 }
201
202 + hashFunCode, ok := mh.Names[strings.ToLower(hashFunStr)]
203 + if !ok {
204 + res.SetError(fmt.Errorf("unrecognized hash function: %s", strings.ToLower(hashFunStr)), cmds.ErrNormal)
205 + return
206 + }
207 +
208 + prefix.MhType = hashFunCode
209 + prefix.MhLength = -1
210 +
211 if hash {
212 nilnode, err := core.NewNode(n.Context(), &core.BuildCfg{
213 //TODO: need this to be true or all files
test/sharness/t0040-add-and-cat.sh
+18
@@ -403,6 +403,10 @@ MARS="zdj7WWx6fGNrNGkdpkuTAxCjKbQ1pPtarqA6VQhedhLTZu34J"
403 VENUS="zdj7WbB1BUF8WejmVpQCmMLd1RbPnxJtvAj1Lep6eTmXRFbrz"
404 add_directory '--cid-version=1 --raw-leaves=false'
405
406 +PLANETS="zDMZof1kqxDAx9myQbXsyWwyWP9qRPsXsWH7XuTz6isT7Rh1S6nM"
407 +MARS="zCT5htkdz1ZBHYVQXFQn51ngPXLVqaHSWoae87V1d6e9qWpSAjXw"
408 +VENUS="zCT5htke5JcdoMM4WhmUKXWf2QC3TnQToqGZHH1WsZERv6kPhFPg"
409 +add_directory '--hash=blake2b-256'
410
411 test_expect_success "'ipfs add -rn' succeeds" '
412 mkdir -p mountdir/moons/jupiter &&
@@ -446,12 +450,26 @@ test_add_cat_5MB --cid-version=1 "zdj7WiiaedqVBXjX4SNqx3jfuZideDqdLYnDzCDJ66JDMK
450 # format (i.e. not raw)
451 test_add_cat_5MB '--cid-version=1 --raw-leaves=false' "zdj7WfgEsj897BBZj2mcfsRLhaPZcCixPV2G7DkWgF1Wdr64P"
452
453 +# note: --hash=blake2b-256 implies --cid-version=1 which implies --raw-leaves=true
454 +# the specified hash represents the leaf nodes stored as raw leaves and
455 +# encoded with the blake2b-256 hash funtion
456 +test_add_cat_5MB '--hash=blake2b-256' "zDMZof1kuxn7ebvKyvmkYLPvocSvFYxxAWT1yQBN1wWiXXr7w5mY"
457 +
458 +# the specified hash represents the leaf nodes stored as protoful nodes and
459 +# encoded with the blake2b-256 hash funtion
460 +test_add_cat_5MB '--hash=blake2b-256 --raw-leaves=false' "zDMZof1krz3SFTyhboRyWZyUP2qNgVdn9wjtaX211aHJ8WgeyT9v"
461 +
462 test_add_cat_expensive "" "QmU9SWAPPmNEKZB8umYMmjYvN7VyHqABNvdA6GUi4MMEz3"
463
464 # note: the specified hash implies that internal nodes are stored
465 # using CidV1 and leaves are stored using raw blocks
466 test_add_cat_expensive "--cid-version=1" "zdj7WcatQrtuE4WMkS4XsfsMixuQN2po4irkYhqxeJyW1wgCq"
467
468 +# note: --hash=blake2b-256 implies --cid-version=1 which implies --raw-leaves=true
469 +# the specified hash represents the leaf nodes stored as raw leaves and
470 +# encoded with the blake2b-256 hash funtion
471 +test_add_cat_expensive '--hash=blake2b-256' "zDMZof1kwndounDzQCANUHjiE3zt1mPEgx7RE3JTHoZrRRa79xcv"
472 +
473 test_add_named_pipe " Post http://$API_ADDR/api/v0/add?encoding=json&progress=true&r=true&stream-channels=true:"
474
475 test_add_pwd_is_symlink