Skip to content

Commit

Permalink
Merge pull request #4424 from ElrondNetwork/fix_duplicates_on_txpool_api
Browse files Browse the repository at this point in the history
Fix duplicated txs into tx pool api response
  • Loading branch information
gabi-vuls committed Aug 31, 2022
2 parents 2bd74ce + c65295e commit 8816bd7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 50 deletions.
35 changes: 8 additions & 27 deletions node/external/transactionAPI/apiTransactionProcessor.go
Expand Up @@ -21,7 +21,6 @@ import (
"github.com/ElrondNetwork/elrond-go/process"
"github.com/ElrondNetwork/elrond-go/process/txstatus"
"github.com/ElrondNetwork/elrond-go/sharding"
"github.com/ElrondNetwork/elrond-go/storage"
"github.com/ElrondNetwork/elrond-go/storage/txcache"
)

Expand Down Expand Up @@ -351,35 +350,17 @@ func (atp *apiTransactionProcessor) extractRequestedTxInfo(wrappedTx *txcache.Wr
return tx
}

func (atp *apiTransactionProcessor) getDataStoresForSender(senderShard uint32) []storage.Cacher {
cachers := make([]storage.Cacher, 0)
numOfShards := atp.shardCoordinator.NumberOfShards()
for shard := uint32(0); shard < numOfShards; shard++ {
cacheId := process.ShardCacherIdentifier(senderShard, shard)
shardCache := atp.dataPool.Transactions().ShardDataStore(cacheId)
cachers = append(cachers, shardCache)
}

cacheId := process.ShardCacherIdentifier(senderShard, common.MetachainShardId)
shardCache := atp.dataPool.Transactions().ShardDataStore(cacheId)
cachers = append(cachers, shardCache)

return cachers
}

func (atp *apiTransactionProcessor) fetchTxsForSender(sender string, senderShard uint32) []*txcache.WrappedTransaction {
txsForSender := make([]*txcache.WrappedTransaction, 0)
cachers := atp.getDataStoresForSender(senderShard)
for _, cache := range cachers {
txCache, ok := cache.(*txcache.TxCache)
if !ok {
continue
}

txs := txCache.GetTransactionsPoolForSender(sender)
txsForSender = append(txsForSender, txs...)
cacheId := process.ShardCacherIdentifier(senderShard, senderShard)
cache := atp.dataPool.Transactions().ShardDataStore(cacheId)
txCache, ok := cache.(*txcache.TxCache)
if !ok {
log.Warn("fetchTxsForSender could not cast to TxCache")
return nil
}

txsForSender := txCache.GetTransactionsPoolForSender(sender)

sort.Slice(txsForSender, func(i, j int) bool {
return txsForSender[i].Tx.GetNonce() < txsForSender[j].Tx.GetNonce()
})
Expand Down
30 changes: 7 additions & 23 deletions node/external/transactionAPI/apiTransactionProcessor_test.go
Expand Up @@ -797,7 +797,7 @@ func TestApiTransactionProcessor_GetTransactionsPoolForSender(t *testing.T) {

// if no tx is found in pool for a sender, it isn't an error, but return empty slice
newSender := "new-sender"
res, err = atp.GetTransactionsPoolForSender(newSender, "")
res, err = atp.GetTransactionsPoolForSender(newSender, "")
require.NoError(t, err)
require.Equal(t, &common.TransactionsPoolForSenderApiResponse{
Transactions: []common.Transaction{},
Expand All @@ -807,7 +807,7 @@ func TestApiTransactionProcessor_GetTransactionsPoolForSender(t *testing.T) {
func TestApiTransactionProcessor_GetLastPoolNonceForSender(t *testing.T) {
t.Parallel()

txHash0, txHash1, txHash2 := []byte("txHash0"), []byte("txHash1"), []byte("txHash2")
txHash0, txHash1, txHash2, txHash3, txHash4 := []byte("txHash0"), []byte("txHash1"), []byte("txHash2"), []byte("txHash3"), []byte("txHash4")
sender := "alice"
lastNonce := uint64(10)
txCacheIntraShard, _ := txcache.NewTxCache(txcache.ConfigSourceMe{
Expand All @@ -823,31 +823,15 @@ func TestApiTransactionProcessor_GetLastPoolNonceForSender(t *testing.T) {
txCacheIntraShard.AddTx(createTx(txHash2, sender, 3))
txCacheIntraShard.AddTx(createTx(txHash0, sender, 1))
txCacheIntraShard.AddTx(createTx(txHash1, sender, 2))

txHash3, txHash4 := []byte("txHash3"), []byte("txHash4")
txCacheWithMeta, _ := txcache.NewTxCache(txcache.ConfigSourceMe{
Name: "test-meta",
NumChunks: 4,
NumBytesPerSenderThreshold: 1_048_576, // 1 MB
CountPerSenderThreshold: math.MaxUint32,
}, &txcachemocks.TxGasHandlerMock{
MinimumGasMove: 1,
MinimumGasPrice: 1,
GasProcessingDivisor: 1,
})
txCacheWithMeta.AddTx(createTx(txHash3, sender, lastNonce))
txCacheWithMeta.AddTx(createTx(txHash4, sender, 5))
txCacheIntraShard.AddTx(createTx(txHash3, sender, lastNonce))
txCacheIntraShard.AddTx(createTx(txHash4, sender, 5))

args := createMockArgAPITransactionProcessor()
args.DataPool = &dataRetrieverMock.PoolsHolderStub{
TransactionsCalled: func() dataRetriever.ShardedDataCacherNotifier {
return &testscommon.ShardedDataStub{
ShardDataStoreCalled: func(cacheID string) storage.Cacher {
if len(cacheID) == 1 { // self shard
return txCacheIntraShard
}

return txCacheWithMeta
return txCacheIntraShard
},
}
},
Expand Down Expand Up @@ -962,11 +946,11 @@ func TestApiTransactionProcessor_GetTransactionsPoolNonceGapsForSender(t *testin

// if no tx is found in pool for a sender, it isn't an error, but return empty slice
newSender := "new-sender"
res, err = atp.GetTransactionsPoolNonceGapsForSender(newSender)
res, err = atp.GetTransactionsPoolNonceGapsForSender(newSender)
require.NoError(t, err)
require.Equal(t, &common.TransactionsPoolNonceGapsForSenderApiResponse{
Sender: newSender,
Gaps: []common.NonceGapApiResponse{},
Gaps: []common.NonceGapApiResponse{},
}, res)
}

Expand Down

0 comments on commit 8816bd7

Please sign in to comment.