[#1689] client/netmap: Refactor Client.config()

There are problems with that code:
- explicit casts,
- `ErrConfigNotFound` which is not a part of a public API,
- hand-rolled assertions, even though neo-go already has everything we
  need.

So, remove the error, use `stackitem/Item.Try*()` methods for
conversions. Note, that readUint64Config() returns an error if the
parameter is missing. This is likely an error, but this behaviour is
preserved in this PR: `TryInteger()` returns error when applied to
`Null`. By contract, `TryBool()` returns false for `Null`, so this
PR introduces no functional changes.

Refs 82c7a50b8a/pkg/vm/stackitem/item.go (L418)

Change-Id: I445d28a7c6b5abb9a2bb97b57c0cc42d617e16f7
Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
Evgenii Stratonikov 2025-04-05 09:07:03 +03:00
parent 3d771aa21c
commit d933609084
Signed by: fyrchik
SSH key fingerprint: SHA256:m/TTwCzjnRkXgnzEx9X92ccxy1CcVeinOgDb3NPWWmg

View file

@ -2,7 +2,6 @@ package netmap
import (
"context"
"errors"
"fmt"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client"
@ -106,29 +105,27 @@ func (c *Client) MaintenanceModeAllowed(ctx context.Context) (bool, error) {
}
func (c *Client) readUInt64Config(ctx context.Context, key string) (uint64, error) {
v, err := c.config(ctx, []byte(key), IntegerAssert)
v, err := c.config(ctx, []byte(key))
if err != nil {
return 0, fmt.Errorf("read netconfig value '%s': %w", key, err)
}
// IntegerAssert is guaranteed to return int64 if the error is nil.
return uint64(v.(int64)), nil
bi, err := v.TryInteger()
if err != nil {
return 0, err
}
return bi.Uint64(), nil
}
// reads boolean value by the given key from the FrostFS network configuration
// stored in the Sidechain. Returns false if key is not presented.
func (c *Client) readBoolConfig(ctx context.Context, key string) (bool, error) {
v, err := c.config(ctx, []byte(key), BoolAssert)
v, err := c.config(ctx, []byte(key))
if err != nil {
if errors.Is(err, ErrConfigNotFound) {
return false, nil
}
return false, fmt.Errorf("read netconfig value '%s': %w", key, err)
}
// BoolAssert is guaranteed to return bool if the error is nil.
return v.(bool), nil
return v.TryBool()
}
// SetConfigPrm groups parameters of SetConfig operation.
@ -277,15 +274,11 @@ func bytesToBool(val []byte) bool {
return false
}
// 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 FrostFS Netmap contract.
//
// Returns ErrConfigNotFound if config key is not found in the contract.
func (c *Client) config(ctx context.Context, key []byte, assert func(stackitem.Item) (any, error)) (any, error) {
func (c *Client) config(ctx context.Context, key []byte) (stackitem.Item, error) {
prm := client.TestInvokePrm{}
prm.SetMethod(configMethod)
prm.SetArgs(key)
@ -301,26 +294,7 @@ func (c *Client) config(ctx context.Context, key []byte, assert func(stackitem.I
configMethod, ln)
}
if _, ok := items[0].(stackitem.Null); ok {
return nil, ErrConfigNotFound
}
return assert(items[0])
}
// IntegerAssert converts stack item to int64.
func IntegerAssert(item stackitem.Item) (any, error) {
return client.IntFromStackItem(item)
}
// StringAssert converts stack item to string.
func StringAssert(item stackitem.Item) (any, error) {
return client.StringFromStackItem(item)
}
// BoolAssert converts stack item to bool.
func BoolAssert(item stackitem.Item) (any, error) {
return client.BoolFromStackItem(item)
return items[0], nil
}
// iterateRecords iterates over all config records and passes them to f.