From e910504f7ecbea641c91ca140b0e9578f7e204fb Mon Sep 17 00:00:00 2001 From: jules01 Date: Tue, 19 Sep 2023 13:39:58 +0300 Subject: [PATCH 1/8] - removed caches from core --- storage/errors.go | 11 - storage/interface.go | 69 --- storage/keyValuePair.go | 7 - storage/lrucache/capacity/capacityLRUCache.go | 284 ---------- .../capacity/capacityLRUCache_test.go | 499 ------------------ storage/lrucache/export_test.go | 5 - storage/lrucache/lrucache.go | 191 ------- storage/lrucache/lrucache_test.go | 420 --------------- storage/lrucache/simpleLRUCacheAdapter.go | 23 - 9 files changed, 1509 deletions(-) delete mode 100644 storage/errors.go delete mode 100644 storage/interface.go delete mode 100644 storage/keyValuePair.go delete mode 100644 storage/lrucache/capacity/capacityLRUCache.go delete mode 100644 storage/lrucache/capacity/capacityLRUCache_test.go delete mode 100644 storage/lrucache/export_test.go delete mode 100644 storage/lrucache/lrucache.go delete mode 100644 storage/lrucache/lrucache_test.go delete mode 100644 storage/lrucache/simpleLRUCacheAdapter.go diff --git a/storage/errors.go b/storage/errors.go deleted file mode 100644 index 7671c8818..000000000 --- a/storage/errors.go +++ /dev/null @@ -1,11 +0,0 @@ -package storage - -import ( - "errors" -) - -// ErrCacheSizeInvalid signals that size of cache is less than 1 -var ErrCacheSizeInvalid = errors.New("cache size is less than 1") - -// ErrCacheCapacityInvalid signals that capacity of cache is less than 1 -var ErrCacheCapacityInvalid = errors.New("cache capacity is less than 1") diff --git a/storage/interface.go b/storage/interface.go deleted file mode 100644 index 2cb05c422..000000000 --- a/storage/interface.go +++ /dev/null @@ -1,69 +0,0 @@ -package storage - -// Cacher provides caching services -type Cacher interface { - // Clear is used to completely clear the cache. - Clear() - // Put adds a value to the cache. Returns true if an eviction occurred. - Put(key []byte, value interface{}, sizeInBytes int) (evicted bool) - // Get looks up a key's value from the cache. - Get(key []byte) (value interface{}, ok bool) - // Has checks if a key is in the cache, without updating the - // recent-ness or deleting it for being stale. - Has(key []byte) bool - // Peek returns the key value (or undefined if not found) without updating - // the "recently used"-ness of the key. - Peek(key []byte) (value interface{}, ok bool) - // HasOrAdd checks if a key is in the cache without updating the - // recent-ness or deleting it for being stale, and if not adds the value. - HasOrAdd(key []byte, value interface{}, sizeInBytes int) (has, added bool) - // Remove removes the provided key from the cache. - Remove(key []byte) - // Keys returns a slice of the keys in the cache, from oldest to newest. - Keys() [][]byte - // Len returns the number of items in the cache. - Len() int - // SizeInBytesContained returns the size in bytes of all contained elements - SizeInBytesContained() uint64 - // MaxSize returns the maximum number of items which can be stored in the cache. - MaxSize() int - // RegisterHandler registers a new handler to be called when a new data is added - RegisterHandler(handler func(key []byte, value interface{}), id string) - // UnRegisterHandler deletes the handler from the list - UnRegisterHandler(id string) - // Close closes the underlying temporary db if the cacher implementation has one, - // otherwise it does nothing - Close() error - // IsInterfaceNil returns true if there is no value under the interface - IsInterfaceNil() bool -} - -// ForEachItem is an iterator callback -type ForEachItem func(key []byte, value interface{}) - -// LRUCacheHandler is the interface for LRU cache. -type LRUCacheHandler interface { - Add(key, value interface{}) bool - Get(key interface{}) (value interface{}, ok bool) - Contains(key interface{}) (ok bool) - ContainsOrAdd(key, value interface{}) (ok, evicted bool) - Peek(key interface{}) (value interface{}, ok bool) - Remove(key interface{}) bool - Keys() []interface{} - Len() int - Purge() -} - -// SizedLRUCacheHandler is the interface for size capable LRU cache. -type SizedLRUCacheHandler interface { - AddSized(key, value interface{}, sizeInBytes int64) bool - Get(key interface{}) (value interface{}, ok bool) - Contains(key interface{}) (ok bool) - AddSizedIfMissing(key, value interface{}, sizeInBytes int64) (ok, evicted bool) - Peek(key interface{}) (value interface{}, ok bool) - Remove(key interface{}) bool - Keys() []interface{} - Len() int - SizeInBytesContained() uint64 - Purge() -} diff --git a/storage/keyValuePair.go b/storage/keyValuePair.go deleted file mode 100644 index 06a5f11ed..000000000 --- a/storage/keyValuePair.go +++ /dev/null @@ -1,7 +0,0 @@ -package storage - -// KeyValuePair is a tuple of (key, value) -type KeyValuePair struct { - Key []byte - Value []byte -} diff --git a/storage/lrucache/capacity/capacityLRUCache.go b/storage/lrucache/capacity/capacityLRUCache.go deleted file mode 100644 index 275a440d6..000000000 --- a/storage/lrucache/capacity/capacityLRUCache.go +++ /dev/null @@ -1,284 +0,0 @@ -package capacity - -import ( - "container/list" - "sync" - - "github.com/multiversx/mx-chain-core-go/storage" -) - -// capacityLRU implements a non thread safe LRU Cache with a max capacity size -type capacityLRU struct { - lock sync.Mutex - size int - maxCapacityInBytes int64 - currentCapacityInBytes int64 - //TODO investigate if we can replace this list with a binary tree. Check also the other implementation lruCache - evictList *list.List - items map[interface{}]*list.Element -} - -// entry is used to hold a value in the evictList -type entry struct { - key interface{} - value interface{} - size int64 -} - -// NewCapacityLRU constructs an CapacityLRU of the given size with a byte size capacity -func NewCapacityLRU(size int, byteCapacity int64) (*capacityLRU, error) { - if size < 1 { - return nil, storage.ErrCacheSizeInvalid - } - if byteCapacity < 1 { - return nil, storage.ErrCacheCapacityInvalid - } - c := &capacityLRU{ - size: size, - maxCapacityInBytes: byteCapacity, - evictList: list.New(), - items: make(map[interface{}]*list.Element), - } - return c, nil -} - -// Purge is used to completely clear the cache. -func (c *capacityLRU) Purge() { - c.lock.Lock() - defer c.lock.Unlock() - - c.items = make(map[interface{}]*list.Element) - c.evictList.Init() - c.currentCapacityInBytes = 0 -} - -// AddSized adds a value to the cache. Returns true if an eviction occurred. -func (c *capacityLRU) AddSized(key, value interface{}, sizeInBytes int64) bool { - c.lock.Lock() - defer c.lock.Unlock() - - c.addSized(key, value, sizeInBytes) - - return c.evictIfNeeded() -} - -func (c *capacityLRU) addSized(key interface{}, value interface{}, sizeInBytes int64) { - if sizeInBytes < 0 { - return - } - - // Check for existing item - if ent, ok := c.items[key]; ok { - c.update(key, value, sizeInBytes, ent) - } else { - c.addNew(key, value, sizeInBytes) - } -} - -// AddSizedAndReturnEvicted adds the given key-value pair to the cache, and returns the evicted values -func (c *capacityLRU) AddSizedAndReturnEvicted(key, value interface{}, sizeInBytes int64) map[interface{}]interface{} { - c.lock.Lock() - defer c.lock.Unlock() - - c.addSized(key, value, sizeInBytes) - - evictedValues := make(map[interface{}]interface{}) - for c.shouldEvict() { - evicted := c.evictList.Back() - if evicted == nil { - continue - } - - c.removeElement(evicted) - evictedEntry, ok := evicted.Value.(*entry) - if !ok { - continue - } - - evictedValues[evictedEntry.key] = evictedEntry.value - } - - return evictedValues -} - -func (c *capacityLRU) addNew(key interface{}, value interface{}, sizeInBytes int64) { - ent := &entry{ - key: key, - value: value, - size: sizeInBytes, - } - e := c.evictList.PushFront(ent) - c.items[key] = e - c.currentCapacityInBytes += sizeInBytes -} - -func (c *capacityLRU) update(key interface{}, value interface{}, sizeInBytes int64, ent *list.Element) { - c.evictList.MoveToFront(ent) - - e := ent.Value.(*entry) - sizeDiff := sizeInBytes - e.size - e.value = value - e.size = sizeInBytes - c.currentCapacityInBytes += sizeDiff - - c.adjustSize(key, sizeInBytes) -} - -// Get looks up a key's value from the cache. -func (c *capacityLRU) Get(key interface{}) (interface{}, bool) { - c.lock.Lock() - defer c.lock.Unlock() - - if ent, ok := c.items[key]; ok { - c.evictList.MoveToFront(ent) - if ent.Value.(*entry) == nil { - return nil, false - } - - return ent.Value.(*entry).value, true - } - - return nil, false -} - -// Contains checks if a key is in the cache, without updating the recent-ness -// or deleting it for being stale. -func (c *capacityLRU) Contains(key interface{}) bool { - c.lock.Lock() - defer c.lock.Unlock() - - _, ok := c.items[key] - - return ok -} - -// AddSizedIfMissing checks if a key is in the cache without updating the -// recent-ness or deleting it for being stale, and if not, adds the value. -// Returns whether found and whether an eviction occurred. -func (c *capacityLRU) AddSizedIfMissing(key, value interface{}, sizeInBytes int64) (bool, bool) { - if sizeInBytes < 0 { - return false, false - } - - c.lock.Lock() - defer c.lock.Unlock() - - _, ok := c.items[key] - if ok { - return true, false - } - c.addNew(key, value, sizeInBytes) - evicted := c.evictIfNeeded() - - return false, evicted -} - -// Peek returns the key value (or undefined if not found) without updating -// the "recently used"-ness of the key. -func (c *capacityLRU) Peek(key interface{}) (interface{}, bool) { - c.lock.Lock() - defer c.lock.Unlock() - - ent, ok := c.items[key] - if ok { - return ent.Value.(*entry).value, true - } - return nil, ok -} - -// Remove removes the provided key from the cache, returning if the -// key was contained. -func (c *capacityLRU) Remove(key interface{}) bool { - c.lock.Lock() - defer c.lock.Unlock() - - if ent, ok := c.items[key]; ok { - c.removeElement(ent) - return true - } - return false -} - -// Keys returns a slice of the keys in the cache, from oldest to newest. -func (c *capacityLRU) Keys() []interface{} { - c.lock.Lock() - defer c.lock.Unlock() - - keys := make([]interface{}, len(c.items)) - i := 0 - for ent := c.evictList.Back(); ent != nil; ent = ent.Prev() { - keys[i] = ent.Value.(*entry).key - i++ - } - return keys -} - -// Len returns the number of items in the cache. -func (c *capacityLRU) Len() int { - c.lock.Lock() - defer c.lock.Unlock() - - return c.evictList.Len() -} - -// SizeInBytesContained returns the size in bytes of all contained elements -func (c *capacityLRU) SizeInBytesContained() uint64 { - c.lock.Lock() - defer c.lock.Unlock() - - return uint64(c.currentCapacityInBytes) -} - -// removeOldest removes the oldest item from the cache. -func (c *capacityLRU) removeOldest() { - ent := c.evictList.Back() - if ent != nil { - c.removeElement(ent) - } -} - -// removeElement is used to remove a given list element from the cache -func (c *capacityLRU) removeElement(e *list.Element) { - c.evictList.Remove(e) - kv := e.Value.(*entry) - delete(c.items, kv.key) - c.currentCapacityInBytes -= kv.size -} - -func (c *capacityLRU) adjustSize(key interface{}, sizeInBytes int64) { - element := c.items[key] - if element == nil || element.Value == nil || element.Value.(*entry) == nil { - return - } - - v := element.Value.(*entry) - c.currentCapacityInBytes -= v.size - v.size = sizeInBytes - element.Value = v - c.currentCapacityInBytes += sizeInBytes - c.evictIfNeeded() -} - -func (c *capacityLRU) shouldEvict() bool { - if c.evictList.Len() == 1 { - // keep at least one element, no matter how large it is - return false - } - - return c.evictList.Len() > c.size || c.currentCapacityInBytes > c.maxCapacityInBytes -} - -func (c *capacityLRU) evictIfNeeded() bool { - evicted := false - for c.shouldEvict() { - c.removeOldest() - evicted = true - } - - return evicted -} - -// IsInterfaceNil returns true if there is no value under the interface -func (c *capacityLRU) IsInterfaceNil() bool { - return c == nil -} diff --git a/storage/lrucache/capacity/capacityLRUCache_test.go b/storage/lrucache/capacity/capacityLRUCache_test.go deleted file mode 100644 index c59d4877e..000000000 --- a/storage/lrucache/capacity/capacityLRUCache_test.go +++ /dev/null @@ -1,499 +0,0 @@ -package capacity - -import ( - "testing" - - "github.com/multiversx/mx-chain-core-go/core/check" - "github.com/multiversx/mx-chain-core-go/storage" - "github.com/stretchr/testify/assert" -) - -func createDefaultCache() *capacityLRU { - cache, _ := NewCapacityLRU(100, 100) - return cache -} - -//------- NewCapacityLRU - -func TestNewCapacityLRU_WithInvalidSize(t *testing.T) { - t.Parallel() - - size := 0 - capacity := int64(1) - cache, err := NewCapacityLRU(size, capacity) - assert.True(t, check.IfNil(cache)) - assert.Equal(t, storage.ErrCacheSizeInvalid, err) -} - -func TestNewCapacityLRU_WithInvalidCapacity(t *testing.T) { - t.Parallel() - - size := 1 - capacity := int64(0) - cache, err := NewCapacityLRU(size, capacity) - assert.Nil(t, cache) - assert.Equal(t, storage.ErrCacheCapacityInvalid, err) -} - -func TestNewCapacityLRU(t *testing.T) { - t.Parallel() - - size := 1 - capacity := int64(5) - - cache, err := NewCapacityLRU(size, capacity) - assert.False(t, check.IfNil(cache)) - assert.Nil(t, err) - assert.Equal(t, size, cache.size) - assert.Equal(t, capacity, cache.maxCapacityInBytes) - assert.Equal(t, int64(0), cache.currentCapacityInBytes) - assert.NotNil(t, cache.evictList) - assert.NotNil(t, cache.items) -} - -//------- AddSized - -func TestCapacityLRUCache_AddSizedNegativeSizeInBytesShouldReturn(t *testing.T) { - t.Parallel() - - c := createDefaultCache() - data := []byte("test") - key := "key" - c.AddSized(key, data, -1) - - assert.Equal(t, 0, c.Len()) -} - -func TestCapacityLRUCache_AddSizedSimpleTestShouldWork(t *testing.T) { - t.Parallel() - - c := createDefaultCache() - data := []byte("test") - key := "key" - capacity := int64(5) - c.AddSized(key, data, capacity) - - v, ok := c.Get(key) - assert.True(t, ok) - assert.NotNil(t, v) - assert.Equal(t, data, v) - - keys := c.Keys() - assert.Equal(t, 1, len(keys)) - assert.Equal(t, key, keys[0]) -} - -func TestCapacityLRUCache_AddSizedEvictionByCacheSizeShouldWork(t *testing.T) { - t.Parallel() - - c, _ := NewCapacityLRU(3, 100000) - - keys := []string{"key1", "key2", "key3", "key4", "key5"} - - c.AddSized(keys[0], struct{}{}, 0) - assert.Equal(t, 1, c.Len()) - - c.AddSized(keys[1], struct{}{}, 0) - assert.Equal(t, 2, c.Len()) - - c.AddSized(keys[2], struct{}{}, 0) - assert.Equal(t, 3, c.Len()) - - c.AddSized(keys[3], struct{}{}, 0) - assert.Equal(t, 3, c.Len()) - assert.False(t, c.Contains(keys[0])) - assert.True(t, c.Contains(keys[3])) - - c.AddSized(keys[4], struct{}{}, 0) - assert.Equal(t, 3, c.Len()) - assert.False(t, c.Contains(keys[1])) - assert.True(t, c.Contains(keys[4])) -} - -func TestCapacityLRUCache_AddSizedEvictionBySizeInBytesShouldWork(t *testing.T) { - t.Parallel() - - c, _ := NewCapacityLRU(100000, 1000) - - keys := []string{"key1", "key2", "key3", "key4"} - - c.AddSized(keys[0], struct{}{}, 500) - assert.Equal(t, 1, c.Len()) - - c.AddSized(keys[1], struct{}{}, 500) - assert.Equal(t, 2, c.Len()) - - c.AddSized(keys[2], struct{}{}, 500) - assert.Equal(t, 2, c.Len()) - assert.False(t, c.Contains(keys[0])) - assert.True(t, c.Contains(keys[2])) - - c.AddSized(keys[3], struct{}{}, 500) - assert.Equal(t, 2, c.Len()) - assert.False(t, c.Contains(keys[1])) - assert.True(t, c.Contains(keys[3])) -} - -func TestCapacityLRUCache_AddSizedEvictionBySizeInBytesOneLargeElementShouldWork(t *testing.T) { - t.Parallel() - - c, _ := NewCapacityLRU(100000, 1000) - - keys := []string{"key1", "key2", "key3", "key4"} - - c.AddSized(keys[0], struct{}{}, 500) - assert.Equal(t, 1, c.Len()) - - c.AddSized(keys[1], struct{}{}, 500) - assert.Equal(t, 2, c.Len()) - - c.AddSized(keys[2], struct{}{}, 500) - assert.Equal(t, 2, c.Len()) - assert.False(t, c.Contains(keys[0])) - assert.True(t, c.Contains(keys[2])) - - c.AddSized(keys[3], struct{}{}, 500000) - assert.Equal(t, 1, c.Len()) - assert.False(t, c.Contains(keys[0])) - assert.False(t, c.Contains(keys[1])) - assert.False(t, c.Contains(keys[2])) - assert.True(t, c.Contains(keys[3])) -} - -func TestCapacityLRUCache_AddSizedEvictionBySizeInBytesOneLargeElementEvictedBySmallElementsShouldWork(t *testing.T) { - t.Parallel() - - c, _ := NewCapacityLRU(100000, 1000) - - keys := []string{"key1", "key2", "key3"} - - c.AddSized(keys[0], struct{}{}, 500000) - assert.Equal(t, 1, c.Len()) - - c.AddSized(keys[1], struct{}{}, 500) - assert.Equal(t, 1, c.Len()) - - c.AddSized(keys[2], struct{}{}, 500) - assert.Equal(t, 2, c.Len()) - assert.False(t, c.Contains(keys[0])) - assert.True(t, c.Contains(keys[1])) - assert.True(t, c.Contains(keys[2])) -} - -func TestCapacityLRUCache_AddSizedEvictionBySizeInBytesExistingOneLargeElementShouldWork(t *testing.T) { - t.Parallel() - - c, _ := NewCapacityLRU(100000, 1000) - - keys := []string{"key1", "key2"} - - c.AddSized(keys[0], struct{}{}, 500) - assert.Equal(t, 1, c.Len()) - - c.AddSized(keys[1], struct{}{}, 500) - assert.Equal(t, 2, c.Len()) - - c.AddSized(keys[0], struct{}{}, 500000) - assert.Equal(t, 1, c.Len()) - assert.True(t, c.Contains(keys[0])) - assert.False(t, c.Contains(keys[1])) -} - -//------- AddSizedIfMissing - -func TestCapacityLRUCache_AddSizedIfMissing(t *testing.T) { - t.Parallel() - - c := createDefaultCache() - data := []byte("data1") - key := "key" - - found, evicted := c.AddSizedIfMissing(key, data, 1) - assert.False(t, found) - assert.False(t, evicted) - - v, ok := c.Get(key) - assert.True(t, ok) - assert.NotNil(t, v) - assert.Equal(t, data, v) - - data2 := []byte("data2") - found, evicted = c.AddSizedIfMissing(key, data2, 1) - assert.True(t, found) - assert.False(t, evicted) - - v, ok = c.Get(key) - assert.True(t, ok) - assert.NotNil(t, v) - assert.Equal(t, data, v) -} - -func TestCapacityLRUCache_AddSizedIfMissingNegativeSizeInBytesShouldReturnFalse(t *testing.T) { - t.Parallel() - - c := createDefaultCache() - data := []byte("data1") - key := "key" - - has, evicted := c.AddSizedIfMissing(key, data, -1) - assert.False(t, has) - assert.False(t, evicted) - assert.Equal(t, 0, c.Len()) -} - -//------- Get - -func TestCapacityLRUCache_GetShouldWork(t *testing.T) { - t.Parallel() - - key := "key" - value := &struct{ A int }{A: 10} - - c := createDefaultCache() - c.AddSized(key, value, 0) - - recovered, exists := c.Get(key) - assert.True(t, value == recovered) //pointer testing - assert.True(t, exists) - - recovered, exists = c.Get("key not found") - assert.Nil(t, recovered) - assert.False(t, exists) -} - -//------- Purge - -func TestCapacityLRUCache_PurgeShouldWork(t *testing.T) { - t.Parallel() - - c, _ := NewCapacityLRU(100000, 1000) - - keys := []string{"key1", "key2"} - c.AddSized(keys[0], struct{}{}, 500) - c.AddSized(keys[1], struct{}{}, 500) - - c.Purge() - - assert.Equal(t, 0, c.Len()) - assert.Equal(t, int64(0), c.currentCapacityInBytes) -} - -//------- Peek - -func TestCapacityLRUCache_PeekNotFoundShouldWork(t *testing.T) { - t.Parallel() - - c, _ := NewCapacityLRU(100000, 1000) - val, found := c.Peek("key not found") - - assert.Nil(t, val) - assert.False(t, found) -} - -func TestCapacityLRUCache_PeekShouldWork(t *testing.T) { - t.Parallel() - - c, _ := NewCapacityLRU(100000, 1000) - key1 := "key1" - key2 := "key2" - val1 := &struct{}{} - - c.AddSized(key1, val1, 0) - c.AddSized(key2, struct{}{}, 0) - - //at this point key2 is more "recent" than key1 - assert.True(t, c.evictList.Front().Value.(*entry).key == key2) - - val, found := c.Peek(key1) - assert.True(t, val == val1) //pointer testing - assert.True(t, found) - - //recentness should not have been altered - assert.True(t, c.evictList.Front().Value.(*entry).key == key2) -} - -//------- Remove - -func TestCapacityLRUCache_RemoveNotFoundShouldWork(t *testing.T) { - t.Parallel() - - c, _ := NewCapacityLRU(100000, 1000) - removed := c.Remove("key not found") - - assert.False(t, removed) -} - -func TestCapacityLRUCache_RemovedShouldWork(t *testing.T) { - t.Parallel() - - c, _ := NewCapacityLRU(100000, 1000) - key1 := "key1" - key2 := "key2" - - c.AddSized(key1, struct{}{}, 0) - c.AddSized(key2, struct{}{}, 0) - - assert.Equal(t, 2, c.Len()) - - c.Remove(key1) - - assert.Equal(t, 1, c.Len()) - assert.True(t, c.Contains(key2)) -} - -// ---------- AddSizedAndReturnEvicted - -func TestCapacityLRUCache_AddSizedAndReturnEvictedNegativeSizeInBytesShouldReturn(t *testing.T) { - t.Parallel() - - c := createDefaultCache() - data := []byte("test") - key := "key" - c.AddSizedAndReturnEvicted(key, data, -1) - - assert.Equal(t, 0, c.Len()) -} - -func TestCapacityLRUCache_AddSizedAndReturnEvictedSimpleTestShouldWork(t *testing.T) { - t.Parallel() - - c := createDefaultCache() - data := []byte("test") - key := "key" - capacity := int64(5) - c.AddSizedAndReturnEvicted(key, data, capacity) - - v, ok := c.Get(key) - assert.True(t, ok) - assert.NotNil(t, v) - assert.Equal(t, data, v) - - keys := c.Keys() - assert.Equal(t, 1, len(keys)) - assert.Equal(t, key, keys[0]) -} - -func TestCapacityLRUCache_AddSizedAndReturnEvictedEvictionByCacheSizeShouldWork(t *testing.T) { - t.Parallel() - - c, _ := NewCapacityLRU(3, 100000) - - keys := []string{"key1", "key2", "key3", "key4", "key5"} - values := []string{"val1", "val2", "val3", "val4", "val5"} - - evicted := c.AddSizedAndReturnEvicted(keys[0], values[0], int64(len(values[0]))) - assert.Equal(t, 0, len(evicted)) - assert.Equal(t, 1, c.Len()) - - evicted = c.AddSizedAndReturnEvicted(keys[1], values[1], int64(len(values[1]))) - assert.Equal(t, 0, len(evicted)) - assert.Equal(t, 2, c.Len()) - - evicted = c.AddSizedAndReturnEvicted(keys[2], values[2], int64(len(values[2]))) - assert.Equal(t, 0, len(evicted)) - assert.Equal(t, 3, c.Len()) - - evicted = c.AddSizedAndReturnEvicted(keys[3], values[3], int64(len(values[3]))) - assert.Equal(t, 3, c.Len()) - assert.False(t, c.Contains(keys[0])) - assert.True(t, c.Contains(keys[3])) - assert.Equal(t, 1, len(evicted)) - assert.Equal(t, values[0], evicted[keys[0]]) - - evicted = c.AddSizedAndReturnEvicted(keys[4], values[4], int64(len(values[4]))) - assert.Equal(t, 3, c.Len()) - assert.False(t, c.Contains(keys[1])) - assert.True(t, c.Contains(keys[4])) - assert.Equal(t, 1, len(evicted)) - assert.Equal(t, values[1], evicted[keys[1]]) -} - -func TestCapacityLRUCache_AddSizedAndReturnEvictedEvictionBySizeInBytesShouldWork(t *testing.T) { - t.Parallel() - - c, _ := NewCapacityLRU(100000, 1000) - - keys := []string{"key1", "key2", "key3", "key4"} - values := []string{"val1", "val2", "val3", "val4"} - - evicted := c.AddSizedAndReturnEvicted(keys[0], values[0], 500) - assert.Equal(t, 0, len(evicted)) - assert.Equal(t, 1, c.Len()) - - evicted = c.AddSizedAndReturnEvicted(keys[1], values[1], 500) - assert.Equal(t, 0, len(evicted)) - assert.Equal(t, 2, c.Len()) - - evicted = c.AddSizedAndReturnEvicted(keys[2], values[2], 500) - assert.Equal(t, 2, c.Len()) - assert.False(t, c.Contains(keys[0])) - assert.True(t, c.Contains(keys[2])) - assert.Equal(t, 1, len(evicted)) - assert.Equal(t, values[0], evicted[keys[0]]) - - evicted = c.AddSizedAndReturnEvicted(keys[3], values[3], 500) - assert.Equal(t, 2, c.Len()) - assert.False(t, c.Contains(keys[1])) - assert.True(t, c.Contains(keys[3])) - assert.Equal(t, 1, len(evicted)) - assert.Equal(t, values[1], evicted[keys[1]]) -} - -func TestCapacityLRUCache_AddSizedAndReturnEvictedEvictionBySizeInBytesOneLargeElementShouldWork(t *testing.T) { - t.Parallel() - - c, _ := NewCapacityLRU(100000, 1000) - - keys := []string{"key1", "key2", "key3", "key4"} - values := []string{"val1", "val2", "val3", "val4"} - - evicted := c.AddSizedAndReturnEvicted(keys[0], values[0], 500) - assert.Equal(t, 0, len(evicted)) - assert.Equal(t, 1, c.Len()) - - evicted = c.AddSizedAndReturnEvicted(keys[1], values[1], 500) - assert.Equal(t, 0, len(evicted)) - assert.Equal(t, 2, c.Len()) - - evicted = c.AddSizedAndReturnEvicted(keys[2], values[2], 500) - assert.Equal(t, 2, c.Len()) - assert.False(t, c.Contains(keys[0])) - assert.True(t, c.Contains(keys[2])) - assert.Equal(t, 1, len(evicted)) - assert.Equal(t, values[0], evicted[keys[0]]) - - evicted = c.AddSizedAndReturnEvicted(keys[3], values[3], 500000) - assert.Equal(t, 1, c.Len()) - assert.False(t, c.Contains(keys[0])) - assert.False(t, c.Contains(keys[1])) - assert.False(t, c.Contains(keys[2])) - assert.True(t, c.Contains(keys[3])) - assert.Equal(t, 2, len(evicted)) - assert.Equal(t, values[1], evicted[keys[1]]) - assert.Equal(t, values[2], evicted[keys[2]]) -} - -func TestCapacityLRUCache_AddSizedAndReturnEvictedEvictionBySizeInBytesOneLargeElementEvictedBySmallElementsShouldWork(t *testing.T) { - t.Parallel() - - c, _ := NewCapacityLRU(100000, 1000) - - keys := []string{"key1", "key2", "key3"} - values := []string{"val1", "val2", "val3"} - - evicted := c.AddSizedAndReturnEvicted(keys[0], values[0], 500000) - assert.Equal(t, 0, len(evicted)) - assert.Equal(t, 1, c.Len()) - - evicted = c.AddSizedAndReturnEvicted(keys[1], values[1], 500) - assert.Equal(t, 1, c.Len()) - assert.Equal(t, 1, len(evicted)) - assert.Equal(t, values[0], evicted[keys[0]]) - - evicted = c.AddSizedAndReturnEvicted(keys[2], values[2], 500) - assert.Equal(t, 0, len(evicted)) - assert.Equal(t, 2, c.Len()) - assert.False(t, c.Contains(keys[0])) - assert.True(t, c.Contains(keys[1])) - assert.True(t, c.Contains(keys[2])) -} diff --git a/storage/lrucache/export_test.go b/storage/lrucache/export_test.go deleted file mode 100644 index 92889ed26..000000000 --- a/storage/lrucache/export_test.go +++ /dev/null @@ -1,5 +0,0 @@ -package lrucache - -func (c *lruCache) AddedDataHandlers() map[string]func(key []byte, value interface{}) { - return c.mapDataHandlers -} diff --git a/storage/lrucache/lrucache.go b/storage/lrucache/lrucache.go deleted file mode 100644 index 93a8d767c..000000000 --- a/storage/lrucache/lrucache.go +++ /dev/null @@ -1,191 +0,0 @@ -package lrucache - -import ( - "sync" - - lru "github.com/hashicorp/golang-lru" - "github.com/multiversx/mx-chain-core-go/storage" - "github.com/multiversx/mx-chain-core-go/storage/lrucache/capacity" -) - -var _ storage.Cacher = (*lruCache)(nil) - -// LRUCache implements a Least Recently Used eviction cache -type lruCache struct { - cache storage.SizedLRUCacheHandler - maxsize int - - mutAddedDataHandlers sync.RWMutex - mapDataHandlers map[string]func(key []byte, value interface{}) -} - -// NewCache creates a new LRU cache instance -func NewCache(size int) (*lruCache, error) { - cache, err := lru.New(size) - if err != nil { - return nil, err - } - - c := createLRUCache(size, cache) - - return c, nil -} - -// NewCacheWithEviction creates a new sized LRU cache instance with eviction function -func NewCacheWithEviction(size int, onEvicted func(key interface{}, value interface{})) (*lruCache, error) { - cache, err := lru.NewWithEvict(size, onEvicted) - if err != nil { - return nil, err - } - - c := createLRUCache(size, cache) - - return c, nil -} - -func createLRUCache(size int, cache *lru.Cache) *lruCache { - c := &lruCache{ - cache: &simpleLRUCacheAdapter{ - LRUCacheHandler: cache, - }, - maxsize: size, - mutAddedDataHandlers: sync.RWMutex{}, - mapDataHandlers: make(map[string]func(key []byte, value interface{})), - } - return c -} - -// NewCacheWithSizeInBytes creates a new sized LRU cache instance -func NewCacheWithSizeInBytes(size int, sizeInBytes int64) (*lruCache, error) { - cache, err := capacity.NewCapacityLRU(size, sizeInBytes) - if err != nil { - return nil, err - } - - c := &lruCache{ - cache: cache, - maxsize: size, - mutAddedDataHandlers: sync.RWMutex{}, - mapDataHandlers: make(map[string]func(key []byte, value interface{})), - } - - return c, nil -} - -// Clear is used to completely clear the cache. -func (c *lruCache) Clear() { - c.cache.Purge() -} - -// Put adds a value to the cache. Returns true if an eviction occurred. -func (c *lruCache) Put(key []byte, value interface{}, sizeInBytes int) (evicted bool) { - evicted = c.cache.AddSized(string(key), value, int64(sizeInBytes)) - - c.callAddedDataHandlers(key, value) - - return evicted -} - -// RegisterHandler registers a new handler to be called when a new data is added -func (c *lruCache) RegisterHandler(handler func(key []byte, value interface{}), id string) { - if handler == nil { - return - } - - c.mutAddedDataHandlers.Lock() - c.mapDataHandlers[id] = handler - c.mutAddedDataHandlers.Unlock() -} - -// UnRegisterHandler removes the handler from the list -func (c *lruCache) UnRegisterHandler(id string) { - c.mutAddedDataHandlers.Lock() - delete(c.mapDataHandlers, id) - c.mutAddedDataHandlers.Unlock() -} - -// Get looks up a key's value from the cache. -func (c *lruCache) Get(key []byte) (value interface{}, ok bool) { - return c.cache.Get(string(key)) -} - -// Has checks if a key is in the cache, without updating the -// recent-ness or deleting it for being stale. -func (c *lruCache) Has(key []byte) bool { - return c.cache.Contains(string(key)) -} - -// Peek returns the key value (or undefined if not found) without updating -// the "recently used"-ness of the key. -func (c *lruCache) Peek(key []byte) (value interface{}, ok bool) { - v, ok := c.cache.Peek(string(key)) - - if !ok { - return nil, ok - } - - return v, ok -} - -// HasOrAdd checks if a key is in the cache without updating the -// recent-ness or deleting it for being stale, and if not, adds the value. -// Returns whether found and whether an eviction occurred. -func (c *lruCache) HasOrAdd(key []byte, value interface{}, sizeInBytes int) (has, added bool) { - has, _ = c.cache.AddSizedIfMissing(string(key), value, int64(sizeInBytes)) - - if !has { - c.callAddedDataHandlers(key, value) - } - - return has, !has -} - -func (c *lruCache) callAddedDataHandlers(key []byte, value interface{}) { - c.mutAddedDataHandlers.RLock() - for _, handler := range c.mapDataHandlers { - go handler(key, value) - } - c.mutAddedDataHandlers.RUnlock() -} - -// Remove removes the provided key from the cache. -func (c *lruCache) Remove(key []byte) { - c.cache.Remove(string(key)) -} - -// Keys returns a slice of the keys in the cache, from oldest to newest. -func (c *lruCache) Keys() [][]byte { - res := c.cache.Keys() - r := make([][]byte, len(res)) - - for i := 0; i < len(res); i++ { - r[i] = []byte(res[i].(string)) - } - - return r -} - -// Len returns the number of items in the cache. -func (c *lruCache) Len() int { - return c.cache.Len() -} - -// SizeInBytesContained returns the size in bytes of all contained elements -func (c *lruCache) SizeInBytesContained() uint64 { - return c.cache.SizeInBytesContained() -} - -// MaxSize returns the maximum number of items which can be stored in cache. -func (c *lruCache) MaxSize() int { - return c.maxsize -} - -// Close does nothing for this cacher implementation -func (c *lruCache) Close() error { - return nil -} - -// IsInterfaceNil returns true if there is no value under the interface -func (c *lruCache) IsInterfaceNil() bool { - return c == nil -} diff --git a/storage/lrucache/lrucache_test.go b/storage/lrucache/lrucache_test.go deleted file mode 100644 index 8265b00cf..000000000 --- a/storage/lrucache/lrucache_test.go +++ /dev/null @@ -1,420 +0,0 @@ -package lrucache_test - -import ( - "bytes" - "fmt" - "sync" - "testing" - "time" - - "github.com/multiversx/mx-chain-core-go/core/check" - "github.com/multiversx/mx-chain-core-go/storage" - "github.com/multiversx/mx-chain-core-go/storage/lrucache" - "github.com/stretchr/testify/assert" -) - -var timeoutWaitForWaitGroups = time.Second * 2 - -//------- NewCache - -func TestNewCache_BadSizeShouldErr(t *testing.T) { - t.Parallel() - - c, err := lrucache.NewCache(0) - - assert.True(t, check.IfNil(c)) - assert.NotNil(t, err) -} - -func TestNewCache_ShouldWork(t *testing.T) { - t.Parallel() - - c, err := lrucache.NewCache(1) - - assert.False(t, check.IfNil(c)) - assert.Nil(t, err) -} - -//------- NewCacheWithSizeInBytes - -func TestNewCacheWithSizeInBytes_BadSizeShouldErr(t *testing.T) { - t.Parallel() - - c, err := lrucache.NewCacheWithSizeInBytes(0, 100000) - - assert.True(t, check.IfNil(c)) - assert.Equal(t, storage.ErrCacheSizeInvalid, err) -} - -func TestNewCacheWithSizeInBytes_BadSizeInBytesShouldErr(t *testing.T) { - t.Parallel() - - c, err := lrucache.NewCacheWithSizeInBytes(1, 0) - - assert.True(t, check.IfNil(c)) - assert.Equal(t, storage.ErrCacheCapacityInvalid, err) -} - -func TestNewCacheWithSizeInBytes_ShouldWork(t *testing.T) { - t.Parallel() - - c, err := lrucache.NewCacheWithSizeInBytes(1, 100000) - - assert.False(t, check.IfNil(c)) - assert.Nil(t, err) -} - -func TestLRUCache_PutNotPresent(t *testing.T) { - t.Parallel() - - key, val := []byte("key"), []byte("value") - c, _ := lrucache.NewCache(10) - - l := c.Len() - - assert.Zero(t, l, "cache expected to be empty") - - c.Put(key, val, 0) - l = c.Len() - - assert.Equal(t, l, 1, "cache size expected 1 but found %d", l) -} - -func TestLRUCache_PutPresent(t *testing.T) { - t.Parallel() - - key, val := []byte("key"), []byte("value") - c, _ := lrucache.NewCache(10) - - c.Put(key, val, 0) - c.Put(key, val, 0) - - l := c.Len() - assert.Equal(t, l, 1, "cache size expected 1 but found %d", l) -} - -func TestLRUCache_PutPresentRewrite(t *testing.T) { - t.Parallel() - - key := []byte("key") - val1 := []byte("value1") - val2 := []byte("value2") - c, _ := lrucache.NewCache(10) - - c.Put(key, val1, 0) - c.Put(key, val2, 0) - - l := c.Len() - assert.Equal(t, l, 1, "cache size expected 1 but found %d", l) - recoveredVal, has := c.Get(key) - assert.True(t, has) - assert.Equal(t, val2, recoveredVal) -} - -func TestLRUCache_GetNotPresent(t *testing.T) { - t.Parallel() - - key := []byte("key1") - c, _ := lrucache.NewCache(10) - - v, ok := c.Get(key) - - assert.False(t, ok, "value %s not expected to be found", v) -} - -func TestLRUCache_GetPresent(t *testing.T) { - t.Parallel() - - key, val := []byte("key2"), []byte("value2") - c, _ := lrucache.NewCache(10) - - c.Put(key, val, 0) - - v, ok := c.Get(key) - - assert.True(t, ok, "value expected but not found") - assert.Equal(t, val, v) -} - -func TestLRUCache_HasNotPresent(t *testing.T) { - t.Parallel() - - key := []byte("key3") - c, _ := lrucache.NewCache(10) - - found := c.Has(key) - - assert.False(t, found, "key %s not expected to be found", key) -} - -func TestLRUCache_HasPresent(t *testing.T) { - t.Parallel() - - key, val := []byte("key4"), []byte("value4") - c, _ := lrucache.NewCache(10) - - c.Put(key, val, 0) - - found := c.Has(key) - - assert.True(t, found, "value expected but not found") -} - -func TestLRUCache_PeekNotPresent(t *testing.T) { - t.Parallel() - - key := []byte("key5") - c, _ := lrucache.NewCache(10) - - _, ok := c.Peek(key) - - assert.False(t, ok, "not expected to find key %s", key) -} - -func TestLRUCache_PeekPresent(t *testing.T) { - t.Parallel() - - key, val := []byte("key6"), []byte("value6") - c, _ := lrucache.NewCache(10) - - c.Put(key, val, 0) - v, ok := c.Peek(key) - - assert.True(t, ok, "value expected but not found") - assert.Equal(t, val, v, "expected to find %s but found %s", val, v) -} - -func TestLRUCache_HasOrAddNotPresent(t *testing.T) { - t.Parallel() - - key, val := []byte("key7"), []byte("value7") - c, _ := lrucache.NewCache(10) - - _, ok := c.Peek(key) - assert.False(t, ok, "not expected to find key %s", key) - - c.HasOrAdd(key, val, 0) - v, ok := c.Peek(key) - assert.True(t, ok, "value expected but not found") - assert.Equal(t, val, v, "expected to find %s but found %s", val, v) -} - -func TestLRUCache_HasOrAddPresent(t *testing.T) { - t.Parallel() - - key, val := []byte("key8"), []byte("value8") - c, _ := lrucache.NewCache(10) - - _, ok := c.Peek(key) - - assert.False(t, ok, "not expected to find key %s", key) - - c.HasOrAdd(key, val, 0) - v, ok := c.Peek(key) - - assert.True(t, ok, "value expected but not found") - assert.Equal(t, val, v, "expected to find %s but found %s", val, v) -} - -func TestLRUCache_RemoveNotPresent(t *testing.T) { - t.Parallel() - - key := []byte("key9") - c, _ := lrucache.NewCache(10) - - found := c.Has(key) - - assert.False(t, found, "not expected to find key %s", key) - - c.Remove(key) - found = c.Has(key) - - assert.False(t, found, "not expected to find key %s", key) -} - -func TestLRUCache_RemovePresent(t *testing.T) { - t.Parallel() - - key, val := []byte("key10"), []byte("value10") - c, _ := lrucache.NewCache(10) - - c.Put(key, val, 0) - found := c.Has(key) - - assert.True(t, found, "expected to find key %s", key) - - c.Remove(key) - found = c.Has(key) - - assert.False(t, found, "not expected to find key %s", key) -} - -func TestLRUCache_Keys(t *testing.T) { - t.Parallel() - - c, _ := lrucache.NewCache(10) - - for i := 0; i < 20; i++ { - key, val := []byte(fmt.Sprintf("key%d", i)), []byte(fmt.Sprintf("value%d", i)) - c.Put(key, val, 0) - } - - keys := c.Keys() - - // check also that cache size does not grow over the capacity - assert.Equal(t, 10, len(keys), "expected cache size 10 but current size %d", len(keys)) -} - -func TestLRUCache_Len(t *testing.T) { - t.Parallel() - - c, _ := lrucache.NewCache(10) - - for i := 0; i < 20; i++ { - key, val := []byte(fmt.Sprintf("key%d", i)), []byte(fmt.Sprintf("value%d", i)) - c.Put(key, val, 0) - } - - l := c.Len() - - assert.Equal(t, 10, l, "expected cache size 10 but current size %d", l) -} - -func TestLRUCache_Clear(t *testing.T) { - t.Parallel() - - c, _ := lrucache.NewCache(10) - - for i := 0; i < 5; i++ { - key, val := []byte(fmt.Sprintf("key%d", i)), []byte(fmt.Sprintf("value%d", i)) - c.Put(key, val, 0) - } - - l := c.Len() - - assert.Equal(t, 5, l, "expected size 5, got %d", l) - - c.Clear() - l = c.Len() - - assert.Zero(t, l, "expected size 0, got %d", l) -} - -func TestLRUCache_CacherRegisterAddedDataHandlerNilHandlerShouldIgnore(t *testing.T) { - t.Parallel() - - c, _ := lrucache.NewCache(100) - c.RegisterHandler(nil, "") - - assert.Equal(t, 0, len(c.AddedDataHandlers())) -} - -func TestLRUCache_CacherRegisterPutAddedDataHandlerShouldWork(t *testing.T) { - t.Parallel() - - wg := sync.WaitGroup{} - wg.Add(1) - chDone := make(chan bool) - - f := func(key []byte, value interface{}) { - if !bytes.Equal([]byte("aaaa"), key) { - return - } - - wg.Done() - } - - go func() { - wg.Wait() - chDone <- true - }() - - c, _ := lrucache.NewCache(100) - c.RegisterHandler(f, "") - c.Put([]byte("aaaa"), "bbbb", 0) - - select { - case <-chDone: - case <-time.After(timeoutWaitForWaitGroups): - assert.Fail(t, "should have been called") - return - } - - assert.Equal(t, 1, len(c.AddedDataHandlers())) -} - -func TestLRUCache_CacherRegisterHasOrAddAddedDataHandlerShouldWork(t *testing.T) { - t.Parallel() - - wg := sync.WaitGroup{} - wg.Add(1) - chDone := make(chan bool) - - f := func(key []byte, value interface{}) { - if !bytes.Equal([]byte("aaaa"), key) { - return - } - - wg.Done() - } - - go func() { - wg.Wait() - chDone <- true - }() - - c, _ := lrucache.NewCache(100) - c.RegisterHandler(f, "") - c.HasOrAdd([]byte("aaaa"), "bbbb", 0) - - select { - case <-chDone: - case <-time.After(timeoutWaitForWaitGroups): - assert.Fail(t, "should have been called") - return - } - - assert.Equal(t, 1, len(c.AddedDataHandlers())) -} - -func TestLRUCache_CacherRegisterHasOrAddAddedDataHandlerNotAddedShouldNotCall(t *testing.T) { - t.Parallel() - - wg := sync.WaitGroup{} - wg.Add(1) - chDone := make(chan bool) - - f := func(key []byte, value interface{}) { - wg.Done() - } - - go func() { - wg.Wait() - chDone <- true - }() - - c, _ := lrucache.NewCache(100) - //first add, no call - c.HasOrAdd([]byte("aaaa"), "bbbb", 0) - c.RegisterHandler(f, "") - //second add, should not call as the data was found - c.HasOrAdd([]byte("aaaa"), "bbbb", 0) - - select { - case <-chDone: - assert.Fail(t, "should have not been called") - return - case <-time.After(timeoutWaitForWaitGroups): - } - - assert.Equal(t, 1, len(c.AddedDataHandlers())) -} - -func TestLRUCache_CloseShouldNotErr(t *testing.T) { - t.Parallel() - - c, _ := lrucache.NewCache(1) - - err := c.Close() - assert.Nil(t, err) -} diff --git a/storage/lrucache/simpleLRUCacheAdapter.go b/storage/lrucache/simpleLRUCacheAdapter.go deleted file mode 100644 index 82b481bd3..000000000 --- a/storage/lrucache/simpleLRUCacheAdapter.go +++ /dev/null @@ -1,23 +0,0 @@ -package lrucache - -import "github.com/multiversx/mx-chain-core-go/storage" - -// simpleLRUCacheAdapter provides an adapter between LRUCacheHandler and SizeLRUCacheHandler -type simpleLRUCacheAdapter struct { - storage.LRUCacheHandler -} - -// AddSized calls the Add method without the size in bytes parameter -func (slca *simpleLRUCacheAdapter) AddSized(key, value interface{}, _ int64) bool { - return slca.Add(key, value) -} - -// AddSizedIfMissing calls ContainsOrAdd without the size in bytes parameter -func (slca *simpleLRUCacheAdapter) AddSizedIfMissing(key, value interface{}, _ int64) (ok, evicted bool) { - return slca.ContainsOrAdd(key, value) -} - -// SizeInBytesContained returns 0 -func (slca *simpleLRUCacheAdapter) SizeInBytesContained() uint64 { - return 0 -} From 2af2ec1ec41b8883f6c36b6fd3616a315807f012 Mon Sep 17 00:00:00 2001 From: jules01 Date: Tue, 19 Sep 2023 13:41:02 +0300 Subject: [PATCH 2/8] - go mod tidy --- go.mod | 1 - go.sum | 2 -- 2 files changed, 3 deletions(-) diff --git a/go.mod b/go.mod index 3dc52a614..26892ac42 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,6 @@ require ( github.com/denisbrodbeck/machineid v1.0.1 github.com/gogo/protobuf v1.3.2 github.com/golang/protobuf v1.5.2 - github.com/hashicorp/golang-lru v0.5.4 github.com/mr-tron/base58 v1.2.0 github.com/pelletier/go-toml v1.9.3 github.com/pkg/errors v0.9.1 diff --git a/go.sum b/go.sum index c8ab99700..1b4ae8775 100644 --- a/go.sum +++ b/go.sum @@ -46,8 +46,6 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc= -github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= From 566f55d213ab603ba2847818de7e700882e07d53 Mon Sep 17 00:00:00 2001 From: jules01 Date: Tue, 19 Sep 2023 13:47:27 +0300 Subject: [PATCH 3/8] - added KeyValuePair data type --- data/types.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/data/types.go b/data/types.go index 52a6b0d7e..d6680afe3 100644 --- a/data/types.go +++ b/data/types.go @@ -5,3 +5,9 @@ type LogData struct { LogHandler TxHash string } + +// KeyValuePair is a tuple of (key, value) +type KeyValuePair struct { + Key []byte + Value []byte +} From b4a35a26ddd7255805e02bedac4fe5afbcea7d31 Mon Sep 17 00:00:00 2001 From: jules01 Date: Mon, 23 Oct 2023 18:05:17 +0300 Subject: [PATCH 4/8] - fixed linter issues in tests --- Makefile | 12 ++++++++++++ data/outport/common_test.go | 3 +-- data/transaction/transaction_test.go | 3 +-- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 2521da73c..6a2a70b5d 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,15 @@ test: @echo " > Running unit tests" go test -cover -race -coverprofile=coverage.txt -covermode=atomic -v ./... + +lint-install: +ifeq (,$(wildcard test -f bin/golangci-lint)) + @echo "Installing golint" + curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s +endif + +run-lint: + @echo "Running golint" + bin/golangci-lint run --max-issues-per-linter 0 --max-same-issues 0 --timeout=2m + +lint: lint-install run-lint \ No newline at end of file diff --git a/data/outport/common_test.go b/data/outport/common_test.go index 434a64e15..0fe18b65f 100644 --- a/data/outport/common_test.go +++ b/data/outport/common_test.go @@ -55,8 +55,7 @@ func TestGetBody(t *testing.T) { require.Nil(t, receivedBody) require.Equal(t, errNilBodyHandler, err) - var body data.BodyHandler - body = &block.Body{} + body := &block.Body{} receivedBody, err = GetBody(body) require.Nil(t, err) require.Equal(t, body, receivedBody) diff --git a/data/transaction/transaction_test.go b/data/transaction/transaction_test.go index 023066537..93151a32e 100644 --- a/data/transaction/transaction_test.go +++ b/data/transaction/transaction_test.go @@ -351,8 +351,7 @@ func TestTransaction_CheckIntegrityShouldErr(t *testing.T) { func TestTransaction_ImplementsGuardedTransactionHandler(t *testing.T) { t.Parallel() - var tx data.TransactionHandler - tx = &transaction.Transaction{} + var tx data.TransactionHandler = &transaction.Transaction{} _, ok := tx.(data.GuardedTransactionHandler) assert.True(t, ok) From 83c55f3e1f17f7b17f90f5b230a7dcbc298adbff Mon Sep 17 00:00:00 2001 From: jules01 Date: Mon, 23 Oct 2023 18:07:10 +0300 Subject: [PATCH 5/8] - added empty line --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 6a2a70b5d..d18369270 100644 --- a/Makefile +++ b/Makefile @@ -12,4 +12,4 @@ run-lint: @echo "Running golint" bin/golangci-lint run --max-issues-per-linter 0 --max-same-issues 0 --timeout=2m -lint: lint-install run-lint \ No newline at end of file +lint: lint-install run-lint From 1a5c79a232a9f4287a28b690320b83ee26a968a2 Mon Sep 17 00:00:00 2001 From: jules01 Date: Mon, 23 Oct 2023 18:08:46 +0300 Subject: [PATCH 6/8] - changed test --- data/outport/common_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data/outport/common_test.go b/data/outport/common_test.go index 0fe18b65f..555657ca1 100644 --- a/data/outport/common_test.go +++ b/data/outport/common_test.go @@ -55,7 +55,7 @@ func TestGetBody(t *testing.T) { require.Nil(t, receivedBody) require.Equal(t, errNilBodyHandler, err) - body := &block.Body{} + var body data.BodyHandler = &block.Body{} receivedBody, err = GetBody(body) require.Nil(t, err) require.Equal(t, body, receivedBody) From 19ac7022353e4fdec34d2cb4f1a0a402f5972c1a Mon Sep 17 00:00:00 2001 From: jules01 Date: Wed, 25 Oct 2023 10:19:46 +0300 Subject: [PATCH 7/8] - removed unused, wrongly placed, stubs & mocks --- data/mock/cacherMock.go | 156 --------------------- data/mock/memDbMock.go | 124 ---------------- data/mock/multipleShardsCoordinatorMock.go | 59 -------- data/mock/requestHandlerStub.go | 120 ---------------- data/mock/shardCoordinatorMock.go | 50 ------- data/mock/storerStub.go | 137 ------------------ 6 files changed, 646 deletions(-) delete mode 100644 data/mock/cacherMock.go delete mode 100644 data/mock/memDbMock.go delete mode 100644 data/mock/multipleShardsCoordinatorMock.go delete mode 100644 data/mock/requestHandlerStub.go delete mode 100644 data/mock/shardCoordinatorMock.go delete mode 100644 data/mock/storerStub.go diff --git a/data/mock/cacherMock.go b/data/mock/cacherMock.go deleted file mode 100644 index 20a5186c5..000000000 --- a/data/mock/cacherMock.go +++ /dev/null @@ -1,156 +0,0 @@ -package mock - -import ( - "sync" -) - -// CacherMock - -type CacherMock struct { - mut sync.RWMutex - dataMap map[string]interface{} - mutAddedDataHandlers sync.RWMutex - addedDataHandlers []func(key []byte, val interface{}) -} - -// NewCacherMock - -func NewCacherMock() *CacherMock { - return &CacherMock{ - dataMap: make(map[string]interface{}), - addedDataHandlers: make([]func(key []byte, val interface{}), 0), - } -} - -// Clear - -func (cacher *CacherMock) Clear() { - cacher.mut.Lock() - defer cacher.mut.Unlock() - - cacher.dataMap = make(map[string]interface{}) -} - -// Put - -func (cacher *CacherMock) Put(key []byte, value interface{}, _ int) (evicted bool) { - cacher.mut.Lock() - defer cacher.mut.Unlock() - - cacher.dataMap[string(key)] = value - cacher.callAddedDataHandlers(key, value) - - return false -} - -func (cacher *CacherMock) callAddedDataHandlers(key []byte, val interface{}) { - cacher.mutAddedDataHandlers.RLock() - for _, handler := range cacher.addedDataHandlers { - go handler(key, val) - } - cacher.mutAddedDataHandlers.RUnlock() -} - -// Get - -func (cacher *CacherMock) Get(key []byte) (value interface{}, ok bool) { - cacher.mut.RLock() - defer cacher.mut.RUnlock() - - val, ok := cacher.dataMap[string(key)] - - return val, ok -} - -// Has - -func (cacher *CacherMock) Has(key []byte) bool { - cacher.mut.RLock() - defer cacher.mut.RUnlock() - - _, ok := cacher.dataMap[string(key)] - - return ok -} - -// Peek - -func (cacher *CacherMock) Peek(key []byte) (value interface{}, ok bool) { - cacher.mut.RLock() - defer cacher.mut.RUnlock() - - val, ok := cacher.dataMap[string(key)] - - return val, ok -} - -// HasOrAdd - -func (cacher *CacherMock) HasOrAdd(key []byte, value interface{}, _ int) (has, added bool) { - cacher.mut.Lock() - defer cacher.mut.Unlock() - - _, has = cacher.dataMap[string(key)] - if has { - return true, false - } - - cacher.dataMap[string(key)] = value - cacher.callAddedDataHandlers(key, value) - return false, true -} - -// Remove - -func (cacher *CacherMock) Remove(key []byte) { - cacher.mut.Lock() - defer cacher.mut.Unlock() - - delete(cacher.dataMap, string(key)) -} - -// Keys - -func (cacher *CacherMock) Keys() [][]byte { - keys := make([][]byte, len(cacher.dataMap)) - idx := 0 - for k := range cacher.dataMap { - keys[idx] = []byte(k) - idx++ - } - - return keys -} - -// Len - -func (cacher *CacherMock) Len() int { - cacher.mut.RLock() - defer cacher.mut.RUnlock() - - return len(cacher.dataMap) -} - -// SizeInBytesContained - -func (cacher *CacherMock) SizeInBytesContained() uint64 { - return 0 -} - -// MaxSize - -func (cacher *CacherMock) MaxSize() int { - return 10000 -} - -// RegisterHandler - -func (cacher *CacherMock) RegisterHandler(handler func(key []byte, value interface{}), _ string) { - if handler == nil { - return - } - - cacher.mutAddedDataHandlers.Lock() - cacher.addedDataHandlers = append(cacher.addedDataHandlers, handler) - cacher.mutAddedDataHandlers.Unlock() -} - -// UnRegisterHandler - -func (cacher *CacherMock) UnRegisterHandler(string) { -} - -// IsInterfaceNil returns true if there is no value under the interface -func (cacher *CacherMock) IsInterfaceNil() bool { - return cacher == nil -} - -// Close - -func (cacher *CacherMock) Close() error { - return nil -} diff --git a/data/mock/memDbMock.go b/data/mock/memDbMock.go deleted file mode 100644 index 6e6de2c9e..000000000 --- a/data/mock/memDbMock.go +++ /dev/null @@ -1,124 +0,0 @@ -package mock - -import ( - "encoding/base64" - "errors" - "fmt" - "sync" -) - -// MemDbMock represents the memory database storage. It holds a map of key value pairs -// and a mutex to handle concurrent accesses to the map -type MemDbMock struct { - db map[string][]byte - mutx sync.RWMutex - PutCalled func(key, val []byte) error -} - -// NewMemDbMock creates a new memorydb object -func NewMemDbMock() *MemDbMock { - return &MemDbMock{ - db: make(map[string][]byte), - mutx: sync.RWMutex{}, - } -} - -// Put adds the value to the (key, val) storage medium -func (s *MemDbMock) Put(key, val []byte) error { - s.mutx.Lock() - defer s.mutx.Unlock() - - s.db[string(key)] = val - - if s.PutCalled != nil { - return s.PutCalled(key, val) - } - - return nil -} - -// Get gets the value associated to the key, or reports an error -func (s *MemDbMock) Get(key []byte) ([]byte, error) { - s.mutx.RLock() - defer s.mutx.RUnlock() - - val, ok := s.db[string(key)] - - if !ok { - return nil, fmt.Errorf("key: %s not found", base64.StdEncoding.EncodeToString(key)) - } - - return val, nil -} - -// Has returns true if the given key is present in the persistence medium, false otherwise -func (s *MemDbMock) Has(key []byte) error { - s.mutx.RLock() - defer s.mutx.RUnlock() - - _, ok := s.db[string(key)] - if !ok { - return errors.New("key not present") - } - - return nil -} - -// Init initializes the storage medium and prepares it for usage -func (s *MemDbMock) Init() error { - // no special initialization needed - return nil -} - -// Close closes the files/resources associated to the storage medium -func (s *MemDbMock) Close() error { - // nothing to do - return nil -} - -// Remove removes the data associated to the given key -func (s *MemDbMock) Remove(key []byte) error { - s.mutx.Lock() - defer s.mutx.Unlock() - - delete(s.db, string(key)) - - return nil -} - -// Destroy removes the storage medium stored data -func (s *MemDbMock) Destroy() error { - s.mutx.Lock() - defer s.mutx.Unlock() - - s.db = make(map[string][]byte) - - return nil -} - -// DestroyClosed removes the already closed storage medium stored data -func (s *MemDbMock) DestroyClosed() error { - return nil -} - -// RangeKeys will iterate over all contained (key, value) pairs calling the handler for each pair -func (s *MemDbMock) RangeKeys(handler func(key []byte, value []byte) bool) { - if handler == nil { - return - } - - s.mutx.RLock() - defer s.mutx.RUnlock() - - for k, v := range s.db { - shouldContinue := handler([]byte(k), v) - if !shouldContinue { - return - } - } -} - -// IsInterfaceNil returns true if there is no value under the interface -func (s *MemDbMock) IsInterfaceNil() bool { - return s == nil -} diff --git a/data/mock/multipleShardsCoordinatorMock.go b/data/mock/multipleShardsCoordinatorMock.go deleted file mode 100644 index c0c1f5aa8..000000000 --- a/data/mock/multipleShardsCoordinatorMock.go +++ /dev/null @@ -1,59 +0,0 @@ -package mock - -import ( - "fmt" -) - -// MultipleShardsCoordinatorMock - -type MultipleShardsCoordinatorMock struct { - ComputeIdCalled func(address []byte) uint32 - NoShards uint32 - CurrentShard uint32 -} - -// NewMultiShardsCoordinatorMock - -func NewMultiShardsCoordinatorMock(nrShard uint32) *MultipleShardsCoordinatorMock { - return &MultipleShardsCoordinatorMock{NoShards: nrShard} -} - -// NumberOfShards - -func (scm *MultipleShardsCoordinatorMock) NumberOfShards() uint32 { - return scm.NoShards -} - -// ComputeId - -func (scm *MultipleShardsCoordinatorMock) ComputeId(address []byte) uint32 { - if scm.ComputeIdCalled == nil { - return scm.SelfId() - } - return scm.ComputeIdCalled(address) -} - -// SelfId - -func (scm *MultipleShardsCoordinatorMock) SelfId() uint32 { - return scm.CurrentShard -} - -// SameShard - -func (scm *MultipleShardsCoordinatorMock) SameShard(_, _ []byte) bool { - return true -} - -// CommunicationIdentifier returns the identifier between current shard ID and destination shard ID -// identifier is generated such as the first shard from identifier is always smaller than the last -func (scm *MultipleShardsCoordinatorMock) CommunicationIdentifier(destShardID uint32) string { - if destShardID == scm.CurrentShard { - return fmt.Sprintf("_%d", scm.CurrentShard) - } - - if destShardID < scm.CurrentShard { - return fmt.Sprintf("_%d_%d", destShardID, scm.CurrentShard) - } - - return fmt.Sprintf("_%d_%d", scm.CurrentShard, destShardID) -} - -// IsInterfaceNil returns true if there is no value under the interface -func (scm *MultipleShardsCoordinatorMock) IsInterfaceNil() bool { - return scm == nil -} diff --git a/data/mock/requestHandlerStub.go b/data/mock/requestHandlerStub.go deleted file mode 100644 index 268ce86a1..000000000 --- a/data/mock/requestHandlerStub.go +++ /dev/null @@ -1,120 +0,0 @@ -package mock - -import "time" - -// RequestHandlerStub - -type RequestHandlerStub struct { - RequestShardHeaderCalled func(shardID uint32, hash []byte) - RequestMetaHeaderCalled func(hash []byte) - RequestMetaHeaderByNonceCalled func(nonce uint64) - RequestShardHeaderByNonceCalled func(shardID uint32, nonce uint64) - RequestTransactionHandlerCalled func(destShardID uint32, txHashes [][]byte) - RequestScrHandlerCalled func(destShardID uint32, txHashes [][]byte) - RequestRewardTxHandlerCalled func(destShardID uint32, txHashes [][]byte) - RequestMiniBlockHandlerCalled func(destShardID uint32, miniblockHash []byte) - RequestMiniBlocksHandlerCalled func(destShardID uint32, miniblocksHashes [][]byte) - RequestTrieNodesCalled func(destShardID uint32, hashes [][]byte, topic string) - RequestStartOfEpochMetaBlockCalled func(epoch uint32) -} - -// RequestInterval - -func (rhs *RequestHandlerStub) RequestInterval() time.Duration { - return time.Second -} - -// RequestStartOfEpochMetaBlock - -func (rhs *RequestHandlerStub) RequestStartOfEpochMetaBlock(epoch uint32) { - if rhs.RequestStartOfEpochMetaBlockCalled == nil { - return - } - rhs.RequestStartOfEpochMetaBlockCalled(epoch) -} - -// SetEpoch - -func (rhs *RequestHandlerStub) SetEpoch(_ uint32) { -} - -// RequestShardHeader - -func (rhs *RequestHandlerStub) RequestShardHeader(shardID uint32, hash []byte) { - if rhs.RequestShardHeaderCalled == nil { - return - } - rhs.RequestShardHeaderCalled(shardID, hash) -} - -// RequestMetaHeader - -func (rhs *RequestHandlerStub) RequestMetaHeader(hash []byte) { - if rhs.RequestMetaHeaderCalled == nil { - return - } - rhs.RequestMetaHeaderCalled(hash) -} - -// RequestMetaHeaderByNonce - -func (rhs *RequestHandlerStub) RequestMetaHeaderByNonce(nonce uint64) { - if rhs.RequestMetaHeaderByNonceCalled == nil { - return - } - rhs.RequestMetaHeaderByNonceCalled(nonce) -} - -// RequestShardHeaderByNonce - -func (rhs *RequestHandlerStub) RequestShardHeaderByNonce(shardID uint32, nonce uint64) { - if rhs.RequestShardHeaderByNonceCalled == nil { - return - } - rhs.RequestShardHeaderByNonceCalled(shardID, nonce) -} - -// RequestTransaction - -func (rhs *RequestHandlerStub) RequestTransaction(destShardID uint32, txHashes [][]byte) { - if rhs.RequestTransactionHandlerCalled == nil { - return - } - rhs.RequestTransactionHandlerCalled(destShardID, txHashes) -} - -// RequestUnsignedTransactions - -func (rhs *RequestHandlerStub) RequestUnsignedTransactions(destShardID uint32, txHashes [][]byte) { - if rhs.RequestScrHandlerCalled == nil { - return - } - rhs.RequestScrHandlerCalled(destShardID, txHashes) -} - -// RequestRewardTransactions - -func (rhs *RequestHandlerStub) RequestRewardTransactions(destShardID uint32, txHashes [][]byte) { - if rhs.RequestRewardTxHandlerCalled == nil { - return - } - rhs.RequestRewardTxHandlerCalled(destShardID, txHashes) -} - -// RequestMiniBlock - -func (rhs *RequestHandlerStub) RequestMiniBlock(destShardID uint32, miniblockHash []byte) { - if rhs.RequestMiniBlockHandlerCalled == nil { - return - } - rhs.RequestMiniBlockHandlerCalled(destShardID, miniblockHash) -} - -// RequestMiniBlocks - -func (rhs *RequestHandlerStub) RequestMiniBlocks(destShardID uint32, miniblocksHashes [][]byte) { - if rhs.RequestMiniBlocksHandlerCalled == nil { - return - } - rhs.RequestMiniBlocksHandlerCalled(destShardID, miniblocksHashes) -} - -// RequestTrieNodes - -func (rhs *RequestHandlerStub) RequestTrieNodes(destShardID uint32, hashes [][]byte, topic string) { - if rhs.RequestTrieNodesCalled == nil { - return - } - rhs.RequestTrieNodesCalled(destShardID, hashes, topic) -} - -// IsInterfaceNil returns true if there is no value under the interface -func (rhs *RequestHandlerStub) IsInterfaceNil() bool { - return rhs == nil -} diff --git a/data/mock/shardCoordinatorMock.go b/data/mock/shardCoordinatorMock.go deleted file mode 100644 index ed6bd5539..000000000 --- a/data/mock/shardCoordinatorMock.go +++ /dev/null @@ -1,50 +0,0 @@ -package mock - -import ( - "github.com/multiversx/mx-chain-core-go/core" -) - -// ShardCoordinatorMock - -type ShardCoordinatorMock struct { - SelfID uint32 - NumOfShards uint32 -} - -// NumberOfShards - -func (scm *ShardCoordinatorMock) NumberOfShards() uint32 { - return scm.NumOfShards -} - -// ComputeId - -func (scm *ShardCoordinatorMock) ComputeId(_ []byte) uint32 { - panic("implement me") -} - -// SetSelfId - -func (scm *ShardCoordinatorMock) SetSelfId(_ uint32) error { - panic("implement me") -} - -// SelfId - -func (scm *ShardCoordinatorMock) SelfId() uint32 { - return scm.SelfID -} - -// SameShard - -func (scm *ShardCoordinatorMock) SameShard(_, _ []byte) bool { - return true -} - -// CommunicationIdentifier - -func (scm *ShardCoordinatorMock) CommunicationIdentifier(destShardID uint32) string { - if destShardID == core.MetachainShardId { - return "_0_META" - } - - return "_0" -} - -// IsInterfaceNil returns true if there is no value under the interface -func (scm *ShardCoordinatorMock) IsInterfaceNil() bool { - return scm == nil -} diff --git a/data/mock/storerStub.go b/data/mock/storerStub.go deleted file mode 100644 index fed47ae00..000000000 --- a/data/mock/storerStub.go +++ /dev/null @@ -1,137 +0,0 @@ -package mock - -// StorerStub - -type StorerStub struct { - PutCalled func(key, data []byte) error - GetCalled func(key []byte) ([]byte, error) - GetFromEpochCalled func(key []byte, epoch uint32) ([]byte, error) - GetBulkFromEpochCalled func(keys [][]byte, epoch uint32) (map[string][]byte, error) - HasCalled func(key []byte) error - HasInEpochCalled func(key []byte, epoch uint32) error - SearchFirstCalled func(key []byte) ([]byte, error) - RemoveCalled func(key []byte) error - ClearCacheCalled func() - DestroyUnitCalled func() error - RangeKeysCalled func(handler func(key []byte, val []byte) bool) - PutInEpochCalled func(key, data []byte, epoch uint32) error - GetOldestEpochCalled func() (uint32, error) - CloseCalled func() error -} - -// PutInEpoch - -func (ss *StorerStub) PutInEpoch(key, data []byte, epoch uint32) error { - if ss.PutInEpochCalled != nil { - return ss.PutInEpochCalled(key, data, epoch) - } - - return nil -} - -// GetFromEpoch - -func (ss *StorerStub) GetFromEpoch(key []byte, epoch uint32) ([]byte, error) { - if ss.GetFromEpochCalled != nil { - return ss.GetFromEpochCalled(key, epoch) - } - - return nil, nil -} - -// GetBulkFromEpoch - -func (ss *StorerStub) GetBulkFromEpoch(keys [][]byte, epoch uint32) (map[string][]byte, error) { - if ss.GetBulkFromEpochCalled != nil { - return ss.GetBulkFromEpochCalled(keys, epoch) - } - - return nil, nil -} - -// SearchFirst - -func (ss *StorerStub) SearchFirst(key []byte) ([]byte, error) { - if ss.SearchFirstCalled != nil { - return ss.SearchFirstCalled(key) - } - - return nil, nil -} - -// Close - -func (ss *StorerStub) Close() error { - if ss.CloseCalled != nil { - return ss.CloseCalled() - } - - return nil -} - -// Put - -func (ss *StorerStub) Put(key, data []byte) error { - if ss.PutCalled != nil { - return ss.PutCalled(key, data) - } - - return nil -} - -// Get - -func (ss *StorerStub) Get(key []byte) ([]byte, error) { - if ss.GetCalled != nil { - return ss.GetCalled(key) - } - - return nil, nil -} - -// Has - -func (ss *StorerStub) Has(key []byte) error { - if ss.HasCalled != nil { - return ss.HasCalled(key) - } - - return nil -} - -// Remove - -func (ss *StorerStub) Remove(key []byte) error { - if ss.RemoveCalled != nil { - return ss.RemoveCalled(key) - } - - return nil -} - -// ClearCache - -func (ss *StorerStub) ClearCache() { - if ss.ClearCacheCalled != nil { - ss.ClearCacheCalled() - } -} - -// DestroyUnit - -func (ss *StorerStub) DestroyUnit() error { - if ss.DestroyUnitCalled != nil { - return ss.DestroyUnitCalled() - } - - return nil -} - -// RangeKeys - -func (ss *StorerStub) RangeKeys(handler func(key []byte, val []byte) bool) { - if ss.RangeKeysCalled != nil { - ss.RangeKeysCalled(handler) - } -} - -// GetOldestEpoch - -func (ss *StorerStub) GetOldestEpoch() (uint32, error) { - if ss.GetOldestEpochCalled != nil { - return ss.GetOldestEpochCalled() - } - - return 0, nil -} - -// IsInterfaceNil returns true if there is no value under the interface -func (ss *StorerStub) IsInterfaceNil() bool { - return ss == nil -} From 1cb1d0d3285be392e7d01c2091965e39f3194f42 Mon Sep 17 00:00:00 2001 From: Bogdan Rosianu Date: Mon, 6 Nov 2023 16:52:36 +0200 Subject: [PATCH 8/8] remove 'omitempty' for the guarded api field --- data/api/guardianData.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data/api/guardianData.go b/data/api/guardianData.go index 135a36cb2..dda348abf 100644 --- a/data/api/guardianData.go +++ b/data/api/guardianData.go @@ -11,5 +11,5 @@ type Guardian struct { type GuardianData struct { ActiveGuardian *Guardian `json:"activeGuardian,omitempty"` PendingGuardian *Guardian `json:"pendingGuardian,omitempty"` - Guarded bool `json:"guarded,omitempty"` + Guarded bool `json:"guarded"` }