Skip to content

Commit d33d3cb

Browse files
committed
internal: get pending and queued transaction by address (ethereum#22992)
1 parent c4af3e7 commit d33d3cb

File tree

7 files changed

+73
-0
lines changed

7 files changed

+73
-0
lines changed

core/tx_pool.go

+17
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,23 @@ func (pool *TxPool) Content() (map[common.Address]types.Transactions, map[common
535535
return pending, queued
536536
}
537537

538+
// ContentFrom retrieves the data content of the transaction pool, returning the
539+
// pending as well as queued transactions of this address, grouped by nonce.
540+
func (pool *TxPool) ContentFrom(addr common.Address) (types.Transactions, types.Transactions) {
541+
pool.mu.RLock()
542+
defer pool.mu.RUnlock()
543+
544+
var pending types.Transactions
545+
if list, ok := pool.pending[addr]; ok {
546+
pending = list.Flatten()
547+
}
548+
var queued types.Transactions
549+
if list, ok := pool.queue[addr]; ok {
550+
queued = list.Flatten()
551+
}
552+
return pending, queued
553+
}
554+
538555
// Pending retrieves all currently processable transactions, grouped by origin
539556
// account and sorted by nonce. The returned transaction set is a copy and can be
540557
// freely modified by calling code.

eth/api_backend.go

+4
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,10 @@ func (b *EthApiBackend) TxPoolContent() (map[common.Address]types.Transactions,
328328
return b.eth.TxPool().Content()
329329
}
330330

331+
func (b *EthApiBackend) TxPoolContentFrom(addr common.Address) (types.Transactions, types.Transactions) {
332+
return b.eth.TxPool().ContentFrom(addr)
333+
}
334+
331335
func (b *EthApiBackend) OrderTxPoolContent() (map[common.Address]types.OrderTransactions, map[common.Address]types.OrderTransactions) {
332336
return b.eth.OrderPool().Content()
333337
}

internal/ethapi/api.go

+23
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,29 @@ func (s *PublicTxPoolAPI) Content() map[string]map[string]map[string]*RPCTransac
202202
return content
203203
}
204204

205+
// ContentFrom returns the transactions contained within the transaction pool.
206+
func (s *PublicTxPoolAPI) ContentFrom(addr common.Address) map[string]map[string]*RPCTransaction {
207+
content := make(map[string]map[string]*RPCTransaction, 2)
208+
pending, queue := s.b.TxPoolContentFrom(addr)
209+
curHeader := s.b.CurrentHeader()
210+
211+
// Build the pending transactions
212+
dump := make(map[string]*RPCTransaction, len(pending))
213+
for _, tx := range pending {
214+
dump[fmt.Sprintf("%d", tx.Nonce())] = newRPCPendingTransaction(tx, curHeader, s.b.ChainConfig())
215+
}
216+
content["pending"] = dump
217+
218+
// Build the queued transactions
219+
dump = make(map[string]*RPCTransaction, len(queue))
220+
for _, tx := range queue {
221+
dump[fmt.Sprintf("%d", tx.Nonce())] = newRPCPendingTransaction(tx, curHeader, s.b.ChainConfig())
222+
}
223+
content["queued"] = dump
224+
225+
return content
226+
}
227+
205228
// Status returns the number of pending and queued transaction in the pool.
206229
func (s *PublicTxPoolAPI) Status() map[string]hexutil.Uint {
207230
pending, queue := s.b.Stats()

internal/ethapi/backend.go

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ type Backend interface {
8484
GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error)
8585
Stats() (pending int, queued int)
8686
TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions)
87+
TxPoolContentFrom(addr common.Address) (types.Transactions, types.Transactions)
8788
SubscribeNewTxsEvent(chan<- core.NewTxsEvent) event.Subscription
8889

8990
// Order Pool Transaction

internal/web3ext/web3ext.go

+5
Original file line numberDiff line numberDiff line change
@@ -1066,6 +1066,11 @@ web3._extend({
10661066
return status;
10671067
}
10681068
}),
1069+
new web3._extend.Method({
1070+
name: 'contentFrom',
1071+
call: 'txpool_contentFrom',
1072+
params: 1,
1073+
}),
10691074
]
10701075
});
10711076
`

les/api_backend.go

+4
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,10 @@ func (b *LesApiBackend) TxPoolContent() (map[common.Address]types.Transactions,
225225
return b.eth.txPool.Content()
226226
}
227227

228+
func (b *LesApiBackend) TxPoolContentFrom(addr common.Address) (types.Transactions, types.Transactions) {
229+
return b.eth.txPool.ContentFrom(addr)
230+
}
231+
228232
func (b *LesApiBackend) OrderTxPoolContent() (map[common.Address]types.OrderTransactions, map[common.Address]types.OrderTransactions) {
229233
return make(map[common.Address]types.OrderTransactions), make(map[common.Address]types.OrderTransactions)
230234
}

light/txpool.go

+19
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,25 @@ func (self *TxPool) Content() (map[common.Address]types.Transactions, map[common
530530
return pending, queued
531531
}
532532

533+
// ContentFrom retrieves the data content of the transaction pool, returning the
534+
// pending as well as queued transactions of this address, grouped by nonce.
535+
func (pool *TxPool) ContentFrom(addr common.Address) (types.Transactions, types.Transactions) {
536+
pool.mu.RLock()
537+
defer pool.mu.RUnlock()
538+
539+
// Retrieve the pending transactions and sort by nonce
540+
var pending types.Transactions
541+
for _, tx := range pool.pending {
542+
account, _ := types.Sender(pool.signer, tx)
543+
if account != addr {
544+
continue
545+
}
546+
pending = append(pending, tx)
547+
}
548+
// There are no queued transactions in a light pool, just return an empty map
549+
return pending, types.Transactions{}
550+
}
551+
533552
// RemoveTransactions removes all given transactions from the pool.
534553
func (self *TxPool) RemoveTransactions(txs types.Transactions) {
535554
self.mu.Lock()

0 commit comments

Comments
 (0)