Skip to content
This repository was archived by the owner on Jun 20, 2024. It is now read-only.

Commit 48dfb39

Browse files
committed
feat: fail fast ipns lookup and fix encoding
1 parent edc8e70 commit 48dfb39

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

gateway.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
gopath "path"
78

@@ -28,7 +29,9 @@ import (
2829
"github.com/ipld/go-ipld-prime"
2930
"github.com/ipld/go-ipld-prime/node/basicnode"
3031
"github.com/ipld/go-ipld-prime/schema"
32+
"github.com/libp2p/go-libp2p/core/peer"
3133
"github.com/libp2p/go-libp2p/core/routing"
34+
mc "github.com/multiformats/go-multicodec"
3235
)
3336

3437
type bifrostGateway struct {
@@ -112,7 +115,19 @@ func (api *bifrostGateway) GetBlock(ctx context.Context, c cid.Cid) (blocks.Bloc
112115
}
113116

114117
func (api *bifrostGateway) GetIPNSRecord(ctx context.Context, c cid.Cid) ([]byte, error) {
115-
return api.routing.GetValue(ctx, "/ipns/"+c.String())
118+
// Fails fast if the CID is not an encoded Libp2p Key, avoids wasteful
119+
// round trips to the remote routing provider.
120+
if mc.Code(c.Type()) != mc.Libp2pKey {
121+
return nil, errors.New("provided cid is not an encoded libp2p key")
122+
}
123+
124+
// The value store expects the key itself to be encoded as a multihash.
125+
id, err := peer.FromCid(c)
126+
if err != nil {
127+
return nil, err
128+
}
129+
130+
return api.routing.GetValue(ctx, "/ipns/"+string(id))
116131
}
117132

118133
func (api *bifrostGateway) GetDNSLinkRecord(ctx context.Context, hostname string) (ifacepath.Path, error) {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ require (
2222
github.com/ipld/go-codec-dagpb v1.5.0
2323
github.com/ipld/go-ipld-prime v0.19.0
2424
github.com/libp2p/go-libp2p v0.23.4
25+
github.com/multiformats/go-multicodec v0.7.0
2526
github.com/prometheus/client_golang v1.14.0
2627
github.com/spf13/cobra v1.6.1
2728
)
@@ -83,7 +84,6 @@ require (
8384
github.com/multiformats/go-multiaddr v0.8.0 // indirect
8485
github.com/multiformats/go-multiaddr-dns v0.3.1 // indirect
8586
github.com/multiformats/go-multibase v0.1.1 // indirect
86-
github.com/multiformats/go-multicodec v0.7.0 // indirect
8787
github.com/multiformats/go-multihash v0.2.1 // indirect
8888
github.com/multiformats/go-varint v0.0.7 // indirect
8989
github.com/opencontainers/runtime-spec v1.0.3-0.20211123151946-c2389c3cb60a // indirect

0 commit comments

Comments
 (0)