From ac0821a1a544bfeeecff1f4a0d32047b809be617 Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Thu, 21 Dec 2023 12:26:47 +0300 Subject: [PATCH] [#883] adm: Support proxy accounts Signed-off-by: Alex Vanin --- .../internal/modules/morph/proxy.go | 68 +++++++++++++++++++ .../internal/modules/morph/root.go | 34 ++++++++++ 2 files changed, 102 insertions(+) create mode 100644 cmd/frostfs-adm/internal/modules/morph/proxy.go diff --git a/cmd/frostfs-adm/internal/modules/morph/proxy.go b/cmd/frostfs-adm/internal/modules/morph/proxy.go new file mode 100644 index 000000000..effcf0e27 --- /dev/null +++ b/cmd/frostfs-adm/internal/modules/morph/proxy.go @@ -0,0 +1,68 @@ +package morph + +import ( + "fmt" + + commonCmd "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/internal/common" + "github.com/nspcc-dev/neo-go/pkg/encoding/address" + "github.com/nspcc-dev/neo-go/pkg/io" + "github.com/nspcc-dev/neo-go/pkg/rpcclient/management" + "github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag" + "github.com/nspcc-dev/neo-go/pkg/util" + "github.com/nspcc-dev/neo-go/pkg/vm/emit" + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +const ( + accountAddressFlag = "account" +) + +func addProxyAccount(cmd *cobra.Command, _ []string) { + acc, _ := cmd.Flags().GetString(accountAddressFlag) + addr, err := address.StringToUint160(acc) + commonCmd.ExitOnErr(cmd, "invalid account: %w", err) + err = processAccount(cmd, addr, "addAccount") + commonCmd.ExitOnErr(cmd, "processing error: %w", err) +} + +func removeProxyAccount(cmd *cobra.Command, _ []string) { + acc, _ := cmd.Flags().GetString(accountAddressFlag) + addr, err := address.StringToUint160(acc) + commonCmd.ExitOnErr(cmd, "invalid account: %w", err) + err = processAccount(cmd, addr, "removeAccount") + commonCmd.ExitOnErr(cmd, "processing error: %w", err) +} + +func processAccount(cmd *cobra.Command, addr util.Uint160, method string) error { + wCtx, err := newInitializeContext(cmd, viper.GetViper()) + if err != nil { + return fmt.Errorf("can't to initialize context: %w", err) + } + + r := management.NewReader(wCtx.ReadOnlyInvoker) + cs, err := r.GetContractByID(1) + if err != nil { + return fmt.Errorf("can't get NNS contract info: %w", err) + } + + proxyHash, err := nnsResolveHash(wCtx.ReadOnlyInvoker, cs.Hash, proxyContract+".frostfs") + if err != nil { + return fmt.Errorf("can't get proxy contract hash: %w", err) + } + + bw := io.NewBufBinWriter() + emit.AppCall(bw.BinWriter, proxyHash, method, callflag.All, addr) + + if err := wCtx.sendConsensusTx(bw.Bytes()); err != nil { + return err + } + + if err = wCtx.awaitTx(); err != nil { + return err + } + + cmd.Println("Proxy contract has been updated") + + return nil +} diff --git a/cmd/frostfs-adm/internal/modules/morph/root.go b/cmd/frostfs-adm/internal/modules/morph/root.go index 475bf4b06..75409421a 100644 --- a/cmd/frostfs-adm/internal/modules/morph/root.go +++ b/cmd/frostfs-adm/internal/modules/morph/root.go @@ -241,6 +241,26 @@ var ( }, Run: listNetmapCandidatesNodes, } + + proxyAddAccountCmd = &cobra.Command{ + Use: "proxy-add-account", + Short: "Adds account to proxy contract", + PreRun: func(cmd *cobra.Command, _ []string) { + _ = viper.BindPFlag(alphabetWalletsFlag, cmd.Flags().Lookup(alphabetWalletsFlag)) + _ = viper.BindPFlag(endpointFlag, cmd.Flags().Lookup(endpointFlag)) + }, + Run: addProxyAccount, + } + + proxyRemoveAccountCmd = &cobra.Command{ + Use: "proxy-remove-account", + Short: "Remove from proxy contract", + PreRun: func(cmd *cobra.Command, _ []string) { + _ = viper.BindPFlag(alphabetWalletsFlag, cmd.Flags().Lookup(alphabetWalletsFlag)) + _ = viper.BindPFlag(endpointFlag, cmd.Flags().Lookup(endpointFlag)) + }, + Run: removeProxyAccount, + } ) func init() { @@ -267,6 +287,20 @@ func init() { initAddRuleChainCmd() initRemoveRuleChainCmd() initListRuleChainsCmd() + initProxyAddAccount() + initProxyRemoveAccount() +} + +func initProxyAddAccount() { + RootCmd.AddCommand(proxyAddAccountCmd) + proxyAddAccountCmd.Flags().StringP(endpointFlag, endpointFlagShort, "", endpointFlagDesc) + proxyAddAccountCmd.Flags().String(accountAddressFlag, "", "Wallet address string") +} + +func initProxyRemoveAccount() { + RootCmd.AddCommand(proxyRemoveAccountCmd) + proxyRemoveAccountCmd.Flags().StringP(endpointFlag, endpointFlagShort, "", endpointFlagDesc) + proxyRemoveAccountCmd.Flags().String(accountAddressFlag, "", "Wallet address string") } func initNetmapCandidatesCmd() {