// Copyright 2016 The go-ethereum Authors // This file is part of the go-ethereum library. // // The go-ethereum library is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // The go-ethereum library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public License // along with the go-ethereum library. If not, see . package ethapi import ( "fmt" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/rpc" ) type TxPool interface { Stats() (pending int, queued int) Content() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) } // TxPoolDebugAPI offers and API for the transaction pool. It only operates on data that // is non confidential. type TxPoolDebugAPI struct{ Pool TxPool } // Content returns the transactions contained within the transaction pool. func (s TxPoolDebugAPI) Content() map[string]map[string]map[string]*RPCTransaction { content := map[string]map[string]map[string]*RPCTransaction{ "pending": make(map[string]map[string]*RPCTransaction), "queued": make(map[string]map[string]*RPCTransaction), } pending, queue := s.Pool.Content() // Flatten the pending transactions for account, txs := range pending { dump := make(map[string]*RPCTransaction) for nonce, tx := range txs { dump[fmt.Sprintf("%d", nonce)] = newRPCTransaction(tx) } content["pending"][account.Hex()] = dump } // Flatten the queued transactions for account, txs := range queue { dump := make(map[string]*RPCTransaction) for nonce, tx := range txs { dump[fmt.Sprintf("%d", nonce)] = newRPCTransaction(tx) } content["queued"][account.Hex()] = dump } return content } // Status returns the number of pending and queued transaction in the pool. func (s TxPoolDebugAPI) Status() map[string]*rpc.HexNumber { pending, queue := s.Pool.Stats() return map[string]*rpc.HexNumber{ "pending": rpc.NewHexNumber(pending), "queued": rpc.NewHexNumber(queue), } } // Inspect retrieves the content of the transaction pool and flattens it into an // easily inspectable list. func (s TxPoolDebugAPI) Inspect() map[string]map[string]map[string]string { content := map[string]map[string]map[string]string{ "pending": make(map[string]map[string]string), "queued": make(map[string]map[string]string), } pending, queue := s.Pool.Content() // Define a formatter to flatten a transaction into a string var format = func(tx *types.Transaction) string { if to := tx.To(); to != nil { return fmt.Sprintf("%s: %v wei + %v × %v gas", tx.To().Hex(), tx.Value(), tx.Gas(), tx.GasPrice()) } return fmt.Sprintf("contract creation: %v wei + %v × %v gas", tx.Value(), tx.Gas(), tx.GasPrice()) } // Flatten the pending transactions for account, txs := range pending { dump := make(map[string]string) for nonce, tx := range txs { dump[fmt.Sprintf("%d", nonce)] = format(tx) } content["pending"][account.Hex()] = dump } // Flatten the queued transactions for account, txs := range queue { dump := make(map[string]string) for nonce, tx := range txs { dump[fmt.Sprintf("%d", nonce)] = format(tx) } content["queued"][account.Hex()] = dump } return content }