Skip to content

Commit 0a8bc14

Browse files
committed
GetClosestPeers(): remove closer-than and timeout parameters.
This aligns it with latest version of the spec.
1 parent d53886a commit 0a8bc14

File tree

8 files changed

+55
-174
lines changed

8 files changed

+55
-174
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ require (
4040
github.com/libp2p/go-libp2p v0.43.0
4141
github.com/libp2p/go-libp2p-kad-dht v0.34.0
4242
github.com/libp2p/go-libp2p-record v0.3.1
43-
github.com/libp2p/go-libp2p-routing-helpers v0.7.6-0.20250903125449-17ee6fbf872c
43+
github.com/libp2p/go-libp2p-routing-helpers v0.7.6-0.20250925120012-fe2c8e446449
4444
github.com/libp2p/go-libp2p-testing v0.12.0
4545
github.com/libp2p/go-msgio v0.3.0
4646
github.com/miekg/dns v1.1.68

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,8 @@ github.com/libp2p/go-libp2p-kbucket v0.7.0 h1:vYDvRjkyJPeWunQXqcW2Z6E93Ywx7fX0jg
230230
github.com/libp2p/go-libp2p-kbucket v0.7.0/go.mod h1:blOINGIj1yiPYlVEX0Rj9QwEkmVnz3EP8LK1dRKBC6g=
231231
github.com/libp2p/go-libp2p-record v0.3.1 h1:cly48Xi5GjNw5Wq+7gmjfBiG9HCzQVkiZOUZ8kUl+Fg=
232232
github.com/libp2p/go-libp2p-record v0.3.1/go.mod h1:T8itUkLcWQLCYMqtX7Th6r7SexyUJpIyPgks757td/E=
233-
github.com/libp2p/go-libp2p-routing-helpers v0.7.6-0.20250903125449-17ee6fbf872c h1:oWvPNbSi3yoJMDe04qvICNpwrKoub+x0EDb3bjc5cxs=
234-
github.com/libp2p/go-libp2p-routing-helpers v0.7.6-0.20250903125449-17ee6fbf872c/go.mod h1:Q1VSaOawgsvaa3hGl/PejADIhl2deiqSEsQDpB3Ggss=
233+
github.com/libp2p/go-libp2p-routing-helpers v0.7.6-0.20250925120012-fe2c8e446449 h1:Rfa4ltusUBgkPpRBXQdGBLMAzzoBMb+76sVGOblunTg=
234+
github.com/libp2p/go-libp2p-routing-helpers v0.7.6-0.20250925120012-fe2c8e446449/go.mod h1:Q1VSaOawgsvaa3hGl/PejADIhl2deiqSEsQDpB3Ggss=
235235
github.com/libp2p/go-libp2p-testing v0.12.0 h1:EPvBb4kKMWO29qP4mZGyhVzUyR25dvfUIK5WDu6iPUA=
236236
github.com/libp2p/go-libp2p-testing v0.12.0/go.mod h1:KcGDRXyN7sQCllucn1cOOS+Dmm7ujhfEyXQL5lvkcPg=
237237
github.com/libp2p/go-msgio v0.3.0 h1:mf3Z8B1xcFN314sWX+2vOTShIE0Mmn2TXn3YCUQGNj0=

routing/http/client/client.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"net/http"
1212
gourl "net/url"
1313
"slices"
14-
"strconv"
1514
"strings"
1615
"time"
1716

@@ -641,7 +640,7 @@ func (c *Client) PutIPNS(ctx context.Context, name ipns.Name, record *ipns.Recor
641640
}
642641

643642
// GetClosestPeers obtains the closest peers to the given peer ID.
644-
func (c *Client) GetClosestPeers(ctx context.Context, peerID, closerThan peer.ID, count int) (peers iter.ResultIter[*types.PeerRecord], err error) {
643+
func (c *Client) GetClosestPeers(ctx context.Context, peerID peer.ID) (peers iter.ResultIter[*types.PeerRecord], err error) {
645644
m := newMeasurement("GetClosestPeers")
646645

647646
// Build the base URL path
@@ -650,16 +649,6 @@ func (c *Client) GetClosestPeers(ctx context.Context, peerID, closerThan peer.ID
650649
return nil, err
651650
}
652651

653-
// Add query parameters
654-
queryParams := make(gourl.Values)
655-
if closerThan != "" {
656-
queryParams.Set("closer-than", closerThan.String())
657-
}
658-
if count > 0 {
659-
queryParams.Set("count", strconv.Itoa(count))
660-
}
661-
u += "?" + queryParams.Encode()
662-
663652
// Create the HTTP request
664653
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil)
665654
if err != nil {

routing/http/client/client_test.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ func (m *mockContentRouter) FindPeers(ctx context.Context, pid peer.ID, limit in
4848
return args.Get(0).(iter.ResultIter[*types.PeerRecord]), args.Error(1)
4949
}
5050

51-
func (m *mockContentRouter) GetClosestPeers(ctx context.Context, peerID, closerThan peer.ID, count int) (iter.ResultIter[*types.PeerRecord], error) {
52-
args := m.Called(ctx, peerID, closerThan, count)
51+
func (m *mockContentRouter) GetClosestPeers(ctx context.Context, peerID peer.ID) (iter.ResultIter[*types.PeerRecord], error) {
52+
args := m.Called(ctx, peerID)
5353
return args.Get(0).(iter.ResultIter[*types.PeerRecord]), args.Error(1)
5454
}
5555

@@ -903,12 +903,6 @@ func TestClient_GetClosestPeers(t *testing.T) {
903903
httpStatusCode: 404,
904904
expResult: nil,
905905
},
906-
{
907-
name: "passes count and closerThan along",
908-
expStreamingResponse: true,
909-
routerResult: peerRecords,
910-
expResult: peerRecords,
911-
},
912906
}
913907
for _, c := range cases {
914908
t.Run(c.name, func(t *testing.T) {

routing/http/contentrouter/contentrouter.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ type Client interface {
3131
GetIPNS(ctx context.Context, name ipns.Name) (*ipns.Record, error)
3232
PutIPNS(ctx context.Context, name ipns.Name, record *ipns.Record) error
3333
// GetClosestPeers returns the DHT closest peers to the given peer ID.
34-
// If empty, it will use the content router's peer ID (self). `closerThan` (optional) forces resulting records to be closer to `PeerID` than to `closerThan`. `count` specifies how many records to return ([1,100], with 20 as default when set to 0).
35-
GetClosestPeers(ctx context.Context, peerID, closerThan peer.ID, count int) (iter.ResultIter[*types.PeerRecord], error)
34+
// If empty, it will use the content router's peer ID (self).
35+
GetClosestPeers(ctx context.Context, peerID peer.ID) (iter.ResultIter[*types.PeerRecord], error)
3636
}
3737

3838
type contentRouter struct {
@@ -308,8 +308,8 @@ func (c *contentRouter) SearchValue(ctx context.Context, key string, opts ...rou
308308
return ch, nil
309309
}
310310

311-
func (c *contentRouter) GetClosestPeers(ctx context.Context, pid, closerThan peer.ID, count int) (<-chan peer.AddrInfo, error) {
312-
iter, err := c.client.GetClosestPeers(ctx, pid, closerThan, count)
311+
func (c *contentRouter) GetClosestPeers(ctx context.Context, pid peer.ID) (<-chan peer.AddrInfo, error) {
312+
iter, err := c.client.GetClosestPeers(ctx, pid)
313313
if err != nil {
314314
return nil, err
315315
}

routing/http/contentrouter/contentrouter_test.go

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ func (m *mockClient) PutIPNS(ctx context.Context, name ipns.Name, record *ipns.R
5353
return args.Error(0)
5454
}
5555

56-
func (m *mockClient) GetClosestPeers(ctx context.Context, peerID, closerThan peer.ID, count int) (iter.ResultIter[*types.PeerRecord], error) {
57-
args := m.Called(ctx, peerID, closerThan, count)
56+
func (m *mockClient) GetClosestPeers(ctx context.Context, peerID peer.ID) (iter.ResultIter[*types.PeerRecord], error) {
57+
args := m.Called(ctx, peerID)
5858
return args.Get(0).(iter.ResultIter[*types.PeerRecord]), args.Error(1)
5959
}
6060

@@ -270,8 +270,6 @@ func TestGetClosestPeers(t *testing.T) {
270270
crc := NewContentRoutingClient(client)
271271

272272
peerID := peer.ID("test-peer")
273-
closerThan := peer.ID("test-peer-2")
274-
count := 2
275273

276274
// Mock response with two peer records
277275
peer1 := peer.ID("peer1")
@@ -295,9 +293,9 @@ func TestGetClosestPeers(t *testing.T) {
295293

296294
peerIter := iter.ToResultIter[*types.PeerRecord](iter.FromSlice([]*types.PeerRecord{peerRec1, peerRec2}))
297295

298-
client.On("GetClosestPeers", ctx, peerID, closerThan, count).Return(peerIter, nil)
296+
client.On("GetClosestPeers", ctx, peerID).Return(peerIter, nil)
299297

300-
infos, err := crc.GetClosestPeers(ctx, peerID, closerThan, count)
298+
infos, err := crc.GetClosestPeers(ctx, peerID)
301299
require.NoError(t, err)
302300

303301
var actual []peer.AddrInfo
@@ -319,8 +317,6 @@ func TestGetClosestPeers(t *testing.T) {
319317
crc := NewContentRoutingClient(client)
320318

321319
peerID := peer.ID("test-peer")
322-
closerThan := peer.ID("closer-than")
323-
count := 1
324320

325321
peer1 := peer.ID("peer1")
326322
peerRec1 := &types.PeerRecord{
@@ -333,9 +329,9 @@ func TestGetClosestPeers(t *testing.T) {
333329
// Mock response with an empty iterator
334330
peerIter := iter.ToResultIter[*types.PeerRecord](iter.FromSlice([]*types.PeerRecord{peerRec1}))
335331

336-
client.On("GetClosestPeers", ctx, peerID, closerThan, count).Return(peerIter, nil)
332+
client.On("GetClosestPeers", ctx, peerID).Return(peerIter, nil)
337333

338-
infos, err := crc.GetClosestPeers(ctx, peerID, closerThan, count)
334+
infos, err := crc.GetClosestPeers(ctx, peerID)
339335
require.NoError(t, err)
340336

341337
var actual []peer.AddrInfo
@@ -352,14 +348,12 @@ func TestGetClosestPeers(t *testing.T) {
352348
crc := NewContentRoutingClient(client)
353349

354350
peerID := peer.ID("test-peer")
355-
closerThan := peer.ID("closer-than")
356-
count := 1
357351

358352
// Mock error response
359353
peerIter := iter.ToResultIter[*types.PeerRecord](iter.FromSlice([]*types.PeerRecord{}))
360-
client.On("GetClosestPeers", ctx, peerID, closerThan, count).Return(peerIter, assert.AnError)
354+
client.On("GetClosestPeers", ctx, peerID).Return(peerIter, assert.AnError)
361355

362-
infos, err := crc.GetClosestPeers(ctx, peerID, closerThan, count)
356+
infos, err := crc.GetClosestPeers(ctx, peerID)
363357
require.ErrorIs(t, err, assert.AnError)
364358
assert.Nil(t, infos)
365359
})

routing/http/server/server.go

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"io"
1010
"mime"
1111
"net/http"
12-
"strconv"
1312
"strings"
1413
"sync/atomic"
1514
"time"
@@ -24,7 +23,6 @@ import (
2423
jsontypes "github.com/ipfs/boxo/routing/http/types/json"
2524
"github.com/ipfs/go-cid"
2625
logging "github.com/ipfs/go-log/v2"
27-
"github.com/libp2p/go-libp2p-kad-dht/amino"
2826
"github.com/libp2p/go-libp2p/core/peer"
2927
"github.com/libp2p/go-libp2p/core/routing"
3028
"github.com/multiformats/go-multiaddr"
@@ -44,7 +42,6 @@ const (
4442
DefaultRecordsLimit = 20
4543
DefaultStreamingRecordsLimit = 0
4644
DefaultRoutingTimeout = 30 * time.Second
47-
DefaultGetClosestPeersCount = amino.DefaultBucketSize // 20 - Amino DHT bucket size
4845
)
4946

5047
var logger = logging.Logger("routing/http/server")
@@ -90,15 +87,9 @@ type DelegatedRouter interface {
9087
// It is guaranteed that the record matches the provided name.
9188
PutIPNS(ctx context.Context, name ipns.Name, record *ipns.Record) error
9289

93-
// GetClosestPeers returns up to count peers closest to the given peerID in the DHT keyspace.
94-
//
95-
// If peerID is empty, implementations should use their own peer ID.
96-
// If closerThan is specified, only peers closer to peerID than closerThan are returned.
97-
// If count is 0, a sensible default of the routing system is used.
98-
//
99-
// Note: Amino DHT implementations are limited to returning at most 20 peers
100-
// due to the DHT bucket size.
101-
GetClosestPeers(ctx context.Context, peerID, closerThan peer.ID, count int) (iter.ResultIter[*types.PeerRecord], error)
90+
// GetClosestPeers returns the DHT closest peers to the given peer ID.
91+
// If empty, it will use the content router's peer ID (self). `closerThan` (optional) forces resulting records to be closer to `PeerID` than to `closerThan`. `count` specifies how many records to return ([1,100], with 20 as default when set to 0).
92+
GetClosestPeers(ctx context.Context, peerID peer.ID) (iter.ResultIter[*types.PeerRecord], error)
10293
}
10394

10495
// ContentRouter is deprecated, use DelegatedRouter instead.
@@ -619,30 +610,6 @@ func (s *server) getClosestPeers(w http.ResponseWriter, r *http.Request) {
619610
return
620611
}
621612

622-
query := r.URL.Query()
623-
closerThanStr := query.Get("closer-than")
624-
var closerThanPid peer.ID
625-
if closerThanStr != "" { // it is fine to omit. We will pass an empty peer.ID then.
626-
closerThanPid, err = parsePeerID(closerThanStr)
627-
if err != nil {
628-
writeErr(w, "GetClosestPeers", http.StatusBadRequest, fmt.Errorf("unable to parse closer-than PeerID %q: %w", closerThanStr, err))
629-
return
630-
}
631-
}
632-
633-
countStr := query.Get("count")
634-
count, err := strconv.Atoi(countStr)
635-
if err != nil {
636-
count = 0
637-
}
638-
if count > 100 {
639-
count = 100
640-
}
641-
// If limit is still 0, set THE default.
642-
if count <= 0 {
643-
count = DefaultGetClosestPeersCount
644-
}
645-
646613
mediaType, err := s.detectResponseType(r)
647614
if err != nil {
648615
writeErr(w, "GetClosestPeers", http.StatusBadRequest, err)
@@ -661,7 +628,7 @@ func (s *server) getClosestPeers(w http.ResponseWriter, r *http.Request) {
661628
ctx, cancel := context.WithTimeout(r.Context(), s.routingTimeout)
662629
defer cancel()
663630

664-
provIter, err := s.svc.GetClosestPeers(ctx, pid, closerThanPid, count)
631+
provIter, err := s.svc.GetClosestPeers(ctx, pid)
665632
if err != nil {
666633
if errors.Is(err, routing.ErrNotFound) {
667634
// handlerFunc takes care of setting the 404 and necessary headers

0 commit comments

Comments
 (0)