From 82fda4231639d25dcdc699ea0e6db4c8f886f304 Mon Sep 17 00:00:00 2001
From: Evgenii Stratonikov <evgeniy@nspcc.ru>
Date: Tue, 5 Apr 2022 17:21:11 +0300
Subject: [PATCH] [#1294] neofs-adm: Use Global scope where needed

Provide explicit argument to `sendCommitteeTx` signifying whether a tx
should try to use group signer.

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
---
 cmd/neofs-adm/internal/modules/morph/container.go |  2 +-
 cmd/neofs-adm/internal/modules/morph/epoch.go     |  2 +-
 cmd/neofs-adm/internal/modules/morph/generate.go  |  2 +-
 .../internal/modules/morph/initialize.go          | 15 +++++++++++----
 .../internal/modules/morph/initialize_deploy.go   |  4 ++--
 .../internal/modules/morph/initialize_nns.go      |  6 +++---
 .../internal/modules/morph/initialize_register.go |  2 +-
 .../internal/modules/morph/initialize_roles.go    |  2 +-
 cmd/neofs-adm/internal/modules/morph/policy.go    |  6 +-----
 9 files changed, 22 insertions(+), 19 deletions(-)

diff --git a/cmd/neofs-adm/internal/modules/morph/container.go b/cmd/neofs-adm/internal/modules/morph/container.go
index 0f4dae49b0..01472ff127 100644
--- a/cmd/neofs-adm/internal/modules/morph/container.go
+++ b/cmd/neofs-adm/internal/modules/morph/container.go
@@ -190,7 +190,7 @@ func restoreContainers(cmd *cobra.Command, _ []string) error {
 			panic(bw.Err)
 		}
 
-		if err := wCtx.sendCommitteeTx(bw.Bytes(), -1); err != nil {
+		if err := wCtx.sendCommitteeTx(bw.Bytes(), -1, true); err != nil {
 			return err
 		}
 	}
diff --git a/cmd/neofs-adm/internal/modules/morph/epoch.go b/cmd/neofs-adm/internal/modules/morph/epoch.go
index 14ab193b54..f8f3d19415 100644
--- a/cmd/neofs-adm/internal/modules/morph/epoch.go
+++ b/cmd/neofs-adm/internal/modules/morph/epoch.go
@@ -46,7 +46,7 @@ func forceNewEpochCmd(cmd *cobra.Command, args []string) error {
 	// transaction locally.
 	bw := io.NewBufBinWriter()
 	emit.AppCall(bw.BinWriter, nmHash, "newEpoch", callflag.All, newEpoch)
-	if err := wCtx.sendCommitteeTx(bw.Bytes(), -1); err != nil {
+	if err := wCtx.sendCommitteeTx(bw.Bytes(), -1, true); err != nil {
 		return err
 	}
 
diff --git a/cmd/neofs-adm/internal/modules/morph/generate.go b/cmd/neofs-adm/internal/modules/morph/generate.go
index 2e6cfc2c6d..7c7bd758c4 100644
--- a/cmd/neofs-adm/internal/modules/morph/generate.go
+++ b/cmd/neofs-adm/internal/modules/morph/generate.go
@@ -190,7 +190,7 @@ func refillGas(cmd *cobra.Command, gasFlag string, createWallet bool) error {
 		return fmt.Errorf("BUG: invalid transfer arguments: %w", bw.Err)
 	}
 
-	if err := wCtx.sendCommitteeTx(bw.Bytes(), -1); err != nil {
+	if err := wCtx.sendCommitteeTx(bw.Bytes(), -1, false); err != nil {
 		return err
 	}
 
diff --git a/cmd/neofs-adm/internal/modules/morph/initialize.go b/cmd/neofs-adm/internal/modules/morph/initialize.go
index ae97c80f2b..0c22791b83 100644
--- a/cmd/neofs-adm/internal/modules/morph/initialize.go
+++ b/cmd/neofs-adm/internal/modules/morph/initialize.go
@@ -250,8 +250,8 @@ func (c *initializeContext) nnsContractState() (*state.Contract, error) {
 	return cs, nil
 }
 
-func (c *initializeContext) getSigner() transaction.Signer {
-	if c.groupKey != nil {
+func (c *initializeContext) getSigner(tryGroup bool) transaction.Signer {
+	if tryGroup && c.groupKey != nil {
 		return transaction.Signer{
 			Scopes:        transaction.CustomGroups,
 			AllowedGroups: keys.PublicKeys{c.groupKey},
@@ -263,6 +263,10 @@ func (c *initializeContext) getSigner() transaction.Signer {
 		Scopes:  transaction.Global, // Scope is important, as we have nested call to container contract.
 	}
 
+	if !tryGroup {
+		return signer
+	}
+
 	nnsCs, err := c.nnsContractState()
 	if err != nil {
 		return signer
@@ -325,9 +329,12 @@ loop:
 	return retErr
 }
 
-func (c *initializeContext) sendCommitteeTx(script []byte, sysFee int64) error {
+// sendCommitteeTx creates transaction from script and sends it to RPC.
+// If sysFee is -1, it is calculated automatically. If tryGroup is false,
+// global scope is used for the signer (useful when working with native contracts).
+func (c *initializeContext) sendCommitteeTx(script []byte, sysFee int64, tryGroup bool) error {
 	tx, err := c.Client.CreateTxFromScript(script, c.CommitteeAcc, sysFee, 0, []client.SignerAccount{{
-		Signer:  c.getSigner(),
+		Signer:  c.getSigner(tryGroup),
 		Account: c.CommitteeAcc,
 	}})
 	if err != nil {
diff --git a/cmd/neofs-adm/internal/modules/morph/initialize_deploy.go b/cmd/neofs-adm/internal/modules/morph/initialize_deploy.go
index d0857b9084..25b4601b25 100644
--- a/cmd/neofs-adm/internal/modules/morph/initialize_deploy.go
+++ b/cmd/neofs-adm/internal/modules/morph/initialize_deploy.go
@@ -278,7 +278,7 @@ func (c *initializeContext) updateContracts() error {
 	c.Command.Printf("NNS: Set %s -> %s\n", morphClient.NNSGroupKeyName, hex.EncodeToString(groupKey.Bytes()))
 
 	totalGasCost += sysFee
-	if err := c.sendCommitteeTx(w.Bytes(), totalGasCost); err != nil {
+	if err := c.sendCommitteeTx(w.Bytes(), totalGasCost, false); err != nil {
 		return err
 	}
 	return c.awaitTx()
@@ -360,7 +360,7 @@ func (c *initializeContext) deployContracts() error {
 			return fmt.Errorf("can't deploy %s contract: %s", ctrName, res.FaultException)
 		}
 
-		if err := c.sendCommitteeTx(res.Script, res.GasConsumed); err != nil {
+		if err := c.sendCommitteeTx(res.Script, res.GasConsumed, false); err != nil {
 			return err
 		}
 	}
diff --git a/cmd/neofs-adm/internal/modules/morph/initialize_nns.go b/cmd/neofs-adm/internal/modules/morph/initialize_nns.go
index 1a72801f5f..cb4ec8a64c 100644
--- a/cmd/neofs-adm/internal/modules/morph/initialize_nns.go
+++ b/cmd/neofs-adm/internal/modules/morph/initialize_nns.go
@@ -41,7 +41,7 @@ func (c *initializeContext) setNNS() error {
 			"neofs", c.CommitteeAcc.Contract.ScriptHash(),
 			"ops@nspcc.ru", int64(3600), int64(600), int64(604800), int64(3600))
 		emit.Opcodes(bw.BinWriter, opcode.ASSERT)
-		if err := c.sendCommitteeTx(bw.Bytes(), -1); err != nil {
+		if err := c.sendCommitteeTx(bw.Bytes(), -1, true); err != nil {
 			return fmt.Errorf("can't add domain root to NNS: %w", err)
 		}
 		if err := c.awaitTx(); err != nil {
@@ -86,7 +86,7 @@ func (c *initializeContext) updateNNSGroup(nnsHash util.Uint160, pub *keys.Publi
 	if err != nil {
 		return err
 	}
-	return c.sendCommitteeTx(bw.Bytes(), sysFee)
+	return c.sendCommitteeTx(bw.Bytes(), sysFee, true)
 }
 
 func (c *initializeContext) emitUpdateNNSGroupScript(bw *io.BufBinWriter, nnsHash util.Uint160, pub *keys.PublicKey) (int64, error) {
@@ -161,7 +161,7 @@ func (c *initializeContext) nnsRegisterDomain(nnsHash, expectedHash util.Uint160
 		return err
 	}
 	sysFee := int64(defaultRegisterSysfee + native.GASFactor)
-	return c.sendCommitteeTx(script, sysFee)
+	return c.sendCommitteeTx(script, sysFee, true)
 }
 
 func (c *initializeContext) nnsRootRegistered(nnsHash util.Uint160) (bool, error) {
diff --git a/cmd/neofs-adm/internal/modules/morph/initialize_register.go b/cmd/neofs-adm/internal/modules/morph/initialize_register.go
index 4a37a333bf..29e063f153 100644
--- a/cmd/neofs-adm/internal/modules/morph/initialize_register.go
+++ b/cmd/neofs-adm/internal/modules/morph/initialize_register.go
@@ -82,7 +82,7 @@ func (c *initializeContext) transferNEOToAlphabetContracts() error {
 		emit.Opcodes(bw.BinWriter, opcode.ASSERT)
 	}
 
-	if err := c.sendCommitteeTx(bw.Bytes(), -1); err != nil {
+	if err := c.sendCommitteeTx(bw.Bytes(), -1, false); err != nil {
 		return err
 	}
 
diff --git a/cmd/neofs-adm/internal/modules/morph/initialize_roles.go b/cmd/neofs-adm/internal/modules/morph/initialize_roles.go
index 2c9614eb85..080bb173e3 100644
--- a/cmd/neofs-adm/internal/modules/morph/initialize_roles.go
+++ b/cmd/neofs-adm/internal/modules/morph/initialize_roles.go
@@ -29,7 +29,7 @@ func (c *initializeContext) setNotaryAndAlphabetNodes() error {
 	emit.AppCall(w.BinWriter, designateHash, "designateAsRole",
 		callflag.States|callflag.AllowNotify, int64(noderoles.NeoFSAlphabet), pubs)
 
-	if err := c.sendCommitteeTx(w.Bytes(), -1); err != nil {
+	if err := c.sendCommitteeTx(w.Bytes(), -1, false); err != nil {
 		return err
 	}
 
diff --git a/cmd/neofs-adm/internal/modules/morph/policy.go b/cmd/neofs-adm/internal/modules/morph/policy.go
index 29bd670981..d4a3e05d7a 100644
--- a/cmd/neofs-adm/internal/modules/morph/policy.go
+++ b/cmd/neofs-adm/internal/modules/morph/policy.go
@@ -51,11 +51,7 @@ func setPolicyCmd(cmd *cobra.Command, args []string) error {
 		emit.AppCall(bw.BinWriter, policyHash, "set"+kv[0], callflag.All, int64(value))
 	}
 
-	// Unset group key to use `Global` signer scope. This function is unusual, because we
-	// work with native contract, not NeoFS one.
-	wCtx.groupKey = nil
-
-	if err := wCtx.sendCommitteeTx(bw.Bytes(), -1); err != nil {
+	if err := wCtx.sendCommitteeTx(bw.Bytes(), -1, false); err != nil {
 		return err
 	}