Skip to content

Commit 82b7b36

Browse files
author
mark
committed
core, eth, internal, les, light: get pending and queued transaction by address
1 parent 216ed05 commit 82b7b36

File tree

7 files changed

+73
-0
lines changed

7 files changed

+73
-0
lines changed

core/tx_pool.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,23 @@ func (pool *TxPool) Content() (map[common.Address]types.Transactions, map[common
493493
return pending, queued
494494
}
495495

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

eth/api_backend.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,10 @@ func (b *EthAPIBackend) TxPoolContent() (map[common.Address]types.Transactions,
263263
return b.eth.TxPool().Content()
264264
}
265265

266+
func (b *EthAPIBackend) TxPoolContentFrom(addr common.Address) (types.Transactions, types.Transactions) {
267+
return b.eth.TxPool().ContentFrom(addr)
268+
}
269+
266270
func (b *EthAPIBackend) TxPool() *core.TxPool {
267271
return b.eth.TxPool()
268272
}

internal/ethapi/api.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,29 @@ func (s *PublicTxPoolAPI) Content() map[string]map[string]map[string]*RPCTransac
141141
return content
142142
}
143143

144+
// ContentFrom returns the transactions contained within the transaction pool.
145+
func (s *PublicTxPoolAPI) ContentFrom(addr common.Address) map[string]map[string]*RPCTransaction {
146+
content := make(map[string]map[string]*RPCTransaction, 2)
147+
pending, queue := s.b.TxPoolContentFrom(addr)
148+
curHeader := s.b.CurrentHeader()
149+
150+
// Build the pending transactions
151+
dump := make(map[string]*RPCTransaction, len(pending))
152+
for _, tx := range pending {
153+
dump[fmt.Sprintf("%d", tx.Nonce())] = newRPCPendingTransaction(tx, curHeader, s.b.ChainConfig())
154+
}
155+
content["pending"] = dump
156+
157+
// Build the queued transactions
158+
dump = make(map[string]*RPCTransaction, len(queue))
159+
for _, tx := range queue {
160+
dump[fmt.Sprintf("%d", tx.Nonce())] = newRPCPendingTransaction(tx, curHeader, s.b.ChainConfig())
161+
}
162+
content["queued"] = dump
163+
164+
return content
165+
}
166+
144167
// Status returns the number of pending and queued transaction in the pool.
145168
func (s *PublicTxPoolAPI) Status() map[string]hexutil.Uint {
146169
pending, queue := s.b.Stats()

internal/ethapi/backend.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ type Backend interface {
7676
GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error)
7777
Stats() (pending int, queued int)
7878
TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions)
79+
TxPoolContentFrom(addr common.Address) (types.Transactions, types.Transactions)
7980
SubscribeNewTxsEvent(chan<- core.NewTxsEvent) event.Subscription
8081

8182
// Filter API

internal/web3ext/web3ext.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,11 @@ web3._extend({
798798
return status;
799799
}
800800
}),
801+
new web3._extend.Method({
802+
name: 'contentFrom',
803+
call: 'txpool_contentFrom',
804+
params: 1,
805+
}),
801806
]
802807
});
803808
`

les/api_backend.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,10 @@ func (b *LesApiBackend) TxPoolContent() (map[common.Address]types.Transactions,
212212
return b.eth.txPool.Content()
213213
}
214214

215+
func (b *LesApiBackend) TxPoolContentFrom(addr common.Address) (types.Transactions, types.Transactions) {
216+
return b.eth.txPool.ContentFrom(addr)
217+
}
218+
215219
func (b *LesApiBackend) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription {
216220
return b.eth.txPool.SubscribeNewTxsEvent(ch)
217221
}

light/txpool.go

Lines changed: 19 additions & 0 deletions
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+
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)