forked from TrueCloudLab/frostfs-node
[#1239] morph/client: Remove intermediate conversion in morph client
Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
parent
6936195afa
commit
882236a03b
2 changed files with 68 additions and 59 deletions
|
@ -22,21 +22,6 @@ const (
|
||||||
Offline
|
Offline
|
||||||
)
|
)
|
||||||
|
|
||||||
// PeerWithState groups information about peer
|
|
||||||
// and its state in network map.
|
|
||||||
type PeerWithState struct {
|
|
||||||
peer []byte
|
|
||||||
state State
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ps PeerWithState) State() State {
|
|
||||||
return ps.state
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ps PeerWithState) Peer() []byte {
|
|
||||||
return ps.peer
|
|
||||||
}
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
nodeInfoFixedPrmNumber = 1
|
nodeInfoFixedPrmNumber = 1
|
||||||
|
|
||||||
|
@ -72,12 +57,12 @@ func (c *Client) GetCandidates() (*netmap.Netmap, error) {
|
||||||
return nil, fmt.Errorf("could not perform test invocation (%s): %w", netMapCandidatesMethod, err)
|
return nil, fmt.Errorf("could not perform test invocation (%s): %w", netMapCandidatesMethod, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
candVals, err := peersWithStateFromStackItems(prms, netMapCandidatesMethod)
|
candVals, err := nodeInfosFromStackItems(prms, netMapCandidatesMethod)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("could not parse contract response: %w", err)
|
return nil, fmt.Errorf("could not parse contract response: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return unmarshalCandidates(candVals)
|
return netmap.NewNetmap(netmap.NodesFromInfo(candVals))
|
||||||
}
|
}
|
||||||
|
|
||||||
// NetMap performs the test invoke of get network map
|
// NetMap performs the test invoke of get network map
|
||||||
|
@ -95,31 +80,7 @@ func (c *Client) NetMap() ([][]byte, error) {
|
||||||
return peersFromStackItems(prms, netMapMethod)
|
return peersFromStackItems(prms, netMapMethod)
|
||||||
}
|
}
|
||||||
|
|
||||||
func unmarshalCandidates(rawCandidate []*PeerWithState) (*netmap.Netmap, error) {
|
func nodeInfosFromStackItems(stack []stackitem.Item, method string) ([]netmap.NodeInfo, error) {
|
||||||
candidates := make([]netmap.NodeInfo, 0, len(rawCandidate))
|
|
||||||
|
|
||||||
for _, candidate := range rawCandidate {
|
|
||||||
nodeInfo := netmap.NewNodeInfo()
|
|
||||||
if err := nodeInfo.Unmarshal(candidate.Peer()); err != nil {
|
|
||||||
return nil, fmt.Errorf("can't unmarshal peer info: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
switch candidate.State() {
|
|
||||||
case Online:
|
|
||||||
nodeInfo.SetState(netmap.NodeStateOnline)
|
|
||||||
case Offline:
|
|
||||||
nodeInfo.SetState(netmap.NodeStateOffline)
|
|
||||||
default:
|
|
||||||
nodeInfo.SetState(0)
|
|
||||||
}
|
|
||||||
|
|
||||||
candidates = append(candidates, *nodeInfo)
|
|
||||||
}
|
|
||||||
|
|
||||||
return netmap.NewNetmap(netmap.NodesFromInfo(candidates))
|
|
||||||
}
|
|
||||||
|
|
||||||
func peersWithStateFromStackItems(stack []stackitem.Item, method string) ([]*PeerWithState, error) {
|
|
||||||
if ln := len(stack); ln != 1 {
|
if ln := len(stack); ln != 1 {
|
||||||
return nil, fmt.Errorf("unexpected stack item count (%s): %d", method, ln)
|
return nil, fmt.Errorf("unexpected stack item count (%s): %d", method, ln)
|
||||||
}
|
}
|
||||||
|
@ -129,54 +90,52 @@ func peersWithStateFromStackItems(stack []stackitem.Item, method string) ([]*Pee
|
||||||
return nil, fmt.Errorf("could not get stack item array from stack item (%s): %w", method, err)
|
return nil, fmt.Errorf("could not get stack item array from stack item (%s): %w", method, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
res := make([]*PeerWithState, 0, len(netmapNodes))
|
res := make([]netmap.NodeInfo, len(netmapNodes))
|
||||||
for i := range netmapNodes {
|
for i := range netmapNodes {
|
||||||
node, err := peerWithStateFromStackItem(netmapNodes[i])
|
err := stackItemToNodeInfo(netmapNodes[i], &res[i])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("could not parse stack item (Peer #%d): %w", i, err)
|
return nil, fmt.Errorf("could not parse stack item (Peer #%d): %w", i, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
res = append(res, node)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func peerWithStateFromStackItem(prm stackitem.Item) (*PeerWithState, error) {
|
func stackItemToNodeInfo(prm stackitem.Item, res *netmap.NodeInfo) error {
|
||||||
prms, err := client.ArrayFromStackItem(prm)
|
prms, err := client.ArrayFromStackItem(prm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("could not get stack item array (PeerWithState): %w", err)
|
return fmt.Errorf("could not get stack item array (PeerWithState): %w", err)
|
||||||
} else if ln := len(prms); ln != peerWithStateFixedPrmNumber {
|
} else if ln := len(prms); ln != peerWithStateFixedPrmNumber {
|
||||||
return nil, fmt.Errorf(
|
return fmt.Errorf(
|
||||||
"unexpected stack item count (PeerWithState): expected %d, has %d",
|
"unexpected stack item count (PeerWithState): expected %d, has %d",
|
||||||
peerWithStateFixedPrmNumber,
|
peerWithStateFixedPrmNumber,
|
||||||
ln,
|
ln,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
var res PeerWithState
|
peer, err := peerInfoFromStackItem(prms[0])
|
||||||
|
if err != nil {
|
||||||
// peer
|
return fmt.Errorf("could not get bytes from 'node' field of PeerWithState: %w", err)
|
||||||
if res.peer, err = peerInfoFromStackItem(prms[0]); err != nil {
|
} else if err = res.Unmarshal(peer); err != nil {
|
||||||
return nil, fmt.Errorf("could not get bytes from 'node' field of PeerWithState: %w", err)
|
return fmt.Errorf("can't unmarshal peer info: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// state
|
// state
|
||||||
state, err := client.IntFromStackItem(prms[1])
|
state, err := client.IntFromStackItem(prms[1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("could not get int from 'state' field of PeerWithState: %w", err)
|
return fmt.Errorf("could not get int from 'state' field of PeerWithState: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
switch state {
|
switch state {
|
||||||
case 1:
|
case 1:
|
||||||
res.state = Online
|
res.SetState(netmap.NodeStateOnline)
|
||||||
case 2:
|
case 2:
|
||||||
res.state = Offline
|
res.SetState(netmap.NodeStateOffline)
|
||||||
default:
|
default:
|
||||||
res.state = Undefined
|
res.SetState(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &res, nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func peersFromStackItems(stack []stackitem.Item, method string) ([][]byte, error) {
|
func peersFromStackItems(stack []stackitem.Item, method string) ([][]byte, error) {
|
||||||
|
|
50
pkg/morph/client/netmap/netmap_test.go
Normal file
50
pkg/morph/client/netmap/netmap_test.go
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
package netmap
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math/big"
|
||||||
|
"math/rand"
|
||||||
|
"strconv"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
||||||
|
"github.com/nspcc-dev/neofs-sdk-go/netmap"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_stackItemsToNodeInfos(t *testing.T) {
|
||||||
|
expected := make([]netmap.NodeInfo, 4)
|
||||||
|
for i := range expected {
|
||||||
|
pub := make([]byte, 33)
|
||||||
|
rand.Read(pub)
|
||||||
|
|
||||||
|
expected[i].SetState(netmap.NodeState(i % 3))
|
||||||
|
expected[i].SetPublicKey(pub)
|
||||||
|
|
||||||
|
var attr netmap.NodeAttribute
|
||||||
|
attr.SetKey("key")
|
||||||
|
attr.SetValue(strconv.Itoa(i))
|
||||||
|
expected[i].SetAttributes(attr)
|
||||||
|
}
|
||||||
|
|
||||||
|
items := make([]stackitem.Item, 4)
|
||||||
|
for i := range items {
|
||||||
|
data, err := expected[i].Marshal()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
state := int64(expected[i].State())
|
||||||
|
if state != 0 { // In contract online=1, offline=2, in API it is the other way.
|
||||||
|
state = 3 - state
|
||||||
|
}
|
||||||
|
|
||||||
|
items[i] = stackitem.NewStruct([]stackitem.Item{
|
||||||
|
stackitem.NewStruct([]stackitem.Item{
|
||||||
|
stackitem.NewByteArray(data),
|
||||||
|
}),
|
||||||
|
stackitem.NewBigInteger(big.NewInt(state)),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
actual, err := nodeInfosFromStackItems([]stackitem.Item{stackitem.NewArray(items)}, "")
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, expected, actual)
|
||||||
|
}
|
Loading…
Reference in a new issue