frostfs-node/pkg/morph/client/netmap/innerring.go
Evgenii Stratonikov e3487d5af5 [#1535] morph: Unify test invoke error messages
Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
2024-12-04 09:50:20 +00:00

97 lines
2.4 KiB
Go

package netmap
import (
"context"
"crypto/elliptic"
"fmt"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
)
// UpdateIRPrm groups parameters of UpdateInnerRing
// invocation.
type UpdateIRPrm struct {
keys keys.PublicKeys
client.InvokePrmOptional
}
// SetKeys sets new inner ring keys.
func (u *UpdateIRPrm) SetKeys(keys keys.PublicKeys) {
u.keys = keys
}
// UpdateInnerRing updates inner ring keys.
func (c *Client) UpdateInnerRing(ctx context.Context, p UpdateIRPrm) error {
args := make([][]byte, len(p.keys))
for i := range args {
args[i] = p.keys[i].Bytes()
}
prm := client.InvokePrm{}
prm.SetMethod(updateInnerRingMethod)
prm.SetArgs(args)
prm.InvokePrmOptional = p.InvokePrmOptional
_, err := c.client.Invoke(ctx, prm)
return err
}
// GetInnerRingList return current IR list.
func (c *Client) GetInnerRingList() (keys.PublicKeys, error) {
invokePrm := client.TestInvokePrm{}
invokePrm.SetMethod(innerRingListMethod)
prms, err := c.client.TestInvoke(invokePrm)
if err != nil {
return nil, fmt.Errorf("test invoke (%s): %w", innerRingListMethod, err)
}
return irKeysFromStackItem(prms, innerRingListMethod)
}
func irKeysFromStackItem(stack []stackitem.Item, method string) (keys.PublicKeys, error) {
if ln := len(stack); ln != 1 {
return nil, fmt.Errorf("unexpected stack item count (%s): %d", method, ln)
}
irs, err := client.ArrayFromStackItem(stack[0])
if err != nil {
return nil, fmt.Errorf("could not get stack item array from stack item (%s): %w", method, err)
}
irKeys := make(keys.PublicKeys, len(irs))
for i := range irs {
irKeys[i], err = irKeyFromStackItem(irs[i])
if err != nil {
return nil, err
}
}
return irKeys, nil
}
const irNodeFixedPrmNumber = 1
func irKeyFromStackItem(prm stackitem.Item) (*keys.PublicKey, error) {
prms, err := client.ArrayFromStackItem(prm)
if err != nil {
return nil, fmt.Errorf("could not get stack item array (IRNode): %w", err)
} else if ln := len(prms); ln != irNodeFixedPrmNumber {
return nil, fmt.Errorf(
"unexpected stack item count (IRNode): expected %d, has %d",
irNodeFixedPrmNumber,
ln,
)
}
byteKey, err := client.BytesFromStackItem(prms[0])
if err != nil {
return nil, fmt.Errorf("could not parse bytes from stack item (IRNode): %w", err)
}
return keys.NewPublicKeyFromBytes(byteKey, elliptic.P256())
}