Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect shardId in mempool syncing #805

Merged
merged 3 commits into from
Oct 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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