Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/add-netmapCandidates-netmap-method #647

Merged
merged 3 commits into from
Jun 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 21 additions & 18 deletions pkg/morph/client/netmap/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type cfg struct {
addPeerMethod, // add peer method name for invocation
newEpochMethod, // new epoch method name for invocation
netMapMethod, // get network map method name
netMapCandidatesMethod, // get network candidates method name
snapshotMethod, // get network map snapshot method name
epochSnapshotMethod, // get network map snapshot by epoch method name
updateStateMethod, // update state method name for invocation
Expand All @@ -44,30 +45,32 @@ type cfg struct {
}

const (
defaultAddPeerMethod = "addPeer" // default add peer method name
defaultNewEpochMethod = "newEpoch" // default new epoch method name
defaultNetMapMethod = "netmap" // default get network map method name
defaultSnapshotMethod = "snapshot" // default get network map snapshot method name
defaultUpdateStateMethod = "updateState" // default update state method name
defaultEpochMethod = "epoch" // default get epoch number method name
defaultSetInnerRingMethod = "updateInnerRing" // default set innerring method name
defaultSetConfigMethod = "setConfig" // default get config value method name
defaultConfigMethod = "config" // default get config value method name
defaultAddPeerMethod = "addPeer" // default add peer method name
defaultConfigMethod = "config" // default get config value method name
defaultEpochMethod = "epoch" // default get epoch number method name
defaultNetMapCandidateMethod = "netmapCandidates" // default get network candidates method name
defaultNetMapMethod = "netmap" // default get network map method name
defaultNewEpochMethod = "newEpoch" // default new epoch method name
defaultSetConfigMethod = "setConfig" // default get config value method name
defaultSetInnerRingMethod = "updateInnerRing" // default set innerring method name
defaultSnapshotMethod = "snapshot" // default get network map snapshot method name
defaultUpdateStateMethod = "updateState" // default update state method name

defaultEpochSnapshotMethod = "snapshotByEpoch" // default get network map snapshot by epoch method name
)

func defaultConfig() *cfg {
return &cfg{
addPeerMethod: defaultAddPeerMethod,
newEpochMethod: defaultNewEpochMethod,
netMapMethod: defaultNetMapMethod,
snapshotMethod: defaultSnapshotMethod,
epochSnapshotMethod: defaultEpochSnapshotMethod,
updateStateMethod: defaultUpdateStateMethod,
epochMethod: defaultEpochMethod,
setConfigMethod: defaultSetConfigMethod,
configMethod: defaultConfigMethod,
addPeerMethod: defaultAddPeerMethod,
configMethod: defaultConfigMethod,
epochMethod: defaultEpochMethod,
netMapCandidatesMethod: defaultNetMapCandidateMethod,
netMapMethod: defaultNetMapMethod,
newEpochMethod: defaultNewEpochMethod,
setConfigMethod: defaultSetConfigMethod,
snapshotMethod: defaultSnapshotMethod,
updateStateMethod: defaultUpdateStateMethod,
epochSnapshotMethod: defaultEpochSnapshotMethod,
}
}

Expand Down
135 changes: 133 additions & 2 deletions pkg/morph/client/netmap/netmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,55 @@ type EpochSnapshotValues struct {
*GetNetMapValues
}

const nodeInfoFixedPrmNumber = 1
// GetNetMapCandidatesArgs groups the arguments
// of get network map candidates test invoke call.
type GetNetMapCandidatesArgs struct {
}

// GetNetMapCandidatesValues groups the stack parameters
// returned by get network map candidates test invoke.
type GetNetMapCandidatesValues struct {
netmapNodes []*PeerWithState
}

func (g GetNetMapCandidatesValues) NetmapNodes() []*PeerWithState {
return g.netmapNodes
}

// State is an enumeration of various states of the NeoFS node.
type State int64
carpawell marked this conversation as resolved.
Show resolved Hide resolved

const (
// Undefined is unknown state.
Undefined State = iota

// Online is network unavailable state.
Online

// Offline is an active state in the network.
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 (
nodeInfoFixedPrmNumber = 1

peerWithStateFixedPrmNumber = 2
)

// SetDiff sets argument for snapshot method of
// netmap contract.
Expand Down Expand Up @@ -107,6 +155,85 @@ func (c *Client) EpochSnapshot(args EpochSnapshotArgs) (*EpochSnapshotValues, er
}, nil
}

func (c *Client) Candidates(_ GetNetMapCandidatesArgs) (*GetNetMapCandidatesValues, error) {
prms, err := c.client.TestInvoke(
c.netMapCandidatesMethod,
)
if err != nil {
return nil, fmt.Errorf("could not perform test invocation (%s): %w", c.netMapCandidatesMethod, err)
}

candVals, err := peersWithStateFromStackItems(prms, c.netMapCandidatesMethod)
if err != nil {
return nil, fmt.Errorf("could not parse contract response: %w", err)
}

return candVals, nil
}

func peersWithStateFromStackItems(stack []stackitem.Item, method string) (*GetNetMapCandidatesValues, error) {
if ln := len(stack); ln != 1 {
return nil, fmt.Errorf("unexpected stack item count (%s): %d", method, ln)
}

netmapNodes, 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)
}

