Skip to content

Commit

Permalink
Merge branch 'rc/v1.6.0' into facade_tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sstanculeanu committed May 12, 2023
2 parents dd4c89a + fa09e17 commit cafae33
Show file tree
Hide file tree
Showing 18 changed files with 1,149 additions and 207 deletions.
9 changes: 9 additions & 0 deletions testscommon/trie/snapshotPruningStorerStub.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type SnapshotPruningStorerStub struct {
PutInEpochWithoutCacheCalled func(key []byte, data []byte, epoch uint32) error
GetLatestStorageEpochCalled func() (uint32, error)
RemoveFromCurrentEpochCalled func(key []byte) error
CloseCalled func() error
}

// GetFromOldEpochsWithoutAddingToCache -
Expand Down Expand Up @@ -88,3 +89,11 @@ func (spss *SnapshotPruningStorerStub) RemoveFromCurrentEpoch(key []byte) error
}
return spss.Remove(key)
}

// Close -
func (spss *SnapshotPruningStorerStub) Close() error {
if spss.CloseCalled != nil {
return spss.CloseCalled()
}
return nil
}
21 changes: 21 additions & 0 deletions trie/dfsIterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,27 @@ import (
"github.com/stretchr/testify/assert"
)

func TestNewDFSIterator(t *testing.T) {
t.Parallel()

t.Run("nil trie should error", func(t *testing.T) {
t.Parallel()

it, err := trie.NewDFSIterator(nil)
assert.Equal(t, trie.ErrNilTrie, err)
assert.Nil(t, it)
})
t.Run("should work", func(t *testing.T) {
t.Parallel()

tr := initTrie()

it, err := trie.NewDFSIterator(tr)
assert.Nil(t, err)
assert.NotNil(t, it)
})
}

