core: remove Persist from NativeCache interface

Lower native cache should be assigned to the upper's value during persist.
This commit is contained in:
Anna Shaleva 2022-04-19 12:39:34 +03:00
parent 8ec8511d9d
commit c8bdd2ad1a
7 changed files with 3 additions and 101 deletions

View file

@ -87,19 +87,6 @@ func (c *DesignationCache) Copy() storage.NativeContractCache {
return cp return cp
} }
// Persist implements NativeContractCache interface.
func (c *DesignationCache) Persist(ps storage.NativeContractCache) (storage.NativeContractCache, error) {
if ps == nil {
ps = &DesignationCache{}
}
psCache, ok := ps.(*DesignationCache)
if !ok {
return nil, errors.New("not a Designation native cache")
}
copyDesignationCache(c, psCache)
return psCache, nil
}
func copyDesignationCache(src, dst *DesignationCache) { func copyDesignationCache(src, dst *DesignationCache) {
*dst = *src *dst = *src
} }

View file

@ -84,21 +84,6 @@ func (c *ManagementCache) Copy() storage.NativeContractCache {
return cp return cp
} }
// Persist implements NativeContractCache interface.
func (c *ManagementCache) Persist(ps storage.NativeContractCache) (storage.NativeContractCache, error) {
if ps == nil {
ps = &ManagementCache{}
}
psCache, ok := ps.(*ManagementCache)
if !ok {
return nil, errors.New("not a Management native cache")
}
psCache.contracts = c.contracts
psCache.nep17 = c.nep17
psCache.nep11 = c.nep11
return psCache, nil
}
// MakeContractKey creates a key from account script hash. // MakeContractKey creates a key from account script hash.
func MakeContractKey(h util.Uint160) []byte { func MakeContractKey(h util.Uint160) []byte {
return makeUint160Key(prefixContract, h) return makeUint160Key(prefixContract, h)

View file

@ -119,19 +119,6 @@ func (c *NeoCache) Copy() storage.NativeContractCache {
return cp return cp
} }
// Persist implements NativeContractCache interface.
func (c *NeoCache) Persist(ps storage.NativeContractCache) (storage.NativeContractCache, error) {
if ps == nil {
ps = &NeoCache{}
}
psCache, ok := ps.(*NeoCache)
if !ok {
return nil, errors.New("not a NEO native cache")
}
copyNeoCache(c, psCache)
return psCache, nil
}
func copyNeoCache(src, dst *NeoCache) { func copyNeoCache(src, dst *NeoCache) {
dst.votesChanged = src.votesChanged dst.votesChanged = src.votesChanged
dst.nextValidators = src.nextValidators.Copy() dst.nextValidators = src.nextValidators.Copy()

View file

@ -68,19 +68,6 @@ func (c *NotaryCache) Copy() storage.NativeContractCache {
return cp return cp
} }
// Persist implements NativeContractCache interface.
func (c *NotaryCache) Persist(ps storage.NativeContractCache) (storage.NativeContractCache, error) {
if ps == nil {
ps = &NotaryCache{}
}
psCache, ok := ps.(*NotaryCache)
if !ok {
return nil, errors.New("not a Notary native cache")
}
copyNotaryCache(c, psCache)
return psCache, nil
}
func copyNotaryCache(src, dst *NotaryCache) { func copyNotaryCache(src, dst *NotaryCache) {
*dst = *src *dst = *src
} }

View file

@ -96,19 +96,6 @@ func (c *OracleCache) Copy() storage.NativeContractCache {
return cp return cp
} }
// Persist implements NativeContractCache interface.
func (c *OracleCache) Persist(ps storage.NativeContractCache) (storage.NativeContractCache, error) {
if ps == nil {
ps = &OracleCache{}
}
psCache, ok := ps.(*OracleCache)
if !ok {
return nil, errors.New("not an Oracle native cache")
}
copyOracleCache(c, psCache)
return psCache, nil
}
func copyOracleCache(src, dst *OracleCache) { func copyOracleCache(src, dst *OracleCache) {
*dst = *src *dst = *src
} }

View file

@ -1,7 +1,6 @@
package native package native
import ( import (
"errors"
"fmt" "fmt"
"math/big" "math/big"
"sort" "sort"
@ -79,19 +78,6 @@ func (c *PolicyCache) Copy() storage.NativeContractCache {
return cp return cp
} }
// Persist implements NativeContractCache interface.
func (c *PolicyCache) Persist(ps storage.NativeContractCache) (storage.NativeContractCache, error) {
if ps == nil {
ps = &PolicyCache{}
}
psCache, ok := ps.(*PolicyCache)
if !ok {
return nil, errors.New("not a Policy native cache")
}
copyPolicyCache(c, psCache)
return psCache, nil
}
func copyPolicyCache(src, dst *PolicyCache) { func copyPolicyCache(src, dst *PolicyCache) {
*dst = *src *dst = *src
dst.blockedAccounts = make([]util.Uint160, len(src.blockedAccounts)) dst.blockedAccounts = make([]util.Uint160, len(src.blockedAccounts))

View file

@ -3,7 +3,6 @@ package storage
import ( import (
"bytes" "bytes"
"context" "context"
"fmt"
"sort" "sort"
"strings" "strings"
"sync" "sync"
@ -33,9 +32,6 @@ type NativeContractCache interface {
// Copy returns a copy of native cache item that can safely be changed within // Copy returns a copy of native cache item that can safely be changed within
// the subsequent DAO operations. // the subsequent DAO operations.
Copy() NativeContractCache Copy() NativeContractCache
// Persist persists changes from upper native cache wrapper to the underlying
// native cache `ps`. The resulting up-to-date cache and an error are returned.
Persist(ps NativeContractCache) (NativeContractCache, error)
} }
type ( type (
@ -369,11 +365,7 @@ func (s *MemCachedStore) persist(isSync bool) (int, error) {
s.stor = nil s.stor = nil
if cached, ok := s.ps.(*MemCachedStore); ok { if cached, ok := s.ps.(*MemCachedStore); ok {
for id, nativeCache := range s.nativeCache { for id, nativeCache := range s.nativeCache {
updatedCache, err := nativeCache.Persist(cached.nativeCache[id]) cached.nativeCache[id] = nativeCache
if err != nil {
return 0, fmt.Errorf("failed to persist native cache changes for private MemCachedStore: %w", err)
}
cached.nativeCache[id] = updatedCache
} }
s.nativeCache = nil s.nativeCache = nil
} }
@ -411,12 +403,7 @@ func (s *MemCachedStore) persist(isSync bool) (int, error) {
if isPSCached { if isPSCached {
cached.nativeCacheLock.Lock() cached.nativeCacheLock.Lock()
for id, nativeCache := range tempstore.nativeCache { for id, nativeCache := range tempstore.nativeCache {
updatedCache, err := nativeCache.Persist(cached.nativeCache[id]) cached.nativeCache[id] = nativeCache
if err != nil {
cached.nativeCacheLock.Unlock()
return 0, fmt.Errorf("failed to persist native cache changes: %w", err)
}
cached.nativeCache[id] = updatedCache
} }
cached.nativeCacheLock.Unlock() cached.nativeCacheLock.Unlock()
} }
@ -442,11 +429,7 @@ func (s *MemCachedStore) persist(isSync bool) (int, error) {
} }
if isPSCached { if isPSCached {
for id, nativeCache := range s.nativeCache { for id, nativeCache := range s.nativeCache {
updatedCache, err := nativeCache.Persist(tempstore.nativeCache[id]) tempstore.nativeCache[id] = nativeCache
if err != nil {
return 0, fmt.Errorf("failed to persist native cache changes: %w", err)
}
tempstore.nativeCache[id] = updatedCache
} }
s.nativeCache = tempstore.nativeCache s.nativeCache = tempstore.nativeCache
} }