Skip to content

Commit

Permalink
added close to intercepted data cacher and a few unit tests on nil ch…
Browse files Browse the repository at this point in the history
…eckers.
  • Loading branch information
cristure committed Oct 3, 2024
1 parent b9eb2a5 commit 8451f62
Show file tree
Hide file tree
Showing 13 changed files with 101 additions and 75 deletions.
4 changes: 1 addition & 3 deletions epochStart/bootstrap/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -996,9 +996,7 @@ func TestCreateSyncers(t *testing.T) {
epochStartProvider.whiteListerVerifiedTxs = &testscommon.WhiteListHandlerStub{}
epochStartProvider.requestHandler = &testscommon.RequestHandlerStub{}
epochStartProvider.storageService = &storageMocks.ChainStorerStub{}
epochStartProvider.interceptedDataVerifierFactory = &processMock.InterceptedDataVerifierFactoryMock{CreateCalled: func(topic string) (process.InterceptedDataVerifier, error) {
return &processMock.InterceptedDataVerifierMock{}, nil
}}
epochStartProvider.interceptedDataVerifierFactory = &processMock.InterceptedDataVerifierFactoryMock{}

err := epochStartProvider.createSyncers()
assert.Nil(t, err)
Expand Down
4 changes: 1 addition & 3 deletions epochStart/bootstrap/storageProcess_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,7 @@ func TestStorageEpochStartBootstrap_BootstrapMetablockNotFound(t *testing.T) {
}
args.GeneralConfig = testscommon.GetGeneralConfig()
args.GeneralConfig.EpochStartConfig.RoundsPerEpoch = roundsPerEpoch
args.InterceptedDataVerifierFactory = &processMock.InterceptedDataVerifierFactoryMock{CreateCalled: func(topic string) (process.InterceptedDataVerifier, error) {
return &processMock.InterceptedDataVerifierMock{}, nil
}}
args.InterceptedDataVerifierFactory = &processMock.InterceptedDataVerifierFactoryMock{}
sesb, _ := NewStorageEpochStartBootstrap(args)

params, err := sesb.Bootstrap()
Expand Down
6 changes: 6 additions & 0 deletions epochStart/bootstrap/syncEpochStartMeta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ func TestNewEpochStartMetaSyncer_NilsShouldError(t *testing.T) {
ess, err = NewEpochStartMetaSyncer(args)
assert.True(t, check.IfNil(ess))
assert.Equal(t, epochStart.ErrNilMetablockProcessor, err)

args = getEpochStartSyncerArgs()
args.InterceptedDataVerifierFactory = nil
ess, err = NewEpochStartMetaSyncer(args)
assert.True(t, check.IfNil(ess))
assert.Equal(t, epochStart.ErrNilInterceptedDataVerifierFactory, err)
}

func TestNewEpochStartMetaSyncer_ShouldWork(t *testing.T) {
Expand Down
15 changes: 12 additions & 3 deletions factory/processing/processComponents.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import (
"github.com/multiversx/mx-chain-go/process/factory/interceptorscontainer"
"github.com/multiversx/mx-chain-go/process/headerCheck"
"github.com/multiversx/mx-chain-go/process/heartbeat/validator"
interceptorFactory "github.com/multiversx/mx-chain-go/process/interceptors/factory"
"github.com/multiversx/mx-chain-go/process/peer"
"github.com/multiversx/mx-chain-go/process/receipts"
"github.com/multiversx/mx-chain-go/process/smartContract"
Expand Down Expand Up @@ -133,6 +134,7 @@ type processComponents struct {
receiptsRepository mainFactory.ReceiptsRepository
sentSignaturesTracker process.SentSignaturesTracker
epochSystemSCProcessor process.EpochStartSystemSCProcessor
interceptedDataVerifierFactory process.InterceptedDataVerifierFactory
}

// ProcessComponentsFactoryArgs holds the arguments needed to create a process components factory
Expand Down Expand Up @@ -168,8 +170,6 @@ type ProcessComponentsFactoryArgs struct {

GenesisNonce uint64
GenesisRound uint64

InterceptedDataVerifierFactory process.InterceptedDataVerifierFactory
}

type processComponentsFactory struct {
Expand Down Expand Up @@ -221,6 +221,11 @@ func NewProcessComponentsFactory(args ProcessComponentsFactoryArgs) (*processCom
return nil, err
}

interceptedDataVerifierFactory := interceptorFactory.NewInterceptedDataVerifierFactory(interceptorFactory.InterceptedDataVerifierFactoryArgs{
CacheSpan: time.Duration(args.Config.InterceptedDataVerifier.CacheSpanInSec) * time.Second,
CacheExpiry: time.Duration(args.Config.InterceptedDataVerifier.CacheExpiryInSec) * time.Second,
})

return &processComponentsFactory{
config: args.Config,
epochConfig: args.EpochConfig,
Expand Down Expand Up @@ -252,7 +257,7 @@ func NewProcessComponentsFactory(args ProcessComponentsFactoryArgs) (*processCom
genesisNonce: args.GenesisNonce,
genesisRound: args.GenesisRound,
roundConfig: args.RoundConfig,
interceptedDataVerifierFactory: args.InterceptedDataVerifierFactory,
interceptedDataVerifierFactory: interceptedDataVerifierFactory,
}, nil
}

Expand Down Expand Up @@ -769,6 +774,7 @@ func (pcf *processComponentsFactory) Create() (*processComponents, error) {
accountsParser: pcf.accountsParser,
receiptsRepository: receiptsRepository,
sentSignaturesTracker: sentSignaturesTracker,
interceptedDataVerifierFactory: pcf.interceptedDataVerifierFactory,
}, nil
}

Expand Down Expand Up @@ -2055,6 +2061,9 @@ func (pc *processComponents) Close() error {
if !check.IfNil(pc.txsSender) {
log.LogIfError(pc.txsSender.Close())
}
if !check.IfNil(pc.interceptedDataVerifierFactory) {
log.LogIfError(pc.interceptedDataVerifierFactory.Close())
}

return nil
}
Expand Down
6 changes: 0 additions & 6 deletions factory/processing/processComponents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
testsMocks "github.com/multiversx/mx-chain-go/integrationTests/mock"
"github.com/multiversx/mx-chain-go/p2p"
"github.com/multiversx/mx-chain-go/process"
processMocks "github.com/multiversx/mx-chain-go/process/mock"
"github.com/multiversx/mx-chain-go/sharding"
"github.com/multiversx/mx-chain-go/sharding/nodesCoordinator"
"github.com/multiversx/mx-chain-go/state"
Expand Down Expand Up @@ -267,11 +266,6 @@ func createMockProcessComponentsFactoryArgs() processComp.ProcessComponentsFacto
}

args.State = components.GetStateComponents(args.CoreData, args.StatusCoreComponents)
args.InterceptedDataVerifierFactory = &processMocks.InterceptedDataVerifierFactoryMock{
CreateCalled: func(topic string) (process.InterceptedDataVerifier, error) {
return &processMocks.InterceptedDataVerifierMock{}, nil
},
}

return args
}
Expand Down
61 changes: 27 additions & 34 deletions node/nodeRunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ import (
"github.com/multiversx/mx-chain-go/outport"
"github.com/multiversx/mx-chain-go/process"
"github.com/multiversx/mx-chain-go/process/interceptors"
"github.com/multiversx/mx-chain-go/process/interceptors/factory"
"github.com/multiversx/mx-chain-go/sharding/nodesCoordinator"
"github.com/multiversx/mx-chain-go/state/syncer"
"github.com/multiversx/mx-chain-go/storage/cache"
Expand Down Expand Up @@ -1238,40 +1237,34 @@ func (nr *nodeRunner) CreateManagedProcessComponents(

txExecutionOrderHandler := ordering.NewOrderedCollection()

interceptedDataVerifierFactory := factory.NewInterceptedDataVerifierFactory(factory.InterceptedDataVerifierFactoryArgs{
CacheSpan: time.Duration(nr.configs.GeneralConfig.InterceptedDataVerifier.CacheExpiryInSec),
CacheExpiry: time.Duration(nr.configs.GeneralConfig.InterceptedDataVerifier.CacheExpiryInSec),
})

processArgs := processComp.ProcessComponentsFactoryArgs{
Config: *configs.GeneralConfig,
EpochConfig: *configs.EpochConfig,
RoundConfig: *configs.RoundConfig,
PrefConfigs: *configs.PreferencesConfig,
ImportDBConfig: *configs.ImportDbConfig,
EconomicsConfig: *configs.EconomicsConfig,
AccountsParser: accountsParser,
SmartContractParser: smartContractParser,
GasSchedule: gasScheduleNotifier,
NodesCoordinator: nodesCoordinator,
Data: dataComponents,
CoreData: coreComponents,
Crypto: cryptoComponents,
State: stateComponents,
Network: networkComponents,
BootstrapComponents: bootstrapComponents,
StatusComponents: statusComponents,
StatusCoreComponents: statusCoreComponents,
RequestedItemsHandler: requestedItemsHandler,
WhiteListHandler: whiteListRequest,
WhiteListerVerifiedTxs: whiteListerVerifiedTxs,
MaxRating: configs.RatingsConfig.General.MaxRating,
SystemSCConfig: configs.SystemSCConfig,
ImportStartHandler: importStartHandler,
HistoryRepo: historyRepository,
FlagsConfig: *configs.FlagsConfig,
TxExecutionOrderHandler: txExecutionOrderHandler,
InterceptedDataVerifierFactory: interceptedDataVerifierFactory,
Config: *configs.GeneralConfig,
EpochConfig: *configs.EpochConfig,
RoundConfig: *configs.RoundConfig,
PrefConfigs: *configs.PreferencesConfig,
ImportDBConfig: *configs.ImportDbConfig,
EconomicsConfig: *configs.EconomicsConfig,
AccountsParser: accountsParser,
SmartContractParser: smartContractParser,
GasSchedule: gasScheduleNotifier,
NodesCoordinator: nodesCoordinator,
Data: dataComponents,
CoreData: coreComponents,
Crypto: cryptoComponents,
State: stateComponents,
Network: networkComponents,
BootstrapComponents: bootstrapComponents,
StatusComponents: statusComponents,
StatusCoreComponents: statusCoreComponents,
RequestedItemsHandler: requestedItemsHandler,
WhiteListHandler: whiteListRequest,
WhiteListerVerifiedTxs: whiteListerVerifiedTxs,
MaxRating: configs.RatingsConfig.General.MaxRating,
SystemSCConfig: configs.SystemSCConfig,
ImportStartHandler: importStartHandler,
HistoryRepo: historyRepository,
FlagsConfig: *configs.FlagsConfig,
TxExecutionOrderHandler: txExecutionOrderHandler,
}
processComponentsFactory, err := processComp.NewProcessComponentsFactory(processArgs)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,18 @@ func TestNewMetaInterceptorsContainerFactory_NilPeerSignatureHandler(t *testing.
assert.Equal(t, process.ErrNilPeerSignatureHandler, err)
}

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

coreComp, cryptoComp := createMockComponentHolders()
args := getArgumentsShard(coreComp, cryptoComp)
args.InterceptedDataVerifierFactory = nil
icf, err := interceptorscontainer.NewMetaInterceptorsContainerFactory(args)

assert.Nil(t, icf)
assert.Equal(t, process.ErrNilInterceptedDataVerifierFactory, err)
}

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

Expand Down Expand Up @@ -546,9 +558,7 @@ func testCreateMetaTopicShouldFail(matchStrToErrOnCreate string, matchStrToErrOn
} else {
args.MainMessenger = createMetaStubTopicHandler(matchStrToErrOnCreate, matchStrToErrOnRegister)
}
args.InterceptedDataVerifierFactory = &mock.InterceptedDataVerifierFactoryMock{CreateCalled: func(topic string) (process.InterceptedDataVerifier, error) {
return &mock.InterceptedDataVerifierMock{}, nil
}}
args.InterceptedDataVerifierFactory = &mock.InterceptedDataVerifierFactoryMock{}
icf, _ := interceptorscontainer.NewMetaInterceptorsContainerFactory(args)

mainContainer, fullArchiveConatiner, err := icf.Create()
Expand All @@ -564,9 +574,7 @@ func TestMetaInterceptorsContainerFactory_CreateShouldWork(t *testing.T) {

coreComp, cryptoComp := createMockComponentHolders()
args := getArgumentsMeta(coreComp, cryptoComp)
args.InterceptedDataVerifierFactory = &mock.InterceptedDataVerifierFactoryMock{CreateCalled: func(topic string) (process.InterceptedDataVerifier, error) {
return &mock.InterceptedDataVerifierMock{}, nil
}}
args.InterceptedDataVerifierFactory = &mock.InterceptedDataVerifierFactoryMock{}
icf, _ := interceptorscontainer.NewMetaInterceptorsContainerFactory(args)

mainContainer, fullArchiveContainer, err := icf.Create()
Expand Down Expand Up @@ -599,9 +607,7 @@ func TestMetaInterceptorsContainerFactory_With4ShardsShouldWork(t *testing.T) {
args := getArgumentsMeta(coreComp, cryptoComp)
args.ShardCoordinator = shardCoordinator
args.NodesCoordinator = nodesCoordinator
args.InterceptedDataVerifierFactory = &mock.InterceptedDataVerifierFactoryMock{CreateCalled: func(topic string) (process.InterceptedDataVerifier, error) {
return &mock.InterceptedDataVerifierMock{}, nil
}}
args.InterceptedDataVerifierFactory = &mock.InterceptedDataVerifierFactoryMock{}

icf, err := interceptorscontainer.NewMetaInterceptorsContainerFactory(args)
require.Nil(t, err)
Expand Down Expand Up @@ -653,9 +659,7 @@ func TestMetaInterceptorsContainerFactory_With4ShardsShouldWork(t *testing.T) {
args.NodeOperationMode = common.FullArchiveMode
args.ShardCoordinator = shardCoordinator
args.NodesCoordinator = nodesCoordinator
args.InterceptedDataVerifierFactory = &mock.InterceptedDataVerifierFactoryMock{CreateCalled: func(topic string) (process.InterceptedDataVerifier, error) {
return &mock.InterceptedDataVerifierMock{}, nil
}}
args.InterceptedDataVerifierFactory = &mock.InterceptedDataVerifierFactoryMock{}

icf, err := interceptorscontainer.NewMetaInterceptorsContainerFactory(args)
require.Nil(t, err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,18 @@ func TestNewShardInterceptorsContainerFactory_NilValidityAttesterShouldErr(t *te
assert.Equal(t, process.ErrNilValidityAttester, err)
}

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

coreComp, cryptoComp := createMockComponentHolders()
args := getArgumentsShard(coreComp, cryptoComp)
args.InterceptedDataVerifierFactory = nil
icf, err := interceptorscontainer.NewShardInterceptorsContainerFactory(args)

assert.Nil(t, icf)
assert.Equal(t, process.ErrNilInterceptedDataVerifierFactory, err)
}

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

Expand Down Expand Up @@ -497,9 +509,7 @@ func testCreateShardTopicShouldFail(matchStrToErrOnCreate string, matchStrToErrO

coreComp, cryptoComp := createMockComponentHolders()
args := getArgumentsShard(coreComp, cryptoComp)
args.InterceptedDataVerifierFactory = &mock.InterceptedDataVerifierFactoryMock{CreateCalled: func(topic string) (process.InterceptedDataVerifier, error) {
return &mock.InterceptedDataVerifierMock{}, nil
}}
args.InterceptedDataVerifierFactory = &mock.InterceptedDataVerifierFactoryMock{}
if strings.Contains(t.Name(), "full_archive") {
args.NodeOperationMode = common.FullArchiveMode
args.FullArchiveMessenger = createShardStubTopicHandler(matchStrToErrOnCreate, matchStrToErrOnRegister)
Expand Down Expand Up @@ -566,9 +576,7 @@ func TestShardInterceptorsContainerFactory_CreateShouldWork(t *testing.T) {
},
}
args.WhiteListerVerifiedTxs = &testscommon.WhiteListHandlerStub{}
args.InterceptedDataVerifierFactory = &mock.InterceptedDataVerifierFactoryMock{CreateCalled: func(topic string) (process.InterceptedDataVerifier, error) {
return &mock.InterceptedDataVerifierMock{}, nil
}}
args.InterceptedDataVerifierFactory = &mock.InterceptedDataVerifierFactoryMock{}

icf, _ := interceptorscontainer.NewShardInterceptorsContainerFactory(args)

Expand Down Expand Up @@ -604,9 +612,7 @@ func TestShardInterceptorsContainerFactory_With4ShardsShouldWork(t *testing.T) {
args.ShardCoordinator = shardCoordinator
args.NodesCoordinator = nodesCoordinator
args.PreferredPeersHolder = &p2pmocks.PeersHolderStub{}
args.InterceptedDataVerifierFactory = &mock.InterceptedDataVerifierFactoryMock{CreateCalled: func(topic string) (process.InterceptedDataVerifier, error) {
return &mock.InterceptedDataVerifierMock{}, nil
}}
args.InterceptedDataVerifierFactory = &mock.InterceptedDataVerifierFactoryMock{}

icf, _ := interceptorscontainer.NewShardInterceptorsContainerFactory(args)

Expand Down Expand Up @@ -657,9 +663,7 @@ func TestShardInterceptorsContainerFactory_With4ShardsShouldWork(t *testing.T) {
args.ShardCoordinator = shardCoordinator
args.NodesCoordinator = nodesCoordinator
args.PreferredPeersHolder = &p2pmocks.PeersHolderStub{}
args.InterceptedDataVerifierFactory = &mock.InterceptedDataVerifierFactoryMock{CreateCalled: func(topic string) (process.InterceptedDataVerifier, error) {
return &mock.InterceptedDataVerifierMock{}, nil
}}
args.InterceptedDataVerifierFactory = &mock.InterceptedDataVerifierFactoryMock{}

icf, _ := interceptorscontainer.NewShardInterceptorsContainerFactory(args)

Expand Down
12 changes: 12 additions & 0 deletions process/interceptors/factory/interceptedDataVerifierFactory.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package factory

import (
"fmt"
"sync"
"time"

Expand Down Expand Up @@ -53,6 +54,17 @@ func (idvf *interceptedDataVerifierFactory) Create(topic string) (process.Interc
return interceptors.NewInterceptedDataVerifier(internalCache)
}

func (idvf *interceptedDataVerifierFactory) Close() error {
for topic, cacher := range idvf.interceptedDataVerifierMap {
err := cacher.Close()
if err != nil {
return fmt.Errorf("failed to close cacher on topic %q: %w", topic, err)
}
}

return nil
}

// IsInterfaceNil returns true if there is no value under the interface
func (idvf *interceptedDataVerifierFactory) IsInterfaceNil() bool {
return idvf == nil
Expand Down
1 change: 1 addition & 0 deletions process/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -1411,5 +1411,6 @@ type InterceptedDataVerifier interface {
// InterceptedDataVerifierFactory defines a component that is able to create intercepted data verifiers
type InterceptedDataVerifierFactory interface {
Create(topic string) (InterceptedDataVerifier, error)
Close() error
IsInterfaceNil() bool
}
5 changes: 5 additions & 0 deletions process/mock/interceptedDataVerifierFactoryMock.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ func (idvfs *InterceptedDataVerifierFactoryMock) Create(topic string) (process.I
return &InterceptedDataVerifierMock{}, nil
}

// Close -
func (idvfs *InterceptedDataVerifierFactoryMock) Close() error {
return nil
}

// IsInterfaceNil -
func (idvfs *InterceptedDataVerifierFactoryMock) IsInterfaceNil() bool {
return idvfs == nil
Expand Down
2 changes: 0 additions & 2 deletions testscommon/components/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import (
"github.com/multiversx/mx-chain-go/p2p"
p2pConfig "github.com/multiversx/mx-chain-go/p2p/config"
p2pFactory "github.com/multiversx/mx-chain-go/p2p/factory"
processMock "github.com/multiversx/mx-chain-go/process/mock"
"github.com/multiversx/mx-chain-go/sharding"
"github.com/multiversx/mx-chain-go/sharding/nodesCoordinator"
"github.com/multiversx/mx-chain-go/state"
Expand Down Expand Up @@ -609,7 +608,6 @@ func GetProcessArgs(
},
},
},
InterceptedDataVerifierFactory: &processMock.InterceptedDataVerifierFactoryMock{},
}
}

Expand Down
4 changes: 4 additions & 0 deletions testscommon/generalConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,10 @@ func GetGeneralConfig() config.Config {
ResourceStats: config.ResourceStatsConfig{
RefreshIntervalInSec: 1,
},
InterceptedDataVerifier: config.InterceptedDataVerifierConfig{
CacheSpanInSec: 1,
CacheExpiryInSec: 1,
},
}
}

Expand Down

0 comments on commit 8451f62

Please sign in to comment.