[#705] pkg/morph/netmap: Add innerRingList method

Add `innerRingList` method to `netmap` client
wrapper. Method parses current IR nodes list.

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
Pavel Karpy 2021-07-19 20:15:41 +03:00 committed by Alex Vanin
parent 00b487e3c3
commit b3c81116b9
2 changed files with 68 additions and 1 deletions

View file

@ -33,6 +33,7 @@ type Option func(*cfg)
type cfg struct { type cfg struct {
addPeerMethod, // add peer method name for invocation addPeerMethod, // add peer method name for invocation
newEpochMethod, // new epoch method name for invocation newEpochMethod, // new epoch method name for invocation
innerRingList, // get innerring list method name for invocation
netMapMethod, // get network map method name netMapMethod, // get network map method name
netMapCandidatesMethod, // get network candidates method name netMapCandidatesMethod, // get network candidates method name
snapshotMethod, // get network map snapshot method name snapshotMethod, // get network map snapshot method name
@ -48,6 +49,7 @@ const (
defaultAddPeerMethod = "addPeer" // default add peer method name defaultAddPeerMethod = "addPeer" // default add peer method name
defaultConfigMethod = "config" // default get config value method name defaultConfigMethod = "config" // default get config value method name
defaultEpochMethod = "epoch" // default get epoch number method name defaultEpochMethod = "epoch" // default get epoch number method name
defaultInnerRingListMethod = "innerRingList" // default get innerring list method name
defaultNetMapCandidateMethod = "netmapCandidates" // default get network candidates method name defaultNetMapCandidateMethod = "netmapCandidates" // default get network candidates method name
defaultNetMapMethod = "netmap" // default get network map method name defaultNetMapMethod = "netmap" // default get network map method name
defaultNewEpochMethod = "newEpoch" // default new epoch method name defaultNewEpochMethod = "newEpoch" // default new epoch method name
@ -64,6 +66,7 @@ func defaultConfig() *cfg {
addPeerMethod: defaultAddPeerMethod, addPeerMethod: defaultAddPeerMethod,
configMethod: defaultConfigMethod, configMethod: defaultConfigMethod,
epochMethod: defaultEpochMethod, epochMethod: defaultEpochMethod,
innerRingList: defaultInnerRingListMethod,
netMapCandidatesMethod: defaultNetMapCandidateMethod, netMapCandidatesMethod: defaultNetMapCandidateMethod,
netMapMethod: defaultNetMapMethod, netMapMethod: defaultNetMapMethod,
newEpochMethod: defaultNewEpochMethod, newEpochMethod: defaultNewEpochMethod,

View file

@ -1,6 +1,13 @@
package netmap package netmap
import "github.com/nspcc-dev/neo-go/pkg/crypto/keys" import (
"crypto/elliptic"
"fmt"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
)
// SetInnerRing updates inner ring members in netmap contract. // SetInnerRing updates inner ring members in netmap contract.
func (c *Client) SetInnerRing(keys keys.PublicKeys) error { func (c *Client) SetInnerRing(keys keys.PublicKeys) error {
@ -11,3 +18,60 @@ func (c *Client) SetInnerRing(keys keys.PublicKeys) error {
return c.client.Invoke(c.setInnerRing, args) return c.client.Invoke(c.setInnerRing, args)
} }
// InnerRingList returns public keys of inner ring members in
// netmap contract.
func (c *Client) InnerRingList() (keys.PublicKeys, error) {
prms, err := c.client.TestInvoke(
c.innerRingList,
)
if err != nil {
return nil, fmt.Errorf("could not perform test invocation (%s): %w", c.innerRingList, err)
}
return irKeysFromStackItem(prms, c.innerRingList)
}
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())
}