[#749] morph/client: cache NNS contract hash

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2022-01-29 14:07:12 +03:00 committed by Alex Vanin
parent 050a4bb2b0
commit 4838d3bb80
2 changed files with 13 additions and 5 deletions

View file

@ -33,13 +33,18 @@ import (
// expression (or just declaring a Client variable) is unsafe // expression (or just declaring a Client variable) is unsafe
// and can lead to panic. // and can lead to panic.
type Client struct { 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 *singleClient // works with single neo-go client
*multiClient // creates and caches single clients *multiClient // creates and caches single clients
} }
type cache struct {
nnsHash util.Uint160
}
type singleClient struct { type singleClient struct {
logger *logger.Logger // logging component logger *logger.Logger // logging component

View file

@ -58,12 +58,15 @@ func (c *Client) NNSContractAddress(name string) (sh util.Uint160, err error) {
}) })
} }
cs, err := c.client.GetContractStateByID(nnsContractID) // cache it? if c.nnsHash.Equals(util.Uint160{}) {
if err != nil { cs, err := c.client.GetContractStateByID(nnsContractID)
return sh, fmt.Errorf("NNS contract state: %w", err) 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 { if err != nil {
return sh, fmt.Errorf("NNS.resolve: %w", err) return sh, fmt.Errorf("NNS.resolve: %w", err)
} }