Skip to content

Commit d3642b0

Browse files
committed
core, eth, rpc/api: rpc method to inspect the txpool queue
1 parent f85212a commit d3642b0

3 files changed

Lines changed: 113 additions & 1 deletion

File tree

‎core/transaction_pool.go‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,36 @@ func (pool *TxPool) Stats() (pending int, queued int) {
169169
return
170170
}
171171

172+
// Content retrieves the data content of the transaction pool, returning all the
173+
// pending as well as queued transactions, grouped by account and nonce.
174+
func (pool *TxPool) Content() (map[common.Address]map[uint64][]*types.Transaction, map[common.Address]map[uint64][]*types.Transaction) {
175+
pool.mu.RLock()
176+
defer pool.mu.RUnlock()
177+
178+
// Retrieve all the pending transactions and sort by account and by nonce
179+
pending := make(map[common.Address]map[uint64][]*types.Transaction)
180+
for _, tx := range pool.pending {
181+
account, _ := tx.From()
182+
183+
owned, ok := pending[account]
184+
if !ok {
185+
owned = make(map[uint64][]*types.Transaction)
186+
pending[account] = owned
187+
}
188+
owned[tx.Nonce()] = append(owned[tx.Nonce()], tx)
189+
}
190+
// Retrieve all the queued transactions and sort by account and by nonce
191+
queued := make(map[common.Address]map[uint64][]*types.Transaction)
192+
for account, txs := range pool.queue {
193+
owned := make(map[uint64][]*types.Transaction)
194+
for _, tx := range txs {
195+
owned[tx.Nonce()] = append(owned[tx.Nonce()], tx)
196+
}
197+
queued[account] = owned
198+
}
199+
return pending, queued
200+
}
201+
172202
// SetLocal marks a transaction as local, skipping gas price
173203
// check against local miner minimum in the future
174204
func (pool *TxPool) SetLocal(tx *types.Transaction) {

‎eth/api.go‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,39 @@ func NewPublicTxPoolAPI(e *Ethereum) *PublicTxPoolAPI {
227227
return &PublicTxPoolAPI{e}
228228
}
229229

230+
// Content returns the transactions contained within the transaction pool.
231+
func (s *PublicTxPoolAPI) Content() map[string]map[string]map[string][]*RPCTransaction {
232+
content := map[string]map[string]map[string][]*RPCTransaction{
233+
"pending": make(map[string]map[string][]*RPCTransaction),
234+
"queued": make(map[string]map[string][]*RPCTransaction),
235+
}
236+
pending, queue := s.e.TxPool().Content()
237+
238+
// Flatten the pending transactions
239+
for account, batches := range pending {
240+
dump := make(map[string][]*RPCTransaction)
241+
for nonce, txs := range batches {
242+
nonce := fmt.Sprintf("%d", nonce)
243+
for _, tx := range txs {
244+
dump[nonce] = append(dump[nonce], newRPCPendingTransaction(tx))
245+
}
246+
}
247+
content["pending"][account.Hex()] = dump
248+
}
249+
// Flatten the queued transactions
250+
for account, batches := range queue {
251+
dump := make(map[string][]*RPCTransaction)
252+
for nonce, txs := range batches {
253+
nonce := fmt.Sprintf("%d", nonce)
254+
for _, tx := range txs {
255+
dump[nonce] = append(dump[nonce], newRPCPendingTransaction(tx))
256+
}
257+
}
258+
content["queued"][account.Hex()] = dump
259+
}
260+
return content
261+
}
262+
230263
// Status returns the number of pending and queued transaction in the pool.
231264
func (s *PublicTxPoolAPI) Status() map[string]*rpc.HexNumber {
232265
pending, queue := s.e.TxPool().Stats()
@@ -236,6 +269,47 @@ func (s *PublicTxPoolAPI) Status() map[string]*rpc.HexNumber {
236269
}
237270
}
238271

272+
// Inspect retrieves the content of the transaction pool and flattens it into an
273+
// easily inspectable list.
274+
func (s *PublicTxPoolAPI) Inspect() map[string]map[string]map[string][]string {
275+
content := map[string]map[string]map[string][]string{
276+
"pending": make(map[string]map[string][]string),
277+
"queued": make(map[string]map[string][]string),
278+
}
279+
pending, queue := s.e.TxPool().Content()
280+
281+
// Define a formatter to flatten a transaction into a string
282+
var format = func(tx *types.Transaction) string {
283+
if to := tx.To(); to != nil {
284+
return fmt.Sprintf("%s: %v wei + %v × %v gas", tx.To().Hex(), tx.Value(), tx.Gas(), tx.GasPrice())
285+
}
286+
return fmt.Sprintf("contract creation: %v wei + %v × %v gas", tx.Value(), tx.Gas(), tx.GasPrice())
287+
}
288+
// Flatten the pending transactions
289+
for account, batches := range pending {
290+
dump := make(map[string][]string)
291+
for nonce, txs := range batches {
292+
nonce := fmt.Sprintf("%d", nonce)
293+
for _, tx := range txs {
294+
dump[nonce] = append(dump[nonce], format(tx))
295+
}
296+
}
297+
content["pending"][account.Hex()] = dump
298+
}
299+
// Flatten the queued transactions
300+
for account, batches := range queue {
301+
dump := make(map[string][]string)
302+
for nonce, txs := range batches {
303+
nonce := fmt.Sprintf("%d", nonce)
304+
for _, tx := range txs {
305+
dump[nonce] = append(dump[nonce], format(tx))
306+
}
307+
}
308+
content["queued"][account.Hex()] = dump
309+
}
310+
return content
311+
}
312+
239313
// PublicAccountAPI provides an API to access accounts managed by this node.
240314
// It offers only methods that can retrieve accounts.
241315
type PublicAccountAPI struct {

‎rpc/javascript.go‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,17 @@ web3._extend({
7070
],
7171
properties:
7272
[
73+
new web3._extend.Property({
74+
name: 'content',
75+
getter: 'txpool_content'
76+
}),
77+
new web3._extend.Property({
78+
name: 'inspect',
79+
getter: 'txpool_inspect'
80+
}),
7381
new web3._extend.Property({
7482
name: 'status',
75-
getter: 'txpool_status'
83+
getter: 'txpool_status',
7684
outputFormatter: function(status) {
7785
status.pending = web3._extend.utils.toDecimal(status.pending);
7886
status.queued = web3._extend.utils.toDecimal(status.queued);

0 commit comments

Comments
 (0)