From 9f122f279a202c3cfb8eb761f544d52b30f09298 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Tue, 25 May 2021 15:15:21 +0300 Subject: [PATCH] [#496] morph/container: Construct client wrapper in notary mode Some of the client wrapper's methods should produce notary contract's invocations. In previous implementation all wrappers provided separate methods to do it. Since notary and non-notary invocation scenarios have very different goals, it makes sense to separate the scenarios of using the client wrapper at the stage of its creation. Define `Option` constructor for container client wrapper. Add `TryNotary` option which enables tries of the notary invocations on underlying static client. Mark all notary-dedicated methods as deprecated. Signed-off-by: Leonard Lyubich --- .../client/container/wrapper/container.go | 4 +++ pkg/morph/client/container/wrapper/eacl.go | 2 ++ .../client/container/wrapper/estimations.go | 6 ++++ pkg/morph/client/container/wrapper/wrapper.go | 28 +++++++++++++++++-- 4 files changed, 38 insertions(+), 2 deletions(-) diff --git a/pkg/morph/client/container/wrapper/container.go b/pkg/morph/client/container/wrapper/container.go index e9c68cc6..aa49a58e 100644 --- a/pkg/morph/client/container/wrapper/container.go +++ b/pkg/morph/client/container/wrapper/container.go @@ -48,6 +48,8 @@ func Put(w *Wrapper, cnr *container.Container, sig *pkg.Signature) (*container.I // // Returns calculated container identifier and any error // encountered that caused the saving to interrupt. +// +// If TryNotary is provided, call notary contract. func (w *Wrapper) Put(cnr, key, sig []byte) error { if len(sig) == 0 || len(key) == 0 { return errNilArgument @@ -136,6 +138,8 @@ func Delete(w *Wrapper, cid *container.ID, sig *pkg.Signature) error { // // Returns any error encountered that caused // the removal to interrupt. +// +// If TryNotary is provided, calls notary contract. func (w *Wrapper) Delete(cid, signature []byte) error { if len(signature) == 0 { return errNilArgument diff --git a/pkg/morph/client/container/wrapper/eacl.go b/pkg/morph/client/container/wrapper/eacl.go index 7a23d347..e63702eb 100644 --- a/pkg/morph/client/container/wrapper/eacl.go +++ b/pkg/morph/client/container/wrapper/eacl.go @@ -56,6 +56,8 @@ func (w *Wrapper) GetEACL(cid *containerSDK.ID) (*eacl.Table, *pkg.Signature, er // along with sig.Key() and sig.Sign(). // // Returns error if table is nil. +// +// If TryNotary is provided, calls notary contract. func PutEACL(w *Wrapper, table *eacl.Table, sig *pkg.Signature) error { if table == nil { return errNilArgument diff --git a/pkg/morph/client/container/wrapper/estimations.go b/pkg/morph/client/container/wrapper/estimations.go index 0c17c2b5..4ab76fe2 100644 --- a/pkg/morph/client/container/wrapper/estimations.go +++ b/pkg/morph/client/container/wrapper/estimations.go @@ -14,6 +14,9 @@ func (w *Wrapper) StartEstimation(epoch uint64) error { // StartEstimationNotary votes to produce start estimation notification through // notary contract. +// +// Deprecated: provide TryNotary() option to NewFromMorph +// and use StartEstimation. func (w *Wrapper) StartEstimationNotary(epoch uint64) error { args := container.StartEstimation{} args.SetEpoch(int64(epoch)) @@ -31,6 +34,9 @@ func (w *Wrapper) StopEstimation(epoch uint64) error { // StopEstimationNotary votes to produce stop estimation notification through // notary contract. +// +// Deprecated: provide TryNotary() option to NewFromMorph +// and use StopEstimation. func (w *Wrapper) StopEstimationNotary(epoch uint64) error { args := container.StopEstimation{} args.SetEpoch(int64(epoch)) diff --git a/pkg/morph/client/container/wrapper/wrapper.go b/pkg/morph/client/container/wrapper/wrapper.go index 32ff7e1e..5db748d9 100644 --- a/pkg/morph/client/container/wrapper/wrapper.go +++ b/pkg/morph/client/container/wrapper/wrapper.go @@ -21,9 +21,33 @@ type Wrapper struct { client *container.Client } +// Option allows to set an optional +// parameter of ClientWrapper. +type Option func(*opts) + +type opts []client.StaticClientOption + +func defaultOpts() *opts { + return new(opts) +} + +// TryNotaryInvoke returns option to enable +// notary invocation tries. +func TryNotary() Option { + return func(o *opts) { + *o = append(*o, client.TryNotary()) + } +} + // NewFromMorph returns the wrapper instance from the raw morph client. -func NewFromMorph(cli *client.Client, contract util.Uint160, fee fixedn.Fixed8) (*Wrapper, error) { - staticClient, err := client.NewStatic(cli, contract, fee) +func NewFromMorph(cli *client.Client, contract util.Uint160, fee fixedn.Fixed8, opts ...Option) (*Wrapper, error) { + o := defaultOpts() + + for i := range opts { + opts[i](o) + } + + staticClient, err := client.NewStatic(cli, contract, fee, ([]client.StaticClientOption)(*o)...) if err != nil { return nil, fmt.Errorf("can't create container static client: %w", err) }