From 31b3c71457f0a98e5db67786c39a82dbccd1f9de Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Thu, 7 Oct 2021 16:14:52 +0300 Subject: [PATCH] [#888] neofs-adm: allow to work with multiple NNS versions Signed-off-by: Evgenii Stratonikov --- cmd/neofs-adm/internal/modules/morph/dump.go | 10 ++++------ .../internal/modules/morph/initialize_nns.go | 17 +++++++++++++---- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/cmd/neofs-adm/internal/modules/morph/dump.go b/cmd/neofs-adm/internal/modules/morph/dump.go index b16b820c..8f88beed 100644 --- a/cmd/neofs-adm/internal/modules/morph/dump.go +++ b/cmd/neofs-adm/internal/modules/morph/dump.go @@ -78,9 +78,8 @@ func dumpContractHashes(cmd *cobra.Command, _ []string) error { for i := 0; i < irSize; i++ { ctrHash := "hash is invalid" - bs, err := alphaRes.Stack[i].TryBytes() - if err == nil { - ctrHash = string(bs) // hashes are stored as hex-encoded LE string + if h, err := parseNNSResolveResult(alphaRes.Stack[i]); err == nil { + ctrHash = h.StringLE() } _, _ = tw.Write([]byte(fmt.Sprintf("alphabet %d:\t%s\n", i, ctrHash))) @@ -89,9 +88,8 @@ func dumpContractHashes(cmd *cobra.Command, _ []string) error { for i := range contractList { ctrHash := "hash is invalid" - bs, err := res.Stack[i].TryBytes() - if err == nil { - ctrHash = string(bs) // hashes are stored as hex-encoded LE string + if h, err := parseNNSResolveResult(res.Stack[i]); err == nil { + ctrHash = h.StringLE() } _, _ = tw.Write([]byte(fmt.Sprintf("%s:\t%s\n", contractList[i], ctrHash))) diff --git a/cmd/neofs-adm/internal/modules/morph/initialize_nns.go b/cmd/neofs-adm/internal/modules/morph/initialize_nns.go index e1de223e..07aca041 100644 --- a/cmd/neofs-adm/internal/modules/morph/initialize_nns.go +++ b/cmd/neofs-adm/internal/modules/morph/initialize_nns.go @@ -139,11 +139,20 @@ func nnsResolveHash(c *client.Client, nnsHash util.Uint160, domain string) (util if len(result.Stack) == 0 { return util.Uint160{}, errors.New("result stack is empty") } - arr, ok := result.Stack[len(result.Stack)-1].Value().([]stackitem.Item) - if !ok || len(arr) == 0 { - return util.Uint160{}, errors.New("malformed response") + return parseNNSResolveResult(result.Stack[len(result.Stack)-1]) +} + +// parseNNSResolveResult parses the result of resolving NNS record. +// It works with multiple formats (corresponding to multiple NNS versions). +// If array of hashes is provided, it returns only the first one. +func parseNNSResolveResult(res stackitem.Item) (util.Uint160, error) { + if arr, ok := res.Value().([]stackitem.Item); ok { + if len(arr) == 0 { + return util.Uint160{}, errors.New("NNS record is missing") + } + res = arr[0] } - bs, err := arr[0].TryBytes() + bs, err := res.TryBytes() if err != nil { return util.Uint160{}, errors.New("malformed response") }