From 7a2a76af95a7b9ff59155583f79c43c58ed5303d Mon Sep 17 00:00:00 2001 From: Denis Kirillov Date: Thu, 27 Oct 2022 17:08:45 +0300 Subject: [PATCH] [#356] ns: Use domain instead of name in NNS resolver Signed-off-by: Denis Kirillov --- ns/doc.go | 5 ++++- ns/nns.go | 9 +++++---- ns/nns_test.go | 24 +++++++++++++----------- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/ns/doc.go b/ns/doc.go index c469c5c..f797a52 100644 --- a/ns/doc.go +++ b/ns/doc.go @@ -17,7 +17,10 @@ NNS type is designed to resolve NeoFS-related names using Neo Name Service: err := nns.Dial(nnsServerAddress) // ... - containerID, err := nns.ResolveContainerName(containerName) + var containerDomain container.Domain + containerDomain.SetName(containerName) + + containerID, err := nns.ResolveContainerDomain(containerDomain) // ... */ package ns diff --git a/ns/nns.go b/ns/nns.go index 5b537a1..95109f3 100644 --- a/ns/nns.go +++ b/ns/nns.go @@ -14,6 +14,7 @@ import ( "github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" "github.com/nspcc-dev/neofs-contract/nns" + "github.com/nspcc-dev/neofs-sdk-go/container" cid "github.com/nspcc-dev/neofs-sdk-go/container/id" ) @@ -76,16 +77,16 @@ func (n *NNS) Dial(address string) error { return nil } -// ResolveContainerName looks up for NNS TXT records for the given container name +// ResolveContainerDomain looks up for NNS TXT records for the given container domain // by calling `resolve` method of NNS contract. Returns the first record which represents // valid container ID in a string format. Otherwise, returns an error. // -// ResolveContainerName MUST NOT be called before successful Dial. +// ResolveContainerDomain MUST NOT be called before successful Dial. // // See also https://docs.neo.org/docs/en-us/reference/nns.html. -func (n *NNS) ResolveContainerName(name string) (cid.ID, error) { +func (n *NNS) ResolveContainerDomain(domain container.Domain) (cid.ID, error) { item, err := unwrap.Item(n.invoker.Call(n.nnsContract, "resolve", - name+".container", int64(nns.TXT), + domain.Name()+"."+domain.Zone(), int64(nns.TXT), )) if err != nil { return cid.ID{}, fmt.Errorf("contract invocation: %w", err) diff --git a/ns/nns_test.go b/ns/nns_test.go index 931959a..32e35f4 100644 --- a/ns/nns_test.go +++ b/ns/nns_test.go @@ -12,6 +12,7 @@ import ( "github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" "github.com/nspcc-dev/neo-go/pkg/vm/vmstate" + "github.com/nspcc-dev/neofs-sdk-go/container" cidtest "github.com/nspcc-dev/neofs-sdk-go/container/id/test" "github.com/stretchr/testify/require" ) @@ -60,8 +61,9 @@ func (x brokenArrayStackItem) Convert(t stackitem.Type) (stackitem.Item, error) return x, nil } -func TestNNS_ResolveContainerName(t *testing.T) { - const testContainerName = "some_container" +func TestNNS_ResolveContainerDomain(t *testing.T) { + var testContainerDomain container.Domain + testContainerDomain.SetName("some_container") var nnsContract util.Uint160 @@ -81,21 +83,21 @@ func TestNNS_ResolveContainerName(t *testing.T) { err1 := errors.New("invoke err") testC.err = err1 - _, err2 := n.ResolveContainerName(testContainerName) + _, err2 := n.ResolveContainerDomain(testContainerDomain) require.ErrorIs(t, err2, err1) }) testC.err = nil t.Run("fault exception", func(t *testing.T) { - _, err := n.ResolveContainerName(testContainerName) + _, err := n.ResolveContainerDomain(testContainerDomain) require.Error(t, err) }) testC.res.State = vmstate.Halt.String() t.Run("empty stack", func(t *testing.T) { - _, err := n.ResolveContainerName(testContainerName) + _, err := n.ResolveContainerDomain(testContainerDomain) require.Error(t, err) }) @@ -104,21 +106,21 @@ func TestNNS_ResolveContainerName(t *testing.T) { t.Run("non-array last stack item", func(t *testing.T) { testC.res.Stack[0] = stackitem.NewBigInteger(big.NewInt(11)) - _, err := n.ResolveContainerName(testContainerName) + _, err := n.ResolveContainerDomain(testContainerDomain) require.Error(t, err) }) t.Run("null array", func(t *testing.T) { testC.res.Stack[0] = stackitem.Null{} - _, err := n.ResolveContainerName(testContainerName) + _, err := n.ResolveContainerDomain(testContainerDomain) require.ErrorIs(t, err, errNotFound) }) t.Run("array stack item with non-slice value", func(t *testing.T) { testC.res.Stack[0] = brokenArrayStackItem{} - _, err := n.ResolveContainerName(testContainerName) + _, err := n.ResolveContainerDomain(testContainerDomain) require.Error(t, err) }) @@ -128,7 +130,7 @@ func TestNNS_ResolveContainerName(t *testing.T) { t.Run("non-bytes array element", func(t *testing.T) { arr[0] = stackitem.NewArray(nil) - _, err := n.ResolveContainerName(testContainerName) + _, err := n.ResolveContainerDomain(testContainerDomain) require.Error(t, err) }) @@ -137,7 +139,7 @@ func TestNNS_ResolveContainerName(t *testing.T) { t.Run("non-container array elements", func(t *testing.T) { arr[1] = stackitem.NewByteArray([]byte("some byte array 2")) - _, err := n.ResolveContainerName(testContainerName) + _, err := n.ResolveContainerDomain(testContainerDomain) require.Error(t, err) }) @@ -146,7 +148,7 @@ func TestNNS_ResolveContainerName(t *testing.T) { arr[1] = stackitem.NewByteArray([]byte(id.EncodeToString())) - res, err := n.ResolveContainerName(testContainerName) + res, err := n.ResolveContainerDomain(testContainerDomain) require.NoError(t, err) require.Equal(t, id, res)