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

fix: restore wire format for /api/v0/routing/get #9639

Merged
merged 3 commits into from
Feb 10, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 39 additions & 9 deletions core/commands/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package commands

import (
"context"
"encoding/base64"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -374,15 +375,22 @@ Different key types can specify other 'best' rules.
return err
}

return res.Emit(r)
return res.Emit(routing.QueryEvent{
Extra: base64.StdEncoding.EncodeToString(r),
Type: routing.Value,
})
},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out []byte) error {
_, err := w.Write(out)
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, obj *routing.QueryEvent) error {
res, err := base64.StdEncoding.DecodeString(obj.Extra)
if err != nil {
return err
}
_, err = w.Write(res)
return err
}),
},
Type: []byte{},
Type: routing.QueryEvent{},
}

var putValueRoutingCmd = &cmds.Command{
Expand Down Expand Up @@ -434,15 +442,37 @@ identified by QmFoo.
return err
}

return res.Emit([]byte(fmt.Sprintf("%s added", req.Arguments[0])))
id, err := api.Key().Self(req.Context)
if err != nil {
return err
}

return res.Emit(routing.QueryEvent{
Type: routing.Value,
ID: id.ID(),
})
},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out []byte) error {
_, err := w.Write(out)
return err
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *routing.QueryEvent) error {
pfm := pfuncMap{
routing.FinalPeer: func(obj *routing.QueryEvent, out io.Writer, verbose bool) error {
if verbose {
fmt.Fprintf(out, "* closest peer %s\n", obj.ID)
}
return nil
},
routing.Value: func(obj *routing.QueryEvent, out io.Writer, verbose bool) error {
fmt.Fprintf(out, "%s\n", obj.ID.Pretty())
return nil
},
}

verbose, _ := req.Options[dhtVerboseOptionName].(bool)

return printEvent(out, w, verbose, pfm)
}),
},
Type: []byte{},
Type: routing.QueryEvent{},
}

type printFunc func(obj *routing.QueryEvent, out io.Writer, verbose bool) error
Expand Down
24 changes: 15 additions & 9 deletions test/sharness/t0170-routing-dht.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ test_dht() {
PEERID_0=$(iptb attr get 0 id) &&
PEERID_2=$(iptb attr get 2 id)
'

# ipfs routing findpeer <peerID>
test_expect_success 'findpeer' '
ipfsi 1 routing findpeer $PEERID_0 | sort >actual &&
ipfsi 0 id -f "<addrs>" | cut -d / -f 1-5 | sort >expected &&
test_cmp actual expected
'

# ipfs routing get <key>
test_expect_success 'get with good keys works' '
HASH="$(echo "hello world" | ipfsi 2 add -q)" &&
Expand All @@ -48,39 +48,45 @@ test_dht() {
[ -s putted ] ||
test_fsh cat putted
'

test_expect_success 'put with bad keys fails (issue #5113)' '
ipfsi 0 routing put "foo" <<<bar >putted
ipfsi 0 routing put "/pk/foo" <<<bar >>putted
ipfsi 0 routing 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 routing put "foo" <<<bar &&
test_must_fail ipfsi 0 routing put "/pk/foo" <<<bar &&
test_must_fail ipfsi 0 routing put "/ipns/foo" <<<bar
'

test_expect_success 'get with bad keys (issue #4611)' '
test_must_fail ipfsi 0 routing get "foo" &&
test_must_fail ipfsi 0 routing get "/pk/foo"
'

test_expect_success "add a ref so we can find providers for it" '
echo "some stuff" > afile &&
HASH=$(ipfsi 3 add -q afile)
'

# ipfs routing findprovs <key>
test_expect_success 'findprovs' '
ipfsi 4 routing findprovs $HASH > provs &&
iptb attr get 3 id > expected &&
test_cmp provs expected
'



# ipfs routing get --enc=json has correct properties
test_expect_success 'routing get --enc=json has correct properties' '
HASH="$(echo "hello world" | ipfsi 2 add -q)" &&
ipfsi 2 name publish "/ipfs/$HASH" &&
ipfsi 1 routing get --enc=json "/ipns/$PEERID_2" | jq -e "has(\"Extra\") and has(\"Type\")"
'

# ipfs dht query <peerID>
#
# We test all nodes. 4 nodes should see the same peer ID, one node (the
Expand Down