diff --git a/process/block/baseProcess.go b/process/block/baseProcess.go index 7e2a2f5d982..dcd53bd77dd 100644 --- a/process/block/baseProcess.go +++ b/process/block/baseProcess.go @@ -105,6 +105,7 @@ type baseProcessor struct { pruningDelay uint32 processedMiniBlocksTracker process.ProcessedMiniBlocksTracker receiptsRepository receiptsRepository + processStatusHandler common.ProcessStatusHandler } type bootStorerDataArgs struct { @@ -1820,10 +1821,12 @@ func (bp *baseProcessor) Close() error { // ProcessScheduledBlock processes a scheduled block func (bp *baseProcessor) ProcessScheduledBlock(headerHandler data.HeaderHandler, bodyHandler data.BodyHandler, haveTime func() time.Duration) error { var err error + bp.processStatusHandler.SetBusy("baseProcessor.ProcessScheduledBlock") defer func() { if err != nil { bp.RevertCurrentBlock() } + bp.processStatusHandler.SetIdle() }() scheduledMiniBlocksFromMe, err := getScheduledMiniBlocksFromMe(headerHandler, bodyHandler) diff --git a/process/block/baseProcess_test.go b/process/block/baseProcess_test.go index c4760e95da4..f760c6d63b1 100644 --- a/process/block/baseProcess_test.go +++ b/process/block/baseProcess_test.go @@ -51,6 +51,11 @@ import ( "github.com/stretchr/testify/require" ) +const ( + busyIdentifier = "busy" + idleIdentifier = "idle" +) + func haveTime() time.Duration { return 2000 * time.Millisecond } @@ -2026,6 +2031,15 @@ func TestBaseProcessor_ProcessScheduledBlockShouldFail(t *testing.T) { t.Parallel() arguments := CreateMockArguments(createComponentHolderMocks()) + processHandler := arguments.CoreComponents.ProcessStatusHandler() + mockProcessHandler := processHandler.(*testscommon.ProcessStatusHandlerStub) + busyIdleCalled := make([]string, 0) + mockProcessHandler.SetIdleCalled = func() { + busyIdleCalled = append(busyIdleCalled, idleIdentifier) + } + mockProcessHandler.SetBusyCalled = func(reason string) { + busyIdleCalled = append(busyIdleCalled, busyIdentifier) + } localErr := errors.New("execute all err") scheduledTxsExec := &testscommon.ScheduledTxsExecutionStub{ @@ -2042,11 +2056,21 @@ func TestBaseProcessor_ProcessScheduledBlockShouldFail(t *testing.T) { ) assert.Equal(t, localErr, err) + assert.Equal(t, []string{busyIdentifier, idleIdentifier}, busyIdleCalled) }) t.Run("get root hash fail", func(t *testing.T) { t.Parallel() arguments := CreateMockArguments(createComponentHolderMocks()) + processHandler := arguments.CoreComponents.ProcessStatusHandler() + mockProcessHandler := processHandler.(*testscommon.ProcessStatusHandlerStub) + busyIdleCalled := make([]string, 0) + mockProcessHandler.SetIdleCalled = func() { + busyIdleCalled = append(busyIdleCalled, idleIdentifier) + } + mockProcessHandler.SetBusyCalled = func(reason string) { + busyIdleCalled = append(busyIdleCalled, busyIdentifier) + } localErr := errors.New("root hash err") accounts := &stateMock.AccountsStub{ @@ -2063,6 +2087,7 @@ func TestBaseProcessor_ProcessScheduledBlockShouldFail(t *testing.T) { ) assert.Equal(t, localErr, err) + assert.Equal(t, []string{busyIdentifier, idleIdentifier}, busyIdleCalled) // the order is important }) } @@ -2119,6 +2144,16 @@ func TestBaseProcessor_ProcessScheduledBlockShouldWork(t *testing.T) { } arguments := CreateMockArguments(createComponentHolderMocks()) + processHandler := arguments.CoreComponents.ProcessStatusHandler() + mockProcessHandler := processHandler.(*testscommon.ProcessStatusHandlerStub) + busyIdleCalled := make([]string, 0) + mockProcessHandler.SetIdleCalled = func() { + busyIdleCalled = append(busyIdleCalled, idleIdentifier) + } + mockProcessHandler.SetBusyCalled = func(reason string) { + busyIdleCalled = append(busyIdleCalled, busyIdentifier) + } + arguments.AccountsDB[state.UserAccountsState] = accounts arguments.ScheduledTxsExecutionHandler = scheduledTxsExec arguments.FeeHandler = feeHandler @@ -2132,6 +2167,7 @@ func TestBaseProcessor_ProcessScheduledBlockShouldWork(t *testing.T) { assert.True(t, wasCalledSetScheduledGasAndFees) assert.True(t, wasCalledSetScheduledRootHash) + assert.Equal(t, []string{busyIdentifier, idleIdentifier}, busyIdleCalled) // the order is important } // get initial fees on first getGasAndFees call and final fees on second call diff --git a/process/block/metablock.go b/process/block/metablock.go index 2e1696d7880..3cb4f9d0513 100644 --- a/process/block/metablock.go +++ b/process/block/metablock.go @@ -46,7 +46,6 @@ type metaProcessor struct { rewardsV2EnableEpoch uint32 userStatePruningQueue core.Queue peerStatePruningQueue core.Queue - processStatusHandler common.ProcessStatusHandler } // NewMetaProcessor creates a new metaProcessor object @@ -137,6 +136,7 @@ func NewMetaProcessor(arguments ArgMetaProcessor) (*metaProcessor, error) { pruningDelay: pruningDelay, processedMiniBlocksTracker: arguments.ProcessedMiniBlocksTracker, receiptsRepository: arguments.ReceiptsRepository, + processStatusHandler: arguments.CoreComponents.ProcessStatusHandler(), } mp := metaProcessor{ @@ -151,7 +151,6 @@ func NewMetaProcessor(arguments ArgMetaProcessor) (*metaProcessor, error) { validatorInfoCreator: arguments.EpochValidatorInfoCreator, epochSystemSCProcessor: arguments.EpochSystemSCProcessor, rewardsV2EnableEpoch: arguments.RewardsV2EnableEpoch, - processStatusHandler: arguments.CoreComponents.ProcessStatusHandler(), } log.Debug("metablock: enable epoch for staking v2", "epoch", mp.rewardsV2EnableEpoch) diff --git a/process/block/metablock_test.go b/process/block/metablock_test.go index 00fb6fb21c1..e0613b5b49f 100644 --- a/process/block/metablock_test.go +++ b/process/block/metablock_test.go @@ -891,13 +891,12 @@ func TestMetaProcessor_CommitBlockStorageFailsForHeaderShouldNotReturnError(t *t processHandler := arguments.CoreComponents.ProcessStatusHandler() mockProcessHandler := processHandler.(*testscommon.ProcessStatusHandlerStub) - statusBusySet := false - statusIdleSet := false + busyIdleCalled := make([]string, 0) mockProcessHandler.SetIdleCalled = func() { - statusIdleSet = true + busyIdleCalled = append(busyIdleCalled, idleIdentifier) } mockProcessHandler.SetBusyCalled = func(reason string) { - statusBusySet = true + busyIdleCalled = append(busyIdleCalled, busyIdentifier) } mp.SetHdrForCurrentBlock([]byte("hdr_hash1"), &block.Header{}, true) @@ -905,7 +904,7 @@ func TestMetaProcessor_CommitBlockStorageFailsForHeaderShouldNotReturnError(t *t wg.Wait() assert.True(t, wasCalled) assert.Nil(t, err) - assert.True(t, statusBusySet && statusIdleSet) + assert.Equal(t, []string{busyIdentifier, idleIdentifier}, busyIdleCalled) // the order is important } func TestMetaProcessor_CommitBlockNoTxInPoolShouldErr(t *testing.T) { @@ -2967,13 +2966,12 @@ func TestMetaProcessor_CreateAndProcessBlockCallsProcessAfterFirstEpoch(t *testi } processHandler := arguments.CoreComponents.ProcessStatusHandler() mockProcessHandler := processHandler.(*testscommon.ProcessStatusHandlerStub) - statusBusySet := false - statusIdleSet := false + busyIdleCalled := make([]string, 0) mockProcessHandler.SetIdleCalled = func() { - statusIdleSet = true + busyIdleCalled = append(busyIdleCalled, idleIdentifier) } mockProcessHandler.SetBusyCalled = func(reason string) { - statusBusySet = true + busyIdleCalled = append(busyIdleCalled, busyIdentifier) } mp, _ := blproc.NewMetaProcessor(arguments) @@ -2981,7 +2979,7 @@ func TestMetaProcessor_CreateAndProcessBlockCallsProcessAfterFirstEpoch(t *testi headerHandler, bodyHandler, err := mp.CreateBlock(metaHdr, func() bool { return true }) assert.Nil(t, err) assert.True(t, toggleCalled, calledSaveNodesCoordinator) - assert.True(t, statusBusySet && statusIdleSet) + assert.Equal(t, []string{busyIdentifier, idleIdentifier}, busyIdleCalled) // the order is important err = headerHandler.SetRound(uint64(1)) assert.Nil(t, err) @@ -3001,12 +2999,11 @@ func TestMetaProcessor_CreateAndProcessBlockCallsProcessAfterFirstEpoch(t *testi toggleCalled = false calledSaveNodesCoordinator = false - statusBusySet = false - statusIdleSet = false + busyIdleCalled = make([]string, 0) err = mp.ProcessBlock(headerHandler, bodyHandler, func() time.Duration { return time.Second }) assert.Nil(t, err) assert.True(t, toggleCalled, calledSaveNodesCoordinator) - assert.True(t, statusBusySet && statusIdleSet) + assert.Equal(t, []string{busyIdentifier, idleIdentifier}, busyIdleCalled) // the order is important } func TestMetaProcessor_CreateNewHeaderErrWrongTypeAssertion(t *testing.T) { diff --git a/process/block/shardblock.go b/process/block/shardblock.go index 164f547364f..58c6ddf9e53 100644 --- a/process/block/shardblock.go +++ b/process/block/shardblock.go @@ -51,7 +51,6 @@ type shardProcessor struct { chRcvAllMetaHdrs chan bool userStatePruningQueue core.Queue - processStatusHandler common.ProcessStatusHandler } // NewShardProcessor creates a new shardProcessor object @@ -122,11 +121,11 @@ func NewShardProcessor(arguments ArgShardProcessor) (*shardProcessor, error) { pruningDelay: pruningDelay, processedMiniBlocksTracker: arguments.ProcessedMiniBlocksTracker, receiptsRepository: arguments.ReceiptsRepository, + processStatusHandler: arguments.CoreComponents.ProcessStatusHandler(), } sp := shardProcessor{ - baseProcessor: base, - processStatusHandler: arguments.CoreComponents.ProcessStatusHandler(), + baseProcessor: base, } sp.txCounter, err = NewTransactionCounter(sp.hasher, sp.marshalizer) diff --git a/process/block/shardblock_test.go b/process/block/shardblock_test.go index 171aec9accb..775b558418d 100644 --- a/process/block/shardblock_test.go +++ b/process/block/shardblock_test.go @@ -286,19 +286,18 @@ func TestShardProcess_CreateNewBlockHeaderProcessHeaderExpectCheckRoundCalled(t processHandler := arguments.CoreComponents.ProcessStatusHandler() mockProcessHandler := processHandler.(*testscommon.ProcessStatusHandlerStub) - statusBusySet := false - statusIdleSet := false + busyIdleCalled := make([]string, 0) mockProcessHandler.SetIdleCalled = func() { - statusIdleSet = true + busyIdleCalled = append(busyIdleCalled, idleIdentifier) } mockProcessHandler.SetBusyCalled = func(reason string) { - statusBusySet = true + busyIdleCalled = append(busyIdleCalled, busyIdentifier) } err = shardProcessor.ProcessBlock(headerHandler, bodyHandler, func() time.Duration { return time.Second }) require.Nil(t, err) require.Equal(t, int64(2), checkRoundCt.Get()) - assert.True(t, statusIdleSet && statusBusySet) + assert.Equal(t, []string{busyIdentifier, idleIdentifier}, busyIdleCalled) // the order is important } func TestShardProcessor_ProcessWithDirtyAccountShouldErr(t *testing.T) { @@ -1923,20 +1922,19 @@ func TestShardProcessor_CommitBlockStorageFailsForHeaderShouldErr(t *testing.T) processHandler := arguments.CoreComponents.ProcessStatusHandler() mockProcessHandler := processHandler.(*testscommon.ProcessStatusHandlerStub) - statusBusySet := false - statusIdleSet := false + busyIdleCalled := make([]string, 0) mockProcessHandler.SetIdleCalled = func() { - statusIdleSet = true + busyIdleCalled = append(busyIdleCalled, idleIdentifier) } mockProcessHandler.SetBusyCalled = func(reason string) { - statusBusySet = true + busyIdleCalled = append(busyIdleCalled, busyIdentifier) } err := sp.CommitBlock(hdr, body) wg.Wait() assert.True(t, atomic.LoadUint32(&putCalledNr) > 0) assert.Nil(t, err) - assert.True(t, statusBusySet && statusIdleSet) + assert.Equal(t, []string{busyIdentifier, idleIdentifier}, busyIdleCalled) // the order is important } func TestShardProcessor_CommitBlockStorageFailsForBodyShouldWork(t *testing.T) {