From 20fb8547b6361567f03c44b1d52d283a4be31c24 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Wed, 21 Oct 2020 19:14:29 +0300 Subject: [PATCH] [#60] morph/netmap: Implement Epoch method on Client Signed-off-by: Leonard Lyubich --- pkg/morph/client/netmap/client.go | 19 +++++++++++- pkg/morph/client/netmap/epoch.go | 49 +++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 pkg/morph/client/netmap/epoch.go diff --git a/pkg/morph/client/netmap/client.go b/pkg/morph/client/netmap/client.go index 6df9cbbd1..41de084f0 100644 --- a/pkg/morph/client/netmap/client.go +++ b/pkg/morph/client/netmap/client.go @@ -36,7 +36,8 @@ type cfg struct { netMapMethod, // get network map method name snapshotMethod, // get network map snapshot method name updateStateMethod, // update state method name for invocation - innerRingListMethod string // IR list method name for invocation + innerRingListMethod, // IR list method name for invocation + epochMethod string // get epoch number method name } const ( @@ -46,6 +47,7 @@ const ( defaultSnapshotMethod = "snapshot" // default get network map snapshot method name defaultUpdateStateMethod = "updateState" // default update state method name defaultInnerRIngListMethod = "innerRingList" // default IR list method name + defaultEpochMethod = "epoch" // default get epoch number method name ) func defaultConfig() *cfg { @@ -56,6 +58,7 @@ func defaultConfig() *cfg { snapshotMethod: defaultSnapshotMethod, updateStateMethod: defaultUpdateStateMethod, innerRingListMethod: defaultInnerRIngListMethod, + epochMethod: defaultEpochMethod, } } @@ -160,3 +163,17 @@ func WithInnerRingListMethod(n string) Option { } } } + +// WithEpochMethod returns a client constructor option that +// specifies the method name of epoch number receiving operation. +// +// Ignores empty value. +// +// If option not provided, "epoch" is used. +func WithEpochMethod(n string) Option { + return func(c *cfg) { + if n != "" { + c.epochMethod = n + } + } +} diff --git a/pkg/morph/client/netmap/epoch.go b/pkg/morph/client/netmap/epoch.go new file mode 100644 index 000000000..070cba880 --- /dev/null +++ b/pkg/morph/client/netmap/epoch.go @@ -0,0 +1,49 @@ +package netmap + +import ( + "github.com/nspcc-dev/neofs-node/pkg/morph/client" + "github.com/pkg/errors" +) + +// EpochArgs groups the arguments +// of get epoch number test invoke call. +type EpochArgs struct { +} + +// EpochValues groups the stack parameters +// returned by get epoch number test invoke. +type EpochValues struct { + num int64 +} + +// Number return the number of NeoFS epoch. +func (e EpochValues) Number() int64 { + return e.num +} + +// Epoch performs the test invoke of get epoch number +// method of NeoFS Netmap contract. +func (c *Client) Epoch(_ EpochArgs) (*EpochValues, error) { + items, err := c.client.TestInvoke( + c.epochMethod, + ) + if err != nil { + return nil, errors.Wrapf(err, + "could not perform test invocation (%s)", + c.epochMethod) + } + + if ln := len(items); ln != 1 { + return nil, errors.Errorf("unexpected stack item count (%s): %d", + c.epochMethod, ln) + } + + num, err := client.IntFromStackItem(items[0]) + if err != nil { + return nil, errors.Wrapf(err, "could not get number from stack item (%s)", c.epochMethod) + } + + return &EpochValues{ + num: num, + }, nil +}