res := &GetNetMapCandidatesValues{
netmapNodes: make([]*PeerWithState, 0, len(netmapNodes)),
}

for i := range netmapNodes {
node, err := peerWithStateFromStackItem(netmapNodes[i])
if err != nil {
return nil, fmt.Errorf("could not parse stack item (Peer #%d): %w", i, err)
}

res.netmapNodes = append(res.netmapNodes, node)
}

return res, nil
}

func peerWithStateFromStackItem(prm stackitem.Item) (*PeerWithState, error) {
prms, err := client.ArrayFromStackItem(prm)
if err != nil {
return nil, fmt.Errorf("could not get stack item array (PeerWithState): %w", err)
} else if ln := len(prms); ln != peerWithStateFixedPrmNumber {
return nil, fmt.Errorf(
"unexpected stack item count (PeerWithState): expected %d, has %d",
peerWithStateFixedPrmNumber,
ln,
)
}

var res PeerWithState

// peer
if res.peer, err = peerInfoFromStackItem(prms[0]); err != nil {
return nil, fmt.Errorf("could not get bytes from 'node' field of PeerWithState: %w", err)
}

// state
state, err := client.IntFromStackItem(prms[1])
if err != nil {
return nil, fmt.Errorf("could not get int from 'state' field of PeerWithState: %w", err)
}

switch state {
case 1:
res.state = Online
case 2:
res.state = Offline
default:
res.state = Undefined
}

return &res, nil
}

func peersFromStackItems(stack []stackitem.Item, method string) (*GetNetMapValues, error) {
if ln := len(stack); ln != 1 {
return nil, fmt.Errorf("unexpected stack item count (%s): %d",
Expand Down Expand Up @@ -140,7 +267,11 @@ func peerInfoFromStackItem(prm stackitem.Item) ([]byte, error) {
if err != nil {
return nil, fmt.Errorf("could not get stack item array (PeerInfo): %w", err)
} else if ln := len(prms); ln != nodeInfoFixedPrmNumber {
return nil, fmt.Errorf("unexpected stack item count (PeerInfo): expected %d, has %d", 1, ln)
return nil, fmt.Errorf(
"unexpected stack item count (PeerInfo): expected %d, has %d",
nodeInfoFixedPrmNumber,
ln,
)
}

return client.BytesFromStackItem(prms[0])
Expand Down
40 changes: 40 additions & 0 deletions pkg/morph/client/netmap/wrapper/netmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ func (w Wrapper) GetNetMapByEpoch(epoch uint64) (*netmap.Netmap, error) {
return unmarshalNetmap(vals.Peers())
}

// GetCandidates receives information list about candidates
// for the next epoch network map through the Netmap contract
// call, composes network map from them and returns it.
func (w Wrapper) GetCandidates() (*netmap.Netmap, error) {
args := client.GetNetMapCandidatesArgs{}

vals, err := w.client.Candidates(args)
if err != nil {
return nil, err
}

return unmarshalCandidates(vals.NetmapNodes())
}

func unmarshalNetmap(rawPeers [][]byte) (*netmap.Netmap, error) {
infos := make([]netmap.NodeInfo, 0, len(rawPeers))

Expand All @@ -54,3 +68,29 @@ func unmarshalNetmap(rawPeers [][]byte) (*netmap.Netmap, error) {

return netmap.NewNetmap(nodes)
}

func unmarshalCandidates(rawCandidate []*client.PeerWithState) (*netmap.Netmap, 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 client.Online:
nodeInfo.SetState(netmap.NodeStateOnline)
case client.Offline:
nodeInfo.SetState(netmap.NodeStateOffline)
default:
nodeInfo.SetState(0)
}

candidates = append(candidates, *nodeInfo)
}

nodes := netmap.NodesFromInfo(candidates)

return netmap.NewNetmap(nodes)
}