diff --git a/factory/runType/runTypeComponents.go b/factory/runType/runTypeComponents.go index e25cf1007cc..6e6b049e6a7 100644 --- a/factory/runType/runTypeComponents.go +++ b/factory/runType/runTypeComponents.go @@ -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 { diff --git a/factory/runType/sovereignRunTypeComponents.go b/factory/runType/sovereignRunTypeComponents.go index 9fb73afdd25..e09a94965e3 100644 --- a/factory/runType/sovereignRunTypeComponents.go +++ b/factory/runType/sovereignRunTypeComponents.go @@ -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 { diff --git a/process/sync/storageBootstrap/shardStorageBootstrapFactory.go b/process/sync/storageBootstrap/shardStorageBootstrapFactory.go index 8851d575d5d..32a1c53b2ef 100644 --- a/process/sync/storageBootstrap/shardStorageBootstrapFactory.go +++ b/process/sync/storageBootstrap/shardStorageBootstrapFactory.go @@ -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 diff --git a/process/sync/storageBootstrap/shardStorageBootstrapFactory_test.go b/process/sync/storageBootstrap/shardStorageBootstrapFactory_test.go index 075293e4069..a07062661c1 100644 --- a/process/sync/storageBootstrap/shardStorageBootstrapFactory_test.go +++ b/process/sync/storageBootstrap/shardStorageBootstrapFactory_test.go @@ -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) @@ -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 diff --git a/process/sync/storageBootstrap/shardStorageBootstrapper_test.go b/process/sync/storageBootstrap/shardStorageBootstrapper_test.go index 82422c8e1a9..51c9fcd318d 100644 --- a/process/sync/storageBootstrap/shardStorageBootstrapper_test.go +++ b/process/sync/storageBootstrap/shardStorageBootstrapper_test.go @@ -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 @@ -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 { @@ -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) } diff --git a/process/sync/storageBootstrap/sovereignChainShardStorageBootstrapper_test.go b/process/sync/storageBootstrap/sovereignChainShardStorageBootstrapper_test.go index 8a355d389a2..0876166c470 100644 --- a/process/sync/storageBootstrap/sovereignChainShardStorageBootstrapper_test.go +++ b/process/sync/storageBootstrap/sovereignChainShardStorageBootstrapper_test.go @@ -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()) +} diff --git a/process/sync/storageBootstrap/sovereignShardStorageBootstrapFactory.go b/process/sync/storageBootstrap/sovereignShardStorageBootstrapFactory.go index 17ff7bc8150..25815602f07 100644 --- a/process/sync/storageBootstrap/sovereignShardStorageBootstrapFactory.go +++ b/process/sync/storageBootstrap/sovereignShardStorageBootstrapFactory.go @@ -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 diff --git a/process/sync/storageBootstrap/sovereignShardStorageBootstrapFactory_test.go b/process/sync/storageBootstrap/sovereignShardStorageBootstrapFactory_test.go index a03b48cb211..2c345400524 100644 --- a/process/sync/storageBootstrap/sovereignShardStorageBootstrapFactory_test.go +++ b/process/sync/storageBootstrap/sovereignShardStorageBootstrapFactory_test.go @@ -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) @@ -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())