From cdc1c837f736321c3c59245c723820f9e560e173 Mon Sep 17 00:00:00 2001 From: Kevin Atkinson Date: Fri, 12 May 2017 15:06:08 -0400 Subject: [PATCH] adder: add support for specifying the hash function License: MIT Signed-off-by: Kevin Atkinson --- core/commands/add.go | 18 ++++++++++++++++++ test/sharness/t0040-add-and-cat.sh | 13 +++++++++++++ 2 files changed, 31 insertions(+) diff --git a/core/commands/add.go b/core/commands/add.go index a16817db9353..7a6b61003403 100644 --- a/core/commands/add.go +++ b/core/commands/add.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "io" + "strings" bstore "github.com/ipfs/go-ipfs/blocks/blockstore" blockservice "github.com/ipfs/go-ipfs/blockservice" @@ -17,6 +18,7 @@ import ( mfs "github.com/ipfs/go-ipfs/mfs" ft "github.com/ipfs/go-ipfs/unixfs" + mh "gx/ipfs/QmVGtdTZdTFaLsaj2RwdVG8jcjNNcp1DE914DKZ2kHmXHw/go-multihash" u "gx/ipfs/QmWbjfz3u6HkAdPh34dgPchGbQjob6LXLhAeCGii2TX69n/go-ipfs-util" "gx/ipfs/QmeWjRodbcZFKe5tMN7poEx3izym6osrLSnTLf9UjJZBbs/pb" ) @@ -39,6 +41,7 @@ const ( noCopyOptionName = "nocopy" fstoreCacheOptionName = "fscache" cidVersionOptionName = "cid-version" + hashOptionName = "hash" ) const adderOutChanSize = 8 @@ -90,6 +93,7 @@ You can now refer to the added file in a gateway, like so: cmds.BoolOption(noCopyOptionName, "Add the file using filestore. (experimental)"), cmds.BoolOption(fstoreCacheOptionName, "Check the filestore for pre-existing blocks. (experimental)"), cmds.IntOption(cidVersionOptionName, "Cid version. Non-zero value will change default of 'raw-leaves' to true. (experimental)").Default(0), + cmds.StringOption(hashOptionName, "Hash function to use. Will set Cid version to 1 if used. (experimental)").Default("sha2-256"), }, PreRun: func(req cmds.Request) error { quiet, _, _ := req.Option(quietOptionName).Bool() @@ -164,6 +168,7 @@ You can now refer to the added file in a gateway, like so: nocopy, _, _ := req.Option(noCopyOptionName).Bool() fscache, _, _ := req.Option(fstoreCacheOptionName).Bool() cidVer, _, _ := req.Option(cidVersionOptionName).Int() + hashFunStr, hfset, _ := req.Option(hashOptionName).String() if nocopy && !cfg.Experimental.FilestoreEnabled { 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: return } + if hfset && cidVer == 0 { + cidVer = 1 + } + if cidVer >= 1 && !rbset { rawblks = true } @@ -190,6 +199,15 @@ You can now refer to the added file in a gateway, like so: return } + hashFunCode, ok := mh.Names[strings.ToLower(hashFunStr)] + if !ok { + res.SetError(fmt.Errorf("unrecognized hash function: %s", strings.ToLower(hashFunStr)), cmds.ErrNormal) + return + } + + prefix.MhType = hashFunCode + prefix.MhLength = -1 + if hash { nilnode, err := core.NewNode(n.Context(), &core.BuildCfg{ //TODO: need this to be true or all files diff --git a/test/sharness/t0040-add-and-cat.sh b/test/sharness/t0040-add-and-cat.sh index 4b2d636a4805..5f9c703cf2e3 100755 --- a/test/sharness/t0040-add-and-cat.sh +++ b/test/sharness/t0040-add-and-cat.sh @@ -403,6 +403,10 @@ MARS="zdj7WWx6fGNrNGkdpkuTAxCjKbQ1pPtarqA6VQhedhLTZu34J" VENUS="zdj7WbB1BUF8WejmVpQCmMLd1RbPnxJtvAj1Lep6eTmXRFbrz" add_directory '--cid-version=1 --raw-leaves=false' +#PLANETS="???" +#MARS="???" +#VENUS="???" +#add_directory '--hash=blake2b-256' test_expect_success "'ipfs add -rn' succeeds" ' mkdir -p mountdir/moons/jupiter && @@ -446,6 +450,15 @@ test_add_cat_5MB --cid-version=1 "zdj7WiiaedqVBXjX4SNqx3jfuZideDqdLYnDzCDJ66JDMK # format (i.e. not raw) test_add_cat_5MB '--cid-version=1 --raw-leaves=false' "zdj7WfgEsj897BBZj2mcfsRLhaPZcCixPV2G7DkWgF1Wdr64P" +# note: --hash=blake2b-256 implies --cid-version=1 which implies --raw-leaves=true +# the specified hash represents the leaf nodes stored as raw leaves and +# encoded with the blake2b-256 hash funtion +test_add_cat_5MB '--hash=blake2b-256' "zDMZof1kuxn7ebvKyvmkYLPvocSvFYxxAWT1yQBN1wWiXXr7w5mY" + +# the specified hash represents the leaf nodes stored as protoful nodes and +# encoded with the blake2b-256 hash funtion +test_add_cat_5MB '--hash=blake2b-256 --raw-leaves=false' "zDMZof1krz3SFTyhboRyWZyUP2qNgVdn9wjtaX211aHJ8WgeyT9v" + test_add_cat_expensive "" "QmU9SWAPPmNEKZB8umYMmjYvN7VyHqABNvdA6GUi4MMEz3" # note: the specified hash implies that internal nodes are stored