From 74842e7f4337154a0d047461b8b011c1ff7e4127 Mon Sep 17 00:00:00 2001 From: Alexander Chuprov Date: Fri, 28 Jun 2024 16:35:18 +0300 Subject: [PATCH] [#1210] adm: Fix error handling when contract not found Signed-off-by: Alexander Chuprov --- .../internal/modules/morph/ape/ape_util.go | 4 ++-- .../internal/modules/morph/balance/balance.go | 2 +- .../internal/modules/morph/config/config.go | 4 ++-- .../modules/morph/container/container.go | 4 ++-- .../internal/modules/morph/contract/deploy.go | 2 +- .../modules/morph/contract/dump_hashes.go | 2 +- .../modules/morph/frostfsid/frostfsid.go | 2 +- .../internal/modules/morph/helper/contract.go | 4 ++-- .../internal/modules/morph/helper/netmap.go | 2 +- .../internal/modules/morph/helper/util.go | 17 +++++++++++++++++ .../modules/morph/initialize/initialize_nns.go | 2 +- .../internal/modules/morph/netmap/epoch.go | 2 +- .../modules/morph/netmap/netmap_candidates.go | 2 +- .../internal/modules/morph/nns/helper.go | 2 +- .../internal/modules/morph/node/remove.go | 2 +- .../internal/modules/morph/proxy/proxy.go | 2 +- 16 files changed, 36 insertions(+), 19 deletions(-) diff --git a/cmd/frostfs-adm/internal/modules/morph/ape/ape_util.go b/cmd/frostfs-adm/internal/modules/morph/ape/ape_util.go index dfa7c6392..5e17f4014 100644 --- a/cmd/frostfs-adm/internal/modules/morph/ape/ape_util.go +++ b/cmd/frostfs-adm/internal/modules/morph/ape/ape_util.go @@ -115,7 +115,7 @@ func newPolicyContractReaderInterface(cmd *cobra.Command) (*morph.ContractStorag inv := invoker.New(c, nil) var ch util.Uint160 r := management.NewReader(inv) - nnsCs, err := r.GetContractByID(1) + nnsCs, err := helper.GetContractByID(r, 1) commonCmd.ExitOnErr(cmd, "can't get NNS contract state: %w", err) ch, err = helper.NNSResolveHash(inv, nnsCs.Hash, helper.DomainOf(constants.PolicyContract)) @@ -138,7 +138,7 @@ func newPolicyContractInterface(cmd *cobra.Command) (*morph.ContractStorage, *he var ch util.Uint160 r := management.NewReader(ac.Invoker) - nnsCs, err := r.GetContractByID(1) + nnsCs, err := helper.GetContractByID(r, 1) commonCmd.ExitOnErr(cmd, "can't get NNS contract state: %w", err) ch, err = helper.NNSResolveHash(ac.Invoker, nnsCs.Hash, helper.DomainOf(constants.PolicyContract)) diff --git a/cmd/frostfs-adm/internal/modules/morph/balance/balance.go b/cmd/frostfs-adm/internal/modules/morph/balance/balance.go index 428fb98b9..5519705d4 100644 --- a/cmd/frostfs-adm/internal/modules/morph/balance/balance.go +++ b/cmd/frostfs-adm/internal/modules/morph/balance/balance.go @@ -60,7 +60,7 @@ func dumpBalances(cmd *cobra.Command, _ []string) error { if dumpStorage || dumpAlphabet || dumpProxy { r := management.NewReader(inv) - nnsCs, err = r.GetContractByID(1) + nnsCs, err = helper.GetContractByID(r, 1) if err != nil { return fmt.Errorf("can't get NNS contract info: %w", err) } diff --git a/cmd/frostfs-adm/internal/modules/morph/config/config.go b/cmd/frostfs-adm/internal/modules/morph/config/config.go index ba6e515c1..3a7f84acb 100644 --- a/cmd/frostfs-adm/internal/modules/morph/config/config.go +++ b/cmd/frostfs-adm/internal/modules/morph/config/config.go @@ -34,7 +34,7 @@ func dumpNetworkConfig(cmd *cobra.Command, _ []string) error { inv := invoker.New(c, nil) r := management.NewReader(inv) - cs, err := r.GetContractByID(1) + cs, err := helper.GetContractByID(r, 1) if err != nil { return fmt.Errorf("can't get NNS contract info: %w", err) } @@ -93,7 +93,7 @@ func SetConfigCmd(cmd *cobra.Command, args []string) error { } r := management.NewReader(wCtx.ReadOnlyInvoker) - cs, err := r.GetContractByID(1) + cs, err := helper.GetContractByID(r, 1) if err != nil { return fmt.Errorf("can't get NNS contract info: %w", err) } diff --git a/cmd/frostfs-adm/internal/modules/morph/container/container.go b/cmd/frostfs-adm/internal/modules/morph/container/container.go index eda388d37..a66438975 100644 --- a/cmd/frostfs-adm/internal/modules/morph/container/container.go +++ b/cmd/frostfs-adm/internal/modules/morph/container/container.go @@ -34,7 +34,7 @@ func getContainerContractHash(cmd *cobra.Command, inv *invoker.Invoker) (util.Ui } if err != nil { r := management.NewReader(inv) - nnsCs, err := r.GetContractByID(1) + nnsCs, err := helper.GetContractByID(r, 1) if err != nil { return util.Uint160{}, fmt.Errorf("can't get NNS contract state: %w", err) } @@ -304,7 +304,7 @@ func parseContainers(filename string) ([]Container, error) { func fetchContainerContractHash(wCtx *helper.InitializeContext) (util.Uint160, error) { r := management.NewReader(wCtx.ReadOnlyInvoker) - nnsCs, err := r.GetContractByID(1) + nnsCs, err := helper.GetContractByID(r, 1) if err != nil { return util.Uint160{}, fmt.Errorf("can't get NNS contract state: %w", err) } diff --git a/cmd/frostfs-adm/internal/modules/morph/contract/deploy.go b/cmd/frostfs-adm/internal/modules/morph/contract/deploy.go index 7a8f34a5d..5adb480da 100644 --- a/cmd/frostfs-adm/internal/modules/morph/contract/deploy.go +++ b/cmd/frostfs-adm/internal/modules/morph/contract/deploy.go @@ -79,7 +79,7 @@ func deployContractCmd(cmd *cobra.Command, args []string) error { } r := management.NewReader(c.ReadOnlyInvoker) - nnsCs, err := r.GetContractByID(1) + nnsCs, err := helper.GetContractByID(r, 1) if err != nil { return fmt.Errorf("can't fetch NNS contract state: %w", err) } diff --git a/cmd/frostfs-adm/internal/modules/morph/contract/dump_hashes.go b/cmd/frostfs-adm/internal/modules/morph/contract/dump_hashes.go index 11cc3bd04..5a0d29550 100644 --- a/cmd/frostfs-adm/internal/modules/morph/contract/dump_hashes.go +++ b/cmd/frostfs-adm/internal/modules/morph/contract/dump_hashes.go @@ -42,7 +42,7 @@ func dumpContractHashes(cmd *cobra.Command, _ []string) error { } r := management.NewReader(invoker.New(c, nil)) - cs, err := r.GetContractByID(1) + cs, err := helper.GetContractByID(r, 1) if err != nil { return err } diff --git a/cmd/frostfs-adm/internal/modules/morph/frostfsid/frostfsid.go b/cmd/frostfs-adm/internal/modules/morph/frostfsid/frostfsid.go index 6b274ddfb..442920912 100644 --- a/cmd/frostfs-adm/internal/modules/morph/frostfsid/frostfsid.go +++ b/cmd/frostfs-adm/internal/modules/morph/frostfsid/frostfsid.go @@ -430,7 +430,7 @@ func newFrostfsIDClient(cmd *cobra.Command) (*frostfsidClient, error) { } r := management.NewReader(wCtx.ReadOnlyInvoker) - cs, err := r.GetContractByID(1) + cs, err := helper.GetContractByID(r, 1) if err != nil { return nil, fmt.Errorf("can't get NNS contract info: %w", err) } diff --git a/cmd/frostfs-adm/internal/modules/morph/helper/contract.go b/cmd/frostfs-adm/internal/modules/morph/helper/contract.go index 7326ec1f5..2011301d1 100644 --- a/cmd/frostfs-adm/internal/modules/morph/helper/contract.go +++ b/cmd/frostfs-adm/internal/modules/morph/helper/contract.go @@ -15,7 +15,7 @@ import ( func getFrostfsIDAdminFromContract(roInvoker *invoker.Invoker) (util.Uint160, bool, error) { r := management.NewReader(roInvoker) - cs, err := r.GetContractByID(1) + cs, err := GetContractByID(r, 1) if err != nil { return util.Uint160{}, false, fmt.Errorf("get nns contract: %w", err) } @@ -62,7 +62,7 @@ func GetContractDeployData(c *InitializeContext, ctrName string, keysParam []any // In case if NNS is updated multiple times, we can't calculate // it's actual hash based on local data, thus query chain. r := management.NewReader(c.ReadOnlyInvoker) - nnsCs, err := r.GetContractByID(1) + nnsCs, err := GetContractByID(r, 1) if err != nil { return nil, fmt.Errorf("get nns contract: %w", err) } diff --git a/cmd/frostfs-adm/internal/modules/morph/helper/netmap.go b/cmd/frostfs-adm/internal/modules/morph/helper/netmap.go index 9ea27d3a3..7a778f8c3 100644 --- a/cmd/frostfs-adm/internal/modules/morph/helper/netmap.go +++ b/cmd/frostfs-adm/internal/modules/morph/helper/netmap.go @@ -89,7 +89,7 @@ func EmitNewEpochCall(bw *io.BufBinWriter, wCtx *InitializeContext, nmHash util. func GetNetConfigFromNetmapContract(roInvoker *invoker.Invoker) ([]stackitem.Item, error) { r := management.NewReader(roInvoker) - cs, err := r.GetContractByID(1) + cs, err := GetContractByID(r, 1) if err != nil { return nil, fmt.Errorf("get nns contract: %w", err) } diff --git a/cmd/frostfs-adm/internal/modules/morph/helper/util.go b/cmd/frostfs-adm/internal/modules/morph/helper/util.go index ea8266b38..2d9281c24 100644 --- a/cmd/frostfs-adm/internal/modules/morph/helper/util.go +++ b/cmd/frostfs-adm/internal/modules/morph/helper/util.go @@ -14,10 +14,12 @@ import ( "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/modules/config" "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/modules/morph/constants" "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/innerring" + "github.com/nspcc-dev/neo-go/pkg/core/state" "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/encoding/fixedn" "github.com/nspcc-dev/neo-go/pkg/rpcclient/actor" + "github.com/nspcc-dev/neo-go/pkg/rpcclient/management" "github.com/nspcc-dev/neo-go/pkg/wallet" "github.com/spf13/viper" ) @@ -181,3 +183,18 @@ func ParseGASAmount(s string) (fixedn.Fixed8, error) { } return gasAmount, nil } + +// GetContractByID retrieves a contract by its ID using the standard GetContractByID method. +// However, if the returned state.Contract is nil, it returns an error indicating that the contract was not found. +// See https://git.frostfs.info/TrueCloudLab/frostfs-node/issues/1210 +func GetContractByID(r *management.ContractReader, id int32) (*state.Contract, error) { + cs, err := r.GetContractByID(id) + if err != nil { + return nil, err + } + + if cs == nil { + return nil, errors.New("contract not found") + } + return cs, nil +} diff --git a/cmd/frostfs-adm/internal/modules/morph/initialize/initialize_nns.go b/cmd/frostfs-adm/internal/modules/morph/initialize/initialize_nns.go index 14f12cf8e..e127ca545 100644 --- a/cmd/frostfs-adm/internal/modules/morph/initialize/initialize_nns.go +++ b/cmd/frostfs-adm/internal/modules/morph/initialize/initialize_nns.go @@ -21,7 +21,7 @@ import ( func setNNS(c *helper.InitializeContext) error { r := management.NewReader(c.ReadOnlyInvoker) - nnsCs, err := r.GetContractByID(1) + nnsCs, err := helper.GetContractByID(r, 1) if err != nil { return err } diff --git a/cmd/frostfs-adm/internal/modules/morph/netmap/epoch.go b/cmd/frostfs-adm/internal/modules/morph/netmap/epoch.go index e60dc5673..df9a03fd1 100644 --- a/cmd/frostfs-adm/internal/modules/morph/netmap/epoch.go +++ b/cmd/frostfs-adm/internal/modules/morph/netmap/epoch.go @@ -19,7 +19,7 @@ func ForceNewEpochCmd(cmd *cobra.Command, _ []string) error { } r := management.NewReader(wCtx.ReadOnlyInvoker) - cs, err := r.GetContractByID(1) + cs, err := helper.GetContractByID(r, 1) if err != nil { return fmt.Errorf("can't get NNS contract info: %w", err) } diff --git a/cmd/frostfs-adm/internal/modules/morph/netmap/netmap_candidates.go b/cmd/frostfs-adm/internal/modules/morph/netmap/netmap_candidates.go index e9404e910..d8471bb9a 100644 --- a/cmd/frostfs-adm/internal/modules/morph/netmap/netmap_candidates.go +++ b/cmd/frostfs-adm/internal/modules/morph/netmap/netmap_candidates.go @@ -19,7 +19,7 @@ func listNetmapCandidatesNodes(cmd *cobra.Command, _ []string) { inv := invoker.New(c, nil) r := management.NewReader(inv) - cs, err := r.GetContractByID(1) + cs, err := helper.GetContractByID(r, 1) commonCmd.ExitOnErr(cmd, "can't get NNS contract info: %w", err) nmHash, err := helper.NNSResolveHash(inv, cs.Hash, helper.DomainOf(constants.NetmapContract)) diff --git a/cmd/frostfs-adm/internal/modules/morph/nns/helper.go b/cmd/frostfs-adm/internal/modules/morph/nns/helper.go index 36c3dd2fd..0eaa5ac58 100644 --- a/cmd/frostfs-adm/internal/modules/morph/nns/helper.go +++ b/cmd/frostfs-adm/internal/modules/morph/nns/helper.go @@ -19,7 +19,7 @@ func getRPCClient(cmd *cobra.Command) (*client.Contract, *helper.LocalActor, uti commonCmd.ExitOnErr(cmd, "can't create actor: %w", err) r := management.NewReader(ac.Invoker) - nnsCs, err := r.GetContractByID(1) + nnsCs, err := helper.GetContractByID(r, 1) commonCmd.ExitOnErr(cmd, "can't get NNS contract state: %w", err) return client.New(ac, nnsCs.Hash), ac, nnsCs.Hash } diff --git a/cmd/frostfs-adm/internal/modules/morph/node/remove.go b/cmd/frostfs-adm/internal/modules/morph/node/remove.go index 0d14bc646..0a19102ba 100644 --- a/cmd/frostfs-adm/internal/modules/morph/node/remove.go +++ b/cmd/frostfs-adm/internal/modules/morph/node/remove.go @@ -37,7 +37,7 @@ func RemoveNodesCmd(cmd *cobra.Command, args []string) error { defer wCtx.Close() r := management.NewReader(wCtx.ReadOnlyInvoker) - cs, err := r.GetContractByID(1) + cs, err := helper.GetContractByID(r, 1) if err != nil { return fmt.Errorf("can't get NNS contract info: %w", err) } diff --git a/cmd/frostfs-adm/internal/modules/morph/proxy/proxy.go b/cmd/frostfs-adm/internal/modules/morph/proxy/proxy.go index ba48465f7..cb575b657 100644 --- a/cmd/frostfs-adm/internal/modules/morph/proxy/proxy.go +++ b/cmd/frostfs-adm/internal/modules/morph/proxy/proxy.go @@ -43,7 +43,7 @@ func processAccount(cmd *cobra.Command, addr util.Uint160, method string) error } r := management.NewReader(wCtx.ReadOnlyInvoker) - cs, err := r.GetContractByID(1) + cs, err := helper.GetContractByID(r, 1) if err != nil { return fmt.Errorf("can't get NNS contract info: %w", err) }