[#1170] pkg/morph: Invalidate cache on RPC switch

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
Pavel Karpy 2022-03-31 16:12:42 +03:00 committed by Alex Vanin
parent 6508136204
commit 4ed5b1ceef
5 changed files with 58 additions and 25 deletions

View file

@ -39,7 +39,7 @@ import (
// expression (or just declaring a Client variable) is unsafe
// and can lead to panic.
type Client struct {
cache
cache cache
logger *logger.Logger // logging component
@ -76,11 +76,50 @@ type Client struct {
}
type cache struct {
nnsHash util.Uint160
groupKey *keys.PublicKey
m *sync.RWMutex
nnsHash *util.Uint160
gKey *keys.PublicKey
txHeights *lru.Cache
}
func (c cache) nns() *util.Uint160 {
c.m.RLock()
defer c.m.RUnlock()
return c.nnsHash
}
func (c *cache) setNNSHash(nnsHash util.Uint160) {
c.m.Lock()
defer c.m.Unlock()
c.nnsHash = &nnsHash
}
func (c cache) groupKey() *keys.PublicKey {
c.m.RLock()
defer c.m.RUnlock()
return c.gKey
}
func (c *cache) setGroupKey(groupKey *keys.PublicKey) {
c.m.Lock()
defer c.m.Unlock()
c.gKey = groupKey
}
func (c *cache) invalidate() {
c.m.Lock()
defer c.m.Unlock()
c.nnsHash = nil
c.gKey = nil
c.txHeights.Purge()
}
var (
// ErrNilClient is returned by functions that expect
// a non-nil Client pointer, but received nil.