fix: restore wire format for /api/v0/routing/get|put (#9639)
Closes #9638
Henrique Dias committed
Feb 10, 2023 at 02:41 UTC
fb7f7b15b39cfc6f026bf0d518a669005be94709
2 files changed
+54
-18
core/commands/routing.go
+39
-9
@@ -2,6 +2,7 @@ package commands
2
3
import (
4
"context"
5
+ "encoding/base64"
6
"errors"
7
"fmt"
8
"io"
@@ -374,15 +375,22 @@ Different key types can specify other 'best' rules.
375
return err
376
}
377
377
- return res.Emit(r)
378
+ return res.Emit(routing.QueryEvent{
379
+ Extra: base64.StdEncoding.EncodeToString(r),
380
+ Type: routing.Value,
381
+ })
382
},
383
Encoders: cmds.EncoderMap{
380
- cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out []byte) error {
381
- _, err := w.Write(out)
384
+ cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, obj *routing.QueryEvent) error {
385
+ res, err := base64.StdEncoding.DecodeString(obj.Extra)
386
+ if err != nil {
387
+ return err
388
+ }
389
+ _, err = w.Write(res)
390
return err
391
}),
392
},
385
- Type: []byte{},
393
+ Type: routing.QueryEvent{},
394
}
395
396
var putValueRoutingCmd = &cmds.Command{
@@ -434,15 +442,37 @@ identified by QmFoo.
442
return err
443
}
444
437
- return res.Emit([]byte(fmt.Sprintf("%s added", req.Arguments[0])))
445
+ id, err := api.Key().Self(req.Context)
446
+ if err != nil {
447
+ return err
448
+ }
449
+
450
+ return res.Emit(routing.QueryEvent{
451
+ Type: routing.Value,
452
+ ID: id.ID(),
453
+ })
454
},
455
Encoders: cmds.EncoderMap{
440
- cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out []byte) error {
441
- _, err := w.Write(out)
442
- return err
456
+ cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *routing.QueryEvent) error {
457
+ pfm := pfuncMap{
458
+ routing.FinalPeer: func(obj *routing.QueryEvent, out io.Writer, verbose bool) error {
459
+ if verbose {
460
+ fmt.Fprintf(out, "* closest peer %s\n", obj.ID)
461
+ }
462
+ return nil
463
+ },
464
+ routing.Value: func(obj *routing.QueryEvent, out io.Writer, verbose bool) error {
465
+ fmt.Fprintf(out, "%s\n", obj.ID.Pretty())
466
+ return nil
467
+ },
468
+ }
469
+
470
+ verbose, _ := req.Options[dhtVerboseOptionName].(bool)
471
+
472
+ return printEvent(out, w, verbose, pfm)
473
}),
474
},
445
- Type: []byte{},
475
+ Type: routing.QueryEvent{},
476
}
477
478
type printFunc func(obj *routing.QueryEvent, out io.Writer, verbose bool) error
test/sharness/t0170-routing-dht.sh
+15
-9
@@ -24,14 +24,14 @@ test_dht() {
24
PEERID_0=$(iptb attr get 0 id) &&
25
PEERID_2=$(iptb attr get 2 id)
26
'
27
-
27
+
28
# ipfs routing findpeer <peerID>
29
test_expect_success 'findpeer' '
30
ipfsi 1 routing findpeer $PEERID_0 | sort >actual &&
31
ipfsi 0 id -f "<addrs>" | cut -d / -f 1-5 | sort >expected &&
32
test_cmp actual expected
33
'
34
-
34
+
35
# ipfs routing get <key>
36
test_expect_success 'get with good keys works' '
37
HASH="$(echo "hello world" | ipfsi 2 add -q)" &&
@@ -48,7 +48,7 @@ test_dht() {
48
[ -s putted ] ||
49
test_fsh cat putted
50
'
51
-
51
+
52
test_expect_success 'put with bad keys fails (issue #5113)' '
53
ipfsi 0 routing put "foo" <<<bar >putted
54
ipfsi 0 routing put "/pk/foo" <<<bar >>putted
@@ -56,31 +56,37 @@ test_dht() {
56
[ ! -s putted ] ||
57
test_fsh cat putted
58
'
59
-
59
+
60
test_expect_success 'put with bad keys returns error (issue #4611)' '
61
test_must_fail ipfsi 0 routing put "foo" <<<bar &&
62
test_must_fail ipfsi 0 routing put "/pk/foo" <<<bar &&
63
test_must_fail ipfsi 0 routing put "/ipns/foo" <<<bar
64
'
65
-
65
+
66
test_expect_success 'get with bad keys (issue #4611)' '
67
test_must_fail ipfsi 0 routing get "foo" &&
68
test_must_fail ipfsi 0 routing get "/pk/foo"
69
'
70
-
70
+
71
test_expect_success "add a ref so we can find providers for it" '
72
echo "some stuff" > afile &&
73
HASH=$(ipfsi 3 add -q afile)
74
'
75
-
75
+
76
# ipfs routing findprovs <key>
77
test_expect_success 'findprovs' '
78
ipfsi 4 routing findprovs $HASH > provs &&
79
iptb attr get 3 id > expected &&
80
test_cmp provs expected
81
'
82
-
83
-
82
+
83
+ # ipfs routing get --enc=json has correct properties
84
+ test_expect_success 'routing get --enc=json has correct properties' '
85
+ HASH="$(echo "hello world" | ipfsi 2 add -q)" &&
86
+ ipfsi 2 name publish "/ipfs/$HASH" &&
87
+ ipfsi 1 routing get --enc=json "/ipns/$PEERID_2" | jq -e "has(\"Extra\") and has(\"Type\")"
88
+ '
89
+
90
# ipfs dht query <peerID>
91
#
92
# We test all nodes. 4 nodes should see the same peer ID, one node (the