Skip to content

Commit

Permalink
FEAT: testCleanupNotarizedStorageForHigherNoncesIfExist + refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusmihaic committed Oct 25, 2024
1 parent bb91d78 commit 1e0b915
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 53 deletions.
6 changes: 1 addition & 5 deletions factory/runType/runTypeComponents.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,7 @@ func (rcf *runTypeComponentsFactory) Create() (*runTypeComponents, error) {
}

epochStartBootstrapperFactory := bootstrap.NewEpochStartBootstrapperFactory()

bootstrapperFromStorageFactory, err := storageBootstrap.NewShardStorageBootstrapperFactory()
if err != nil {
return nil, fmt.Errorf("runTypeComponentsFactory - NewShardStorageBootstrapperFactory failed: %w", err)
}
bootstrapperFromStorageFactory := storageBootstrap.NewShardStorageBootstrapperFactory()

shardBootstrapFactory, err := storageBootstrap.NewShardBootstrapFactory()
if err != nil {
Expand Down
5 changes: 1 addition & 4 deletions factory/runType/sovereignRunTypeComponents.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,7 @@ func (rcf *sovereignRunTypeComponentsFactory) Create() (*runTypeComponents, erro
return nil, fmt.Errorf("sovereignRunTypeComponentsFactory - NewSovereignEpochStartBootstrapperFactory failed: %w", err)
}

bootstrapperFromStorageFactory, err := storageBootstrap.NewSovereignShardStorageBootstrapperFactory(rtc.bootstrapperFromStorageCreator)
if err != nil {
return nil, fmt.Errorf("sovereignRunTypeComponentsFactory - NewSovereignShardStorageBootstrapperFactory failed: %w", err)
}
bootstrapperFromStorageFactory := storageBootstrap.NewSovereignShardStorageBootstrapperFactory()

bootstrapperFactory, err := storageBootstrap.NewSovereignShardBootstrapFactory(rtc.bootstrapperCreator)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions process/sync/storageBootstrap/shardStorageBootstrapFactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ type shardStorageBootstrapperFactory struct {
}

// NewShardStorageBootstrapperFactory creates a new instance of shardStorageBootstrapperFactory for run type normal
func NewShardStorageBootstrapperFactory() (*shardStorageBootstrapperFactory, error) {
return &shardStorageBootstrapperFactory{}, nil
func NewShardStorageBootstrapperFactory() *shardStorageBootstrapperFactory {
return &shardStorageBootstrapperFactory{}
}

// CreateBootstrapperFromStorage creates a new instance of shardStorageBootstrapperFactory for run type normal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@ import (
func TestNewShardStorageBootstrapperFactory(t *testing.T) {
t.Parallel()

sbf, err := NewShardStorageBootstrapperFactory()

sbf := NewShardStorageBootstrapperFactory()
require.NotNil(t, sbf)
require.Nil(t, err)
}

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

sbf, _ := NewShardStorageBootstrapperFactory()
sbf := NewShardStorageBootstrapperFactory()
bootStrapper, err := sbf.CreateBootstrapperFromStorage(getDefaultArgShardBootstrapper())

require.NotNil(t, bootStrapper)
Expand All @@ -37,7 +35,7 @@ func TestShardStorageBootstrapperFactory_CreateShardStorageBootstrapper(t *testi
func TestShardStorageBootstrapperFactory_IsInterfaceNil(t *testing.T) {
t.Parallel()

sbf, _ := NewShardStorageBootstrapperFactory()
sbf := NewShardStorageBootstrapperFactory()
require.False(t, sbf.IsInterfaceNil())

sbf = nil
Expand Down
42 changes: 30 additions & 12 deletions process/sync/storageBootstrap/shardStorageBootstrapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,22 @@ func TestShardStorageBootstrapper_LoadFromStorageShouldWork(t *testing.T) {
assert.True(t, wasCalledEpochNotifier)
}

func TestShardStorageBootstrapper_CleanupNotarizedStorageForHigherNoncesIfExist(t *testing.T) {
func TestShardStorageBootstrapper_cleanupNotarizedStorageForHigherNoncesIfExist(t *testing.T) {
testCleanupNotarizedStorageForHigherNoncesIfExist(t, core.MetachainShardId, &block.MetaBlock{}, NewShardStorageBootstrapperFactory())
}

type bootStrapCleaner interface {
cleanupNotarizedStorageForHigherNoncesIfExist(
crossNotarizedHeaders []bootstrapStorage.BootstrapHeaderInfo,
)
}

func testCleanupNotarizedStorageForHigherNoncesIfExist(
t *testing.T,
shardID uint32,
header data.HeaderHandler,
factory BootstrapperFromStorageCreator,
) {
baseArgs := createMockShardStorageBoostrapperArgs()

bForceError := true
Expand All @@ -163,8 +178,8 @@ func TestShardStorageBootstrapper_CleanupNotarizedStorageForHigherNoncesIfExist(
metaHash := []byte("meta_hash")

metaNonceToDelete := metaNonce + maxNumOfConsecutiveNoncesNotFoundAccepted + 2
metaBlock := &block.MetaBlock{Nonce: metaNonceToDelete}
marshalledMetaBlock, _ := baseArgs.Marshalizer.Marshal(metaBlock)
_ = header.SetNonce(metaNonceToDelete)
marshalledMetaBlock, _ := baseArgs.Marshalizer.Marshal(header)

baseArgs.Uint64Converter = &mock.Uint64ByteSliceConverterMock{
ToByteSliceCalled: func(u uint64) []byte {
Expand Down Expand Up @@ -210,32 +225,35 @@ func TestShardStorageBootstrapper_CleanupNotarizedStorageForHigherNoncesIfExist(
args := ArgsShardStorageBootstrapper{
ArgsBaseStorageBootstrapper: baseArgs,
}
ssb, _ := NewShardStorageBootstrapper(args)
ssb, err := factory.CreateBootstrapperFromStorage(args)
require.Nil(t, err)

crossNotarizedHeaders := make([]bootstrapStorage.BootstrapHeaderInfo, 0)
ssbCleaner, castOk := ssb.(bootStrapCleaner)
require.True(t, castOk)

crossNotarizedHeaders := make([]bootstrapStorage.BootstrapHeaderInfo, 0)
crossNotarizedHeaders = append(crossNotarizedHeaders, bootstrapStorage.BootstrapHeaderInfo{ShardId: 0, Nonce: 1})
ssb.cleanupNotarizedStorageForHigherNoncesIfExist(crossNotarizedHeaders)
ssbCleaner.cleanupNotarizedStorageForHigherNoncesIfExist(crossNotarizedHeaders)
assert.Equal(t, 0, numCalled)

crossNotarizedHeaders = append(crossNotarizedHeaders, bootstrapStorage.BootstrapHeaderInfo{ShardId: core.MetachainShardId, Nonce: metaNonce})
ssb.cleanupNotarizedStorageForHigherNoncesIfExist(crossNotarizedHeaders)
crossNotarizedHeaders = append(crossNotarizedHeaders, bootstrapStorage.BootstrapHeaderInfo{ShardId: shardID, Nonce: metaNonce})
ssbCleaner.cleanupNotarizedStorageForHigherNoncesIfExist(crossNotarizedHeaders)
assert.Equal(t, 0, numCalled)
assert.Equal(t, maxNumOfConsecutiveNoncesNotFoundAccepted, numKeysNotFound-1)

numKeysNotFound = 0
metaNonceToDelete = metaNonce + maxNumOfConsecutiveNoncesNotFoundAccepted + 1
metaBlock = &block.MetaBlock{Nonce: metaNonceToDelete}
marshalledMetaBlock, _ = baseArgs.Marshalizer.Marshal(metaBlock)
_ = header.SetNonce(metaNonceToDelete)
marshalledMetaBlock, _ = baseArgs.Marshalizer.Marshal(header)

ssb.cleanupNotarizedStorageForHigherNoncesIfExist(crossNotarizedHeaders)
ssbCleaner.cleanupNotarizedStorageForHigherNoncesIfExist(crossNotarizedHeaders)
assert.Equal(t, 0, numCalled)
assert.Equal(t, maxNumOfConsecutiveNoncesNotFoundAccepted*2, numKeysNotFound-1)

numKeysNotFound = 0
bForceError = false

ssb.cleanupNotarizedStorageForHigherNoncesIfExist(crossNotarizedHeaders)
ssbCleaner.cleanupNotarizedStorageForHigherNoncesIfExist(crossNotarizedHeaders)
assert.Equal(t, 2, numCalled)
assert.Equal(t, maxNumOfConsecutiveNoncesNotFoundAccepted*2, numKeysNotFound-1)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,12 @@ func TestSovereignShardBootstrapFactory_applyCrossNotarizedHeaders(t *testing.T)
require.True(t, wasCrossChainHdrNotarized)
require.True(t, wasCrossChainHdrTracked)
}

func TestSovereignShardBootstrapFactory_cleanupNotarizedStorageForHigherNoncesIfExist(t *testing.T) {
extendedHeader := &block.ShardHeaderExtended{
Header: &block.HeaderV2{
Header: &block.Header{},
},
}
testCleanupNotarizedStorageForHigherNoncesIfExist(t, core.MainChainShardId, extendedHeader, NewSovereignShardStorageBootstrapperFactory())
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
package storageBootstrap

import (
"github.com/multiversx/mx-chain-core-go/core/check"
"github.com/multiversx/mx-chain-go/errors"
"github.com/multiversx/mx-chain-go/process"
)

type sovereignShardStorageBootstrapperFactory struct {
shardStorageBootstrapperFactory BootstrapperFromStorageCreator
}

// NewSovereignShardStorageBootstrapperFactory creates a new instance of shardStorageBootstrapperFactory for run type sovereign
func NewSovereignShardStorageBootstrapperFactory(ssb BootstrapperFromStorageCreator) (*sovereignShardStorageBootstrapperFactory, error) {
if check.IfNil(ssb) {
return nil, errors.ErrNilShardStorageBootstrapperFactory
}

return &sovereignShardStorageBootstrapperFactory{
shardStorageBootstrapperFactory: ssb,
}, nil
func NewSovereignShardStorageBootstrapperFactory() *sovereignShardStorageBootstrapperFactory {
return &sovereignShardStorageBootstrapperFactory{}
}

// CreateBootstrapperFromStorage creates a new instance of shardStorageBootstrapperFactory for run type sovereign
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,20 @@ package storageBootstrap
import (
"testing"

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

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

ssbf, err := NewSovereignShardStorageBootstrapperFactory(nil)

require.Nil(t, ssbf)
require.Equal(t, errors.ErrNilShardStorageBootstrapperFactory, err)

sbf, _ := NewShardStorageBootstrapperFactory()
ssbf, err = NewSovereignShardStorageBootstrapperFactory(sbf)

ssbf := NewSovereignShardStorageBootstrapperFactory()
require.NotNil(t, ssbf)
require.Nil(t, err)
}

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

sbf, _ := NewShardStorageBootstrapperFactory()
ssbf, _ := NewSovereignShardStorageBootstrapperFactory(sbf)
ssbf := NewSovereignShardStorageBootstrapperFactory()
sb, err := ssbf.CreateBootstrapperFromStorage(ArgsShardStorageBootstrapper{})

require.Nil(t, sb)
Expand All @@ -41,8 +31,7 @@ func TestSovereignShardStorageBootstrapperFactory_CreateShardStorageBootstrapper
func TestSovereignShardStorageBootstrapperFactory_IsInterfaceNil(t *testing.T) {
t.Parallel()

sbf, _ := NewShardStorageBootstrapperFactory()
ssbf, _ := NewSovereignShardStorageBootstrapperFactory(sbf)
ssbf := NewSovereignShardStorageBootstrapperFactory()

require.False(t, ssbf.IsInterfaceNil())

Expand Down

0 comments on commit 1e0b915

Please sign in to comment.