Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: check only dbs that are older than the given epoch #6789

Open
wants to merge 3 commits into
base: feat/chain-go-sdk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion node/chainSimulator/components/memoryComponents.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion node/chainSimulator/components/memoryComponents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
6 changes: 5 additions & 1 deletion storage/pruning/triePruningStorer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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()
Expand All @@ -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) {
Expand Down
19 changes: 10 additions & 9 deletions storage/pruning/triePruningStorer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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"))
Expand Down
2 changes: 1 addition & 1 deletion testscommon/snapshotPruningStorerMock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions testscommon/trie/snapshotPruningStorerStub.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions trie/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion trie/snapshotTrieStorageManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"github.com/multiversx/mx-chain-core-go/core"

"github.com/multiversx/mx-chain-go/common"
)

Expand Down Expand Up @@ -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
}
Expand Down
19 changes: 10 additions & 9 deletions trie/snapshotTrieStorageManager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
},
}
Expand All @@ -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
},
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion trie/trieStorageManager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down