[#327] nns: Support Null returned value

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
Pavel Karpy 2022-08-31 18:05:09 +03:00 committed by LeL
parent d808f72c38
commit 71891029da
2 changed files with 21 additions and 11 deletions

View file

@ -2,6 +2,7 @@ package ns
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"net/url" "net/url"
@ -11,6 +12,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/rpcclient/invoker" "github.com/nspcc-dev/neo-go/pkg/rpcclient/invoker"
"github.com/nspcc-dev/neo-go/pkg/rpcclient/unwrap" "github.com/nspcc-dev/neo-go/pkg/rpcclient/unwrap"
"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/neofs-contract/nns" "github.com/nspcc-dev/neofs-contract/nns"
cid "github.com/nspcc-dev/neofs-sdk-go/container/id" cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
) )
@ -82,24 +84,32 @@ func (n *NNS) Dial(address string) error {
// //
// 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) ResolveContainerName(name string) (cid.ID, error) {
arr, err := unwrap.Array(n.invoker.Call(n.nnsContract, "resolve", item, err := unwrap.Item(n.invoker.Call(n.nnsContract, "resolve",
name+".container", int64(nns.TXT), name+".container", 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)
} }
var id cid.ID if _, ok := item.(stackitem.Null); !ok {
arr, ok := item.Value().([]stackitem.Item)
for i := range arr { if !ok {
bs, err := arr[i].TryBytes() // unexpected for types from stackitem package
if err != nil { return cid.ID{}, errors.New("invalid cast to stack item slice")
return cid.ID{}, fmt.Errorf("convert array item to byte slice: %w", err)
} }
err = id.DecodeString(string(bs)) var id cid.ID
if err == nil {
return id, nil for i := range arr {
bs, err := arr[i].TryBytes()
if err != nil {
return cid.ID{}, fmt.Errorf("convert array item to byte slice: %w", err)
}
err = id.DecodeString(string(bs))
if err == nil {
return id, nil
}
} }
} }

View file

@ -112,7 +112,7 @@ func TestNNS_ResolveContainerName(t *testing.T) {
testC.res.Stack[0] = stackitem.Null{} testC.res.Stack[0] = stackitem.Null{}
_, err := n.ResolveContainerName(testContainerName) _, err := n.ResolveContainerName(testContainerName)
require.Error(t, err) 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) {