From 4838d3bb80a0a4b60fc7a8ba6da890673259b99b Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Sat, 29 Jan 2022 14:07:12 +0300 Subject: [PATCH] [#749] morph/client: cache NNS contract hash Signed-off-by: Evgenii Stratonikov --- pkg/morph/client/client.go | 7 ++++++- pkg/morph/client/nns.go | 11 +++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/pkg/morph/client/client.go b/pkg/morph/client/client.go index 9d8d26ebf..b60ffe79e 100644 --- a/pkg/morph/client/client.go +++ b/pkg/morph/client/client.go @@ -33,13 +33,18 @@ import ( // expression (or just declaring a Client variable) is unsafe // and can lead to panic. type Client struct { - // two mutual exclusive modes, exactly one must be non-nil + cache + // two mutual exclusive modes, exactly one must be non-nil *singleClient // works with single neo-go client *multiClient // creates and caches single clients } +type cache struct { + nnsHash util.Uint160 +} + type singleClient struct { logger *logger.Logger // logging component diff --git a/pkg/morph/client/nns.go b/pkg/morph/client/nns.go index f20b62197..4ae3d303a 100644 --- a/pkg/morph/client/nns.go +++ b/pkg/morph/client/nns.go @@ -58,12 +58,15 @@ func (c *Client) NNSContractAddress(name string) (sh util.Uint160, err error) { }) } - cs, err := c.client.GetContractStateByID(nnsContractID) // cache it? - if err != nil { - return sh, fmt.Errorf("NNS contract state: %w", err) + if c.nnsHash.Equals(util.Uint160{}) { + cs, err := c.client.GetContractStateByID(nnsContractID) + if err != nil { + return sh, fmt.Errorf("NNS contract state: %w", err) + } + c.nnsHash = cs.Hash } - sh, err = nnsResolve(c.client, cs.Hash, name) + sh, err = nnsResolve(c.client, c.nnsHash, name) if err != nil { return sh, fmt.Errorf("NNS.resolve: %w", err) }