Skip to content

Commit

Permalink
[nspcc-dev#496] morph/container: Construct client wrapper in notary mode
Browse files Browse the repository at this point in the history
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 <leonard@nspcc.ru>
  • Loading branch information
Leonard Lyubich committed May 25, 2021
1 parent f460a6e commit 4e3e6e5
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
4 changes: 4 additions & 0 deletions pkg/morph/client/container/wrapper/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions pkg/morph/client/container/wrapper/eacl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions pkg/morph/client/container/wrapper/estimations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
Expand Down
28 changes: 26 additions & 2 deletions pkg/morph/client/container/wrapper/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 4e3e6e5

Please sign in to comment.