func TestDFSIterator_Next(t *testing.T) {
t.Parallel()

Expand Down
38 changes: 37 additions & 1 deletion trie/factory/trieCreator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,49 @@ func TestTrieCreator_CreateWithNilCheckpointsStorerShouldErr(t *testing.T) {
require.True(t, strings.Contains(err.Error(), trie.ErrNilStorer.Error()))
}

func TestTrieCreator_CreateTriesComponentsForShardIdMissingStorer(t *testing.T) {
func TestTrieCreator_CreateWithInvalidMaxTrieLevelInMemShouldErr(t *testing.T) {
t.Parallel()

args := getArgs()
tf, _ := factory.NewTrieFactory(args)

createArgs := getCreateArgs()
createArgs.MaxTrieLevelInMem = 0
_, tr, err := tf.Create(createArgs)
require.Nil(t, tr)
require.NotNil(t, err)
require.Contains(t, err.Error(), trie.ErrInvalidLevelValue.Error())
}

func TestTrieCreator_CreateTriesComponentsForShardId(t *testing.T) {
t.Parallel()

t.Run("missing UserAccountsUnit", testWithMissingStorer(dataRetriever.UserAccountsUnit))
t.Run("missing UserAccountsCheckpointsUnit", testWithMissingStorer(dataRetriever.UserAccountsCheckpointsUnit))
t.Run("missing PeerAccountsUnit", testWithMissingStorer(dataRetriever.PeerAccountsUnit))
t.Run("missing PeerAccountsCheckpointsUnit", testWithMissingStorer(dataRetriever.PeerAccountsCheckpointsUnit))
t.Run("should work", func(t *testing.T) {
t.Parallel()

holder, storageManager, err := factory.CreateTriesComponentsForShardId(
false,
testscommon.GetGeneralConfig(),
&mock.CoreComponentsStub{
InternalMarshalizerField: &testscommon.MarshalizerMock{},
HasherField: &hashingMocks.HasherMock{},
PathHandlerField: &testscommon.PathManagerStub{},
ProcessStatusHandlerInternal: &testscommon.ProcessStatusHandlerStub{},
},
&storageStubs.ChainStorerStub{
GetStorerCalled: func(unitType dataRetriever.UnitType) (storage.Storer, error) {
return &storageStubs.StorerStub{}, nil
},
},
)
require.NotNil(t, holder)
require.NotNil(t, storageManager)
require.Nil(t, err)
})
}

func testWithMissingStorer(missingUnit dataRetriever.UnitType) func(t *testing.T) {
Expand Down
31 changes: 31 additions & 0 deletions trie/keyBuilder/disabledKeyBuilder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package keyBuilder

import (
"bytes"
"testing"

"github.com/stretchr/testify/require"
)

func TestDisabledKeyBuilder(t *testing.T) {
t.Parallel()

defer func() {
r := recover()
if r != nil {
require.Fail(t, "should have not panicked")
}
}()

builder := NewDisabledKeyBuilder()
require.NotNil(t, builder)

builder.BuildKey([]byte("key"))

key, err := builder.GetKey()
require.Nil(t, err)
require.True(t, bytes.Equal(key, []byte{}))

clonedBuilder := builder.Clone()
require.Equal(t, &disabledKeyBuilder{}, clonedBuilder)
}
145 changes: 105 additions & 40 deletions trie/snapshotTrieStorageManager_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package trie

import (
"errors"
"strings"
"testing"

"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/core/check"
"github.com/multiversx/mx-chain-go/common"
errorsMx "github.com/multiversx/mx-chain-go/errors"
"github.com/multiversx/mx-chain-go/storage"
"github.com/multiversx/mx-chain-go/testscommon/trie"
"github.com/stretchr/testify/assert"
)
Expand All @@ -31,55 +34,117 @@ func TestNewSnapshotTrieStorageManager(t *testing.T) {
assert.False(t, check.IfNil(stsm))
}

func TestNewSnapshotTrieStorageManager_GetFromOldEpochsWithoutCache(t *testing.T) {
func TestSnapshotTrieStorageManager_Get(t *testing.T) {
t.Parallel()

_, trieStorage := newEmptyTrie()
getFromOldEpochsWithoutCacheCalled := false
trieStorage.mainStorer = &trie.SnapshotPruningStorerStub{
GetFromOldEpochsWithoutAddingToCacheCalled: func(_ []byte) ([]byte, core.OptionalUint32, error) {
getFromOldEpochsWithoutCacheCalled = true
return nil, core.OptionalUint32{}, nil
},
}
stsm, _ := newSnapshotTrieStorageManager(trieStorage, 0)

_, _ = stsm.Get([]byte("key"))
assert.True(t, getFromOldEpochsWithoutCacheCalled)
t.Run("closed storage manager should error", func(t *testing.T) {
t.Parallel()

_, trieStorage := newEmptyTrie()
trieStorage.mainStorer = &trie.SnapshotPruningStorerStub{}
stsm, _ := newSnapshotTrieStorageManager(trieStorage, 0)
_ = stsm.Close()

val, err := stsm.Get([]byte("key"))
assert.Equal(t, errorsMx.ErrContextClosing, err)
assert.Nil(t, val)
})
t.Run("GetFromOldEpochsWithoutAddingToCache returns db closed should error", func(t *testing.T) {
t.Parallel()

_, trieStorage := newEmptyTrie()
trieStorage.mainStorer = &trie.SnapshotPruningStorerStub{
GetFromOldEpochsWithoutAddingToCacheCalled: func(_ []byte) ([]byte, core.OptionalUint32, error) {
return nil, core.OptionalUint32{}, storage.ErrDBIsClosed
},
}
stsm, _ := newSnapshotTrieStorageManager(trieStorage, 0)

val, err := stsm.Get([]byte("key"))
assert.Equal(t, storage.ErrDBIsClosed, err)
assert.Nil(t, val)
})
t.Run("should work from old epochs without cache", func(t *testing.T) {
t.Parallel()

_, trieStorage := newEmptyTrie()
getFromOldEpochsWithoutCacheCalled := false
trieStorage.mainStorer = &trie.SnapshotPruningStorerStub{
GetFromOldEpochsWithoutAddingToCacheCalled: func(_ []byte) ([]byte, core.OptionalUint32, error) {
getFromOldEpochsWithoutCacheCalled = true
return nil, core.OptionalUint32{}, nil
},
}
stsm, _ := newSnapshotTrieStorageManager(trieStorage, 0)

_, _ = stsm.Get([]byte("key"))
assert.True(t, getFromOldEpochsWithoutCacheCalled)
})
}

func TestNewSnapshotTrieStorageManager_PutWithoutCache(t *testing.T) {
func TestSnapshotTrieStorageManager_Put(t *testing.T) {
t.Parallel()

_, trieStorage := newEmptyTrie()
putWithoutCacheCalled := false
trieStorage.mainStorer = &trie.SnapshotPruningStorerStub{
PutInEpochWithoutCacheCalled: func(_ []byte, _ []byte, _ uint32) error {
putWithoutCacheCalled = true
return nil
},
}
stsm, _ := newSnapshotTrieStorageManager(trieStorage, 0)

_ = stsm.Put([]byte("key"), []byte("data"))
assert.True(t, putWithoutCacheCalled)
t.Run("closed storage manager should error", func(t *testing.T) {
t.Parallel()

_, trieStorage := newEmptyTrie()
trieStorage.mainStorer = &trie.SnapshotPruningStorerStub{}
stsm, _ := newSnapshotTrieStorageManager(trieStorage, 0)
_ = stsm.Close()

err := stsm.Put([]byte("key"), []byte("data"))
assert.Equal(t, errorsMx.ErrContextClosing, err)
})
t.Run("should work", func(t *testing.T) {
t.Parallel()

_, trieStorage := newEmptyTrie()
putWithoutCacheCalled := false
trieStorage.mainStorer = &trie.SnapshotPruningStorerStub{
PutInEpochWithoutCacheCalled: func(_ []byte, _ []byte, _ uint32) error {
putWithoutCacheCalled = true
return nil
},
}
stsm, _ := newSnapshotTrieStorageManager(trieStorage, 0)

_ = stsm.Put([]byte("key"), []byte("data"))
assert.True(t, putWithoutCacheCalled)
})
}

func TestNewSnapshotTrieStorageManager_GetFromLastEpoch(t *testing.T) {
func TestSnapshotTrieStorageManager_GetFromLastEpoch(t *testing.T) {
t.Parallel()

_, trieStorage := newEmptyTrie()
getFromLastEpochCalled := false
trieStorage.mainStorer = &trie.SnapshotPruningStorerStub{
GetFromLastEpochCalled: func(_ []byte) ([]byte, error) {
getFromLastEpochCalled = true
return nil, nil
},
}
stsm, _ := newSnapshotTrieStorageManager(trieStorage, 0)

_, _ = stsm.GetFromLastEpoch([]byte("key"))
assert.True(t, getFromLastEpochCalled)
t.Run("closed storage manager should error", func(t *testing.T) {
t.Parallel()

_, trieStorage := newEmptyTrie()
trieStorage.mainStorer = &trie.SnapshotPruningStorerStub{}
stsm, _ := newSnapshotTrieStorageManager(trieStorage, 0)
_ = stsm.Close()

val, err := stsm.GetFromLastEpoch([]byte("key"))
assert.Equal(t, errorsMx.ErrContextClosing, err)
assert.Nil(t, val)
})
t.Run("should work", func(t *testing.T) {
t.Parallel()

_, trieStorage := newEmptyTrie()
getFromLastEpochCalled := false
trieStorage.mainStorer = &trie.SnapshotPruningStorerStub{
GetFromLastEpochCalled: func(_ []byte) ([]byte, error) {
getFromLastEpochCalled = true
return nil, nil
},
}
stsm, _ := newSnapshotTrieStorageManager(trieStorage, 0)

_, _ = stsm.GetFromLastEpoch([]byte("key"))
assert.True(t, getFromLastEpochCalled)
})
}

func TestSnapshotTrieStorageManager_AlsoAddInPreviousEpoch(t *testing.T) {
Expand Down Expand Up @@ -200,7 +265,7 @@ func TestSnapshotTrieStorageManager_AlsoAddInPreviousEpoch(t *testing.T) {
},
PutInEpochCalled: func(_ []byte, _ []byte, _ uint32) error {
putInEpochCalled = true
return nil
return errors.New("error for coverage only")
},
}
stsm, _ := newSnapshotTrieStorageManager(trieStorage, 5)
Expand Down
6 changes: 5 additions & 1 deletion trie/statistics/trieStatisticsCollector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import (
"github.com/stretchr/testify/assert"
)

func TestSnapshotStatistics_AddTrieStats(t *testing.T) {
func TestSnapshotStatistics_Add(t *testing.T) {
t.Parallel()

tsc := NewTrieStatisticsCollector()

tsc.Add(nil) // coverage, early exit

numInserts := 100
for i := 0; i < numInserts; i++ {
tsc.Add(getTrieStatsDTO(rand.Intn(numInserts), uint64(rand.Intn(numInserts))))
Expand Down Expand Up @@ -43,6 +45,8 @@ func TestSnapshotStatistics_AddTrieStats(t *testing.T) {
assert.Equal(t, numTriesToPrint, len(tsc.triesBySize))
assert.Equal(t, numTriesToPrint, len(tsc.triesByDepth))
assert.Equal(t, uint64(i+1), tsc.GetNumNodes())

tsc.Print() // coverage
}
}

Expand Down
40 changes: 40 additions & 0 deletions trie/statistics/trieStatistics_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package statistics

import (
"encoding/hex"
"fmt"
"testing"

"github.com/multiversx/mx-chain-core-go/core"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestTrieStatistics_AddBranchNode(t *testing.T) {
Expand Down Expand Up @@ -107,3 +111,39 @@ func TestTrieStatistics_GetTrieStats(t *testing.T) {
assert.Equal(t, uint64(numExtensions), stats.NumExtensionNodes)
assert.Equal(t, uint64(numLeaves), stats.NumLeafNodes)
}

func TestTrieStatsDTO_ToString(t *testing.T) {
t.Parallel()

tsd := TrieStatsDTO{
Address: "address",
RootHash: []byte("root hash"),
TotalNodesSize: 1,
TotalNumNodes: 1,
MaxTrieDepth: 1,
BranchNodesSize: 1,
NumBranchNodes: 1,
ExtensionNodesSize: 1,
NumExtensionNodes: 1,
LeafNodesSize: 1,
NumLeafNodes: 1,
}

expectedLines := []string{
fmt.Sprintf("address %v,", tsd.Address),
fmt.Sprintf("rootHash %v,", hex.EncodeToString(tsd.RootHash)),
fmt.Sprintf("total trie size = %v,", core.ConvertBytes(tsd.TotalNodesSize)),
fmt.Sprintf("num trie nodes = %v,", tsd.TotalNumNodes),
fmt.Sprintf("max trie depth = %v,", tsd.MaxTrieDepth),
fmt.Sprintf("branch nodes size %v,", core.ConvertBytes(tsd.BranchNodesSize)),
fmt.Sprintf("extension nodes size %v,", core.ConvertBytes(tsd.ExtensionNodesSize)),
fmt.Sprintf("leaf nodes size %v,", core.ConvertBytes(tsd.LeafNodesSize)),
fmt.Sprintf("num branches %v,", tsd.NumBranchNodes),
fmt.Sprintf("num extensions %v,", tsd.NumExtensionNodes),
fmt.Sprintf("num leaves %v", tsd.NumLeafNodes),
}
stringDTO := tsd.ToString()
for i, line := range stringDTO {
require.Equal(t, expectedLines[i], line)
}
}

0 comments on commit cafae33

Please sign in to comment.