From 81bc263f51d9116fadba5e88f1f5f4deeb5a570f Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Tue, 13 Jun 2023 18:18:21 +0200 Subject: [PATCH] feat: replace IpnsEntry by ipns.Name --- coreiface/name.go | 11 +- coreiface/tests/name.go | 217 ++++++++++--------------------------- coreiface/tests/routing.go | 79 +++++--------- 3 files changed, 85 insertions(+), 222 deletions(-) diff --git a/coreiface/name.go b/coreiface/name.go index 0c06183e69..8c3e8e89a4 100644 --- a/coreiface/name.go +++ b/coreiface/name.go @@ -5,20 +5,13 @@ import ( "errors" path "github.com/ipfs/boxo/coreiface/path" + "github.com/ipfs/boxo/ipns" "github.com/ipfs/boxo/coreiface/options" ) var ErrResolveFailed = errors.New("could not resolve name") -// IpnsEntry specifies the interface to IpnsEntries -type IpnsEntry interface { - // Name returns IpnsEntry name - Name() string - // Value returns IpnsEntry value - Value() path.Path -} - type IpnsResult struct { path.Path Err error @@ -34,7 +27,7 @@ type IpnsResult struct { // You can use .Key API to list and generate more names and their respective keys. type NameAPI interface { // Publish announces new IPNS name - Publish(ctx context.Context, path path.Path, opts ...options.NamePublishOption) (IpnsEntry, error) + Publish(ctx context.Context, path path.Path, opts ...options.NamePublishOption) (ipns.Name, error) // Resolve attempts to resolve the newest version of the specified name Resolve(ctx context.Context, name string, opts ...options.NameResolveOption) (path.Path, error) diff --git a/coreiface/tests/name.go b/coreiface/tests/name.go index 8fa733567b..ab55d0425a 100644 --- a/coreiface/tests/name.go +++ b/coreiface/tests/name.go @@ -8,12 +8,12 @@ import ( "testing" "time" - path "github.com/ipfs/boxo/coreiface/path" - - "github.com/ipfs/boxo/files" - coreiface "github.com/ipfs/boxo/coreiface" opt "github.com/ipfs/boxo/coreiface/options" + path "github.com/ipfs/boxo/coreiface/path" + "github.com/ipfs/boxo/files" + "github.com/ipfs/boxo/ipns" + "github.com/stretchr/testify/require" ) func (tp *TestSuite) TestName(t *testing.T) { @@ -44,138 +44,68 @@ func (tp *TestSuite) TestPublishResolve(t *testing.T) { defer cancel() init := func() (coreiface.CoreAPI, path.Path) { apis, err := tp.MakeAPISwarm(t, ctx, 5) - if err != nil { - t.Fatal(err) - return nil, nil - } + require.NoError(t, err) api := apis[0] p, err := addTestObject(ctx, api) - if err != nil { - t.Fatal(err) - return nil, nil - } + require.NoError(t, err) return api, p } run := func(t *testing.T, ropts []opt.NameResolveOption) { t.Run("basic", func(t *testing.T) { api, p := init() - e, err := api.Name().Publish(ctx, p) - if err != nil { - t.Fatal(err) - } + name, err := api.Name().Publish(ctx, p) + require.NoError(t, err) self, err := api.Key().Self(ctx) - if err != nil { - t.Fatal(err) - } - - if e.Name() != coreiface.FormatKeyID(self.ID()) { - t.Errorf("expected e.Name to equal '%s', got '%s'", coreiface.FormatKeyID(self.ID()), e.Name()) - } - - if e.Value().String() != p.String() { - t.Errorf("expected paths to match, '%s'!='%s'", e.Value().String(), p.String()) - } - - resPath, err := api.Name().Resolve(ctx, e.Name(), ropts...) - if err != nil { - t.Fatal(err) - } - - if resPath.String() != p.String() { - t.Errorf("expected paths to match, '%s'!='%s'", resPath.String(), p.String()) - } + require.NoError(t, err) + require.Equal(t, name.String(), ipns.NameFromPeer(self.ID()).String()) + + resPath, err := api.Name().Resolve(ctx, name.String(), ropts...) + require.NoError(t, err) + require.Equal(t, p.String(), resPath.String()) }) t.Run("publishPath", func(t *testing.T) { api, p := init() - e, err := api.Name().Publish(ctx, appendPath(p, "/test")) - if err != nil { - t.Fatal(err) - } + name, err := api.Name().Publish(ctx, appendPath(p, "/test")) + require.NoError(t, err) self, err := api.Key().Self(ctx) - if err != nil { - t.Fatal(err) - } - - if e.Name() != coreiface.FormatKeyID(self.ID()) { - t.Errorf("expected e.Name to equal '%s', got '%s'", coreiface.FormatKeyID(self.ID()), e.Name()) - } - - if e.Value().String() != p.String()+"/test" { - t.Errorf("expected paths to match, '%s'!='%s'", e.Value().String(), p.String()) - } - - resPath, err := api.Name().Resolve(ctx, e.Name(), ropts...) - if err != nil { - t.Fatal(err) - } - - if resPath.String() != p.String()+"/test" { - t.Errorf("expected paths to match, '%s'!='%s'", resPath.String(), p.String()+"/test") - } + require.NoError(t, err) + require.Equal(t, name.String(), ipns.NameFromPeer(self.ID()).String()) + + resPath, err := api.Name().Resolve(ctx, name.String(), ropts...) + require.NoError(t, err) + require.Equal(t, p.String()+"/test", resPath.String()) }) t.Run("revolvePath", func(t *testing.T) { api, p := init() - e, err := api.Name().Publish(ctx, p) - if err != nil { - t.Fatal(err) - } + name, err := api.Name().Publish(ctx, p) + require.NoError(t, err) self, err := api.Key().Self(ctx) - if err != nil { - t.Fatal(err) - } - - if e.Name() != coreiface.FormatKeyID(self.ID()) { - t.Errorf("expected e.Name to equal '%s', got '%s'", coreiface.FormatKeyID(self.ID()), e.Name()) - } - - if e.Value().String() != p.String() { - t.Errorf("expected paths to match, '%s'!='%s'", e.Value().String(), p.String()) - } - - resPath, err := api.Name().Resolve(ctx, e.Name()+"/test", ropts...) - if err != nil { - t.Fatal(err) - } - - if resPath.String() != p.String()+"/test" { - t.Errorf("expected paths to match, '%s'!='%s'", resPath.String(), p.String()+"/test") - } + require.NoError(t, err) + require.Equal(t, name.String(), ipns.NameFromPeer(self.ID()).String()) + + resPath, err := api.Name().Resolve(ctx, name.String()+"/test", ropts...) + require.NoError(t, err) + require.Equal(t, p.String()+"/test", resPath.String()) }) t.Run("publishRevolvePath", func(t *testing.T) { api, p := init() - e, err := api.Name().Publish(ctx, appendPath(p, "/a")) - if err != nil { - t.Fatal(err) - } + name, err := api.Name().Publish(ctx, appendPath(p, "/a")) + require.NoError(t, err) self, err := api.Key().Self(ctx) - if err != nil { - t.Fatal(err) - } - - if e.Name() != coreiface.FormatKeyID(self.ID()) { - t.Errorf("expected e.Name to equal '%s', got '%s'", coreiface.FormatKeyID(self.ID()), e.Name()) - } - - if e.Value().String() != p.String()+"/a" { - t.Errorf("expected paths to match, '%s'!='%s'", e.Value().String(), p.String()) - } - - resPath, err := api.Name().Resolve(ctx, e.Name()+"/b", ropts...) - if err != nil { - t.Fatal(err) - } - - if resPath.String() != p.String()+"/a/b" { - t.Errorf("expected paths to match, '%s'!='%s'", resPath.String(), p.String()+"/a/b") - } + require.NoError(t, err) + require.Equal(t, name.String(), ipns.NameFromPeer(self.ID()).String()) + + resPath, err := api.Name().Resolve(ctx, name.String()+"/b", ropts...) + require.NoError(t, err) + require.Equal(t, p.String()+"/a/b", resPath.String()) }) } @@ -192,42 +122,22 @@ func (tp *TestSuite) TestBasicPublishResolveKey(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() apis, err := tp.MakeAPISwarm(t, ctx, 5) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) api := apis[0] k, err := api.Key().Generate(ctx, "foo") - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) p, err := addTestObject(ctx, api) - if err != nil { - t.Fatal(err) - } - - e, err := api.Name().Publish(ctx, p, opt.Name.Key(k.Name())) - if err != nil { - t.Fatal(err) - } - - if e.Name() != coreiface.FormatKey(k) { - t.Errorf("expected e.Name to equal %s, got '%s'", e.Name(), coreiface.FormatKey(k)) - } - - if e.Value().String() != p.String() { - t.Errorf("expected paths to match, '%s'!='%s'", e.Value().String(), p.String()) - } + require.NoError(t, err) - resPath, err := api.Name().Resolve(ctx, e.Name()) - if err != nil { - t.Fatal(err) - } + name, err := api.Name().Publish(ctx, p, opt.Name.Key(k.Name())) + require.NoError(t, err) + require.Equal(t, name.String(), ipns.NameFromPeer(k.ID()).String()) - if resPath.String() != p.String() { - t.Errorf("expected paths to match, '%s'!='%s'", resPath.String(), p.String()) - } + resPath, err := api.Name().Resolve(ctx, name.String()) + require.NoError(t, err) + require.Equal(t, p.String(), resPath.String()) } func (tp *TestSuite) TestBasicPublishResolveTimeout(t *testing.T) { @@ -236,39 +146,22 @@ func (tp *TestSuite) TestBasicPublishResolveTimeout(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() apis, err := tp.MakeAPISwarm(t, ctx, 5) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) api := apis[0] p, err := addTestObject(ctx, api) - if err != nil { - t.Fatal(err) - } - - e, err := api.Name().Publish(ctx, p, opt.Name.ValidTime(time.Millisecond*100)) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) self, err := api.Key().Self(ctx) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) - if e.Name() != coreiface.FormatKeyID(self.ID()) { - t.Errorf("expected e.Name to equal '%s', got '%s'", coreiface.FormatKeyID(self.ID()), e.Name()) - } - - if e.Value().String() != p.String() { - t.Errorf("expected paths to match, '%s'!='%s'", e.Value().String(), p.String()) - } + name, err := api.Name().Publish(ctx, p, opt.Name.ValidTime(time.Millisecond*100)) + require.NoError(t, err) + require.Equal(t, name.String(), ipns.NameFromPeer(self.ID()).String()) time.Sleep(time.Second) - _, err = api.Name().Resolve(ctx, e.Name()) - if err == nil { - t.Fatal("Expected an error") - } + _, err = api.Name().Resolve(ctx, name.String()) + require.NoError(t, err) } //TODO: When swarm api is created, add multinode tests diff --git a/coreiface/tests/routing.go b/coreiface/tests/routing.go index 8bdc0d3f8b..4e96324750 100644 --- a/coreiface/tests/routing.go +++ b/coreiface/tests/routing.go @@ -7,6 +7,7 @@ import ( iface "github.com/ipfs/boxo/coreiface" "github.com/ipfs/boxo/coreiface/options" + "github.com/ipfs/boxo/coreiface/path" "github.com/ipfs/boxo/ipns" "github.com/stretchr/testify/require" ) @@ -24,19 +25,15 @@ func (tp *TestSuite) TestRouting(t *testing.T) { t.Run("TestRoutingPutOffline", tp.TestRoutingPutOffline) } -func (tp *TestSuite) testRoutingPublishKey(t *testing.T, ctx context.Context, api iface.CoreAPI, opts ...options.NamePublishOption) iface.IpnsEntry { +func (tp *TestSuite) testRoutingPublishKey(t *testing.T, ctx context.Context, api iface.CoreAPI, opts ...options.NamePublishOption) (path.Path, ipns.Name) { p, err := addTestObject(ctx, api) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) - entry, err := api.Name().Publish(ctx, p, opts...) - if err != nil { - t.Fatal(err) - } + name, err := api.Name().Publish(ctx, p, opts...) + require.NoError(t, err) time.Sleep(3 * time.Second) - return entry + return p, name } func (tp *TestSuite) TestRoutingGet(t *testing.T) { @@ -44,49 +41,39 @@ func (tp *TestSuite) TestRoutingGet(t *testing.T) { defer cancel() apis, err := tp.MakeAPISwarm(t, ctx, 2) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) // Node 1: publishes an IPNS name - ipnsEntry := tp.testRoutingPublishKey(t, ctx, apis[0]) + p, name := tp.testRoutingPublishKey(t, ctx, apis[0]) // Node 2: retrieves the best value for the IPNS name. - data, err := apis[1].Routing().Get(ctx, "/ipns/"+ipnsEntry.Name()) - if err != nil { - t.Fatal(err) - } + data, err := apis[1].Routing().Get(ctx, name.String()) + require.NoError(t, err) rec, err := ipns.UnmarshalRecord(data) require.NoError(t, err) val, err := rec.Value() require.NoError(t, err) - require.Equal(t, ipnsEntry.Value().String(), val.String()) + require.Equal(t, p.String(), val.String()) } func (tp *TestSuite) TestRoutingPut(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() apis, err := tp.MakeAPISwarm(t, ctx, 2) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) // Create and publish IPNS entry. - ipnsEntry := tp.testRoutingPublishKey(t, ctx, apis[0]) + _, name := tp.testRoutingPublishKey(t, ctx, apis[0]) // Get valid routing value. - data, err := apis[0].Routing().Get(ctx, "/ipns/"+ipnsEntry.Name()) - if err != nil { - t.Fatal(err) - } + data, err := apis[0].Routing().Get(ctx, name.String()) + require.NoError(t, err) // Put routing value. - err = apis[1].Routing().Put(ctx, "/ipns/"+ipnsEntry.Name(), data) - if err != nil { - t.Fatal(err) - } + err = apis[1].Routing().Put(ctx, name.String(), data) + require.NoError(t, err) } func (tp *TestSuite) TestRoutingPutOffline(t *testing.T) { @@ -95,29 +82,19 @@ func (tp *TestSuite) TestRoutingPutOffline(t *testing.T) { // init a swarm & publish an IPNS entry to get a valid payload apis, err := tp.MakeAPISwarm(t, ctx, 2) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) - ipnsEntry := tp.testRoutingPublishKey(t, ctx, apis[0], options.Name.AllowOffline(true)) - data, err := apis[0].Routing().Get(ctx, "/ipns/"+ipnsEntry.Name()) - if err != nil { - t.Fatal(err) - } + _, name := tp.testRoutingPublishKey(t, ctx, apis[0], options.Name.AllowOffline(true)) + data, err := apis[0].Routing().Get(ctx, name.String()) + require.NoError(t, err) // init our offline node and try to put the payload api, err := tp.makeAPIWithIdentityAndOffline(t, ctx) - if err != nil { - t.Fatal(err) - } - - err = api.Routing().Put(ctx, "/ipns/"+ipnsEntry.Name(), data) - if err == nil { - t.Fatal("this operation should fail because we are offline") - } - - err = api.Routing().Put(ctx, "/ipns/"+ipnsEntry.Name(), data, options.Put.AllowOffline(true)) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) + + err = api.Routing().Put(ctx, name.String(), data) + require.Error(t, err, "this operation should fail because we are offline") + + err = api.Routing().Put(ctx, name.String(), data, options.Put.AllowOffline(true)) + require.NoError(t, err) }