Skip to content

Commit

Permalink
fix: restore wire format for /api/v0/routing/get|put (#9639)
Browse files Browse the repository at this point in the history
Closes #9638
  • Loading branch information
hacdias authored Feb 10, 2023
1 parent 82ede56 commit fb7f7b1
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 18 deletions.
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

0 comments on commit fb7f7b1

Please sign in to comment.