Skip to content

Commit

Permalink
- testing notifying the snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
iulianpascalau committed Feb 3, 2023
1 parent e6c1e13 commit 4732733
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 28 deletions.
3 changes: 3 additions & 0 deletions process/block/baseProcess.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ type baseProcessor struct {
pruningDelay uint32
processedMiniBlocksTracker process.ProcessedMiniBlocksTracker
receiptsRepository receiptsRepository
processStatusHandler common.ProcessStatusHandler
}

type bootStorerDataArgs struct {
Expand Down Expand Up @@ -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)
Expand Down
36 changes: 36 additions & 0 deletions process/block/baseProcess_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ import (
"github.com/stretchr/testify/require"
)

const (
busyIdentifier = "busy"
idleIdentifier = "idle"
)

func haveTime() time.Duration {
return 2000 * time.Millisecond
}
Expand Down Expand Up @@ -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{
Expand All @@ -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{
Expand All @@ -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
})
}

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions process/block/metablock.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ type metaProcessor struct {
rewardsV2EnableEpoch uint32
userStatePruningQueue core.Queue
peerStatePruningQueue core.Queue
processStatusHandler common.ProcessStatusHandler
}

// NewMetaProcessor creates a new metaProcessor object
Expand Down Expand Up @@ -137,6 +136,7 @@ func NewMetaProcessor(arguments ArgMetaProcessor) (*metaProcessor, error) {
pruningDelay: pruningDelay,
processedMiniBlocksTracker: arguments.ProcessedMiniBlocksTracker,
receiptsRepository: arguments.ReceiptsRepository,
processStatusHandler: arguments.CoreComponents.ProcessStatusHandler(),
}

mp := metaProcessor{
Expand All @@ -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)
Expand Down
23 changes: 10 additions & 13 deletions process/block/metablock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -891,21 +891,20 @@ 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)
err := mp.CommitBlock(hdr, body)
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) {
Expand Down Expand Up @@ -2967,21 +2966,20 @@ 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)
metaHdr := &block.MetaBlock{}
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)
Expand All @@ -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) {
Expand Down
5 changes: 2 additions & 3 deletions process/block/shardblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ type shardProcessor struct {
chRcvAllMetaHdrs chan bool

userStatePruningQueue core.Queue
processStatusHandler common.ProcessStatusHandler
}

// NewShardProcessor creates a new shardProcessor object
Expand Down Expand Up @@ -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)
Expand Down
18 changes: 8 additions & 10 deletions process/block/shardblock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 4732733

Please sign in to comment.