From b794aeab6376e11d3685cce1bcb83b12f455f9c9 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Tue, 25 May 2021 15:07:57 +0300 Subject: [PATCH] [#496] morph/client: Construct StaticClient in two work modes There are two scenarios of invocation of contract methods: 1. do not invoke notary contract; 2. try to invoke notary contract if it is enabled in Client. Taking this into account, `StaticClient` can work in one of the two described modes. Based on this, it makes sense at the stage of creating `StaticClient` to fix the call mode, and the further abstract from it. Define `StaticClientOption` setters of `StaticClient` optional parameters. Add `TryNotary` constructor of option which enables notary tries. Call `NotaryInvoke` on underlying `Client` if the option is provided, otherwise call `Invoke`. Mark `NotaryInvoke` method of `StaticClient` as deprecated. Signed-off-by: Leonard Lyubich --- pkg/morph/client/static.go | 39 +++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/pkg/morph/client/static.go b/pkg/morph/client/static.go index 98e6c4f36..feabf6f72 100644 --- a/pkg/morph/client/static.go +++ b/pkg/morph/client/static.go @@ -16,6 +16,8 @@ import ( // expression (or just declaring a StaticClient variable) is unsafe // and can lead to panic. type StaticClient struct { + tryNotary bool + client *Client // neo-go client instance scScriptHash util.Uint160 // contract script-hash @@ -23,6 +25,26 @@ type StaticClient struct { fee fixedn.Fixed8 // invocation fee } +type staticOpts struct { + tryNotary bool +} + +// StaticClientOption allows to set an optional +// parameter of StaticClient. +type StaticClientOption func(*staticOpts) + +func defaultStaticOpts() *staticOpts { + return new(staticOpts) +} + +// TryNotaryInvoke returns option to enable +// notary invocation tries. +func TryNotary() StaticClientOption { + return func(o *staticOpts) { + o.tryNotary = true + } +} + // ErrNilStaticClient is returned by functions that expect // a non-nil StaticClient pointer, but received nil. var ErrNilStaticClient = errors.New("static client is nil") @@ -30,12 +52,19 @@ var ErrNilStaticClient = errors.New("static client is nil") // NewStatic creates, initializes and returns the StaticClient instance. // // If provided Client instance is nil, ErrNilClient is returned. -func NewStatic(client *Client, scriptHash util.Uint160, fee fixedn.Fixed8) (*StaticClient, error) { +func NewStatic(client *Client, scriptHash util.Uint160, fee fixedn.Fixed8, opts ...StaticClientOption) (*StaticClient, error) { if client == nil { return nil, ErrNilClient } + o := defaultStaticOpts() + + for i := range opts { + opts[i](o) + } + return &StaticClient{ + tryNotary: o.tryNotary, client: client, scScriptHash: scriptHash, fee: fee, @@ -44,7 +73,13 @@ func NewStatic(client *Client, scriptHash util.Uint160, fee fixedn.Fixed8) (*Sta // Invoke calls Invoke method of Client with static internal script hash and fee. // Supported args types are the same as in Client. +// +// If TryNotary is provided, calls NotaryInvoke on Client. func (s StaticClient) Invoke(method string, args ...interface{}) error { + if s.tryNotary { + return s.client.NotaryInvoke(s.scScriptHash, s.fee, method, args...) + } + return s.client.Invoke( s.scScriptHash, s.fee, @@ -65,6 +100,8 @@ func (s StaticClient) TestInvoke(method string, args ...interface{}) ([]stackite // NotaryInvoke calls NotaryInvoke method of Client with static internal // script hash. Panics if notary support was not enabled in underlying // moprh client. +// +// Deprecated: provide TryNotary() option to NewStatic and use Invoke. func (s StaticClient) NotaryInvoke(method string, args ...interface{}) error { if s.client.notary == nil { panic(notaryNotEnabledPanicMsg)