[#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:
parent
3d771aa21c
commit
d933609084
1 changed files with 10 additions and 36 deletions
|
@ -2,7 +2,6 @@ package netmap
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client"
|
"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) {
|
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 {
|
if err != nil {
|
||||||
return 0, fmt.Errorf("read netconfig value '%s': %w", key, err)
|
return 0, fmt.Errorf("read netconfig value '%s': %w", key, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IntegerAssert is guaranteed to return int64 if the error is nil.
|
bi, err := v.TryInteger()
|
||||||
return uint64(v.(int64)), nil
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return bi.Uint64(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// reads boolean value by the given key from the FrostFS network configuration
|
// reads boolean value by the given key from the FrostFS network configuration
|
||||||
// stored in the Sidechain. Returns false if key is not presented.
|
// stored in the Sidechain. Returns false if key is not presented.
|
||||||
func (c *Client) readBoolConfig(ctx context.Context, key string) (bool, error) {
|
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 err != nil {
|
||||||
if errors.Is(err, ErrConfigNotFound) {
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return false, fmt.Errorf("read netconfig value '%s': %w", key, err)
|
return false, fmt.Errorf("read netconfig value '%s': %w", key, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// BoolAssert is guaranteed to return bool if the error is nil.
|
return v.TryBool()
|
||||||
return v.(bool), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetConfigPrm groups parameters of SetConfig operation.
|
// SetConfigPrm groups parameters of SetConfig operation.
|
||||||
|
@ -277,15 +274,11 @@ func bytesToBool(val []byte) bool {
|
||||||
return false
|
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
|
// config performs the test invoke of get config value
|
||||||
// method of FrostFS Netmap contract.
|
// method of FrostFS Netmap contract.
|
||||||
//
|
//
|
||||||
// Returns ErrConfigNotFound if config key is not found in the 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 := client.TestInvokePrm{}
|
||||||
prm.SetMethod(configMethod)
|
prm.SetMethod(configMethod)
|
||||||
prm.SetArgs(key)
|
prm.SetArgs(key)
|
||||||
|
@ -301,26 +294,7 @@ func (c *Client) config(ctx context.Context, key []byte, assert func(stackitem.I
|
||||||
configMethod, ln)
|
configMethod, ln)
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, ok := items[0].(stackitem.Null); ok {
|
return items[0], nil
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// iterateRecords iterates over all config records and passes them to f.
|
// iterateRecords iterates over all config records and passes them to f.
|
||||||
|
|
Loading…
Add table
Reference in a new issue