From 8fa2b364a1b7b8bbaad6240a2e1c0db646d1e95d Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Mon, 29 Nov 2021 16:45:25 +0300 Subject: [PATCH] [#749] neofs-adm: add group scope to `force-new-epoch` command Signed-off-by: Evgenii Stratonikov --- cmd/neofs-adm/internal/modules/morph/epoch.go | 15 +++++++-- .../internal/modules/morph/initialize_nns.go | 32 +++++++++++-------- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/cmd/neofs-adm/internal/modules/morph/epoch.go b/cmd/neofs-adm/internal/modules/morph/epoch.go index 9c7121e59..af1faecde 100644 --- a/cmd/neofs-adm/internal/modules/morph/epoch.go +++ b/cmd/neofs-adm/internal/modules/morph/epoch.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/nspcc-dev/neo-go/pkg/core/transaction" + "github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/smartcontract" "github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag" @@ -30,10 +31,20 @@ func forceNewEpochCmd(cmd *cobra.Command, args []string) error { return fmt.Errorf("can't get netmap contract hash: %w", err) } - res, err := wCtx.Client.InvokeFunction(nmHash, "epoch", []smartcontract.Parameter{}, []transaction.Signer{{ + signer := transaction.Signer{ Account: wCtx.CommitteeAcc.Contract.ScriptHash(), Scopes: transaction.Global, // Scope is important, as we have nested call to container contract. - }}) + } + + groupKey, err := nnsResolveKey(wCtx.Client, cs.Hash, groupKeyDomain) + if err != nil { + cmd.Println("Can't resolve neofs contract group key, using Global scope") + } else { + signer.Scopes = transaction.CustomGroups + signer.AllowedGroups = keys.PublicKeys{groupKey} + } + + res, err := wCtx.Client.InvokeFunction(nmHash, "epoch", []smartcontract.Parameter{}, nil) if err != nil || res.State != vm.HaltState.String() || len(res.Stack) == 0 { return errors.New("can't fetch current epoch from the netmap contract") } diff --git a/cmd/neofs-adm/internal/modules/morph/initialize_nns.go b/cmd/neofs-adm/internal/modules/morph/initialize_nns.go index a845882ff..434b4d3ad 100644 --- a/cmd/neofs-adm/internal/modules/morph/initialize_nns.go +++ b/cmd/neofs-adm/internal/modules/morph/initialize_nns.go @@ -93,20 +93,7 @@ func (c *initializeContext) emitUpdateNNSGroupScript(bw *io.BufBinWriter, nnsHas } if !isAvail { - item, err := nnsResolve(c.Client, nnsHash, "group.neofs") - if err != nil { - return 0, err - } - arr, ok := item.Value().([]stackitem.Item) - if !ok || len(arr) == 0 { - return 0, errors.New("NNS record is missing") - } - bs, err := arr[0].TryBytes() - if err != nil { - return 0, errors.New("malformed response") - } - - currentPub, err := keys.NewPublicKeyFromString(string(bs)) + currentPub, err := nnsResolveKey(c.Client, nnsHash, groupKeyDomain) if err != nil { return 0, err } @@ -220,6 +207,23 @@ func nnsResolve(c *client.Client, nnsHash util.Uint160, domain string) (stackite return result.Stack[len(result.Stack)-1], nil } +func nnsResolveKey(c *client.Client, nnsHash util.Uint160, domain string) (*keys.PublicKey, error) { + item, err := nnsResolve(c, nnsHash, domain) + if err != nil { + return nil, err + } + arr, ok := item.Value().([]stackitem.Item) + if !ok || len(arr) == 0 { + return nil, errors.New("NNS record is missing") + } + bs, err := arr[0].TryBytes() + if err != nil { + return nil, errors.New("malformed response") + } + + return keys.NewPublicKeyFromString(string(bs)) +} + // 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.