From 4a316ceae91318e8fc7a1e250e0b9d909306e784 Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Fri, 6 May 2022 11:06:25 +0300 Subject: [PATCH] [#1365] morph: Do not return errors if config key is missing Return default values instead of casting errors in `HomomorphicHashDisabled` method. Signed-off-by: Pavel Karpy --- pkg/morph/client/netmap/config.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/pkg/morph/client/netmap/config.go b/pkg/morph/client/netmap/config.go index 47a539b7..8ab4bf12 100644 --- a/pkg/morph/client/netmap/config.go +++ b/pkg/morph/client/netmap/config.go @@ -1,6 +1,7 @@ package netmap import ( + "errors" "fmt" "strconv" @@ -112,9 +113,17 @@ func (c *Client) EigenTrustAlpha() (float64, error) { // HomomorphicHashDisabled returns global configuration value of homomorphic hashing // settings. +// +// Returns (false, nil) if config key is not found in the contract. func (c *Client) HomomorphicHashDisabled() (bool, error) { + const defaultValue = false + hashingDisabled, err := c.readBoolConfig(homomorphicHashingDisabledKey) if err != nil { + if errors.Is(err, ErrConfigNotFound) { + return defaultValue, nil + } + return false, fmt.Errorf("(%T) could not get homomorphic hash state: %w", c, err) } @@ -319,8 +328,14 @@ func bytesToUint64(val []byte) uint64 { return bigint.FromBytes(val).Uint64() } +// ErrConfigNotFound is returned when the requested key was not found +// in the network config (returned value is `Null`). +var ErrConfigNotFound = errors.New("config value not found") + // config performs the test invoke of get config value // method of NeoFS Netmap contract. +// +// Returns ErrConfigNotFound if config key is not found in the contract. func (c *Client) config(key []byte, assert func(stackitem.Item) (interface{}, error)) (interface{}, error) { prm := client.TestInvokePrm{} prm.SetMethod(configMethod) @@ -337,6 +352,10 @@ func (c *Client) config(key []byte, assert func(stackitem.Item) (interface{}, er configMethod, ln) } + if _, ok := items[0].(stackitem.Null); ok { + return nil, ErrConfigNotFound + } + return assert(items[0]) }