From 23cf9324ccdaf1655fc44e733e50e4a2d86ff7bb Mon Sep 17 00:00:00 2001 From: barryz Date: Mon, 31 May 2021 16:37:02 +0800 Subject: [PATCH] created new api ContentByAccount in txpool namespace --- core/tx_pool.go | 21 +++++++++++++++++++++ eth/api_backend.go | 4 ++++ internal/ethapi/api.go | 25 +++++++++++++++++++++++++ internal/ethapi/backend.go | 1 + les/api_backend.go | 5 +++++ 5 files changed, 56 insertions(+) diff --git a/core/tx_pool.go b/core/tx_pool.go index 5db1d3df32..6e29bd5079 100644 --- a/core/tx_pool.go +++ b/core/tx_pool.go @@ -482,6 +482,27 @@ func (pool *TxPool) Content() (map[common.Address]types.Transactions, map[common return pending, queued } +// ContentByAccount retrieves the data content of the transaction pool with a specific account, +// returning the pending as well as queued transaction. +func (pool *TxPool) ContentByAccount(addr common.Address) (pending, queued types.Transactions) { + pool.mu.Lock() + defer pool.mu.Unlock() + + list, ok := pool.pending[addr] + if !ok { + return nil, nil + } + pending = list.Flatten() + + list, ok = pool.queue[addr] + if !ok { + return nil, nil + } + queued = list.Flatten() + + return +} + // Pending retrieves all currently processable transactions, grouped by origin // account and sorted by nonce. The returned transaction set is a copy and can be // freely modified by calling code. diff --git a/eth/api_backend.go b/eth/api_backend.go index 7ac1f82a86..028feb6c30 100644 --- a/eth/api_backend.go +++ b/eth/api_backend.go @@ -263,6 +263,10 @@ func (b *EthAPIBackend) TxPoolContent() (map[common.Address]types.Transactions, return b.eth.TxPool().Content() } +func (b *EthAPIBackend) TxPoolContentByAccount(addr common.Address) (pending, queued types.Transactions) { + return b.eth.TxPool().ContentByAccount(addr) +} + func (b *EthAPIBackend) TxPool() *core.TxPool { return b.eth.TxPool() } diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index fe3f80c038..e852fce4a5 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -99,6 +99,31 @@ func NewPublicTxPoolAPI(b Backend) *PublicTxPoolAPI { return &PublicTxPoolAPI{b} } +// ContentByAccount returns the transactions contained within the tx pool by given account address. +func (s *PublicTxPoolAPI) ContentByAccount(addr common.Address) map[string]map[string]*RPCTransaction { + // map[(queued|pending)]map[tx.Nonce()]tx + content := map[string]map[string]*RPCTransaction{ + "pending": make(map[string]*RPCTransaction), + "queued": make(map[string]*RPCTransaction), + } + + pending, queued := s.b.TxPoolContentByAccount(addr) + + dump := make(map[string]*RPCTransaction) + for _, tx := range pending { + dump[fmt.Sprintf("%d", tx.Nonce())] = newRPCPendingTransaction(tx) + } + content["pending"] = dump + + dump = make(map[string]*RPCTransaction) + for _, tx := range queued { + dump[fmt.Sprintf("%d", tx.Nonce())] = newRPCPendingTransaction(tx) + } + content["queued"] = dump + + return content +} + // Content returns the transactions contained within the transaction pool. func (s *PublicTxPoolAPI) Content() map[string]map[string]map[string]*RPCTransaction { content := map[string]map[string]map[string]*RPCTransaction{ diff --git a/internal/ethapi/backend.go b/internal/ethapi/backend.go index 07e76583f3..67b6e04e20 100644 --- a/internal/ethapi/backend.go +++ b/internal/ethapi/backend.go @@ -76,6 +76,7 @@ type Backend interface { GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) Stats() (pending int, queued int) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) + TxPoolContentByAccount(address common.Address) (pending, queued types.Transactions) SubscribeNewTxsEvent(chan<- core.NewTxsEvent) event.Subscription // Filter API diff --git a/les/api_backend.go b/les/api_backend.go index 60c64a8bdf..5779af52f1 100644 --- a/les/api_backend.go +++ b/les/api_backend.go @@ -212,6 +212,11 @@ func (b *LesApiBackend) TxPoolContent() (map[common.Address]types.Transactions, return b.eth.txPool.Content() } +func (b *LesApiBackend) TxPoolContentByAccount(addr common.Address) (pending, queued types.Transactions) { + // TODO(barryz) not implement for light node. + return nil, nil +} + func (b *LesApiBackend) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription { return b.eth.txPool.SubscribeNewTxsEvent(ch) }