diff --git a/node/chainSimulator/components/memoryComponents.go b/node/chainSimulator/components/memoryComponents.go index 639dafc753d..89ed5240def 100644 --- a/node/chainSimulator/components/memoryComponents.go +++ b/node/chainSimulator/components/memoryComponents.go @@ -2,6 +2,7 @@ package components import ( "github.com/multiversx/mx-chain-core-go/core" + "github.com/multiversx/mx-chain-go/storage" "github.com/multiversx/mx-chain-go/storage/database" "github.com/multiversx/mx-chain-go/storage/storageunit" @@ -35,7 +36,7 @@ func (store *trieStorage) SetEpochForPutOperation(_ uint32) { } // GetFromOldEpochsWithoutAddingToCache tries to get directly the key -func (store *trieStorage) GetFromOldEpochsWithoutAddingToCache(key []byte) ([]byte, core.OptionalUint32, error) { +func (store *trieStorage) GetFromOldEpochsWithoutAddingToCache(key []byte, _ uint32) ([]byte, core.OptionalUint32, error) { value, err := store.Get(key) return value, core.OptionalUint32{}, err diff --git a/node/chainSimulator/components/memoryComponents_test.go b/node/chainSimulator/components/memoryComponents_test.go index b393bca7d47..37bf0d4ba7e 100644 --- a/node/chainSimulator/components/memoryComponents_test.go +++ b/node/chainSimulator/components/memoryComponents_test.go @@ -22,7 +22,7 @@ func TestCreateMemUnitForTries(t *testing.T) { require.NoError(t, memUnit.PutInEpoch(key, data, 0)) require.NoError(t, memUnit.PutInEpochWithoutCache(key, data, 0)) - value, _, err := memUnit.GetFromOldEpochsWithoutAddingToCache(key) + value, _, err := memUnit.GetFromOldEpochsWithoutAddingToCache(key, 10) require.NoError(t, err) require.Equal(t, data, value) diff --git a/storage/pruning/triePruningStorer.go b/storage/pruning/triePruningStorer.go index e013820db65..5ad8906b1f6 100644 --- a/storage/pruning/triePruningStorer.go +++ b/storage/pruning/triePruningStorer.go @@ -7,6 +7,7 @@ import ( "fmt" "github.com/multiversx/mx-chain-core-go/core" + "github.com/multiversx/mx-chain-go/common" "github.com/multiversx/mx-chain-go/storage" ) @@ -92,7 +93,7 @@ func (ps *triePruningStorer) PutInEpochWithoutCache(key []byte, data []byte, epo } // GetFromOldEpochsWithoutAddingToCache searches the old epochs for the given key without adding to the cache -func (ps *triePruningStorer) GetFromOldEpochsWithoutAddingToCache(key []byte) ([]byte, core.OptionalUint32, error) { +func (ps *triePruningStorer) GetFromOldEpochsWithoutAddingToCache(key []byte, epoch uint32) ([]byte, core.OptionalUint32, error) { v, ok := ps.cacher.Get(key) if ok && !bytes.Equal([]byte(common.ActiveDBKey), key) { ps.stateStatsHandler.IncrementSnapshotCache() @@ -104,6 +105,9 @@ func (ps *triePruningStorer) GetFromOldEpochsWithoutAddingToCache(key []byte) ([ numClosedDbs := 0 for idx := 1; idx < len(ps.activePersisters); idx++ { + if ps.activePersisters[idx].epoch >= epoch { + continue + } val, err := ps.activePersisters[idx].persister.Get(key) if err != nil { if errors.Is(err, storage.ErrDBIsClosed) { diff --git a/storage/pruning/triePruningStorer_test.go b/storage/pruning/triePruningStorer_test.go index 28dc5c93f8e..db45d71e0bb 100644 --- a/storage/pruning/triePruningStorer_test.go +++ b/storage/pruning/triePruningStorer_test.go @@ -4,13 +4,14 @@ import ( "strings" "testing" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/multiversx/mx-chain-go/common" "github.com/multiversx/mx-chain-go/storage" "github.com/multiversx/mx-chain-go/storage/mock" "github.com/multiversx/mx-chain-go/storage/pruning" "github.com/multiversx/mx-chain-go/testscommon" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" ) func TestNewTriePruningStorer(t *testing.T) { @@ -63,13 +64,13 @@ func TestTriePruningStorer_GetFromOldEpochsWithoutCacheSearchesOnlyOldEpochsAndR assert.Nil(t, err) assert.Equal(t, 0, len(cacher.Keys())) - res, epoch, err := ps.GetFromOldEpochsWithoutAddingToCache(testKey1) + res, epoch, err := ps.GetFromOldEpochsWithoutAddingToCache(testKey1, 10) assert.Equal(t, testVal1, res) assert.Nil(t, err) assert.True(t, epoch.HasValue) assert.Equal(t, uint32(0), epoch.Value) - res, epoch, err = ps.GetFromOldEpochsWithoutAddingToCache(testKey2) + res, epoch, err = ps.GetFromOldEpochsWithoutAddingToCache(testKey2, 10) assert.Nil(t, res) assert.NotNil(t, err) assert.False(t, epoch.HasValue) @@ -94,7 +95,7 @@ func TestTriePruningStorer_GetFromOldEpochsWithCache(t *testing.T) { assert.Nil(t, err) ps.SetEpochForPutOperation(1) - res, epoch, err := ps.GetFromOldEpochsWithoutAddingToCache(testKey1) + res, epoch, err := ps.GetFromOldEpochsWithoutAddingToCache(testKey1, 10) assert.Equal(t, testVal1, res) assert.Nil(t, err) assert.False(t, epoch.HasValue) @@ -116,7 +117,7 @@ func TestTriePruningStorer_GetFromOldEpochsWithoutCacheLessActivePersisters(t *t assert.Equal(t, 1, ps.GetNumActivePersisters()) _ = ps.ChangeEpochSimple(1) - val, _, err := ps.GetFromOldEpochsWithoutAddingToCache(testKey1) + val, _, err := ps.GetFromOldEpochsWithoutAddingToCache(testKey1, 10) assert.Nil(t, err) assert.Equal(t, testVal1, val) } @@ -139,7 +140,7 @@ func TestTriePruningStorer_GetFromOldEpochsWithoutCacheMoreActivePersisters(t *t _ = ps.ChangeEpochSimple(2) _ = ps.ChangeEpochSimple(3) - val, _, err := ps.GetFromOldEpochsWithoutAddingToCache(testKey1) + val, _, err := ps.GetFromOldEpochsWithoutAddingToCache(testKey1, 10) assert.Nil(t, err) assert.Equal(t, testVal1, val) } @@ -175,7 +176,7 @@ func TestTriePruningStorer_GetFromOldEpochsWithoutCacheAllPersistersClosed(t *te _ = ps.ChangeEpochSimple(3) _ = ps.Close() - val, _, err := ps.GetFromOldEpochsWithoutAddingToCache([]byte("key")) + val, _, err := ps.GetFromOldEpochsWithoutAddingToCache([]byte("key"), 10) assert.Nil(t, val) assert.Equal(t, storage.ErrDBIsClosed, err) } @@ -198,7 +199,7 @@ func TestTriePruningStorer_GetFromOldEpochsWithoutCacheDoesNotSearchInCurrentSto assert.Nil(t, err) ps.ClearCache() - res, _, err := ps.GetFromOldEpochsWithoutAddingToCache(testKey1) + res, _, err := ps.GetFromOldEpochsWithoutAddingToCache(testKey1, 10) assert.Nil(t, res) assert.NotNil(t, err) assert.True(t, strings.Contains(err.Error(), "not found")) diff --git a/testscommon/snapshotPruningStorerMock.go b/testscommon/snapshotPruningStorerMock.go index 54dc1cba884..babe6160a25 100644 --- a/testscommon/snapshotPruningStorerMock.go +++ b/testscommon/snapshotPruningStorerMock.go @@ -15,7 +15,7 @@ func NewSnapshotPruningStorerMock() *SnapshotPruningStorerMock { } // GetFromOldEpochsWithoutAddingToCache - -func (spsm *SnapshotPruningStorerMock) GetFromOldEpochsWithoutAddingToCache(key []byte) ([]byte, core.OptionalUint32, error) { +func (spsm *SnapshotPruningStorerMock) GetFromOldEpochsWithoutAddingToCache(key []byte, _ uint32) ([]byte, core.OptionalUint32, error) { val, err := spsm.Get(key) return val, core.OptionalUint32{}, err diff --git a/testscommon/trie/snapshotPruningStorerStub.go b/testscommon/trie/snapshotPruningStorerStub.go index a31c795150d..54136498c07 100644 --- a/testscommon/trie/snapshotPruningStorerStub.go +++ b/testscommon/trie/snapshotPruningStorerStub.go @@ -2,13 +2,14 @@ package trie import ( "github.com/multiversx/mx-chain-core-go/core" + "github.com/multiversx/mx-chain-go/testscommon" ) // SnapshotPruningStorerStub - type SnapshotPruningStorerStub struct { *testscommon.MemDbMock - GetFromOldEpochsWithoutAddingToCacheCalled func(key []byte) ([]byte, core.OptionalUint32, error) + GetFromOldEpochsWithoutAddingToCacheCalled func(key []byte, epoch uint32) ([]byte, core.OptionalUint32, error) GetFromLastEpochCalled func(key []byte) ([]byte, error) GetFromCurrentEpochCalled func(key []byte) ([]byte, error) GetFromEpochCalled func(key []byte, epoch uint32) ([]byte, error) @@ -21,9 +22,9 @@ type SnapshotPruningStorerStub struct { } // GetFromOldEpochsWithoutAddingToCache - -func (spss *SnapshotPruningStorerStub) GetFromOldEpochsWithoutAddingToCache(key []byte) ([]byte, core.OptionalUint32, error) { +func (spss *SnapshotPruningStorerStub) GetFromOldEpochsWithoutAddingToCache(key []byte, epoch uint32) ([]byte, core.OptionalUint32, error) { if spss.GetFromOldEpochsWithoutAddingToCacheCalled != nil { - return spss.GetFromOldEpochsWithoutAddingToCacheCalled(key) + return spss.GetFromOldEpochsWithoutAddingToCacheCalled(key, epoch) } return nil, core.OptionalUint32{}, nil diff --git a/trie/interface.go b/trie/interface.go index 3bbc79119f2..8e87ed981ab 100644 --- a/trie/interface.go +++ b/trie/interface.go @@ -9,8 +9,9 @@ import ( "github.com/multiversx/mx-chain-core-go/core" "github.com/multiversx/mx-chain-core-go/hashing" "github.com/multiversx/mx-chain-core-go/marshal" - "github.com/multiversx/mx-chain-go/common" vmcommon "github.com/multiversx/mx-chain-vm-common-go" + + "github.com/multiversx/mx-chain-go/common" ) type node interface { @@ -88,7 +89,7 @@ type epochStorer interface { type snapshotPruningStorer interface { common.BaseStorer - GetFromOldEpochsWithoutAddingToCache(key []byte) ([]byte, core.OptionalUint32, error) + GetFromOldEpochsWithoutAddingToCache(key []byte, epoch uint32) ([]byte, core.OptionalUint32, error) GetFromLastEpoch(key []byte) ([]byte, error) PutInEpoch(key []byte, data []byte, epoch uint32) error PutInEpochWithoutCache(key []byte, data []byte, epoch uint32) error diff --git a/trie/snapshotTrieStorageManager.go b/trie/snapshotTrieStorageManager.go index 60835ab8926..9aefe0ac4e3 100644 --- a/trie/snapshotTrieStorageManager.go +++ b/trie/snapshotTrieStorageManager.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/multiversx/mx-chain-core-go/core" + "github.com/multiversx/mx-chain-go/common" ) @@ -39,7 +40,7 @@ func (stsm *snapshotTrieStorageManager) Get(key []byte) ([]byte, error) { // test point get during snapshot - val, epoch, err := stsm.mainSnapshotStorer.GetFromOldEpochsWithoutAddingToCache(key) + val, epoch, err := stsm.mainSnapshotStorer.GetFromOldEpochsWithoutAddingToCache(key, stsm.epoch) if core.IsClosingError(err) { return nil, err } diff --git a/trie/snapshotTrieStorageManager_test.go b/trie/snapshotTrieStorageManager_test.go index dd6f3662d8d..ca3bcd798c2 100644 --- a/trie/snapshotTrieStorageManager_test.go +++ b/trie/snapshotTrieStorageManager_test.go @@ -7,11 +7,12 @@ import ( "github.com/multiversx/mx-chain-core-go/core" "github.com/multiversx/mx-chain-core-go/core/check" + "github.com/stretchr/testify/assert" + "github.com/multiversx/mx-chain-go/common" "github.com/multiversx/mx-chain-go/storage" "github.com/multiversx/mx-chain-go/testscommon" "github.com/multiversx/mx-chain-go/testscommon/trie" - "github.com/stretchr/testify/assert" ) func TestNewSnapshotTrieStorageManagerInvalidStorerType(t *testing.T) { @@ -56,7 +57,7 @@ func TestSnapshotTrieStorageManager_Get(t *testing.T) { _, trieStorage := newEmptyTrie() trieStorage.mainStorer = &trie.SnapshotPruningStorerStub{ - GetFromOldEpochsWithoutAddingToCacheCalled: func(_ []byte) ([]byte, core.OptionalUint32, error) { + GetFromOldEpochsWithoutAddingToCacheCalled: func(_ []byte, _ uint32) ([]byte, core.OptionalUint32, error) { return nil, core.OptionalUint32{}, storage.ErrDBIsClosed }, } @@ -72,7 +73,7 @@ func TestSnapshotTrieStorageManager_Get(t *testing.T) { _, trieStorage := newEmptyTrie() getFromOldEpochsWithoutCacheCalled := false trieStorage.mainStorer = &trie.SnapshotPruningStorerStub{ - GetFromOldEpochsWithoutAddingToCacheCalled: func(_ []byte) ([]byte, core.OptionalUint32, error) { + GetFromOldEpochsWithoutAddingToCacheCalled: func(_ []byte, _ uint32) ([]byte, core.OptionalUint32, error) { getFromOldEpochsWithoutCacheCalled = true return nil, core.OptionalUint32{}, nil }, @@ -156,7 +157,7 @@ func TestSnapshotTrieStorageManager_AlsoAddInPreviousEpoch(t *testing.T) { val := []byte("val") _, trieStorage := newEmptyTrie() trieStorage.mainStorer = &trie.SnapshotPruningStorerStub{ - GetFromOldEpochsWithoutAddingToCacheCalled: func(_ []byte) ([]byte, core.OptionalUint32, error) { + GetFromOldEpochsWithoutAddingToCacheCalled: func(_ []byte, _ uint32) ([]byte, core.OptionalUint32, error) { return val, core.OptionalUint32{}, nil }, PutInEpochCalled: func(_ []byte, _ []byte, _ uint32) error { @@ -173,7 +174,7 @@ func TestSnapshotTrieStorageManager_AlsoAddInPreviousEpoch(t *testing.T) { val := []byte("val") _, trieStorage := newEmptyTrie() trieStorage.mainStorer = &trie.SnapshotPruningStorerStub{ - GetFromOldEpochsWithoutAddingToCacheCalled: func(_ []byte) ([]byte, core.OptionalUint32, error) { + GetFromOldEpochsWithoutAddingToCacheCalled: func(_ []byte, _ uint32) ([]byte, core.OptionalUint32, error) { epoch := core.OptionalUint32{ Value: 4, HasValue: true, @@ -194,7 +195,7 @@ func TestSnapshotTrieStorageManager_AlsoAddInPreviousEpoch(t *testing.T) { val := []byte("val") _, trieStorage := newEmptyTrie() trieStorage.mainStorer = &trie.SnapshotPruningStorerStub{ - GetFromOldEpochsWithoutAddingToCacheCalled: func(_ []byte) ([]byte, core.OptionalUint32, error) { + GetFromOldEpochsWithoutAddingToCacheCalled: func(_ []byte, _ uint32) ([]byte, core.OptionalUint32, error) { epoch := core.OptionalUint32{ Value: 4, HasValue: true, @@ -215,7 +216,7 @@ func TestSnapshotTrieStorageManager_AlsoAddInPreviousEpoch(t *testing.T) { val := []byte("val") _, trieStorage := newEmptyTrie() trieStorage.mainStorer = &trie.SnapshotPruningStorerStub{ - GetFromOldEpochsWithoutAddingToCacheCalled: func(_ []byte) ([]byte, core.OptionalUint32, error) { + GetFromOldEpochsWithoutAddingToCacheCalled: func(_ []byte, _ uint32) ([]byte, core.OptionalUint32, error) { epoch := core.OptionalUint32{ Value: 3, HasValue: true, @@ -236,7 +237,7 @@ func TestSnapshotTrieStorageManager_AlsoAddInPreviousEpoch(t *testing.T) { val := []byte("val") _, trieStorage := newEmptyTrie() trieStorage.mainStorer = &trie.SnapshotPruningStorerStub{ - GetFromOldEpochsWithoutAddingToCacheCalled: func(_ []byte) ([]byte, core.OptionalUint32, error) { + GetFromOldEpochsWithoutAddingToCacheCalled: func(_ []byte, _ uint32) ([]byte, core.OptionalUint32, error) { epoch := core.OptionalUint32{ Value: 3, HasValue: true, @@ -258,7 +259,7 @@ func TestSnapshotTrieStorageManager_AlsoAddInPreviousEpoch(t *testing.T) { putInEpochCalled := false _, trieStorage := newEmptyTrie() trieStorage.mainStorer = &trie.SnapshotPruningStorerStub{ - GetFromOldEpochsWithoutAddingToCacheCalled: func(_ []byte) ([]byte, core.OptionalUint32, error) { + GetFromOldEpochsWithoutAddingToCacheCalled: func(_ []byte, _ uint32) ([]byte, core.OptionalUint32, error) { epoch := core.OptionalUint32{ Value: 3, HasValue: true, diff --git a/trie/trieStorageManager_test.go b/trie/trieStorageManager_test.go index bba4dde29c7..670119c1b19 100644 --- a/trie/trieStorageManager_test.go +++ b/trie/trieStorageManager_test.go @@ -427,7 +427,7 @@ func TestTrieStorageManager_ShouldTakeSnapshot(t *testing.T) { GetFromCurrentEpochCalled: func(key []byte) ([]byte, error) { return nil, expectedErr // isTrieSynced returns false }, - GetFromOldEpochsWithoutAddingToCacheCalled: func(key []byte) ([]byte, core.OptionalUint32, error) { + GetFromOldEpochsWithoutAddingToCacheCalled: func(key []byte, _ uint32) ([]byte, core.OptionalUint32, error) { return []byte(common.ActiveDBVal), core.OptionalUint32{}, nil }, MemDbMock: testscommon.NewMemDbMock(),