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

Commit

Permalink
feat: fail fast ipns lookup and fix encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Feb 8, 2023
1 parent edc8e70 commit 48dfb39
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
17 changes: 16 additions & 1 deletion gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"errors"
"fmt"
gopath "path"

Expand All @@ -28,7 +29,9 @@ import (
"github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/node/basicnode"
"github.com/ipld/go-ipld-prime/schema"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/routing"
mc "github.com/multiformats/go-multicodec"
)

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

func (api *bifrostGateway) GetIPNSRecord(ctx context.Context, c cid.Cid) ([]byte, error) {
return api.routing.GetValue(ctx, "/ipns/"+c.String())
// Fails fast if the CID is not an encoded Libp2p Key, avoids wasteful
// round trips to the remote routing provider.
if mc.Code(c.Type()) != mc.Libp2pKey {
return nil, errors.New("provided cid is not an encoded libp2p key")
}

// The value store expects the key itself to be encoded as a multihash.
id, err := peer.FromCid(c)
if err != nil {
return nil, err
}

return api.routing.GetValue(ctx, "/ipns/"+string(id))
}

func (api *bifrostGateway) GetDNSLinkRecord(ctx context.Context, hostname string) (ifacepath.Path, error) {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ require (
github.com/ipld/go-codec-dagpb v1.5.0
github.com/ipld/go-ipld-prime v0.19.0
github.com/libp2p/go-libp2p v0.23.4
github.com/multiformats/go-multicodec v0.7.0
github.com/prometheus/client_golang v1.14.0
github.com/spf13/cobra v1.6.1
)
Expand Down Expand Up @@ -83,7 +84,6 @@ require (
github.com/multiformats/go-multiaddr v0.8.0 // indirect
github.com/multiformats/go-multiaddr-dns v0.3.1 // indirect
github.com/multiformats/go-multibase v0.1.1 // indirect
github.com/multiformats/go-multicodec v0.7.0 // indirect
github.com/multiformats/go-multihash v0.2.1 // indirect
github.com/multiformats/go-varint v0.0.7 // indirect
github.com/opencontainers/runtime-spec v1.0.3-0.20211123151946-c2389c3cb60a // indirect
Expand Down

0 comments on commit 48dfb39

Please sign in to comment.