Skip to content

Commit

Permalink
fix unconfirmed tx to consider pending txs (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
codchen authored Dec 20, 2023
1 parent 3c6f69d commit 8f34e4c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
9 changes: 8 additions & 1 deletion internal/mempool/mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func (txmp *TxMempool) Unlock() {
// Size returns the number of valid transactions in the mempool. It is
// thread-safe.
func (txmp *TxMempool) Size() int {
return txmp.txStore.Size()
return txmp.txStore.Size() + txmp.pendingTxs.Size()
}

// SizeBytes return the total sum in bytes of all the valid transactions in the
Expand Down Expand Up @@ -437,6 +437,13 @@ func (txmp *TxMempool) ReapMaxTxs(max int) types.Txs {
for _, wtx := range wTxs {
txs = append(txs, wtx.tx)
}
if len(txs) < max {
// retrieve more from pending txs
pending := txmp.pendingTxs.Peek(max - len(txs))
for _, ptx := range pending {
txs = append(txs, ptx.tx.tx)
}
}
return txs
}

Expand Down
20 changes: 18 additions & 2 deletions internal/mempool/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ func (wtl *WrappedTxList) Remove(wtx *WrappedTx) {
}

type PendingTxs struct {
mtx *sync.Mutex
mtx *sync.RWMutex
txs []PendingTxInfo
}

Expand All @@ -306,7 +306,7 @@ type PendingTxInfo struct {

func NewPendingTxs() *PendingTxs {
return &PendingTxs{
mtx: &sync.Mutex{},
mtx: &sync.RWMutex{},
txs: []PendingTxInfo{},
}
}
Expand Down Expand Up @@ -356,3 +356,19 @@ func (p *PendingTxs) Insert(tx *WrappedTx, resCheckTx *abci.ResponseCheckTxV2, t
txInfo: txInfo,
})
}

func (p *PendingTxs) Peek(max int) []PendingTxInfo {
p.mtx.RLock()
defer p.mtx.RUnlock()
// priority is fifo
if max > len(p.txs) {
return p.txs
}
return p.txs[:max]
}

func (p *PendingTxs) Size() int {
p.mtx.RLock()
defer p.mtx.RUnlock()
return len(p.txs)
}
2 changes: 2 additions & 0 deletions internal/rpc/core/mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ func (env *Environment) BroadcastTxCommit(ctx context.Context, req *coretypes.Re
// More: https://docs.tendermint.com/master/rpc/#/Info/unconfirmed_txs
func (env *Environment) UnconfirmedTxs(ctx context.Context, req *coretypes.RequestUnconfirmedTxs) (*coretypes.ResultUnconfirmedTxs, error) {
totalCount := env.Mempool.Size()
fmt.Printf("EVMTEST mempool size: %d\n", totalCount)
perPage := env.validatePerPage(req.PerPage.IntPtr())
page, err := validatePage(req.Page.IntPtr(), perPage, totalCount)
if err != nil {
Expand All @@ -159,6 +160,7 @@ func (env *Environment) UnconfirmedTxs(ctx context.Context, req *coretypes.Reque
txs := env.Mempool.ReapMaxTxs(skipCount + tmmath.MinInt(perPage, totalCount-skipCount))
result := txs[skipCount:]

fmt.Printf("EVMTEST unconfirmed tx result count: %d, skipping %d\n", len(result), skipCount)
return &coretypes.ResultUnconfirmedTxs{
Count: len(result),
Total: totalCount,
Expand Down

0 comments on commit 8f34e4c

Please sign in to comment.