diff --git a/core/coreapi/interface/name.go b/core/coreapi/interface/name.go index 782f68351..a02bc0787 100644 --- a/core/coreapi/interface/name.go +++ b/core/coreapi/interface/name.go @@ -40,7 +40,7 @@ type NameAPI interface { // Search is a version of Resolve which outputs paths as they are discovered, // reducing the time to first entry // - // Note that by default only the last path returned before the channel closes - // can be considered 'safe'. + // Note: by default, all paths read from the channel are considered unsafe, + // except the latest (last path in channel read buffer). Search(ctx context.Context, name string, opts ...options.NameResolveOption) (<-chan IpnsResult, error) } diff --git a/namesys/base.go b/namesys/base.go index 508847ac3..1906bdf5d 100644 --- a/namesys/base.go +++ b/namesys/base.go @@ -21,14 +21,14 @@ type resolver interface { } // resolve is a helper for implementing Resolver.ResolveN using resolveOnce. -func resolve(ctx context.Context, r resolver, name string, options opts.ResolveOpts, prefix string) (path.Path, error) { +func resolve(ctx context.Context, r resolver, name string, options opts.ResolveOpts) (path.Path, error) { ctx, cancel := context.WithCancel(ctx) defer cancel() err := ErrResolveFailed var p path.Path - resCh := resolveAsync(ctx, r, name, options, prefix) + resCh := resolveAsync(ctx, r, name, options) for res := range resCh { p, err = res.Path, res.Err @@ -40,7 +40,7 @@ func resolve(ctx context.Context, r resolver, name string, options opts.ResolveO return p, err } -func resolveAsync(ctx context.Context, r resolver, name string, options opts.ResolveOpts, prefix string) <-chan Result { +func resolveAsync(ctx context.Context, r resolver, name string, options opts.ResolveOpts) <-chan Result { resCh := r.resolveOnceAsync(ctx, name, options) depth := options.Depth outCh := make(chan Result, 1) @@ -86,8 +86,8 @@ func resolveAsync(ctx context.Context, r resolver, name string, options opts.Res subCtx, cancelSub = context.WithCancel(ctx) defer cancelSub() - p := strings.TrimPrefix(res.value.String(), prefix) - subCh = resolveAsync(subCtx, r, p, subopts, prefix) + p := strings.TrimPrefix(res.value.String(), ipnsPrefix) + subCh = resolveAsync(subCtx, r, p, subopts) case res, ok := <-subCh: if !ok { subCh = nil diff --git a/namesys/dns.go b/namesys/dns.go index f4d37e654..d3f9e0956 100644 --- a/namesys/dns.go +++ b/namesys/dns.go @@ -28,12 +28,12 @@ func NewDNSResolver() *DNSResolver { // Resolve implements Resolver. func (r *DNSResolver) Resolve(ctx context.Context, name string, options ...opts.ResolveOpt) (path.Path, error) { - return resolve(ctx, r, name, opts.ProcessOpts(options), "/ipns/") + return resolve(ctx, r, name, opts.ProcessOpts(options)) } // ResolveAsync implements Resolver. func (r *DNSResolver) ResolveAsync(ctx context.Context, name string, options ...opts.ResolveOpt) <-chan Result { - return resolveAsync(ctx, r, name, opts.ProcessOpts(options), "/ipns/") + return resolveAsync(ctx, r, name, opts.ProcessOpts(options)) } type lookupRes struct { diff --git a/namesys/ipns_resolver_validation_test.go b/namesys/ipns_resolver_validation_test.go index e12841421..5bcef4e14 100644 --- a/namesys/ipns_resolver_validation_test.go +++ b/namesys/ipns_resolver_validation_test.go @@ -5,9 +5,10 @@ import ( "testing" "time" - opts "github.com/ipfs/go-ipfs/namesys/opts" path "gx/ipfs/QmdrpbDgeYH3VxkCciQCJY5LkDYdXtig6unDzQmMxFtWEw/go-path" + opts "github.com/ipfs/go-ipfs/namesys/opts" + testutil "gx/ipfs/QmNfQbgBfARAtrYsBguChX6VJ5nbjeoYy1KdC36aaYWqG8/go-testutil" u "gx/ipfs/QmPdKqUcHGFdeSpvjVoaTRPPstGif9GBZb5Q56RVw9o69A/go-ipfs-util" routing "gx/ipfs/QmPmFeQ5oY5G6M7aBWggi5phxEPXwsQntE1DFcUzETULdp/go-libp2p-routing" @@ -57,7 +58,7 @@ func TestResolverValidation(t *testing.T) { } // Resolve entry - resp, err := resolve(ctx, resolver, id.Pretty(), opts.DefaultResolveOpts(), "/ipns/") + resp, err := resolve(ctx, resolver, id.Pretty(), opts.DefaultResolveOpts()) if err != nil { t.Fatal(err) } @@ -77,7 +78,7 @@ func TestResolverValidation(t *testing.T) { } // Record should fail validation because entry is expired - _, err = resolve(ctx, resolver, id.Pretty(), opts.DefaultResolveOpts(), "/ipns/") + _, err = resolve(ctx, resolver, id.Pretty(), opts.DefaultResolveOpts()) if err == nil { t.Fatal("ValidateIpnsRecord should have returned error") } @@ -99,7 +100,7 @@ func TestResolverValidation(t *testing.T) { // Record should fail validation because public key defined by // ipns path doesn't match record signature - _, err = resolve(ctx, resolver, id2.Pretty(), opts.DefaultResolveOpts(), "/ipns/") + _, err = resolve(ctx, resolver, id2.Pretty(), opts.DefaultResolveOpts()) if err == nil { t.Fatal("ValidateIpnsRecord should have failed signature verification") } @@ -117,7 +118,7 @@ func TestResolverValidation(t *testing.T) { // Record should fail validation because public key is not available // in peer store or on network - _, err = resolve(ctx, resolver, id3.Pretty(), opts.DefaultResolveOpts(), "/ipns/") + _, err = resolve(ctx, resolver, id3.Pretty(), opts.DefaultResolveOpts()) if err == nil { t.Fatal("ValidateIpnsRecord should have failed because public key was not found") } @@ -132,7 +133,7 @@ func TestResolverValidation(t *testing.T) { // public key is available in the peer store by looking it up in // the DHT, which causes the DHT to fetch it and cache it in the // peer store - _, err = resolve(ctx, resolver, id3.Pretty(), opts.DefaultResolveOpts(), "/ipns/") + _, err = resolve(ctx, resolver, id3.Pretty(), opts.DefaultResolveOpts()) if err != nil { t.Fatal(err) } diff --git a/namesys/namesys.go b/namesys/namesys.go index 83cd7dbcc..50d302079 100644 --- a/namesys/namesys.go +++ b/namesys/namesys.go @@ -62,7 +62,7 @@ func (ns *mpns) Resolve(ctx context.Context, name string, options ...opts.Resolv return path.ParsePath("/ipfs/" + name) } - return resolve(ctx, ns, name, opts.ProcessOpts(options), "/ipns/") + return resolve(ctx, ns, name, opts.ProcessOpts(options)) } func (ns *mpns) ResolveAsync(ctx context.Context, name string, options ...opts.ResolveOpt) <-chan Result { @@ -79,7 +79,7 @@ func (ns *mpns) ResolveAsync(ctx context.Context, name string, options ...opts.R return res } - return resolveAsync(ctx, ns, name, opts.ProcessOpts(options), "/ipns/") + return resolveAsync(ctx, ns, name, opts.ProcessOpts(options)) } // resolveOnce implements resolver. diff --git a/namesys/proquint.go b/namesys/proquint.go index 0caaf9497..894579313 100644 --- a/namesys/proquint.go +++ b/namesys/proquint.go @@ -4,16 +4,17 @@ import ( "context" "errors" - opts "github.com/ipfs/go-ipfs/namesys/opts" proquint "gx/ipfs/QmYnf27kzqR2cxt6LFZdrAFJuQd6785fTkBvMuEj9EeRxM/proquint" path "gx/ipfs/QmdrpbDgeYH3VxkCciQCJY5LkDYdXtig6unDzQmMxFtWEw/go-path" + + opts "github.com/ipfs/go-ipfs/namesys/opts" ) type ProquintResolver struct{} // Resolve implements Resolver. func (r *ProquintResolver) Resolve(ctx context.Context, name string, options ...opts.ResolveOpt) (path.Path, error) { - return resolve(ctx, r, name, opts.ProcessOpts(options), "/ipns/") + return resolve(ctx, r, name, opts.ProcessOpts(options)) } // resolveOnce implements resolver. Decodes the proquint string. diff --git a/namesys/routing.go b/namesys/routing.go index ef7e376e6..25daafff4 100644 --- a/namesys/routing.go +++ b/namesys/routing.go @@ -5,9 +5,10 @@ import ( "strings" "time" - opts "github.com/ipfs/go-ipfs/namesys/opts" path "gx/ipfs/QmdrpbDgeYH3VxkCciQCJY5LkDYdXtig6unDzQmMxFtWEw/go-path" + opts "github.com/ipfs/go-ipfs/namesys/opts" + cid "gx/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7/go-cid" routing "gx/ipfs/QmPmFeQ5oY5G6M7aBWggi5phxEPXwsQntE1DFcUzETULdp/go-libp2p-routing" mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash" @@ -39,12 +40,12 @@ func NewIpnsResolver(route routing.ValueStore) *IpnsResolver { // Resolve implements Resolver. func (r *IpnsResolver) Resolve(ctx context.Context, name string, options ...opts.ResolveOpt) (path.Path, error) { - return resolve(ctx, r, name, opts.ProcessOpts(options), "/ipns/") + return resolve(ctx, r, name, opts.ProcessOpts(options)) } // ResolveAsync implements Resolver. func (r *IpnsResolver) ResolveAsync(ctx context.Context, name string, options ...opts.ResolveOpt) <-chan Result { - return resolveAsync(ctx, r, name, opts.ProcessOpts(options), "/ipns/") + return resolveAsync(ctx, r, name, opts.ProcessOpts(options)) } // resolveOnce implements resolver. Uses the IPFS routing system to