[#303] morph/netmap: Implement getting network map snapshot by epoch

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-01-12 14:26:58 +03:00 committed by Alex Vanin
parent d58e28afee
commit 9ea75e51b2
3 changed files with 78 additions and 2 deletions

View file

@ -35,6 +35,7 @@ type cfg struct {
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 snapshotMethod, // get network map snapshot method name
epochSnapshotMethod, // get network map snapshot by epoch method name
updateStateMethod, // update state method name for invocation updateStateMethod, // update state method name for invocation
innerRingListMethod, // IR list method name for invocation innerRingListMethod, // IR list method name for invocation
epochMethod, // get epoch number method name epochMethod, // get epoch number method name
@ -50,6 +51,8 @@ const (
defaultInnerRIngListMethod = "innerRingList" // default IR list method name defaultInnerRIngListMethod = "innerRingList" // default IR list method name
defaultEpochMethod = "epoch" // default get epoch number method name defaultEpochMethod = "epoch" // default get epoch number method name
defaultConfigMethod = "config" // default get config value method name defaultConfigMethod = "config" // default get config value method name
defaultEpochSnapshotMethod = "snapshotByEpoch" // default get network map snapshot by epoch method name
) )
func defaultConfig() *cfg { func defaultConfig() *cfg {
@ -58,6 +61,7 @@ func defaultConfig() *cfg {
newEpochMethod: defaultNewEpochMethod, newEpochMethod: defaultNewEpochMethod,
netMapMethod: defaultNetMapMethod, netMapMethod: defaultNetMapMethod,
snapshotMethod: defaultSnapshotMethod, snapshotMethod: defaultSnapshotMethod,
epochSnapshotMethod: defaultEpochSnapshotMethod,
updateStateMethod: defaultUpdateStateMethod, updateStateMethod: defaultUpdateStateMethod,
innerRingListMethod: defaultInnerRIngListMethod, innerRingListMethod: defaultInnerRIngListMethod,
epochMethod: defaultEpochMethod, epochMethod: defaultEpochMethod,
@ -194,3 +198,17 @@ func WithConfigMethod(n string) Option {
} }
} }
} }
// WithEpochSnapshotMethod returns a client constructor option that
// specifies the method name of snapshot by value receiving operation.
//
// Ignores empty value.
//
// If option not provided, "snapshotByValue" is used.
func WithEpochSnapshotMethod(n string) Option {
return func(c *cfg) {
if n != "" {
c.epochSnapshotMethod = n
}
}
}

View file

@ -23,6 +23,18 @@ type GetNetMapValues struct {
peers [][]byte peers [][]byte
} }
// EpochSnapshotArgs groups the arguments
// of snapshot by epoch test invoke call.
type EpochSnapshotArgs struct {
epoch uint64
}
// EpochSnapshotValues groups the stack parameters
// returned by snapshot by epoch test invoke.
type EpochSnapshotValues struct {
*GetNetMapValues
}
const nodeInfoFixedPrmNumber = 1 const nodeInfoFixedPrmNumber = 1
// SetDiff sets argument for snapshot method of // SetDiff sets argument for snapshot method of
@ -31,6 +43,11 @@ func (g *GetSnapshotArgs) SetDiff(d uint64) {
g.diff = d g.diff = d
} }
// SetEpoch sets epoch number to get snapshot.
func (a *EpochSnapshotArgs) SetEpoch(d uint64) {
a.epoch = 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 {
@ -69,6 +86,29 @@ func (c *Client) Snapshot(a GetSnapshotArgs) (*GetNetMapValues, error) {
return peersFromStackItems(prms, c.snapshotMethod) return peersFromStackItems(prms, c.snapshotMethod)
} }
// EpochSnapshot performs the test invoke of get snapshot of network map by epoch
// from NeoFS Netmap contract.
func (c *Client) EpochSnapshot(args EpochSnapshotArgs) (*EpochSnapshotValues, error) {
prms, err := c.client.TestInvoke(
c.epochSnapshotMethod,
int64(args.epoch),
)
if err != nil {
return nil, errors.Wrapf(err,
"could not perform test invocation (%s)",
c.epochSnapshotMethod)
}
nmVals, err := peersFromStackItems(prms, c.epochSnapshotMethod)
if err != nil {
return nil, err
}
return &EpochSnapshotValues{
GetNetMapValues: nmVals,
}, nil
}
func peersFromStackItems(stack []stackitem.Item, method string) (*GetNetMapValues, error) { func peersFromStackItems(stack []stackitem.Item, method string) (*GetNetMapValues, error) {
if ln := len(stack); ln != 1 { if ln := len(stack); ln != 1 {
return nil, errors.Errorf("unexpected stack item count (%s): %d", return nil, errors.Errorf("unexpected stack item count (%s): %d",

View file

@ -14,12 +14,30 @@ func (w Wrapper) GetNetMap(diff uint64) (*netmap.Netmap, error) {
args := client.GetSnapshotArgs{} args := client.GetSnapshotArgs{}
args.SetDiff(diff) args.SetDiff(diff)
peers, err := w.client.Snapshot(args) vals, err := w.client.Snapshot(args)
if err != nil { if err != nil {
return nil, err return nil, err
} }
rawPeers := peers.Peers() // slice of serialized node infos return unmarshalNetmap(vals.Peers())
}
// GetNetMapByEpoch receives information list about storage nodes
// through the Netmap contract call, composes network map
// from them and returns it. Returns snapshot of the specified epoch number.
func (w Wrapper) GetNetMapByEpoch(epoch uint64) (*netmap.Netmap, error) {
args := client.EpochSnapshotArgs{}
args.SetEpoch(epoch)
vals, err := w.client.EpochSnapshot(args)
if err != nil {
return nil, err
}
return unmarshalNetmap(vals.Peers())
}
func unmarshalNetmap(rawPeers [][]byte) (*netmap.Netmap, error) {
infos := make([]netmap.NodeInfo, 0, len(rawPeers)) infos := make([]netmap.NodeInfo, 0, len(rawPeers))
for _, peer := range rawPeers { for _, peer := range rawPeers {