[#356] ns: Use domain instead of name in NNS resolver

Signed-off-by: Denis Kirillov <denis@nspcc.ru>
This commit is contained in:
Denis Kirillov 2022-10-27 17:08:45 +03:00 committed by LeL
parent da4ddcf337
commit 7a2a76af95
3 changed files with 22 additions and 16 deletions

View file

@ -17,7 +17,10 @@ NNS type is designed to resolve NeoFS-related names using Neo Name Service:
err := nns.Dial(nnsServerAddress) err := nns.Dial(nnsServerAddress)
// ... // ...
containerID, err := nns.ResolveContainerName(containerName) var containerDomain container.Domain
containerDomain.SetName(containerName)
containerID, err := nns.ResolveContainerDomain(containerDomain)
// ... // ...
*/ */
package ns package ns

View file

@ -14,6 +14,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/util" "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/stackitem"
"github.com/nspcc-dev/neofs-contract/nns" "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" cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
) )
@ -76,16 +77,16 @@ func (n *NNS) Dial(address string) error {
return nil 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 // by calling `resolve` method of NNS contract. Returns the first record which represents
// valid container ID in a string format. Otherwise, returns an error. // 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. // 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", 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 { if err != nil {
return cid.ID{}, fmt.Errorf("contract invocation: %w", err) return cid.ID{}, fmt.Errorf("contract invocation: %w", err)

View file

@ -12,6 +12,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/util" "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/stackitem"
"github.com/nspcc-dev/neo-go/pkg/vm/vmstate" "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" cidtest "github.com/nspcc-dev/neofs-sdk-go/container/id/test"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -60,8 +61,9 @@ func (x brokenArrayStackItem) Convert(t stackitem.Type) (stackitem.Item, error)
return x, nil return x, nil
} }
func TestNNS_ResolveContainerName(t *testing.T) { func TestNNS_ResolveContainerDomain(t *testing.T) {
const testContainerName = "some_container" var testContainerDomain container.Domain
testContainerDomain.SetName("some_container")
var nnsContract util.Uint160 var nnsContract util.Uint160
@ -81,21 +83,21 @@ func TestNNS_ResolveContainerName(t *testing.T) {
err1 := errors.New("invoke err") err1 := errors.New("invoke err")
testC.err = err1 testC.err = err1
_, err2 := n.ResolveContainerName(testContainerName) _, err2 := n.ResolveContainerDomain(testContainerDomain)
require.ErrorIs(t, err2, err1) require.ErrorIs(t, err2, err1)
}) })
testC.err = nil testC.err = nil
t.Run("fault exception", func(t *testing.T) { t.Run("fault exception", func(t *testing.T) {
_, err := n.ResolveContainerName(testContainerName) _, err := n.ResolveContainerDomain(testContainerDomain)
require.Error(t, err) require.Error(t, err)
}) })
testC.res.State = vmstate.Halt.String() testC.res.State = vmstate.Halt.String()
t.Run("empty stack", func(t *testing.T) { t.Run("empty stack", func(t *testing.T) {
_, err := n.ResolveContainerName(testContainerName) _, err := n.ResolveContainerDomain(testContainerDomain)
require.Error(t, err) 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) { t.Run("non-array last stack item", func(t *testing.T) {
testC.res.Stack[0] = stackitem.NewBigInteger(big.NewInt(11)) testC.res.Stack[0] = stackitem.NewBigInteger(big.NewInt(11))
_, err := n.ResolveContainerName(testContainerName) _, err := n.ResolveContainerDomain(testContainerDomain)
require.Error(t, err) require.Error(t, err)
}) })
t.Run("null array", func(t *testing.T) { t.Run("null array", func(t *testing.T) {
testC.res.Stack[0] = stackitem.Null{} testC.res.Stack[0] = stackitem.Null{}
_, err := n.ResolveContainerName(testContainerName) _, err := n.ResolveContainerDomain(testContainerDomain)
require.ErrorIs(t, err, errNotFound) require.ErrorIs(t, err, errNotFound)
}) })
t.Run("array stack item with non-slice value", func(t *testing.T) { t.Run("array stack item with non-slice value", func(t *testing.T) {
testC.res.Stack[0] = brokenArrayStackItem{} testC.res.Stack[0] = brokenArrayStackItem{}
_, err := n.ResolveContainerName(testContainerName) _, err := n.ResolveContainerDomain(testContainerDomain)
require.Error(t, err) require.Error(t, err)
}) })
@ -128,7 +130,7 @@ func TestNNS_ResolveContainerName(t *testing.T) {
t.Run("non-bytes array element", func(t *testing.T) { t.Run("non-bytes array element", func(t *testing.T) {
arr[0] = stackitem.NewArray(nil) arr[0] = stackitem.NewArray(nil)
_, err := n.ResolveContainerName(testContainerName) _, err := n.ResolveContainerDomain(testContainerDomain)
require.Error(t, err) require.Error(t, err)
}) })
@ -137,7 +139,7 @@ func TestNNS_ResolveContainerName(t *testing.T) {
t.Run("non-container array elements", func(t *testing.T) { t.Run("non-container array elements", func(t *testing.T) {
arr[1] = stackitem.NewByteArray([]byte("some byte array 2")) arr[1] = stackitem.NewByteArray([]byte("some byte array 2"))
_, err := n.ResolveContainerName(testContainerName) _, err := n.ResolveContainerDomain(testContainerDomain)
require.Error(t, err) require.Error(t, err)
}) })
@ -146,7 +148,7 @@ func TestNNS_ResolveContainerName(t *testing.T) {
arr[1] = stackitem.NewByteArray([]byte(id.EncodeToString())) arr[1] = stackitem.NewByteArray([]byte(id.EncodeToString()))
res, err := n.ResolveContainerName(testContainerName) res, err := n.ResolveContainerDomain(testContainerDomain)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, id, res) require.Equal(t, id, res)