From 349c0d556c4e20d3c65d9ae37f861b783b0816ab Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Thu, 9 Feb 2023 15:18:10 +0100 Subject: [PATCH 1/3] fix: revert wire format for /api/v0/routing/get --- core/commands/routing.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/core/commands/routing.go b/core/commands/routing.go index d0f13c556af..88e8215849a 100644 --- a/core/commands/routing.go +++ b/core/commands/routing.go @@ -2,6 +2,7 @@ package commands import ( "context" + "encoding/base64" "errors" "fmt" "io" @@ -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{ From 73c5ba86fa675b4e84ab84f7f216ccd3f68d986c Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Thu, 9 Feb 2023 16:47:04 +0100 Subject: [PATCH 2/3] restore put behaviour and test for get --- core/commands/routing.go | 32 +++++++++++++++++++++++++----- test/sharness/t0170-routing-dht.sh | 24 +++++++++++++--------- 2 files changed, 42 insertions(+), 14 deletions(-) diff --git a/core/commands/routing.go b/core/commands/routing.go index 88e8215849a..460abb97ae4 100644 --- a/core/commands/routing.go +++ b/core/commands/routing.go @@ -442,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 diff --git a/test/sharness/t0170-routing-dht.sh b/test/sharness/t0170-routing-dht.sh index 85462dcc22c..bcd41ed0d83 100755 --- a/test/sharness/t0170-routing-dht.sh +++ b/test/sharness/t0170-routing-dht.sh @@ -24,14 +24,14 @@ test_dht() { PEERID_0=$(iptb attr get 0 id) && PEERID_2=$(iptb attr get 2 id) ' - + # ipfs routing findpeer test_expect_success 'findpeer' ' ipfsi 1 routing findpeer $PEERID_0 | sort >actual && ipfsi 0 id -f "" | cut -d / -f 1-5 | sort >expected && test_cmp actual expected ' - + # ipfs routing get test_expect_success 'get with good keys works' ' HASH="$(echo "hello world" | ipfsi 2 add -q)" && @@ -48,7 +48,7 @@ test_dht() { [ -s putted ] || test_fsh cat putted ' - + test_expect_success 'put with bad keys fails (issue #5113)' ' ipfsi 0 routing put "foo" <<putted ipfsi 0 routing put "/pk/foo" <<>putted @@ -56,31 +56,37 @@ test_dht() { [ ! -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" << afile && HASH=$(ipfsi 3 add -q afile) ' - + # ipfs routing findprovs 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 # # We test all nodes. 4 nodes should see the same peer ID, one node (the From 20173ac4bb1600ef5b4b7634e8a6c7f9b1a98127 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Thu, 9 Feb 2023 16:49:03 +0100 Subject: [PATCH 3/3] fix quotes --- test/sharness/t0170-routing-dht.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/sharness/t0170-routing-dht.sh b/test/sharness/t0170-routing-dht.sh index bcd41ed0d83..e6e9940f28a 100755 --- a/test/sharness/t0170-routing-dht.sh +++ b/test/sharness/t0170-routing-dht.sh @@ -84,7 +84,7 @@ test_dht() { 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")' + ipfsi 1 routing get --enc=json "/ipns/$PEERID_2" | jq -e "has(\"Extra\") and has(\"Type\")" ' # ipfs dht query