Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve documentation and fix dht put bug #6750

Merged
merged 3 commits into from
Nov 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 17 additions & 13 deletions core/commands/dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"time"

cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
Expand Down Expand Up @@ -515,8 +516,8 @@ var putValueDhtCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Write a key/value pair to the routing system.",
ShortDescription: `
Given a key of the form /foo/bar and a value of any form, this will write that
value to the routing system with that key.
Given a key of the form /foo/bar and a valid value for that key, this will write
that value to the routing system with that key.

Keys have two parts: a keytype (foo) and the key name (bar). IPNS uses the
/ipns keytype, and expects the key name to be a Peer ID. IPNS entries are
Expand All @@ -527,15 +528,15 @@ this is only /ipns. Unless you have a relatively deep understanding of the
go-ipfs routing internals, you likely want to be using 'ipfs name publish' instead
of this.

Value is arbitrary text. Standard input can be used to provide value.

NOTE: A value may not exceed 2048 bytes.
The value must be a valid value for the given key type. For example, if the key
is /ipns/QmFoo, the value must be IPNS record (protobuf) signed with the key
identified by QmFoo.
`,
},

Arguments: []cmds.Argument{
cmds.StringArg("key", true, false, "The key to store the value at."),
cmds.StringArg("value", true, false, "The value to store.").EnableStdin(),
cmds.FileArg("value", true, false, "The value to store.").EnableStdin(),
},
Options: []cmds.Option{
cmds.BoolOption(dhtVerboseOptionName, "v", "Print extra information."),
Expand All @@ -546,12 +547,6 @@ NOTE: A value may not exceed 2048 bytes.
return err
}

// Needed to parse stdin args.
err = req.ParseBodyArgs()
if err != nil {
return err
}

if !nd.IsOnline {
return ErrNotOnline
}
Expand All @@ -561,7 +556,16 @@ NOTE: A value may not exceed 2048 bytes.
return err
}

data := req.Arguments[1]
file, err := cmdenv.GetFileArg(req.Files.Entries())
if err != nil {
return err
}
defer file.Close()

data, err := ioutil.ReadAll(file)
if err != nil {
return err
}

ctx, cancel := context.WithCancel(req.Context)
ctx, events := routing.RegisterForQueryEvents(ctx)
Expand Down
44 changes: 19 additions & 25 deletions test/sharness/t0170-dht.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ test_description="Test dht command"

. lib/test-lib.sh

TEST_DHT_VALUE="foobar"
TEST_DHT_PATH="/pk/QmbWTwYGcmdyK9CYfNBcfs9nhZs17a6FQ4Y8oea278xx41"

test_dht() {
NUM_NODES=5

Expand All @@ -29,38 +26,35 @@ test_dht() {
test_cmp actual expected
'

# ipfs dht put <key> <value>
test_expect_failure 'put with good keys (#3124)' '
ipfsi 0 dht put "$TEST_DHT_PATH" "$TEST_DHT_VALUE" | sort >putted &&
[ -s putted ] ||
test_fsh cat putted
'

test_expect_failure 'put round trips (#3124)' '
echo -n "$TEST_DHT_VALUE" >expected &&
ipfsi 0 dht get "$TEST_DHT_PATH" >actual &&
test_cmp actual expected
'

# ipfs dht get <key>
test_expect_success 'get with good keys' '
test_expect_success 'get with good keys works' '
HASH="$(echo "hello world" | ipfsi 2 add -q)" &&
ipfsi 2 name publish "/ipfs/$HASH" &&
ipfsi 1 dht get "/ipns/$PEERID_2" | grep -aq "/ipfs/$HASH"
ipfsi 1 dht get "/ipns/$PEERID_2" >get_result
'

test_expect_success 'get with good keys contains the right value' '
cat get_result | grep -aq "/ipfs/$HASH"
'

test_expect_success 'put round trips (#3124)' '
ipfsi 0 dht put "/ipns/$PEERID_2" get_result | sort >putted &&
[ -s putted ] ||
test_fsh cat putted
'

test_expect_success 'put with bad keys fails (issue #5113)' '
ipfsi 0 dht put "foo" "bar" >putted
ipfsi 0 dht put "/pk/foo" "bar" >>putted
ipfsi 0 dht put "/ipns/foo" "bar" >>putted
ipfsi 0 dht put "foo" <<<bar >putted
ipfsi 0 dht put "/pk/foo" <<<bar >>putted
ipfsi 0 dht put "/ipns/foo" <<<bar >>putted
[ ! -s putted ] ||
test_fsh cat putted
'

test_expect_success 'put with bad keys returns error (issue #4611)' '
test_must_fail ipfsi 0 dht put "foo" "bar" &&
test_must_fail ipfsi 0 dht put "/pk/foo" "bar" &&
test_must_fail ipfsi 0 dht put "/ipns/foo" "bar"
test_must_fail ipfsi 0 dht put "foo" <<<bar &&
test_must_fail ipfsi 0 dht put "/pk/foo" <<<bar &&
test_must_fail ipfsi 0 dht put "/ipns/foo" <<<bar
'

test_expect_success 'get with bad keys (issue #4611)' '
Expand Down Expand Up @@ -101,7 +95,7 @@ test_dht() {
test_expect_success "dht commands fail when offline" '
test_must_fail ipfsi 0 dht findprovs "$HASH" 2>err_findprovs &&
test_must_fail ipfsi 0 dht findpeer "$HASH" 2>err_findpeer &&
test_must_fail ipfsi 0 dht put "$TEST_DHT_PATH" "$TEST_DHT_VALUE" 2>err_put &&
test_must_fail ipfsi 0 dht put "/ipns/$PEERID_2" "get_result" 2>err_put &&
test_should_contain "this command must be run in online mode" err_findprovs &&
test_should_contain "this command must be run in online mode" err_findpeer &&
test_should_contain "this command must be run in online mode" err_put
Expand Down