[#1151] morph/client: Cache notary transaction heights

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2022-02-10 14:58:14 +03:00 committed by Alex Vanin
parent e0dce1043a
commit e10b8f53d6
3 changed files with 27 additions and 4 deletions

View file

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"time" "time"
lru "github.com/hashicorp/golang-lru"
"github.com/nspcc-dev/neo-go/pkg/core/native/nativenames" "github.com/nspcc-dev/neo-go/pkg/core/native/nativenames"
"github.com/nspcc-dev/neo-go/pkg/core/native/noderoles" "github.com/nspcc-dev/neo-go/pkg/core/native/noderoles"
"github.com/nspcc-dev/neo-go/pkg/core/transaction" "github.com/nspcc-dev/neo-go/pkg/core/transaction"
@ -42,8 +43,9 @@ type Client struct {
} }
type cache struct { type cache struct {
nnsHash util.Uint160 nnsHash util.Uint160
groupKey *keys.PublicKey groupKey *keys.PublicKey
txHeights *lru.Cache
} }
type singleClient struct { type singleClient struct {

View file

@ -4,6 +4,7 @@ import (
"context" "context"
"time" "time"
lru "github.com/hashicorp/golang-lru"
"github.com/nspcc-dev/neo-go/pkg/core/transaction" "github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/rpc/client" "github.com/nspcc-dev/neo-go/pkg/rpc/client"
@ -83,6 +84,7 @@ func New(key *keys.PrivateKey, endpoint string, opts ...Option) (*Client, error)
if cfg.singleCli != nil { if cfg.singleCli != nil {
return &Client{ return &Client{
cache: initClientCache(),
singleClient: blankSingleClient(cfg.singleCli, wallet.NewAccountFromPrivateKey(key), cfg), singleClient: blankSingleClient(cfg.singleCli, wallet.NewAccountFromPrivateKey(key), cfg),
}, nil }, nil
} }
@ -90,6 +92,7 @@ func New(key *keys.PrivateKey, endpoint string, opts ...Option) (*Client, error)
endpoints := append(cfg.extraEndpoints, endpoint) endpoints := append(cfg.extraEndpoints, endpoint)
return &Client{ return &Client{
cache: initClientCache(),
multiClient: &multiClient{ multiClient: &multiClient{
cfg: *cfg, cfg: *cfg,
account: wallet.NewAccountFromPrivateKey(key), account: wallet.NewAccountFromPrivateKey(key),
@ -99,6 +102,13 @@ func New(key *keys.PrivateKey, endpoint string, opts ...Option) (*Client, error)
}, nil }, nil
} }
func initClientCache() cache {
c, _ := lru.New(100) // returns error only if size is negative
return cache{
txHeights: c,
}
}
// WithContext returns a client constructor option that // WithContext returns a client constructor option that
// specifies the neo-go client context. // specifies the neo-go client context.
// //

View file

@ -876,13 +876,24 @@ func (c *Client) CalculateNonceAndVUB(hash util.Uint256) (nonce uint32, vub uint
return 0, 0, nil return 0, 0, nil
} }
// TODO: #1151 cache values since some operations uses same TX as triggers
nonce = binary.LittleEndian.Uint32(hash.BytesLE()) nonce = binary.LittleEndian.Uint32(hash.BytesLE())
height, err := c.client.GetTransactionHeight(hash) height, err := c.getTransactionHeight(hash)
if err != nil { if err != nil {
return 0, 0, fmt.Errorf("could not get transaction height: %w", err) return 0, 0, fmt.Errorf("could not get transaction height: %w", err)
} }
return nonce, height + c.notary.txValidTime, nil return nonce, height + c.notary.txValidTime, nil
} }
func (c *Client) getTransactionHeight(h util.Uint256) (uint32, error) {
if rh, ok := c.txHeights.Get(h); ok {
return rh.(uint32), nil
}
height, err := c.client.GetTransactionHeight(h)
if err != nil {
return 0, err
}
c.txHeights.Add(h, height)
return height, nil
}