From a49f2bc47c2962319ff1ee529b67dbe3dd04e874 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Wed, 29 Apr 2020 18:04:05 +0300 Subject: [PATCH] client: remove Balancer getter/setter, make it an Option Keep it internal to the client instance, it makes no sense exposing it to the outside user. --- pkg/rpc/client/client.go | 65 ++++++++++++++++++---------------------- pkg/rpc/client/rpc.go | 2 +- 2 files changed, 30 insertions(+), 37 deletions(-) diff --git a/pkg/rpc/client/client.go b/pkg/rpc/client/client.go index a60566d32..07e6c0539 100644 --- a/pkg/rpc/client/client.go +++ b/pkg/rpc/client/client.go @@ -29,20 +29,25 @@ const ( // Client represents the middleman for executing JSON RPC calls // to remote NEO RPC nodes. type Client struct { - cli *http.Client - endpoint *url.URL - ctx context.Context - version string - wifMu *sync.Mutex - wif *keys.WIF - balancerMu *sync.Mutex - balancer request.BalanceGetter + cli *http.Client + endpoint *url.URL + ctx context.Context + version string + wifMu *sync.Mutex + wif *keys.WIF + balancer request.BalanceGetter } // Options defines options for the RPC client. // All Values are optional. If any duration is not specified // a default of 3 seconds will be used. type Options struct { + // Balancer is an implementation of request.BalanceGetter interface, + // if not set then the default Client's implementation will be used, but + // it relies on server support for `getunspents` RPC call which is + // standard for neo-go, but only implemented as a plugin for C# node. So + // you can override it here to use NeoScanServer for example. + Balancer request.BalanceGetter Cert string Key string CACert string @@ -86,14 +91,18 @@ func New(ctx context.Context, endpoint string, opts Options) (*Client, error) { if opts.Cert != "" && opts.Key != "" { } - return &Client{ - ctx: ctx, - cli: httpClient, - balancerMu: new(sync.Mutex), - wifMu: new(sync.Mutex), - endpoint: url, - version: opts.Version, - }, nil + cl := &Client{ + ctx: ctx, + cli: httpClient, + wifMu: new(sync.Mutex), + endpoint: url, + version: opts.Version, + } + if opts.Balancer == nil { + opts.Balancer = cl + } + cl.balancer = opts.Balancer + return cl, nil } // WIF returns WIF structure associated with the client. @@ -121,26 +130,10 @@ func (c *Client) SetWIF(wif string) error { return nil } -// Balancer is a getter for balance field. -func (c *Client) Balancer() request.BalanceGetter { - c.balancerMu.Lock() - defer c.balancerMu.Unlock() - return c.balancer -} - -// SetBalancer is a setter for balance field. -func (c *Client) SetBalancer(b request.BalanceGetter) { - c.balancerMu.Lock() - defer c.balancerMu.Unlock() - - if b != nil { - c.balancer = b - } -} - -// CalculateInputs creates input transactions for the specified amount of given -// asset belonging to specified address. This implementation uses GetUnspents -// JSON-RPC call internally, so make sure your RPC server suppors that. +// CalculateInputs implements request.BalanceGetter interface and returns inputs +// array for the specified amount of given asset belonging to specified address. +// This implementation uses GetUnspents JSON-RPC call internally, so make sure +// your RPC server supports that. func (c *Client) CalculateInputs(address string, asset util.Uint256, cost util.Fixed8) ([]transaction.Input, util.Fixed8, error) { var utxos state.UnspentBalances diff --git a/pkg/rpc/client/rpc.go b/pkg/rpc/client/rpc.go index bb0144582..400303f88 100644 --- a/pkg/rpc/client/rpc.go +++ b/pkg/rpc/client/rpc.go @@ -484,7 +484,7 @@ func (c *Client) TransferAsset(asset util.Uint256, address string, amount util.F Address: address, Value: amount, WIF: c.WIF(), - Balancer: c.Balancer(), + Balancer: c.balancer, } resp util.Uint256 )