[#35] Add snapshot method for morph/netmap client

With snapshot method node can get previous network
maps from previous epoch.

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2020-09-22 11:09:45 +03:00
parent c410b23c97
commit 92fb384c09
2 changed files with 53 additions and 9 deletions

View file

@ -34,6 +34,7 @@ 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
netMapMethod, // get network map method name netMapMethod, // get network map method name
snapshotMethod, // get network map snapshot method name
updateStateMethod, // update state method name for invocation updateStateMethod, // update state method name for invocation
innerRingListMethod string // IR list method name for invocation innerRingListMethod string // IR list method name for invocation
} }
@ -42,6 +43,7 @@ const (
defaultAddPeerMethod = "addPeer" // default add peer method name defaultAddPeerMethod = "addPeer" // default add peer method name
defaultNewEpochMethod = "newEpoch" // default new epoch method name defaultNewEpochMethod = "newEpoch" // default new epoch method name
defaultNetMapMethod = "netmap" // default get network map 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 defaultUpdateStateMethod = "updateState" // default update state method name
defaultInnerRIngListMethod = "innerRingList" // default IR list method name defaultInnerRIngListMethod = "innerRingList" // default IR list method name
) )
@ -51,6 +53,7 @@ func defaultConfig() *cfg {
addPeerMethod: defaultAddPeerMethod, addPeerMethod: defaultAddPeerMethod,
newEpochMethod: defaultNewEpochMethod, newEpochMethod: defaultNewEpochMethod,
netMapMethod: defaultNetMapMethod, netMapMethod: defaultNetMapMethod,
snapshotMethod: defaultSnapshotMethod,
updateStateMethod: defaultUpdateStateMethod, updateStateMethod: defaultUpdateStateMethod,
innerRingListMethod: defaultInnerRIngListMethod, innerRingListMethod: defaultInnerRIngListMethod,
} }

View file

@ -11,6 +11,12 @@ import (
type GetNetMapArgs struct { type GetNetMapArgs struct {
} }
// GetSnapshotArgs groups the arguments
// of get netmap snapshot test invoke call.
type GetSnapshotArgs struct {
diff uint64
}
// GetNetMapValues groups the stack parameters // GetNetMapValues groups the stack parameters
// returned by get network map test invoke. // returned by get network map test invoke.
type GetNetMapValues struct { type GetNetMapValues struct {
@ -19,6 +25,12 @@ type GetNetMapValues struct {
const nodeInfoFixedPrmNumber = 1 const nodeInfoFixedPrmNumber = 1
// SetDiff sets argument for snapshot method of
// netmap contract.
func (g *GetSnapshotArgs) SetDiff(d uint64) {
g.diff = d
}
// Peers return the list of peers from // Peers return the list of peers from
// network map in a binary format. // network map in a binary format.
func (g GetNetMapValues) Peers() [][]byte { func (g GetNetMapValues) Peers() [][]byte {
@ -32,24 +44,53 @@ func (c *Client) NetMap(_ GetNetMapArgs) (*GetNetMapValues, error) {
c.netMapMethod, c.netMapMethod,
) )
if err != nil { if err != nil {
return nil, errors.Wrapf(err, "could not perform test invocation (%s)", c.netMapMethod) return nil, errors.Wrapf(err,
} else if ln := len(prms); ln != 1 { "could not perform test invocation (%s)",
return nil, errors.Errorf("unexpected stack item count (%s): %d", c.netMapMethod, ln) c.netMapMethod)
} }
prms, err = client.ArrayFromStackItem(prms[0]) return peersFromStackItems(prms, c.netMapMethod)
}
// NetMap performs the test invoke of get snapshot of network map
// from NeoFS Netmap contract. Contract saves only one previous epoch,
// so all invokes with diff > 1 return error.
func (c *Client) Snapshot(a GetSnapshotArgs) (*GetNetMapValues, error) {
prms, err := c.client.TestInvoke(
c.snapshotMethod,
int64(a.diff),
)
if err != nil { if err != nil {
return nil, errors.Wrapf(err, "could not get stack item array from stack item (%s)", c.netMapMethod) return nil, errors.Wrapf(err,
"could not perform test invocation (%s)",
c.netMapMethod)
}
return peersFromStackItems(prms, c.snapshotMethod)
}
func peersFromStackItems(stack []stackitem.Item, method string) (*GetNetMapValues, error) {
if ln := len(stack); ln != 1 {
return nil, errors.Errorf("unexpected stack item count (%s): %d",
method, ln)
}
peers, err := client.ArrayFromStackItem(stack[0])
if err != nil {
return nil, errors.Wrapf(err,
"could not get stack item array from stack item (%s)",
method)
} }
res := &GetNetMapValues{ res := &GetNetMapValues{
peers: make([][]byte, 0, len(prms)), peers: make([][]byte, 0, len(peers)),
} }
for i := range prms { for i := range peers {
peer, err := peerInfoFromStackItem(prms[i]) peer, err := peerInfoFromStackItem(peers[i])
if err != nil { if err != nil {
return nil, errors.Wrapf(err, "could not parse stack item (Peer #%d)", i) return nil, errors.Wrapf(err,
"could not parse stack item (Peer #%d)", i)
} }
res.peers = append(res.peers, peer) res.peers = append(res.peers, peer)