Skip to content

Commit

Permalink
Respect shardId in mempool syncing (#805)
Browse files Browse the repository at this point in the history
  • Loading branch information
sidenaio authored Oct 1, 2021
1 parent c32e5de commit 4b9adcb
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 12 deletions.
2 changes: 1 addition & 1 deletion api/blockchain_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func (api *BlockchainApi) TxReceipt(hash common.Hash) *TxReceipt {
}

func (api *BlockchainApi) Mempool() []common.Hash {
pending := api.pool.GetPendingTransaction(true, false)
pending := api.pool.GetPendingTransaction(true, common.MultiShard, false)

var txs []common.Hash
for _, tx := range pending {
Expand Down
5 changes: 3 additions & 2 deletions core/mempool/async_txpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"github.com/idena-network/idena-go/blockchain/types"
"github.com/idena-network/idena-go/common"
)

const batchSize = 1000
Expand Down Expand Up @@ -45,8 +46,8 @@ func (pool *AsyncTxPool) AddExternalTxs(txs ...*types.Transaction) error {
return nil
}

func (pool *AsyncTxPool) GetPendingTransaction(noFilter bool, count bool) []*types.Transaction {
return pool.txPool.GetPendingTransaction(noFilter, count)
func (pool *AsyncTxPool) GetPendingTransaction(noFilter bool, id common.ShardId, count bool) []*types.Transaction {
return pool.txPool.GetPendingTransaction(noFilter, id, count)
}

func (pool *AsyncTxPool) loop() {
Expand Down
12 changes: 7 additions & 5 deletions core/mempool/txpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var (
type TransactionPool interface {
AddInternalTx(tx *types.Transaction) error
AddExternalTxs(txs ...*types.Transaction) error
GetPendingTransaction(noFilter bool, count bool) []*types.Transaction
GetPendingTransaction(noFilter bool, id common.ShardId, count bool) []*types.Transaction
IsSyncing() bool
}

Expand Down Expand Up @@ -399,7 +399,7 @@ func (pool *TxPool) put(tx *types.Transaction) error {
return nil
}

func (pool *TxPool) GetPendingTransaction(noFilter bool, count bool) []*types.Transaction {
func (pool *TxPool) GetPendingTransaction(noFilter bool, shardId common.ShardId, count bool) []*types.Transaction {
all := pool.all.List()
pool.mutex.Lock()
defer pool.mutex.Unlock()
Expand All @@ -410,9 +410,11 @@ func (pool *TxPool) GetPendingTransaction(noFilter bool, count bool) []*types.Tr
}
for _, tx := range all {
if noFilter || pool.txSyncCounts[tx.Hash()] <= maxTxSyncCounts {
result = append(result, tx)
if count {
pool.txSyncCounts[tx.Hash()] ++
if shardId == common.MultiShard || tx.LoadShardId() == shardId {
result = append(result, tx)
if count {
pool.txSyncCounts[tx.Hash()]++
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/mempool/txpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ func TestTxPool_AddWithTxKeeper(t *testing.T) {
time.Sleep(time.Second)
require.Len(t, pool.txKeeper.txs, 320)

pool.txKeeper.RemoveTxs([]common.Hash{pool.GetPendingTransaction(false, false)[0].Hash()})
pool.txKeeper.RemoveTxs([]common.Hash{pool.GetPendingTransaction(false, common.MultiShard, false)[0].Hash()})
time.Sleep(time.Second)

prevPool := pool
Expand Down
2 changes: 1 addition & 1 deletion deferredtx/job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (f *fakeTxPool) AddExternalTxs(txs ...*types.Transaction) error {
panic("implement me")
}

func (f fakeTxPool) GetPendingTransaction(bool, bool) []*types.Transaction {
func (f fakeTxPool) GetPendingTransaction(bool, common.ShardId, bool) []*types.Transaction {
panic("implement me")
}

Expand Down
2 changes: 1 addition & 1 deletion protocol/gossip.go
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,7 @@ func (h *IdenaGossipHandler) RequestBlockByHash(hash common.Hash) {

func (h *IdenaGossipHandler) syncTxPool(p *protoPeer) {
const maximalPeersNumberForFullSync = 3
pending := h.txpool.GetPendingTransaction(p.peers <= maximalPeersNumberForFullSync, true)
pending := h.txpool.GetPendingTransaction(p.peers <= maximalPeersNumberForFullSync, p.shardId, true)
for _, tx := range pending {
payload := pushPullHash{
Type: pushTx,
Expand Down
2 changes: 1 addition & 1 deletion tests/txpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ func TestTxPool_ResetTo(t *testing.T) {
},
})

txs := pool.GetPendingTransaction(true, false)
txs := pool.GetPendingTransaction(true, common.MultiShard, false)

require.Equal(4, len(txs))
require.Contains(txs, tx1)
Expand Down

0 comments on commit 4b9adcb

Please sign in to comment.