diff --git a/process/block/preprocess/basePreProcess.go b/process/block/preprocess/basePreProcess.go index cbee99359e5..37e33d887a7 100644 --- a/process/block/preprocess/basePreProcess.go +++ b/process/block/preprocess/basePreProcess.go @@ -323,7 +323,11 @@ func (bpp *basePreProcess) computeExistingAndRequestMissing( } txShardInfoObject := &txShardInfo{senderShardID: miniBlock.SenderShardID, receiverShardID: miniBlock.ReceiverShardID} - searchFirst := miniBlock.Type == block.InvalidBlock + method := process.SearchMethodJustPeek + if miniBlock.Type == block.InvalidBlock { + method = process.SearchMethodSearchFirst + } + for j := 0; j < len(miniBlock.TxHashes); j++ { txHash := miniBlock.TxHashes[j] @@ -338,7 +342,7 @@ func (bpp *basePreProcess) computeExistingAndRequestMissing( miniBlock.ReceiverShardID, txHash, txPool, - searchFirst) + method) if err != nil { txHashes = append(txHashes, txHash) diff --git a/process/block/preprocess/rewardTxPreProcessor.go b/process/block/preprocess/rewardTxPreProcessor.go index 8f3da4d0449..c8a15e44eae 100644 --- a/process/block/preprocess/rewardTxPreProcessor.go +++ b/process/block/preprocess/rewardTxPreProcessor.go @@ -381,7 +381,7 @@ func (rtp *rewardTxPreprocessor) computeMissingRewardTxsForMiniBlock(miniBlock * miniBlock.ReceiverShardID, txHash, rtp.rewardTxPool, - false, + process.SearchMethodJustPeek, ) if tx == nil { diff --git a/process/block/preprocess/smartContractResults.go b/process/block/preprocess/smartContractResults.go index c9706a26687..fda1ecb0f8c 100644 --- a/process/block/preprocess/smartContractResults.go +++ b/process/block/preprocess/smartContractResults.go @@ -452,7 +452,7 @@ func (scr *smartContractResults) computeMissingScrsForMiniBlock(miniBlock *block miniBlock.ReceiverShardID, txHash, scr.scrPool, - false) + process.SearchMethodPeekWithFallbackSearchFirst) if check.IfNil(tx) { missingSmartContractResults = append(missingSmartContractResults, txHash) @@ -484,7 +484,14 @@ func (scr *smartContractResults) getAllScrsFromMiniBlock( tmp, _ := txCache.Peek(txHash) if tmp == nil { - return nil, nil, process.ErrNilSmartContractResult + tmp, _ = scr.scrPool.SearchFirstData(txHash) + if tmp == nil { + return nil, nil, process.ErrNilSmartContractResult + } + + log.Debug("scr hash not found with Peek method but found with SearchFirstData", + "scr hash", txHash, + "strCache", strCache) } tx, ok := tmp.(*smartContractResult.SmartContractResult) diff --git a/process/block/preprocess/smartContractResults_test.go b/process/block/preprocess/smartContractResults_test.go index 8e397f07958..61cd0110be6 100644 --- a/process/block/preprocess/smartContractResults_test.go +++ b/process/block/preprocess/smartContractResults_test.go @@ -495,7 +495,13 @@ func TestScrsPreProcessor_GetTransactionFromPool(t *testing.T) { ) txHash := []byte("tx1_hash") - tx, _ := process.GetTransactionHandlerFromPool(1, 1, txHash, tdp.UnsignedTransactions(), false) + tx, _ := process.GetTransactionHandlerFromPool( + 1, + 1, + txHash, + tdp.UnsignedTransactions(), + process.SearchMethodPeekWithFallbackSearchFirst, + ) assert.NotNil(t, txs) assert.NotNil(t, tx) assert.Equal(t, uint64(10), tx.(*smartContractResult.SmartContractResult).Nonce) @@ -752,6 +758,95 @@ func TestScrsPreprocessor_GetAllTxsFromMiniBlockShouldWork(t *testing.T) { } } +func TestScrsPreprocessor_GetAllTxsFromMiniBlockShouldWorkEvenIfScrIsMisplaced(t *testing.T) { + t.Parallel() + + hasher := &hashingMocks.HasherMock{} + marshalizer := &mock.MarshalizerMock{} + dataPool := dataRetrieverMock.NewPoolsHolderMock() + senderShardId := uint32(0) + destinationShardId := uint32(1) + + txsSlice := []*smartContractResult.SmartContractResult{ + {Nonce: 1}, + {Nonce: 2}, + {Nonce: 3}, + } + transactionsHashes := make([][]byte, len(txsSlice)) + + // add defined transactions to sender-destination cacher + for idx, tx := range txsSlice { + transactionsHashes[idx] = computeHash(tx, marshalizer, hasher) + + if idx < len(txsSlice)-1 { + // place the first scrs correctly in pool + dataPool.UnsignedTransactions().AddData( + transactionsHashes[idx], + tx, + tx.Size(), + process.ShardCacherIdentifier(senderShardId, destinationShardId), + ) + } else { + // misplace the last one + dataPool.UnsignedTransactions().AddData( + transactionsHashes[idx], + tx, + tx.Size(), + process.ShardCacherIdentifier(senderShardId, senderShardId), // only in shard 0 + ) + } + } + + // add some random data + txRandom := &smartContractResult.SmartContractResult{Nonce: 4} + dataPool.UnsignedTransactions().AddData( + computeHash(txRandom, marshalizer, hasher), + txRandom, + txRandom.Size(), + process.ShardCacherIdentifier(3, 4), + ) + + requestTransaction := func(shardID uint32, txHashes [][]byte) {} + txs, _ := NewSmartContractResultPreprocessor( + dataPool.UnsignedTransactions(), + &mock.ChainStorerMock{}, + &hashingMocks.HasherMock{}, + &mock.MarshalizerMock{}, + &testscommon.TxProcessorMock{}, + mock.NewMultiShardsCoordinatorMock(3), + &stateMock.AccountsStub{}, + requestTransaction, + &testscommon.GasHandlerStub{}, + feeHandlerMock(), + createMockPubkeyConverter(), + &testscommon.BlockSizeComputationStub{}, + &testscommon.BalanceComputationStub{}, + &epochNotifier.EpochNotifierStub{}, + 2, + &testscommon.ProcessedMiniBlocksTrackerStub{}, + ) + + mb := &block.MiniBlock{ + SenderShardID: senderShardId, + ReceiverShardID: destinationShardId, + TxHashes: transactionsHashes, + Type: block.SmartContractResultBlock, + } + + txsRetrieved, txHashesRetrieved, err := txs.getAllScrsFromMiniBlock(mb, haveTimeTrue) + + assert.Nil(t, err) + assert.Equal(t, len(txsSlice), len(txsRetrieved)) + assert.Equal(t, len(txsSlice), len(txHashesRetrieved)) + + for idx, tx := range txsSlice { + // txReceived should be all txs in the same order + assert.Equal(t, txsRetrieved[idx], tx) + // verify corresponding transaction hashes + assert.Equal(t, txHashesRetrieved[idx], computeHash(tx, marshalizer, hasher)) + } +} + func TestScrsPreprocessor_RemoveBlockDataFromPoolsNilBlockShouldErr(t *testing.T) { t.Parallel() diff --git a/process/block/preprocess/transactions.go b/process/block/preprocess/transactions.go index 67a882862ee..83ff56339bf 100644 --- a/process/block/preprocess/transactions.go +++ b/process/block/preprocess/transactions.go @@ -800,7 +800,10 @@ func (txs *transactions) AddTxsFromMiniBlocks(miniBlocks block.MiniBlockSlice) { } txShardInfoToSet := &txShardInfo{senderShardID: mb.SenderShardID, receiverShardID: mb.ReceiverShardID} - searchFirst := mb.Type == block.InvalidBlock + method := process.SearchMethodJustPeek + if mb.Type == block.InvalidBlock { + method = process.SearchMethodSearchFirst + } for _, txHash := range mb.TxHashes { tx, err := process.GetTransactionHandler( @@ -810,7 +813,7 @@ func (txs *transactions) AddTxsFromMiniBlocks(miniBlocks block.MiniBlockSlice) { txs.txPool, txs.storage, txs.marshalizer, - searchFirst, + method, ) if err != nil { log.Debug("transactions.AddTxsFromMiniBlocks: GetTransactionHandler", "tx hash", txHash, "error", err.Error()) @@ -948,7 +951,10 @@ func (txs *transactions) computeMissingTxsForMiniBlock(miniBlock *block.MiniBloc } missingTransactions := make([][]byte, 0, len(miniBlock.TxHashes)) - searchFirst := txs.blockType == block.InvalidBlock + method := process.SearchMethodJustPeek + if txs.blockType == block.InvalidBlock { + method = process.SearchMethodSearchFirst + } for _, txHash := range miniBlock.TxHashes { tx, _ := process.GetTransactionHandlerFromPool( @@ -956,7 +962,7 @@ func (txs *transactions) computeMissingTxsForMiniBlock(miniBlock *block.MiniBloc miniBlock.ReceiverShardID, txHash, txs.txPool, - searchFirst) + method) if tx == nil || tx.IsInterfaceNil() { missingTransactions = append(missingTransactions, txHash) diff --git a/process/block/preprocess/transactions_test.go b/process/block/preprocess/transactions_test.go index 0f0cc3c5396..e77249034a8 100644 --- a/process/block/preprocess/transactions_test.go +++ b/process/block/preprocess/transactions_test.go @@ -445,7 +445,12 @@ func TestTxsPreProcessor_GetTransactionFromPool(t *testing.T) { dataPool := initDataPool() txs := createGoodPreprocessor(dataPool) txHash := []byte("tx2_hash") - tx, _ := process.GetTransactionHandlerFromPool(1, 1, txHash, dataPool.Transactions(), false) + tx, _ := process.GetTransactionHandlerFromPool( + 1, + 1, + txHash, + dataPool.Transactions(), + process.SearchMethodJustPeek) assert.NotNil(t, txs) assert.NotNil(t, tx) assert.Equal(t, uint64(10), tx.(*transaction.Transaction).Nonce) diff --git a/process/common.go b/process/common.go index fff4908e75f..a124faf1726 100644 --- a/process/common.go +++ b/process/common.go @@ -26,6 +26,35 @@ import ( var log = logger.GetOrCreate("process") +// ShardedCacheSearchMethod defines the algorithm for searching through a sharded cache +type ShardedCacheSearchMethod byte + +const ( + // SearchMethodJustPeek will make the algorithm invoke just Peek method + SearchMethodJustPeek ShardedCacheSearchMethod = iota + + // SearchMethodSearchFirst will make the algorithm invoke just SearchFirst method + SearchMethodSearchFirst + + // SearchMethodPeekWithFallbackSearchFirst will first try a Peek method. If the data is not found will fall back + // to SearchFirst method + SearchMethodPeekWithFallbackSearchFirst +) + +// ToString converts the ShardedCacheSearchMethod to its string representation +func (method ShardedCacheSearchMethod) ToString() string { + switch method { + case SearchMethodJustPeek: + return "just peek" + case SearchMethodSearchFirst: + return "search first" + case SearchMethodPeekWithFallbackSearchFirst: + return "peek with fallback to search first" + default: + return fmt.Sprintf("unknown method %d", method) + } +} + // GetShardHeader gets the header, which is associated with the given hash, from pool or storage func GetShardHeader( hash []byte, @@ -361,7 +390,7 @@ func GetTransactionHandler( shardedDataCacherNotifier dataRetriever.ShardedDataCacherNotifier, storageService dataRetriever.StorageService, marshalizer marshal.Marshalizer, - searchFirst bool, + method ShardedCacheSearchMethod, ) (data.TransactionHandler, error) { err := checkGetTransactionParamsForNil(shardedDataCacherNotifier, storageService, marshalizer) @@ -369,7 +398,7 @@ func GetTransactionHandler( return nil, err } - tx, err := GetTransactionHandlerFromPool(senderShardID, destShardID, txHash, shardedDataCacherNotifier, searchFirst) + tx, err := GetTransactionHandlerFromPool(senderShardID, destShardID, txHash, shardedDataCacherNotifier, method) if err != nil { tx, err = GetTransactionHandlerFromStorage(txHash, storageService, marshalizer) if err != nil { @@ -386,30 +415,55 @@ func GetTransactionHandlerFromPool( destShardID uint32, txHash []byte, shardedDataCacherNotifier dataRetriever.ShardedDataCacherNotifier, - searchFirst bool, + method ShardedCacheSearchMethod, ) (data.TransactionHandler, error) { - if shardedDataCacherNotifier == nil { + if check.IfNil(shardedDataCacherNotifier) { return nil, ErrNilShardedDataCacherNotifier } + return getTransactionHandlerFromPool(senderShardID, destShardID, txHash, shardedDataCacherNotifier, method) +} + +func getTransactionHandlerFromPool( + senderShardID uint32, + destShardID uint32, + txHash []byte, + shardedDataCacherNotifier dataRetriever.ShardedDataCacherNotifier, + method ShardedCacheSearchMethod, +) (data.TransactionHandler, error) { var val interface{} - ok := false - if searchFirst { + var ok bool + + if method == SearchMethodSearchFirst { val, ok = shardedDataCacherNotifier.SearchFirstData(txHash) - if !ok { - return nil, ErrTxNotFound - } - } else { - strCache := ShardCacherIdentifier(senderShardID, destShardID) - txStore := shardedDataCacherNotifier.ShardDataStore(strCache) - if txStore == nil { - return nil, ErrNilStorage - } + return castDataFromCacheAsTransactionHandler(val, ok) + } + + strCache := ShardCacherIdentifier(senderShardID, destShardID) + txStore := shardedDataCacherNotifier.ShardDataStore(strCache) + if txStore == nil { + return nil, ErrNilStorage + } + + switch method { + case SearchMethodJustPeek: val, ok = txStore.Peek(txHash) + case SearchMethodPeekWithFallbackSearchFirst: + val, ok = txStore.Peek(txHash) + if !ok { + val, ok = shardedDataCacherNotifier.SearchFirstData(txHash) + } + default: + return nil, fmt.Errorf("%w for provided method: %s in getTransactionHandlerFromPool", + ErrInvalidValue, method.ToString()) } + return castDataFromCacheAsTransactionHandler(val, ok) +} + +func castDataFromCacheAsTransactionHandler(val interface{}, ok bool) (data.TransactionHandler, error) { if !ok { return nil, ErrTxNotFound } diff --git a/process/common_test.go b/process/common_test.go index b805023659c..3d34dec0240 100644 --- a/process/common_test.go +++ b/process/common_test.go @@ -4,6 +4,7 @@ import ( "bytes" "errors" "math/big" + "strings" "testing" "github.com/ElrondNetwork/elrond-go-core/core" @@ -21,6 +22,8 @@ import ( ) func TestGetShardHeaderShouldErrNilCacher(t *testing.T) { + t.Parallel() + hash := []byte("X") marshalizer := &mock.MarshalizerMock{} @@ -32,6 +35,8 @@ func TestGetShardHeaderShouldErrNilCacher(t *testing.T) { } func TestGetShardHeaderShouldErrNilMarshalizer(t *testing.T) { + t.Parallel() + hash := []byte("X") cacher := &mock.HeadersCacherStub{} @@ -43,6 +48,8 @@ func TestGetShardHeaderShouldErrNilMarshalizer(t *testing.T) { } func TestGetShardHeaderShouldErrNilStorage(t *testing.T) { + t.Parallel() + hash := []byte("X") cacher := &mock.HeadersCacherStub{} @@ -54,6 +61,8 @@ func TestGetShardHeaderShouldErrNilStorage(t *testing.T) { } func TestGetShardHeaderShouldGetHeaderFromPool(t *testing.T) { + t.Parallel() + hash := []byte("X") hdr := &block.Header{Nonce: 1} @@ -70,6 +79,8 @@ func TestGetShardHeaderShouldGetHeaderFromPool(t *testing.T) { } func TestGetShardHeaderShouldGetHeaderFromStorage(t *testing.T) { + t.Parallel() + hash := []byte("X") hdr := &block.Header{Nonce: 1} @@ -97,6 +108,8 @@ func TestGetShardHeaderShouldGetHeaderFromStorage(t *testing.T) { } func TestGetMetaHeaderShouldErrNilCacher(t *testing.T) { + t.Parallel() + hash := []byte("X") marshalizer := &mock.MarshalizerMock{} @@ -108,6 +121,8 @@ func TestGetMetaHeaderShouldErrNilCacher(t *testing.T) { } func TestGetMetaHeaderShouldErrNilMarshalizer(t *testing.T) { + t.Parallel() + hash := []byte("X") cacher := &mock.HeadersCacherStub{} @@ -119,6 +134,8 @@ func TestGetMetaHeaderShouldErrNilMarshalizer(t *testing.T) { } func TestGetMetaHeaderShouldErrNilStorage(t *testing.T) { + t.Parallel() + hash := []byte("X") cacher := &mock.HeadersCacherStub{} @@ -130,6 +147,8 @@ func TestGetMetaHeaderShouldErrNilStorage(t *testing.T) { } func TestGetMetaHeaderShouldGetHeaderFromPool(t *testing.T) { + t.Parallel() + hash := []byte("X") hdr := &block.MetaBlock{Nonce: 1} @@ -146,6 +165,8 @@ func TestGetMetaHeaderShouldGetHeaderFromPool(t *testing.T) { } func TestGetMetaHeaderShouldGetHeaderFromStorage(t *testing.T) { + t.Parallel() + hash := []byte("X") hdr := &block.MetaBlock{Nonce: 1} @@ -173,6 +194,8 @@ func TestGetMetaHeaderShouldGetHeaderFromStorage(t *testing.T) { } func TestGetShardHeaderFromPoolShouldErrNilCacher(t *testing.T) { + t.Parallel() + hash := []byte("X") header, err := process.GetShardHeaderFromPool(hash, nil) @@ -181,6 +204,8 @@ func TestGetShardHeaderFromPoolShouldErrNilCacher(t *testing.T) { } func TestGetShardHeaderFromPoolShouldErrMissingHeader(t *testing.T) { + t.Parallel() + hash := []byte("X") cacher := &mock.HeadersCacherStub{ @@ -195,6 +220,8 @@ func TestGetShardHeaderFromPoolShouldErrMissingHeader(t *testing.T) { } func TestGetShardHeaderFromPoolShouldErrWrongTypeAssertion(t *testing.T) { + t.Parallel() + hash := []byte("X") cacher := &mock.HeadersCacherStub{ @@ -209,6 +236,8 @@ func TestGetShardHeaderFromPoolShouldErrWrongTypeAssertion(t *testing.T) { } func TestGetShardHeaderFromPoolShouldWork(t *testing.T) { + t.Parallel() + hash := []byte("X") hdr := &block.Header{Nonce: 10} @@ -224,6 +253,8 @@ func TestGetShardHeaderFromPoolShouldWork(t *testing.T) { } func TestGetMetaHeaderFromPoolShouldErrNilCacher(t *testing.T) { + t.Parallel() + hash := []byte("X") header, err := process.GetMetaHeaderFromPool(hash, nil) @@ -232,6 +263,8 @@ func TestGetMetaHeaderFromPoolShouldErrNilCacher(t *testing.T) { } func TestGetMetaHeaderFromPoolShouldErrMissingHeader(t *testing.T) { + t.Parallel() + hash := []byte("X") cacher := &mock.HeadersCacherStub{ @@ -246,6 +279,8 @@ func TestGetMetaHeaderFromPoolShouldErrMissingHeader(t *testing.T) { } func TestGetMetaHeaderFromPoolShouldErrWrongTypeAssertion(t *testing.T) { + t.Parallel() + hash := []byte("X") cacher := &mock.HeadersCacherStub{ GetHeaderByHashCalled: func(hash []byte) (handler data.HeaderHandler, e error) { @@ -259,6 +294,8 @@ func TestGetMetaHeaderFromPoolShouldErrWrongTypeAssertion(t *testing.T) { } func TestGetMetaHeaderFromPoolShouldWork(t *testing.T) { + t.Parallel() + hash := []byte("X") hdr := &block.MetaBlock{Nonce: 10} @@ -274,6 +311,8 @@ func TestGetMetaHeaderFromPoolShouldWork(t *testing.T) { } func TestGetHeaderFromStorageShouldWork(t *testing.T) { + t.Parallel() + shardHeader := &block.Header{Nonce: 42} metaHeader := &block.MetaBlock{Nonce: 43} marshalizer := &mock.MarshalizerMock{} @@ -303,6 +342,8 @@ func TestGetHeaderFromStorageShouldWork(t *testing.T) { } func TestGetShardHeaderFromStorageShouldErrNilCacher(t *testing.T) { + t.Parallel() + hash := []byte("X") storageService := &mock.ChainStorerMock{} @@ -313,6 +354,8 @@ func TestGetShardHeaderFromStorageShouldErrNilCacher(t *testing.T) { } func TestGetShardHeaderFromStorageShouldErrNilStorage(t *testing.T) { + t.Parallel() + hash := []byte("X") marshalizer := &mock.MarshalizerMock{} @@ -323,6 +366,8 @@ func TestGetShardHeaderFromStorageShouldErrNilStorage(t *testing.T) { } func TestGetShardHeaderFromStorageShouldErrNilHeadersStorage(t *testing.T) { + t.Parallel() + hash := []byte("X") marshalizer := &mock.MarshalizerMock{} @@ -338,6 +383,8 @@ func TestGetShardHeaderFromStorageShouldErrNilHeadersStorage(t *testing.T) { } func TestGetShardHeaderFromStorageShouldErrMissingHeader(t *testing.T) { + t.Parallel() + hash := []byte("X") marshalizer := &mock.MarshalizerMock{} @@ -357,6 +404,8 @@ func TestGetShardHeaderFromStorageShouldErrMissingHeader(t *testing.T) { } func TestGetShardHeaderFromStorageShouldErrUnmarshalWithoutSuccess(t *testing.T) { + t.Parallel() + hash := []byte("X") marshalizer := &mock.MarshalizerMock{} @@ -376,6 +425,8 @@ func TestGetShardHeaderFromStorageShouldErrUnmarshalWithoutSuccess(t *testing.T) } func TestGetShardHeaderFromStorageShouldWork(t *testing.T) { + t.Parallel() + hash := []byte("X") hdr := &block.Header{} @@ -399,6 +450,8 @@ func TestGetShardHeaderFromStorageShouldWork(t *testing.T) { } func TestGetMetaHeaderFromStorageShouldErrNilCacher(t *testing.T) { + t.Parallel() + hash := []byte("X") storageService := &mock.ChainStorerMock{} @@ -409,6 +462,8 @@ func TestGetMetaHeaderFromStorageShouldErrNilCacher(t *testing.T) { } func TestGetMetaHeaderFromStorageShouldErrNilStorage(t *testing.T) { + t.Parallel() + hash := []byte("X") marshalizer := &mock.MarshalizerMock{} @@ -419,6 +474,8 @@ func TestGetMetaHeaderFromStorageShouldErrNilStorage(t *testing.T) { } func TestGetMetaHeaderFromStorageShouldErrNilHeadersStorage(t *testing.T) { + t.Parallel() + hash := []byte("X") marshalizer := &mock.MarshalizerMock{} @@ -434,6 +491,8 @@ func TestGetMetaHeaderFromStorageShouldErrNilHeadersStorage(t *testing.T) { } func TestGetMetaHeaderFromStorageShouldErrMissingHeader(t *testing.T) { + t.Parallel() + hash := []byte("X") marshalizer := &mock.MarshalizerMock{} @@ -453,6 +512,8 @@ func TestGetMetaHeaderFromStorageShouldErrMissingHeader(t *testing.T) { } func TestGetMetaHeaderFromStorageShouldErrUnmarshalWithoutSuccess(t *testing.T) { + t.Parallel() + hash := []byte("X") marshalizer := &mock.MarshalizerMock{} @@ -472,6 +533,8 @@ func TestGetMetaHeaderFromStorageShouldErrUnmarshalWithoutSuccess(t *testing.T) } func TestGetMetaHeaderFromStorageShouldWork(t *testing.T) { + t.Parallel() + hash := []byte("X") hdr := &block.MetaBlock{} @@ -495,6 +558,8 @@ func TestGetMetaHeaderFromStorageShouldWork(t *testing.T) { } func TestGetMarshalizedHeaderFromStorageShouldErrNilMarshalizer(t *testing.T) { + t.Parallel() + hash := []byte("X") storageService := &mock.ChainStorerMock{} @@ -505,6 +570,8 @@ func TestGetMarshalizedHeaderFromStorageShouldErrNilMarshalizer(t *testing.T) { } func TestGetMarshalizedHeaderFromStorageShouldErrNilStorage(t *testing.T) { + t.Parallel() + hash := []byte("X") marshalizer := &mock.MarshalizerMock{} @@ -515,6 +582,8 @@ func TestGetMarshalizedHeaderFromStorageShouldErrNilStorage(t *testing.T) { } func TestGetMarshalizedHeaderFromStorageShouldErrNilHeadersStorage(t *testing.T) { + t.Parallel() + hash := []byte("X") marshalizer := &mock.MarshalizerMock{} @@ -530,6 +599,8 @@ func TestGetMarshalizedHeaderFromStorageShouldErrNilHeadersStorage(t *testing.T) } func TestGetMarshalizedHeaderFromStorageShouldErrMissingHeader(t *testing.T) { + t.Parallel() + hash := []byte("X") marshalizer := &mock.MarshalizerMock{} @@ -549,6 +620,8 @@ func TestGetMarshalizedHeaderFromStorageShouldErrMissingHeader(t *testing.T) { } func TestGetMarshalizedHeaderFromStorageShouldWork(t *testing.T) { + t.Parallel() + hash := []byte("X") hdr := &block.Header{Nonce: 1} @@ -573,6 +646,8 @@ func TestGetMarshalizedHeaderFromStorageShouldWork(t *testing.T) { } func TestGetShardHeaderWithNonceShouldErrNilCacher(t *testing.T) { + t.Parallel() + nonce := uint64(1) shardId := uint32(0) @@ -594,6 +669,8 @@ func TestGetShardHeaderWithNonceShouldErrNilCacher(t *testing.T) { } func TestGetShardHeaderWithNonceShouldErrNilMarshalizer(t *testing.T) { + t.Parallel() + nonce := uint64(1) shardId := uint32(0) @@ -615,6 +692,8 @@ func TestGetShardHeaderWithNonceShouldErrNilMarshalizer(t *testing.T) { } func TestGetShardHeaderWithNonceShouldErrNilStorage(t *testing.T) { + t.Parallel() + nonce := uint64(1) shardId := uint32(0) @@ -636,6 +715,8 @@ func TestGetShardHeaderWithNonceShouldErrNilStorage(t *testing.T) { } func TestGetShardHeaderWithNonceShouldErrNilUint64Converter(t *testing.T) { + t.Parallel() + nonce := uint64(1) shardId := uint32(0) @@ -657,6 +738,8 @@ func TestGetShardHeaderWithNonceShouldErrNilUint64Converter(t *testing.T) { } func TestGetShardHeaderWithNonceShouldGetHeaderFromPool(t *testing.T) { + t.Parallel() + hash := []byte("X") nonce := uint64(1) shardId := uint32(0) @@ -683,6 +766,8 @@ func TestGetShardHeaderWithNonceShouldGetHeaderFromPool(t *testing.T) { } func TestGetShardHeaderWithNonceShouldGetHeaderFromStorage(t *testing.T) { + t.Parallel() + hash := []byte("X") nonce := uint64(1) nonceToByte := []byte("1") @@ -734,6 +819,8 @@ func TestGetShardHeaderWithNonceShouldGetHeaderFromStorage(t *testing.T) { } func TestGetMetaHeaderWithNonceShouldErrNilCacher(t *testing.T) { + t.Parallel() + nonce := uint64(1) marshalizer := &mock.MarshalizerMock{} @@ -753,6 +840,8 @@ func TestGetMetaHeaderWithNonceShouldErrNilCacher(t *testing.T) { } func TestGetMetaHeaderWithNonceShouldErrNilMarshalizer(t *testing.T) { + t.Parallel() + nonce := uint64(1) cacher := &mock.HeadersCacherStub{} @@ -772,6 +861,8 @@ func TestGetMetaHeaderWithNonceShouldErrNilMarshalizer(t *testing.T) { } func TestGetMetaHeaderWithNonceShouldErrNilStorage(t *testing.T) { + t.Parallel() + nonce := uint64(1) cacher := &mock.HeadersCacherStub{} @@ -791,6 +882,8 @@ func TestGetMetaHeaderWithNonceShouldErrNilStorage(t *testing.T) { } func TestGetMetaHeaderWithNonceShouldErrNilUint64Converter(t *testing.T) { + t.Parallel() + nonce := uint64(1) cacher := &mock.HeadersCacherStub{} @@ -810,6 +903,8 @@ func TestGetMetaHeaderWithNonceShouldErrNilUint64Converter(t *testing.T) { } func TestGetMetaHeaderWithNonceShouldGetHeaderFromPool(t *testing.T) { + t.Parallel() + hash := []byte("X") nonce := uint64(1) @@ -834,6 +929,8 @@ func TestGetMetaHeaderWithNonceShouldGetHeaderFromPool(t *testing.T) { } func TestGetMetaHeaderWithNonceShouldGetHeaderFromStorage(t *testing.T) { + t.Parallel() + hash := []byte("X") nonce := uint64(1) nonceToByte := []byte("1") @@ -882,6 +979,8 @@ func TestGetMetaHeaderWithNonceShouldGetHeaderFromStorage(t *testing.T) { } func TestGetShardHeaderFromPoolWithNonceShouldErrNilCacher(t *testing.T) { + t.Parallel() + nonce := uint64(1) shardId := uint32(0) @@ -892,6 +991,8 @@ func TestGetShardHeaderFromPoolWithNonceShouldErrNilCacher(t *testing.T) { } func TestGetShardHeaderFromPoolWithNonceShouldErrMissingHashForHeaderNonceWhenShardIdHashMapIsNil(t *testing.T) { + t.Parallel() + nonce := uint64(1) shardId := uint32(0) @@ -904,6 +1005,8 @@ func TestGetShardHeaderFromPoolWithNonceShouldErrMissingHashForHeaderNonceWhenSh } func TestGetShardHeaderFromPoolWithNonceShouldErrMissingHashForHeaderNonceWhenLoadFromShardIdHashMapFails(t *testing.T) { + t.Parallel() + nonce := uint64(1) shardId := uint32(0) @@ -916,6 +1019,8 @@ func TestGetShardHeaderFromPoolWithNonceShouldErrMissingHashForHeaderNonceWhenLo } func TestGetShardHeaderFromPoolWithNonceShouldErrMissingHeader(t *testing.T) { + t.Parallel() + nonce := uint64(1) shardId := uint32(0) @@ -932,6 +1037,8 @@ func TestGetShardHeaderFromPoolWithNonceShouldErrMissingHeader(t *testing.T) { } func TestGetShardHeaderFromPoolWithNonceShouldErrWrongTypeAssertion(t *testing.T) { + t.Parallel() + nonce := uint64(1) shardId := uint32(0) @@ -948,6 +1055,8 @@ func TestGetShardHeaderFromPoolWithNonceShouldErrWrongTypeAssertion(t *testing.T } func TestGetShardHeaderFromPoolWithNonceShouldWork(t *testing.T) { + t.Parallel() + hash := []byte("X") nonce := uint64(1) shardId := uint32(0) @@ -966,6 +1075,8 @@ func TestGetShardHeaderFromPoolWithNonceShouldWork(t *testing.T) { } func TestGetMetaHeaderFromPoolWithNonceShouldErrNilCacher(t *testing.T) { + t.Parallel() + nonce := uint64(1) header, hash, err := process.GetMetaHeaderFromPoolWithNonce(nonce, nil) @@ -975,6 +1086,8 @@ func TestGetMetaHeaderFromPoolWithNonceShouldErrNilCacher(t *testing.T) { } func TestGetMetaHeaderFromPoolWithNonceShouldErrMissingHashForHeaderNonceWhenShardIdHashMapIsNil(t *testing.T) { + t.Parallel() + nonce := uint64(1) cacher := &mock.HeadersCacherStub{} @@ -986,6 +1099,8 @@ func TestGetMetaHeaderFromPoolWithNonceShouldErrMissingHashForHeaderNonceWhenSha } func TestGetMetaHeaderFromPoolWithNonceShouldErrMissingHashForHeaderNonceWhenLoadFromShardIdHashMapFails(t *testing.T) { + t.Parallel() + nonce := uint64(1) cacher := &mock.HeadersCacherStub{} @@ -997,6 +1112,8 @@ func TestGetMetaHeaderFromPoolWithNonceShouldErrMissingHashForHeaderNonceWhenLoa } func TestGetMetaHeaderFromPoolWithNonceShouldErrMissingHeader(t *testing.T) { + t.Parallel() + nonce := uint64(1) cacher := &mock.HeadersCacherStub{ @@ -1012,6 +1129,8 @@ func TestGetMetaHeaderFromPoolWithNonceShouldErrMissingHeader(t *testing.T) { } func TestGetMetaHeaderFromPoolWithNonceShouldErrWrongTypeAssertion(t *testing.T) { + t.Parallel() + hash := []byte("X") nonce := uint64(1) @@ -1028,6 +1147,8 @@ func TestGetMetaHeaderFromPoolWithNonceShouldErrWrongTypeAssertion(t *testing.T) } func TestGetMetaHeaderFromPoolWithNonceShouldWork(t *testing.T) { + t.Parallel() + hash := []byte("X") nonce := uint64(1) @@ -1045,6 +1166,8 @@ func TestGetMetaHeaderFromPoolWithNonceShouldWork(t *testing.T) { } func TestGetShardHeaderFromStorageWithNonceShouldErrNilStorage(t *testing.T) { + t.Parallel() + nonce := uint64(1) shardId := uint32(0) @@ -1064,6 +1187,8 @@ func TestGetShardHeaderFromStorageWithNonceShouldErrNilStorage(t *testing.T) { } func TestGetShardHeaderFromStorageWithNonceShouldErrNilUint64Converter(t *testing.T) { + t.Parallel() + nonce := uint64(1) shardId := uint32(0) @@ -1083,6 +1208,8 @@ func TestGetShardHeaderFromStorageWithNonceShouldErrNilUint64Converter(t *testin } func TestGetShardHeaderFromStorageWithNonceShouldErrNilMarshalizer(t *testing.T) { + t.Parallel() + nonce := uint64(1) shardId := uint32(0) @@ -1102,6 +1229,8 @@ func TestGetShardHeaderFromStorageWithNonceShouldErrNilMarshalizer(t *testing.T) } func TestGetShardHeaderFromStorageWithNonceShouldErrNilHeadersStorage(t *testing.T) { + t.Parallel() + nonce := uint64(1) shardId := uint32(0) @@ -1126,6 +1255,8 @@ func TestGetShardHeaderFromStorageWithNonceShouldErrNilHeadersStorage(t *testing } func TestGetShardHeaderFromStorageWithNonceShouldErrMissingHashForHeaderNonce(t *testing.T) { + t.Parallel() + nonce := uint64(1) shardId := uint32(0) @@ -1154,6 +1285,8 @@ func TestGetShardHeaderFromStorageWithNonceShouldErrMissingHashForHeaderNonce(t } func TestGetShardHeaderFromStorageWithNonceShouldErrMissingHeader(t *testing.T) { + t.Parallel() + nonce := uint64(1) shardId := uint32(0) hash := []byte("X") @@ -1194,6 +1327,8 @@ func TestGetShardHeaderFromStorageWithNonceShouldErrMissingHeader(t *testing.T) } func TestGetShardHeaderFromStorageWithNonceShouldErrUnmarshalWithoutSuccess(t *testing.T) { + t.Parallel() + nonce := uint64(1) shardId := uint32(0) hash := []byte("X") @@ -1272,6 +1407,8 @@ func initDefaultStorageServiceAndConverter(nonce uint64, hash []byte, hdr data.H } func TestGetHeaderFromStorageWithNonceShouldWorkForShard(t *testing.T) { + t.Parallel() + nonce := uint64(1) shardId := uint32(0) hash := []byte("X") @@ -1293,6 +1430,8 @@ func TestGetHeaderFromStorageWithNonceShouldWorkForShard(t *testing.T) { } func TestGetShardHeaderFromStorageWithNonceShouldWorkForShard(t *testing.T) { + t.Parallel() + nonce := uint64(1) shardId := uint32(0) hash := []byte("X") @@ -1313,6 +1452,8 @@ func TestGetShardHeaderFromStorageWithNonceShouldWorkForShard(t *testing.T) { } func TestGetMetaHeaderFromStorageWithNonceShouldErrNilStorage(t *testing.T) { + t.Parallel() + nonce := uint64(1) uint64Converter := &mock.Uint64ByteSliceConverterMock{} @@ -1330,6 +1471,8 @@ func TestGetMetaHeaderFromStorageWithNonceShouldErrNilStorage(t *testing.T) { } func TestGetMetaHeaderFromStorageWithNonceShouldErrNilUint64Converter(t *testing.T) { + t.Parallel() + nonce := uint64(1) storageService := &mock.ChainStorerMock{} @@ -1347,6 +1490,8 @@ func TestGetMetaHeaderFromStorageWithNonceShouldErrNilUint64Converter(t *testing } func TestGetMetaHeaderFromStorageWithNonceShouldErrNilMarshalizer(t *testing.T) { + t.Parallel() + nonce := uint64(1) storageService := &mock.ChainStorerMock{} @@ -1364,6 +1509,8 @@ func TestGetMetaHeaderFromStorageWithNonceShouldErrNilMarshalizer(t *testing.T) } func TestGetMetaHeaderFromStorageWithNonceShouldErrNilHeadersStorage(t *testing.T) { + t.Parallel() + nonce := uint64(1) storageService := &mock.ChainStorerMock{ @@ -1386,6 +1533,8 @@ func TestGetMetaHeaderFromStorageWithNonceShouldErrNilHeadersStorage(t *testing. } func TestGetMetaHeaderFromStorageWithNonceShouldErrMissingHashForHeaderNonce(t *testing.T) { + t.Parallel() + nonce := uint64(1) storageService := &mock.ChainStorerMock{ @@ -1412,6 +1561,8 @@ func TestGetMetaHeaderFromStorageWithNonceShouldErrMissingHashForHeaderNonce(t * } func TestGetMetaHeaderFromStorageWithNonceShouldErrMissingHeader(t *testing.T) { + t.Parallel() + nonce := uint64(1) hash := []byte("X") nonceToByte := []byte("1") @@ -1450,6 +1601,8 @@ func TestGetMetaHeaderFromStorageWithNonceShouldErrMissingHeader(t *testing.T) { } func TestGetMetaHeaderFromStorageWithNonceShouldErrUnmarshalWithoutSuccess(t *testing.T) { + t.Parallel() + nonce := uint64(1) hash := []byte("X") nonceToByte := []byte("1") @@ -1491,6 +1644,8 @@ func TestGetMetaHeaderFromStorageWithNonceShouldErrUnmarshalWithoutSuccess(t *te } func TestGetMetaHeaderFromStorageWithNonceShouldWork(t *testing.T) { + t.Parallel() + nonce := uint64(1) hash := []byte("X") nonceToByte := []byte("1") @@ -1534,6 +1689,8 @@ func TestGetMetaHeaderFromStorageWithNonceShouldWork(t *testing.T) { } func TestGetHeaderFromStorageWithNonceShouldWorkForMeta(t *testing.T) { + t.Parallel() + nonce := uint64(1) hash := []byte("X") nonceToByte := []byte("1") @@ -1577,64 +1734,65 @@ func TestGetHeaderFromStorageWithNonceShouldWorkForMeta(t *testing.T) { assert.Equal(t, hdr, header) } -func TestGetTransactionHandlerShouldErrNilShardedDataCacherNotifier(t *testing.T) { - hash := []byte("X") - - storageService := &mock.ChainStorerMock{} - marshalizer := &mock.MarshalizerMock{} - - tx, err := process.GetTransactionHandler( - 0, - 0, - hash, - nil, - storageService, - marshalizer, - false) - - assert.Nil(t, tx) - assert.Equal(t, process.ErrNilShardedDataCacherNotifier, err) -} +func TestGetTransactionHandler_Errors(t *testing.T) { + t.Parallel() -func TestGetTransactionHandlerShouldErrNilStorage(t *testing.T) { hash := []byte("X") + storageService := &mock.ChainStorerMock{} + marshaller := &mock.MarshalizerMock{} shardedDataCacherNotifier := testscommon.NewShardedDataStub() - marshalizer := &mock.MarshalizerMock{} - tx, err := process.GetTransactionHandler( - 0, - 0, - hash, - shardedDataCacherNotifier, - nil, - marshalizer, - false) - - assert.Nil(t, tx) - assert.Equal(t, process.ErrNilStorage, err) -} - -func TestGetTransactionHandlerShouldErrNilMarshalizer(t *testing.T) { - hash := []byte("X") + t.Run("errors if the sharded cacher is nil", func(t *testing.T) { + t.Parallel() - storageService := &mock.ChainStorerMock{} - shardedDataCacherNotifier := testscommon.NewShardedDataStub() + tx, err := process.GetTransactionHandler( + 0, + 0, + hash, + nil, + storageService, + marshaller, + process.SearchMethodJustPeek) + + assert.Nil(t, tx) + assert.Equal(t, process.ErrNilShardedDataCacherNotifier, err) + }) + t.Run("errors if the storage service is nil", func(t *testing.T) { + t.Parallel() - tx, err := process.GetTransactionHandler( - 0, - 0, - hash, - shardedDataCacherNotifier, - storageService, - nil, - false) + tx, err := process.GetTransactionHandler( + 0, + 0, + hash, + shardedDataCacherNotifier, + nil, + marshaller, + process.SearchMethodJustPeek) + + assert.Nil(t, tx) + assert.Equal(t, process.ErrNilStorage, err) + }) + t.Run("errors if the marshaller is nil", func(t *testing.T) { + t.Parallel() - assert.Nil(t, tx) - assert.Equal(t, process.ErrNilMarshalizer, err) + tx, err := process.GetTransactionHandler( + 0, + 0, + hash, + shardedDataCacherNotifier, + storageService, + nil, + process.SearchMethodJustPeek) + + assert.Nil(t, tx) + assert.Equal(t, process.ErrNilMarshalizer, err) + }) } func TestGetTransactionHandlerShouldGetTransactionFromPool(t *testing.T) { + t.Parallel() + hash := []byte("X") txFromPool := &transaction.Transaction{Nonce: 1} @@ -1657,13 +1815,15 @@ func TestGetTransactionHandlerShouldGetTransactionFromPool(t *testing.T) { shardedDataCacherNotifier, storageService, marshalizer, - false) + process.SearchMethodJustPeek) assert.Nil(t, err) assert.Equal(t, tx, txFromPool) } func TestGetTransactionHandlerShouldGetTransactionFromStorage(t *testing.T) { + t.Parallel() + hash := []byte("X") txFromStorage := &transaction.Transaction{ Nonce: 1, @@ -1697,32 +1857,75 @@ func TestGetTransactionHandlerShouldGetTransactionFromStorage(t *testing.T) { shardedDataCacherNotifier, storageService, marshalizer, - false) + process.SearchMethodJustPeek) assert.Nil(t, err) assert.Equal(t, tx, txFromStorage) } -func TestGetTransactionHandlerFromPoolShouldErrNilShardedDataCacherNotifier(t *testing.T) { +func TestGetTransactionHandlerFromPool_Errors(t *testing.T) { + t.Parallel() + hash := []byte("X") - tx, err := process.GetTransactionHandlerFromPool( - 0, - 0, - hash, - nil, - false) + shardedDataCacherNotifier := testscommon.NewShardedDataStub() + shardedDataCacherNotifier.ShardDataStoreCalled = func(cacheID string) storage.Cacher { + return testscommon.NewCacherMock() + } - assert.Nil(t, tx) - assert.Equal(t, process.ErrNilShardedDataCacherNotifier, err) + t.Run("nil sharded cache", func(t *testing.T) { + tx, err := process.GetTransactionHandlerFromPool( + 0, + 0, + hash, + nil, + process.SearchMethodJustPeek) + + assert.Nil(t, tx) + assert.Equal(t, process.ErrNilShardedDataCacherNotifier, err) + }) + t.Run("invalid method", func(t *testing.T) { + tx, err := process.GetTransactionHandlerFromPool( + 0, + 0, + hash, + shardedDataCacherNotifier, + 166) + + assert.Nil(t, tx) + assert.True(t, errors.Is(err, process.ErrInvalidValue)) + assert.True(t, strings.Contains(err.Error(), "invalid value")) + assert.True(t, strings.Contains(err.Error(), "166")) + }) + t.Run("nil cache", func(t *testing.T) { + shardedDataCacherNotifier.ShardDataStoreCalled = func(cacheID string) storage.Cacher { + return nil + } + + tx, err := process.GetTransactionHandlerFromPool( + 0, + 0, + hash, + shardedDataCacherNotifier, + process.SearchMethodJustPeek) + + assert.Nil(t, tx) + assert.Equal(t, process.ErrNilStorage, err) + }) } -func TestGetTransactionHandlerFromPoolShouldErrNilStorage(t *testing.T) { +func TestGetTransactionHandlerFromPoolShouldErrTxNotFound(t *testing.T) { + t.Parallel() + hash := []byte("X") shardedDataCacherNotifier := &testscommon.ShardedDataStub{ ShardDataStoreCalled: func(cacheId string) (c storage.Cacher) { - return nil + return &testscommon.CacherStub{ + PeekCalled: func(key []byte) (value interface{}, ok bool) { + return nil, false + }, + } }, } @@ -1731,20 +1934,22 @@ func TestGetTransactionHandlerFromPoolShouldErrNilStorage(t *testing.T) { 0, hash, shardedDataCacherNotifier, - false) + process.SearchMethodJustPeek) assert.Nil(t, tx) - assert.Equal(t, process.ErrNilStorage, err) + assert.Equal(t, process.ErrTxNotFound, err) } -func TestGetTransactionHandlerFromPoolShouldErrTxNotFound(t *testing.T) { +func TestGetTransactionHandlerFromPoolShouldErrInvalidTxInPool(t *testing.T) { + t.Parallel() + hash := []byte("X") shardedDataCacherNotifier := &testscommon.ShardedDataStub{ ShardDataStoreCalled: func(cacheId string) (c storage.Cacher) { return &testscommon.CacherStub{ PeekCalled: func(key []byte) (value interface{}, ok bool) { - return nil, false + return nil, true }, } }, @@ -1755,20 +1960,23 @@ func TestGetTransactionHandlerFromPoolShouldErrTxNotFound(t *testing.T) { 0, hash, shardedDataCacherNotifier, - false) + process.SearchMethodJustPeek) assert.Nil(t, tx) - assert.Equal(t, process.ErrTxNotFound, err) + assert.Equal(t, process.ErrInvalidTxInPool, err) } -func TestGetTransactionHandlerFromPoolShouldErrInvalidTxInPool(t *testing.T) { +func TestGetTransactionHandlerFromPoolShouldWorkWithPeek(t *testing.T) { + t.Parallel() + hash := []byte("X") + txFromPool := &transaction.Transaction{Nonce: 1} shardedDataCacherNotifier := &testscommon.ShardedDataStub{ ShardDataStoreCalled: func(cacheId string) (c storage.Cacher) { return &testscommon.CacherStub{ PeekCalled: func(key []byte) (value interface{}, ok bool) { - return nil, true + return txFromPool, true }, } }, @@ -1779,24 +1987,54 @@ func TestGetTransactionHandlerFromPoolShouldErrInvalidTxInPool(t *testing.T) { 0, hash, shardedDataCacherNotifier, - false) + process.SearchMethodJustPeek) - assert.Nil(t, tx) - assert.Equal(t, process.ErrInvalidTxInPool, err) + assert.Nil(t, err) + assert.Equal(t, txFromPool, tx) } -func TestGetTransactionHandlerFromPoolShouldWork(t *testing.T) { +func TestGetTransactionHandlerFromPoolShouldWorkWithSearchFirst(t *testing.T) { + t.Parallel() + hash := []byte("X") txFromPool := &transaction.Transaction{Nonce: 1} + shardedDataCacherNotifier := &testscommon.ShardedDataStub{ + SearchFirstDataCalled: func(key []byte) (value interface{}, ok bool) { + return txFromPool, true + }, + } + + tx, err := process.GetTransactionHandlerFromPool( + 0, + 0, + hash, + shardedDataCacherNotifier, + process.SearchMethodSearchFirst) + + assert.Nil(t, err) + assert.Equal(t, txFromPool, tx) +} + +func TestGetTransactionHandlerFromPoolShouldWorkWithPeekFallbackToSearchFirst(t *testing.T) { + t.Parallel() + + hash := []byte("X") + txFromPool := &transaction.Transaction{Nonce: 1} + + peekCalled := false shardedDataCacherNotifier := &testscommon.ShardedDataStub{ ShardDataStoreCalled: func(cacheId string) (c storage.Cacher) { return &testscommon.CacherStub{ PeekCalled: func(key []byte) (value interface{}, ok bool) { - return txFromPool, true + peekCalled = true + return nil, false }, } }, + SearchFirstDataCalled: func(key []byte) (value interface{}, ok bool) { + return txFromPool, true + }, } tx, err := process.GetTransactionHandlerFromPool( @@ -1804,13 +2042,16 @@ func TestGetTransactionHandlerFromPoolShouldWork(t *testing.T) { 0, hash, shardedDataCacherNotifier, - false) + process.SearchMethodPeekWithFallbackSearchFirst) assert.Nil(t, err) assert.Equal(t, txFromPool, tx) + assert.True(t, peekCalled) } func TestGetTransactionHandlerFromStorageShouldErrNilStorage(t *testing.T) { + t.Parallel() + hash := []byte("X") marshalizer := &mock.MarshalizerMock{} @@ -1825,6 +2066,8 @@ func TestGetTransactionHandlerFromStorageShouldErrNilStorage(t *testing.T) { } func TestGetTransactionHandlerFromStorageShouldErrNilMarshalizer(t *testing.T) { + t.Parallel() + hash := []byte("X") storageService := &mock.ChainStorerMock{} @@ -1839,6 +2082,8 @@ func TestGetTransactionHandlerFromStorageShouldErrNilMarshalizer(t *testing.T) { } func TestGetTransactionHandlerFromStorageShouldErrWhenTxIsNotFound(t *testing.T) { + t.Parallel() + hash := []byte("X") errExpected := errors.New("error") @@ -1859,6 +2104,8 @@ func TestGetTransactionHandlerFromStorageShouldErrWhenTxIsNotFound(t *testing.T) } func TestGetTransactionHandlerFromStorageShouldErrWhenUnmarshalFail(t *testing.T) { + t.Parallel() + hash := []byte("X") marshalizer := &mock.MarshalizerMock{} @@ -1878,6 +2125,8 @@ func TestGetTransactionHandlerFromStorageShouldErrWhenUnmarshalFail(t *testing.T } func TestGetTransactionHandlerFromStorageShouldWork(t *testing.T) { + t.Parallel() + hash := []byte("X") txFromPool := &transaction.Transaction{ Nonce: 1, @@ -1905,6 +2154,8 @@ func TestGetTransactionHandlerFromStorageShouldWork(t *testing.T) { } func TestSortHeadersByNonceShouldWork(t *testing.T) { + t.Parallel() + headers := []data.HeaderHandler{ &block.Header{Nonce: 3}, &block.Header{Nonce: 2}, @@ -1992,6 +2243,8 @@ func TestGetMiniBlockHeaderWithHash(t *testing.T) { } func TestCheckIfIndexesAreOutOfBound(t *testing.T) { + t.Parallel() + txHashes := [][]byte{[]byte("txHash1"), []byte("txHash2"), []byte("txHash3")} miniBlock := &block.MiniBlock{TxHashes: txHashes} @@ -2017,6 +2270,8 @@ func TestCheckIfIndexesAreOutOfBound(t *testing.T) { } func TestUnmarshalHeader(t *testing.T) { + t.Parallel() + marshalizer := &mock.MarshalizerMock{} shardHeaderV1 := &block.Header{Nonce: 42, EpochStartMetaHash: []byte{0xaa, 0xbb}} @@ -2028,6 +2283,8 @@ func TestUnmarshalHeader(t *testing.T) { metaHeaderBuffer, _ := marshalizer.Marshal(metaHeader) t.Run("should work", func(t *testing.T) { + t.Parallel() + header, err := process.UnmarshalHeader(1, marshalizer, shardHeaderV1Buffer) assert.Nil(t, err) assert.Equal(t, shardHeaderV1, header) @@ -2042,6 +2299,8 @@ func TestUnmarshalHeader(t *testing.T) { }) t.Run("should err", func(t *testing.T) { + t.Parallel() + header, err := process.UnmarshalHeader(1, marshalizer, []byte{0xb, 0xa, 0xd}) assert.NotNil(t, err) assert.Nil(t, header) @@ -2051,3 +2310,13 @@ func TestUnmarshalHeader(t *testing.T) { assert.Nil(t, header) }) } + +func TestShardedCacheSearchMethod_ToString(t *testing.T) { + t.Parallel() + + assert.Equal(t, "just peek", process.SearchMethodJustPeek.ToString()) + assert.Equal(t, "search first", process.SearchMethodSearchFirst.ToString()) + assert.Equal(t, "peek with fallback to search first", process.SearchMethodPeekWithFallbackSearchFirst.ToString()) + str := process.ShardedCacheSearchMethod(166).ToString() + assert.Equal(t, "unknown method 166", str) +}