@cryptotaxi247 / kubo / commits / d0d508b2e

fix: fix 'dht query' command to actually return the closest peers

Then improve the tests such that they actually work.

Steven Allen committed Apr 13, 2020 at 17:42 UTC d0d508b2e1ec7e6a51ca78f6d10ee0c7b560efea
2 files changed +36 -26
core/commands/dht.go
+18 -16
@@ -81,19 +81,23 @@ var queryDhtCmd = &cmds.Command{
81 dht = nd.DHT.LAN
82 }
83
84 - closestPeers, err := dht.GetClosestPeers(ctx, string(id))
85 - if err != nil {
86 - cancel()
87 - return err
88 - }
89 -
84 + errCh := make(chan error, 1)
85 go func() {
86 + defer close(errCh)
87 defer cancel()
92 - for p := range closestPeers {
93 - routing.PublishQueryEvent(ctx, &routing.QueryEvent{
94 - ID: p,
95 - Type: routing.FinalPeer,
96 - })
88 + closestPeers, err := dht.GetClosestPeers(ctx, string(id))
89 + if closestPeers != nil {
90 + for p := range closestPeers {
91 + routing.PublishQueryEvent(ctx, &routing.QueryEvent{
92 + ID: p,
93 + Type: routing.FinalPeer,
94 + })
95 + }
96 + }
97 +
98 + if err != nil {
99 + errCh <- err
100 + return
101 }
102 }()
103
@@ -103,15 +107,13 @@ var queryDhtCmd = &cmds.Command{
107 }
108 }
109
106 - return nil
110 + return <-errCh
111 },
112 Encoders: cmds.EncoderMap{
113 cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *routing.QueryEvent) error {
114 pfm := pfuncMap{
111 - routing.PeerResponse: func(obj *routing.QueryEvent, out io.Writer, verbose bool) error {
112 - for _, p := range obj.Responses {
113 - fmt.Fprintf(out, "%s\n", p.ID.Pretty())
114 - }
115 + routing.FinalPeer: func(obj *routing.QueryEvent, out io.Writer, verbose bool) error {
116 + fmt.Fprintf(out, "%s\n", obj.ID)
117 return nil
118 },
119 }
test/sharness/t0170-dht.sh
+18 -10
@@ -76,16 +76,24 @@ test_dht() {
76
77
78 # ipfs dht query <peerID>
79 - ## We query 3 different keys, to statisically lower the chance that the queryer
80 - ## turns out to be the closest to what a key hashes to.
81 - # TODO: flaky. tracked by https://github.com/ipfs/go-ipfs/issues/2620
82 - test_expect_success 'query' '
83 - ipfsi 3 dht query "$(echo banana | ipfsi 3 add -q)" >actual &&
84 - ipfsi 3 dht query "$(echo apple | ipfsi 3 add -q)" >>actual &&
85 - ipfsi 3 dht query "$(echo pear | ipfsi 3 add -q)" >>actual &&
86 - PEERS=$(wc -l actual | cut -d '"'"' '"'"' -f 1) &&
87 - [ -s actual ] ||
88 - test_might_fail test_fsh cat actual
79 + #
80 + # We test all nodes. 4 nodes should see the same peer ID, one node (the
81 + # closest) should see a different one.
82 +
83 + for i in $(test_seq 0 4); do
84 + test_expect_success "query from $i" '
85 + ipfsi "$i" dht query "$HASH" | head -1 >closest-$i
86 + '
87 + done
88 +
89 + test_expect_success "collecting results" '
90 + cat closest-* | sort | uniq -c | sed -e "s/ *\([0-9]\+\) .*/\1/g" | sort -g > actual &&
91 + echo 1 > expected &&
92 + echo 4 >> expected
93 + '
94 +
95 + test_expect_success "checking results" '
96 + test_cmp actual expected
97 '
98
99 test_expect_success 'stop iptb' '