From 4856ce328ec0715eca1eafd26ff5b3208a64caf8 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Fri, 12 May 2023 15:15:14 +0200 Subject: [PATCH] feat: expose routing v1 server via optional setting --- CHANGELOG.md | 1 + cmd/ipfs/daemon.go | 4 + config/gateway.go | 5 + config/routing.go | 18 ---- core/corehttp/routing.go | 124 ++++++++++++++++++++++ core/node/libp2p/routingopt.go | 6 +- docs/changelogs/v0.23.md | 24 +++++ docs/config.md | 9 ++ docs/examples/kubo-as-a-library/go.mod | 6 +- docs/examples/kubo-as-a-library/go.sum | 16 ++- go.mod | 5 +- go.sum | 15 ++- routing/delegated.go | 76 ++------------ routing/delegated_test.go | 42 +------- routing/wrapper.go | 15 +-- test/cli/content_routing_http_test.go | 35 ++++--- test/cli/delegated_routing_http_test.go | 24 ++--- test/cli/routing_http_test.go | 134 ++++++++++++++++++++++++ test/dependencies/go.mod | 4 +- test/dependencies/go.sum | 12 +-- 20 files changed, 371 insertions(+), 204 deletions(-) create mode 100644 core/corehttp/routing.go create mode 100644 docs/changelogs/v0.23.md create mode 100644 test/cli/routing_http_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 0138eb231e4..f15248cc119 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ # Kubo Changelogs +- [v0.23](docs/changelogs/v0.23.md) - [v0.22](docs/changelogs/v0.22.md) - [v0.21](docs/changelogs/v0.21.md) - [v0.20](docs/changelogs/v0.20.md) diff --git a/cmd/ipfs/daemon.go b/cmd/ipfs/daemon.go index 5788bf3ce45..54ba582110d 100644 --- a/cmd/ipfs/daemon.go +++ b/cmd/ipfs/daemon.go @@ -850,6 +850,10 @@ func serveHTTPGateway(req *cmds.Request, cctx *oldcmds.Context) (<-chan error, e opts = append(opts, corehttp.P2PProxyOption()) } + if cfg.Gateway.ExposeRoutingAPI.WithDefault(config.DefaultExposeRoutingAPI) { + opts = append(opts, corehttp.RoutingOption()) + } + if len(cfg.Gateway.RootRedirect) > 0 { opts = append(opts, corehttp.RedirectOption("", cfg.Gateway.RootRedirect)) } diff --git a/config/gateway.go b/config/gateway.go index 816b1f48d23..7297151a31c 100644 --- a/config/gateway.go +++ b/config/gateway.go @@ -3,6 +3,7 @@ package config const ( DefaultInlineDNSLink = false DefaultDeserializedResponses = true + DefaultExposeRoutingAPI = false ) type GatewaySpec struct { @@ -73,4 +74,8 @@ type Gateway struct { // PublicGateways configures behavior of known public gateways. // Each key is a fully qualified domain name (FQDN). PublicGateways map[string]*GatewaySpec + + // ExposeRoutingAPI configures the gateway to expose a Routing v1 HTTP Server + // under /routing/v1: https://specs.ipfs.tech/routing/routing-v1/. + ExposeRoutingAPI Flag } diff --git a/config/routing.go b/config/routing.go index ede8f0f9ea9..a70aead049d 100644 --- a/config/routing.go +++ b/config/routing.go @@ -3,7 +3,6 @@ package config import ( "encoding/json" "fmt" - "runtime" ) // Routing defines configuration options for libp2p routing @@ -133,23 +132,6 @@ var MethodNameList = []MethodName{MethodNameProvide, MethodNameFindPeers, Method type HTTPRouterParams struct { // Endpoint is the URL where the routing implementation will point to get the information. Endpoint string - - // MaxProvideBatchSize determines the maximum amount of CIDs sent per batch. - // Servers might not accept more than 100 elements per batch. 100 elements by default. - MaxProvideBatchSize int - - // MaxProvideConcurrency determines the number of threads used when providing content. GOMAXPROCS by default. - MaxProvideConcurrency int -} - -func (hrp *HTTPRouterParams) FillDefaults() { - if hrp.MaxProvideBatchSize == 0 { - hrp.MaxProvideBatchSize = 100 - } - - if hrp.MaxProvideConcurrency == 0 { - hrp.MaxProvideConcurrency = runtime.GOMAXPROCS(0) - } } type DHTRouterParams struct { diff --git a/core/corehttp/routing.go b/core/corehttp/routing.go new file mode 100644 index 00000000000..90d3c1a4189 --- /dev/null +++ b/core/corehttp/routing.go @@ -0,0 +1,124 @@ +package corehttp + +import ( + "context" + "net" + "net/http" + + "github.com/ipfs/boxo/ipns" + "github.com/ipfs/boxo/routing/http/server" + "github.com/ipfs/boxo/routing/http/types" + "github.com/ipfs/boxo/routing/http/types/iter" + cid "github.com/ipfs/go-cid" + core "github.com/ipfs/kubo/core" + "github.com/libp2p/go-libp2p/core/peer" +) + +func RoutingOption() ServeOption { + return func(n *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) { + handler := server.Handler(&contentRouter{n}) + mux.Handle("/routing/v1/", handler) + return mux, nil + } +} + +type contentRouter struct { + n *core.IpfsNode +} + +func (r *contentRouter) GetProviders(ctx context.Context, key cid.Cid, limit int) (iter.ResultIter[types.Record], error) { + ctx, cancel := context.WithCancel(ctx) + ch := r.n.Routing.FindProvidersAsync(ctx, key, limit) + return iter.ToResultIter[types.Record](&peerChanIter{ + ch: ch, + cancel: cancel, + }), nil +} + +func (r *contentRouter) GetPeers(ctx context.Context, pid peer.ID, limit int) (iter.ResultIter[types.Record], error) { + ctx, cancel := context.WithCancel(ctx) + defer cancel() + + addr, err := r.n.Routing.FindPeer(ctx, pid) + if err != nil { + return nil, err + } + + rec := &types.PeerRecord{ + Schema: types.SchemaPeer, + ID: &addr.ID, + } + + for _, addr := range addr.Addrs { + rec.Addrs = append(rec.Addrs, types.Multiaddr{Multiaddr: addr}) + } + + return iter.ToResultIter[types.Record](iter.FromSlice[types.Record]([]types.Record{rec})), nil +} + +func (r *contentRouter) GetIPNSRecord(ctx context.Context, name ipns.Name) (*ipns.Record, error) { + ctx, cancel := context.WithCancel(ctx) + defer cancel() + + raw, err := r.n.Routing.GetValue(ctx, string(name.RoutingKey())) + if err != nil { + return nil, err + } + + return ipns.UnmarshalRecord(raw) +} + +func (r *contentRouter) PutIPNSRecord(ctx context.Context, name ipns.Name, record *ipns.Record) error { + ctx, cancel := context.WithCancel(ctx) + defer cancel() + + raw, err := ipns.MarshalRecord(record) + if err != nil { + return err + } + + // The caller guarantees that name matches the record. This is double checked + // by the internals of PutValue. + return r.n.Routing.PutValue(ctx, string(name.RoutingKey()), raw) +} + +type peerChanIter struct { + ch <-chan peer.AddrInfo + cancel context.CancelFunc + next *peer.AddrInfo +} + +func (it *peerChanIter) Next() bool { + addr, ok := <-it.ch + if ok { + it.next = &addr + return true + } else { + it.next = nil + return false + } +} + +func (it *peerChanIter) Val() types.Record { + if it.next == nil { + return nil + } + + // We don't know what type of protocol this peer provides. It is likely Bitswap + // but it might not be. Therefore, we set an unknown protocol with an unknown schema. + rec := &types.PeerRecord{ + Schema: types.SchemaPeer, + ID: &it.next.ID, + } + + for _, addr := range it.next.Addrs { + rec.Addrs = append(rec.Addrs, types.Multiaddr{Multiaddr: addr}) + } + + return rec +} + +func (it *peerChanIter) Close() error { + it.cancel() + return nil +} diff --git a/core/node/libp2p/routingopt.go b/core/node/libp2p/routingopt.go index 82b68361d64..f91bd1d0917 100644 --- a/core/node/libp2p/routingopt.go +++ b/core/node/libp2p/routingopt.go @@ -135,11 +135,7 @@ func ConstructDelegatedRouting(routers config.Routers, methods config.Methods, p Datastore: args.Datastore, Context: args.Ctx, }, - &irouting.ExtraHTTPParams{ - PeerID: peerID, - Addrs: httpAddrsFromConfig(addrs), - PrivKeyB64: privKey, - }) + ) } } diff --git a/docs/changelogs/v0.23.md b/docs/changelogs/v0.23.md new file mode 100644 index 00000000000..1b3fec0fe04 --- /dev/null +++ b/docs/changelogs/v0.23.md @@ -0,0 +1,24 @@ +# Kubo changelog v0.23 + +- [v0.23.0](#v0230) + +## v0.23.0 + +- [Overview](#overview) +- [๐Ÿ”ฆ Highlights](#-highlights) + - [Local Gateway: optional support for `/routing/v1` endpoint](#local-gateway-optional-support-for-routingv1-endpoint) +- [๐Ÿ“ Changelog](#-changelog) +- [๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ Contributors](#-contributors) + +### Overview + +### ๐Ÿ”ฆ Highlights + +#### Local Gateway: optional support for `/routing/v1` endpoint + +The local gateway, hosted at `127.0.0.1`, now supports the delegated [Routing V1](https://specs.ipfs.tech/routing/http-routing-v1/) API. +This is disabled by default, but can be enabled by setting `Config.Gateway.ExposeRoutingAPI = true`. + +### ๐Ÿ“ Changelog + +### ๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ Contributors diff --git a/docs/config.md b/docs/config.md index b7e21e12a2b..a679707ce16 100644 --- a/docs/config.md +++ b/docs/config.md @@ -659,6 +659,15 @@ Default: `true` Type: `flag` +#### `Gateway.ExposeRoutingAPI` + +An optional flag to explicitly configure whether this gateway provides a Routing +V1 endpoint. This only affects your local gateway, at `127.0.0.1`: https://specs.ipfs.tech/routing/routing-v1/. + +Default: `false` + +Type: `flag` + ### `Gateway.HTTPHeaders` Headers to set on gateway responses. diff --git a/docs/examples/kubo-as-a-library/go.mod b/docs/examples/kubo-as-a-library/go.mod index 5cc1054a245..37286210ae1 100644 --- a/docs/examples/kubo-as-a-library/go.mod +++ b/docs/examples/kubo-as-a-library/go.mod @@ -7,7 +7,7 @@ go 1.18 replace github.com/ipfs/kubo => ./../../.. require ( - github.com/ipfs/boxo v0.11.0 + github.com/ipfs/boxo v0.11.1-0.20230809093309-ecc6fb41f53d github.com/ipfs/kubo v0.0.0-00010101000000-000000000000 github.com/libp2p/go-libp2p v0.29.2 github.com/multiformats/go-multiaddr v0.10.1 @@ -52,7 +52,6 @@ require ( github.com/google/gopacket v1.1.19 // indirect github.com/google/pprof v0.0.0-20230705174524-200ffdc848b8 // indirect github.com/google/uuid v1.3.0 // indirect - github.com/gorilla/mux v1.8.0 // indirect github.com/gorilla/websocket v1.5.0 // indirect github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect github.com/hannahhoward/go-pubsub v0.0.0-20200423002714-8d62886cc36e // indirect @@ -91,7 +90,7 @@ require ( github.com/ipfs/go-unixfsnode v1.7.1 // indirect github.com/ipld/go-car/v2 v2.10.2-0.20230622090957-499d0c909d33 // indirect github.com/ipld/go-codec-dagpb v1.6.0 // indirect - github.com/ipld/go-ipld-prime v0.20.0 // indirect + github.com/ipld/go-ipld-prime v0.20.1-0.20230619045951-499e9b9c2024 // indirect github.com/jackpal/go-nat-pmp v1.0.2 // indirect github.com/jbenet/go-temp-err-catcher v0.1.0 // indirect github.com/jbenet/goprocess v0.1.4 // indirect @@ -153,7 +152,6 @@ require ( github.com/quic-go/quic-go v0.36.4 // indirect github.com/quic-go/webtransport-go v0.5.3 // indirect github.com/raulk/go-watchdog v1.3.0 // indirect - github.com/samber/lo v1.36.0 // indirect github.com/spaolacci/murmur3 v1.1.0 // indirect github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 // indirect github.com/ucarion/urlpath v0.0.0-20200424170820-7ccc79b76bbb // indirect diff --git a/docs/examples/kubo-as-a-library/go.sum b/docs/examples/kubo-as-a-library/go.sum index 3213c24ebc9..6430e8ab84a 100644 --- a/docs/examples/kubo-as-a-library/go.sum +++ b/docs/examples/kubo-as-a-library/go.sum @@ -159,7 +159,7 @@ github.com/francoispqt/gojay v1.2.13 h1:d2m3sFjloqoIUQU3TsHBgj6qg/BVGlTBeHDUmyJn github.com/francoispqt/gojay v1.2.13/go.mod h1:ehT5mTG4ua4581f1++1WLG0vPdaA9HaiDsoyrBGkyDY= github.com/frankban/quicktest v1.11.3/go.mod h1:wRf/ReqHper53s+kmmSZizM8NamnL3IM0I9ntUbOk+k= github.com/frankban/quicktest v1.14.0/go.mod h1:NeW+ay9A/U67EYXNFA1nPE8e/tnQv/09mUdL/ijj8og= -github.com/frankban/quicktest v1.14.4 h1:g2rn0vABPOOXmZUj+vbmUp0lPoXEMuhTpIluN0XL9UY= +github.com/frankban/quicktest v1.14.5 h1:dfYrrRyLtiqT9GyKXgdh+k4inNeTvmGbuSgZ3lx3GhA= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY= @@ -270,7 +270,6 @@ github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5m github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= github.com/gopherjs/gopherjs v0.0.0-20190430165422-3e4dfb77656c h1:7lF+Vz0LqiRidnzC1Oq86fpX1q/iEv2KJdrCtttYjT4= github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= -github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA= @@ -301,8 +300,8 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1: github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/ipfs/bbloom v0.0.4 h1:Gi+8EGJ2y5qiD5FbsbpX/TMNcJw8gSqr7eyjHa4Fhvs= github.com/ipfs/bbloom v0.0.4/go.mod h1:cS9YprKXpoZ9lT0n/Mw/a6/aFV6DTjTLYHeA+gyqMG0= -github.com/ipfs/boxo v0.11.0 h1:urMxhZ3xoF4HssJVD3+0ssGT9pptEfHfbL8DYdoWFlg= -github.com/ipfs/boxo v0.11.0/go.mod h1:8IfDmp+FzFGcF4zjAgHMVPpwYw4AjN9ePEzDfkaYJ1w= +github.com/ipfs/boxo v0.11.1-0.20230809093309-ecc6fb41f53d h1:11P/IcImiaLEOFc+xGiFza4/xnVicYIcbyawQixF8Qg= +github.com/ipfs/boxo v0.11.1-0.20230809093309-ecc6fb41f53d/go.mod h1:Tbv+IR9baTM1VprtVTUMC2EPu6lR4803DahiQk5IQMk= github.com/ipfs/go-bitfield v1.1.0 h1:fh7FIo8bSwaJEh6DdTWbCeZ1eqOaOkKFI74SCnsWbGA= github.com/ipfs/go-bitfield v1.1.0/go.mod h1:paqf1wjq/D2BBmzfTVFlJQ9IlFOZpg422HL0HqsGWHU= github.com/ipfs/go-block-format v0.0.2/go.mod h1:AWR46JfpcObNfg3ok2JHDUfdiHRgWhJgCQF+KIgOPJY= @@ -398,8 +397,8 @@ github.com/ipld/go-codec-dagpb v1.6.0 h1:9nYazfyu9B1p3NAgfVdpRco3Fs2nFC72DqVsMj6 github.com/ipld/go-codec-dagpb v1.6.0/go.mod h1:ANzFhfP2uMJxRBr8CE+WQWs5UsNa0pYtmKZ+agnUw9s= github.com/ipld/go-ipld-prime v0.11.0/go.mod h1:+WIAkokurHmZ/KwzDOMUuoeJgaRQktHtEaLglS3ZeV8= github.com/ipld/go-ipld-prime v0.14.1/go.mod h1:QcE4Y9n/ZZr8Ijg5bGPT0GqYWgZ1704nH0RDcQtgTP0= -github.com/ipld/go-ipld-prime v0.20.0 h1:Ud3VwE9ClxpO2LkCYP7vWPc0Fo+dYdYzgxUJZ3uRG4g= -github.com/ipld/go-ipld-prime v0.20.0/go.mod h1:PzqZ/ZR981eKbgdr3y2DJYeD/8bgMawdGVlJDE8kK+M= +github.com/ipld/go-ipld-prime v0.20.1-0.20230619045951-499e9b9c2024 h1:HfSCtpmGw0fzCfhZKzASxVjNk8tA1oSCDEF/To3eVg0= +github.com/ipld/go-ipld-prime v0.20.1-0.20230619045951-499e9b9c2024/go.mod h1:PRQpXNcJypaPiiSdarsrJABPkYrBvafwDl0B9HjujZ8= github.com/ipld/go-ipld-prime/storage/bsadapter v0.0.0-20230102063945-1a409dc236dd h1:gMlw/MhNr2Wtp5RwGdsW23cs+yCuj9k2ON7i9MiJlRo= github.com/jackpal/go-nat-pmp v1.0.2 h1:KzKSgb7qkJvOUTqYl9/Hg/me3pWgBmERKrTGD7BdWus= github.com/jackpal/go-nat-pmp v1.0.2/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+4orBN1SBKc= @@ -655,8 +654,6 @@ github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjR github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd/go.mod h1:hPqNNc0+uJM6H+SuU8sEs5K5IQeKccPqeSjfgcKGgPk= -github.com/samber/lo v1.36.0 h1:4LaOxH1mHnbDGhTVE0i1z8v/lWaQW8AIfOD3HU4mSaw= -github.com/samber/lo v1.36.0/go.mod h1:HLeWcJRRyLKp3+/XBJvOrerCQn9mhdKMHyd7IRlgeQ8= github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= github.com/shurcooL/component v0.0.0-20170202220835-f88ec8f54cc4/go.mod h1:XhFIlyj5a1fBNx5aJTbKoIq0mNaPvOagO+HjB3EtxrY= github.com/shurcooL/events v0.0.0-20181021180414-410e4ca65f48/go.mod h1:5u70Mqkb5O5cxEA8nxTsgrgLehJeAw6Oc4Ab1c/P1HM= @@ -723,7 +720,6 @@ github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpP github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 h1:epCh84lMvA70Z7CTTCmYQn2CKbY8j86K7/FAIr141uY= github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7/go.mod h1:q4W45IWZaF22tdD+VEXcAWRA037jwmWEB5VWYORlTpc= github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA= -github.com/thoas/go-funk v0.9.1 h1:O549iLZqPpTUQ10ykd26sZhzD+rmR5pWhuElrhbC20M= github.com/tj/assert v0.0.3 h1:Df/BlaZ20mq6kuai7f5z2TvPFiwC3xaWJSDQNiIS3Rk= github.com/tv42/httpunix v0.0.0-20191220191345-2ba4b9c3382c h1:u6SKchux2yDvFQnDHS3lPnIRmfVJ5Sxy3ao2SIdysLQ= github.com/tv42/httpunix v0.0.0-20191220191345-2ba4b9c3382c/go.mod h1:hzIxponao9Kjc7aWznkXaL4U4TWaDSs8zcsY4Ka08nM= @@ -737,7 +733,7 @@ github.com/viant/toolbox v0.24.0/go.mod h1:OxMCG57V0PXuIP2HNQrtJf2CjqdmbrOx5EkMI github.com/wangjia184/sortedset v0.0.0-20160527075905-f5d03557ba30/go.mod h1:YkocrP2K2tcw938x9gCOmT5G5eCD6jsTz0SZuyAqwIE= github.com/warpfork/go-testmark v0.3.0/go.mod h1:jhEf8FVxd+F17juRubpmut64NEG6I2rgkUhlcqqXwE0= github.com/warpfork/go-testmark v0.9.0/go.mod h1:jhEf8FVxd+F17juRubpmut64NEG6I2rgkUhlcqqXwE0= -github.com/warpfork/go-testmark v0.11.0 h1:J6LnV8KpceDvo7spaNU4+DauH2n1x+6RaO2rJrmpQ9U= +github.com/warpfork/go-testmark v0.12.1 h1:rMgCpJfwy1sJ50x0M0NgyphxYYPMOODIJHhsXyEHU0s= github.com/warpfork/go-wish v0.0.0-20180510122957-5ad1f5abf436/go.mod h1:x6AKhvSSexNrVSrViXSHUEbICjmGXhtgABaHIySUSGw= github.com/warpfork/go-wish v0.0.0-20200122115046-b9ea61034e4a/go.mod h1:x6AKhvSSexNrVSrViXSHUEbICjmGXhtgABaHIySUSGw= github.com/warpfork/go-wish v0.0.0-20220906213052-39a1cc7a02d0 h1:GDDkbFiaK8jsSDJfjId/PEGEShv6ugrt4kYsC5UIDaQ= diff --git a/go.mod b/go.mod index d730dd2f34e..ba51aee8bc4 100644 --- a/go.mod +++ b/go.mod @@ -15,7 +15,7 @@ require ( github.com/fsnotify/fsnotify v1.6.0 github.com/google/uuid v1.3.0 github.com/hashicorp/go-multierror v1.1.1 - github.com/ipfs/boxo v0.11.0 + github.com/ipfs/boxo v0.11.1-0.20230809093309-ecc6fb41f53d github.com/ipfs/go-block-format v0.1.2 github.com/ipfs/go-cid v0.4.1 github.com/ipfs/go-cidutil v0.1.0 @@ -39,7 +39,7 @@ require ( github.com/ipld/go-car v0.5.0 github.com/ipld/go-car/v2 v2.10.2-0.20230622090957-499d0c909d33 github.com/ipld/go-codec-dagpb v1.6.0 - github.com/ipld/go-ipld-prime v0.20.0 + github.com/ipld/go-ipld-prime v0.20.1-0.20230619045951-499e9b9c2024 github.com/jbenet/go-random v0.0.0-20190219211222-123a90aedc0c github.com/jbenet/go-temp-err-catcher v0.1.0 github.com/jbenet/goprocess v0.1.4 @@ -198,7 +198,6 @@ require ( github.com/quic-go/webtransport-go v0.5.3 // indirect github.com/raulk/go-watchdog v1.3.0 // indirect github.com/rs/cors v1.7.0 // indirect - github.com/samber/lo v1.36.0 // indirect github.com/spaolacci/murmur3 v1.1.0 // indirect github.com/texttheater/golang-levenshtein v0.0.0-20180516184445-d188e65d659e // indirect github.com/tidwall/match v1.1.1 // indirect diff --git a/go.sum b/go.sum index 22b6ea3cf2b..3331266525b 100644 --- a/go.sum +++ b/go.sum @@ -179,7 +179,7 @@ github.com/francoispqt/gojay v1.2.13 h1:d2m3sFjloqoIUQU3TsHBgj6qg/BVGlTBeHDUmyJn github.com/francoispqt/gojay v1.2.13/go.mod h1:ehT5mTG4ua4581f1++1WLG0vPdaA9HaiDsoyrBGkyDY= github.com/frankban/quicktest v1.11.3/go.mod h1:wRf/ReqHper53s+kmmSZizM8NamnL3IM0I9ntUbOk+k= github.com/frankban/quicktest v1.14.0/go.mod h1:NeW+ay9A/U67EYXNFA1nPE8e/tnQv/09mUdL/ijj8og= -github.com/frankban/quicktest v1.14.4 h1:g2rn0vABPOOXmZUj+vbmUp0lPoXEMuhTpIluN0XL9UY= +github.com/frankban/quicktest v1.14.5 h1:dfYrrRyLtiqT9GyKXgdh+k4inNeTvmGbuSgZ3lx3GhA= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY= @@ -337,8 +337,8 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1: github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/ipfs/bbloom v0.0.4 h1:Gi+8EGJ2y5qiD5FbsbpX/TMNcJw8gSqr7eyjHa4Fhvs= github.com/ipfs/bbloom v0.0.4/go.mod h1:cS9YprKXpoZ9lT0n/Mw/a6/aFV6DTjTLYHeA+gyqMG0= -github.com/ipfs/boxo v0.11.0 h1:urMxhZ3xoF4HssJVD3+0ssGT9pptEfHfbL8DYdoWFlg= -github.com/ipfs/boxo v0.11.0/go.mod h1:8IfDmp+FzFGcF4zjAgHMVPpwYw4AjN9ePEzDfkaYJ1w= +github.com/ipfs/boxo v0.11.1-0.20230809093309-ecc6fb41f53d h1:11P/IcImiaLEOFc+xGiFza4/xnVicYIcbyawQixF8Qg= +github.com/ipfs/boxo v0.11.1-0.20230809093309-ecc6fb41f53d/go.mod h1:Tbv+IR9baTM1VprtVTUMC2EPu6lR4803DahiQk5IQMk= github.com/ipfs/go-bitfield v1.1.0 h1:fh7FIo8bSwaJEh6DdTWbCeZ1eqOaOkKFI74SCnsWbGA= github.com/ipfs/go-bitfield v1.1.0/go.mod h1:paqf1wjq/D2BBmzfTVFlJQ9IlFOZpg422HL0HqsGWHU= github.com/ipfs/go-bitswap v0.11.0 h1:j1WVvhDX1yhG32NTC9xfxnqycqYIlhzEzLXG/cU1HyQ= @@ -446,8 +446,8 @@ github.com/ipld/go-codec-dagpb v1.6.0 h1:9nYazfyu9B1p3NAgfVdpRco3Fs2nFC72DqVsMj6 github.com/ipld/go-codec-dagpb v1.6.0/go.mod h1:ANzFhfP2uMJxRBr8CE+WQWs5UsNa0pYtmKZ+agnUw9s= github.com/ipld/go-ipld-prime v0.11.0/go.mod h1:+WIAkokurHmZ/KwzDOMUuoeJgaRQktHtEaLglS3ZeV8= github.com/ipld/go-ipld-prime v0.14.1/go.mod h1:QcE4Y9n/ZZr8Ijg5bGPT0GqYWgZ1704nH0RDcQtgTP0= -github.com/ipld/go-ipld-prime v0.20.0 h1:Ud3VwE9ClxpO2LkCYP7vWPc0Fo+dYdYzgxUJZ3uRG4g= -github.com/ipld/go-ipld-prime v0.20.0/go.mod h1:PzqZ/ZR981eKbgdr3y2DJYeD/8bgMawdGVlJDE8kK+M= +github.com/ipld/go-ipld-prime v0.20.1-0.20230619045951-499e9b9c2024 h1:HfSCtpmGw0fzCfhZKzASxVjNk8tA1oSCDEF/To3eVg0= +github.com/ipld/go-ipld-prime v0.20.1-0.20230619045951-499e9b9c2024/go.mod h1:PRQpXNcJypaPiiSdarsrJABPkYrBvafwDl0B9HjujZ8= github.com/ipld/go-ipld-prime/storage/bsadapter v0.0.0-20230102063945-1a409dc236dd h1:gMlw/MhNr2Wtp5RwGdsW23cs+yCuj9k2ON7i9MiJlRo= github.com/jackpal/go-nat-pmp v1.0.2 h1:KzKSgb7qkJvOUTqYl9/Hg/me3pWgBmERKrTGD7BdWus= github.com/jackpal/go-nat-pmp v1.0.2/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+4orBN1SBKc= @@ -762,8 +762,6 @@ github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU= github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd/go.mod h1:hPqNNc0+uJM6H+SuU8sEs5K5IQeKccPqeSjfgcKGgPk= -github.com/samber/lo v1.36.0 h1:4LaOxH1mHnbDGhTVE0i1z8v/lWaQW8AIfOD3HU4mSaw= -github.com/samber/lo v1.36.0/go.mod h1:HLeWcJRRyLKp3+/XBJvOrerCQn9mhdKMHyd7IRlgeQ8= github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= github.com/shurcooL/component v0.0.0-20170202220835-f88ec8f54cc4/go.mod h1:XhFIlyj5a1fBNx5aJTbKoIq0mNaPvOagO+HjB3EtxrY= github.com/shurcooL/events v0.0.0-20181021180414-410e4ca65f48/go.mod h1:5u70Mqkb5O5cxEA8nxTsgrgLehJeAw6Oc4Ab1c/P1HM= @@ -836,7 +834,6 @@ github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7/go.mod h1:q4W45 github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA= github.com/texttheater/golang-levenshtein v0.0.0-20180516184445-d188e65d659e h1:T5PdfK/M1xyrHwynxMIVMWLS7f/qHwfslZphxtGnw7s= github.com/texttheater/golang-levenshtein v0.0.0-20180516184445-d188e65d659e/go.mod h1:XDKHRm5ThF8YJjx001LtgelzsoaEcvnA7lVWz9EeX3g= -github.com/thoas/go-funk v0.9.1 h1:O549iLZqPpTUQ10ykd26sZhzD+rmR5pWhuElrhbC20M= github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM= github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= @@ -859,7 +856,7 @@ github.com/viant/toolbox v0.24.0/go.mod h1:OxMCG57V0PXuIP2HNQrtJf2CjqdmbrOx5EkMI github.com/wangjia184/sortedset v0.0.0-20160527075905-f5d03557ba30/go.mod h1:YkocrP2K2tcw938x9gCOmT5G5eCD6jsTz0SZuyAqwIE= github.com/warpfork/go-testmark v0.3.0/go.mod h1:jhEf8FVxd+F17juRubpmut64NEG6I2rgkUhlcqqXwE0= github.com/warpfork/go-testmark v0.9.0/go.mod h1:jhEf8FVxd+F17juRubpmut64NEG6I2rgkUhlcqqXwE0= -github.com/warpfork/go-testmark v0.11.0 h1:J6LnV8KpceDvo7spaNU4+DauH2n1x+6RaO2rJrmpQ9U= +github.com/warpfork/go-testmark v0.12.1 h1:rMgCpJfwy1sJ50x0M0NgyphxYYPMOODIJHhsXyEHU0s= github.com/warpfork/go-wish v0.0.0-20180510122957-5ad1f5abf436/go.mod h1:x6AKhvSSexNrVSrViXSHUEbICjmGXhtgABaHIySUSGw= github.com/warpfork/go-wish v0.0.0-20200122115046-b9ea61034e4a/go.mod h1:x6AKhvSSexNrVSrViXSHUEbICjmGXhtgABaHIySUSGw= github.com/warpfork/go-wish v0.0.0-20220906213052-39a1cc7a02d0 h1:GDDkbFiaK8jsSDJfjId/PEGEShv6ugrt4kYsC5UIDaQ= diff --git a/routing/delegated.go b/routing/delegated.go index 6d34970f556..d30049f24d4 100644 --- a/routing/delegated.go +++ b/routing/delegated.go @@ -2,7 +2,6 @@ package routing import ( "context" - "encoding/base64" "errors" "fmt" "net/http" @@ -18,17 +17,15 @@ import ( "github.com/libp2p/go-libp2p-kad-dht/fullrt" record "github.com/libp2p/go-libp2p-record" routinghelpers "github.com/libp2p/go-libp2p-routing-helpers" - ic "github.com/libp2p/go-libp2p/core/crypto" host "github.com/libp2p/go-libp2p/core/host" "github.com/libp2p/go-libp2p/core/peer" "github.com/libp2p/go-libp2p/core/routing" - ma "github.com/multiformats/go-multiaddr" "go.opencensus.io/stats/view" ) var log = logging.Logger("routing/delegated") -func Parse(routers config.Routers, methods config.Methods, extraDHT *ExtraDHTParams, extraHTTP *ExtraHTTPParams) (routing.Routing, error) { +func Parse(routers config.Routers, methods config.Methods, extraDHT *ExtraDHTParams) (routing.Routing, error) { if err := methods.Check(); err != nil { return nil, err } @@ -38,7 +35,7 @@ func Parse(routers config.Routers, methods config.Methods, extraDHT *ExtraDHTPar // Create all needed routers from method names for mn, m := range methods { - router, err := parse(make(map[string]bool), createdRouters, m.RouterName, routers, extraDHT, extraHTTP) + router, err := parse(make(map[string]bool), createdRouters, m.RouterName, routers, extraDHT) if err != nil { return nil, err } @@ -67,7 +64,6 @@ func parse(visited map[string]bool, routerName string, routersCfg config.Routers, extraDHT *ExtraDHTParams, - extraHTTP *ExtraHTTPParams, ) (routing.Routing, error) { // check if we already created it r, ok := createdRouters[routerName] @@ -92,14 +88,14 @@ func parse(visited map[string]bool, var err error switch cfg.Type { case config.RouterTypeHTTP: - router, err = httpRoutingFromConfig(cfg.Router, extraHTTP) + router, err = httpRoutingFromConfig(cfg.Router) case config.RouterTypeDHT: router, err = dhtRoutingFromConfig(cfg.Router, extraDHT) case config.RouterTypeParallel: crp := cfg.Parameters.(*config.ComposableRouterParams) var pr []*routinghelpers.ParallelRouter for _, cr := range crp.Routers { - ri, err := parse(visited, createdRouters, cr.RouterName, routersCfg, extraDHT, extraHTTP) + ri, err := parse(visited, createdRouters, cr.RouterName, routersCfg, extraDHT) if err != nil { return nil, err } @@ -119,7 +115,7 @@ func parse(visited map[string]bool, crp := cfg.Parameters.(*config.ComposableRouterParams) var sr []*routinghelpers.SequentialRouter for _, cr := range crp.Routers { - ri, err := parse(visited, createdRouters, cr.RouterName, routersCfg, extraDHT, extraHTTP) + ri, err := parse(visited, createdRouters, cr.RouterName, routersCfg, extraDHT) if err != nil { return nil, err } @@ -148,12 +144,6 @@ func parse(visited map[string]bool, return router, nil } -type ExtraHTTPParams struct { - PeerID string - Addrs []string - PrivKeyB64 string -} - func ConstructHTTPRouter(endpoint string, peerID string, addrs []string, privKey string) (routing.Routing, error) { return httpRoutingFromConfig( config.Router{ @@ -162,22 +152,15 @@ func ConstructHTTPRouter(endpoint string, peerID string, addrs []string, privKey Endpoint: endpoint, }, }, - &ExtraHTTPParams{ - PeerID: peerID, - Addrs: addrs, - PrivKeyB64: privKey, - }, ) } -func httpRoutingFromConfig(conf config.Router, extraHTTP *ExtraHTTPParams) (routing.Routing, error) { +func httpRoutingFromConfig(conf config.Router) (routing.Routing, error) { params := conf.Parameters.(*config.HTTPRouterParams) if params.Endpoint == "" { return nil, NewParamNeededErr("Endpoint", conf.Type) } - params.FillDefaults() - // Increase per-host connection pool since we are making lots of concurrent requests. transport := http.DefaultTransport.(*http.Transport).Clone() transport.MaxIdleConns = 500 @@ -190,21 +173,9 @@ func httpRoutingFromConfig(conf config.Router, extraHTTP *ExtraHTTPParams) (rout }, } - key, err := decodePrivKey(extraHTTP.PrivKeyB64) - if err != nil { - return nil, err - } - - addrInfo, err := createAddrInfo(extraHTTP.PeerID, extraHTTP.Addrs) - if err != nil { - return nil, err - } - cli, err := drclient.New( params.Endpoint, drclient.WithHTTPClient(delegateHTTPClient), - drclient.WithIdentity(key), - drclient.WithProviderInfo(addrInfo.ID, addrInfo.Addrs), drclient.WithUserAgent(version.GetUserAgentVersion()), ) if err != nil { @@ -213,8 +184,6 @@ func httpRoutingFromConfig(conf config.Router, extraHTTP *ExtraHTTPParams) (rout cr := contentrouter.NewContentRoutingClient( cli, - contentrouter.WithMaxProvideBatchSize(params.MaxProvideBatchSize), - contentrouter.WithMaxProvideConcurrency(params.MaxProvideConcurrency), ) err = view.Register(drclient.OpenCensusViews...) @@ -224,41 +193,12 @@ func httpRoutingFromConfig(conf config.Router, extraHTTP *ExtraHTTPParams) (rout return &httpRoutingWrapper{ ContentRouting: cr, + PeerRouting: cr, + ValueStore: cr, ProvideManyRouter: cr, }, nil } -func decodePrivKey(keyB64 string) (ic.PrivKey, error) { - pk, err := base64.StdEncoding.DecodeString(keyB64) - if err != nil { - return nil, err - } - - return ic.UnmarshalPrivateKey(pk) -} - -func createAddrInfo(peerID string, addrs []string) (peer.AddrInfo, error) { - pID, err := peer.Decode(peerID) - if err != nil { - return peer.AddrInfo{}, err - } - - var mas []ma.Multiaddr - for _, a := range addrs { - m, err := ma.NewMultiaddr(a) - if err != nil { - return peer.AddrInfo{}, err - } - - mas = append(mas, m) - } - - return peer.AddrInfo{ - ID: pID, - Addrs: mas, - }, nil -} - type ExtraDHTParams struct { BootstrapPeers []peer.AddrInfo Host host.Host diff --git a/routing/delegated_test.go b/routing/delegated_test.go index da0210f5ebb..9158d30b0ae 100644 --- a/routing/delegated_test.go +++ b/routing/delegated_test.go @@ -1,22 +1,15 @@ package routing import ( - "crypto/rand" - "encoding/base64" "testing" "github.com/ipfs/kubo/config" - "github.com/libp2p/go-libp2p/core/crypto" - "github.com/libp2p/go-libp2p/core/peer" "github.com/stretchr/testify/require" ) func TestParser(t *testing.T) { require := require.New(t) - pid, sk, err := generatePeerID() - require.NoError(err) - router, err := Parse(config.Routers{ "r1": config.RouterParser{ Router: config.Router{ @@ -54,10 +47,7 @@ func TestParser(t *testing.T) { config.MethodNameProvide: config.Method{ RouterName: "r2", }, - }, &ExtraDHTParams{}, &ExtraHTTPParams{ - PeerID: string(pid), - PrivKeyB64: sk, - }) + }, &ExtraDHTParams{}) require.NoError(err) @@ -71,9 +61,6 @@ func TestParser(t *testing.T) { func TestParserRecursive(t *testing.T) { require := require.New(t) - pid, sk, err := generatePeerID() - require.NoError(err) - router, err := Parse(config.Routers{ "http1": config.RouterParser{ Router: config.Router{ @@ -145,10 +132,7 @@ func TestParserRecursive(t *testing.T) { config.MethodNameProvide: config.Method{ RouterName: "composable2", }, - }, &ExtraDHTParams{}, &ExtraHTTPParams{ - PeerID: string(pid), - PrivKeyB64: sk, - }) + }, &ExtraDHTParams{}) require.NoError(err) @@ -201,27 +185,7 @@ func TestParserRecursiveLoop(t *testing.T) { config.MethodNameProvide: config.Method{ RouterName: "composable2", }, - }, &ExtraDHTParams{}, nil) + }, &ExtraDHTParams{}) require.ErrorContains(err, "dependency loop creating router with name \"composable2\"") } - -func generatePeerID() (string, string, error) { - sk, pk, err := crypto.GenerateEd25519Key(rand.Reader) - if err != nil { - return "", "", err - } - - bytes, err := crypto.MarshalPrivateKey(sk) - if err != nil { - return "", "", err - } - - enc := base64.StdEncoding.EncodeToString(bytes) - if err != nil { - return "", "", err - } - - pid, err := peer.IDFromPublicKey(pk) - return pid.String(), enc, err -} diff --git a/routing/wrapper.go b/routing/wrapper.go index f6a753843cd..f63544989c5 100644 --- a/routing/wrapper.go +++ b/routing/wrapper.go @@ -4,7 +4,6 @@ import ( "context" routinghelpers "github.com/libp2p/go-libp2p-routing-helpers" - "github.com/libp2p/go-libp2p/core/peer" "github.com/libp2p/go-libp2p/core/routing" ) @@ -20,6 +19,8 @@ var _ routinghelpers.ProvideManyRouter = &httpRoutingWrapper{} // http delegated routing. type httpRoutingWrapper struct { routing.ContentRouting + routing.PeerRouting + routing.ValueStore routinghelpers.ProvideManyRouter } @@ -27,18 +28,6 @@ func (c *httpRoutingWrapper) Bootstrap(ctx context.Context) error { return nil } -func (c *httpRoutingWrapper) FindPeer(ctx context.Context, id peer.ID) (peer.AddrInfo, error) { - return peer.AddrInfo{}, routing.ErrNotSupported -} - -func (c *httpRoutingWrapper) PutValue(context.Context, string, []byte, ...routing.Option) error { - return routing.ErrNotSupported -} - -func (c *httpRoutingWrapper) GetValue(context.Context, string, ...routing.Option) ([]byte, error) { - return nil, routing.ErrNotSupported -} - func (c *httpRoutingWrapper) SearchValue(context.Context, string, ...routing.Option) (<-chan []byte, error) { out := make(chan []byte) close(out) diff --git a/test/cli/content_routing_http_test.go b/test/cli/content_routing_http_test.go index c1ea64d3fb5..4f25c34db3e 100644 --- a/test/cli/content_routing_http_test.go +++ b/test/cli/content_routing_http_test.go @@ -9,45 +9,50 @@ import ( "testing" "time" + "github.com/ipfs/boxo/ipns" "github.com/ipfs/boxo/routing/http/server" "github.com/ipfs/boxo/routing/http/types" "github.com/ipfs/boxo/routing/http/types/iter" "github.com/ipfs/go-cid" "github.com/ipfs/kubo/test/cli/harness" "github.com/ipfs/kubo/test/cli/testutils" + "github.com/libp2p/go-libp2p/core/peer" + "github.com/libp2p/go-libp2p/core/routing" "github.com/stretchr/testify/assert" ) type fakeHTTPContentRouter struct { - m sync.Mutex - findProvidersCalls int - provideCalls int + m sync.Mutex + getProvidersCalls int + getPeersCalls int } -func (r *fakeHTTPContentRouter) FindProviders(ctx context.Context, key cid.Cid, limit int) (iter.ResultIter[types.ProviderResponse], error) { +func (r *fakeHTTPContentRouter) GetProviders(ctx context.Context, key cid.Cid, limit int) (iter.ResultIter[types.Record], error) { r.m.Lock() defer r.m.Unlock() - r.findProvidersCalls++ - return iter.FromSlice([]iter.Result[types.ProviderResponse]{}), nil + r.getProvidersCalls++ + return iter.FromSlice([]iter.Result[types.Record]{}), nil } -func (r *fakeHTTPContentRouter) ProvideBitswap(ctx context.Context, req *server.BitswapWriteProvideRequest) (time.Duration, error) { +func (r *fakeHTTPContentRouter) GetPeers(ctx context.Context, pid peer.ID, limit int) (iter.ResultIter[types.Record], error) { r.m.Lock() defer r.m.Unlock() - r.provideCalls++ - return 0, nil + r.getPeersCalls++ + return iter.FromSlice([]iter.Result[types.Record]{}), nil } -func (r *fakeHTTPContentRouter) Provide(ctx context.Context, req *server.WriteProvideRequest) (types.ProviderResponse, error) { - r.m.Lock() - defer r.m.Unlock() - r.provideCalls++ - return nil, nil + +func (r *fakeHTTPContentRouter) GetIPNSRecord(ctx context.Context, name ipns.Name) (*ipns.Record, error) { + return nil, routing.ErrNotSupported +} + +func (r *fakeHTTPContentRouter) PutIPNSRecord(ctx context.Context, name ipns.Name, record *ipns.Record) error { + return routing.ErrNotSupported } func (r *fakeHTTPContentRouter) numFindProvidersCalls() int { r.m.Lock() defer r.m.Unlock() - return r.findProvidersCalls + return r.getProvidersCalls } // userAgentRecorder records the user agent of every HTTP request diff --git a/test/cli/delegated_routing_http_test.go b/test/cli/delegated_routing_http_test.go index b1ebacd1478..9831198beec 100644 --- a/test/cli/delegated_routing_http_test.go +++ b/test/cli/delegated_routing_http_test.go @@ -89,10 +89,10 @@ func TestHTTPDelegatedRouting(t *testing.T) { t.Run("adding HTTP delegated routing endpoint to Routing.Routers config works", func(t *testing.T) { server := fakeServer("application/json", ToJSONStr(JSONObj{ "Providers": []JSONObj{{ - "Protocol": "transport-bitswap", - "Schema": "bitswap", - "ID": provs[0], - "Addrs": []string{"/ip4/0.0.0.0/tcp/4001", "/ip4/0.0.0.0/tcp/4002"}, + "Protocols": []string{"transport-bitswap"}, + "Schema": "peer", + "ID": provs[0], + "Addrs": []string{"/ip4/0.0.0.0/tcp/4001", "/ip4/0.0.0.0/tcp/4002"}, }}, })) t.Cleanup(server.Close) @@ -124,15 +124,15 @@ func TestHTTPDelegatedRouting(t *testing.T) { t.Run("adding HTTP delegated routing endpoint to Routing.Routers config works (streaming)", func(t *testing.T) { server := fakeServer("application/x-ndjson", ToJSONStr(JSONObj{ - "Protocol": "transport-bitswap", - "Schema": "bitswap", - "ID": provs[1], - "Addrs": []string{"/ip4/0.0.0.0/tcp/4001", "/ip4/0.0.0.0/tcp/4002"}, + "Protocols": []string{"transport-bitswap"}, + "Schema": "peer", + "ID": provs[1], + "Addrs": []string{"/ip4/0.0.0.0/tcp/4001", "/ip4/0.0.0.0/tcp/4002"}, }), ToJSONStr(JSONObj{ - "Protocol": "transport-bitswap", - "Schema": "bitswap", - "ID": provs[0], - "Addrs": []string{"/ip4/0.0.0.0/tcp/4001", "/ip4/0.0.0.0/tcp/4002"}, + "Protocols": []string{"transport-bitswap"}, + "Schema": "peer", + "ID": provs[0], + "Addrs": []string{"/ip4/0.0.0.0/tcp/4001", "/ip4/0.0.0.0/tcp/4002"}, })) t.Cleanup(server.Close) diff --git a/test/cli/routing_http_test.go b/test/cli/routing_http_test.go new file mode 100644 index 00000000000..7e90eb65c7e --- /dev/null +++ b/test/cli/routing_http_test.go @@ -0,0 +1,134 @@ +package cli + +import ( + "context" + "testing" + + "github.com/ipfs/boxo/ipns" + "github.com/ipfs/boxo/routing/http/client" + "github.com/ipfs/boxo/routing/http/types" + "github.com/ipfs/boxo/routing/http/types/iter" + "github.com/ipfs/go-cid" + "github.com/ipfs/kubo/config" + "github.com/ipfs/kubo/test/cli/harness" + "github.com/libp2p/go-libp2p/core/peer" + "github.com/stretchr/testify/assert" +) + +func TestRoutingV1(t *testing.T) { + t.Parallel() + + nodes := harness.NewT(t).NewNodes(5).Init() + nodes.ForEachPar(func(node *harness.Node) { + node.UpdateConfig(func(cfg *config.Config) { + cfg.Gateway.ExposeRoutingAPI = config.True + cfg.Routing.Type = config.NewOptionalString("dht") + }) + }) + nodes.StartDaemons().Connect() + + t.Run("Get Providers Responds With Correct Peers", func(t *testing.T) { + t.Parallel() + + cidStr := nodes[2].IPFSAddStr("hello world") + _ = nodes[3].IPFSAddStr("hello world") + + cid, err := cid.Decode(cidStr) + assert.NoError(t, err) + + c, err := client.New(nodes[1].GatewayURL()) + assert.NoError(t, err) + + resultsIter, err := c.GetProviders(context.Background(), cid) + assert.NoError(t, err) + + records, err := iter.ReadAllResults(resultsIter) + assert.NoError(t, err) + + var peers []peer.ID + for _, record := range records { + assert.Equal(t, types.SchemaPeer, record.GetSchema()) + + peer, ok := record.(*types.PeerRecord) + assert.True(t, ok) + peers = append(peers, *peer.ID) + } + + assert.Contains(t, peers, nodes[2].PeerID()) + assert.Contains(t, peers, nodes[3].PeerID()) + }) + + t.Run("Get Peers Responds With Correct Peers", func(t *testing.T) { + t.Parallel() + + c, err := client.New(nodes[1].GatewayURL()) + assert.NoError(t, err) + + resultsIter, err := c.GetPeers(context.Background(), nodes[2].PeerID()) + assert.NoError(t, err) + + records, err := iter.ReadAllResults(resultsIter) + assert.NoError(t, err) + assert.Len(t, records, 1) + assert.IsType(t, records[0].GetSchema(), records[0].GetSchema()) + assert.IsType(t, records[0], &types.PeerRecord{}) + + peer := records[0].(*types.PeerRecord) + assert.Equal(t, nodes[2].PeerID().String(), peer.ID.String()) + assert.NotEmpty(t, peer.Addrs) + }) + + t.Run("Get IPNS Record Responds With Correct Record", func(t *testing.T) { + t.Parallel() + + cidStr := nodes[0].IPFSAddStr("hello get ipns test") + nodes[0].IPFS("name", "publish", "--allow-offline", cidStr) + + // Ask for record from a different peer. + c, err := client.New(nodes[1].GatewayURL()) + assert.NoError(t, err) + + record, err := c.GetIPNSRecord(context.Background(), ipns.NameFromPeer(nodes[0].PeerID())) + assert.NoError(t, err) + + value, err := record.Value() + assert.NoError(t, err) + assert.Equal(t, "/ipfs/"+cidStr, value.String()) + }) + + t.Run("Put IPNS Record Succeeds", func(t *testing.T) { + t.Parallel() + + // Publish a record. + cidStr := nodes[0].IPFSAddStr("hello get ipns test 2") + nodes[0].IPFS("name", "publish", "--allow-offline", cidStr) + c, err := client.New(nodes[0].GatewayURL()) + assert.NoError(t, err) + record, err := c.GetIPNSRecord(context.Background(), ipns.NameFromPeer(nodes[0].PeerID())) + assert.NoError(t, err) + value, err := record.Value() + assert.NoError(t, err) + assert.Equal(t, "/ipfs/"+cidStr, value.String()) + + // Start lonely node that is not connected to other nodes. + node := harness.NewT(t).NewNode().Init() + node.UpdateConfig(func(cfg *config.Config) { + cfg.Gateway.ExposeRoutingAPI = config.True + cfg.Routing.Type = config.NewOptionalString("dht") + }) + node.StartDaemon() + + // Put IPNS record in lonely node. It should be accepted as it is a valid record. + c, err = client.New(node.GatewayURL()) + assert.NoError(t, err) + err = c.PutIPNSRecord(context.Background(), ipns.NameFromPeer(nodes[0].PeerID()), record) + assert.NoError(t, err) + + // Get the record from lonely node and double check. + record, err = c.GetIPNSRecord(context.Background(), ipns.NameFromPeer(nodes[0].PeerID())) + assert.NoError(t, err) + value, err = record.Value() + assert.NoError(t, err) + assert.Equal(t, "/ipfs/"+cidStr, value.String()) + }) +} diff --git a/test/dependencies/go.mod b/test/dependencies/go.mod index bfee9a0a7e6..1f45aac172c 100644 --- a/test/dependencies/go.mod +++ b/test/dependencies/go.mod @@ -7,7 +7,7 @@ replace github.com/ipfs/kubo => ../../ require ( github.com/Kubuxu/gocovmerge v0.0.0-20161216165753-7ecaa51963cd github.com/golangci/golangci-lint v1.49.0 - github.com/ipfs/boxo v0.11.0 + github.com/ipfs/boxo v0.11.1-0.20230809093309-ecc6fb41f53d github.com/ipfs/go-cid v0.4.1 github.com/ipfs/go-cidutil v0.1.0 github.com/ipfs/go-datastore v0.6.0 @@ -16,7 +16,7 @@ require ( github.com/ipfs/hang-fds v0.1.0 github.com/ipfs/iptb v1.4.0 github.com/ipfs/iptb-plugins v0.5.0 - github.com/ipld/go-ipld-prime v0.20.0 + github.com/ipld/go-ipld-prime v0.20.1-0.20230619045951-499e9b9c2024 github.com/jbenet/go-random v0.0.0-20190219211222-123a90aedc0c github.com/jbenet/go-random-files v0.0.0-20190219210431-31b3f20ebded github.com/libp2p/go-libp2p v0.29.2 diff --git a/test/dependencies/go.sum b/test/dependencies/go.sum index 4967be85f1e..fe55ad125da 100644 --- a/test/dependencies/go.sum +++ b/test/dependencies/go.sum @@ -180,7 +180,7 @@ github.com/flynn/noise v1.0.0 h1:DlTHqmzmvcEiKj+4RYo/imoswx/4r6iBlCMfVtrMXpQ= github.com/flynn/noise v1.0.0/go.mod h1:xbMo+0i6+IGbYdJhF31t2eR1BIU0CYc12+BNAKwUTag= github.com/francoispqt/gojay v1.2.13 h1:d2m3sFjloqoIUQU3TsHBgj6qg/BVGlTBeHDUmyJnXKk= github.com/francoispqt/gojay v1.2.13/go.mod h1:ehT5mTG4ua4581f1++1WLG0vPdaA9HaiDsoyrBGkyDY= -github.com/frankban/quicktest v1.14.4 h1:g2rn0vABPOOXmZUj+vbmUp0lPoXEMuhTpIluN0XL9UY= +github.com/frankban/quicktest v1.14.5 h1:dfYrrRyLtiqT9GyKXgdh+k4inNeTvmGbuSgZ3lx3GhA= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY= github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw= @@ -395,8 +395,8 @@ github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NH github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/ipfs/bbloom v0.0.4 h1:Gi+8EGJ2y5qiD5FbsbpX/TMNcJw8gSqr7eyjHa4Fhvs= github.com/ipfs/bbloom v0.0.4/go.mod h1:cS9YprKXpoZ9lT0n/Mw/a6/aFV6DTjTLYHeA+gyqMG0= -github.com/ipfs/boxo v0.11.0 h1:urMxhZ3xoF4HssJVD3+0ssGT9pptEfHfbL8DYdoWFlg= -github.com/ipfs/boxo v0.11.0/go.mod h1:8IfDmp+FzFGcF4zjAgHMVPpwYw4AjN9ePEzDfkaYJ1w= +github.com/ipfs/boxo v0.11.1-0.20230809093309-ecc6fb41f53d h1:11P/IcImiaLEOFc+xGiFza4/xnVicYIcbyawQixF8Qg= +github.com/ipfs/boxo v0.11.1-0.20230809093309-ecc6fb41f53d/go.mod h1:Tbv+IR9baTM1VprtVTUMC2EPu6lR4803DahiQk5IQMk= github.com/ipfs/go-bitfield v1.1.0 h1:fh7FIo8bSwaJEh6DdTWbCeZ1eqOaOkKFI74SCnsWbGA= github.com/ipfs/go-bitfield v1.1.0/go.mod h1:paqf1wjq/D2BBmzfTVFlJQ9IlFOZpg422HL0HqsGWHU= github.com/ipfs/go-block-format v0.1.2 h1:GAjkfhVx1f4YTODS6Esrj1wt2HhrtwTnhEr+DyPUaJo= @@ -461,8 +461,8 @@ github.com/ipld/go-car v0.5.0 h1:kcCEa3CvYMs0iE5BzD5sV7O2EwMiCIp3uF8tA6APQT8= github.com/ipld/go-car/v2 v2.10.2-0.20230622090957-499d0c909d33 h1:0OZwzSYWIuiKEOXd/2vm5cMcEmmGLFn+1h6lHELCm3s= github.com/ipld/go-codec-dagpb v1.6.0 h1:9nYazfyu9B1p3NAgfVdpRco3Fs2nFC72DqVsMj6rOcc= github.com/ipld/go-codec-dagpb v1.6.0/go.mod h1:ANzFhfP2uMJxRBr8CE+WQWs5UsNa0pYtmKZ+agnUw9s= -github.com/ipld/go-ipld-prime v0.20.0 h1:Ud3VwE9ClxpO2LkCYP7vWPc0Fo+dYdYzgxUJZ3uRG4g= -github.com/ipld/go-ipld-prime v0.20.0/go.mod h1:PzqZ/ZR981eKbgdr3y2DJYeD/8bgMawdGVlJDE8kK+M= +github.com/ipld/go-ipld-prime v0.20.1-0.20230619045951-499e9b9c2024 h1:HfSCtpmGw0fzCfhZKzASxVjNk8tA1oSCDEF/To3eVg0= +github.com/ipld/go-ipld-prime v0.20.1-0.20230619045951-499e9b9c2024/go.mod h1:PRQpXNcJypaPiiSdarsrJABPkYrBvafwDl0B9HjujZ8= github.com/jackpal/go-nat-pmp v1.0.2 h1:KzKSgb7qkJvOUTqYl9/Hg/me3pWgBmERKrTGD7BdWus= github.com/jackpal/go-nat-pmp v1.0.2/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+4orBN1SBKc= github.com/jbenet/go-cienv v0.1.0/go.mod h1:TqNnHUmJgXau0nCzC7kXWeotg3J9W34CUv5Djy1+FlA= @@ -892,7 +892,7 @@ github.com/uudashr/gocognit v1.0.6 h1:2Cgi6MweCsdB6kpcVQp7EW4U23iBFQWfTXiWlyp842 github.com/uudashr/gocognit v1.0.6/go.mod h1:nAIUuVBnYU7pcninia3BHOvQkpQCeO76Uscky5BOwcY= github.com/viant/assertly v0.4.8/go.mod h1:aGifi++jvCrUaklKEKT0BU95igDNaqkvz+49uaYMPRU= github.com/viant/toolbox v0.24.0/go.mod h1:OxMCG57V0PXuIP2HNQrtJf2CjqdmbrOx5EkMILuUhzM= -github.com/warpfork/go-testmark v0.11.0 h1:J6LnV8KpceDvo7spaNU4+DauH2n1x+6RaO2rJrmpQ9U= +github.com/warpfork/go-testmark v0.12.1 h1:rMgCpJfwy1sJ50x0M0NgyphxYYPMOODIJHhsXyEHU0s= github.com/warpfork/go-wish v0.0.0-20220906213052-39a1cc7a02d0 h1:GDDkbFiaK8jsSDJfjId/PEGEShv6ugrt4kYsC5UIDaQ= github.com/warpfork/go-wish v0.0.0-20220906213052-39a1cc7a02d0/go.mod h1:x6AKhvSSexNrVSrViXSHUEbICjmGXhtgABaHIySUSGw= github.com/whyrusleeping/cbor v0.0.0-20171005072247-63513f603b11 h1:5HZfQkwe0mIfyDmc1Em5GqlNRzcdtlv4HTNmdpt7XH0=