forked from TrueCloudLab/frostfs-node
[#625] morph/client: make method names constant
We don't use custom names and the only place where custom method option is used it provides the default name and can be omitted. Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
parent
35dec2f494
commit
3c5b62d839
44 changed files with 249 additions and 1097 deletions
|
@ -1,8 +1,6 @@
|
|||
package netmap
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
|
||||
"github.com/nspcc-dev/neofs-sdk-go/netmap"
|
||||
)
|
||||
|
@ -19,216 +17,39 @@ type NodeInfo = netmap.NodeInfo
|
|||
// and can lead to panic.
|
||||
type Client struct {
|
||||
client *client.StaticClient // static Netmap contract client
|
||||
|
||||
*cfg // contract method names
|
||||
}
|
||||
|
||||
// ErrNilClient is returned by functions that expect
|
||||
// a non-nil Client pointer, but received nil.
|
||||
var ErrNilClient = errors.New("netmap contract client is nil")
|
||||
|
||||
// Option is a client configuration change function.
|
||||
type Option func(*cfg)
|
||||
|
||||
type cfg struct {
|
||||
addPeerMethod, // add peer method name for invocation
|
||||
newEpochMethod, // new epoch method name for invocation
|
||||
innerRingList, // get innerring list 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
|
||||
epochMethod, // get epoch number method name
|
||||
lastEpochBlockMethod, // get last epoch number method name
|
||||
updateInnerRing, // update innerring method name
|
||||
setConfigMethod, // set config method name
|
||||
configMethod, // get config value method name
|
||||
configListMethod string // list config method name
|
||||
}
|
||||
|
||||
const (
|
||||
defaultAddPeerMethod = "addPeer" // default add peer method name
|
||||
defaultConfigMethod = "config" // default get config value method name
|
||||
defaultEpochMethod = "epoch" // default get epoch number method name
|
||||
defaultLastEpochBlockMethod = "lastEpochBlock" // default get last epoch block number method name
|
||||
defaultInnerRingListMethod = "innerRingList" // default get innerring list 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
|
||||
defaultUpdateInnerRingMethod = "updateInnerRing" // default update innerring method name
|
||||
defaultSnapshotMethod = "snapshot" // default get network map snapshot method name
|
||||
defaultUpdateStateMethod = "updateState" // default update state method name
|
||||
addPeerMethod = "addPeer"
|
||||
configMethod = "config"
|
||||
epochMethod = "epoch"
|
||||
lastEpochBlockMethod = "lastEpochBlock"
|
||||
innerRingListMethod = "innerRingList"
|
||||
netMapCandidatesMethod = "netmapCandidates"
|
||||
netMapMethod = "netmap"
|
||||
newEpochMethod = "newEpoch"
|
||||
setConfigMethod = "setConfig"
|
||||
updateInnerRingMethod = "updateInnerRing"
|
||||
snapshotMethod = "snapshot"
|
||||
updateStateMethod = "updateState"
|
||||
|
||||
defaultEpochSnapshotMethod = "snapshotByEpoch" // default get network map snapshot by epoch method name
|
||||
epochSnapshotMethod = "snapshotByEpoch"
|
||||
|
||||
defaultConfigListMethod = "listConfig" // default config listing method name
|
||||
configListMethod = "listConfig"
|
||||
)
|
||||
|
||||
func defaultConfig() *cfg {
|
||||
return &cfg{
|
||||
addPeerMethod: defaultAddPeerMethod,
|
||||
configMethod: defaultConfigMethod,
|
||||
epochMethod: defaultEpochMethod,
|
||||
lastEpochBlockMethod: defaultLastEpochBlockMethod,
|
||||
innerRingList: defaultInnerRingListMethod,
|
||||
netMapCandidatesMethod: defaultNetMapCandidateMethod,
|
||||
netMapMethod: defaultNetMapMethod,
|
||||
newEpochMethod: defaultNewEpochMethod,
|
||||
setConfigMethod: defaultSetConfigMethod,
|
||||
snapshotMethod: defaultSnapshotMethod,
|
||||
updateStateMethod: defaultUpdateStateMethod,
|
||||
updateInnerRing: defaultUpdateInnerRingMethod,
|
||||
epochSnapshotMethod: defaultEpochSnapshotMethod,
|
||||
configListMethod: defaultConfigListMethod,
|
||||
}
|
||||
}
|
||||
|
||||
// New creates, initializes and returns the Client instance.
|
||||
//
|
||||
// If StaticClient is nil, client.ErrNilStaticClient is returned.
|
||||
//
|
||||
// Other values are set according to provided options, or by default:
|
||||
// * add peer method name: AddPeer;
|
||||
// * new epoch method name: NewEpoch;
|
||||
// * get network map method name: Netmap;
|
||||
// * update state method name: UpdateState;
|
||||
//
|
||||
// If desired option satisfies the default value, it can be omitted.
|
||||
// If multiple options of the same config value are supplied,
|
||||
// the option with the highest index in the arguments will be used.
|
||||
func New(c *client.StaticClient, opts ...Option) (*Client, error) {
|
||||
func New(c *client.StaticClient) (*Client, error) {
|
||||
if c == nil {
|
||||
return nil, client.ErrNilStaticClient
|
||||
}
|
||||
|
||||
res := &Client{
|
||||
client: c,
|
||||
cfg: defaultConfig(), // build default configuration
|
||||
}
|
||||
|
||||
// apply options
|
||||
for _, opt := range opts {
|
||||
opt(res.cfg)
|
||||
}
|
||||
|
||||
return res, nil
|
||||
return &Client{client: c}, nil
|
||||
}
|
||||
|
||||
// Morph returns raw morph client.
|
||||
func (c Client) Morph() *client.Client {
|
||||
return c.client.Morph()
|
||||
}
|
||||
|
||||
// WithAddPeerMethod returns a client constructor option that
|
||||
// specifies the method name of adding peer operation.
|
||||
//
|
||||
// Ignores empty value.
|
||||
//
|
||||
// If option not provided, "AddPeer" is used.
|
||||
func WithAddPeerMethod(n string) Option {
|
||||
return func(c *cfg) {
|
||||
if n != "" {
|
||||
c.addPeerMethod = n
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// WithNewEpochMethod returns a client constructor option that
|
||||
// specifies the method name of new epoch operation.
|
||||
//
|
||||
// Ignores empty value.
|
||||
//
|
||||
// If option not provided, "NewEpoch" is used.
|
||||
func WithNewEpochMethod(n string) Option {
|
||||
return func(c *cfg) {
|
||||
if n != "" {
|
||||
c.newEpochMethod = n
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// WithNetMapMethod returns a client constructor option that
|
||||
// specifies the method name of network map receiving operation.
|
||||
//
|
||||
// Ignores empty value.
|
||||
//
|
||||
// If option not provided, "Netmap" is used.
|
||||
func WithNetMapMethod(n string) Option {
|
||||
return func(c *cfg) {
|
||||
if n != "" {
|
||||
c.netMapMethod = n
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// WithUpdateStateMethod returns a client constructor option that
|
||||
// specifies the method name of peer state updating operation.
|
||||
//
|
||||
// Ignores empty value.
|
||||
//
|
||||
// If option not provided, "UpdateState" is used.
|
||||
func WithUpdateStateMethod(n string) Option {
|
||||
return func(c *cfg) {
|
||||
if n != "" {
|
||||
c.updateStateMethod = n
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// WithConfigMethod returns a client constructor option that
|
||||
// specifies the method name of config value receiving operation.
|
||||
//
|
||||
// Ignores empty value.
|
||||
//
|
||||
// If option not provided, "config" is used.
|
||||
func WithConfigMethod(n string) Option {
|
||||
return func(c *cfg) {
|
||||
if n != "" {
|
||||
c.configMethod = n
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// WithConfigListMethod returns a client constructor option that
|
||||
// specifies the config listing method name.
|
||||
//
|
||||
// Ignores empty value.
|
||||
//
|
||||
// If option not provided, "listConfig" is used.
|
||||
func WithConfigListMethod(n string) Option {
|
||||
return func(c *cfg) {
|
||||
if n != "" {
|
||||
c.configListMethod = n
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue