Skip to content

Commit c49e065

Browse files
markya0616markkaralabe
authored
internal: get pending and queued transaction by address (ethereum#22992)
* core, eth, internal, les, light: get pending and queued transaction by address * core: tiny nitpick fixes * light: tiny nitpick Co-authored-by: mark <[email protected]> Co-authored-by: Péter Szilágyi <[email protected]>
1 parent 846badc commit c49e065

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
@@ -494,6 +494,23 @@ func (pool *TxPool) Content() (map[common.Address]types.Transactions, map[common
494494
return pending, queued
495495
}
496496

497+
// ContentFrom retrieves the data content of the transaction pool, returning the
498+
// pending as well as queued transactions of this address, grouped by nonce.
499+
func (pool *TxPool) ContentFrom(addr common.Address) (types.Transactions, types.Transactions) {
500+
pool.mu.RLock()
501+
defer pool.mu.RUnlock()
502+
503+
var pending types.Transactions
504+
if list, ok := pool.pending[addr]; ok {
505+
pending = list.Flatten()
506+
}
507+
var queued types.Transactions
508+
if list, ok := pool.queue[addr]; ok {
509+
queued = list.Flatten()
510+
}
511+
return pending, queued
512+
}
513+
497514
// Pending retrieves all currently processable transactions, grouped by origin
498515
// account and sorted by nonce. The returned transaction set is a copy and can be
499516
// freely modified by calling code.

eth/api_backend.go

+4
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,10 @@ func (b *EthAPIBackend) TxPoolContent() (map[common.Address]types.Transactions,
267267
return b.eth.TxPool().Content()
268268
}
269269

270+
func (b *EthAPIBackend) TxPoolContentFrom(addr common.Address) (types.Transactions, types.Transactions) {
271+
return b.eth.TxPool().ContentFrom(addr)
272+
}
273+
270274
func (b *EthAPIBackend) TxPool() *core.TxPool {
271275
return b.eth.TxPool()
272276
}

internal/ethapi/api.go

+23
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,29 @@ func (s *PublicTxPoolAPI) Content() map[string]map[string]map[string]*RPCTransac
175175
return content
176176
}
177177

178+
// ContentFrom returns the transactions contained within the transaction pool.
179+
func (s *PublicTxPoolAPI) ContentFrom(addr common.Address) map[string]map[string]*RPCTransaction {
180+
content := make(map[string]map[string]*RPCTransaction, 2)
181+
pending, queue := s.b.TxPoolContentFrom(addr)
182+
curHeader := s.b.CurrentHeader()
183+
184+
// Build the pending transactions
185+
dump := make(map[string]*RPCTransaction, len(pending))
186+
for _, tx := range pending {
187+
dump[fmt.Sprintf("%d", tx.Nonce())] = newRPCPendingTransaction(tx, curHeader, s.b.ChainConfig())
188+
}
189+
content["pending"] = dump
190+
191+
// Build the queued transactions
192+
dump = make(map[string]*RPCTransaction, len(queue))
193+
for _, tx := range queue {
194+
dump[fmt.Sprintf("%d", tx.Nonce())] = newRPCPendingTransaction(tx, curHeader, s.b.ChainConfig())
195+
}
196+
content["queued"] = dump
197+
198+
return content
199+
}
200+
178201
// Status returns the number of pending and queued transaction in the pool.
179202
func (s *PublicTxPoolAPI) Status() map[string]hexutil.Uint {
180203
pending, queue := s.b.Stats()

internal/ethapi/backend.go

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ type Backend interface {
7777
GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error)
7878
Stats() (pending int, queued int)
7979
TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions)
80+
TxPoolContentFrom(addr common.Address) (types.Transactions, types.Transactions)
8081
SubscribeNewTxsEvent(chan<- core.NewTxsEvent) event.Subscription
8182

8283
// Filter API

internal/web3ext/web3ext.go

+5
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,11 @@ web3._extend({
810810
return status;
811811
}
812812
}),
813+
new web3._extend.Method({
814+
name: 'contentFrom',
815+
call: 'txpool_contentFrom',
816+
params: 1,
817+
}),
813818
]
814819
});
815820
`

les/api_backend.go

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

222+
func (b *LesApiBackend) TxPoolContentFrom(addr common.Address) (types.Transactions, types.Transactions) {
223+
return b.eth.txPool.ContentFrom(addr)
224+
}
225+
222226
func (b *LesApiBackend) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription {
223227
return b.eth.txPool.SubscribeNewTxsEvent(ch)
224228
}

light/txpool.go

+19
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,25 @@ func (pool *TxPool) Content() (map[common.Address]types.Transactions, map[common
505505
return pending, queued
506506
}
507507

508+
// ContentFrom retrieves the data content of the transaction pool, returning the
509+
// pending as well as queued transactions of this address, grouped by nonce.
510+
func (pool *TxPool) ContentFrom(addr common.Address) (types.Transactions, types.Transactions) {
511+
pool.mu.RLock()
512+
defer pool.mu.RUnlock()
513+
514+
// Retrieve the pending transactions and sort by nonce
515+
var pending types.Transactions
516+
for _, tx := range pool.pending {
517+
account, _ := types.Sender(pool.signer, tx)
518+
if account != addr {
519+
continue
520+
}
521+
pending = append(pending, tx)
522+
}
523+
// There are no queued transactions in a light pool, just return an empty map
524+
return pending, types.Transactions{}
525+
}
526+
508527
// RemoveTransactions removes all given transactions from the pool.
509528
func (pool *TxPool) RemoveTransactions(txs types.Transactions) {
510529
pool.mu.Lock()

0 commit comments

Comments
 (0)